1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2015, 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;
36 with Freeze; use Freeze;
38 with Lib.Xref; use Lib.Xref;
39 with Namet; use Namet;
40 with Nlists; use Nlists;
41 with Nmake; use Nmake;
43 with Restrict; use Restrict;
44 with Rident; use Rident;
45 with Rtsfind; use Rtsfind;
47 with Sem_Aux; use Sem_Aux;
48 with Sem_Case; use Sem_Case;
49 with Sem_Ch3; use Sem_Ch3;
50 with Sem_Ch6; use Sem_Ch6;
51 with Sem_Ch8; use Sem_Ch8;
52 with Sem_Dim; use Sem_Dim;
53 with Sem_Disp; use Sem_Disp;
54 with Sem_Eval; use Sem_Eval;
55 with Sem_Prag; use Sem_Prag;
56 with Sem_Res; use Sem_Res;
57 with Sem_Type; use Sem_Type;
58 with Sem_Util; use Sem_Util;
59 with Sem_Warn; use Sem_Warn;
60 with Sinput; use Sinput;
61 with Snames; use Snames;
62 with Stand; use Stand;
63 with Sinfo; use Sinfo;
64 with Stringt; use Stringt;
65 with Targparm; use Targparm;
66 with Ttypes; use Ttypes;
67 with Tbuild; use Tbuild;
68 with Urealp; use Urealp;
69 with Warnsw; use Warnsw;
71 with GNAT.Heap_Sort_G;
73 package body Sem_Ch13 is
75 SSU : constant Pos := System_Storage_Unit;
76 -- Convenient short hand for commonly used constant
78 -----------------------
79 -- Local Subprograms --
80 -----------------------
82 procedure Alignment_Check_For_Size_Change (Typ : Entity_Id; Size : Uint);
83 -- This routine is called after setting one of the sizes of type entity
84 -- Typ to Size. The purpose is to deal with the situation of a derived
85 -- type whose inherited alignment is no longer appropriate for the new
86 -- size value. In this case, we reset the Alignment to unknown.
88 procedure Build_Discrete_Static_Predicate
92 -- Given a predicated type Typ, where Typ is a discrete static subtype,
93 -- whose predicate expression is Expr, tests if Expr is a static predicate,
94 -- and if so, builds the predicate range list. Nam is the name of the one
95 -- argument to the predicate function. Occurrences of the type name in the
96 -- predicate expression have been replaced by identifier references to this
97 -- name, which is unique, so any identifier with Chars matching Nam must be
98 -- a reference to the type. If the predicate is non-static, this procedure
99 -- returns doing nothing. If the predicate is static, then the predicate
100 -- list is stored in Static_Discrete_Predicate (Typ), and the Expr is
101 -- rewritten as a canonicalized membership operation.
103 procedure Build_Predicate_Functions (Typ : Entity_Id; N : Node_Id);
104 -- If Typ has predicates (indicated by Has_Predicates being set for Typ),
105 -- then either there are pragma Predicate entries on the rep chain for the
106 -- type (note that Predicate aspects are converted to pragma Predicate), or
107 -- there are inherited aspects from a parent type, or ancestor subtypes.
108 -- This procedure builds the spec and body for the Predicate function that
109 -- tests these predicates. N is the freeze node for the type. The spec of
110 -- the function is inserted before the freeze node, and the body of the
111 -- function is inserted after the freeze node. If the predicate expression
112 -- has at least one Raise_Expression, then this procedure also builds the
113 -- M version of the predicate function for use in membership tests.
115 procedure Check_Pool_Size_Clash (Ent : Entity_Id; SP, SS : Node_Id);
116 -- Called if both Storage_Pool and Storage_Size attribute definition
117 -- clauses (SP and SS) are present for entity Ent. Issue error message.
119 procedure Freeze_Entity_Checks (N : Node_Id);
120 -- Called from Analyze_Freeze_Entity and Analyze_Generic_Freeze Entity
121 -- to generate appropriate semantic checks that are delayed until this
122 -- point (they had to be delayed this long for cases of delayed aspects,
123 -- e.g. analysis of statically predicated subtypes in choices, for which
124 -- we have to be sure the subtypes in question are frozen before checking.
126 function Get_Alignment_Value (Expr : Node_Id) return Uint;
127 -- Given the expression for an alignment value, returns the corresponding
128 -- Uint value. If the value is inappropriate, then error messages are
129 -- posted as required, and a value of No_Uint is returned.
131 function Is_Operational_Item (N : Node_Id) return Boolean;
132 -- A specification for a stream attribute is allowed before the full type
133 -- is declared, as explained in AI-00137 and the corrigendum. Attributes
134 -- that do not specify a representation characteristic are operational
137 function Is_Predicate_Static
139 Nam : Name_Id) return Boolean;
140 -- Given predicate expression Expr, tests if Expr is predicate-static in
141 -- the sense of the rules in (RM 3.2.4 (15-24)). Occurrences of the type
142 -- name in the predicate expression have been replaced by references to
143 -- an identifier whose Chars field is Nam. This name is unique, so any
144 -- identifier with Chars matching Nam must be a reference to the type.
145 -- Returns True if the expression is predicate-static and False otherwise,
146 -- but is not in the business of setting flags or issuing error messages.
148 -- Only scalar types can have static predicates, so False is always
149 -- returned for non-scalar types.
151 -- Note: the RM seems to suggest that string types can also have static
152 -- predicates. But that really makes lttle sense as very few useful
153 -- predicates can be constructed for strings. Remember that:
157 -- is not a static expression. So even though the clearly faulty RM wording
158 -- allows the following:
160 -- subtype S is String with Static_Predicate => S < "DEF"
162 -- We can't allow this, otherwise we have predicate-static applying to a
163 -- larger class than static expressions, which was never intended.
165 procedure New_Stream_Subprogram
169 Nam : TSS_Name_Type);
170 -- Create a subprogram renaming of a given stream attribute to the
171 -- designated subprogram and then in the tagged case, provide this as a
172 -- primitive operation, or in the untagged case make an appropriate TSS
173 -- entry. This is more properly an expansion activity than just semantics,
174 -- but the presence of user-defined stream functions for limited types
175 -- is a legality check, which is why this takes place here rather than in
176 -- exp_ch13, where it was previously. Nam indicates the name of the TSS
177 -- function to be generated.
179 -- To avoid elaboration anomalies with freeze nodes, for untagged types
180 -- we generate both a subprogram declaration and a subprogram renaming
181 -- declaration, so that the attribute specification is handled as a
182 -- renaming_as_body. For tagged types, the specification is one of the
185 procedure Resolve_Iterable_Operation
190 -- If the name of a primitive operation for an Iterable aspect is
191 -- overloaded, resolve according to required signature.
197 Biased : Boolean := True);
198 -- If Biased is True, sets Has_Biased_Representation flag for E, and
199 -- outputs a warning message at node N if Warn_On_Biased_Representation is
200 -- is True. This warning inserts the string Msg to describe the construct
203 ----------------------------------------------
204 -- Table for Validate_Unchecked_Conversions --
205 ----------------------------------------------
207 -- The following table collects unchecked conversions for validation.
208 -- Entries are made by Validate_Unchecked_Conversion and then the call
209 -- to Validate_Unchecked_Conversions does the actual error checking and
210 -- posting of warnings. The reason for this delayed processing is to take
211 -- advantage of back-annotations of size and alignment values performed by
214 -- Note: the reason we store a Source_Ptr value instead of a Node_Id is
215 -- that by the time Validate_Unchecked_Conversions is called, Sprint will
216 -- already have modified all Sloc values if the -gnatD option is set.
218 type UC_Entry is record
219 Eloc : Source_Ptr; -- node used for posting warnings
220 Source : Entity_Id; -- source type for unchecked conversion
221 Target : Entity_Id; -- target type for unchecked conversion
222 Act_Unit : Entity_Id; -- actual function instantiated
225 package Unchecked_Conversions is new Table.Table (
226 Table_Component_Type => UC_Entry,
227 Table_Index_Type => Int,
228 Table_Low_Bound => 1,
230 Table_Increment => 200,
231 Table_Name => "Unchecked_Conversions");
233 ----------------------------------------
234 -- Table for Validate_Address_Clauses --
235 ----------------------------------------
237 -- If an address clause has the form
239 -- for X'Address use Expr
241 -- where Expr is of the form Y'Address or recursively is a reference to a
242 -- constant of either of these forms, and X and Y are entities of objects,
243 -- then if Y has a smaller alignment than X, that merits a warning about
244 -- possible bad alignment. The following table collects address clauses of
245 -- this kind. We put these in a table so that they can be checked after the
246 -- back end has completed annotation of the alignments of objects, since we
247 -- can catch more cases that way.
249 type Address_Clause_Check_Record is record
251 -- The address clause
254 -- The entity of the object overlaying Y
257 -- The entity of the object being overlaid
260 -- Whether the address is offset within Y
263 package Address_Clause_Checks is new Table.Table (
264 Table_Component_Type => Address_Clause_Check_Record,
265 Table_Index_Type => Int,
266 Table_Low_Bound => 1,
268 Table_Increment => 200,
269 Table_Name => "Address_Clause_Checks");
271 -----------------------------------------
272 -- Adjust_Record_For_Reverse_Bit_Order --
273 -----------------------------------------
275 procedure Adjust_Record_For_Reverse_Bit_Order (R : Entity_Id) is
280 -- Processing depends on version of Ada
282 -- For Ada 95, we just renumber bits within a storage unit. We do the
283 -- same for Ada 83 mode, since we recognize the Bit_Order attribute in
284 -- Ada 83, and are free to add this extension.
286 if Ada_Version < Ada_2005 then
287 Comp := First_Component_Or_Discriminant (R);
288 while Present (Comp) loop
289 CC := Component_Clause (Comp);
291 -- If component clause is present, then deal with the non-default
292 -- bit order case for Ada 95 mode.
294 -- We only do this processing for the base type, and in fact that
295 -- is important, since otherwise if there are record subtypes, we
296 -- could reverse the bits once for each subtype, which is wrong.
298 if Present (CC) and then Ekind (R) = E_Record_Type then
300 CFB : constant Uint := Component_Bit_Offset (Comp);
301 CSZ : constant Uint := Esize (Comp);
302 CLC : constant Node_Id := Component_Clause (Comp);
303 Pos : constant Node_Id := Position (CLC);
304 FB : constant Node_Id := First_Bit (CLC);
306 Storage_Unit_Offset : constant Uint :=
307 CFB / System_Storage_Unit;
309 Start_Bit : constant Uint :=
310 CFB mod System_Storage_Unit;
313 -- Cases where field goes over storage unit boundary
315 if Start_Bit + CSZ > System_Storage_Unit then
317 -- Allow multi-byte field but generate warning
319 if Start_Bit mod System_Storage_Unit = 0
320 and then CSZ mod System_Storage_Unit = 0
323 ("info: multi-byte field specified with "
324 & "non-standard Bit_Order?V?", CLC);
326 if Bytes_Big_Endian then
328 ("\bytes are not reversed "
329 & "(component is big-endian)?V?", CLC);
332 ("\bytes are not reversed "
333 & "(component is little-endian)?V?", CLC);
336 -- Do not allow non-contiguous field
340 ("attempt to specify non-contiguous field "
341 & "not permitted", CLC);
343 ("\caused by non-standard Bit_Order "
346 ("\consider possibility of using "
347 & "Ada 2005 mode here", CLC);
350 -- Case where field fits in one storage unit
353 -- Give warning if suspicious component clause
355 if Intval (FB) >= System_Storage_Unit
356 and then Warn_On_Reverse_Bit_Order
359 ("info: Bit_Order clause does not affect " &
360 "byte ordering?V?", Pos);
362 Intval (Pos) + Intval (FB) /
365 ("info: position normalized to ^ before bit " &
366 "order interpreted?V?", Pos);
369 -- Here is where we fix up the Component_Bit_Offset value
370 -- to account for the reverse bit order. Some examples of
371 -- what needs to be done are:
373 -- First_Bit .. Last_Bit Component_Bit_Offset
385 -- The rule is that the first bit is is obtained by
386 -- subtracting the old ending bit from storage_unit - 1.
388 Set_Component_Bit_Offset
390 (Storage_Unit_Offset * System_Storage_Unit) +
391 (System_Storage_Unit - 1) -
392 (Start_Bit + CSZ - 1));
394 Set_Normalized_First_Bit
396 Component_Bit_Offset (Comp) mod
397 System_Storage_Unit);
402 Next_Component_Or_Discriminant (Comp);
405 -- For Ada 2005, we do machine scalar processing, as fully described In
406 -- AI-133. This involves gathering all components which start at the
407 -- same byte offset and processing them together. Same approach is still
408 -- valid in later versions including Ada 2012.
412 Max_Machine_Scalar_Size : constant Uint :=
414 (Standard_Long_Long_Integer_Size);
415 -- We use this as the maximum machine scalar size
418 SSU : constant Uint := UI_From_Int (System_Storage_Unit);
421 -- This first loop through components does two things. First it
422 -- deals with the case of components with component clauses whose
423 -- length is greater than the maximum machine scalar size (either
424 -- accepting them or rejecting as needed). Second, it counts the
425 -- number of components with component clauses whose length does
426 -- not exceed this maximum for later processing.
429 Comp := First_Component_Or_Discriminant (R);
430 while Present (Comp) loop
431 CC := Component_Clause (Comp);
435 Fbit : constant Uint := Static_Integer (First_Bit (CC));
436 Lbit : constant Uint := Static_Integer (Last_Bit (CC));
439 -- Case of component with last bit >= max machine scalar
441 if Lbit >= Max_Machine_Scalar_Size then
443 -- This is allowed only if first bit is zero, and
444 -- last bit + 1 is a multiple of storage unit size.
446 if Fbit = 0 and then (Lbit + 1) mod SSU = 0 then
448 -- This is the case to give a warning if enabled
450 if Warn_On_Reverse_Bit_Order then
452 ("info: multi-byte field specified with "
453 & " non-standard Bit_Order?V?", CC);
455 if Bytes_Big_Endian then
457 ("\bytes are not reversed "
458 & "(component is big-endian)?V?", CC);
461 ("\bytes are not reversed "
462 & "(component is little-endian)?V?", CC);
466 -- Give error message for RM 13.5.1(10) violation
470 ("machine scalar rules not followed for&",
471 First_Bit (CC), Comp);
473 Error_Msg_Uint_1 := Lbit;
474 Error_Msg_Uint_2 := Max_Machine_Scalar_Size;
476 ("\last bit (^) exceeds maximum machine "
480 if (Lbit + 1) mod SSU /= 0 then
481 Error_Msg_Uint_1 := SSU;
483 ("\and is not a multiple of Storage_Unit (^) "
488 Error_Msg_Uint_1 := Fbit;
490 ("\and first bit (^) is non-zero "
496 -- OK case of machine scalar related component clause,
497 -- For now, just count them.
500 Num_CC := Num_CC + 1;
505 Next_Component_Or_Discriminant (Comp);
508 -- We need to sort the component clauses on the basis of the
509 -- Position values in the clause, so we can group clauses with
510 -- the same Position together to determine the relevant machine
514 Comps : array (0 .. Num_CC) of Entity_Id;
515 -- Array to collect component and discriminant entities. The
516 -- data starts at index 1, the 0'th entry is for the sort
519 function CP_Lt (Op1, Op2 : Natural) return Boolean;
520 -- Compare routine for Sort
522 procedure CP_Move (From : Natural; To : Natural);
523 -- Move routine for Sort
525 package Sorting is new GNAT.Heap_Sort_G (CP_Move, CP_Lt);
529 -- Start and stop positions in the component list of the set of
530 -- components with the same starting position (that constitute
531 -- components in a single machine scalar).
534 -- Maximum last bit value of any component in this set
537 -- Corresponding machine scalar size
543 function CP_Lt (Op1, Op2 : Natural) return Boolean is
545 return Position (Component_Clause (Comps (Op1))) <
546 Position (Component_Clause (Comps (Op2)));
553 procedure CP_Move (From : Natural; To : Natural) is
555 Comps (To) := Comps (From);
558 -- Start of processing for Sort_CC
561 -- Collect the machine scalar relevant component clauses
564 Comp := First_Component_Or_Discriminant (R);
565 while Present (Comp) loop
567 CC : constant Node_Id := Component_Clause (Comp);
570 -- Collect only component clauses whose last bit is less
571 -- than machine scalar size. Any component clause whose
572 -- last bit exceeds this value does not take part in
573 -- machine scalar layout considerations. The test for
574 -- Error_Posted makes sure we exclude component clauses
575 -- for which we already posted an error.
578 and then not Error_Posted (Last_Bit (CC))
579 and then Static_Integer (Last_Bit (CC)) <
580 Max_Machine_Scalar_Size
582 Num_CC := Num_CC + 1;
583 Comps (Num_CC) := Comp;
587 Next_Component_Or_Discriminant (Comp);
590 -- Sort by ascending position number
592 Sorting.Sort (Num_CC);
594 -- We now have all the components whose size does not exceed
595 -- the max machine scalar value, sorted by starting position.
596 -- In this loop we gather groups of clauses starting at the
597 -- same position, to process them in accordance with AI-133.
600 while Stop < Num_CC loop
605 (Last_Bit (Component_Clause (Comps (Start))));
606 while Stop < Num_CC loop
608 (Position (Component_Clause (Comps (Stop + 1)))) =
610 (Position (Component_Clause (Comps (Stop))))
618 (Component_Clause (Comps (Stop)))));
624 -- Now we have a group of component clauses from Start to
625 -- Stop whose positions are identical, and MaxL is the
626 -- maximum last bit value of any of these components.
628 -- We need to determine the corresponding machine scalar
629 -- size. This loop assumes that machine scalar sizes are
630 -- even, and that each possible machine scalar has twice
631 -- as many bits as the next smaller one.
633 MSS := Max_Machine_Scalar_Size;
635 and then (MSS / 2) >= SSU
636 and then (MSS / 2) > MaxL
641 -- Here is where we fix up the Component_Bit_Offset value
642 -- to account for the reverse bit order. Some examples of
643 -- what needs to be done for the case of a machine scalar
646 -- First_Bit .. Last_Bit Component_Bit_Offset
658 -- The rule is that the first bit is obtained by subtracting
659 -- the old ending bit from machine scalar size - 1.
661 for C in Start .. Stop loop
663 Comp : constant Entity_Id := Comps (C);
664 CC : constant Node_Id := Component_Clause (Comp);
666 LB : constant Uint := Static_Integer (Last_Bit (CC));
667 NFB : constant Uint := MSS - Uint_1 - LB;
668 NLB : constant Uint := NFB + Esize (Comp) - 1;
669 Pos : constant Uint := Static_Integer (Position (CC));
672 if Warn_On_Reverse_Bit_Order then
673 Error_Msg_Uint_1 := MSS;
675 ("info: reverse bit order in machine " &
676 "scalar of length^?V?", First_Bit (CC));
677 Error_Msg_Uint_1 := NFB;
678 Error_Msg_Uint_2 := NLB;
680 if Bytes_Big_Endian then
682 ("\big-endian range for component "
683 & "& is ^ .. ^?V?", First_Bit (CC), Comp);
686 ("\little-endian range for component"
687 & "& is ^ .. ^?V?", First_Bit (CC), Comp);
691 Set_Component_Bit_Offset (Comp, Pos * SSU + NFB);
692 Set_Normalized_First_Bit (Comp, NFB mod SSU);
699 end Adjust_Record_For_Reverse_Bit_Order;
701 -------------------------------------
702 -- Alignment_Check_For_Size_Change --
703 -------------------------------------
705 procedure Alignment_Check_For_Size_Change (Typ : Entity_Id; Size : Uint) is
707 -- If the alignment is known, and not set by a rep clause, and is
708 -- inconsistent with the size being set, then reset it to unknown,
709 -- we assume in this case that the size overrides the inherited
710 -- alignment, and that the alignment must be recomputed.
712 if Known_Alignment (Typ)
713 and then not Has_Alignment_Clause (Typ)
714 and then Size mod (Alignment (Typ) * SSU) /= 0
716 Init_Alignment (Typ);
718 end Alignment_Check_For_Size_Change;
720 -------------------------------------
721 -- Analyze_Aspects_At_Freeze_Point --
722 -------------------------------------
724 procedure Analyze_Aspects_At_Freeze_Point (E : Entity_Id) is
729 procedure Analyze_Aspect_Default_Value (ASN : Node_Id);
730 -- This routine analyzes an Aspect_Default_[Component_]Value denoted by
731 -- the aspect specification node ASN.
733 procedure Inherit_Delayed_Rep_Aspects (ASN : Node_Id);
734 -- As discussed in the spec of Aspects (see Aspect_Delay declaration),
735 -- a derived type can inherit aspects from its parent which have been
736 -- specified at the time of the derivation using an aspect, as in:
738 -- type A is range 1 .. 10
739 -- with Size => Not_Defined_Yet;
743 -- Not_Defined_Yet : constant := 64;
745 -- In this example, the Size of A is considered to be specified prior
746 -- to the derivation, and thus inherited, even though the value is not
747 -- known at the time of derivation. To deal with this, we use two entity
748 -- flags. The flag Has_Derived_Rep_Aspects is set in the parent type (A
749 -- here), and then the flag May_Inherit_Delayed_Rep_Aspects is set in
750 -- the derived type (B here). If this flag is set when the derived type
751 -- is frozen, then this procedure is called to ensure proper inheritance
752 -- of all delayed aspects from the parent type. The derived type is E,
753 -- the argument to Analyze_Aspects_At_Freeze_Point. ASN is the first
754 -- aspect specification node in the Rep_Item chain for the parent type.
756 procedure Make_Pragma_From_Boolean_Aspect (ASN : Node_Id);
757 -- Given an aspect specification node ASN whose expression is an
758 -- optional Boolean, this routines creates the corresponding pragma
759 -- at the freezing point.
761 ----------------------------------
762 -- Analyze_Aspect_Default_Value --
763 ----------------------------------
765 procedure Analyze_Aspect_Default_Value (ASN : Node_Id) is
766 Ent : constant Entity_Id := Entity (ASN);
767 Expr : constant Node_Id := Expression (ASN);
768 Id : constant Node_Id := Identifier (ASN);
771 Error_Msg_Name_1 := Chars (Id);
773 if not Is_Type (Ent) then
774 Error_Msg_N ("aspect% can only apply to a type", Id);
777 elsif not Is_First_Subtype (Ent) then
778 Error_Msg_N ("aspect% cannot apply to subtype", Id);
781 elsif A_Id = Aspect_Default_Value
782 and then not Is_Scalar_Type (Ent)
784 Error_Msg_N ("aspect% can only be applied to scalar type", Id);
787 elsif A_Id = Aspect_Default_Component_Value then
788 if not Is_Array_Type (Ent) then
789 Error_Msg_N ("aspect% can only be applied to array type", Id);
792 elsif not Is_Scalar_Type (Component_Type (Ent)) then
793 Error_Msg_N ("aspect% requires scalar components", Id);
798 Set_Has_Default_Aspect (Base_Type (Ent));
800 if Is_Scalar_Type (Ent) then
801 Set_Default_Aspect_Value (Base_Type (Ent), Expr);
803 Set_Default_Aspect_Component_Value (Base_Type (Ent), Expr);
805 end Analyze_Aspect_Default_Value;
807 ---------------------------------
808 -- Inherit_Delayed_Rep_Aspects --
809 ---------------------------------
811 procedure Inherit_Delayed_Rep_Aspects (ASN : Node_Id) is
812 P : constant Entity_Id := Entity (ASN);
813 -- Entithy for parent type
816 -- Item from Rep_Item chain
821 -- Loop through delayed aspects for the parent type
824 while Present (N) loop
825 if Nkind (N) = N_Aspect_Specification then
826 exit when Entity (N) /= P;
828 if Is_Delayed_Aspect (N) then
829 A := Get_Aspect_Id (Chars (Identifier (N)));
831 -- Process delayed rep aspect. For Boolean attributes it is
832 -- not possible to cancel an attribute once set (the attempt
833 -- to use an aspect with xxx => False is an error) for a
834 -- derived type. So for those cases, we do not have to check
835 -- if a clause has been given for the derived type, since it
836 -- is harmless to set it again if it is already set.
842 when Aspect_Alignment =>
843 if not Has_Alignment_Clause (E) then
844 Set_Alignment (E, Alignment (P));
849 when Aspect_Atomic =>
850 if Is_Atomic (P) then
856 when Aspect_Atomic_Components =>
857 if Has_Atomic_Components (P) then
858 Set_Has_Atomic_Components (Base_Type (E));
863 when Aspect_Bit_Order =>
864 if Is_Record_Type (E)
865 and then No (Get_Attribute_Definition_Clause
866 (E, Attribute_Bit_Order))
867 and then Reverse_Bit_Order (P)
869 Set_Reverse_Bit_Order (Base_Type (E));
874 when Aspect_Component_Size =>
876 and then not Has_Component_Size_Clause (E)
879 (Base_Type (E), Component_Size (P));
884 when Aspect_Machine_Radix =>
885 if Is_Decimal_Fixed_Point_Type (E)
886 and then not Has_Machine_Radix_Clause (E)
888 Set_Machine_Radix_10 (E, Machine_Radix_10 (P));
891 -- Object_Size (also Size which also sets Object_Size)
893 when Aspect_Object_Size | Aspect_Size =>
894 if not Has_Size_Clause (E)
896 No (Get_Attribute_Definition_Clause
897 (E, Attribute_Object_Size))
899 Set_Esize (E, Esize (P));
905 if not Is_Packed (E) then
906 Set_Is_Packed (Base_Type (E));
908 if Is_Bit_Packed_Array (P) then
909 Set_Is_Bit_Packed_Array (Base_Type (E));
910 Set_Packed_Array_Impl_Type
911 (E, Packed_Array_Impl_Type (P));
915 -- Scalar_Storage_Order
917 when Aspect_Scalar_Storage_Order =>
918 if (Is_Record_Type (E) or else Is_Array_Type (E))
919 and then No (Get_Attribute_Definition_Clause
920 (E, Attribute_Scalar_Storage_Order))
921 and then Reverse_Storage_Order (P)
923 Set_Reverse_Storage_Order (Base_Type (E));
925 -- Clear default SSO indications, since the aspect
926 -- overrides the default.
928 Set_SSO_Set_Low_By_Default (Base_Type (E), False);
929 Set_SSO_Set_High_By_Default (Base_Type (E), False);
935 if Is_Fixed_Point_Type (E)
936 and then not Has_Small_Clause (E)
938 Set_Small_Value (E, Small_Value (P));
943 when Aspect_Storage_Size =>
944 if (Is_Access_Type (E) or else Is_Task_Type (E))
945 and then not Has_Storage_Size_Clause (E)
947 Set_Storage_Size_Variable
948 (Base_Type (E), Storage_Size_Variable (P));
953 when Aspect_Value_Size =>
955 -- Value_Size is never inherited, it is either set by
956 -- default, or it is explicitly set for the derived
957 -- type. So nothing to do here.
963 when Aspect_Volatile =>
964 if Is_Volatile (P) then
968 -- Volatile_Components
970 when Aspect_Volatile_Components =>
971 if Has_Volatile_Components (P) then
972 Set_Has_Volatile_Components (Base_Type (E));
975 -- That should be all the Rep Aspects
978 pragma Assert (Aspect_Delay (A_Id) /= Rep_Aspect);
985 N := Next_Rep_Item (N);
987 end Inherit_Delayed_Rep_Aspects;
989 -------------------------------------
990 -- Make_Pragma_From_Boolean_Aspect --
991 -------------------------------------
993 procedure Make_Pragma_From_Boolean_Aspect (ASN : Node_Id) is
994 Ident : constant Node_Id := Identifier (ASN);
995 A_Name : constant Name_Id := Chars (Ident);
996 A_Id : constant Aspect_Id := Get_Aspect_Id (A_Name);
997 Ent : constant Entity_Id := Entity (ASN);
998 Expr : constant Node_Id := Expression (ASN);
999 Loc : constant Source_Ptr := Sloc (ASN);
1003 procedure Check_False_Aspect_For_Derived_Type;
1004 -- This procedure checks for the case of a false aspect for a derived
1005 -- type, which improperly tries to cancel an aspect inherited from
1008 -----------------------------------------
1009 -- Check_False_Aspect_For_Derived_Type --
1010 -----------------------------------------
1012 procedure Check_False_Aspect_For_Derived_Type is
1016 -- We are only checking derived types
1018 if not Is_Derived_Type (E) then
1022 Par := Nearest_Ancestor (E);
1025 when Aspect_Atomic | Aspect_Shared =>
1026 if not Is_Atomic (Par) then
1030 when Aspect_Atomic_Components =>
1031 if not Has_Atomic_Components (Par) then
1035 when Aspect_Discard_Names =>
1036 if not Discard_Names (Par) then
1041 if not Is_Packed (Par) then
1045 when Aspect_Unchecked_Union =>
1046 if not Is_Unchecked_Union (Par) then
1050 when Aspect_Volatile =>
1051 if not Is_Volatile (Par) then
1055 when Aspect_Volatile_Components =>
1056 if not Has_Volatile_Components (Par) then
1064 -- Fall through means we are canceling an inherited aspect
1066 Error_Msg_Name_1 := A_Name;
1068 ("derived type& inherits aspect%, cannot cancel", Expr, E);
1070 end Check_False_Aspect_For_Derived_Type;
1072 -- Start of processing for Make_Pragma_From_Boolean_Aspect
1075 -- Note that we know Expr is present, because for a missing Expr
1076 -- argument, we knew it was True and did not need to delay the
1077 -- evaluation to the freeze point.
1079 if Is_False (Static_Boolean (Expr)) then
1080 Check_False_Aspect_For_Derived_Type;
1085 Pragma_Argument_Associations => New_List (
1086 Make_Pragma_Argument_Association (Sloc (Ident),
1087 Expression => New_Occurrence_Of (Ent, Sloc (Ident)))),
1089 Pragma_Identifier =>
1090 Make_Identifier (Sloc (Ident), Chars (Ident)));
1092 Set_From_Aspect_Specification (Prag, True);
1093 Set_Corresponding_Aspect (Prag, ASN);
1094 Set_Aspect_Rep_Item (ASN, Prag);
1095 Set_Is_Delayed_Aspect (Prag);
1096 Set_Parent (Prag, ASN);
1098 end Make_Pragma_From_Boolean_Aspect;
1100 -- Start of processing for Analyze_Aspects_At_Freeze_Point
1103 -- Must be visible in current scope
1105 if not Scope_Within_Or_Same (Current_Scope, Scope (E)) then
1109 -- Look for aspect specification entries for this entity
1111 ASN := First_Rep_Item (E);
1112 while Present (ASN) loop
1113 if Nkind (ASN) = N_Aspect_Specification then
1114 exit when Entity (ASN) /= E;
1116 if Is_Delayed_Aspect (ASN) then
1117 A_Id := Get_Aspect_Id (ASN);
1121 -- For aspects whose expression is an optional Boolean, make
1122 -- the corresponding pragma at the freeze point.
1124 when Boolean_Aspects |
1125 Library_Unit_Aspects =>
1126 Make_Pragma_From_Boolean_Aspect (ASN);
1128 -- Special handling for aspects that don't correspond to
1129 -- pragmas/attributes.
1131 when Aspect_Default_Value |
1132 Aspect_Default_Component_Value =>
1134 -- Do not inherit aspect for anonymous base type of a
1135 -- scalar or array type, because they apply to the first
1136 -- subtype of the type, and will be processed when that
1137 -- first subtype is frozen.
1139 if Is_Derived_Type (E)
1140 and then not Comes_From_Source (E)
1141 and then E /= First_Subtype (E)
1145 Analyze_Aspect_Default_Value (ASN);
1148 -- Ditto for iterator aspects, because the corresponding
1149 -- attributes may not have been analyzed yet.
1151 when Aspect_Constant_Indexing |
1152 Aspect_Variable_Indexing |
1153 Aspect_Default_Iterator |
1154 Aspect_Iterator_Element =>
1155 Analyze (Expression (ASN));
1157 if Etype (Expression (ASN)) = Any_Type then
1159 ("\aspect must be fully defined before & is frozen",
1163 when Aspect_Iterable =>
1164 Validate_Iterable_Aspect (E, ASN);
1170 Ritem := Aspect_Rep_Item (ASN);
1172 if Present (Ritem) then
1178 Next_Rep_Item (ASN);
1181 -- This is where we inherit delayed rep aspects from our parent. Note
1182 -- that if we fell out of the above loop with ASN non-empty, it means
1183 -- we hit an aspect for an entity other than E, and it must be the
1184 -- type from which we were derived.
1186 if May_Inherit_Delayed_Rep_Aspects (E) then
1187 Inherit_Delayed_Rep_Aspects (ASN);
1189 end Analyze_Aspects_At_Freeze_Point;
1191 -----------------------------------
1192 -- Analyze_Aspect_Specifications --
1193 -----------------------------------
1195 procedure Analyze_Aspect_Specifications (N : Node_Id; E : Entity_Id) is
1196 procedure Decorate (Asp : Node_Id; Prag : Node_Id);
1197 -- Establish linkages between an aspect and its corresponding
1200 procedure Insert_After_SPARK_Mode
1204 -- Subsidiary to the analysis of aspects Abstract_State, Ghost,
1205 -- Initializes, Initial_Condition and Refined_State. Insert node Prag
1206 -- before node Ins_Nod. If Ins_Nod is for pragma SPARK_Mode, then skip
1207 -- SPARK_Mode. Decls is the associated declarative list where Prag is to
1210 procedure Insert_Pragma (Prag : Node_Id);
1211 -- Subsidiary to the analysis of aspects Attach_Handler, Contract_Cases,
1212 -- Depends, Global, Post, Pre, Refined_Depends and Refined_Global.
1213 -- Insert pragma Prag such that it mimics the placement of a source
1214 -- pragma of the same kind.
1216 -- procedure Proc (Formal : ...) with Global => ...;
1218 -- procedure Proc (Formal : ...);
1219 -- pragma Global (...);
1225 procedure Decorate (Asp : Node_Id; Prag : Node_Id) is
1227 Set_Aspect_Rep_Item (Asp, Prag);
1228 Set_Corresponding_Aspect (Prag, Asp);
1229 Set_From_Aspect_Specification (Prag);
1230 Set_Parent (Prag, Asp);
1233 -----------------------------
1234 -- Insert_After_SPARK_Mode --
1235 -----------------------------
1237 procedure Insert_After_SPARK_Mode
1242 Decl : Node_Id := Ins_Nod;
1248 and then Nkind (Decl) = N_Pragma
1249 and then Pragma_Name (Decl) = Name_SPARK_Mode
1251 Decl := Next (Decl);
1254 if Present (Decl) then
1255 Insert_Before (Decl, Prag);
1257 -- Aitem acts as the last declaration
1260 Append_To (Decls, Prag);
1262 end Insert_After_SPARK_Mode;
1268 procedure Insert_Pragma (Prag : Node_Id) is
1273 if Nkind (N) = N_Subprogram_Body then
1274 if Present (Declarations (N)) then
1276 -- Skip other internally generated pragmas from aspects to find
1277 -- the proper insertion point. As a result the order of pragmas
1278 -- is the same as the order of aspects.
1280 -- As precondition pragmas generated from conjuncts in the
1281 -- precondition aspect are presented in reverse order to
1282 -- Insert_Pragma, insert them in the correct order here by not
1283 -- skipping previously inserted precondition pragmas when the
1284 -- current pragma is a precondition.
1286 Decl := First (Declarations (N));
1287 while Present (Decl) loop
1288 if Nkind (Decl) = N_Pragma
1289 and then From_Aspect_Specification (Decl)
1290 and then not (Get_Pragma_Id (Decl) = Pragma_Precondition
1292 Get_Pragma_Id (Prag) = Pragma_Precondition)
1300 if Present (Decl) then
1301 Insert_Before (Decl, Prag);
1303 Append (Prag, Declarations (N));
1306 Set_Declarations (N, New_List (Prag));
1309 -- When the context is a library unit, the pragma is added to the
1310 -- Pragmas_After list.
1312 elsif Nkind (Parent (N)) = N_Compilation_Unit then
1313 Aux := Aux_Decls_Node (Parent (N));
1315 if No (Pragmas_After (Aux)) then
1316 Set_Pragmas_After (Aux, New_List);
1319 Prepend (Prag, Pragmas_After (Aux));
1324 Insert_After (N, Prag);
1334 L : constant List_Id := Aspect_Specifications (N);
1336 Ins_Node : Node_Id := N;
1337 -- Insert pragmas/attribute definition clause after this node when no
1338 -- delayed analysis is required.
1340 -- Start of processing for Analyze_Aspect_Specifications
1342 -- The general processing involves building an attribute definition
1343 -- clause or a pragma node that corresponds to the aspect. Then in order
1344 -- to delay the evaluation of this aspect to the freeze point, we attach
1345 -- the corresponding pragma/attribute definition clause to the aspect
1346 -- specification node, which is then placed in the Rep Item chain. In
1347 -- this case we mark the entity by setting the flag Has_Delayed_Aspects
1348 -- and we evaluate the rep item at the freeze point. When the aspect
1349 -- doesn't have a corresponding pragma/attribute definition clause, then
1350 -- its analysis is simply delayed at the freeze point.
1352 -- Some special cases don't require delay analysis, thus the aspect is
1353 -- analyzed right now.
1355 -- Note that there is a special handling for Pre, Post, Test_Case,
1356 -- Contract_Cases aspects. In these cases, we do not have to worry
1357 -- about delay issues, since the pragmas themselves deal with delay
1358 -- of visibility for the expression analysis. Thus, we just insert
1359 -- the pragma after the node N.
1362 pragma Assert (Present (L));
1364 -- Loop through aspects
1366 Aspect := First (L);
1367 Aspect_Loop : while Present (Aspect) loop
1368 Analyze_One_Aspect : declare
1369 Expr : constant Node_Id := Expression (Aspect);
1370 Id : constant Node_Id := Identifier (Aspect);
1371 Loc : constant Source_Ptr := Sloc (Aspect);
1372 Nam : constant Name_Id := Chars (Id);
1373 A_Id : constant Aspect_Id := Get_Aspect_Id (Nam);
1376 Delay_Required : Boolean;
1377 -- Set False if delay is not required
1379 Eloc : Source_Ptr := No_Location;
1380 -- Source location of expression, modified when we split PPC's. It
1381 -- is set below when Expr is present.
1383 procedure Analyze_Aspect_External_Or_Link_Name;
1384 -- Perform analysis of the External_Name or Link_Name aspects
1386 procedure Analyze_Aspect_Implicit_Dereference;
1387 -- Perform analysis of the Implicit_Dereference aspects
1389 procedure Make_Aitem_Pragma
1390 (Pragma_Argument_Associations : List_Id;
1391 Pragma_Name : Name_Id);
1392 -- This is a wrapper for Make_Pragma used for converting aspects
1393 -- to pragmas. It takes care of Sloc (set from Loc) and building
1394 -- the pragma identifier from the given name. In addition the
1395 -- flags Class_Present and Split_PPC are set from the aspect
1396 -- node, as well as Is_Ignored. This routine also sets the
1397 -- From_Aspect_Specification in the resulting pragma node to
1398 -- True, and sets Corresponding_Aspect to point to the aspect.
1399 -- The resulting pragma is assigned to Aitem.
1401 ------------------------------------------
1402 -- Analyze_Aspect_External_Or_Link_Name --
1403 ------------------------------------------
1405 procedure Analyze_Aspect_External_Or_Link_Name is
1407 -- Verify that there is an Import/Export aspect defined for the
1408 -- entity. The processing of that aspect in turn checks that
1409 -- there is a Convention aspect declared. The pragma is
1410 -- constructed when processing the Convention aspect.
1417 while Present (A) loop
1418 exit when Nam_In (Chars (Identifier (A)), Name_Export,
1425 ("missing Import/Export for Link/External name",
1429 end Analyze_Aspect_External_Or_Link_Name;
1431 -----------------------------------------
1432 -- Analyze_Aspect_Implicit_Dereference --
1433 -----------------------------------------
1435 procedure Analyze_Aspect_Implicit_Dereference is
1437 if not Is_Type (E) or else not Has_Discriminants (E) then
1439 ("aspect must apply to a type with discriminants", N);
1446 Disc := First_Discriminant (E);
1447 while Present (Disc) loop
1448 if Chars (Expr) = Chars (Disc)
1449 and then Ekind (Etype (Disc)) =
1450 E_Anonymous_Access_Type
1452 Set_Has_Implicit_Dereference (E);
1453 Set_Has_Implicit_Dereference (Disc);
1457 Next_Discriminant (Disc);
1460 -- Error if no proper access discriminant.
1463 ("not an access discriminant of&", Expr, E);
1466 end Analyze_Aspect_Implicit_Dereference;
1468 -----------------------
1469 -- Make_Aitem_Pragma --
1470 -----------------------
1472 procedure Make_Aitem_Pragma
1473 (Pragma_Argument_Associations : List_Id;
1474 Pragma_Name : Name_Id)
1476 Args : List_Id := Pragma_Argument_Associations;
1479 -- We should never get here if aspect was disabled
1481 pragma Assert (not Is_Disabled (Aspect));
1483 -- Certain aspects allow for an optional name or expression. Do
1484 -- not generate a pragma with empty argument association list.
1486 if No (Args) or else No (Expression (First (Args))) then
1494 Pragma_Argument_Associations => Args,
1495 Pragma_Identifier =>
1496 Make_Identifier (Sloc (Id), Pragma_Name),
1497 Class_Present => Class_Present (Aspect),
1498 Split_PPC => Split_PPC (Aspect));
1500 -- Set additional semantic fields
1502 if Is_Ignored (Aspect) then
1503 Set_Is_Ignored (Aitem);
1504 elsif Is_Checked (Aspect) then
1505 Set_Is_Checked (Aitem);
1508 Set_Corresponding_Aspect (Aitem, Aspect);
1509 Set_From_Aspect_Specification (Aitem, True);
1510 end Make_Aitem_Pragma;
1512 -- Start of processing for Analyze_One_Aspect
1515 -- Skip aspect if already analyzed, to avoid looping in some cases
1517 if Analyzed (Aspect) then
1521 -- Skip looking at aspect if it is totally disabled. Just mark it
1522 -- as such for later reference in the tree. This also sets the
1523 -- Is_Ignored and Is_Checked flags appropriately.
1525 Check_Applicable_Policy (Aspect);
1527 if Is_Disabled (Aspect) then
1531 -- Set the source location of expression, used in the case of
1532 -- a failed precondition/postcondition or invariant. Note that
1533 -- the source location of the expression is not usually the best
1534 -- choice here. For example, it gets located on the last AND
1535 -- keyword in a chain of boolean expressiond AND'ed together.
1536 -- It is best to put the message on the first character of the
1537 -- assertion, which is the effect of the First_Node call here.
1539 if Present (Expr) then
1540 Eloc := Sloc (First_Node (Expr));
1543 -- Check restriction No_Implementation_Aspect_Specifications
1545 if Implementation_Defined_Aspect (A_Id) then
1547 (No_Implementation_Aspect_Specifications, Aspect);
1550 -- Check restriction No_Specification_Of_Aspect
1552 Check_Restriction_No_Specification_Of_Aspect (Aspect);
1554 -- Mark aspect analyzed (actual analysis is delayed till later)
1556 Set_Analyzed (Aspect);
1557 Set_Entity (Aspect, E);
1558 Ent := New_Occurrence_Of (E, Sloc (Id));
1560 -- Check for duplicate aspect. Note that the Comes_From_Source
1561 -- test allows duplicate Pre/Post's that we generate internally
1562 -- to escape being flagged here.
1564 if No_Duplicates_Allowed (A_Id) then
1566 while Anod /= Aspect loop
1567 if Comes_From_Source (Aspect)
1568 and then Same_Aspect (A_Id, Get_Aspect_Id (Anod))
1570 Error_Msg_Name_1 := Nam;
1571 Error_Msg_Sloc := Sloc (Anod);
1573 -- Case of same aspect specified twice
1575 if Class_Present (Anod) = Class_Present (Aspect) then
1576 if not Class_Present (Anod) then
1578 ("aspect% for & previously given#",
1582 ("aspect `%''Class` for & previously given#",
1592 -- Check some general restrictions on language defined aspects
1594 if not Implementation_Defined_Aspect (A_Id) then
1595 Error_Msg_Name_1 := Nam;
1597 -- Not allowed for renaming declarations
1599 if Nkind (N) in N_Renaming_Declaration then
1601 ("aspect % not allowed for renaming declaration",
1605 -- Not allowed for formal type declarations
1607 if Nkind (N) = N_Formal_Type_Declaration then
1609 ("aspect % not allowed for formal type declaration",
1614 -- Copy expression for later processing by the procedures
1615 -- Check_Aspect_At_[Freeze_Point | End_Of_Declarations]
1617 Set_Entity (Id, New_Copy_Tree (Expr));
1619 -- Set Delay_Required as appropriate to aspect
1621 case Aspect_Delay (A_Id) is
1622 when Always_Delay =>
1623 Delay_Required := True;
1626 Delay_Required := False;
1630 -- If expression has the form of an integer literal, then
1631 -- do not delay, since we know the value cannot change.
1632 -- This optimization catches most rep clause cases.
1634 -- For Boolean aspects, don't delay if no expression
1636 if A_Id in Boolean_Aspects and then No (Expr) then
1637 Delay_Required := False;
1639 -- For non-Boolean aspects, don't delay if integer literal
1641 elsif A_Id not in Boolean_Aspects
1642 and then Present (Expr)
1643 and then Nkind (Expr) = N_Integer_Literal
1645 Delay_Required := False;
1647 -- All other cases are delayed
1650 Delay_Required := True;
1651 Set_Has_Delayed_Rep_Aspects (E);
1655 -- Processing based on specific aspect
1658 when Aspect_Unimplemented =>
1659 null; -- ??? temp for now
1661 -- No_Aspect should be impossible
1664 raise Program_Error;
1666 -- Case 1: Aspects corresponding to attribute definition
1669 when Aspect_Address |
1672 Aspect_Component_Size |
1673 Aspect_Constant_Indexing |
1674 Aspect_Default_Iterator |
1675 Aspect_Dispatching_Domain |
1676 Aspect_External_Tag |
1679 Aspect_Iterator_Element |
1680 Aspect_Machine_Radix |
1681 Aspect_Object_Size |
1684 Aspect_Scalar_Storage_Order |
1687 Aspect_Simple_Storage_Pool |
1688 Aspect_Storage_Pool |
1689 Aspect_Stream_Size |
1691 Aspect_Variable_Indexing |
1694 -- Indexing aspects apply only to tagged type
1696 if (A_Id = Aspect_Constant_Indexing
1698 A_Id = Aspect_Variable_Indexing)
1699 and then not (Is_Type (E)
1700 and then Is_Tagged_Type (E))
1703 ("indexing aspect can only apply to a tagged type",
1708 -- For the case of aspect Address, we don't consider that we
1709 -- know the entity is never set in the source, since it is
1710 -- is likely aliasing is occurring.
1712 -- Note: one might think that the analysis of the resulting
1713 -- attribute definition clause would take care of that, but
1714 -- that's not the case since it won't be from source.
1716 if A_Id = Aspect_Address then
1717 Set_Never_Set_In_Source (E, False);
1720 -- Correctness of the profile of a stream operation is
1721 -- verified at the freeze point, but we must detect the
1722 -- illegal specification of this aspect for a subtype now,
1723 -- to prevent malformed rep_item chains.
1725 if A_Id = Aspect_Input or else
1726 A_Id = Aspect_Output or else
1727 A_Id = Aspect_Read or else
1730 if not Is_First_Subtype (E) then
1732 ("local name must be a first subtype", Aspect);
1735 -- If stream aspect applies to the class-wide type,
1736 -- the generated attribute definition applies to the
1737 -- class-wide type as well.
1739 elsif Class_Present (Aspect) then
1741 Make_Attribute_Reference (Loc,
1743 Attribute_Name => Name_Class);
1747 -- Construct the attribute definition clause
1750 Make_Attribute_Definition_Clause (Loc,
1752 Chars => Chars (Id),
1753 Expression => Relocate_Node (Expr));
1755 -- If the address is specified, then we treat the entity as
1756 -- referenced, to avoid spurious warnings. This is analogous
1757 -- to what is done with an attribute definition clause, but
1758 -- here we don't want to generate a reference because this
1759 -- is the point of definition of the entity.
1761 if A_Id = Aspect_Address then
1765 -- Case 2: Aspects corresponding to pragmas
1767 -- Case 2a: Aspects corresponding to pragmas with two
1768 -- arguments, where the first argument is a local name
1769 -- referring to the entity, and the second argument is the
1770 -- aspect definition expression.
1772 -- Linker_Section/Suppress/Unsuppress
1774 when Aspect_Linker_Section |
1776 Aspect_Unsuppress =>
1779 (Pragma_Argument_Associations => New_List (
1780 Make_Pragma_Argument_Association (Loc,
1781 Expression => New_Occurrence_Of (E, Loc)),
1782 Make_Pragma_Argument_Association (Sloc (Expr),
1783 Expression => Relocate_Node (Expr))),
1784 Pragma_Name => Chars (Id));
1788 -- Corresponds to pragma Implemented, construct the pragma
1790 when Aspect_Synchronization =>
1792 (Pragma_Argument_Associations => New_List (
1793 Make_Pragma_Argument_Association (Loc,
1794 Expression => New_Occurrence_Of (E, Loc)),
1795 Make_Pragma_Argument_Association (Sloc (Expr),
1796 Expression => Relocate_Node (Expr))),
1797 Pragma_Name => Name_Implemented);
1801 when Aspect_Attach_Handler =>
1803 (Pragma_Argument_Associations => New_List (
1804 Make_Pragma_Argument_Association (Sloc (Ent),
1806 Make_Pragma_Argument_Association (Sloc (Expr),
1807 Expression => Relocate_Node (Expr))),
1808 Pragma_Name => Name_Attach_Handler);
1810 -- We need to insert this pragma into the tree to get proper
1811 -- processing and to look valid from a placement viewpoint.
1813 Insert_Pragma (Aitem);
1816 -- Dynamic_Predicate, Predicate, Static_Predicate
1818 when Aspect_Dynamic_Predicate |
1820 Aspect_Static_Predicate =>
1822 -- These aspects apply only to subtypes
1824 if not Is_Type (E) then
1826 ("predicate can only be specified for a subtype",
1830 elsif Is_Incomplete_Type (E) then
1832 ("predicate cannot apply to incomplete view", Aspect);
1836 -- Construct the pragma (always a pragma Predicate, with
1837 -- flags recording whether it is static/dynamic). We also
1838 -- set flags recording this in the type itself.
1841 (Pragma_Argument_Associations => New_List (
1842 Make_Pragma_Argument_Association (Sloc (Ent),
1844 Make_Pragma_Argument_Association (Sloc (Expr),
1845 Expression => Relocate_Node (Expr))),
1846 Pragma_Name => Name_Predicate);
1848 -- Mark type has predicates, and remember what kind of
1849 -- aspect lead to this predicate (we need this to access
1850 -- the right set of check policies later on).
1852 Set_Has_Predicates (E);
1854 if A_Id = Aspect_Dynamic_Predicate then
1855 Set_Has_Dynamic_Predicate_Aspect (E);
1856 elsif A_Id = Aspect_Static_Predicate then
1857 Set_Has_Static_Predicate_Aspect (E);
1860 -- If the type is private, indicate that its completion
1861 -- has a freeze node, because that is the one that will
1862 -- be visible at freeze time.
1864 if Is_Private_Type (E) and then Present (Full_View (E)) then
1865 Set_Has_Predicates (Full_View (E));
1867 if A_Id = Aspect_Dynamic_Predicate then
1868 Set_Has_Dynamic_Predicate_Aspect (Full_View (E));
1869 elsif A_Id = Aspect_Static_Predicate then
1870 Set_Has_Static_Predicate_Aspect (Full_View (E));
1873 Set_Has_Delayed_Aspects (Full_View (E));
1874 Ensure_Freeze_Node (Full_View (E));
1877 -- Case 2b: Aspects corresponding to pragmas with two
1878 -- arguments, where the second argument is a local name
1879 -- referring to the entity, and the first argument is the
1880 -- aspect definition expression.
1884 when Aspect_Convention =>
1886 -- The aspect may be part of the specification of an import
1887 -- or export pragma. Scan the aspect list to gather the
1888 -- other components, if any. The name of the generated
1889 -- pragma is one of Convention/Import/Export.
1892 Args : constant List_Id := New_List (
1893 Make_Pragma_Argument_Association (Sloc (Expr),
1894 Expression => Relocate_Node (Expr)),
1895 Make_Pragma_Argument_Association (Sloc (Ent),
1896 Expression => Ent));
1898 Imp_Exp_Seen : Boolean := False;
1899 -- Flag set when aspect Import or Export has been seen
1901 Imp_Seen : Boolean := False;
1902 -- Flag set when aspect Import has been seen
1906 Extern_Arg : Node_Id;
1911 Extern_Arg := Empty;
1913 Prag_Nam := Chars (Id);
1916 while Present (Asp) loop
1917 Asp_Nam := Chars (Identifier (Asp));
1919 -- Aspects Import and Export take precedence over
1920 -- aspect Convention. As a result the generated pragma
1921 -- must carry the proper interfacing aspect's name.
1923 if Nam_In (Asp_Nam, Name_Import, Name_Export) then
1924 if Imp_Exp_Seen then
1925 Error_Msg_N ("conflicting", Asp);
1927 Imp_Exp_Seen := True;
1929 if Asp_Nam = Name_Import then
1934 Prag_Nam := Asp_Nam;
1936 -- Aspect External_Name adds an extra argument to the
1937 -- generated pragma.
1939 elsif Asp_Nam = Name_External_Name then
1941 Make_Pragma_Argument_Association (Loc,
1943 Expression => Relocate_Node (Expression (Asp)));
1945 -- Aspect Link_Name adds an extra argument to the
1946 -- generated pragma.
1948 elsif Asp_Nam = Name_Link_Name then
1950 Make_Pragma_Argument_Association (Loc,
1952 Expression => Relocate_Node (Expression (Asp)));
1958 -- Assemble the full argument list
1960 if Present (Extern_Arg) then
1961 Append_To (Args, Extern_Arg);
1964 if Present (Link_Arg) then
1965 Append_To (Args, Link_Arg);
1969 (Pragma_Argument_Associations => Args,
1970 Pragma_Name => Prag_Nam);
1972 -- Store the generated pragma Import in the related
1975 if Imp_Seen and then Is_Subprogram (E) then
1976 Set_Import_Pragma (E, Aitem);
1980 -- CPU, Interrupt_Priority, Priority
1982 -- These three aspects can be specified for a subprogram spec
1983 -- or body, in which case we analyze the expression and export
1984 -- the value of the aspect.
1986 -- Previously, we generated an equivalent pragma for bodies
1987 -- (note that the specs cannot contain these pragmas). The
1988 -- pragma was inserted ahead of local declarations, rather than
1989 -- after the body. This leads to a certain duplication between
1990 -- the processing performed for the aspect and the pragma, but
1991 -- given the straightforward handling required it is simpler
1992 -- to duplicate than to translate the aspect in the spec into
1993 -- a pragma in the declarative part of the body.
1996 Aspect_Interrupt_Priority |
1999 if Nkind_In (N, N_Subprogram_Body,
2000 N_Subprogram_Declaration)
2002 -- Analyze the aspect expression
2004 Analyze_And_Resolve (Expr, Standard_Integer);
2006 -- Interrupt_Priority aspect not allowed for main
2007 -- subprograms. ARM D.1 does not forbid this explicitly,
2008 -- but ARM J.15.11 (6/3) does not permit pragma
2009 -- Interrupt_Priority for subprograms.
2011 if A_Id = Aspect_Interrupt_Priority then
2013 ("Interrupt_Priority aspect cannot apply to "
2014 & "subprogram", Expr);
2016 -- The expression must be static
2018 elsif not Is_OK_Static_Expression (Expr) then
2019 Flag_Non_Static_Expr
2020 ("aspect requires static expression!", Expr);
2022 -- Check whether this is the main subprogram. Issue a
2023 -- warning only if it is obviously not a main program
2024 -- (when it has parameters or when the subprogram is
2025 -- within a package).
2027 elsif Present (Parameter_Specifications
2028 (Specification (N)))
2029 or else not Is_Compilation_Unit (Defining_Entity (N))
2031 -- See ARM D.1 (14/3) and D.16 (12/3)
2034 ("aspect applied to subprogram other than the "
2035 & "main subprogram has no effect??", Expr);
2037 -- Otherwise check in range and export the value
2039 -- For the CPU aspect
2041 elsif A_Id = Aspect_CPU then
2042 if Is_In_Range (Expr, RTE (RE_CPU_Range)) then
2044 -- Value is correct so we export the value to make
2045 -- it available at execution time.
2048 (Main_Unit, UI_To_Int (Expr_Value (Expr)));
2052 ("main subprogram CPU is out of range", Expr);
2055 -- For the Priority aspect
2057 elsif A_Id = Aspect_Priority then
2058 if Is_In_Range (Expr, RTE (RE_Priority)) then
2060 -- Value is correct so we export the value to make
2061 -- it available at execution time.
2064 (Main_Unit, UI_To_Int (Expr_Value (Expr)));
2066 -- Ignore pragma if Relaxed_RM_Semantics to support
2067 -- other targets/non GNAT compilers.
2069 elsif not Relaxed_RM_Semantics then
2071 ("main subprogram priority is out of range",
2076 -- Load an arbitrary entity from System.Tasking.Stages
2077 -- or System.Tasking.Restricted.Stages (depending on
2078 -- the supported profile) to make sure that one of these
2079 -- packages is implicitly with'ed, since we need to have
2080 -- the tasking run time active for the pragma Priority to
2081 -- have any effect. Previously we with'ed the package
2082 -- System.Tasking, but this package does not trigger the
2083 -- required initialization of the run-time library.
2086 Discard : Entity_Id;
2088 if Restricted_Profile then
2089 Discard := RTE (RE_Activate_Restricted_Tasks);
2091 Discard := RTE (RE_Activate_Tasks);
2095 -- Handling for these Aspects in subprograms is complete
2102 -- Pass the aspect as an attribute
2105 Make_Attribute_Definition_Clause (Loc,
2107 Chars => Chars (Id),
2108 Expression => Relocate_Node (Expr));
2113 when Aspect_Warnings =>
2115 (Pragma_Argument_Associations => New_List (
2116 Make_Pragma_Argument_Association (Sloc (Expr),
2117 Expression => Relocate_Node (Expr)),
2118 Make_Pragma_Argument_Association (Loc,
2119 Expression => New_Occurrence_Of (E, Loc))),
2120 Pragma_Name => Chars (Id));
2122 -- Case 2c: Aspects corresponding to pragmas with three
2125 -- Invariant aspects have a first argument that references the
2126 -- entity, a second argument that is the expression and a third
2127 -- argument that is an appropriate message.
2129 -- Invariant, Type_Invariant
2131 when Aspect_Invariant |
2132 Aspect_Type_Invariant =>
2134 -- Analysis of the pragma will verify placement legality:
2135 -- an invariant must apply to a private type, or appear in
2136 -- the private part of a spec and apply to a completion.
2139 (Pragma_Argument_Associations => New_List (
2140 Make_Pragma_Argument_Association (Sloc (Ent),
2142 Make_Pragma_Argument_Association (Sloc (Expr),
2143 Expression => Relocate_Node (Expr))),
2144 Pragma_Name => Name_Invariant);
2146 -- Add message unless exception messages are suppressed
2148 if not Opt.Exception_Locations_Suppressed then
2149 Append_To (Pragma_Argument_Associations (Aitem),
2150 Make_Pragma_Argument_Association (Eloc,
2151 Chars => Name_Message,
2153 Make_String_Literal (Eloc,
2154 Strval => "failed invariant from "
2155 & Build_Location_String (Eloc))));
2158 -- For Invariant case, insert immediately after the entity
2159 -- declaration. We do not have to worry about delay issues
2160 -- since the pragma processing takes care of this.
2162 Delay_Required := False;
2164 -- Case 2d : Aspects that correspond to a pragma with one
2169 -- Aspect Abstract_State introduces implicit declarations for
2170 -- all state abstraction entities it defines. To emulate this
2171 -- behavior, insert the pragma at the beginning of the visible
2172 -- declarations of the related package so that it is analyzed
2175 when Aspect_Abstract_State => Abstract_State : declare
2176 Context : Node_Id := N;
2181 -- When aspect Abstract_State appears on a generic package,
2182 -- it is propageted to the package instance. The context in
2183 -- this case is the instance spec.
2185 if Nkind (Context) = N_Package_Instantiation then
2186 Context := Instance_Spec (Context);
2189 if Nkind_In (Context, N_Generic_Package_Declaration,
2190 N_Package_Declaration)
2193 (Pragma_Argument_Associations => New_List (
2194 Make_Pragma_Argument_Association (Loc,
2195 Expression => Relocate_Node (Expr))),
2196 Pragma_Name => Name_Abstract_State);
2197 Decorate (Aspect, Aitem);
2199 Decls := Visible_Declarations (Specification (Context));
2201 -- In general pragma Abstract_State must be at the top
2202 -- of the existing visible declarations to emulate its
2203 -- source counterpart. The only exception to this is a
2204 -- generic instance in which case the pragma must be
2205 -- inserted after the association renamings.
2207 if Present (Decls) then
2208 Decl := First (Decls);
2210 -- The visible declarations of a generic instance have
2211 -- the following structure:
2213 -- <renamings of generic formals>
2214 -- <renamings of internally-generated spec and body>
2215 -- <first source declaration>
2217 -- The pragma must be inserted before the first source
2218 -- declaration, skip the instance "header".
2220 if Is_Generic_Instance (Defining_Entity (Context)) then
2221 while Present (Decl)
2222 and then not Comes_From_Source (Decl)
2224 Decl := Next (Decl);
2228 -- When aspects Abstract_State, Ghost,
2229 -- Initial_Condition and Initializes are out of order,
2230 -- ensure that pragma SPARK_Mode is always at the top
2231 -- of the declarations to properly enabled/suppress
2234 Insert_After_SPARK_Mode
2239 -- Otherwise the pragma forms a new declarative list
2242 Set_Visible_Declarations
2243 (Specification (Context), New_List (Aitem));
2248 ("aspect & must apply to a package declaration",
2255 -- Aspect Default_Internal_Condition is never delayed because
2256 -- it is equivalent to a source pragma which appears after the
2257 -- related private type. To deal with forward references, the
2258 -- generated pragma is stored in the rep chain of the related
2259 -- private type as types do not carry contracts. The pragma is
2260 -- wrapped inside of a procedure at the freeze point of the
2261 -- private type's full view.
2263 when Aspect_Default_Initial_Condition =>
2265 (Pragma_Argument_Associations => New_List (
2266 Make_Pragma_Argument_Association (Loc,
2267 Expression => Relocate_Node (Expr))),
2269 Name_Default_Initial_Condition);
2271 Decorate (Aspect, Aitem);
2272 Insert_Pragma (Aitem);
2275 -- Default_Storage_Pool
2277 when Aspect_Default_Storage_Pool =>
2279 (Pragma_Argument_Associations => New_List (
2280 Make_Pragma_Argument_Association (Loc,
2281 Expression => Relocate_Node (Expr))),
2283 Name_Default_Storage_Pool);
2285 Decorate (Aspect, Aitem);
2286 Insert_Pragma (Aitem);
2291 -- Aspect Depends is never delayed because it is equivalent to
2292 -- a source pragma which appears after the related subprogram.
2293 -- To deal with forward references, the generated pragma is
2294 -- stored in the contract of the related subprogram and later
2295 -- analyzed at the end of the declarative region. See routine
2296 -- Analyze_Depends_In_Decl_Part for details.
2298 when Aspect_Depends =>
2300 (Pragma_Argument_Associations => New_List (
2301 Make_Pragma_Argument_Association (Loc,
2302 Expression => Relocate_Node (Expr))),
2303 Pragma_Name => Name_Depends);
2305 Decorate (Aspect, Aitem);
2306 Insert_Pragma (Aitem);
2309 -- Aspect Extensions_Visible is never delayed because it is
2310 -- equivalent to a source pragma which appears after the
2311 -- related subprogram.
2313 when Aspect_Extensions_Visible =>
2315 (Pragma_Argument_Associations => New_List (
2316 Make_Pragma_Argument_Association (Loc,
2317 Expression => Relocate_Node (Expr))),
2318 Pragma_Name => Name_Extensions_Visible);
2320 Decorate (Aspect, Aitem);
2321 Insert_Pragma (Aitem);
2324 -- Aspect Ghost is never delayed because it is equivalent to a
2325 -- source pragma which appears at the top of [generic] package
2326 -- declarations or after an object, a [generic] subprogram, or
2327 -- a type declaration.
2329 when Aspect_Ghost => Ghost : declare
2334 (Pragma_Argument_Associations => New_List (
2335 Make_Pragma_Argument_Association (Loc,
2336 Expression => Relocate_Node (Expr))),
2337 Pragma_Name => Name_Ghost);
2339 Decorate (Aspect, Aitem);
2341 -- When the aspect applies to a [generic] package, insert
2342 -- the pragma at the top of the visible declarations. This
2343 -- emulates the placement of a source pragma.
2345 if Nkind_In (N, N_Generic_Package_Declaration,
2346 N_Package_Declaration)
2348 Decls := Visible_Declarations (Specification (N));
2352 Set_Visible_Declarations (N, Decls);
2355 -- When aspects Abstract_State, Ghost, Initial_Condition
2356 -- and Initializes are out of order, ensure that pragma
2357 -- SPARK_Mode is always at the top of the declarations to
2358 -- properly enabled/suppress errors.
2360 Insert_After_SPARK_Mode
2362 Ins_Nod => First (Decls),
2365 -- Otherwise the context is an object, [generic] subprogram
2366 -- or type declaration.
2369 Insert_Pragma (Aitem);
2377 -- Aspect Global is never delayed because it is equivalent to
2378 -- a source pragma which appears after the related subprogram.
2379 -- To deal with forward references, the generated pragma is
2380 -- stored in the contract of the related subprogram and later
2381 -- analyzed at the end of the declarative region. See routine
2382 -- Analyze_Global_In_Decl_Part for details.
2384 when Aspect_Global =>
2386 (Pragma_Argument_Associations => New_List (
2387 Make_Pragma_Argument_Association (Loc,
2388 Expression => Relocate_Node (Expr))),
2389 Pragma_Name => Name_Global);
2391 Decorate (Aspect, Aitem);
2392 Insert_Pragma (Aitem);
2395 -- Initial_Condition
2397 -- Aspect Initial_Condition is never delayed because it is
2398 -- equivalent to a source pragma which appears after the
2399 -- related package. To deal with forward references, the
2400 -- generated pragma is stored in the contract of the related
2401 -- package and later analyzed at the end of the declarative
2402 -- region. See routine Analyze_Initial_Condition_In_Decl_Part
2405 when Aspect_Initial_Condition => Initial_Condition : declare
2406 Context : Node_Id := N;
2410 -- When aspect Initial_Condition appears on a generic
2411 -- package, it is propageted to the package instance. The
2412 -- context in this case is the instance spec.
2414 if Nkind (Context) = N_Package_Instantiation then
2415 Context := Instance_Spec (Context);
2418 if Nkind_In (Context, N_Generic_Package_Declaration,
2419 N_Package_Declaration)
2421 Decls := Visible_Declarations (Specification (Context));
2424 (Pragma_Argument_Associations => New_List (
2425 Make_Pragma_Argument_Association (Loc,
2426 Expression => Relocate_Node (Expr))),
2428 Name_Initial_Condition);
2429 Decorate (Aspect, Aitem);
2433 Set_Visible_Declarations (Context, Decls);
2436 -- When aspects Abstract_State, Ghost, Initial_Condition
2437 -- and Initializes are out of order, ensure that pragma
2438 -- SPARK_Mode is always at the top of the declarations to
2439 -- properly enabled/suppress errors.
2441 Insert_After_SPARK_Mode
2443 Ins_Nod => First (Decls),
2448 ("aspect & must apply to a package declaration",
2453 end Initial_Condition;
2457 -- Aspect Initializes is never delayed because it is equivalent
2458 -- to a source pragma appearing after the related package. To
2459 -- deal with forward references, the generated pragma is stored
2460 -- in the contract of the related package and later analyzed at
2461 -- the end of the declarative region. For details, see routine
2462 -- Analyze_Initializes_In_Decl_Part.
2464 when Aspect_Initializes => Initializes : declare
2465 Context : Node_Id := N;
2469 -- When aspect Initializes appears on a generic package,
2470 -- it is propageted to the package instance. The context
2471 -- in this case is the instance spec.
2473 if Nkind (Context) = N_Package_Instantiation then
2474 Context := Instance_Spec (Context);
2477 if Nkind_In (Context, N_Generic_Package_Declaration,
2478 N_Package_Declaration)
2480 Decls := Visible_Declarations (Specification (Context));
2483 (Pragma_Argument_Associations => New_List (
2484 Make_Pragma_Argument_Association (Loc,
2485 Expression => Relocate_Node (Expr))),
2486 Pragma_Name => Name_Initializes);
2487 Decorate (Aspect, Aitem);
2491 Set_Visible_Declarations (Context, Decls);
2494 -- When aspects Abstract_State, Ghost, Initial_Condition
2495 -- and Initializes are out of order, ensure that pragma
2496 -- SPARK_Mode is always at the top of the declarations to
2497 -- properly enabled/suppress errors.
2499 Insert_After_SPARK_Mode
2501 Ins_Nod => First (Decls),
2506 ("aspect & must apply to a package declaration",
2515 when Aspect_Obsolescent => declare
2523 Make_Pragma_Argument_Association (Sloc (Expr),
2524 Expression => Relocate_Node (Expr)));
2528 (Pragma_Argument_Associations => Args,
2529 Pragma_Name => Chars (Id));
2534 when Aspect_Part_Of =>
2535 if Nkind_In (N, N_Object_Declaration,
2536 N_Package_Instantiation)
2539 (Pragma_Argument_Associations => New_List (
2540 Make_Pragma_Argument_Association (Loc,
2541 Expression => Relocate_Node (Expr))),
2542 Pragma_Name => Name_Part_Of);
2546 ("aspect & must apply to a variable or package "
2547 & "instantiation", Aspect, Id);
2552 when Aspect_SPARK_Mode => SPARK_Mode : declare
2557 (Pragma_Argument_Associations => New_List (
2558 Make_Pragma_Argument_Association (Loc,
2559 Expression => Relocate_Node (Expr))),
2560 Pragma_Name => Name_SPARK_Mode);
2562 -- When the aspect appears on a package or a subprogram
2563 -- body, insert the generated pragma at the top of the body
2564 -- declarations to emulate the behavior of a source pragma.
2566 if Nkind_In (N, N_Package_Body, N_Subprogram_Body) then
2567 Decorate (Aspect, Aitem);
2569 Decls := Declarations (N);
2573 Set_Declarations (N, Decls);
2576 Prepend_To (Decls, Aitem);
2579 -- When the aspect is associated with a [generic] package
2580 -- declaration, insert the generated pragma at the top of
2581 -- the visible declarations to emulate the behavior of a
2584 elsif Nkind_In (N, N_Generic_Package_Declaration,
2585 N_Package_Declaration)
2587 Decorate (Aspect, Aitem);
2589 Decls := Visible_Declarations (Specification (N));
2593 Set_Visible_Declarations (Specification (N), Decls);
2596 Prepend_To (Decls, Aitem);
2603 -- Aspect Refined_Depends is never delayed because it is
2604 -- equivalent to a source pragma which appears in the
2605 -- declarations of the related subprogram body. To deal with
2606 -- forward references, the generated pragma is stored in the
2607 -- contract of the related subprogram body and later analyzed
2608 -- at the end of the declarative region. For details, see
2609 -- routine Analyze_Refined_Depends_In_Decl_Part.
2611 when Aspect_Refined_Depends =>
2613 (Pragma_Argument_Associations => New_List (
2614 Make_Pragma_Argument_Association (Loc,
2615 Expression => Relocate_Node (Expr))),
2616 Pragma_Name => Name_Refined_Depends);
2618 Decorate (Aspect, Aitem);
2619 Insert_Pragma (Aitem);
2624 -- Aspect Refined_Global is never delayed because it is
2625 -- equivalent to a source pragma which appears in the
2626 -- declarations of the related subprogram body. To deal with
2627 -- forward references, the generated pragma is stored in the
2628 -- contract of the related subprogram body and later analyzed
2629 -- at the end of the declarative region. For details, see
2630 -- routine Analyze_Refined_Global_In_Decl_Part.
2632 when Aspect_Refined_Global =>
2634 (Pragma_Argument_Associations => New_List (
2635 Make_Pragma_Argument_Association (Loc,
2636 Expression => Relocate_Node (Expr))),
2637 Pragma_Name => Name_Refined_Global);
2639 Decorate (Aspect, Aitem);
2640 Insert_Pragma (Aitem);
2645 when Aspect_Refined_Post =>
2647 (Pragma_Argument_Associations => New_List (
2648 Make_Pragma_Argument_Association (Loc,
2649 Expression => Relocate_Node (Expr))),
2650 Pragma_Name => Name_Refined_Post);
2654 when Aspect_Refined_State => Refined_State : declare
2658 -- The corresponding pragma for Refined_State is inserted in
2659 -- the declarations of the related package body. This action
2660 -- synchronizes both the source and from-aspect versions of
2663 if Nkind (N) = N_Package_Body then
2664 Decls := Declarations (N);
2667 (Pragma_Argument_Associations => New_List (
2668 Make_Pragma_Argument_Association (Loc,
2669 Expression => Relocate_Node (Expr))),
2670 Pragma_Name => Name_Refined_State);
2671 Decorate (Aspect, Aitem);
2675 Set_Declarations (N, Decls);
2678 -- Pragma Refined_State must be inserted after pragma
2679 -- SPARK_Mode in the tree. This ensures that any error
2680 -- messages dependent on SPARK_Mode will be properly
2681 -- enabled/suppressed.
2683 Insert_After_SPARK_Mode
2685 Ins_Nod => First (Decls),
2690 ("aspect & must apply to a package body", Aspect, Id);
2696 -- Relative_Deadline
2698 when Aspect_Relative_Deadline =>
2700 (Pragma_Argument_Associations => New_List (
2701 Make_Pragma_Argument_Association (Loc,
2702 Expression => Relocate_Node (Expr))),
2703 Pragma_Name => Name_Relative_Deadline);
2705 -- If the aspect applies to a task, the corresponding pragma
2706 -- must appear within its declarations, not after.
2708 if Nkind (N) = N_Task_Type_Declaration then
2714 if No (Task_Definition (N)) then
2715 Set_Task_Definition (N,
2716 Make_Task_Definition (Loc,
2717 Visible_Declarations => New_List,
2718 End_Label => Empty));
2721 Def := Task_Definition (N);
2722 V := Visible_Declarations (Def);
2723 if not Is_Empty_List (V) then
2724 Insert_Before (First (V), Aitem);
2727 Set_Visible_Declarations (Def, New_List (Aitem));
2734 -- Case 2e: Annotate aspect
2736 when Aspect_Annotate =>
2743 -- The argument can be a single identifier
2745 if Nkind (Expr) = N_Identifier then
2747 -- One level of parens is allowed
2749 if Paren_Count (Expr) > 1 then
2750 Error_Msg_F ("extra parentheses ignored", Expr);
2753 Set_Paren_Count (Expr, 0);
2755 -- Add the single item to the list
2757 Args := New_List (Expr);
2759 -- Otherwise we must have an aggregate
2761 elsif Nkind (Expr) = N_Aggregate then
2763 -- Must be positional
2765 if Present (Component_Associations (Expr)) then
2767 ("purely positional aggregate required", Expr);
2771 -- Must not be parenthesized
2773 if Paren_Count (Expr) /= 0 then
2774 Error_Msg_F ("extra parentheses ignored", Expr);
2777 -- List of arguments is list of aggregate expressions
2779 Args := Expressions (Expr);
2781 -- Anything else is illegal
2784 Error_Msg_F ("wrong form for Annotate aspect", Expr);
2788 -- Prepare pragma arguments
2791 Arg := First (Args);
2792 while Present (Arg) loop
2794 Make_Pragma_Argument_Association (Sloc (Arg),
2795 Expression => Relocate_Node (Arg)));
2800 Make_Pragma_Argument_Association (Sloc (Ent),
2801 Chars => Name_Entity,
2802 Expression => Ent));
2805 (Pragma_Argument_Associations => Pargs,
2806 Pragma_Name => Name_Annotate);
2809 -- Case 3 : Aspects that don't correspond to pragma/attribute
2810 -- definition clause.
2812 -- Case 3a: The aspects listed below don't correspond to
2813 -- pragmas/attributes but do require delayed analysis.
2815 -- Default_Value can only apply to a scalar type
2817 when Aspect_Default_Value =>
2818 if not Is_Scalar_Type (E) then
2820 ("aspect Default_Value must apply to a scalar type", N);
2825 -- Default_Component_Value can only apply to an array type
2826 -- with scalar components.
2828 when Aspect_Default_Component_Value =>
2829 if not (Is_Array_Type (E)
2830 and then Is_Scalar_Type (Component_Type (E)))
2832 Error_Msg_N ("aspect Default_Component_Value can only "
2833 & "apply to an array of scalar components", N);
2838 -- Case 3b: The aspects listed below don't correspond to
2839 -- pragmas/attributes and don't need delayed analysis.
2841 -- Implicit_Dereference
2843 -- For Implicit_Dereference, External_Name and Link_Name, only
2844 -- the legality checks are done during the analysis, thus no
2845 -- delay is required.
2847 when Aspect_Implicit_Dereference =>
2848 Analyze_Aspect_Implicit_Dereference;
2851 -- External_Name, Link_Name
2853 when Aspect_External_Name |
2855 Analyze_Aspect_External_Or_Link_Name;
2860 when Aspect_Dimension =>
2861 Analyze_Aspect_Dimension (N, Id, Expr);
2866 when Aspect_Dimension_System =>
2867 Analyze_Aspect_Dimension_System (N, Id, Expr);
2870 -- Case 4: Aspects requiring special handling
2872 -- Pre/Post/Test_Case/Contract_Cases whose corresponding
2873 -- pragmas take care of the delay.
2877 -- Aspects Pre/Post generate Precondition/Postcondition pragmas
2878 -- with a first argument that is the expression, and a second
2879 -- argument that is an informative message if the test fails.
2880 -- This is inserted right after the declaration, to get the
2881 -- required pragma placement. The processing for the pragmas
2882 -- takes care of the required delay.
2884 when Pre_Post_Aspects => Pre_Post : declare
2888 if A_Id = Aspect_Pre or else A_Id = Aspect_Precondition then
2889 Pname := Name_Precondition;
2891 Pname := Name_Postcondition;
2894 -- If the expressions is of the form A and then B, then
2895 -- we generate separate Pre/Post aspects for the separate
2896 -- clauses. Since we allow multiple pragmas, there is no
2897 -- problem in allowing multiple Pre/Post aspects internally.
2898 -- These should be treated in reverse order (B first and
2899 -- A second) since they are later inserted just after N in
2900 -- the order they are treated. This way, the pragma for A
2901 -- ends up preceding the pragma for B, which may have an
2902 -- importance for the error raised (either constraint error
2903 -- or precondition error).
2905 -- We do not do this for Pre'Class, since we have to put
2906 -- these conditions together in a complex OR expression.
2908 -- We do not do this in ASIS mode, as ASIS relies on the
2909 -- original node representing the complete expression, when
2910 -- retrieving it through the source aspect table.
2913 and then (Pname = Name_Postcondition
2914 or else not Class_Present (Aspect))
2916 while Nkind (Expr) = N_And_Then loop
2917 Insert_After (Aspect,
2918 Make_Aspect_Specification (Sloc (Left_Opnd (Expr)),
2919 Identifier => Identifier (Aspect),
2920 Expression => Relocate_Node (Left_Opnd (Expr)),
2921 Class_Present => Class_Present (Aspect),
2922 Split_PPC => True));
2923 Rewrite (Expr, Relocate_Node (Right_Opnd (Expr)));
2924 Eloc := Sloc (Expr);
2928 -- Build the precondition/postcondition pragma
2930 -- Add note about why we do NOT need Copy_Tree here???
2933 (Pragma_Argument_Associations => New_List (
2934 Make_Pragma_Argument_Association (Eloc,
2935 Chars => Name_Check,
2936 Expression => Relocate_Node (Expr))),
2937 Pragma_Name => Pname);
2939 -- Add message unless exception messages are suppressed
2941 if not Opt.Exception_Locations_Suppressed then
2942 Append_To (Pragma_Argument_Associations (Aitem),
2943 Make_Pragma_Argument_Association (Eloc,
2944 Chars => Name_Message,
2946 Make_String_Literal (Eloc,
2948 & Get_Name_String (Pname)
2950 & Build_Location_String (Eloc))));
2953 Set_Is_Delayed_Aspect (Aspect);
2955 -- For Pre/Post cases, insert immediately after the entity
2956 -- declaration, since that is the required pragma placement.
2957 -- Note that for these aspects, we do not have to worry
2958 -- about delay issues, since the pragmas themselves deal
2959 -- with delay of visibility for the expression analysis.
2961 Insert_Pragma (Aitem);
2968 when Aspect_Test_Case => Test_Case : declare
2970 Comp_Expr : Node_Id;
2971 Comp_Assn : Node_Id;
2977 if Nkind (Parent (N)) = N_Compilation_Unit then
2978 Error_Msg_Name_1 := Nam;
2979 Error_Msg_N ("incorrect placement of aspect `%`", E);
2983 if Nkind (Expr) /= N_Aggregate then
2984 Error_Msg_Name_1 := Nam;
2986 ("wrong syntax for aspect `%` for &", Id, E);
2990 -- Make pragma expressions refer to the original aspect
2991 -- expressions through the Original_Node link. This is used
2992 -- in semantic analysis for ASIS mode, so that the original
2993 -- expression also gets analyzed.
2995 Comp_Expr := First (Expressions (Expr));
2996 while Present (Comp_Expr) loop
2997 New_Expr := Relocate_Node (Comp_Expr);
2999 Make_Pragma_Argument_Association (Sloc (Comp_Expr),
3000 Expression => New_Expr));
3004 Comp_Assn := First (Component_Associations (Expr));
3005 while Present (Comp_Assn) loop
3006 if List_Length (Choices (Comp_Assn)) /= 1
3008 Nkind (First (Choices (Comp_Assn))) /= N_Identifier
3010 Error_Msg_Name_1 := Nam;
3012 ("wrong syntax for aspect `%` for &", Id, E);
3017 Make_Pragma_Argument_Association (Sloc (Comp_Assn),
3018 Chars => Chars (First (Choices (Comp_Assn))),
3020 Relocate_Node (Expression (Comp_Assn))));
3024 -- Build the test-case pragma
3027 (Pragma_Argument_Associations => Args,
3028 Pragma_Name => Nam);
3033 when Aspect_Contract_Cases =>
3035 (Pragma_Argument_Associations => New_List (
3036 Make_Pragma_Argument_Association (Loc,
3037 Expression => Relocate_Node (Expr))),
3038 Pragma_Name => Nam);
3040 Decorate (Aspect, Aitem);
3041 Insert_Pragma (Aitem);
3044 -- Case 5: Special handling for aspects with an optional
3045 -- boolean argument.
3047 -- In the general case, the corresponding pragma cannot be
3048 -- generated yet because the evaluation of the boolean needs
3049 -- to be delayed till the freeze point.
3051 when Boolean_Aspects |
3052 Library_Unit_Aspects =>
3054 Set_Is_Boolean_Aspect (Aspect);
3056 -- Lock_Free aspect only apply to protected objects
3058 if A_Id = Aspect_Lock_Free then
3059 if Ekind (E) /= E_Protected_Type then
3060 Error_Msg_Name_1 := Nam;
3062 ("aspect % only applies to a protected object",
3066 -- Set the Uses_Lock_Free flag to True if there is no
3067 -- expression or if the expression is True. The
3068 -- evaluation of this aspect should be delayed to the
3069 -- freeze point (why???)
3072 or else Is_True (Static_Boolean (Expr))
3074 Set_Uses_Lock_Free (E);
3077 Record_Rep_Item (E, Aspect);
3082 elsif A_Id = Aspect_Import or else A_Id = Aspect_Export then
3084 -- For the case of aspects Import and Export, we don't
3085 -- consider that we know the entity is never set in the
3086 -- source, since it is is likely modified outside the
3089 -- Note: one might think that the analysis of the
3090 -- resulting pragma would take care of that, but
3091 -- that's not the case since it won't be from source.
3093 if Ekind (E) = E_Variable then
3094 Set_Never_Set_In_Source (E, False);
3097 -- In older versions of Ada the corresponding pragmas
3098 -- specified a Convention. In Ada 2012 the convention is
3099 -- specified as a separate aspect, and it is optional,
3100 -- given that it defaults to Convention_Ada. The code
3101 -- that verifed that there was a matching convention
3104 -- Resolve the expression of an Import or Export here,
3105 -- and require it to be of type Boolean and static. This
3106 -- is not quite right, because in general this should be
3107 -- delayed, but that seems tricky for these, because
3108 -- normally Boolean aspects are replaced with pragmas at
3109 -- the freeze point (in Make_Pragma_From_Boolean_Aspect),
3110 -- but in the case of these aspects we can't generate
3111 -- a simple pragma with just the entity name. ???
3113 if not Present (Expr)
3114 or else Is_True (Static_Boolean (Expr))
3116 if A_Id = Aspect_Import then
3117 Set_Is_Imported (E);
3119 -- An imported entity cannot have an explicit
3122 if Nkind (N) = N_Object_Declaration
3123 and then Present (Expression (N))
3126 ("imported entities cannot be initialized "
3127 & "(RM B.1(24))", Expression (N));
3130 elsif A_Id = Aspect_Export then
3131 Set_Is_Exported (E);
3138 -- Library unit aspects require special handling in the case
3139 -- of a package declaration, the pragma needs to be inserted
3140 -- in the list of declarations for the associated package.
3141 -- There is no issue of visibility delay for these aspects.
3143 if A_Id in Library_Unit_Aspects
3145 Nkind_In (N, N_Package_Declaration,
3146 N_Generic_Package_Declaration)
3147 and then Nkind (Parent (N)) /= N_Compilation_Unit
3149 -- Aspect is legal on a local instantiation of a library-
3150 -- level generic unit.
3152 and then not Is_Generic_Instance (Defining_Entity (N))
3155 ("incorrect context for library unit aspect&", Id);
3159 -- External property aspects are Boolean by nature, but
3160 -- their pragmas must contain two arguments, the second
3161 -- being the optional Boolean expression.
3163 if A_Id = Aspect_Async_Readers or else
3164 A_Id = Aspect_Async_Writers or else
3165 A_Id = Aspect_Effective_Reads or else
3166 A_Id = Aspect_Effective_Writes
3172 -- The first argument of the external property pragma
3173 -- is the related object.
3177 Make_Pragma_Argument_Association (Sloc (Ent),
3178 Expression => Ent));
3180 -- The second argument is the optional Boolean
3181 -- expression which must be propagated even if it
3182 -- evaluates to False as this has special semantic
3185 if Present (Expr) then
3187 Make_Pragma_Argument_Association (Loc,
3188 Expression => Relocate_Node (Expr)));
3192 (Pragma_Argument_Associations => Args,
3193 Pragma_Name => Nam);
3196 -- Cases where we do not delay, includes all cases where the
3197 -- expression is missing other than the above cases.
3199 elsif not Delay_Required or else No (Expr) then
3201 (Pragma_Argument_Associations => New_List (
3202 Make_Pragma_Argument_Association (Sloc (Ent),
3203 Expression => Ent)),
3204 Pragma_Name => Chars (Id));
3205 Delay_Required := False;
3207 -- In general cases, the corresponding pragma/attribute
3208 -- definition clause will be inserted later at the freezing
3209 -- point, and we do not need to build it now.
3217 -- This is special because for access types we need to generate
3218 -- an attribute definition clause. This also works for single
3219 -- task declarations, but it does not work for task type
3220 -- declarations, because we have the case where the expression
3221 -- references a discriminant of the task type. That can't use
3222 -- an attribute definition clause because we would not have
3223 -- visibility on the discriminant. For that case we must
3224 -- generate a pragma in the task definition.
3226 when Aspect_Storage_Size =>
3230 if Ekind (E) = E_Task_Type then
3232 Decl : constant Node_Id := Declaration_Node (E);
3235 pragma Assert (Nkind (Decl) = N_Task_Type_Declaration);
3237 -- If no task definition, create one
3239 if No (Task_Definition (Decl)) then
3240 Set_Task_Definition (Decl,
3241 Make_Task_Definition (Loc,
3242 Visible_Declarations => Empty_List,
3243 End_Label => Empty));
3246 -- Create a pragma and put it at the start of the task
3247 -- definition for the task type declaration.
3250 (Pragma_Argument_Associations => New_List (
3251 Make_Pragma_Argument_Association (Loc,
3252 Expression => Relocate_Node (Expr))),
3253 Pragma_Name => Name_Storage_Size);
3257 Visible_Declarations (Task_Definition (Decl)));
3261 -- All other cases, generate attribute definition
3265 Make_Attribute_Definition_Clause (Loc,
3267 Chars => Chars (Id),
3268 Expression => Relocate_Node (Expr));
3272 -- Attach the corresponding pragma/attribute definition clause to
3273 -- the aspect specification node.
3275 if Present (Aitem) then
3276 Set_From_Aspect_Specification (Aitem);
3279 -- In the context of a compilation unit, we directly put the
3280 -- pragma in the Pragmas_After list of the N_Compilation_Unit_Aux
3281 -- node (no delay is required here) except for aspects on a
3282 -- subprogram body (see below) and a generic package, for which we
3283 -- need to introduce the pragma before building the generic copy
3284 -- (see sem_ch12), and for package instantiations, where the
3285 -- library unit pragmas are better handled early.
3287 if Nkind (Parent (N)) = N_Compilation_Unit
3288 and then (Present (Aitem) or else Is_Boolean_Aspect (Aspect))
3291 Aux : constant Node_Id := Aux_Decls_Node (Parent (N));
3294 pragma Assert (Nkind (Aux) = N_Compilation_Unit_Aux);
3296 -- For a Boolean aspect, create the corresponding pragma if
3297 -- no expression or if the value is True.
3299 if Is_Boolean_Aspect (Aspect) and then No (Aitem) then
3300 if Is_True (Static_Boolean (Expr)) then
3302 (Pragma_Argument_Associations => New_List (
3303 Make_Pragma_Argument_Association (Sloc (Ent),
3304 Expression => Ent)),
3305 Pragma_Name => Chars (Id));
3307 Set_From_Aspect_Specification (Aitem, True);
3308 Set_Corresponding_Aspect (Aitem, Aspect);
3315 -- If the aspect is on a subprogram body (relevant aspect
3316 -- is Inline), add the pragma in front of the declarations.
3318 if Nkind (N) = N_Subprogram_Body then
3319 if No (Declarations (N)) then
3320 Set_Declarations (N, New_List);
3323 Prepend (Aitem, Declarations (N));
3325 elsif Nkind (N) = N_Generic_Package_Declaration then
3326 if No (Visible_Declarations (Specification (N))) then
3327 Set_Visible_Declarations (Specification (N), New_List);
3331 Visible_Declarations (Specification (N)));
3333 elsif Nkind (N) = N_Package_Instantiation then
3335 Spec : constant Node_Id :=
3336 Specification (Instance_Spec (N));
3338 if No (Visible_Declarations (Spec)) then
3339 Set_Visible_Declarations (Spec, New_List);
3342 Prepend (Aitem, Visible_Declarations (Spec));
3346 if No (Pragmas_After (Aux)) then
3347 Set_Pragmas_After (Aux, New_List);
3350 Append (Aitem, Pragmas_After (Aux));
3357 -- The evaluation of the aspect is delayed to the freezing point.
3358 -- The pragma or attribute clause if there is one is then attached
3359 -- to the aspect specification which is put in the rep item list.
3361 if Delay_Required then
3362 if Present (Aitem) then
3363 Set_Is_Delayed_Aspect (Aitem);
3364 Set_Aspect_Rep_Item (Aspect, Aitem);
3365 Set_Parent (Aitem, Aspect);
3368 Set_Is_Delayed_Aspect (Aspect);
3370 -- In the case of Default_Value, link the aspect to base type
3371 -- as well, even though it appears on a first subtype. This is
3372 -- mandated by the semantics of the aspect. Do not establish
3373 -- the link when processing the base type itself as this leads
3374 -- to a rep item circularity. Verify that we are dealing with
3375 -- a scalar type to prevent cascaded errors.
3377 if A_Id = Aspect_Default_Value
3378 and then Is_Scalar_Type (E)
3379 and then Base_Type (E) /= E
3381 Set_Has_Delayed_Aspects (Base_Type (E));
3382 Record_Rep_Item (Base_Type (E), Aspect);
3385 Set_Has_Delayed_Aspects (E);
3386 Record_Rep_Item (E, Aspect);
3388 -- When delay is not required and the context is a package or a
3389 -- subprogram body, insert the pragma in the body declarations.
3391 elsif Nkind_In (N, N_Package_Body, N_Subprogram_Body) then
3392 if No (Declarations (N)) then
3393 Set_Declarations (N, New_List);
3396 -- The pragma is added before source declarations
3398 Prepend_To (Declarations (N), Aitem);
3400 -- When delay is not required and the context is not a compilation
3401 -- unit, we simply insert the pragma/attribute definition clause
3405 Insert_After (Ins_Node, Aitem);
3408 end Analyze_One_Aspect;
3412 end loop Aspect_Loop;
3414 if Has_Delayed_Aspects (E) then
3415 Ensure_Freeze_Node (E);
3417 end Analyze_Aspect_Specifications;
3419 ---------------------------------------------------
3420 -- Analyze_Aspect_Specifications_On_Body_Or_Stub --
3421 ---------------------------------------------------
3423 procedure Analyze_Aspect_Specifications_On_Body_Or_Stub (N : Node_Id) is
3424 Body_Id : constant Entity_Id := Defining_Entity (N);
3426 procedure Diagnose_Misplaced_Aspects (Spec_Id : Entity_Id);
3427 -- Subprogram body [stub] N has aspects, but they are not properly
3428 -- placed. Emit an error message depending on the aspects involved.
3429 -- Spec_Id is the entity of the corresponding spec.
3431 --------------------------------
3432 -- Diagnose_Misplaced_Aspects --
3433 --------------------------------
3435 procedure Diagnose_Misplaced_Aspects (Spec_Id : Entity_Id) is
3436 procedure Misplaced_Aspect_Error
3439 -- Emit an error message concerning misplaced aspect Asp. Ref_Nam is
3440 -- the name of the refined version of the aspect.
3442 ----------------------------
3443 -- Misplaced_Aspect_Error --
3444 ----------------------------
3446 procedure Misplaced_Aspect_Error
3450 Asp_Nam : constant Name_Id := Chars (Identifier (Asp));
3451 Asp_Id : constant Aspect_Id := Get_Aspect_Id (Asp_Nam);
3454 -- The corresponding spec already contains the aspect in question
3455 -- and the one appearing on the body must be the refined form:
3457 -- procedure P with Global ...;
3458 -- procedure P with Global ... is ... end P;
3462 if Has_Aspect (Spec_Id, Asp_Id) then
3463 Error_Msg_Name_1 := Asp_Nam;
3465 -- Subunits cannot carry aspects that apply to a subprogram
3468 if Nkind (Parent (N)) = N_Subunit then
3469 Error_Msg_N ("aspect % cannot apply to a subunit", Asp);
3471 -- Otherwise suggest the refined form
3474 Error_Msg_Name_2 := Ref_Nam;
3475 Error_Msg_N ("aspect % should be %", Asp);
3478 -- Otherwise the aspect must appear on the spec, not on the body
3481 -- procedure P with Global ... is ... end P;
3485 ("aspect specification must appear in subprogram declaration",
3488 end Misplaced_Aspect_Error;
3495 -- Start of processing for Diagnose_Misplaced_Aspects
3498 -- Iterate over the aspect specifications and emit specific errors
3499 -- where applicable.
3501 Asp := First (Aspect_Specifications (N));
3502 while Present (Asp) loop
3503 Asp_Nam := Chars (Identifier (Asp));
3505 -- Do not emit errors on aspects that can appear on a subprogram
3506 -- body. This scenario occurs when the aspect specification list
3507 -- contains both misplaced and properly placed aspects.
3509 if Aspect_On_Body_Or_Stub_OK (Get_Aspect_Id (Asp_Nam)) then
3512 -- Special diagnostics for SPARK aspects
3514 elsif Asp_Nam = Name_Depends then
3515 Misplaced_Aspect_Error (Asp, Name_Refined_Depends);
3517 elsif Asp_Nam = Name_Global then
3518 Misplaced_Aspect_Error (Asp, Name_Refined_Global);
3520 elsif Asp_Nam = Name_Post then
3521 Misplaced_Aspect_Error (Asp, Name_Refined_Post);
3523 -- Otherwise a language-defined aspect is misplaced
3527 ("aspect specification must appear in subprogram declaration",
3533 end Diagnose_Misplaced_Aspects;
3537 Spec_Id : Entity_Id;
3539 -- Start of processing for Analyze_Aspects_On_Body_Or_Stub
3542 if Nkind (N) = N_Subprogram_Body_Stub then
3543 Spec_Id := Corresponding_Spec_Of_Stub (N);
3545 Spec_Id := Corresponding_Spec (N);
3548 -- Language-defined aspects cannot be associated with a subprogram body
3549 -- [stub] if the subprogram has a spec. Certain implementation defined
3550 -- aspects are allowed to break this rule (for all applicable cases, see
3551 -- table Aspects.Aspect_On_Body_Or_Stub_OK).
3553 if Present (Spec_Id) and then not Aspects_On_Body_Or_Stub_OK (N) then
3554 Diagnose_Misplaced_Aspects (Spec_Id);
3556 Analyze_Aspect_Specifications (N, Body_Id);
3558 end Analyze_Aspect_Specifications_On_Body_Or_Stub;
3560 -----------------------
3561 -- Analyze_At_Clause --
3562 -----------------------
3564 -- An at clause is replaced by the corresponding Address attribute
3565 -- definition clause that is the preferred approach in Ada 95.
3567 procedure Analyze_At_Clause (N : Node_Id) is
3568 CS : constant Boolean := Comes_From_Source (N);
3571 -- This is an obsolescent feature
3573 Check_Restriction (No_Obsolescent_Features, N);
3575 if Warn_On_Obsolescent_Feature then
3577 ("?j?at clause is an obsolescent feature (RM J.7(2))", N);
3579 ("\?j?use address attribute definition clause instead", N);
3582 -- Rewrite as address clause
3585 Make_Attribute_Definition_Clause (Sloc (N),
3586 Name => Identifier (N),
3587 Chars => Name_Address,
3588 Expression => Expression (N)));
3590 -- We preserve Comes_From_Source, since logically the clause still comes
3591 -- from the source program even though it is changed in form.
3593 Set_Comes_From_Source (N, CS);
3595 -- Analyze rewritten clause
3597 Analyze_Attribute_Definition_Clause (N);
3598 end Analyze_At_Clause;
3600 -----------------------------------------
3601 -- Analyze_Attribute_Definition_Clause --
3602 -----------------------------------------
3604 procedure Analyze_Attribute_Definition_Clause (N : Node_Id) is
3605 Loc : constant Source_Ptr := Sloc (N);
3606 Nam : constant Node_Id := Name (N);
3607 Attr : constant Name_Id := Chars (N);
3608 Expr : constant Node_Id := Expression (N);
3609 Id : constant Attribute_Id := Get_Attribute_Id (Attr);
3612 -- The entity of Nam after it is analyzed. In the case of an incomplete
3613 -- type, this is the underlying type.
3616 -- The underlying entity to which the attribute applies. Generally this
3617 -- is the Underlying_Type of Ent, except in the case where the clause
3618 -- applies to full view of incomplete type or private type in which case
3619 -- U_Ent is just a copy of Ent.
3621 FOnly : Boolean := False;
3622 -- Reset to True for subtype specific attribute (Alignment, Size)
3623 -- and for stream attributes, i.e. those cases where in the call to
3624 -- Rep_Item_Too_Late, FOnly is set True so that only the freezing rules
3625 -- are checked. Note that the case of stream attributes is not clear
3626 -- from the RM, but see AI95-00137. Also, the RM seems to disallow
3627 -- Storage_Size for derived task types, but that is also clearly
3630 procedure Analyze_Stream_TSS_Definition (TSS_Nam : TSS_Name_Type);
3631 -- Common processing for 'Read, 'Write, 'Input and 'Output attribute
3632 -- definition clauses.
3634 function Duplicate_Clause return Boolean;
3635 -- This routine checks if the aspect for U_Ent being given by attribute
3636 -- definition clause N is for an aspect that has already been specified,
3637 -- and if so gives an error message. If there is a duplicate, True is
3638 -- returned, otherwise if there is no error, False is returned.
3640 procedure Check_Indexing_Functions;
3641 -- Check that the function in Constant_Indexing or Variable_Indexing
3642 -- attribute has the proper type structure. If the name is overloaded,
3643 -- check that some interpretation is legal.
3645 procedure Check_Iterator_Functions;
3646 -- Check that there is a single function in Default_Iterator attribute
3647 -- has the proper type structure.
3649 function Check_Primitive_Function (Subp : Entity_Id) return Boolean;
3650 -- Common legality check for the previous two
3652 -----------------------------------
3653 -- Analyze_Stream_TSS_Definition --
3654 -----------------------------------
3656 procedure Analyze_Stream_TSS_Definition (TSS_Nam : TSS_Name_Type) is
3657 Subp : Entity_Id := Empty;
3662 Is_Read : constant Boolean := (TSS_Nam = TSS_Stream_Read);
3663 -- True for Read attribute, false for other attributes
3665 function Has_Good_Profile (Subp : Entity_Id) return Boolean;
3666 -- Return true if the entity is a subprogram with an appropriate
3667 -- profile for the attribute being defined.
3669 ----------------------
3670 -- Has_Good_Profile --
3671 ----------------------
3673 function Has_Good_Profile (Subp : Entity_Id) return Boolean is
3675 Is_Function : constant Boolean := (TSS_Nam = TSS_Stream_Input);
3676 Expected_Ekind : constant array (Boolean) of Entity_Kind :=
3677 (False => E_Procedure, True => E_Function);
3681 if Ekind (Subp) /= Expected_Ekind (Is_Function) then
3685 F := First_Formal (Subp);
3688 or else Ekind (Etype (F)) /= E_Anonymous_Access_Type
3689 or else Designated_Type (Etype (F)) /=
3690 Class_Wide_Type (RTE (RE_Root_Stream_Type))
3695 if not Is_Function then
3699 Expected_Mode : constant array (Boolean) of Entity_Kind :=
3700 (False => E_In_Parameter,
3701 True => E_Out_Parameter);
3703 if Parameter_Mode (F) /= Expected_Mode (Is_Read) then
3710 -- If the attribute specification comes from an aspect
3711 -- specification for a class-wide stream, the parameter must be
3712 -- a class-wide type of the entity to which the aspect applies.
3714 if From_Aspect_Specification (N)
3715 and then Class_Present (Parent (N))
3716 and then Is_Class_Wide_Type (Typ)
3722 Typ := Etype (Subp);
3725 -- Verify that the prefix of the attribute and the local name for
3726 -- the type of the formal match, or one is the class-wide of the
3727 -- other, in the case of a class-wide stream operation.
3729 if Base_Type (Typ) = Base_Type (Ent)
3730 or else (Is_Class_Wide_Type (Typ)
3731 and then Typ = Class_Wide_Type (Base_Type (Ent)))
3732 or else (Is_Class_Wide_Type (Ent)
3733 and then Ent = Class_Wide_Type (Base_Type (Typ)))
3740 if Present ((Next_Formal (F)))
3744 elsif not Is_Scalar_Type (Typ)
3745 and then not Is_First_Subtype (Typ)
3746 and then not Is_Class_Wide_Type (Typ)
3753 end Has_Good_Profile;
3755 -- Start of processing for Analyze_Stream_TSS_Definition
3760 if not Is_Type (U_Ent) then
3761 Error_Msg_N ("local name must be a subtype", Nam);
3764 elsif not Is_First_Subtype (U_Ent) then
3765 Error_Msg_N ("local name must be a first subtype", Nam);
3769 Pnam := TSS (Base_Type (U_Ent), TSS_Nam);
3771 -- If Pnam is present, it can be either inherited from an ancestor
3772 -- type (in which case it is legal to redefine it for this type), or
3773 -- be a previous definition of the attribute for the same type (in
3774 -- which case it is illegal).
3776 -- In the first case, it will have been analyzed already, and we
3777 -- can check that its profile does not match the expected profile
3778 -- for a stream attribute of U_Ent. In the second case, either Pnam
3779 -- has been analyzed (and has the expected profile), or it has not
3780 -- been analyzed yet (case of a type that has not been frozen yet
3781 -- and for which the stream attribute has been set using Set_TSS).
3784 and then (No (First_Entity (Pnam)) or else Has_Good_Profile (Pnam))
3786 Error_Msg_Sloc := Sloc (Pnam);
3787 Error_Msg_Name_1 := Attr;
3788 Error_Msg_N ("% attribute already defined #", Nam);
3794 if Is_Entity_Name (Expr) then
3795 if not Is_Overloaded (Expr) then
3796 if Has_Good_Profile (Entity (Expr)) then
3797 Subp := Entity (Expr);
3801 Get_First_Interp (Expr, I, It);
3802 while Present (It.Nam) loop
3803 if Has_Good_Profile (It.Nam) then
3808 Get_Next_Interp (I, It);
3813 if Present (Subp) then
3814 if Is_Abstract_Subprogram (Subp) then
3815 Error_Msg_N ("stream subprogram must not be abstract", Expr);
3818 -- A stream subprogram for an interface type must be a null
3819 -- procedure (RM 13.13.2 (38/3)).
3821 elsif Is_Interface (U_Ent)
3822 and then not Is_Class_Wide_Type (U_Ent)
3823 and then not Inside_A_Generic
3825 (Ekind (Subp) = E_Function
3829 (Unit_Declaration_Node (Ultimate_Alias (Subp)))))
3832 ("stream subprogram for interface type "
3833 & "must be null procedure", Expr);
3836 Set_Entity (Expr, Subp);
3837 Set_Etype (Expr, Etype (Subp));
3839 New_Stream_Subprogram (N, U_Ent, Subp, TSS_Nam);
3842 Error_Msg_Name_1 := Attr;
3843 Error_Msg_N ("incorrect expression for% attribute", Expr);
3845 end Analyze_Stream_TSS_Definition;
3847 ------------------------------
3848 -- Check_Indexing_Functions --
3849 ------------------------------
3851 procedure Check_Indexing_Functions is
3852 Indexing_Found : Boolean := False;
3854 procedure Check_One_Function (Subp : Entity_Id);
3855 -- Check one possible interpretation. Sets Indexing_Found True if a
3856 -- legal indexing function is found.
3858 procedure Illegal_Indexing (Msg : String);
3859 -- Diagnose illegal indexing function if not overloaded. In the
3860 -- overloaded case indicate that no legal interpretation exists.
3862 ------------------------
3863 -- Check_One_Function --
3864 ------------------------
3866 procedure Check_One_Function (Subp : Entity_Id) is
3867 Default_Element : Node_Id;
3868 Ret_Type : constant Entity_Id := Etype (Subp);
3871 if not Is_Overloadable (Subp) then
3872 Illegal_Indexing ("illegal indexing function for type&");
3875 elsif Scope (Subp) /= Scope (Ent) then
3876 if Nkind (Expr) = N_Expanded_Name then
3878 -- Indexing function can't be declared elsewhere
3881 ("indexing function must be declared in scope of type&");
3886 elsif No (First_Formal (Subp)) then
3888 ("Indexing requires a function that applies to type&");
3891 elsif No (Next_Formal (First_Formal (Subp))) then
3893 ("indexing function must have at least two parameters");
3896 elsif Is_Derived_Type (Ent) then
3897 if (Attr = Name_Constant_Indexing
3899 (Find_Aspect (Etype (Ent), Aspect_Constant_Indexing)))
3901 (Attr = Name_Variable_Indexing
3903 (Find_Aspect (Etype (Ent), Aspect_Variable_Indexing)))
3905 if Debug_Flag_Dot_XX then
3910 ("indexing function already inherited "
3911 & "from parent type");
3917 if not Check_Primitive_Function (Subp) then
3919 ("Indexing aspect requires a function that applies to type&");
3923 -- If partial declaration exists, verify that it is not tagged.
3925 if Ekind (Current_Scope) = E_Package
3926 and then Has_Private_Declaration (Ent)
3927 and then From_Aspect_Specification (N)
3929 List_Containing (Parent (Ent)) =
3930 Private_Declarations
3931 (Specification (Unit_Declaration_Node (Current_Scope)))
3932 and then Nkind (N) = N_Attribute_Definition_Clause
3939 First (Visible_Declarations
3941 (Unit_Declaration_Node (Current_Scope))));
3943 while Present (Decl) loop
3944 if Nkind (Decl) = N_Private_Type_Declaration
3945 and then Ent = Full_View (Defining_Identifier (Decl))
3946 and then Tagged_Present (Decl)
3947 and then No (Aspect_Specifications (Decl))
3950 ("Indexing aspect cannot be specified on full view "
3951 & "if partial view is tagged");
3960 -- An indexing function must return either the default element of
3961 -- the container, or a reference type. For variable indexing it
3962 -- must be the latter.
3965 Find_Value_Of_Aspect
3966 (Etype (First_Formal (Subp)), Aspect_Iterator_Element);
3968 if Present (Default_Element) then
3969 Analyze (Default_Element);
3971 if Is_Entity_Name (Default_Element)
3972 and then not Covers (Entity (Default_Element), Ret_Type)
3976 ("wrong return type for indexing function");
3981 -- For variable_indexing the return type must be a reference type
3983 if Attr = Name_Variable_Indexing then
3984 if not Has_Implicit_Dereference (Ret_Type) then
3986 ("variable indexing must return a reference type");
3989 elsif Is_Access_Constant
3990 (Etype (First_Discriminant (Ret_Type)))
3993 ("variable indexing must return an access to variable");
3998 if Has_Implicit_Dereference (Ret_Type)
4000 Is_Access_Constant (Etype (First_Discriminant (Ret_Type)))
4003 ("constant indexing must return an access to constant");
4006 elsif Is_Access_Type (Etype (First_Formal (Subp)))
4007 and then not Is_Access_Constant (Etype (First_Formal (Subp)))
4010 ("constant indexing must apply to an access to constant");
4015 -- All checks succeeded.
4017 Indexing_Found := True;
4018 end Check_One_Function;
4020 -----------------------
4021 -- Illegal_Indexing --
4022 -----------------------
4024 procedure Illegal_Indexing (Msg : String) is
4026 Error_Msg_NE (Msg, N, Ent);
4027 end Illegal_Indexing;
4029 -- Start of processing for Check_Indexing_Functions
4038 if not Is_Overloaded (Expr) then
4039 Check_One_Function (Entity (Expr));
4047 Indexing_Found := False;
4048 Get_First_Interp (Expr, I, It);
4049 while Present (It.Nam) loop
4051 -- Note that analysis will have added the interpretation
4052 -- that corresponds to the dereference. We only check the
4053 -- subprogram itself.
4055 if Is_Overloadable (It.Nam) then
4056 Check_One_Function (It.Nam);
4059 Get_Next_Interp (I, It);
4064 if not Indexing_Found and then not Error_Posted (N) then
4066 ("aspect Indexing requires a local function that "
4067 & "applies to type&", Expr, Ent);
4069 end Check_Indexing_Functions;
4071 ------------------------------
4072 -- Check_Iterator_Functions --
4073 ------------------------------
4075 procedure Check_Iterator_Functions is
4076 Default : Entity_Id;
4078 function Valid_Default_Iterator (Subp : Entity_Id) return Boolean;
4079 -- Check one possible interpretation for validity
4081 ----------------------------
4082 -- Valid_Default_Iterator --
4083 ----------------------------
4085 function Valid_Default_Iterator (Subp : Entity_Id) return Boolean is
4089 if not Check_Primitive_Function (Subp) then
4092 Formal := First_Formal (Subp);
4095 -- False if any subsequent formal has no default expression
4097 Formal := Next_Formal (Formal);
4098 while Present (Formal) loop
4099 if No (Expression (Parent (Formal))) then
4103 Next_Formal (Formal);
4106 -- True if all subsequent formals have default expressions
4109 end Valid_Default_Iterator;
4111 -- Start of processing for Check_Iterator_Functions
4116 if not Is_Entity_Name (Expr) then
4117 Error_Msg_N ("aspect Iterator must be a function name", Expr);
4120 if not Is_Overloaded (Expr) then
4121 if not Check_Primitive_Function (Entity (Expr)) then
4123 ("aspect Indexing requires a function that applies to type&",
4124 Entity (Expr), Ent);
4127 if not Valid_Default_Iterator (Entity (Expr)) then
4128 Error_Msg_N ("improper function for default iterator", Expr);
4138 Get_First_Interp (Expr, I, It);
4139 while Present (It.Nam) loop
4140 if not Check_Primitive_Function (It.Nam)
4141 or else not Valid_Default_Iterator (It.Nam)
4145 elsif Present (Default) then
4146 Error_Msg_N ("default iterator must be unique", Expr);
4152 Get_Next_Interp (I, It);
4156 if Present (Default) then
4157 Set_Entity (Expr, Default);
4158 Set_Is_Overloaded (Expr, False);
4161 end Check_Iterator_Functions;
4163 -------------------------------
4164 -- Check_Primitive_Function --
4165 -------------------------------
4167 function Check_Primitive_Function (Subp : Entity_Id) return Boolean is
4171 if Ekind (Subp) /= E_Function then
4175 if No (First_Formal (Subp)) then
4178 Ctrl := Etype (First_Formal (Subp));
4181 -- Type of formal may be the class-wide type, an access to such,
4182 -- or an incomplete view.
4185 or else Ctrl = Class_Wide_Type (Ent)
4187 (Ekind (Ctrl) = E_Anonymous_Access_Type
4188 and then (Designated_Type (Ctrl) = Ent
4190 Designated_Type (Ctrl) = Class_Wide_Type (Ent)))
4192 (Ekind (Ctrl) = E_Incomplete_Type
4193 and then Full_View (Ctrl) = Ent)
4201 end Check_Primitive_Function;
4203 ----------------------
4204 -- Duplicate_Clause --
4205 ----------------------
4207 function Duplicate_Clause return Boolean is
4211 -- Nothing to do if this attribute definition clause comes from
4212 -- an aspect specification, since we could not be duplicating an
4213 -- explicit clause, and we dealt with the case of duplicated aspects
4214 -- in Analyze_Aspect_Specifications.
4216 if From_Aspect_Specification (N) then
4220 -- Otherwise current clause may duplicate previous clause, or a
4221 -- previously given pragma or aspect specification for the same
4224 A := Get_Rep_Item (U_Ent, Chars (N), Check_Parents => False);
4227 Error_Msg_Name_1 := Chars (N);
4228 Error_Msg_Sloc := Sloc (A);
4230 Error_Msg_NE ("aspect% for & previously given#", N, U_Ent);
4235 end Duplicate_Clause;
4237 -- Start of processing for Analyze_Attribute_Definition_Clause
4240 -- The following code is a defense against recursion. Not clear that
4241 -- this can happen legitimately, but perhaps some error situations can
4242 -- cause it, and we did see this recursion during testing.
4244 if Analyzed (N) then
4247 Set_Analyzed (N, True);
4250 -- Ignore some selected attributes in CodePeer mode since they are not
4251 -- relevant in this context.
4253 if CodePeer_Mode then
4256 -- Ignore Component_Size in CodePeer mode, to avoid changing the
4257 -- internal representation of types by implicitly packing them.
4259 when Attribute_Component_Size =>
4260 Rewrite (N, Make_Null_Statement (Sloc (N)));
4268 -- Process Ignore_Rep_Clauses option
4270 if Ignore_Rep_Clauses then
4273 -- The following should be ignored. They do not affect legality
4274 -- and may be target dependent. The basic idea of -gnatI is to
4275 -- ignore any rep clauses that may be target dependent but do not
4276 -- affect legality (except possibly to be rejected because they
4277 -- are incompatible with the compilation target).
4279 when Attribute_Alignment |
4280 Attribute_Bit_Order |
4281 Attribute_Component_Size |
4282 Attribute_Machine_Radix |
4283 Attribute_Object_Size |
4286 Attribute_Stream_Size |
4287 Attribute_Value_Size =>
4288 Kill_Rep_Clause (N);
4291 -- The following should not be ignored, because in the first place
4292 -- they are reasonably portable, and should not cause problems
4293 -- in compiling code from another target, and also they do affect
4294 -- legality, e.g. failing to provide a stream attribute for a type
4295 -- may make a program illegal.
4297 when Attribute_External_Tag |
4301 Attribute_Simple_Storage_Pool |
4302 Attribute_Storage_Pool |
4303 Attribute_Storage_Size |
4307 -- We do not do anything here with address clauses, they will be
4308 -- removed by Freeze later on, but for now, it works better to
4309 -- keep then in the tree.
4311 when Attribute_Address =>
4314 -- Other cases are errors ("attribute& cannot be set with
4315 -- definition clause"), which will be caught below.
4323 Ent := Entity (Nam);
4325 if Rep_Item_Too_Early (Ent, N) then
4329 -- Rep clause applies to full view of incomplete type or private type if
4330 -- we have one (if not, this is a premature use of the type). However,
4331 -- certain semantic checks need to be done on the specified entity (i.e.
4332 -- the private view), so we save it in Ent.
4334 if Is_Private_Type (Ent)
4335 and then Is_Derived_Type (Ent)
4336 and then not Is_Tagged_Type (Ent)
4337 and then No (Full_View (Ent))
4339 -- If this is a private type whose completion is a derivation from
4340 -- another private type, there is no full view, and the attribute
4341 -- belongs to the type itself, not its underlying parent.
4345 elsif Ekind (Ent) = E_Incomplete_Type then
4347 -- The attribute applies to the full view, set the entity of the
4348 -- attribute definition accordingly.
4350 Ent := Underlying_Type (Ent);
4352 Set_Entity (Nam, Ent);
4355 U_Ent := Underlying_Type (Ent);
4358 -- Avoid cascaded error
4360 if Etype (Nam) = Any_Type then
4363 -- Must be declared in current scope or in case of an aspect
4364 -- specification, must be visible in current scope.
4366 elsif Scope (Ent) /= Current_Scope
4368 not (From_Aspect_Specification (N)
4369 and then Scope_Within_Or_Same (Current_Scope, Scope (Ent)))
4371 Error_Msg_N ("entity must be declared in this scope", Nam);
4374 -- Must not be a source renaming (we do have some cases where the
4375 -- expander generates a renaming, and those cases are OK, in such
4376 -- cases any attribute applies to the renamed object as well).
4378 elsif Is_Object (Ent)
4379 and then Present (Renamed_Object (Ent))
4381 -- Case of renamed object from source, this is an error
4383 if Comes_From_Source (Renamed_Object (Ent)) then
4384 Get_Name_String (Chars (N));
4385 Error_Msg_Strlen := Name_Len;
4386 Error_Msg_String (1 .. Name_Len) := Name_Buffer (1 .. Name_Len);
4388 ("~ clause not allowed for a renaming declaration "
4389 & "(RM 13.1(6))", Nam);
4392 -- For the case of a compiler generated renaming, the attribute
4393 -- definition clause applies to the renamed object created by the
4394 -- expander. The easiest general way to handle this is to create a
4395 -- copy of the attribute definition clause for this object.
4397 elsif Is_Entity_Name (Renamed_Object (Ent)) then
4399 Make_Attribute_Definition_Clause (Loc,
4401 New_Occurrence_Of (Entity (Renamed_Object (Ent)), Loc),
4403 Expression => Duplicate_Subexpr (Expression (N))));
4405 -- If the renamed object is not an entity, it must be a dereference
4406 -- of an unconstrained function call, and we must introduce a new
4407 -- declaration to capture the expression. This is needed in the case
4408 -- of 'Alignment, where the original declaration must be rewritten.
4412 (Nkind (Renamed_Object (Ent)) = N_Explicit_Dereference);
4416 -- If no underlying entity, use entity itself, applies to some
4417 -- previously detected error cases ???
4419 elsif No (U_Ent) then
4422 -- Cannot specify for a subtype (exception Object/Value_Size)
4424 elsif Is_Type (U_Ent)
4425 and then not Is_First_Subtype (U_Ent)
4426 and then Id /= Attribute_Object_Size
4427 and then Id /= Attribute_Value_Size
4428 and then not From_At_Mod (N)
4430 Error_Msg_N ("cannot specify attribute for subtype", Nam);
4434 Set_Entity (N, U_Ent);
4435 Check_Restriction_No_Use_Of_Attribute (N);
4437 -- Switch on particular attribute
4445 -- Address attribute definition clause
4447 when Attribute_Address => Address : begin
4449 -- A little error check, catch for X'Address use X'Address;
4451 if Nkind (Nam) = N_Identifier
4452 and then Nkind (Expr) = N_Attribute_Reference
4453 and then Attribute_Name (Expr) = Name_Address
4454 and then Nkind (Prefix (Expr)) = N_Identifier
4455 and then Chars (Nam) = Chars (Prefix (Expr))
4458 ("address for & is self-referencing", Prefix (Expr), Ent);
4462 -- Not that special case, carry on with analysis of expression
4464 Analyze_And_Resolve (Expr, RTE (RE_Address));
4466 -- Even when ignoring rep clauses we need to indicate that the
4467 -- entity has an address clause and thus it is legal to declare
4468 -- it imported. Freeze will get rid of the address clause later.
4470 if Ignore_Rep_Clauses then
4471 if Ekind_In (U_Ent, E_Variable, E_Constant) then
4472 Record_Rep_Item (U_Ent, N);
4478 if Duplicate_Clause then
4481 -- Case of address clause for subprogram
4483 elsif Is_Subprogram (U_Ent) then
4484 if Has_Homonym (U_Ent) then
4486 ("address clause cannot be given " &
4487 "for overloaded subprogram",
4492 -- For subprograms, all address clauses are permitted, and we
4493 -- mark the subprogram as having a deferred freeze so that Gigi
4494 -- will not elaborate it too soon.
4496 -- Above needs more comments, what is too soon about???
4498 Set_Has_Delayed_Freeze (U_Ent);
4500 -- Case of address clause for entry
4502 elsif Ekind (U_Ent) = E_Entry then
4503 if Nkind (Parent (N)) = N_Task_Body then
4505 ("entry address must be specified in task spec", Nam);
4509 -- For entries, we require a constant address
4511 Check_Constant_Address_Clause (Expr, U_Ent);
4513 -- Special checks for task types
4515 if Is_Task_Type (Scope (U_Ent))
4516 and then Comes_From_Source (Scope (U_Ent))
4519 ("??entry address declared for entry in task type", N);
4521 ("\??only one task can be declared of this type", N);
4524 -- Entry address clauses are obsolescent
4526 Check_Restriction (No_Obsolescent_Features, N);
4528 if Warn_On_Obsolescent_Feature then
4530 ("?j?attaching interrupt to task entry is an " &
4531 "obsolescent feature (RM J.7.1)", N);
4533 ("\?j?use interrupt procedure instead", N);
4536 -- Case of an address clause for a controlled object which we
4537 -- consider to be erroneous.
4539 elsif Is_Controlled (Etype (U_Ent))
4540 or else Has_Controlled_Component (Etype (U_Ent))
4543 ("??controlled object& must not be overlaid", Nam, U_Ent);
4545 ("\??Program_Error will be raised at run time", Nam);
4546 Insert_Action (Declaration_Node (U_Ent),
4547 Make_Raise_Program_Error (Loc,
4548 Reason => PE_Overlaid_Controlled_Object));
4551 -- Case of address clause for a (non-controlled) object
4553 elsif Ekind_In (U_Ent, E_Variable, E_Constant) then
4555 Expr : constant Node_Id := Expression (N);
4560 -- Exported variables cannot have an address clause, because
4561 -- this cancels the effect of the pragma Export.
4563 if Is_Exported (U_Ent) then
4565 ("cannot export object with address clause", Nam);
4569 Find_Overlaid_Entity (N, O_Ent, Off);
4571 -- Overlaying controlled objects is erroneous
4574 and then (Has_Controlled_Component (Etype (O_Ent))
4575 or else Is_Controlled (Etype (O_Ent)))
4578 ("??cannot overlay with controlled object", Expr);
4580 ("\??Program_Error will be raised at run time", Expr);
4581 Insert_Action (Declaration_Node (U_Ent),
4582 Make_Raise_Program_Error (Loc,
4583 Reason => PE_Overlaid_Controlled_Object));
4586 elsif Present (O_Ent)
4587 and then Ekind (U_Ent) = E_Constant
4588 and then not Is_Constant_Object (O_Ent)
4590 Error_Msg_N ("??constant overlays a variable", Expr);
4592 -- Imported variables can have an address clause, but then
4593 -- the import is pretty meaningless except to suppress
4594 -- initializations, so we do not need such variables to
4595 -- be statically allocated (and in fact it causes trouble
4596 -- if the address clause is a local value).
4598 elsif Is_Imported (U_Ent) then
4599 Set_Is_Statically_Allocated (U_Ent, False);
4602 -- We mark a possible modification of a variable with an
4603 -- address clause, since it is likely aliasing is occurring.
4605 Note_Possible_Modification (Nam, Sure => False);
4607 -- Here we are checking for explicit overlap of one variable
4608 -- by another, and if we find this then mark the overlapped
4609 -- variable as also being volatile to prevent unwanted
4610 -- optimizations. This is a significant pessimization so
4611 -- avoid it when there is an offset, i.e. when the object
4612 -- is composite; they cannot be optimized easily anyway.
4615 and then Is_Object (O_Ent)
4618 -- The following test is an expedient solution to what
4619 -- is really a problem in CodePeer. Suppressing the
4620 -- Set_Treat_As_Volatile call here prevents later
4621 -- generation (in some cases) of trees that CodePeer
4622 -- should, but currently does not, handle correctly.
4623 -- This test should probably be removed when CodePeer
4624 -- is improved, just because we want the tree CodePeer
4625 -- analyzes to match the tree for which we generate code
4626 -- as closely as is practical. ???
4628 and then not CodePeer_Mode
4630 -- ??? O_Ent might not be in current unit
4632 Set_Treat_As_Volatile (O_Ent);
4635 -- Legality checks on the address clause for initialized
4636 -- objects is deferred until the freeze point, because
4637 -- a subsequent pragma might indicate that the object
4638 -- is imported and thus not initialized. Also, the address
4639 -- clause might involve entities that have yet to be
4642 Set_Has_Delayed_Freeze (U_Ent);
4644 -- If an initialization call has been generated for this
4645 -- object, it needs to be deferred to after the freeze node
4646 -- we have just now added, otherwise GIGI will see a
4647 -- reference to the variable (as actual to the IP call)
4648 -- before its definition.
4651 Init_Call : constant Node_Id :=
4652 Remove_Init_Call (U_Ent, N);
4655 if Present (Init_Call) then
4656 Append_Freeze_Action (U_Ent, Init_Call);
4658 -- Reset Initialization_Statements pointer so that
4659 -- if there is a pragma Import further down, it can
4660 -- clear any default initialization.
4662 Set_Initialization_Statements (U_Ent, Init_Call);
4666 if Is_Exported (U_Ent) then
4668 ("& cannot be exported if an address clause is given",
4671 ("\define and export a variable "
4672 & "that holds its address instead", Nam);
4675 -- Entity has delayed freeze, so we will generate an
4676 -- alignment check at the freeze point unless suppressed.
4678 if not Range_Checks_Suppressed (U_Ent)
4679 and then not Alignment_Checks_Suppressed (U_Ent)
4681 Set_Check_Address_Alignment (N);
4684 -- Kill the size check code, since we are not allocating
4685 -- the variable, it is somewhere else.
4687 Kill_Size_Check_Code (U_Ent);
4689 -- If the address clause is of the form:
4691 -- for Y'Address use X'Address
4695 -- Const : constant Address := X'Address;
4697 -- for Y'Address use Const;
4699 -- then we make an entry in the table for checking the size
4700 -- and alignment of the overlaying variable. We defer this
4701 -- check till after code generation to take full advantage
4702 -- of the annotation done by the back end.
4704 -- If the entity has a generic type, the check will be
4705 -- performed in the instance if the actual type justifies
4706 -- it, and we do not insert the clause in the table to
4707 -- prevent spurious warnings.
4709 -- Note: we used to test Comes_From_Source and only give
4710 -- this warning for source entities, but we have removed
4711 -- this test. It really seems bogus to generate overlays
4712 -- that would trigger this warning in generated code.
4713 -- Furthermore, by removing the test, we handle the
4714 -- aspect case properly.
4716 if Address_Clause_Overlay_Warnings
4717 and then Present (O_Ent)
4718 and then Is_Object (O_Ent)
4720 if not Is_Generic_Type (Etype (U_Ent)) then
4721 Address_Clause_Checks.Append ((N, U_Ent, O_Ent, Off));
4724 -- If variable overlays a constant view, and we are
4725 -- warning on overlays, then mark the variable as
4726 -- overlaying a constant (we will give warnings later
4727 -- if this variable is assigned).
4729 if Is_Constant_Object (O_Ent)
4730 and then Ekind (U_Ent) = E_Variable
4732 Set_Overlays_Constant (U_Ent);
4737 -- Not a valid entity for an address clause
4740 Error_Msg_N ("address cannot be given for &", Nam);
4748 -- Alignment attribute definition clause
4750 when Attribute_Alignment => Alignment : declare
4751 Align : constant Uint := Get_Alignment_Value (Expr);
4752 Max_Align : constant Uint := UI_From_Int (Maximum_Alignment);
4757 if not Is_Type (U_Ent)
4758 and then Ekind (U_Ent) /= E_Variable
4759 and then Ekind (U_Ent) /= E_Constant
4761 Error_Msg_N ("alignment cannot be given for &", Nam);
4763 elsif Duplicate_Clause then
4766 elsif Align /= No_Uint then
4767 Set_Has_Alignment_Clause (U_Ent);
4769 -- Tagged type case, check for attempt to set alignment to a
4770 -- value greater than Max_Align, and reset if so.
4772 if Is_Tagged_Type (U_Ent) and then Align > Max_Align then
4774 ("alignment for & set to Maximum_Aligment??", Nam);
4775 Set_Alignment (U_Ent, Max_Align);
4780 Set_Alignment (U_Ent, Align);
4783 -- For an array type, U_Ent is the first subtype. In that case,
4784 -- also set the alignment of the anonymous base type so that
4785 -- other subtypes (such as the itypes for aggregates of the
4786 -- type) also receive the expected alignment.
4788 if Is_Array_Type (U_Ent) then
4789 Set_Alignment (Base_Type (U_Ent), Align);
4798 -- Bit_Order attribute definition clause
4800 when Attribute_Bit_Order => Bit_Order : declare
4802 if not Is_Record_Type (U_Ent) then
4804 ("Bit_Order can only be defined for record type", Nam);
4806 elsif Duplicate_Clause then
4810 Analyze_And_Resolve (Expr, RTE (RE_Bit_Order));
4812 if Etype (Expr) = Any_Type then
4815 elsif not Is_OK_Static_Expression (Expr) then
4816 Flag_Non_Static_Expr
4817 ("Bit_Order requires static expression!", Expr);
4820 if (Expr_Value (Expr) = 0) /= Bytes_Big_Endian then
4821 Set_Reverse_Bit_Order (Base_Type (U_Ent), True);
4827 --------------------
4828 -- Component_Size --
4829 --------------------
4831 -- Component_Size attribute definition clause
4833 when Attribute_Component_Size => Component_Size_Case : declare
4834 Csize : constant Uint := Static_Integer (Expr);
4838 New_Ctyp : Entity_Id;
4842 if not Is_Array_Type (U_Ent) then
4843 Error_Msg_N ("component size requires array type", Nam);
4847 Btype := Base_Type (U_Ent);
4848 Ctyp := Component_Type (Btype);
4850 if Duplicate_Clause then
4853 elsif Rep_Item_Too_Early (Btype, N) then
4856 elsif Csize /= No_Uint then
4857 Check_Size (Expr, Ctyp, Csize, Biased);
4859 -- For the biased case, build a declaration for a subtype that
4860 -- will be used to represent the biased subtype that reflects
4861 -- the biased representation of components. We need the subtype
4862 -- to get proper conversions on referencing elements of the
4863 -- array. Note: component size clauses are ignored in VM mode.
4865 if VM_Target = No_VM then
4868 Make_Defining_Identifier (Loc,
4870 New_External_Name (Chars (U_Ent), 'C', 0, 'T'));
4873 Make_Subtype_Declaration (Loc,
4874 Defining_Identifier => New_Ctyp,
4875 Subtype_Indication =>
4876 New_Occurrence_Of (Component_Type (Btype), Loc));
4878 Set_Parent (Decl, N);
4879 Analyze (Decl, Suppress => All_Checks);
4881 Set_Has_Delayed_Freeze (New_Ctyp, False);
4882 Set_Esize (New_Ctyp, Csize);
4883 Set_RM_Size (New_Ctyp, Csize);
4884 Init_Alignment (New_Ctyp);
4885 Set_Is_Itype (New_Ctyp, True);
4886 Set_Associated_Node_For_Itype (New_Ctyp, U_Ent);
4888 Set_Component_Type (Btype, New_Ctyp);
4889 Set_Biased (New_Ctyp, N, "component size clause");
4892 Set_Component_Size (Btype, Csize);
4894 -- For VM case, we ignore component size clauses
4897 -- Give a warning unless we are in GNAT mode, in which case
4898 -- the warning is suppressed since it is not useful.
4900 if not GNAT_Mode then
4902 ("component size ignored in this configuration??", N);
4906 -- Deal with warning on overridden size
4908 if Warn_On_Overridden_Size
4909 and then Has_Size_Clause (Ctyp)
4910 and then RM_Size (Ctyp) /= Csize
4913 ("component size overrides size clause for&?S?", N, Ctyp);
4916 Set_Has_Component_Size_Clause (Btype, True);
4917 Set_Has_Non_Standard_Rep (Btype, True);
4919 end Component_Size_Case;
4921 -----------------------
4922 -- Constant_Indexing --
4923 -----------------------
4925 when Attribute_Constant_Indexing =>
4926 Check_Indexing_Functions;
4932 when Attribute_CPU => CPU :
4934 -- CPU attribute definition clause not allowed except from aspect
4937 if From_Aspect_Specification (N) then
4938 if not Is_Task_Type (U_Ent) then
4939 Error_Msg_N ("CPU can only be defined for task", Nam);
4941 elsif Duplicate_Clause then
4945 -- The expression must be analyzed in the special manner
4946 -- described in "Handling of Default and Per-Object
4947 -- Expressions" in sem.ads.
4949 -- The visibility to the discriminants must be restored
4951 Push_Scope_And_Install_Discriminants (U_Ent);
4952 Preanalyze_Spec_Expression (Expr, RTE (RE_CPU_Range));
4953 Uninstall_Discriminants_And_Pop_Scope (U_Ent);
4955 if not Is_OK_Static_Expression (Expr) then
4956 Check_Restriction (Static_Priorities, Expr);
4962 ("attribute& cannot be set with definition clause", N);
4966 ----------------------
4967 -- Default_Iterator --
4968 ----------------------
4970 when Attribute_Default_Iterator => Default_Iterator : declare
4975 if not Is_Tagged_Type (U_Ent) then
4977 ("aspect Default_Iterator applies to tagged type", Nam);
4980 Check_Iterator_Functions;
4984 if not Is_Entity_Name (Expr)
4985 or else Ekind (Entity (Expr)) /= E_Function
4987 Error_Msg_N ("aspect Iterator must be a function", Expr);
4989 Func := Entity (Expr);
4992 -- The type of the first parameter must be T, T'class, or a
4993 -- corresponding access type (5.5.1 (8/3)
4995 if No (First_Formal (Func)) then
4998 Typ := Etype (First_Formal (Func));
5002 or else Typ = Class_Wide_Type (U_Ent)
5003 or else (Is_Access_Type (Typ)
5004 and then Designated_Type (Typ) = U_Ent)
5005 or else (Is_Access_Type (Typ)
5006 and then Designated_Type (Typ) =
5007 Class_Wide_Type (U_Ent))
5013 ("Default Iterator must be a primitive of&", Func, U_Ent);
5015 end Default_Iterator;
5017 ------------------------
5018 -- Dispatching_Domain --
5019 ------------------------
5021 when Attribute_Dispatching_Domain => Dispatching_Domain :
5023 -- Dispatching_Domain attribute definition clause not allowed
5024 -- except from aspect specification.
5026 if From_Aspect_Specification (N) then
5027 if not Is_Task_Type (U_Ent) then
5029 ("Dispatching_Domain can only be defined for task", Nam);
5031 elsif Duplicate_Clause then
5035 -- The expression must be analyzed in the special manner
5036 -- described in "Handling of Default and Per-Object
5037 -- Expressions" in sem.ads.
5039 -- The visibility to the discriminants must be restored
5041 Push_Scope_And_Install_Discriminants (U_Ent);
5043 Preanalyze_Spec_Expression
5044 (Expr, RTE (RE_Dispatching_Domain));
5046 Uninstall_Discriminants_And_Pop_Scope (U_Ent);
5051 ("attribute& cannot be set with definition clause", N);
5053 end Dispatching_Domain;
5059 when Attribute_External_Tag => External_Tag :
5061 if not Is_Tagged_Type (U_Ent) then
5062 Error_Msg_N ("should be a tagged type", Nam);
5065 if Duplicate_Clause then
5069 Analyze_And_Resolve (Expr, Standard_String);
5071 if not Is_OK_Static_Expression (Expr) then
5072 Flag_Non_Static_Expr
5073 ("static string required for tag name!", Nam);
5076 if VM_Target /= No_VM then
5077 Error_Msg_Name_1 := Attr;
5079 ("% attribute unsupported in this configuration", Nam);
5082 if not Is_Library_Level_Entity (U_Ent) then
5084 ("??non-unique external tag supplied for &", N, U_Ent);
5086 ("\??same external tag applies to all "
5087 & "subprogram calls", N);
5089 ("\??corresponding internal tag cannot be obtained", N);
5094 --------------------------
5095 -- Implicit_Dereference --
5096 --------------------------
5098 when Attribute_Implicit_Dereference =>
5100 -- Legality checks already performed at the point of the type
5101 -- declaration, aspect is not delayed.
5109 when Attribute_Input =>
5110 Analyze_Stream_TSS_Definition (TSS_Stream_Input);
5111 Set_Has_Specified_Stream_Input (Ent);
5113 ------------------------
5114 -- Interrupt_Priority --
5115 ------------------------
5117 when Attribute_Interrupt_Priority => Interrupt_Priority :
5119 -- Interrupt_Priority attribute definition clause not allowed
5120 -- except from aspect specification.
5122 if From_Aspect_Specification (N) then
5123 if not Is_Concurrent_Type (U_Ent) then
5125 ("Interrupt_Priority can only be defined for task "
5126 & "and protected object", Nam);
5128 elsif Duplicate_Clause then
5132 -- The expression must be analyzed in the special manner
5133 -- described in "Handling of Default and Per-Object
5134 -- Expressions" in sem.ads.
5136 -- The visibility to the discriminants must be restored
5138 Push_Scope_And_Install_Discriminants (U_Ent);
5140 Preanalyze_Spec_Expression
5141 (Expr, RTE (RE_Interrupt_Priority));
5143 Uninstall_Discriminants_And_Pop_Scope (U_Ent);
5148 ("attribute& cannot be set with definition clause", N);
5150 end Interrupt_Priority;
5156 when Attribute_Iterable =>
5159 if Nkind (Expr) /= N_Aggregate then
5160 Error_Msg_N ("aspect Iterable must be an aggregate", Expr);
5167 Assoc := First (Component_Associations (Expr));
5168 while Present (Assoc) loop
5169 if not Is_Entity_Name (Expression (Assoc)) then
5170 Error_Msg_N ("value must be a function", Assoc);
5177 ----------------------
5178 -- Iterator_Element --
5179 ----------------------
5181 when Attribute_Iterator_Element =>
5184 if not Is_Entity_Name (Expr)
5185 or else not Is_Type (Entity (Expr))
5187 Error_Msg_N ("aspect Iterator_Element must be a type", Expr);
5194 -- Machine radix attribute definition clause
5196 when Attribute_Machine_Radix => Machine_Radix : declare
5197 Radix : constant Uint := Static_Integer (Expr);
5200 if not Is_Decimal_Fixed_Point_Type (U_Ent) then
5201 Error_Msg_N ("decimal fixed-point type expected for &", Nam);
5203 elsif Duplicate_Clause then
5206 elsif Radix /= No_Uint then
5207 Set_Has_Machine_Radix_Clause (U_Ent);
5208 Set_Has_Non_Standard_Rep (Base_Type (U_Ent));
5212 elsif Radix = 10 then
5213 Set_Machine_Radix_10 (U_Ent);
5215 Error_Msg_N ("machine radix value must be 2 or 10", Expr);
5224 -- Object_Size attribute definition clause
5226 when Attribute_Object_Size => Object_Size : declare
5227 Size : constant Uint := Static_Integer (Expr);
5230 pragma Warnings (Off, Biased);
5233 if not Is_Type (U_Ent) then
5234 Error_Msg_N ("Object_Size cannot be given for &", Nam);
5236 elsif Duplicate_Clause then
5240 Check_Size (Expr, U_Ent, Size, Biased);
5242 if Is_Scalar_Type (U_Ent) then
5243 if Size /= 8 and then Size /= 16 and then Size /= 32
5244 and then UI_Mod (Size, 64) /= 0
5247 ("Object_Size must be 8, 16, 32, or multiple of 64",
5251 elsif Size mod 8 /= 0 then
5252 Error_Msg_N ("Object_Size must be a multiple of 8", Expr);
5255 Set_Esize (U_Ent, Size);
5256 Set_Has_Object_Size_Clause (U_Ent);
5257 Alignment_Check_For_Size_Change (U_Ent, Size);
5265 when Attribute_Output =>
5266 Analyze_Stream_TSS_Definition (TSS_Stream_Output);
5267 Set_Has_Specified_Stream_Output (Ent);
5273 when Attribute_Priority => Priority :
5275 -- Priority attribute definition clause not allowed except from
5276 -- aspect specification.
5278 if From_Aspect_Specification (N) then
5279 if not (Is_Concurrent_Type (U_Ent)
5280 or else Ekind (U_Ent) = E_Procedure)
5283 ("Priority can only be defined for task and protected "
5286 elsif Duplicate_Clause then
5290 -- The expression must be analyzed in the special manner
5291 -- described in "Handling of Default and Per-Object
5292 -- Expressions" in sem.ads.
5294 -- The visibility to the discriminants must be restored
5296 Push_Scope_And_Install_Discriminants (U_Ent);
5297 Preanalyze_Spec_Expression (Expr, Standard_Integer);
5298 Uninstall_Discriminants_And_Pop_Scope (U_Ent);
5300 if not Is_OK_Static_Expression (Expr) then
5301 Check_Restriction (Static_Priorities, Expr);
5307 ("attribute& cannot be set with definition clause", N);
5315 when Attribute_Read =>
5316 Analyze_Stream_TSS_Definition (TSS_Stream_Read);
5317 Set_Has_Specified_Stream_Read (Ent);
5319 --------------------------
5320 -- Scalar_Storage_Order --
5321 --------------------------
5323 -- Scalar_Storage_Order attribute definition clause
5325 when Attribute_Scalar_Storage_Order => Scalar_Storage_Order : declare
5327 if not (Is_Record_Type (U_Ent) or else Is_Array_Type (U_Ent)) then
5329 ("Scalar_Storage_Order can only be defined for "
5330 & "record or array type", Nam);
5332 elsif Duplicate_Clause then
5336 Analyze_And_Resolve (Expr, RTE (RE_Bit_Order));
5338 if Etype (Expr) = Any_Type then
5341 elsif not Is_OK_Static_Expression (Expr) then
5342 Flag_Non_Static_Expr
5343 ("Scalar_Storage_Order requires static expression!", Expr);
5345 elsif (Expr_Value (Expr) = 0) /= Bytes_Big_Endian then
5347 -- Here for the case of a non-default (i.e. non-confirming)
5348 -- Scalar_Storage_Order attribute definition.
5350 if Support_Nondefault_SSO_On_Target then
5351 Set_Reverse_Storage_Order (Base_Type (U_Ent), True);
5354 ("non-default Scalar_Storage_Order "
5355 & "not supported on target", Expr);
5359 -- Clear SSO default indications since explicit setting of the
5360 -- order overrides the defaults.
5362 Set_SSO_Set_Low_By_Default (Base_Type (U_Ent), False);
5363 Set_SSO_Set_High_By_Default (Base_Type (U_Ent), False);
5365 end Scalar_Storage_Order;
5371 -- Size attribute definition clause
5373 when Attribute_Size => Size : declare
5374 Size : constant Uint := Static_Integer (Expr);
5381 if Duplicate_Clause then
5384 elsif not Is_Type (U_Ent)
5385 and then Ekind (U_Ent) /= E_Variable
5386 and then Ekind (U_Ent) /= E_Constant
5388 Error_Msg_N ("size cannot be given for &", Nam);
5390 elsif Is_Array_Type (U_Ent)
5391 and then not Is_Constrained (U_Ent)
5394 ("size cannot be given for unconstrained array", Nam);
5396 elsif Size /= No_Uint then
5397 if VM_Target /= No_VM and then not GNAT_Mode then
5399 -- Size clause is not handled properly on VM targets.
5400 -- Display a warning unless we are in GNAT mode, in which
5401 -- case this is useless.
5404 ("size clauses are ignored in this configuration??", N);
5407 if Is_Type (U_Ent) then
5410 Etyp := Etype (U_Ent);
5413 -- Check size, note that Gigi is in charge of checking that the
5414 -- size of an array or record type is OK. Also we do not check
5415 -- the size in the ordinary fixed-point case, since it is too
5416 -- early to do so (there may be subsequent small clause that
5417 -- affects the size). We can check the size if a small clause
5418 -- has already been given.
5420 if not Is_Ordinary_Fixed_Point_Type (U_Ent)
5421 or else Has_Small_Clause (U_Ent)
5423 Check_Size (Expr, Etyp, Size, Biased);
5424 Set_Biased (U_Ent, N, "size clause", Biased);
5427 -- For types set RM_Size and Esize if possible
5429 if Is_Type (U_Ent) then
5430 Set_RM_Size (U_Ent, Size);
5432 -- For elementary types, increase Object_Size to power of 2,
5433 -- but not less than a storage unit in any case (normally
5434 -- this means it will be byte addressable).
5436 -- For all other types, nothing else to do, we leave Esize
5437 -- (object size) unset, the back end will set it from the
5438 -- size and alignment in an appropriate manner.
5440 -- In both cases, we check whether the alignment must be
5441 -- reset in the wake of the size change.
5443 if Is_Elementary_Type (U_Ent) then
5444 if Size <= System_Storage_Unit then
5445 Init_Esize (U_Ent, System_Storage_Unit);
5446 elsif Size <= 16 then
5447 Init_Esize (U_Ent, 16);
5448 elsif Size <= 32 then
5449 Init_Esize (U_Ent, 32);
5451 Set_Esize (U_Ent, (Size + 63) / 64 * 64);
5454 Alignment_Check_For_Size_Change (U_Ent, Esize (U_Ent));
5456 Alignment_Check_For_Size_Change (U_Ent, Size);
5459 -- For objects, set Esize only
5462 if Is_Elementary_Type (Etyp) then
5463 if Size /= System_Storage_Unit
5465 Size /= System_Storage_Unit * 2
5467 Size /= System_Storage_Unit * 4
5469 Size /= System_Storage_Unit * 8
5471 Error_Msg_Uint_1 := UI_From_Int (System_Storage_Unit);
5472 Error_Msg_Uint_2 := Error_Msg_Uint_1 * 8;
5474 ("size for primitive object must be a power of 2"
5475 & " in the range ^-^", N);
5479 Set_Esize (U_Ent, Size);
5482 Set_Has_Size_Clause (U_Ent);
5490 -- Small attribute definition clause
5492 when Attribute_Small => Small : declare
5493 Implicit_Base : constant Entity_Id := Base_Type (U_Ent);
5497 Analyze_And_Resolve (Expr, Any_Real);
5499 if Etype (Expr) = Any_Type then
5502 elsif not Is_OK_Static_Expression (Expr) then
5503 Flag_Non_Static_Expr
5504 ("small requires static expression!", Expr);
5508 Small := Expr_Value_R (Expr);
5510 if Small <= Ureal_0 then
5511 Error_Msg_N ("small value must be greater than zero", Expr);
5517 if not Is_Ordinary_Fixed_Point_Type (U_Ent) then
5519 ("small requires an ordinary fixed point type", Nam);
5521 elsif Has_Small_Clause (U_Ent) then
5522 Error_Msg_N ("small already given for &", Nam);
5524 elsif Small > Delta_Value (U_Ent) then
5526 ("small value must not be greater than delta value", Nam);
5529 Set_Small_Value (U_Ent, Small);
5530 Set_Small_Value (Implicit_Base, Small);
5531 Set_Has_Small_Clause (U_Ent);
5532 Set_Has_Small_Clause (Implicit_Base);
5533 Set_Has_Non_Standard_Rep (Implicit_Base);
5541 -- Storage_Pool attribute definition clause
5543 when Attribute_Storage_Pool | Attribute_Simple_Storage_Pool => declare
5548 if Ekind (U_Ent) = E_Access_Subprogram_Type then
5550 ("storage pool cannot be given for access-to-subprogram type",
5555 Ekind_In (U_Ent, E_Access_Type, E_General_Access_Type)
5558 ("storage pool can only be given for access types", Nam);
5561 elsif Is_Derived_Type (U_Ent) then
5563 ("storage pool cannot be given for a derived access type",
5566 elsif Duplicate_Clause then
5569 elsif Present (Associated_Storage_Pool (U_Ent)) then
5570 Error_Msg_N ("storage pool already given for &", Nam);
5574 -- Check for Storage_Size previously given
5577 SS : constant Node_Id :=
5578 Get_Attribute_Definition_Clause
5579 (U_Ent, Attribute_Storage_Size);
5581 if Present (SS) then
5582 Check_Pool_Size_Clash (U_Ent, N, SS);
5586 -- Storage_Pool case
5588 if Id = Attribute_Storage_Pool then
5590 (Expr, Class_Wide_Type (RTE (RE_Root_Storage_Pool)));
5592 -- In the Simple_Storage_Pool case, we allow a variable of any
5593 -- simple storage pool type, so we Resolve without imposing an
5597 Analyze_And_Resolve (Expr);
5599 if not Present (Get_Rep_Pragma
5600 (Etype (Expr), Name_Simple_Storage_Pool_Type))
5603 ("expression must be of a simple storage pool type", Expr);
5607 if not Denotes_Variable (Expr) then
5608 Error_Msg_N ("storage pool must be a variable", Expr);
5612 if Nkind (Expr) = N_Type_Conversion then
5613 T := Etype (Expression (Expr));
5618 -- The Stack_Bounded_Pool is used internally for implementing
5619 -- access types with a Storage_Size. Since it only work properly
5620 -- when used on one specific type, we need to check that it is not
5621 -- hijacked improperly:
5623 -- type T is access Integer;
5624 -- for T'Storage_Size use n;
5625 -- type Q is access Float;
5626 -- for Q'Storage_Size use T'Storage_Size; -- incorrect
5628 if RTE_Available (RE_Stack_Bounded_Pool)
5629 and then Base_Type (T) = RTE (RE_Stack_Bounded_Pool)
5631 Error_Msg_N ("non-shareable internal Pool", Expr);
5635 -- If the argument is a name that is not an entity name, then
5636 -- we construct a renaming operation to define an entity of
5637 -- type storage pool.
5639 if not Is_Entity_Name (Expr)
5640 and then Is_Object_Reference (Expr)
5642 Pool := Make_Temporary (Loc, 'P', Expr);
5645 Rnode : constant Node_Id :=
5646 Make_Object_Renaming_Declaration (Loc,
5647 Defining_Identifier => Pool,
5649 New_Occurrence_Of (Etype (Expr), Loc),
5653 -- If the attribute definition clause comes from an aspect
5654 -- clause, then insert the renaming before the associated
5655 -- entity's declaration, since the attribute clause has
5656 -- not yet been appended to the declaration list.
5658 if From_Aspect_Specification (N) then
5659 Insert_Before (Parent (Entity (N)), Rnode);
5661 Insert_Before (N, Rnode);
5665 Set_Associated_Storage_Pool (U_Ent, Pool);
5668 elsif Is_Entity_Name (Expr) then
5669 Pool := Entity (Expr);
5671 -- If pool is a renamed object, get original one. This can
5672 -- happen with an explicit renaming, and within instances.
5674 while Present (Renamed_Object (Pool))
5675 and then Is_Entity_Name (Renamed_Object (Pool))
5677 Pool := Entity (Renamed_Object (Pool));
5680 if Present (Renamed_Object (Pool))
5681 and then Nkind (Renamed_Object (Pool)) = N_Type_Conversion
5682 and then Is_Entity_Name (Expression (Renamed_Object (Pool)))
5684 Pool := Entity (Expression (Renamed_Object (Pool)));
5687 Set_Associated_Storage_Pool (U_Ent, Pool);
5689 elsif Nkind (Expr) = N_Type_Conversion
5690 and then Is_Entity_Name (Expression (Expr))
5691 and then Nkind (Original_Node (Expr)) = N_Attribute_Reference
5693 Pool := Entity (Expression (Expr));
5694 Set_Associated_Storage_Pool (U_Ent, Pool);
5697 Error_Msg_N ("incorrect reference to a Storage Pool", Expr);
5706 -- Storage_Size attribute definition clause
5708 when Attribute_Storage_Size => Storage_Size : declare
5709 Btype : constant Entity_Id := Base_Type (U_Ent);
5712 if Is_Task_Type (U_Ent) then
5714 -- Check obsolescent (but never obsolescent if from aspect)
5716 if not From_Aspect_Specification (N) then
5717 Check_Restriction (No_Obsolescent_Features, N);
5719 if Warn_On_Obsolescent_Feature then
5721 ("?j?storage size clause for task is an " &
5722 "obsolescent feature (RM J.9)", N);
5723 Error_Msg_N ("\?j?use Storage_Size pragma instead", N);
5730 if not Is_Access_Type (U_Ent)
5731 and then Ekind (U_Ent) /= E_Task_Type
5733 Error_Msg_N ("storage size cannot be given for &", Nam);
5735 elsif Is_Access_Type (U_Ent) and Is_Derived_Type (U_Ent) then
5737 ("storage size cannot be given for a derived access type",
5740 elsif Duplicate_Clause then
5744 Analyze_And_Resolve (Expr, Any_Integer);
5746 if Is_Access_Type (U_Ent) then
5748 -- Check for Storage_Pool previously given
5751 SP : constant Node_Id :=
5752 Get_Attribute_Definition_Clause
5753 (U_Ent, Attribute_Storage_Pool);
5756 if Present (SP) then
5757 Check_Pool_Size_Clash (U_Ent, SP, N);
5761 -- Special case of for x'Storage_Size use 0
5763 if Is_OK_Static_Expression (Expr)
5764 and then Expr_Value (Expr) = 0
5766 Set_No_Pool_Assigned (Btype);
5770 Set_Has_Storage_Size_Clause (Btype);
5778 when Attribute_Stream_Size => Stream_Size : declare
5779 Size : constant Uint := Static_Integer (Expr);
5782 if Ada_Version <= Ada_95 then
5783 Check_Restriction (No_Implementation_Attributes, N);
5786 if Duplicate_Clause then
5789 elsif Is_Elementary_Type (U_Ent) then
5790 if Size /= System_Storage_Unit
5792 Size /= System_Storage_Unit * 2
5794 Size /= System_Storage_Unit * 4
5796 Size /= System_Storage_Unit * 8
5798 Error_Msg_Uint_1 := UI_From_Int (System_Storage_Unit);
5800 ("stream size for elementary type must be a"
5801 & " power of 2 and at least ^", N);
5803 elsif RM_Size (U_Ent) > Size then
5804 Error_Msg_Uint_1 := RM_Size (U_Ent);
5806 ("stream size for elementary type must be a"
5807 & " power of 2 and at least ^", N);
5810 Set_Has_Stream_Size_Clause (U_Ent);
5813 Error_Msg_N ("Stream_Size cannot be given for &", Nam);
5821 -- Value_Size attribute definition clause
5823 when Attribute_Value_Size => Value_Size : declare
5824 Size : constant Uint := Static_Integer (Expr);
5828 if not Is_Type (U_Ent) then
5829 Error_Msg_N ("Value_Size cannot be given for &", Nam);
5831 elsif Duplicate_Clause then
5834 elsif Is_Array_Type (U_Ent)
5835 and then not Is_Constrained (U_Ent)
5838 ("Value_Size cannot be given for unconstrained array", Nam);
5841 if Is_Elementary_Type (U_Ent) then
5842 Check_Size (Expr, U_Ent, Size, Biased);
5843 Set_Biased (U_Ent, N, "value size clause", Biased);
5846 Set_RM_Size (U_Ent, Size);
5850 -----------------------
5851 -- Variable_Indexing --
5852 -----------------------
5854 when Attribute_Variable_Indexing =>
5855 Check_Indexing_Functions;
5861 when Attribute_Write =>
5862 Analyze_Stream_TSS_Definition (TSS_Stream_Write);
5863 Set_Has_Specified_Stream_Write (Ent);
5865 -- All other attributes cannot be set
5869 ("attribute& cannot be set with definition clause", N);
5872 -- The test for the type being frozen must be performed after any
5873 -- expression the clause has been analyzed since the expression itself
5874 -- might cause freezing that makes the clause illegal.
5876 if Rep_Item_Too_Late (U_Ent, N, FOnly) then
5879 end Analyze_Attribute_Definition_Clause;
5881 ----------------------------
5882 -- Analyze_Code_Statement --
5883 ----------------------------
5885 procedure Analyze_Code_Statement (N : Node_Id) is
5886 HSS : constant Node_Id := Parent (N);
5887 SBody : constant Node_Id := Parent (HSS);
5888 Subp : constant Entity_Id := Current_Scope;
5895 -- Analyze and check we get right type, note that this implements the
5896 -- requirement (RM 13.8(1)) that Machine_Code be with'ed, since that
5897 -- is the only way that Asm_Insn could possibly be visible.
5899 Analyze_And_Resolve (Expression (N));
5901 if Etype (Expression (N)) = Any_Type then
5903 elsif Etype (Expression (N)) /= RTE (RE_Asm_Insn) then
5904 Error_Msg_N ("incorrect type for code statement", N);
5908 Check_Code_Statement (N);
5910 -- Make sure we appear in the handled statement sequence of a
5911 -- subprogram (RM 13.8(3)).
5913 if Nkind (HSS) /= N_Handled_Sequence_Of_Statements
5914 or else Nkind (SBody) /= N_Subprogram_Body
5917 ("code statement can only appear in body of subprogram", N);
5921 -- Do remaining checks (RM 13.8(3)) if not already done
5923 if not Is_Machine_Code_Subprogram (Subp) then
5924 Set_Is_Machine_Code_Subprogram (Subp);
5926 -- No exception handlers allowed
5928 if Present (Exception_Handlers (HSS)) then
5930 ("exception handlers not permitted in machine code subprogram",
5931 First (Exception_Handlers (HSS)));
5934 -- No declarations other than use clauses and pragmas (we allow
5935 -- certain internally generated declarations as well).
5937 Decl := First (Declarations (SBody));
5938 while Present (Decl) loop
5939 DeclO := Original_Node (Decl);
5940 if Comes_From_Source (DeclO)
5941 and not Nkind_In (DeclO, N_Pragma,
5942 N_Use_Package_Clause,
5944 N_Implicit_Label_Declaration)
5947 ("this declaration not allowed in machine code subprogram",
5954 -- No statements other than code statements, pragmas, and labels.
5955 -- Again we allow certain internally generated statements.
5957 -- In Ada 2012, qualified expressions are names, and the code
5958 -- statement is initially parsed as a procedure call.
5960 Stmt := First (Statements (HSS));
5961 while Present (Stmt) loop
5962 StmtO := Original_Node (Stmt);
5964 -- A procedure call transformed into a code statement is OK.
5966 if Ada_Version >= Ada_2012
5967 and then Nkind (StmtO) = N_Procedure_Call_Statement
5968 and then Nkind (Name (StmtO)) = N_Qualified_Expression
5972 elsif Comes_From_Source (StmtO)
5973 and then not Nkind_In (StmtO, N_Pragma,
5978 ("this statement is not allowed in machine code subprogram",
5985 end Analyze_Code_Statement;
5987 -----------------------------------------------
5988 -- Analyze_Enumeration_Representation_Clause --
5989 -----------------------------------------------
5991 procedure Analyze_Enumeration_Representation_Clause (N : Node_Id) is
5992 Ident : constant Node_Id := Identifier (N);
5993 Aggr : constant Node_Id := Array_Aggregate (N);
5994 Enumtype : Entity_Id;
6001 Err : Boolean := False;
6002 -- Set True to avoid cascade errors and crashes on incorrect source code
6004 Lo : constant Uint := Expr_Value (Type_Low_Bound (Universal_Integer));
6005 Hi : constant Uint := Expr_Value (Type_High_Bound (Universal_Integer));
6006 -- Allowed range of universal integer (= allowed range of enum lit vals)
6010 -- Minimum and maximum values of entries
6013 -- Pointer to node for literal providing max value
6016 if Ignore_Rep_Clauses then
6017 Kill_Rep_Clause (N);
6021 -- Ignore enumeration rep clauses by default in CodePeer mode,
6022 -- unless -gnatd.I is specified, as a work around for potential false
6023 -- positive messages.
6025 if CodePeer_Mode and not Debug_Flag_Dot_II then
6029 -- First some basic error checks
6032 Enumtype := Entity (Ident);
6034 if Enumtype = Any_Type
6035 or else Rep_Item_Too_Early (Enumtype, N)
6039 Enumtype := Underlying_Type (Enumtype);
6042 if not Is_Enumeration_Type (Enumtype) then
6044 ("enumeration type required, found}",
6045 Ident, First_Subtype (Enumtype));
6049 -- Ignore rep clause on generic actual type. This will already have
6050 -- been flagged on the template as an error, and this is the safest
6051 -- way to ensure we don't get a junk cascaded message in the instance.
6053 if Is_Generic_Actual_Type (Enumtype) then
6056 -- Type must be in current scope
6058 elsif Scope (Enumtype) /= Current_Scope then
6059 Error_Msg_N ("type must be declared in this scope", Ident);
6062 -- Type must be a first subtype
6064 elsif not Is_First_Subtype (Enumtype) then
6065 Error_Msg_N ("cannot give enumeration rep clause for subtype", N);
6068 -- Ignore duplicate rep clause
6070 elsif Has_Enumeration_Rep_Clause (Enumtype) then
6071 Error_Msg_N ("duplicate enumeration rep clause ignored", N);
6074 -- Don't allow rep clause for standard [wide_[wide_]]character
6076 elsif Is_Standard_Character_Type (Enumtype) then
6077 Error_Msg_N ("enumeration rep clause not allowed for this type", N);
6080 -- Check that the expression is a proper aggregate (no parentheses)
6082 elsif Paren_Count (Aggr) /= 0 then
6084 ("extra parentheses surrounding aggregate not allowed",
6088 -- All tests passed, so set rep clause in place
6091 Set_Has_Enumeration_Rep_Clause (Enumtype);
6092 Set_Has_Enumeration_Rep_Clause (Base_Type (Enumtype));
6095 -- Now we process the aggregate. Note that we don't use the normal
6096 -- aggregate code for this purpose, because we don't want any of the
6097 -- normal expansion activities, and a number of special semantic
6098 -- rules apply (including the component type being any integer type)
6100 Elit := First_Literal (Enumtype);
6102 -- First the positional entries if any
6104 if Present (Expressions (Aggr)) then
6105 Expr := First (Expressions (Aggr));
6106 while Present (Expr) loop
6108 Error_Msg_N ("too many entries in aggregate", Expr);
6112 Val := Static_Integer (Expr);
6114 -- Err signals that we found some incorrect entries processing
6115 -- the list. The final checks for completeness and ordering are
6116 -- skipped in this case.
6118 if Val = No_Uint then
6121 elsif Val < Lo or else Hi < Val then
6122 Error_Msg_N ("value outside permitted range", Expr);
6126 Set_Enumeration_Rep (Elit, Val);
6127 Set_Enumeration_Rep_Expr (Elit, Expr);
6133 -- Now process the named entries if present
6135 if Present (Component_Associations (Aggr)) then
6136 Assoc := First (Component_Associations (Aggr));
6137 while Present (Assoc) loop
6138 Choice := First (Choices (Assoc));
6140 if Present (Next (Choice)) then
6142 ("multiple choice not allowed here", Next (Choice));
6146 if Nkind (Choice) = N_Others_Choice then
6147 Error_Msg_N ("others choice not allowed here", Choice);
6150 elsif Nkind (Choice) = N_Range then
6152 -- ??? should allow zero/one element range here
6154 Error_Msg_N ("range not allowed here", Choice);
6158 Analyze_And_Resolve (Choice, Enumtype);
6160 if Error_Posted (Choice) then
6165 if Is_Entity_Name (Choice)
6166 and then Is_Type (Entity (Choice))
6168 Error_Msg_N ("subtype name not allowed here", Choice);
6171 -- ??? should allow static subtype with zero/one entry
6173 elsif Etype (Choice) = Base_Type (Enumtype) then
6174 if not Is_OK_Static_Expression (Choice) then
6175 Flag_Non_Static_Expr
6176 ("non-static expression used for choice!", Choice);
6180 Elit := Expr_Value_E (Choice);
6182 if Present (Enumeration_Rep_Expr (Elit)) then
6184 Sloc (Enumeration_Rep_Expr (Elit));
6186 ("representation for& previously given#",
6191 Set_Enumeration_Rep_Expr (Elit, Expression (Assoc));
6193 Expr := Expression (Assoc);
6194 Val := Static_Integer (Expr);
6196 if Val = No_Uint then
6199 elsif Val < Lo or else Hi < Val then
6200 Error_Msg_N ("value outside permitted range", Expr);
6204 Set_Enumeration_Rep (Elit, Val);
6214 -- Aggregate is fully processed. Now we check that a full set of
6215 -- representations was given, and that they are in range and in order.
6216 -- These checks are only done if no other errors occurred.
6222 Elit := First_Literal (Enumtype);
6223 while Present (Elit) loop
6224 if No (Enumeration_Rep_Expr (Elit)) then
6225 Error_Msg_NE ("missing representation for&!", N, Elit);
6228 Val := Enumeration_Rep (Elit);
6230 if Min = No_Uint then
6234 if Val /= No_Uint then
6235 if Max /= No_Uint and then Val <= Max then
6237 ("enumeration value for& not ordered!",
6238 Enumeration_Rep_Expr (Elit), Elit);
6241 Max_Node := Enumeration_Rep_Expr (Elit);
6245 -- If there is at least one literal whose representation is not
6246 -- equal to the Pos value, then note that this enumeration type
6247 -- has a non-standard representation.
6249 if Val /= Enumeration_Pos (Elit) then
6250 Set_Has_Non_Standard_Rep (Base_Type (Enumtype));
6257 -- Now set proper size information
6260 Minsize : Uint := UI_From_Int (Minimum_Size (Enumtype));
6263 if Has_Size_Clause (Enumtype) then
6265 -- All OK, if size is OK now
6267 if RM_Size (Enumtype) >= Minsize then
6271 -- Try if we can get by with biasing
6274 UI_From_Int (Minimum_Size (Enumtype, Biased => True));
6276 -- Error message if even biasing does not work
6278 if RM_Size (Enumtype) < Minsize then
6279 Error_Msg_Uint_1 := RM_Size (Enumtype);
6280 Error_Msg_Uint_2 := Max;
6282 ("previously given size (^) is too small "
6283 & "for this value (^)", Max_Node);
6285 -- If biasing worked, indicate that we now have biased rep
6289 (Enumtype, Size_Clause (Enumtype), "size clause");
6294 Set_RM_Size (Enumtype, Minsize);
6295 Set_Enum_Esize (Enumtype);
6298 Set_RM_Size (Base_Type (Enumtype), RM_Size (Enumtype));
6299 Set_Esize (Base_Type (Enumtype), Esize (Enumtype));
6300 Set_Alignment (Base_Type (Enumtype), Alignment (Enumtype));
6304 -- We repeat the too late test in case it froze itself
6306 if Rep_Item_Too_Late (Enumtype, N) then
6309 end Analyze_Enumeration_Representation_Clause;
6311 ----------------------------
6312 -- Analyze_Free_Statement --
6313 ----------------------------
6315 procedure Analyze_Free_Statement (N : Node_Id) is
6317 Analyze (Expression (N));
6318 end Analyze_Free_Statement;
6320 ---------------------------
6321 -- Analyze_Freeze_Entity --
6322 ---------------------------
6324 procedure Analyze_Freeze_Entity (N : Node_Id) is
6326 Freeze_Entity_Checks (N);
6327 end Analyze_Freeze_Entity;
6329 -----------------------------------
6330 -- Analyze_Freeze_Generic_Entity --
6331 -----------------------------------
6333 procedure Analyze_Freeze_Generic_Entity (N : Node_Id) is
6335 Freeze_Entity_Checks (N);
6336 end Analyze_Freeze_Generic_Entity;
6338 ------------------------------------------
6339 -- Analyze_Record_Representation_Clause --
6340 ------------------------------------------
6342 -- Note: we check as much as we can here, but we can't do any checks
6343 -- based on the position values (e.g. overlap checks) until freeze time
6344 -- because especially in Ada 2005 (machine scalar mode), the processing
6345 -- for non-standard bit order can substantially change the positions.
6346 -- See procedure Check_Record_Representation_Clause (called from Freeze)
6347 -- for the remainder of this processing.
6349 procedure Analyze_Record_Representation_Clause (N : Node_Id) is
6350 Ident : constant Node_Id := Identifier (N);
6355 Hbit : Uint := Uint_0;
6359 Rectype : Entity_Id;
6362 function Is_Inherited (Comp : Entity_Id) return Boolean;
6363 -- True if Comp is an inherited component in a record extension
6369 function Is_Inherited (Comp : Entity_Id) return Boolean is
6370 Comp_Base : Entity_Id;
6373 if Ekind (Rectype) = E_Record_Subtype then
6374 Comp_Base := Original_Record_Component (Comp);
6379 return Comp_Base /= Original_Record_Component (Comp_Base);
6384 Is_Record_Extension : Boolean;
6385 -- True if Rectype is a record extension
6387 CR_Pragma : Node_Id := Empty;
6388 -- Points to N_Pragma node if Complete_Representation pragma present
6390 -- Start of processing for Analyze_Record_Representation_Clause
6393 if Ignore_Rep_Clauses then
6394 Kill_Rep_Clause (N);
6399 Rectype := Entity (Ident);
6401 if Rectype = Any_Type or else Rep_Item_Too_Early (Rectype, N) then
6404 Rectype := Underlying_Type (Rectype);
6407 -- First some basic error checks
6409 if not Is_Record_Type (Rectype) then
6411 ("record type required, found}", Ident, First_Subtype (Rectype));
6414 elsif Scope (Rectype) /= Current_Scope then
6415 Error_Msg_N ("type must be declared in this scope", N);
6418 elsif not Is_First_Subtype (Rectype) then
6419 Error_Msg_N ("cannot give record rep clause for subtype", N);
6422 elsif Has_Record_Rep_Clause (Rectype) then
6423 Error_Msg_N ("duplicate record rep clause ignored", N);
6426 elsif Rep_Item_Too_Late (Rectype, N) then
6430 -- We know we have a first subtype, now possibly go the the anonymous
6431 -- base type to determine whether Rectype is a record extension.
6433 Recdef := Type_Definition (Declaration_Node (Base_Type (Rectype)));
6434 Is_Record_Extension :=
6435 Nkind (Recdef) = N_Derived_Type_Definition
6436 and then Present (Record_Extension_Part (Recdef));
6438 if Present (Mod_Clause (N)) then
6440 Loc : constant Source_Ptr := Sloc (N);
6441 M : constant Node_Id := Mod_Clause (N);
6442 P : constant List_Id := Pragmas_Before (M);
6446 pragma Warnings (Off, Mod_Val);
6449 Check_Restriction (No_Obsolescent_Features, Mod_Clause (N));
6451 if Warn_On_Obsolescent_Feature then
6453 ("?j?mod clause is an obsolescent feature (RM J.8)", N);
6455 ("\?j?use alignment attribute definition clause instead", N);
6462 -- In ASIS_Mode mode, expansion is disabled, but we must convert
6463 -- the Mod clause into an alignment clause anyway, so that the
6464 -- back-end can compute and back-annotate properly the size and
6465 -- alignment of types that may include this record.
6467 -- This seems dubious, this destroys the source tree in a manner
6468 -- not detectable by ASIS ???
6470 if Operating_Mode = Check_Semantics and then ASIS_Mode then
6472 Make_Attribute_Definition_Clause (Loc,
6473 Name => New_Occurrence_Of (Base_Type (Rectype), Loc),
6474 Chars => Name_Alignment,
6475 Expression => Relocate_Node (Expression (M)));
6477 Set_From_At_Mod (AtM_Nod);
6478 Insert_After (N, AtM_Nod);
6479 Mod_Val := Get_Alignment_Value (Expression (AtM_Nod));
6480 Set_Mod_Clause (N, Empty);
6483 -- Get the alignment value to perform error checking
6485 Mod_Val := Get_Alignment_Value (Expression (M));
6490 -- For untagged types, clear any existing component clauses for the
6491 -- type. If the type is derived, this is what allows us to override
6492 -- a rep clause for the parent. For type extensions, the representation
6493 -- of the inherited components is inherited, so we want to keep previous
6494 -- component clauses for completeness.
6496 if not Is_Tagged_Type (Rectype) then
6497 Comp := First_Component_Or_Discriminant (Rectype);
6498 while Present (Comp) loop
6499 Set_Component_Clause (Comp, Empty);
6500 Next_Component_Or_Discriminant (Comp);
6504 -- All done if no component clauses
6506 CC := First (Component_Clauses (N));
6512 -- A representation like this applies to the base type
6514 Set_Has_Record_Rep_Clause (Base_Type (Rectype));
6515 Set_Has_Non_Standard_Rep (Base_Type (Rectype));
6516 Set_Has_Specified_Layout (Base_Type (Rectype));
6518 -- Process the component clauses
6520 while Present (CC) loop
6524 if Nkind (CC) = N_Pragma then
6527 -- The only pragma of interest is Complete_Representation
6529 if Pragma_Name (CC) = Name_Complete_Representation then
6533 -- Processing for real component clause
6536 Posit := Static_Integer (Position (CC));
6537 Fbit := Static_Integer (First_Bit (CC));
6538 Lbit := Static_Integer (Last_Bit (CC));
6541 and then Fbit /= No_Uint
6542 and then Lbit /= No_Uint
6546 ("position cannot be negative", Position (CC));
6550 ("first bit cannot be negative", First_Bit (CC));
6552 -- The Last_Bit specified in a component clause must not be
6553 -- less than the First_Bit minus one (RM-13.5.1(10)).
6555 elsif Lbit < Fbit - 1 then
6557 ("last bit cannot be less than first bit minus one",
6560 -- Values look OK, so find the corresponding record component
6561 -- Even though the syntax allows an attribute reference for
6562 -- implementation-defined components, GNAT does not allow the
6563 -- tag to get an explicit position.
6565 elsif Nkind (Component_Name (CC)) = N_Attribute_Reference then
6566 if Attribute_Name (Component_Name (CC)) = Name_Tag then
6567 Error_Msg_N ("position of tag cannot be specified", CC);
6569 Error_Msg_N ("illegal component name", CC);
6573 Comp := First_Entity (Rectype);
6574 while Present (Comp) loop
6575 exit when Chars (Comp) = Chars (Component_Name (CC));
6581 -- Maybe component of base type that is absent from
6582 -- statically constrained first subtype.
6584 Comp := First_Entity (Base_Type (Rectype));
6585 while Present (Comp) loop
6586 exit when Chars (Comp) = Chars (Component_Name (CC));
6593 ("component clause is for non-existent field", CC);
6595 -- Ada 2012 (AI05-0026): Any name that denotes a
6596 -- discriminant of an object of an unchecked union type
6597 -- shall not occur within a record_representation_clause.
6599 -- The general restriction of using record rep clauses on
6600 -- Unchecked_Union types has now been lifted. Since it is
6601 -- possible to introduce a record rep clause which mentions
6602 -- the discriminant of an Unchecked_Union in non-Ada 2012
6603 -- code, this check is applied to all versions of the
6606 elsif Ekind (Comp) = E_Discriminant
6607 and then Is_Unchecked_Union (Rectype)
6610 ("cannot reference discriminant of unchecked union",
6611 Component_Name (CC));
6613 elsif Is_Record_Extension and then Is_Inherited (Comp) then
6615 ("component clause not allowed for inherited "
6616 & "component&", CC, Comp);
6618 elsif Present (Component_Clause (Comp)) then
6620 -- Diagnose duplicate rep clause, or check consistency
6621 -- if this is an inherited component. In a double fault,
6622 -- there may be a duplicate inconsistent clause for an
6623 -- inherited component.
6625 if Scope (Original_Record_Component (Comp)) = Rectype
6626 or else Parent (Component_Clause (Comp)) = N
6628 Error_Msg_Sloc := Sloc (Component_Clause (Comp));
6629 Error_Msg_N ("component clause previously given#", CC);
6633 Rep1 : constant Node_Id := Component_Clause (Comp);
6635 if Intval (Position (Rep1)) /=
6636 Intval (Position (CC))
6637 or else Intval (First_Bit (Rep1)) /=
6638 Intval (First_Bit (CC))
6639 or else Intval (Last_Bit (Rep1)) /=
6640 Intval (Last_Bit (CC))
6643 ("component clause inconsistent "
6644 & "with representation of ancestor", CC);
6646 elsif Warn_On_Redundant_Constructs then
6648 ("?r?redundant confirming component clause "
6649 & "for component!", CC);
6654 -- Normal case where this is the first component clause we
6655 -- have seen for this entity, so set it up properly.
6658 -- Make reference for field in record rep clause and set
6659 -- appropriate entity field in the field identifier.
6662 (Comp, Component_Name (CC), Set_Ref => False);
6663 Set_Entity (Component_Name (CC), Comp);
6665 -- Update Fbit and Lbit to the actual bit number
6667 Fbit := Fbit + UI_From_Int (SSU) * Posit;
6668 Lbit := Lbit + UI_From_Int (SSU) * Posit;
6670 if Has_Size_Clause (Rectype)
6671 and then RM_Size (Rectype) <= Lbit
6674 ("bit number out of range of specified size",
6677 Set_Component_Clause (Comp, CC);
6678 Set_Component_Bit_Offset (Comp, Fbit);
6679 Set_Esize (Comp, 1 + (Lbit - Fbit));
6680 Set_Normalized_First_Bit (Comp, Fbit mod SSU);
6681 Set_Normalized_Position (Comp, Fbit / SSU);
6683 if Warn_On_Overridden_Size
6684 and then Has_Size_Clause (Etype (Comp))
6685 and then RM_Size (Etype (Comp)) /= Esize (Comp)
6688 ("?S?component size overrides size clause for&",
6689 Component_Name (CC), Etype (Comp));
6692 -- This information is also set in the corresponding
6693 -- component of the base type, found by accessing the
6694 -- Original_Record_Component link if it is present.
6696 Ocomp := Original_Record_Component (Comp);
6703 (Component_Name (CC),
6709 (Comp, First_Node (CC), "component clause", Biased);
6711 if Present (Ocomp) then
6712 Set_Component_Clause (Ocomp, CC);
6713 Set_Component_Bit_Offset (Ocomp, Fbit);
6714 Set_Normalized_First_Bit (Ocomp, Fbit mod SSU);
6715 Set_Normalized_Position (Ocomp, Fbit / SSU);
6716 Set_Esize (Ocomp, 1 + (Lbit - Fbit));
6718 Set_Normalized_Position_Max
6719 (Ocomp, Normalized_Position (Ocomp));
6721 -- Note: we don't use Set_Biased here, because we
6722 -- already gave a warning above if needed, and we
6723 -- would get a duplicate for the same name here.
6725 Set_Has_Biased_Representation
6726 (Ocomp, Has_Biased_Representation (Comp));
6729 if Esize (Comp) < 0 then
6730 Error_Msg_N ("component size is negative", CC);
6741 -- Check missing components if Complete_Representation pragma appeared
6743 if Present (CR_Pragma) then
6744 Comp := First_Component_Or_Discriminant (Rectype);
6745 while Present (Comp) loop
6746 if No (Component_Clause (Comp)) then
6748 ("missing component clause for &", CR_Pragma, Comp);
6751 Next_Component_Or_Discriminant (Comp);
6754 -- Give missing components warning if required
6756 elsif Warn_On_Unrepped_Components then
6758 Num_Repped_Components : Nat := 0;
6759 Num_Unrepped_Components : Nat := 0;
6762 -- First count number of repped and unrepped components
6764 Comp := First_Component_Or_Discriminant (Rectype);
6765 while Present (Comp) loop
6766 if Present (Component_Clause (Comp)) then
6767 Num_Repped_Components := Num_Repped_Components + 1;
6769 Num_Unrepped_Components := Num_Unrepped_Components + 1;
6772 Next_Component_Or_Discriminant (Comp);
6775 -- We are only interested in the case where there is at least one
6776 -- unrepped component, and at least half the components have rep
6777 -- clauses. We figure that if less than half have them, then the
6778 -- partial rep clause is really intentional. If the component
6779 -- type has no underlying type set at this point (as for a generic
6780 -- formal type), we don't know enough to give a warning on the
6783 if Num_Unrepped_Components > 0
6784 and then Num_Unrepped_Components < Num_Repped_Components
6786 Comp := First_Component_Or_Discriminant (Rectype);
6787 while Present (Comp) loop
6788 if No (Component_Clause (Comp))
6789 and then Comes_From_Source (Comp)
6790 and then Present (Underlying_Type (Etype (Comp)))
6791 and then (Is_Scalar_Type (Underlying_Type (Etype (Comp)))
6792 or else Size_Known_At_Compile_Time
6793 (Underlying_Type (Etype (Comp))))
6794 and then not Has_Warnings_Off (Rectype)
6796 -- Ignore discriminant in unchecked union, since it is
6797 -- not there, and cannot have a component clause.
6799 and then (not Is_Unchecked_Union (Rectype)
6800 or else Ekind (Comp) /= E_Discriminant)
6802 Error_Msg_Sloc := Sloc (Comp);
6804 ("?C?no component clause given for & declared #",
6808 Next_Component_Or_Discriminant (Comp);
6813 end Analyze_Record_Representation_Clause;
6815 -------------------------------------
6816 -- Build_Discrete_Static_Predicate --
6817 -------------------------------------
6819 procedure Build_Discrete_Static_Predicate
6824 Loc : constant Source_Ptr := Sloc (Expr);
6826 Non_Static : exception;
6827 -- Raised if something non-static is found
6829 Btyp : constant Entity_Id := Base_Type (Typ);
6831 BLo : constant Uint := Expr_Value (Type_Low_Bound (Btyp));
6832 BHi : constant Uint := Expr_Value (Type_High_Bound (Btyp));
6833 -- Low bound and high bound value of base type of Typ
6837 -- Bounds for constructing the static predicate. We use the bound of the
6838 -- subtype if it is static, otherwise the corresponding base type bound.
6839 -- Note: a non-static subtype can have a static predicate.
6844 -- One entry in a Rlist value, a single REnt (range entry) value denotes
6845 -- one range from Lo to Hi. To represent a single value range Lo = Hi =
6848 type RList is array (Nat range <>) of REnt;
6849 -- A list of ranges. The ranges are sorted in increasing order, and are
6850 -- disjoint (there is a gap of at least one value between each range in
6851 -- the table). A value is in the set of ranges in Rlist if it lies
6852 -- within one of these ranges.
6854 False_Range : constant RList :=
6855 RList'(1 .. 0 => REnt'(No_Uint, No_Uint));
6856 -- An empty set of ranges represents a range list that can never be
6857 -- satisfied, since there are no ranges in which the value could lie,
6858 -- so it does not lie in any of them. False_Range is a canonical value
6859 -- for this empty set, but general processing should test for an Rlist
6860 -- with length zero (see Is_False predicate), since other null ranges
6861 -- may appear which must be treated as False.
6863 True_Range : constant RList := RList'(1 => REnt'(BLo, BHi));
6864 -- Range representing True, value must be in the base range
6866 function "and" (Left : RList; Right : RList) return RList;
6867 -- And's together two range lists, returning a range list. This is a set
6868 -- intersection operation.
6870 function "or" (Left : RList; Right : RList) return RList;
6871 -- Or's together two range lists, returning a range list. This is a set
6874 function "not" (Right : RList) return RList;
6875 -- Returns complement of a given range list, i.e. a range list
6876 -- representing all the values in TLo .. THi that are not in the input
6879 function Build_Val (V : Uint) return Node_Id;
6880 -- Return an analyzed N_Identifier node referencing this value, suitable
6881 -- for use as an entry in the Static_Discrte_Predicate list. This node
6882 -- is typed with the base type.
6884 function Build_Range (Lo : Uint; Hi : Uint) return Node_Id;
6885 -- Return an analyzed N_Range node referencing this range, suitable for
6886 -- use as an entry in the Static_Discrete_Predicate list. This node is
6887 -- typed with the base type.
6889 function Get_RList (Exp : Node_Id) return RList;
6890 -- This is a recursive routine that converts the given expression into a
6891 -- list of ranges, suitable for use in building the static predicate.
6893 function Is_False (R : RList) return Boolean;
6894 pragma Inline (Is_False);
6895 -- Returns True if the given range list is empty, and thus represents a
6896 -- False list of ranges that can never be satisfied.
6898 function Is_True (R : RList) return Boolean;
6899 -- Returns True if R trivially represents the True predicate by having a
6900 -- single range from BLo to BHi.
6902 function Is_Type_Ref (N : Node_Id) return Boolean;
6903 pragma Inline (Is_Type_Ref);
6904 -- Returns if True if N is a reference to the type for the predicate in
6905 -- the expression (i.e. if it is an identifier whose Chars field matches
6906 -- the Nam given in the call). N must not be parenthesized, if the type
6907 -- name appears in parens, this routine will return False.
6909 function Lo_Val (N : Node_Id) return Uint;
6910 -- Given an entry from a Static_Discrete_Predicate list that is either
6911 -- a static expression or static range, gets either the expression value
6912 -- or the low bound of the range.
6914 function Hi_Val (N : Node_Id) return Uint;
6915 -- Given an entry from a Static_Discrete_Predicate list that is either
6916 -- a static expression or static range, gets either the expression value
6917 -- or the high bound of the range.
6919 function Membership_Entry (N : Node_Id) return RList;
6920 -- Given a single membership entry (range, value, or subtype), returns
6921 -- the corresponding range list. Raises Static_Error if not static.
6923 function Membership_Entries (N : Node_Id) return RList;
6924 -- Given an element on an alternatives list of a membership operation,
6925 -- returns the range list corresponding to this entry and all following
6926 -- entries (i.e. returns the "or" of this list of values).
6928 function Stat_Pred (Typ : Entity_Id) return RList;
6929 -- Given a type, if it has a static predicate, then return the predicate
6930 -- as a range list, otherwise raise Non_Static.
6936 function "and" (Left : RList; Right : RList) return RList is
6938 -- First range of result
6940 SLeft : Nat := Left'First;
6941 -- Start of rest of left entries
6943 SRight : Nat := Right'First;
6944 -- Start of rest of right entries
6947 -- If either range is True, return the other
6949 if Is_True (Left) then
6951 elsif Is_True (Right) then
6955 -- If either range is False, return False
6957 if Is_False (Left) or else Is_False (Right) then
6961 -- Loop to remove entries at start that are disjoint, and thus just
6962 -- get discarded from the result entirely.
6965 -- If no operands left in either operand, result is false
6967 if SLeft > Left'Last or else SRight > Right'Last then
6970 -- Discard first left operand entry if disjoint with right
6972 elsif Left (SLeft).Hi < Right (SRight).Lo then
6975 -- Discard first right operand entry if disjoint with left
6977 elsif Right (SRight).Hi < Left (SLeft).Lo then
6978 SRight := SRight + 1;
6980 -- Otherwise we have an overlapping entry
6987 -- Now we have two non-null operands, and first entries overlap. The
6988 -- first entry in the result will be the overlapping part of these
6991 FEnt := REnt'(Lo => UI_Max (Left (SLeft).Lo, Right (SRight).Lo),
6992 Hi => UI_Min (Left (SLeft).Hi, Right (SRight).Hi));
6994 -- Now we can remove the entry that ended at a lower value, since its
6995 -- contribution is entirely contained in Fent.
6997 if Left (SLeft).Hi <= Right (SRight).Hi then
7000 SRight := SRight + 1;
7003 -- Compute result by concatenating this first entry with the "and" of
7004 -- the remaining parts of the left and right operands. Note that if
7005 -- either of these is empty, "and" will yield empty, so that we will
7006 -- end up with just Fent, which is what we want in that case.
7009 FEnt & (Left (SLeft .. Left'Last) and Right (SRight .. Right'Last));
7016 function "not" (Right : RList) return RList is
7018 -- Return True if False range
7020 if Is_False (Right) then
7024 -- Return False if True range
7026 if Is_True (Right) then
7030 -- Here if not trivial case
7033 Result : RList (1 .. Right'Length + 1);
7034 -- May need one more entry for gap at beginning and end
7037 -- Number of entries stored in Result
7042 if Right (Right'First).Lo > TLo then
7044 Result (Count) := REnt'(TLo, Right (Right'First).Lo - 1);
7047 -- Gaps between ranges
7049 for J in Right'First .. Right'Last - 1 loop
7051 Result (Count) := REnt'(Right (J).Hi + 1, Right (J + 1).Lo - 1);
7056 if Right (Right'Last).Hi < THi then
7058 Result (Count) := REnt'(Right (Right'Last).Hi + 1, THi);
7061 return Result (1 .. Count);
7069 function "or" (Left : RList; Right : RList) return RList is
7071 -- First range of result
7073 SLeft : Nat := Left'First;
7074 -- Start of rest of left entries
7076 SRight : Nat := Right'First;
7077 -- Start of rest of right entries
7080 -- If either range is True, return True
7082 if Is_True (Left) or else Is_True (Right) then
7086 -- If either range is False (empty), return the other
7088 if Is_False (Left) then
7090 elsif Is_False (Right) then
7094 -- Initialize result first entry from left or right operand depending
7095 -- on which starts with the lower range.
7097 if Left (SLeft).Lo < Right (SRight).Lo then
7098 FEnt := Left (SLeft);
7101 FEnt := Right (SRight);
7102 SRight := SRight + 1;
7105 -- This loop eats ranges from left and right operands that are
7106 -- contiguous with the first range we are gathering.
7109 -- Eat first entry in left operand if contiguous or overlapped by
7110 -- gathered first operand of result.
7112 if SLeft <= Left'Last
7113 and then Left (SLeft).Lo <= FEnt.Hi + 1
7115 FEnt.Hi := UI_Max (FEnt.Hi, Left (SLeft).Hi);
7118 -- Eat first entry in right operand if contiguous or overlapped by
7119 -- gathered right operand of result.
7121 elsif SRight <= Right'Last
7122 and then Right (SRight).Lo <= FEnt.Hi + 1
7124 FEnt.Hi := UI_Max (FEnt.Hi, Right (SRight).Hi);
7125 SRight := SRight + 1;
7127 -- All done if no more entries to eat
7134 -- Obtain result as the first entry we just computed, concatenated
7135 -- to the "or" of the remaining results (if one operand is empty,
7136 -- this will just concatenate with the other
7139 FEnt & (Left (SLeft .. Left'Last) or Right (SRight .. Right'Last));
7146 function Build_Range (Lo : Uint; Hi : Uint) return Node_Id is
7151 Low_Bound => Build_Val (Lo),
7152 High_Bound => Build_Val (Hi));
7153 Set_Etype (Result, Btyp);
7154 Set_Analyzed (Result);
7162 function Build_Val (V : Uint) return Node_Id is
7166 if Is_Enumeration_Type (Typ) then
7167 Result := Get_Enum_Lit_From_Pos (Typ, V, Loc);
7169 Result := Make_Integer_Literal (Loc, V);
7172 Set_Etype (Result, Btyp);
7173 Set_Is_Static_Expression (Result);
7174 Set_Analyzed (Result);
7182 function Get_RList (Exp : Node_Id) return RList is
7187 -- Static expression can only be true or false
7189 if Is_OK_Static_Expression (Exp) then
7190 if Expr_Value (Exp) = 0 then
7197 -- Otherwise test node type
7205 when N_Op_And | N_And_Then =>
7206 return Get_RList (Left_Opnd (Exp))
7208 Get_RList (Right_Opnd (Exp));
7212 when N_Op_Or | N_Or_Else =>
7213 return Get_RList (Left_Opnd (Exp))
7215 Get_RList (Right_Opnd (Exp));
7220 return not Get_RList (Right_Opnd (Exp));
7222 -- Comparisons of type with static value
7224 when N_Op_Compare =>
7226 -- Type is left operand
7228 if Is_Type_Ref (Left_Opnd (Exp))
7229 and then Is_OK_Static_Expression (Right_Opnd (Exp))
7231 Val := Expr_Value (Right_Opnd (Exp));
7233 -- Typ is right operand
7235 elsif Is_Type_Ref (Right_Opnd (Exp))
7236 and then Is_OK_Static_Expression (Left_Opnd (Exp))
7238 Val := Expr_Value (Left_Opnd (Exp));
7240 -- Invert sense of comparison
7243 when N_Op_Gt => Op := N_Op_Lt;
7244 when N_Op_Lt => Op := N_Op_Gt;
7245 when N_Op_Ge => Op := N_Op_Le;
7246 when N_Op_Le => Op := N_Op_Ge;
7247 when others => null;
7250 -- Other cases are non-static
7256 -- Construct range according to comparison operation
7260 return RList'(1 => REnt'(Val, Val));
7263 return RList'(1 => REnt'(Val, BHi));
7266 return RList'(1 => REnt'(Val + 1, BHi));
7269 return RList'(1 => REnt'(BLo, Val));
7272 return RList'(1 => REnt'(BLo, Val - 1));
7275 return RList'(REnt'(BLo, Val - 1), REnt'(Val + 1, BHi));
7278 raise Program_Error;
7284 if not Is_Type_Ref (Left_Opnd (Exp)) then
7288 if Present (Right_Opnd (Exp)) then
7289 return Membership_Entry (Right_Opnd (Exp));
7291 return Membership_Entries (First (Alternatives (Exp)));
7294 -- Negative membership (NOT IN)
7297 if not Is_Type_Ref (Left_Opnd (Exp)) then
7301 if Present (Right_Opnd (Exp)) then
7302 return not Membership_Entry (Right_Opnd (Exp));
7304 return not Membership_Entries (First (Alternatives (Exp)));
7307 -- Function call, may be call to static predicate
7309 when N_Function_Call =>
7310 if Is_Entity_Name (Name (Exp)) then
7312 Ent : constant Entity_Id := Entity (Name (Exp));
7314 if Is_Predicate_Function (Ent)
7316 Is_Predicate_Function_M (Ent)
7318 return Stat_Pred (Etype (First_Formal (Ent)));
7323 -- Other function call cases are non-static
7327 -- Qualified expression, dig out the expression
7329 when N_Qualified_Expression =>
7330 return Get_RList (Expression (Exp));
7332 when N_Case_Expression =>
7339 if not Is_Entity_Name (Expression (Expr))
7340 or else Etype (Expression (Expr)) /= Typ
7343 ("expression must denaote subtype", Expression (Expr));
7347 -- Collect discrete choices in all True alternatives
7349 Choices := New_List;
7350 Alt := First (Alternatives (Exp));
7351 while Present (Alt) loop
7352 Dep := Expression (Alt);
7354 if not Is_OK_Static_Expression (Dep) then
7357 elsif Is_True (Expr_Value (Dep)) then
7358 Append_List_To (Choices,
7359 New_Copy_List (Discrete_Choices (Alt)));
7365 return Membership_Entries (First (Choices));
7368 -- Expression with actions: if no actions, dig out expression
7370 when N_Expression_With_Actions =>
7371 if Is_Empty_List (Actions (Exp)) then
7372 return Get_RList (Expression (Exp));
7380 return (Get_RList (Left_Opnd (Exp))
7381 and not Get_RList (Right_Opnd (Exp)))
7382 or (Get_RList (Right_Opnd (Exp))
7383 and not Get_RList (Left_Opnd (Exp)));
7385 -- Any other node type is non-static
7396 function Hi_Val (N : Node_Id) return Uint is
7398 if Is_OK_Static_Expression (N) then
7399 return Expr_Value (N);
7401 pragma Assert (Nkind (N) = N_Range);
7402 return Expr_Value (High_Bound (N));
7410 function Is_False (R : RList) return Boolean is
7412 return R'Length = 0;
7419 function Is_True (R : RList) return Boolean is
7422 and then R (R'First).Lo = BLo
7423 and then R (R'First).Hi = BHi;
7430 function Is_Type_Ref (N : Node_Id) return Boolean is
7432 return Nkind (N) = N_Identifier
7433 and then Chars (N) = Nam
7434 and then Paren_Count (N) = 0;
7441 function Lo_Val (N : Node_Id) return Uint is
7443 if Is_OK_Static_Expression (N) then
7444 return Expr_Value (N);
7446 pragma Assert (Nkind (N) = N_Range);
7447 return Expr_Value (Low_Bound (N));
7451 ------------------------
7452 -- Membership_Entries --
7453 ------------------------
7455 function Membership_Entries (N : Node_Id) return RList is
7457 if No (Next (N)) then
7458 return Membership_Entry (N);
7460 return Membership_Entry (N) or Membership_Entries (Next (N));
7462 end Membership_Entries;
7464 ----------------------
7465 -- Membership_Entry --
7466 ----------------------
7468 function Membership_Entry (N : Node_Id) return RList is
7476 if Nkind (N) = N_Range then
7477 if not Is_OK_Static_Expression (Low_Bound (N))
7479 not Is_OK_Static_Expression (High_Bound (N))
7483 SLo := Expr_Value (Low_Bound (N));
7484 SHi := Expr_Value (High_Bound (N));
7485 return RList'(1 => REnt'(SLo, SHi));
7488 -- Static expression case
7490 elsif Is_OK_Static_Expression (N) then
7491 Val := Expr_Value (N);
7492 return RList'(1 => REnt'(Val, Val));
7494 -- Identifier (other than static expression) case
7496 else pragma Assert (Nkind (N) = N_Identifier);
7500 if Is_Type (Entity (N)) then
7502 -- If type has predicates, process them
7504 if Has_Predicates (Entity (N)) then
7505 return Stat_Pred (Entity (N));
7507 -- For static subtype without predicates, get range
7509 elsif Is_OK_Static_Subtype (Entity (N)) then
7510 SLo := Expr_Value (Type_Low_Bound (Entity (N)));
7511 SHi := Expr_Value (Type_High_Bound (Entity (N)));
7512 return RList'(1 => REnt'(SLo, SHi));
7514 -- Any other type makes us non-static
7520 -- Any other kind of identifier in predicate (e.g. a non-static
7521 -- expression value) means this is not a static predicate.
7527 end Membership_Entry;
7533 function Stat_Pred (Typ : Entity_Id) return RList is
7535 -- Not static if type does not have static predicates
7537 if not Has_Static_Predicate (Typ) then
7541 -- Otherwise we convert the predicate list to a range list
7544 Spred : constant List_Id := Static_Discrete_Predicate (Typ);
7545 Result : RList (1 .. List_Length (Spred));
7549 P := First (Static_Discrete_Predicate (Typ));
7550 for J in Result'Range loop
7551 Result (J) := REnt'(Lo_Val (P), Hi_Val (P));
7559 -- Start of processing for Build_Discrete_Static_Predicate
7562 -- Establish bounds for the predicate
7564 if Compile_Time_Known_Value (Type_Low_Bound (Typ)) then
7565 TLo := Expr_Value (Type_Low_Bound (Typ));
7570 if Compile_Time_Known_Value (Type_High_Bound (Typ)) then
7571 THi := Expr_Value (Type_High_Bound (Typ));
7576 -- Analyze the expression to see if it is a static predicate
7579 Ranges : constant RList := Get_RList (Expr);
7580 -- Range list from expression if it is static
7585 -- Convert range list into a form for the static predicate. In the
7586 -- Ranges array, we just have raw ranges, these must be converted
7587 -- to properly typed and analyzed static expressions or range nodes.
7589 -- Note: here we limit ranges to the ranges of the subtype, so that
7590 -- a predicate is always false for values outside the subtype. That
7591 -- seems fine, such values are invalid anyway, and considering them
7592 -- to fail the predicate seems allowed and friendly, and furthermore
7593 -- simplifies processing for case statements and loops.
7597 for J in Ranges'Range loop
7599 Lo : Uint := Ranges (J).Lo;
7600 Hi : Uint := Ranges (J).Hi;
7603 -- Ignore completely out of range entry
7605 if Hi < TLo or else Lo > THi then
7608 -- Otherwise process entry
7611 -- Adjust out of range value to subtype range
7621 -- Convert range into required form
7623 Append_To (Plist, Build_Range (Lo, Hi));
7628 -- Processing was successful and all entries were static, so now we
7629 -- can store the result as the predicate list.
7631 Set_Static_Discrete_Predicate (Typ, Plist);
7633 -- The processing for static predicates put the expression into
7634 -- canonical form as a series of ranges. It also eliminated
7635 -- duplicates and collapsed and combined ranges. We might as well
7636 -- replace the alternatives list of the right operand of the
7637 -- membership test with the static predicate list, which will
7638 -- usually be more efficient.
7641 New_Alts : constant List_Id := New_List;
7646 Old_Node := First (Plist);
7647 while Present (Old_Node) loop
7648 New_Node := New_Copy (Old_Node);
7650 if Nkind (New_Node) = N_Range then
7651 Set_Low_Bound (New_Node, New_Copy (Low_Bound (Old_Node)));
7652 Set_High_Bound (New_Node, New_Copy (High_Bound (Old_Node)));
7655 Append_To (New_Alts, New_Node);
7659 -- If empty list, replace by False
7661 if Is_Empty_List (New_Alts) then
7662 Rewrite (Expr, New_Occurrence_Of (Standard_False, Loc));
7664 -- Else replace by set membership test
7669 Left_Opnd => Make_Identifier (Loc, Nam),
7670 Right_Opnd => Empty,
7671 Alternatives => New_Alts));
7673 -- Resolve new expression in function context
7675 Install_Formals (Predicate_Function (Typ));
7676 Push_Scope (Predicate_Function (Typ));
7677 Analyze_And_Resolve (Expr, Standard_Boolean);
7683 -- If non-static, return doing nothing
7688 end Build_Discrete_Static_Predicate;
7690 -------------------------------------------
7691 -- Build_Invariant_Procedure_Declaration --
7692 -------------------------------------------
7694 function Build_Invariant_Procedure_Declaration
7695 (Typ : Entity_Id) return Node_Id
7697 Loc : constant Source_Ptr := Sloc (Typ);
7698 Object_Entity : constant Entity_Id :=
7699 Make_Defining_Identifier (Loc, New_Internal_Name ('I'));
7704 Set_Etype (Object_Entity, Typ);
7706 -- Check for duplicate definiations.
7708 if Has_Invariants (Typ) and then Present (Invariant_Procedure (Typ)) then
7713 Make_Defining_Identifier (Loc,
7714 Chars => New_External_Name (Chars (Typ), "Invariant"));
7715 Set_Has_Invariants (Typ);
7716 Set_Ekind (SId, E_Procedure);
7717 Set_Etype (SId, Standard_Void_Type);
7718 Set_Is_Invariant_Procedure (SId);
7719 Set_Invariant_Procedure (Typ, SId);
7722 Make_Procedure_Specification (Loc,
7723 Defining_Unit_Name => SId,
7724 Parameter_Specifications => New_List (
7725 Make_Parameter_Specification (Loc,
7726 Defining_Identifier => Object_Entity,
7727 Parameter_Type => New_Occurrence_Of (Typ, Loc))));
7729 return Make_Subprogram_Declaration (Loc, Specification => Spec);
7730 end Build_Invariant_Procedure_Declaration;
7732 -------------------------------
7733 -- Build_Invariant_Procedure --
7734 -------------------------------
7736 -- The procedure that is constructed here has the form
7738 -- procedure typInvariant (Ixxx : typ) is
7740 -- pragma Check (Invariant, exp, "failed invariant from xxx");
7741 -- pragma Check (Invariant, exp, "failed invariant from xxx");
7743 -- pragma Check (Invariant, exp, "failed inherited invariant from xxx");
7745 -- end typInvariant;
7747 procedure Build_Invariant_Procedure (Typ : Entity_Id; N : Node_Id) is
7748 Loc : constant Source_Ptr := Sloc (Typ);
7756 -- Name for Check pragma, usually Invariant, but might be Type_Invariant
7757 -- if we come from a Type_Invariant aspect, we make sure to build the
7758 -- Check pragma with the right name, so that Check_Policy works right.
7760 Visible_Decls : constant List_Id := Visible_Declarations (N);
7761 Private_Decls : constant List_Id := Private_Declarations (N);
7763 procedure Add_Invariants (T : Entity_Id; Inherit : Boolean);
7764 -- Appends statements to Stmts for any invariants in the rep item chain
7765 -- of the given type. If Inherit is False, then we only process entries
7766 -- on the chain for the type Typ. If Inherit is True, then we ignore any
7767 -- Invariant aspects, but we process all Invariant'Class aspects, adding
7768 -- "inherited" to the exception message and generating an informational
7769 -- message about the inheritance of an invariant.
7771 Object_Name : Name_Id;
7772 -- Name for argument of invariant procedure
7774 Object_Entity : Node_Id;
7775 -- The entity of the formal for the procedure
7777 --------------------
7778 -- Add_Invariants --
7779 --------------------
7781 procedure Add_Invariants (T : Entity_Id; Inherit : Boolean) is
7791 procedure Replace_Type_Reference (N : Node_Id);
7792 -- Replace a single occurrence N of the subtype name with a reference
7793 -- to the formal of the predicate function. N can be an identifier
7794 -- referencing the subtype, or a selected component, representing an
7795 -- appropriately qualified occurrence of the subtype name.
7797 procedure Replace_Type_References is
7798 new Replace_Type_References_Generic (Replace_Type_Reference);
7799 -- Traverse an expression replacing all occurrences of the subtype
7800 -- name with appropriate references to the object that is the formal
7801 -- parameter of the predicate function. Note that we must ensure
7802 -- that the type and entity information is properly set in the
7803 -- replacement node, since we will do a Preanalyze call of this
7804 -- expression without proper visibility of the procedure argument.
7806 ----------------------------
7807 -- Replace_Type_Reference --
7808 ----------------------------
7810 -- Note: See comments in Add_Predicates.Replace_Type_Reference
7811 -- regarding handling of Sloc and Comes_From_Source.
7813 procedure Replace_Type_Reference (N : Node_Id) is
7816 -- Add semantic information to node to be rewritten, for ASIS
7817 -- navigation needs.
7819 if Nkind (N) = N_Identifier then
7823 elsif Nkind (N) = N_Selected_Component then
7824 Analyze (Prefix (N));
7825 Set_Entity (Selector_Name (N), T);
7826 Set_Etype (Selector_Name (N), T);
7829 -- Invariant'Class, replace with T'Class (obj)
7830 -- In ASIS mode, an inherited item is analyzed already, and the
7831 -- replacement has been done, so do not repeat transformation
7832 -- to prevent ill-formed tree.
7834 if Class_Present (Ritem) then
7836 and then Nkind (Parent (N)) = N_Attribute_Reference
7837 and then Attribute_Name (Parent (N)) = Name_Class
7843 Make_Type_Conversion (Sloc (N),
7845 Make_Attribute_Reference (Sloc (N),
7846 Prefix => New_Occurrence_Of (T, Sloc (N)),
7847 Attribute_Name => Name_Class),
7849 Make_Identifier (Sloc (N), Object_Name)));
7851 Set_Entity (Expression (N), Object_Entity);
7852 Set_Etype (Expression (N), Typ);
7855 -- Invariant, replace with obj
7858 Rewrite (N, Make_Identifier (Sloc (N), Object_Name));
7859 Set_Entity (N, Object_Entity);
7863 Set_Comes_From_Source (N, True);
7864 end Replace_Type_Reference;
7866 -- Start of processing for Add_Invariants
7869 Ritem := First_Rep_Item (T);
7870 while Present (Ritem) loop
7871 if Nkind (Ritem) = N_Pragma
7872 and then Pragma_Name (Ritem) = Name_Invariant
7874 Arg1 := First (Pragma_Argument_Associations (Ritem));
7875 Arg2 := Next (Arg1);
7876 Arg3 := Next (Arg2);
7878 Arg1 := Get_Pragma_Arg (Arg1);
7879 Arg2 := Get_Pragma_Arg (Arg2);
7881 -- For Inherit case, ignore Invariant, process only Class case
7884 if not Class_Present (Ritem) then
7888 -- For Inherit false, process only item for right type
7891 if Entity (Arg1) /= Typ then
7897 Stmts := Empty_List;
7900 Exp := New_Copy_Tree (Arg2);
7902 -- Preserve sloc of original pragma Invariant
7904 Loc := Sloc (Ritem);
7906 -- We need to replace any occurrences of the name of the type
7907 -- with references to the object, converted to type'Class in
7908 -- the case of Invariant'Class aspects.
7910 Replace_Type_References (Exp, T);
7912 -- If this invariant comes from an aspect, find the aspect
7913 -- specification, and replace the saved expression because
7914 -- we need the subtype references replaced for the calls to
7915 -- Preanalyze_Spec_Expressin in Check_Aspect_At_Freeze_Point
7916 -- and Check_Aspect_At_End_Of_Declarations.
7918 if From_Aspect_Specification (Ritem) then
7923 -- Loop to find corresponding aspect, note that this
7924 -- must be present given the pragma is marked delayed.
7926 -- Note: in practice Next_Rep_Item (Ritem) is Empty so
7927 -- this loop does nothing. Furthermore, why isn't this
7928 -- simply Corresponding_Aspect ???
7930 Aitem := Next_Rep_Item (Ritem);
7931 while Present (Aitem) loop
7932 if Nkind (Aitem) = N_Aspect_Specification
7933 and then Aspect_Rep_Item (Aitem) = Ritem
7936 (Identifier (Aitem), New_Copy_Tree (Exp));
7940 Aitem := Next_Rep_Item (Aitem);
7945 -- Now we need to preanalyze the expression to properly capture
7946 -- the visibility in the visible part. The expression will not
7947 -- be analyzed for real until the body is analyzed, but that is
7948 -- at the end of the private part and has the wrong visibility.
7950 Set_Parent (Exp, N);
7951 Preanalyze_Assert_Expression (Exp, Any_Boolean);
7953 -- A class-wide invariant may be inherited in a separate unit,
7954 -- where the corresponding expression cannot be resolved by
7955 -- visibility, because it refers to a local function. Propagate
7956 -- semantic information to the original representation item, to
7957 -- be used when an invariant procedure for a derived type is
7960 -- Unclear how to handle class-wide invariants that are not
7961 -- function calls ???
7964 and then Class_Present (Ritem)
7965 and then Nkind (Exp) = N_Function_Call
7966 and then Nkind (Arg2) = N_Indexed_Component
7969 Make_Function_Call (Loc,
7971 New_Occurrence_Of (Entity (Name (Exp)), Loc),
7972 Parameter_Associations =>
7973 New_Copy_List (Expressions (Arg2))));
7976 -- In ASIS mode, even if assertions are not enabled, we must
7977 -- analyze the original expression in the aspect specification
7978 -- because it is part of the original tree.
7980 if ASIS_Mode and then From_Aspect_Specification (Ritem) then
7982 Inv : constant Node_Id :=
7983 Expression (Corresponding_Aspect (Ritem));
7985 Replace_Type_References (Inv, T);
7986 Preanalyze_Assert_Expression (Inv, Standard_Boolean);
7990 -- Get name to be used for Check pragma
7992 if not From_Aspect_Specification (Ritem) then
7993 Nam := Name_Invariant;
7995 Nam := Chars (Identifier (Corresponding_Aspect (Ritem)));
7998 -- Build first two arguments for Check pragma
8002 Make_Pragma_Argument_Association (Loc,
8003 Expression => Make_Identifier (Loc, Chars => Nam)),
8004 Make_Pragma_Argument_Association (Loc,
8005 Expression => Exp));
8007 -- Add message if present in Invariant pragma
8009 if Present (Arg3) then
8010 Str := Strval (Get_Pragma_Arg (Arg3));
8012 -- If inherited case, and message starts "failed invariant",
8013 -- change it to be "failed inherited invariant".
8016 String_To_Name_Buffer (Str);
8018 if Name_Buffer (1 .. 16) = "failed invariant" then
8019 Insert_Str_In_Name_Buffer ("inherited ", 8);
8020 Str := String_From_Name_Buffer;
8025 Make_Pragma_Argument_Association (Loc,
8026 Expression => Make_String_Literal (Loc, Str)));
8029 -- Add Check pragma to list of statements
8033 Pragma_Identifier =>
8034 Make_Identifier (Loc, Name_Check),
8035 Pragma_Argument_Associations => Assoc));
8037 -- If Inherited case and option enabled, output info msg. Note
8038 -- that we know this is a case of Invariant'Class.
8040 if Inherit and Opt.List_Inherited_Aspects then
8041 Error_Msg_Sloc := Sloc (Ritem);
8043 ("info: & inherits `Invariant''Class` aspect from #?L?",
8049 Next_Rep_Item (Ritem);
8053 -- Start of processing for Build_Invariant_Procedure
8061 -- If the aspect specification exists for some view of the type, the
8062 -- declaration for the procedure has been created.
8064 if Has_Invariants (Typ) then
8065 SId := Invariant_Procedure (Typ);
8068 -- If the body is already present, nothing to do. This will occur when
8069 -- the type is already frozen, which is the case when the invariant
8070 -- appears in a private part, and the freezing takes place before the
8071 -- final pass over full declarations.
8073 -- See Exp_Ch3.Insert_Component_Invariant_Checks for details.
8075 if Present (SId) then
8076 PDecl := Unit_Declaration_Node (SId);
8079 and then Nkind (PDecl) = N_Subprogram_Declaration
8080 and then Present (Corresponding_Body (PDecl))
8086 PDecl := Build_Invariant_Procedure_Declaration (Typ);
8089 -- Recover formal of procedure, for use in the calls to invariant
8090 -- functions (including inherited ones).
8094 (First (Parameter_Specifications (Specification (PDecl))));
8095 Object_Name := Chars (Object_Entity);
8097 -- Add invariants for the current type
8099 Add_Invariants (Typ, Inherit => False);
8101 -- Add invariants for parent types
8104 Current_Typ : Entity_Id;
8105 Parent_Typ : Entity_Id;
8110 Parent_Typ := Etype (Current_Typ);
8112 if Is_Private_Type (Parent_Typ)
8113 and then Present (Full_View (Base_Type (Parent_Typ)))
8115 Parent_Typ := Full_View (Base_Type (Parent_Typ));
8118 exit when Parent_Typ = Current_Typ;
8120 Current_Typ := Parent_Typ;
8121 Add_Invariants (Current_Typ, Inherit => True);
8125 -- Add invariants of progenitors
8127 if Is_Tagged_Type (Typ) and then not Is_Interface (Typ) then
8129 Ifaces_List : Elist_Id;
8134 Collect_Interfaces (Typ, Ifaces_List);
8136 AI := First_Elmt (Ifaces_List);
8137 while Present (AI) loop
8140 if not Is_Ancestor (Iface, Typ, Use_Full_View => True) then
8141 Add_Invariants (Iface, Inherit => True);
8149 -- Build the procedure if we generated at least one Check pragma
8151 if Stmts /= No_List then
8152 Spec := Copy_Separate_Tree (Specification (PDecl));
8155 Make_Subprogram_Body (Loc,
8156 Specification => Spec,
8157 Declarations => Empty_List,
8158 Handled_Statement_Sequence =>
8159 Make_Handled_Sequence_Of_Statements (Loc,
8160 Statements => Stmts));
8162 -- Insert procedure declaration and spec at the appropriate points.
8163 -- If declaration is already analyzed, it was processed by the
8164 -- generated pragma.
8166 if Present (Private_Decls) then
8168 -- The spec goes at the end of visible declarations, but they have
8169 -- already been analyzed, so we need to explicitly do the analyze.
8171 if not Analyzed (PDecl) then
8172 Append_To (Visible_Decls, PDecl);
8176 -- The body goes at the end of the private declarations, which we
8177 -- have not analyzed yet, so we do not need to perform an explicit
8178 -- analyze call. We skip this if there are no private declarations
8179 -- (this is an error that will be caught elsewhere);
8181 Append_To (Private_Decls, PBody);
8183 -- If the invariant appears on the full view of a type, the
8184 -- analysis of the private part is complete, and we must
8185 -- analyze the new body explicitly.
8187 if In_Private_Part (Current_Scope) then
8191 -- If there are no private declarations this may be an error that
8192 -- will be diagnosed elsewhere. However, if this is a non-private
8193 -- type that inherits invariants, it needs no completion and there
8194 -- may be no private part. In this case insert invariant procedure
8195 -- at end of current declarative list, and analyze at once, given
8196 -- that the type is about to be frozen.
8198 elsif not Is_Private_Type (Typ) then
8199 Append_To (Visible_Decls, PDecl);
8200 Append_To (Visible_Decls, PBody);
8205 end Build_Invariant_Procedure;
8207 -------------------------------
8208 -- Build_Predicate_Functions --
8209 -------------------------------
8211 -- The procedures that are constructed here have the form:
8213 -- function typPredicate (Ixxx : typ) return Boolean is
8216 -- exp1 and then exp2 and then ...
8217 -- and then typ1Predicate (typ1 (Ixxx))
8218 -- and then typ2Predicate (typ2 (Ixxx))
8220 -- end typPredicate;
8222 -- Here exp1, and exp2 are expressions from Predicate pragmas. Note that
8223 -- this is the point at which these expressions get analyzed, providing the
8224 -- required delay, and typ1, typ2, are entities from which predicates are
8225 -- inherited. Note that we do NOT generate Check pragmas, that's because we
8226 -- use this function even if checks are off, e.g. for membership tests.
8228 -- If the expression has at least one Raise_Expression, then we also build
8229 -- the typPredicateM version of the function, in which any occurrence of a
8230 -- Raise_Expression is converted to "return False".
8232 procedure Build_Predicate_Functions (Typ : Entity_Id; N : Node_Id) is
8233 Loc : constant Source_Ptr := Sloc (Typ);
8236 -- This is the expression for the result of the function. It is
8237 -- is build by connecting the component predicates with AND THEN.
8240 -- This is the corresponding return expression for the Predicate_M
8241 -- function. It differs in that raise expressions are marked for
8242 -- special expansion (see Process_REs).
8244 Object_Name : constant Name_Id := New_Internal_Name ('I');
8245 -- Name for argument of Predicate procedure. Note that we use the same
8246 -- name for both predicate functions. That way the reference within the
8247 -- predicate expression is the same in both functions.
8249 Object_Entity : constant Entity_Id :=
8250 Make_Defining_Identifier (Loc, Chars => Object_Name);
8251 -- Entity for argument of Predicate procedure
8253 Object_Entity_M : constant Entity_Id :=
8254 Make_Defining_Identifier (Loc, Chars => Object_Name);
8255 -- Entity for argument of Predicate_M procedure
8257 Raise_Expression_Present : Boolean := False;
8258 -- Set True if Expr has at least one Raise_Expression
8260 procedure Add_Call (T : Entity_Id);
8261 -- Includes a call to the predicate function for type T in Expr if T
8262 -- has predicates and Predicate_Function (T) is non-empty.
8264 procedure Add_Predicates;
8265 -- Appends expressions for any Predicate pragmas in the rep item chain
8266 -- Typ to Expr. Note that we look only at items for this exact entity.
8267 -- Inheritance of predicates for the parent type is done by calling the
8268 -- Predicate_Function of the parent type, using Add_Call above.
8270 function Test_RE (N : Node_Id) return Traverse_Result;
8271 -- Used in Test_REs, tests one node for being a raise expression, and if
8272 -- so sets Raise_Expression_Present True.
8274 procedure Test_REs is new Traverse_Proc (Test_RE);
8275 -- Tests to see if Expr contains any raise expressions
8277 function Process_RE (N : Node_Id) return Traverse_Result;
8278 -- Used in Process REs, tests if node N is a raise expression, and if
8279 -- so, marks it to be converted to return False.
8281 procedure Process_REs is new Traverse_Proc (Process_RE);
8282 -- Marks any raise expressions in Expr_M to return False
8288 procedure Add_Call (T : Entity_Id) is
8292 if Present (T) and then Present (Predicate_Function (T)) then
8293 Set_Has_Predicates (Typ);
8295 -- Build the call to the predicate function of T
8299 (T, Convert_To (T, Make_Identifier (Loc, Object_Name)));
8301 -- Add call to evolving expression, using AND THEN if needed
8308 Make_And_Then (Sloc (Expr),
8309 Left_Opnd => Relocate_Node (Expr),
8313 -- Output info message on inheritance if required. Note we do not
8314 -- give this information for generic actual types, since it is
8315 -- unwelcome noise in that case in instantiations. We also
8316 -- generally suppress the message in instantiations, and also
8317 -- if it involves internal names.
8319 if Opt.List_Inherited_Aspects
8320 and then not Is_Generic_Actual_Type (Typ)
8321 and then Instantiation_Depth (Sloc (Typ)) = 0
8322 and then not Is_Internal_Name (Chars (T))
8323 and then not Is_Internal_Name (Chars (Typ))
8325 Error_Msg_Sloc := Sloc (Predicate_Function (T));
8326 Error_Msg_Node_2 := T;
8327 Error_Msg_N ("info: & inherits predicate from & #?L?", Typ);
8332 --------------------
8333 -- Add_Predicates --
8334 --------------------
8336 procedure Add_Predicates is
8341 procedure Replace_Type_Reference (N : Node_Id);
8342 -- Replace a single occurrence N of the subtype name with a reference
8343 -- to the formal of the predicate function. N can be an identifier
8344 -- referencing the subtype, or a selected component, representing an
8345 -- appropriately qualified occurrence of the subtype name.
8347 procedure Replace_Type_References is
8348 new Replace_Type_References_Generic (Replace_Type_Reference);
8349 -- Traverse an expression changing every occurrence of an identifier
8350 -- whose name matches the name of the subtype with a reference to
8351 -- the formal parameter of the predicate function.
8353 ----------------------------
8354 -- Replace_Type_Reference --
8355 ----------------------------
8357 procedure Replace_Type_Reference (N : Node_Id) is
8359 Rewrite (N, Make_Identifier (Sloc (N), Object_Name));
8360 -- Use the Sloc of the usage name, not the defining name
8363 Set_Entity (N, Object_Entity);
8365 -- We want to treat the node as if it comes from source, so that
8366 -- ASIS will not ignore it
8368 Set_Comes_From_Source (N, True);
8369 end Replace_Type_Reference;
8371 -- Start of processing for Add_Predicates
8374 Ritem := First_Rep_Item (Typ);
8375 while Present (Ritem) loop
8376 if Nkind (Ritem) = N_Pragma
8377 and then Pragma_Name (Ritem) = Name_Predicate
8379 -- Acquire arguments
8381 Arg1 := First (Pragma_Argument_Associations (Ritem));
8382 Arg2 := Next (Arg1);
8384 Arg1 := Get_Pragma_Arg (Arg1);
8385 Arg2 := Get_Pragma_Arg (Arg2);
8387 -- See if this predicate pragma is for the current type or for
8388 -- its full view. A predicate on a private completion is placed
8389 -- on the partial view beause this is the visible entity that
8392 if Entity (Arg1) = Typ
8393 or else Full_View (Entity (Arg1)) = Typ
8395 -- We have a match, this entry is for our subtype
8397 -- We need to replace any occurrences of the name of the
8398 -- type with references to the object.
8400 Replace_Type_References (Arg2, Typ);
8402 -- If this predicate comes from an aspect, find the aspect
8403 -- specification, and replace the saved expression because
8404 -- we need the subtype references replaced for the calls to
8405 -- Preanalyze_Spec_Expressin in Check_Aspect_At_Freeze_Point
8406 -- and Check_Aspect_At_End_Of_Declarations.
8408 if From_Aspect_Specification (Ritem) then
8413 -- Loop to find corresponding aspect, note that this
8414 -- must be present given the pragma is marked delayed.
8416 Aitem := Next_Rep_Item (Ritem);
8418 if Nkind (Aitem) = N_Aspect_Specification
8419 and then Aspect_Rep_Item (Aitem) = Ritem
8422 (Identifier (Aitem), New_Copy_Tree (Arg2));
8426 Aitem := Next_Rep_Item (Aitem);
8431 -- Now we can add the expression
8434 Expr := Relocate_Node (Arg2);
8436 -- There already was a predicate, so add to it
8441 Left_Opnd => Relocate_Node (Expr),
8442 Right_Opnd => Relocate_Node (Arg2));
8447 Next_Rep_Item (Ritem);
8455 function Process_RE (N : Node_Id) return Traverse_Result is
8457 if Nkind (N) = N_Raise_Expression then
8458 Set_Convert_To_Return_False (N);
8469 function Test_RE (N : Node_Id) return Traverse_Result is
8471 if Nkind (N) = N_Raise_Expression then
8472 Raise_Expression_Present := True;
8479 -- Start of processing for Build_Predicate_Functions
8482 -- Return if already built or if type does not have predicates
8484 if not Has_Predicates (Typ)
8485 or else Present (Predicate_Function (Typ))
8490 -- Prepare to construct predicate expression
8494 -- Add Predicates for the current type
8498 -- Add predicates for ancestor if present
8501 Atyp : constant Entity_Id := Nearest_Ancestor (Typ);
8503 if Present (Atyp) then
8508 -- Case where predicates are present
8510 if Present (Expr) then
8512 -- Test for raise expression present
8516 -- If raise expression is present, capture a copy of Expr for use
8517 -- in building the predicateM function version later on. For this
8518 -- copy we replace references to Object_Entity by Object_Entity_M.
8520 if Raise_Expression_Present then
8522 Map : constant Elist_Id := New_Elmt_List;
8523 New_V : Entity_Id := Empty;
8525 -- The unanalyzed expression will be copied and appear in
8526 -- both functions. Normally expressions do not declare new
8527 -- entities, but quantified expressions do, so we need to
8528 -- create new entities for their bound variables, to prevent
8529 -- multiple definitions in gigi.
8531 function Reset_Loop_Variable (N : Node_Id)
8532 return Traverse_Result;
8534 procedure Collect_Loop_Variables is
8535 new Traverse_Proc (Reset_Loop_Variable);
8537 ------------------------
8538 -- Reset_Loop_Variable --
8539 ------------------------
8541 function Reset_Loop_Variable (N : Node_Id)
8542 return Traverse_Result
8545 if Nkind (N) = N_Iterator_Specification then
8546 New_V := Make_Defining_Identifier
8547 (Sloc (N), Chars (Defining_Identifier (N)));
8549 Set_Defining_Identifier (N, New_V);
8553 end Reset_Loop_Variable;
8556 Append_Elmt (Object_Entity, Map);
8557 Append_Elmt (Object_Entity_M, Map);
8558 Expr_M := New_Copy_Tree (Expr, Map => Map);
8559 Collect_Loop_Variables (Expr_M);
8563 -- Build the main predicate function
8566 SId : constant Entity_Id :=
8567 Make_Defining_Identifier (Loc,
8568 Chars => New_External_Name (Chars (Typ), "Predicate"));
8569 -- The entity for the the function spec
8571 SIdB : constant Entity_Id :=
8572 Make_Defining_Identifier (Loc,
8573 Chars => New_External_Name (Chars (Typ), "Predicate"));
8574 -- The entity for the function body
8581 -- Build function declaration
8583 Set_Ekind (SId, E_Function);
8584 Set_Is_Internal (SId);
8585 Set_Is_Predicate_Function (SId);
8586 Set_Predicate_Function (Typ, SId);
8588 -- The predicate function is shared between views of a type
8590 if Is_Private_Type (Typ) and then Present (Full_View (Typ)) then
8591 Set_Predicate_Function (Full_View (Typ), SId);
8595 Make_Function_Specification (Loc,
8596 Defining_Unit_Name => SId,
8597 Parameter_Specifications => New_List (
8598 Make_Parameter_Specification (Loc,
8599 Defining_Identifier => Object_Entity,
8600 Parameter_Type => New_Occurrence_Of (Typ, Loc))),
8601 Result_Definition =>
8602 New_Occurrence_Of (Standard_Boolean, Loc));
8605 Make_Subprogram_Declaration (Loc,
8606 Specification => Spec);
8608 -- Build function body
8611 Make_Function_Specification (Loc,
8612 Defining_Unit_Name => SIdB,
8613 Parameter_Specifications => New_List (
8614 Make_Parameter_Specification (Loc,
8615 Defining_Identifier =>
8616 Make_Defining_Identifier (Loc, Object_Name),
8618 New_Occurrence_Of (Typ, Loc))),
8619 Result_Definition =>
8620 New_Occurrence_Of (Standard_Boolean, Loc));
8623 Make_Subprogram_Body (Loc,
8624 Specification => Spec,
8625 Declarations => Empty_List,
8626 Handled_Statement_Sequence =>
8627 Make_Handled_Sequence_Of_Statements (Loc,
8628 Statements => New_List (
8629 Make_Simple_Return_Statement (Loc,
8630 Expression => Expr))));
8632 -- Insert declaration before freeze node and body after
8634 Insert_Before_And_Analyze (N, FDecl);
8635 Insert_After_And_Analyze (N, FBody);
8638 -- Test for raise expressions present and if so build M version
8640 if Raise_Expression_Present then
8642 SId : constant Entity_Id :=
8643 Make_Defining_Identifier (Loc,
8644 Chars => New_External_Name (Chars (Typ), "PredicateM"));
8645 -- The entity for the the function spec
8647 SIdB : constant Entity_Id :=
8648 Make_Defining_Identifier (Loc,
8649 Chars => New_External_Name (Chars (Typ), "PredicateM"));
8650 -- The entity for the function body
8658 -- Mark any raise expressions for special expansion
8660 Process_REs (Expr_M);
8662 -- Build function declaration
8664 Set_Ekind (SId, E_Function);
8665 Set_Is_Predicate_Function_M (SId);
8666 Set_Predicate_Function_M (Typ, SId);
8668 -- The predicate function is shared between views of a type
8670 if Is_Private_Type (Typ) and then Present (Full_View (Typ)) then
8671 Set_Predicate_Function_M (Full_View (Typ), SId);
8675 Make_Function_Specification (Loc,
8676 Defining_Unit_Name => SId,
8677 Parameter_Specifications => New_List (
8678 Make_Parameter_Specification (Loc,
8679 Defining_Identifier => Object_Entity_M,
8680 Parameter_Type => New_Occurrence_Of (Typ, Loc))),
8681 Result_Definition =>
8682 New_Occurrence_Of (Standard_Boolean, Loc));
8685 Make_Subprogram_Declaration (Loc,
8686 Specification => Spec);
8688 -- Build function body
8691 Make_Function_Specification (Loc,
8692 Defining_Unit_Name => SIdB,
8693 Parameter_Specifications => New_List (
8694 Make_Parameter_Specification (Loc,
8695 Defining_Identifier =>
8696 Make_Defining_Identifier (Loc, Object_Name),
8698 New_Occurrence_Of (Typ, Loc))),
8699 Result_Definition =>
8700 New_Occurrence_Of (Standard_Boolean, Loc));
8702 -- Build the body, we declare the boolean expression before
8703 -- doing the return, because we are not really confident of
8704 -- what happens if a return appears within a return.
8707 Make_Defining_Identifier (Loc,
8708 Chars => New_Internal_Name ('B'));
8711 Make_Subprogram_Body (Loc,
8712 Specification => Spec,
8714 Declarations => New_List (
8715 Make_Object_Declaration (Loc,
8716 Defining_Identifier => BTemp,
8717 Constant_Present => True,
8718 Object_Definition =>
8719 New_Occurrence_Of (Standard_Boolean, Loc),
8720 Expression => Expr_M)),
8722 Handled_Statement_Sequence =>
8723 Make_Handled_Sequence_Of_Statements (Loc,
8724 Statements => New_List (
8725 Make_Simple_Return_Statement (Loc,
8726 Expression => New_Occurrence_Of (BTemp, Loc)))));
8728 -- Insert declaration before freeze node and body after
8730 Insert_Before_And_Analyze (N, FDecl);
8731 Insert_After_And_Analyze (N, FBody);
8735 -- See if we have a static predicate. Note that the answer may be
8736 -- yes even if we have an explicit Dynamic_Predicate present.
8743 if not Is_Scalar_Type (Typ) and then not Is_String_Type (Typ) then
8746 PS := Is_Predicate_Static (Expr, Object_Name);
8749 -- Case where we have a predicate-static aspect
8753 -- We don't set Has_Static_Predicate_Aspect, since we can have
8754 -- any of the three cases (Predicate, Dynamic_Predicate, or
8755 -- Static_Predicate) generating a predicate with an expression
8756 -- that is predicate-static. We just indicate that we have a
8757 -- predicate that can be treated as static.
8759 Set_Has_Static_Predicate (Typ);
8761 -- For discrete subtype, build the static predicate list
8763 if Is_Discrete_Type (Typ) then
8764 Build_Discrete_Static_Predicate (Typ, Expr, Object_Name);
8766 -- If we don't get a static predicate list, it means that we
8767 -- have a case where this is not possible, most typically in
8768 -- the case where we inherit a dynamic predicate. We do not
8769 -- consider this an error, we just leave the predicate as
8770 -- dynamic. But if we do succeed in building the list, then
8771 -- we mark the predicate as static.
8773 if No (Static_Discrete_Predicate (Typ)) then
8774 Set_Has_Static_Predicate (Typ, False);
8777 -- For real or string subtype, save predicate expression
8779 elsif Is_Real_Type (Typ) or else Is_String_Type (Typ) then
8780 Set_Static_Real_Or_String_Predicate (Typ, Expr);
8783 -- Case of dynamic predicate (expression is not predicate-static)
8786 -- Again, we don't set Has_Dynamic_Predicate_Aspect, since that
8787 -- is only set if we have an explicit Dynamic_Predicate aspect
8788 -- given. Here we may simply have a Predicate aspect where the
8789 -- expression happens not to be predicate-static.
8791 -- Emit an error when the predicate is categorized as static
8792 -- but its expression is not predicate-static.
8794 -- First a little fiddling to get a nice location for the
8795 -- message. If the expression is of the form (A and then B),
8796 -- then use the left operand for the Sloc. This avoids getting
8797 -- confused by a call to a higher-level predicate with a less
8798 -- convenient source location.
8801 while Nkind (EN) = N_And_Then loop
8802 EN := Left_Opnd (EN);
8805 -- Now post appropriate message
8807 if Has_Static_Predicate_Aspect (Typ) then
8808 if Is_Scalar_Type (Typ) or else Is_String_Type (Typ) then
8810 ("expression is not predicate-static (RM 3.2.4(16-22))",
8814 ("static predicate requires scalar or string type", EN);
8820 end Build_Predicate_Functions;
8822 -----------------------------------------
8823 -- Check_Aspect_At_End_Of_Declarations --
8824 -----------------------------------------
8826 procedure Check_Aspect_At_End_Of_Declarations (ASN : Node_Id) is
8827 Ent : constant Entity_Id := Entity (ASN);
8828 Ident : constant Node_Id := Identifier (ASN);
8829 A_Id : constant Aspect_Id := Get_Aspect_Id (Chars (Ident));
8831 End_Decl_Expr : constant Node_Id := Entity (Ident);
8832 -- Expression to be analyzed at end of declarations
8834 Freeze_Expr : constant Node_Id := Expression (ASN);
8835 -- Expression from call to Check_Aspect_At_Freeze_Point
8837 T : constant Entity_Id := Etype (Freeze_Expr);
8838 -- Type required for preanalyze call
8841 -- Set False if error
8843 -- On entry to this procedure, Entity (Ident) contains a copy of the
8844 -- original expression from the aspect, saved for this purpose, and
8845 -- but Expression (Ident) is a preanalyzed copy of the expression,
8846 -- preanalyzed just after the freeze point.
8848 procedure Check_Overloaded_Name;
8849 -- For aspects whose expression is simply a name, this routine checks if
8850 -- the name is overloaded or not. If so, it verifies there is an
8851 -- interpretation that matches the entity obtained at the freeze point,
8852 -- otherwise the compiler complains.
8854 ---------------------------
8855 -- Check_Overloaded_Name --
8856 ---------------------------
8858 procedure Check_Overloaded_Name is
8860 if not Is_Overloaded (End_Decl_Expr) then
8861 Err := not Is_Entity_Name (End_Decl_Expr)
8862 or else Entity (End_Decl_Expr) /= Entity (Freeze_Expr);
8868 Index : Interp_Index;
8872 Get_First_Interp (End_Decl_Expr, Index, It);
8873 while Present (It.Typ) loop
8874 if It.Nam = Entity (Freeze_Expr) then
8879 Get_Next_Interp (Index, It);
8883 end Check_Overloaded_Name;
8885 -- Start of processing for Check_Aspect_At_End_Of_Declarations
8888 -- Case of aspects Dimension, Dimension_System and Synchronization
8890 if A_Id = Aspect_Synchronization then
8893 -- Case of stream attributes, just have to compare entities. However,
8894 -- the expression is just a name (possibly overloaded), and there may
8895 -- be stream operations declared for unrelated types, so we just need
8896 -- to verify that one of these interpretations is the one available at
8897 -- at the freeze point.
8899 elsif A_Id = Aspect_Input or else
8900 A_Id = Aspect_Output or else
8901 A_Id = Aspect_Read or else
8904 Analyze (End_Decl_Expr);
8905 Check_Overloaded_Name;
8907 elsif A_Id = Aspect_Variable_Indexing or else
8908 A_Id = Aspect_Constant_Indexing or else
8909 A_Id = Aspect_Default_Iterator or else
8910 A_Id = Aspect_Iterator_Element
8912 -- Make type unfrozen before analysis, to prevent spurious errors
8913 -- about late attributes.
8915 Set_Is_Frozen (Ent, False);
8916 Analyze (End_Decl_Expr);
8917 Set_Is_Frozen (Ent, True);
8919 -- If the end of declarations comes before any other freeze
8920 -- point, the Freeze_Expr is not analyzed: no check needed.
8922 if Analyzed (Freeze_Expr) and then not In_Instance then
8923 Check_Overloaded_Name;
8931 -- Indicate that the expression comes from an aspect specification,
8932 -- which is used in subsequent analysis even if expansion is off.
8934 Set_Parent (End_Decl_Expr, ASN);
8936 -- In a generic context the aspect expressions have not been
8937 -- preanalyzed, so do it now. There are no conformance checks
8938 -- to perform in this case.
8941 Check_Aspect_At_Freeze_Point (ASN);
8944 -- The default values attributes may be defined in the private part,
8945 -- and the analysis of the expression may take place when only the
8946 -- partial view is visible. The expression must be scalar, so use
8947 -- the full view to resolve.
8949 elsif (A_Id = Aspect_Default_Value
8951 A_Id = Aspect_Default_Component_Value)
8952 and then Is_Private_Type (T)
8954 Preanalyze_Spec_Expression (End_Decl_Expr, Full_View (T));
8957 Preanalyze_Spec_Expression (End_Decl_Expr, T);
8960 Err := not Fully_Conformant_Expressions (End_Decl_Expr, Freeze_Expr);
8963 -- Output error message if error. Force error on aspect specification
8964 -- even if there is an error on the expression itself.
8968 ("!visibility of aspect for& changes after freeze point",
8971 ("info: & is frozen here, aspects evaluated at this point??",
8972 Freeze_Node (Ent), Ent);
8974 end Check_Aspect_At_End_Of_Declarations;
8976 ----------------------------------
8977 -- Check_Aspect_At_Freeze_Point --
8978 ----------------------------------
8980 procedure Check_Aspect_At_Freeze_Point (ASN : Node_Id) is
8981 Ident : constant Node_Id := Identifier (ASN);
8982 -- Identifier (use Entity field to save expression)
8984 A_Id : constant Aspect_Id := Get_Aspect_Id (Chars (Ident));
8986 T : Entity_Id := Empty;
8987 -- Type required for preanalyze call
8990 -- On entry to this procedure, Entity (Ident) contains a copy of the
8991 -- original expression from the aspect, saved for this purpose.
8993 -- On exit from this procedure Entity (Ident) is unchanged, still
8994 -- containing that copy, but Expression (Ident) is a preanalyzed copy
8995 -- of the expression, preanalyzed just after the freeze point.
8997 -- Make a copy of the expression to be preanalyzed
8999 Set_Expression (ASN, New_Copy_Tree (Entity (Ident)));
9001 -- Find type for preanalyze call
9005 -- No_Aspect should be impossible
9008 raise Program_Error;
9010 -- Aspects taking an optional boolean argument
9012 when Boolean_Aspects |
9013 Library_Unit_Aspects =>
9015 T := Standard_Boolean;
9017 -- Aspects corresponding to attribute definition clauses
9019 when Aspect_Address =>
9020 T := RTE (RE_Address);
9022 when Aspect_Attach_Handler =>
9023 T := RTE (RE_Interrupt_ID);
9025 when Aspect_Bit_Order | Aspect_Scalar_Storage_Order =>
9026 T := RTE (RE_Bit_Order);
9028 when Aspect_Convention =>
9032 T := RTE (RE_CPU_Range);
9034 -- Default_Component_Value is resolved with the component type
9036 when Aspect_Default_Component_Value =>
9037 T := Component_Type (Entity (ASN));
9039 when Aspect_Default_Storage_Pool =>
9040 T := Class_Wide_Type (RTE (RE_Root_Storage_Pool));
9042 -- Default_Value is resolved with the type entity in question
9044 when Aspect_Default_Value =>
9047 when Aspect_Dispatching_Domain =>
9048 T := RTE (RE_Dispatching_Domain);
9050 when Aspect_External_Tag =>
9051 T := Standard_String;
9053 when Aspect_External_Name =>
9054 T := Standard_String;
9056 when Aspect_Link_Name =>
9057 T := Standard_String;
9059 when Aspect_Priority | Aspect_Interrupt_Priority =>
9060 T := Standard_Integer;
9062 when Aspect_Relative_Deadline =>
9063 T := RTE (RE_Time_Span);
9065 when Aspect_Small =>
9066 T := Universal_Real;
9068 -- For a simple storage pool, we have to retrieve the type of the
9069 -- pool object associated with the aspect's corresponding attribute
9070 -- definition clause.
9072 when Aspect_Simple_Storage_Pool =>
9073 T := Etype (Expression (Aspect_Rep_Item (ASN)));
9075 when Aspect_Storage_Pool =>
9076 T := Class_Wide_Type (RTE (RE_Root_Storage_Pool));
9078 when Aspect_Alignment |
9079 Aspect_Component_Size |
9080 Aspect_Machine_Radix |
9081 Aspect_Object_Size |
9083 Aspect_Storage_Size |
9084 Aspect_Stream_Size |
9085 Aspect_Value_Size =>
9088 when Aspect_Linker_Section =>
9089 T := Standard_String;
9091 when Aspect_Synchronization =>
9094 -- Special case, the expression of these aspects is just an entity
9095 -- that does not need any resolution, so just analyze.
9104 Analyze (Expression (ASN));
9107 -- Same for Iterator aspects, where the expression is a function
9108 -- name. Legality rules are checked separately.
9110 when Aspect_Constant_Indexing |
9111 Aspect_Default_Iterator |
9112 Aspect_Iterator_Element |
9113 Aspect_Variable_Indexing =>
9114 Analyze (Expression (ASN));
9117 -- Ditto for Iterable, legality checks in Validate_Iterable_Aspect.
9119 when Aspect_Iterable =>
9123 Cursor : constant Entity_Id := Get_Cursor_Type (ASN, T);
9128 if Cursor = Any_Type then
9132 Assoc := First (Component_Associations (Expression (ASN)));
9133 while Present (Assoc) loop
9134 Expr := Expression (Assoc);
9137 if not Error_Posted (Expr) then
9138 Resolve_Iterable_Operation
9139 (Expr, Cursor, T, Chars (First (Choices (Assoc))));
9148 -- Invariant/Predicate take boolean expressions
9150 when Aspect_Dynamic_Predicate |
9153 Aspect_Static_Predicate |
9154 Aspect_Type_Invariant =>
9155 T := Standard_Boolean;
9157 -- Here is the list of aspects that don't require delay analysis
9159 when Aspect_Abstract_State |
9161 Aspect_Contract_Cases |
9162 Aspect_Default_Initial_Condition |
9165 Aspect_Dimension_System |
9166 Aspect_Extensions_Visible |
9169 Aspect_Implicit_Dereference |
9170 Aspect_Initial_Condition |
9171 Aspect_Initializes |
9172 Aspect_Obsolescent |
9175 Aspect_Postcondition |
9177 Aspect_Precondition |
9178 Aspect_Refined_Depends |
9179 Aspect_Refined_Global |
9180 Aspect_Refined_Post |
9181 Aspect_Refined_State |
9184 Aspect_Unimplemented =>
9185 raise Program_Error;
9189 -- Do the preanalyze call
9191 Preanalyze_Spec_Expression (Expression (ASN), T);
9192 end Check_Aspect_At_Freeze_Point;
9194 -----------------------------------
9195 -- Check_Constant_Address_Clause --
9196 -----------------------------------
9198 procedure Check_Constant_Address_Clause
9202 procedure Check_At_Constant_Address (Nod : Node_Id);
9203 -- Checks that the given node N represents a name whose 'Address is
9204 -- constant (in the same sense as OK_Constant_Address_Clause, i.e. the
9205 -- address value is the same at the point of declaration of U_Ent and at
9206 -- the time of elaboration of the address clause.
9208 procedure Check_Expr_Constants (Nod : Node_Id);
9209 -- Checks that Nod meets the requirements for a constant address clause
9210 -- in the sense of the enclosing procedure.
9212 procedure Check_List_Constants (Lst : List_Id);
9213 -- Check that all elements of list Lst meet the requirements for a
9214 -- constant address clause in the sense of the enclosing procedure.
9216 -------------------------------
9217 -- Check_At_Constant_Address --
9218 -------------------------------
9220 procedure Check_At_Constant_Address (Nod : Node_Id) is
9222 if Is_Entity_Name (Nod) then
9223 if Present (Address_Clause (Entity ((Nod)))) then
9225 ("invalid address clause for initialized object &!",
9228 ("address for& cannot" &
9229 " depend on another address clause! (RM 13.1(22))!",
9232 elsif In_Same_Source_Unit (Entity (Nod), U_Ent)
9233 and then Sloc (U_Ent) < Sloc (Entity (Nod))
9236 ("invalid address clause for initialized object &!",
9238 Error_Msg_Node_2 := U_Ent;
9240 ("\& must be defined before & (RM 13.1(22))!",
9244 elsif Nkind (Nod) = N_Selected_Component then
9246 T : constant Entity_Id := Etype (Prefix (Nod));
9249 if (Is_Record_Type (T)
9250 and then Has_Discriminants (T))
9253 and then Is_Record_Type (Designated_Type (T))
9254 and then Has_Discriminants (Designated_Type (T)))
9257 ("invalid address clause for initialized object &!",
9260 ("\address cannot depend on component" &
9261 " of discriminated record (RM 13.1(22))!",
9264 Check_At_Constant_Address (Prefix (Nod));
9268 elsif Nkind (Nod) = N_Indexed_Component then
9269 Check_At_Constant_Address (Prefix (Nod));
9270 Check_List_Constants (Expressions (Nod));
9273 Check_Expr_Constants (Nod);
9275 end Check_At_Constant_Address;
9277 --------------------------
9278 -- Check_Expr_Constants --
9279 --------------------------
9281 procedure Check_Expr_Constants (Nod : Node_Id) is
9282 Loc_U_Ent : constant Source_Ptr := Sloc (U_Ent);
9283 Ent : Entity_Id := Empty;
9286 if Nkind (Nod) in N_Has_Etype
9287 and then Etype (Nod) = Any_Type
9293 when N_Empty | N_Error =>
9296 when N_Identifier | N_Expanded_Name =>
9297 Ent := Entity (Nod);
9299 -- We need to look at the original node if it is different
9300 -- from the node, since we may have rewritten things and
9301 -- substituted an identifier representing the rewrite.
9303 if Original_Node (Nod) /= Nod then
9304 Check_Expr_Constants (Original_Node (Nod));
9306 -- If the node is an object declaration without initial
9307 -- value, some code has been expanded, and the expression
9308 -- is not constant, even if the constituents might be
9309 -- acceptable, as in A'Address + offset.
9311 if Ekind (Ent) = E_Variable
9313 Nkind (Declaration_Node (Ent)) = N_Object_Declaration
9315 No (Expression (Declaration_Node (Ent)))
9318 ("invalid address clause for initialized object &!",
9321 -- If entity is constant, it may be the result of expanding
9322 -- a check. We must verify that its declaration appears
9323 -- before the object in question, else we also reject the
9326 elsif Ekind (Ent) = E_Constant
9327 and then In_Same_Source_Unit (Ent, U_Ent)
9328 and then Sloc (Ent) > Loc_U_Ent
9331 ("invalid address clause for initialized object &!",
9338 -- Otherwise look at the identifier and see if it is OK
9340 if Ekind_In (Ent, E_Named_Integer, E_Named_Real)
9341 or else Is_Type (Ent)
9345 elsif Ekind_In (Ent, E_Constant, E_In_Parameter) then
9347 -- This is the case where we must have Ent defined before
9348 -- U_Ent. Clearly if they are in different units this
9349 -- requirement is met since the unit containing Ent is
9350 -- already processed.
9352 if not In_Same_Source_Unit (Ent, U_Ent) then
9355 -- Otherwise location of Ent must be before the location
9356 -- of U_Ent, that's what prior defined means.
9358 elsif Sloc (Ent) < Loc_U_Ent then
9363 ("invalid address clause for initialized object &!",
9365 Error_Msg_Node_2 := U_Ent;
9367 ("\& must be defined before & (RM 13.1(22))!",
9371 elsif Nkind (Original_Node (Nod)) = N_Function_Call then
9372 Check_Expr_Constants (Original_Node (Nod));
9376 ("invalid address clause for initialized object &!",
9379 if Comes_From_Source (Ent) then
9381 ("\reference to variable& not allowed"
9382 & " (RM 13.1(22))!", Nod, Ent);
9385 ("non-static expression not allowed"
9386 & " (RM 13.1(22))!", Nod);
9390 when N_Integer_Literal =>
9392 -- If this is a rewritten unchecked conversion, in a system
9393 -- where Address is an integer type, always use the base type
9394 -- for a literal value. This is user-friendly and prevents
9395 -- order-of-elaboration issues with instances of unchecked
9398 if Nkind (Original_Node (Nod)) = N_Function_Call then
9399 Set_Etype (Nod, Base_Type (Etype (Nod)));
9402 when N_Real_Literal |
9404 N_Character_Literal =>
9408 Check_Expr_Constants (Low_Bound (Nod));
9409 Check_Expr_Constants (High_Bound (Nod));
9411 when N_Explicit_Dereference =>
9412 Check_Expr_Constants (Prefix (Nod));
9414 when N_Indexed_Component =>
9415 Check_Expr_Constants (Prefix (Nod));
9416 Check_List_Constants (Expressions (Nod));
9419 Check_Expr_Constants (Prefix (Nod));
9420 Check_Expr_Constants (Discrete_Range (Nod));
9422 when N_Selected_Component =>
9423 Check_Expr_Constants (Prefix (Nod));
9425 when N_Attribute_Reference =>
9426 if Nam_In (Attribute_Name (Nod), Name_Address,
9428 Name_Unchecked_Access,
9429 Name_Unrestricted_Access)
9431 Check_At_Constant_Address (Prefix (Nod));
9434 Check_Expr_Constants (Prefix (Nod));
9435 Check_List_Constants (Expressions (Nod));
9439 Check_List_Constants (Component_Associations (Nod));
9440 Check_List_Constants (Expressions (Nod));
9442 when N_Component_Association =>
9443 Check_Expr_Constants (Expression (Nod));
9445 when N_Extension_Aggregate =>
9446 Check_Expr_Constants (Ancestor_Part (Nod));
9447 Check_List_Constants (Component_Associations (Nod));
9448 Check_List_Constants (Expressions (Nod));
9453 when N_Binary_Op | N_Short_Circuit | N_Membership_Test =>
9454 Check_Expr_Constants (Left_Opnd (Nod));
9455 Check_Expr_Constants (Right_Opnd (Nod));
9458 Check_Expr_Constants (Right_Opnd (Nod));
9460 when N_Type_Conversion |
9461 N_Qualified_Expression |
9463 N_Unchecked_Type_Conversion =>
9464 Check_Expr_Constants (Expression (Nod));
9466 when N_Function_Call =>
9467 if not Is_Pure (Entity (Name (Nod))) then
9469 ("invalid address clause for initialized object &!",
9473 ("\function & is not pure (RM 13.1(22))!",
9474 Nod, Entity (Name (Nod)));
9477 Check_List_Constants (Parameter_Associations (Nod));
9480 when N_Parameter_Association =>
9481 Check_Expr_Constants (Explicit_Actual_Parameter (Nod));
9485 ("invalid address clause for initialized object &!",
9488 ("\must be constant defined before& (RM 13.1(22))!",
9491 end Check_Expr_Constants;
9493 --------------------------
9494 -- Check_List_Constants --
9495 --------------------------
9497 procedure Check_List_Constants (Lst : List_Id) is
9501 if Present (Lst) then
9502 Nod1 := First (Lst);
9503 while Present (Nod1) loop
9504 Check_Expr_Constants (Nod1);
9508 end Check_List_Constants;
9510 -- Start of processing for Check_Constant_Address_Clause
9513 -- If rep_clauses are to be ignored, no need for legality checks. In
9514 -- particular, no need to pester user about rep clauses that violate the
9515 -- rule on constant addresses, given that these clauses will be removed
9516 -- by Freeze before they reach the back end. Similarly in CodePeer mode,
9517 -- we want to relax these checks.
9519 if not Ignore_Rep_Clauses and not CodePeer_Mode then
9520 Check_Expr_Constants (Expr);
9522 end Check_Constant_Address_Clause;
9524 ---------------------------
9525 -- Check_Pool_Size_Clash --
9526 ---------------------------
9528 procedure Check_Pool_Size_Clash (Ent : Entity_Id; SP, SS : Node_Id) is
9532 -- We need to find out which one came first. Note that in the case of
9533 -- aspects mixed with pragmas there are cases where the processing order
9534 -- is reversed, which is why we do the check here.
9536 if Sloc (SP) < Sloc (SS) then
9537 Error_Msg_Sloc := Sloc (SP);
9539 Error_Msg_NE ("Storage_Pool previously given for&#", Post, Ent);
9542 Error_Msg_Sloc := Sloc (SS);
9544 Error_Msg_NE ("Storage_Size previously given for&#", Post, Ent);
9548 ("\cannot have Storage_Size and Storage_Pool (RM 13.11(3))", Post);
9549 end Check_Pool_Size_Clash;
9551 ----------------------------------------
9552 -- Check_Record_Representation_Clause --
9553 ----------------------------------------
9555 procedure Check_Record_Representation_Clause (N : Node_Id) is
9556 Loc : constant Source_Ptr := Sloc (N);
9557 Ident : constant Node_Id := Identifier (N);
9558 Rectype : Entity_Id;
9563 Hbit : Uint := Uint_0;
9567 Max_Bit_So_Far : Uint;
9568 -- Records the maximum bit position so far. If all field positions
9569 -- are monotonically increasing, then we can skip the circuit for
9570 -- checking for overlap, since no overlap is possible.
9572 Tagged_Parent : Entity_Id := Empty;
9573 -- This is set in the case of a derived tagged type for which we have
9574 -- Is_Fully_Repped_Tagged_Type True (indicating that all components are
9575 -- positioned by record representation clauses). In this case we must
9576 -- check for overlap between components of this tagged type, and the
9577 -- components of its parent. Tagged_Parent will point to this parent
9578 -- type. For all other cases Tagged_Parent is left set to Empty.
9580 Parent_Last_Bit : Uint;
9581 -- Relevant only if Tagged_Parent is set, Parent_Last_Bit indicates the
9582 -- last bit position for any field in the parent type. We only need to
9583 -- check overlap for fields starting below this point.
9585 Overlap_Check_Required : Boolean;
9586 -- Used to keep track of whether or not an overlap check is required
9588 Overlap_Detected : Boolean := False;
9589 -- Set True if an overlap is detected
9591 Ccount : Natural := 0;
9592 -- Number of component clauses in record rep clause
9594 procedure Check_Component_Overlap (C1_Ent, C2_Ent : Entity_Id);
9595 -- Given two entities for record components or discriminants, checks
9596 -- if they have overlapping component clauses and issues errors if so.
9598 procedure Find_Component;
9599 -- Finds component entity corresponding to current component clause (in
9600 -- CC), and sets Comp to the entity, and Fbit/Lbit to the zero origin
9601 -- start/stop bits for the field. If there is no matching component or
9602 -- if the matching component does not have a component clause, then
9603 -- that's an error and Comp is set to Empty, but no error message is
9604 -- issued, since the message was already given. Comp is also set to
9605 -- Empty if the current "component clause" is in fact a pragma.
9607 -----------------------------
9608 -- Check_Component_Overlap --
9609 -----------------------------
9611 procedure Check_Component_Overlap (C1_Ent, C2_Ent : Entity_Id) is
9612 CC1 : constant Node_Id := Component_Clause (C1_Ent);
9613 CC2 : constant Node_Id := Component_Clause (C2_Ent);
9616 if Present (CC1) and then Present (CC2) then
9618 -- Exclude odd case where we have two tag components in the same
9619 -- record, both at location zero. This seems a bit strange, but
9620 -- it seems to happen in some circumstances, perhaps on an error.
9622 if Nam_In (Chars (C1_Ent), Name_uTag, Name_uTag) then
9626 -- Here we check if the two fields overlap
9629 S1 : constant Uint := Component_Bit_Offset (C1_Ent);
9630 S2 : constant Uint := Component_Bit_Offset (C2_Ent);
9631 E1 : constant Uint := S1 + Esize (C1_Ent);
9632 E2 : constant Uint := S2 + Esize (C2_Ent);
9635 if E2 <= S1 or else E1 <= S2 then
9638 Error_Msg_Node_2 := Component_Name (CC2);
9639 Error_Msg_Sloc := Sloc (Error_Msg_Node_2);
9640 Error_Msg_Node_1 := Component_Name (CC1);
9642 ("component& overlaps & #", Component_Name (CC1));
9643 Overlap_Detected := True;
9647 end Check_Component_Overlap;
9649 --------------------
9650 -- Find_Component --
9651 --------------------
9653 procedure Find_Component is
9655 procedure Search_Component (R : Entity_Id);
9656 -- Search components of R for a match. If found, Comp is set
9658 ----------------------
9659 -- Search_Component --
9660 ----------------------
9662 procedure Search_Component (R : Entity_Id) is
9664 Comp := First_Component_Or_Discriminant (R);
9665 while Present (Comp) loop
9667 -- Ignore error of attribute name for component name (we
9668 -- already gave an error message for this, so no need to
9671 if Nkind (Component_Name (CC)) = N_Attribute_Reference then
9674 exit when Chars (Comp) = Chars (Component_Name (CC));
9677 Next_Component_Or_Discriminant (Comp);
9679 end Search_Component;
9681 -- Start of processing for Find_Component
9684 -- Return with Comp set to Empty if we have a pragma
9686 if Nkind (CC) = N_Pragma then
9691 -- Search current record for matching component
9693 Search_Component (Rectype);
9695 -- If not found, maybe component of base type discriminant that is
9696 -- absent from statically constrained first subtype.
9699 Search_Component (Base_Type (Rectype));
9702 -- If no component, or the component does not reference the component
9703 -- clause in question, then there was some previous error for which
9704 -- we already gave a message, so just return with Comp Empty.
9706 if No (Comp) or else Component_Clause (Comp) /= CC then
9707 Check_Error_Detected;
9710 -- Normal case where we have a component clause
9713 Fbit := Component_Bit_Offset (Comp);
9714 Lbit := Fbit + Esize (Comp) - 1;
9718 -- Start of processing for Check_Record_Representation_Clause
9722 Rectype := Entity (Ident);
9724 if Rectype = Any_Type then
9727 Rectype := Underlying_Type (Rectype);
9730 -- See if we have a fully repped derived tagged type
9733 PS : constant Entity_Id := Parent_Subtype (Rectype);
9736 if Present (PS) and then Is_Fully_Repped_Tagged_Type (PS) then
9737 Tagged_Parent := PS;
9739 -- Find maximum bit of any component of the parent type
9741 Parent_Last_Bit := UI_From_Int (System_Address_Size - 1);
9742 Pcomp := First_Entity (Tagged_Parent);
9743 while Present (Pcomp) loop
9744 if Ekind_In (Pcomp, E_Discriminant, E_Component) then
9745 if Component_Bit_Offset (Pcomp) /= No_Uint
9746 and then Known_Static_Esize (Pcomp)
9751 Component_Bit_Offset (Pcomp) + Esize (Pcomp) - 1);
9754 Next_Entity (Pcomp);
9760 -- All done if no component clauses
9762 CC := First (Component_Clauses (N));
9768 -- If a tag is present, then create a component clause that places it
9769 -- at the start of the record (otherwise gigi may place it after other
9770 -- fields that have rep clauses).
9772 Fent := First_Entity (Rectype);
9774 if Nkind (Fent) = N_Defining_Identifier
9775 and then Chars (Fent) = Name_uTag
9777 Set_Component_Bit_Offset (Fent, Uint_0);
9778 Set_Normalized_Position (Fent, Uint_0);
9779 Set_Normalized_First_Bit (Fent, Uint_0);
9780 Set_Normalized_Position_Max (Fent, Uint_0);
9781 Init_Esize (Fent, System_Address_Size);
9783 Set_Component_Clause (Fent,
9784 Make_Component_Clause (Loc,
9785 Component_Name => Make_Identifier (Loc, Name_uTag),
9787 Position => Make_Integer_Literal (Loc, Uint_0),
9788 First_Bit => Make_Integer_Literal (Loc, Uint_0),
9790 Make_Integer_Literal (Loc,
9791 UI_From_Int (System_Address_Size))));
9793 Ccount := Ccount + 1;
9796 Max_Bit_So_Far := Uint_Minus_1;
9797 Overlap_Check_Required := False;
9799 -- Process the component clauses
9801 while Present (CC) loop
9804 if Present (Comp) then
9805 Ccount := Ccount + 1;
9807 -- We need a full overlap check if record positions non-monotonic
9809 if Fbit <= Max_Bit_So_Far then
9810 Overlap_Check_Required := True;
9813 Max_Bit_So_Far := Lbit;
9815 -- Check bit position out of range of specified size
9817 if Has_Size_Clause (Rectype)
9818 and then RM_Size (Rectype) <= Lbit
9821 ("bit number out of range of specified size",
9824 -- Check for overlap with tag component
9827 if Is_Tagged_Type (Rectype)
9828 and then Fbit < System_Address_Size
9831 ("component overlaps tag field of&",
9832 Component_Name (CC), Rectype);
9833 Overlap_Detected := True;
9841 -- Check parent overlap if component might overlap parent field
9843 if Present (Tagged_Parent) and then Fbit <= Parent_Last_Bit then
9844 Pcomp := First_Component_Or_Discriminant (Tagged_Parent);
9845 while Present (Pcomp) loop
9846 if not Is_Tag (Pcomp)
9847 and then Chars (Pcomp) /= Name_uParent
9849 Check_Component_Overlap (Comp, Pcomp);
9852 Next_Component_Or_Discriminant (Pcomp);
9860 -- Now that we have processed all the component clauses, check for
9861 -- overlap. We have to leave this till last, since the components can
9862 -- appear in any arbitrary order in the representation clause.
9864 -- We do not need this check if all specified ranges were monotonic,
9865 -- as recorded by Overlap_Check_Required being False at this stage.
9867 -- This first section checks if there are any overlapping entries at
9868 -- all. It does this by sorting all entries and then seeing if there are
9869 -- any overlaps. If there are none, then that is decisive, but if there
9870 -- are overlaps, they may still be OK (they may result from fields in
9871 -- different variants).
9873 if Overlap_Check_Required then
9874 Overlap_Check1 : declare
9876 OC_Fbit : array (0 .. Ccount) of Uint;
9877 -- First-bit values for component clauses, the value is the offset
9878 -- of the first bit of the field from start of record. The zero
9879 -- entry is for use in sorting.
9881 OC_Lbit : array (0 .. Ccount) of Uint;
9882 -- Last-bit values for component clauses, the value is the offset
9883 -- of the last bit of the field from start of record. The zero
9884 -- entry is for use in sorting.
9886 OC_Count : Natural := 0;
9887 -- Count of entries in OC_Fbit and OC_Lbit
9889 function OC_Lt (Op1, Op2 : Natural) return Boolean;
9890 -- Compare routine for Sort
9892 procedure OC_Move (From : Natural; To : Natural);
9893 -- Move routine for Sort
9895 package Sorting is new GNAT.Heap_Sort_G (OC_Move, OC_Lt);
9901 function OC_Lt (Op1, Op2 : Natural) return Boolean is
9903 return OC_Fbit (Op1) < OC_Fbit (Op2);
9910 procedure OC_Move (From : Natural; To : Natural) is
9912 OC_Fbit (To) := OC_Fbit (From);
9913 OC_Lbit (To) := OC_Lbit (From);
9916 -- Start of processing for Overlap_Check
9919 CC := First (Component_Clauses (N));
9920 while Present (CC) loop
9922 -- Exclude component clause already marked in error
9924 if not Error_Posted (CC) then
9927 if Present (Comp) then
9928 OC_Count := OC_Count + 1;
9929 OC_Fbit (OC_Count) := Fbit;
9930 OC_Lbit (OC_Count) := Lbit;
9937 Sorting.Sort (OC_Count);
9939 Overlap_Check_Required := False;
9940 for J in 1 .. OC_Count - 1 loop
9941 if OC_Lbit (J) >= OC_Fbit (J + 1) then
9942 Overlap_Check_Required := True;
9949 -- If Overlap_Check_Required is still True, then we have to do the full
9950 -- scale overlap check, since we have at least two fields that do
9951 -- overlap, and we need to know if that is OK since they are in
9952 -- different variant, or whether we have a definite problem.
9954 if Overlap_Check_Required then
9955 Overlap_Check2 : declare
9956 C1_Ent, C2_Ent : Entity_Id;
9957 -- Entities of components being checked for overlap
9960 -- Component_List node whose Component_Items are being checked
9963 -- Component declaration for component being checked
9966 C1_Ent := First_Entity (Base_Type (Rectype));
9968 -- Loop through all components in record. For each component check
9969 -- for overlap with any of the preceding elements on the component
9970 -- list containing the component and also, if the component is in
9971 -- a variant, check against components outside the case structure.
9972 -- This latter test is repeated recursively up the variant tree.
9974 Main_Component_Loop : while Present (C1_Ent) loop
9975 if not Ekind_In (C1_Ent, E_Component, E_Discriminant) then
9976 goto Continue_Main_Component_Loop;
9979 -- Skip overlap check if entity has no declaration node. This
9980 -- happens with discriminants in constrained derived types.
9981 -- Possibly we are missing some checks as a result, but that
9982 -- does not seem terribly serious.
9984 if No (Declaration_Node (C1_Ent)) then
9985 goto Continue_Main_Component_Loop;
9988 Clist := Parent (List_Containing (Declaration_Node (C1_Ent)));
9990 -- Loop through component lists that need checking. Check the
9991 -- current component list and all lists in variants above us.
9993 Component_List_Loop : loop
9995 -- If derived type definition, go to full declaration
9996 -- If at outer level, check discriminants if there are any.
9998 if Nkind (Clist) = N_Derived_Type_Definition then
9999 Clist := Parent (Clist);
10002 -- Outer level of record definition, check discriminants
10004 if Nkind_In (Clist, N_Full_Type_Declaration,
10005 N_Private_Type_Declaration)
10007 if Has_Discriminants (Defining_Identifier (Clist)) then
10009 First_Discriminant (Defining_Identifier (Clist));
10010 while Present (C2_Ent) loop
10011 exit when C1_Ent = C2_Ent;
10012 Check_Component_Overlap (C1_Ent, C2_Ent);
10013 Next_Discriminant (C2_Ent);
10017 -- Record extension case
10019 elsif Nkind (Clist) = N_Derived_Type_Definition then
10022 -- Otherwise check one component list
10025 Citem := First (Component_Items (Clist));
10026 while Present (Citem) loop
10027 if Nkind (Citem) = N_Component_Declaration then
10028 C2_Ent := Defining_Identifier (Citem);
10029 exit when C1_Ent = C2_Ent;
10030 Check_Component_Overlap (C1_Ent, C2_Ent);
10037 -- Check for variants above us (the parent of the Clist can
10038 -- be a variant, in which case its parent is a variant part,
10039 -- and the parent of the variant part is a component list
10040 -- whose components must all be checked against the current
10041 -- component for overlap).
10043 if Nkind (Parent (Clist)) = N_Variant then
10044 Clist := Parent (Parent (Parent (Clist)));
10046 -- Check for possible discriminant part in record, this
10047 -- is treated essentially as another level in the
10048 -- recursion. For this case the parent of the component
10049 -- list is the record definition, and its parent is the
10050 -- full type declaration containing the discriminant
10053 elsif Nkind (Parent (Clist)) = N_Record_Definition then
10054 Clist := Parent (Parent ((Clist)));
10056 -- If neither of these two cases, we are at the top of
10060 exit Component_List_Loop;
10062 end loop Component_List_Loop;
10064 <<Continue_Main_Component_Loop>>
10065 Next_Entity (C1_Ent);
10067 end loop Main_Component_Loop;
10068 end Overlap_Check2;
10071 -- The following circuit deals with warning on record holes (gaps). We
10072 -- skip this check if overlap was detected, since it makes sense for the
10073 -- programmer to fix this illegality before worrying about warnings.
10075 if not Overlap_Detected and Warn_On_Record_Holes then
10076 Record_Hole_Check : declare
10077 Decl : constant Node_Id := Declaration_Node (Base_Type (Rectype));
10078 -- Full declaration of record type
10080 procedure Check_Component_List
10084 -- Check component list CL for holes. The starting bit should be
10085 -- Sbit. which is zero for the main record component list and set
10086 -- appropriately for recursive calls for variants. DS is set to
10087 -- a list of discriminant specifications to be included in the
10088 -- consideration of components. It is No_List if none to consider.
10090 --------------------------
10091 -- Check_Component_List --
10092 --------------------------
10094 procedure Check_Component_List
10102 Compl := Integer (List_Length (Component_Items (CL)));
10104 if DS /= No_List then
10105 Compl := Compl + Integer (List_Length (DS));
10109 Comps : array (Natural range 0 .. Compl) of Entity_Id;
10110 -- Gather components (zero entry is for sort routine)
10112 Ncomps : Natural := 0;
10113 -- Number of entries stored in Comps (starting at Comps (1))
10116 -- One component item or discriminant specification
10119 -- Starting bit for next component
10122 -- Component entity
10127 function Lt (Op1, Op2 : Natural) return Boolean;
10128 -- Compare routine for Sort
10130 procedure Move (From : Natural; To : Natural);
10131 -- Move routine for Sort
10133 package Sorting is new GNAT.Heap_Sort_G (Move, Lt);
10139 function Lt (Op1, Op2 : Natural) return Boolean is
10141 return Component_Bit_Offset (Comps (Op1))
10143 Component_Bit_Offset (Comps (Op2));
10150 procedure Move (From : Natural; To : Natural) is
10152 Comps (To) := Comps (From);
10156 -- Gather discriminants into Comp
10158 if DS /= No_List then
10159 Citem := First (DS);
10160 while Present (Citem) loop
10161 if Nkind (Citem) = N_Discriminant_Specification then
10163 Ent : constant Entity_Id :=
10164 Defining_Identifier (Citem);
10166 if Ekind (Ent) = E_Discriminant then
10167 Ncomps := Ncomps + 1;
10168 Comps (Ncomps) := Ent;
10177 -- Gather component entities into Comp
10179 Citem := First (Component_Items (CL));
10180 while Present (Citem) loop
10181 if Nkind (Citem) = N_Component_Declaration then
10182 Ncomps := Ncomps + 1;
10183 Comps (Ncomps) := Defining_Identifier (Citem);
10189 -- Now sort the component entities based on the first bit.
10190 -- Note we already know there are no overlapping components.
10192 Sorting.Sort (Ncomps);
10194 -- Loop through entries checking for holes
10197 for J in 1 .. Ncomps loop
10199 Error_Msg_Uint_1 := Component_Bit_Offset (CEnt) - Nbit;
10201 if Error_Msg_Uint_1 > 0 then
10203 ("?H?^-bit gap before component&",
10204 Component_Name (Component_Clause (CEnt)), CEnt);
10207 Nbit := Component_Bit_Offset (CEnt) + Esize (CEnt);
10210 -- Process variant parts recursively if present
10212 if Present (Variant_Part (CL)) then
10213 Variant := First (Variants (Variant_Part (CL)));
10214 while Present (Variant) loop
10215 Check_Component_List
10216 (Component_List (Variant), Nbit, No_List);
10221 end Check_Component_List;
10223 -- Start of processing for Record_Hole_Check
10230 if Is_Tagged_Type (Rectype) then
10231 Sbit := UI_From_Int (System_Address_Size);
10236 if Nkind (Decl) = N_Full_Type_Declaration
10237 and then Nkind (Type_Definition (Decl)) = N_Record_Definition
10239 Check_Component_List
10240 (Component_List (Type_Definition (Decl)),
10242 Discriminant_Specifications (Decl));
10245 end Record_Hole_Check;
10248 -- For records that have component clauses for all components, and whose
10249 -- size is less than or equal to 32, we need to know the size in the
10250 -- front end to activate possible packed array processing where the
10251 -- component type is a record.
10253 -- At this stage Hbit + 1 represents the first unused bit from all the
10254 -- component clauses processed, so if the component clauses are
10255 -- complete, then this is the length of the record.
10257 -- For records longer than System.Storage_Unit, and for those where not
10258 -- all components have component clauses, the back end determines the
10259 -- length (it may for example be appropriate to round up the size
10260 -- to some convenient boundary, based on alignment considerations, etc).
10262 if Unknown_RM_Size (Rectype) and then Hbit + 1 <= 32 then
10264 -- Nothing to do if at least one component has no component clause
10266 Comp := First_Component_Or_Discriminant (Rectype);
10267 while Present (Comp) loop
10268 exit when No (Component_Clause (Comp));
10269 Next_Component_Or_Discriminant (Comp);
10272 -- If we fall out of loop, all components have component clauses
10273 -- and so we can set the size to the maximum value.
10276 Set_RM_Size (Rectype, Hbit + 1);
10279 end Check_Record_Representation_Clause;
10285 procedure Check_Size
10289 Biased : out Boolean)
10291 UT : constant Entity_Id := Underlying_Type (T);
10297 -- Reject patently improper size values.
10299 if Is_Elementary_Type (T)
10300 and then Siz > UI_From_Int (Int'Last)
10302 Error_Msg_N ("Size value too large for elementary type", N);
10304 if Nkind (Original_Node (N)) = N_Op_Expon then
10306 ("\maybe '* was meant, rather than '*'*", Original_Node (N));
10310 -- Dismiss generic types
10312 if Is_Generic_Type (T)
10314 Is_Generic_Type (UT)
10316 Is_Generic_Type (Root_Type (UT))
10320 -- Guard against previous errors
10322 elsif No (UT) or else UT = Any_Type then
10323 Check_Error_Detected;
10326 -- Check case of bit packed array
10328 elsif Is_Array_Type (UT)
10329 and then Known_Static_Component_Size (UT)
10330 and then Is_Bit_Packed_Array (UT)
10338 Asiz := Component_Size (UT);
10339 Indx := First_Index (UT);
10341 Ityp := Etype (Indx);
10343 -- If non-static bound, then we are not in the business of
10344 -- trying to check the length, and indeed an error will be
10345 -- issued elsewhere, since sizes of non-static array types
10346 -- cannot be set implicitly or explicitly.
10348 if not Is_OK_Static_Subtype (Ityp) then
10352 -- Otherwise accumulate next dimension
10354 Asiz := Asiz * (Expr_Value (Type_High_Bound (Ityp)) -
10355 Expr_Value (Type_Low_Bound (Ityp)) +
10359 exit when No (Indx);
10362 if Asiz <= Siz then
10366 Error_Msg_Uint_1 := Asiz;
10368 ("size for& too small, minimum allowed is ^", N, T);
10369 Set_Esize (T, Asiz);
10370 Set_RM_Size (T, Asiz);
10374 -- All other composite types are ignored
10376 elsif Is_Composite_Type (UT) then
10379 -- For fixed-point types, don't check minimum if type is not frozen,
10380 -- since we don't know all the characteristics of the type that can
10381 -- affect the size (e.g. a specified small) till freeze time.
10383 elsif Is_Fixed_Point_Type (UT)
10384 and then not Is_Frozen (UT)
10388 -- Cases for which a minimum check is required
10391 -- Ignore if specified size is correct for the type
10393 if Known_Esize (UT) and then Siz = Esize (UT) then
10397 -- Otherwise get minimum size
10399 M := UI_From_Int (Minimum_Size (UT));
10403 -- Size is less than minimum size, but one possibility remains
10404 -- that we can manage with the new size if we bias the type.
10406 M := UI_From_Int (Minimum_Size (UT, Biased => True));
10409 Error_Msg_Uint_1 := M;
10411 ("size for& too small, minimum allowed is ^", N, T);
10413 Set_RM_Size (T, M);
10421 --------------------------
10422 -- Freeze_Entity_Checks --
10423 --------------------------
10425 procedure Freeze_Entity_Checks (N : Node_Id) is
10426 procedure Hide_Non_Overridden_Subprograms (Typ : Entity_Id);
10427 -- Inspect the primitive operations of type Typ and hide all pairs of
10428 -- implicitly declared non-overridden non-fully conformant homographs
10429 -- (Ada RM 8.3 12.3/2).
10431 -------------------------------------
10432 -- Hide_Non_Overridden_Subprograms --
10433 -------------------------------------
10435 procedure Hide_Non_Overridden_Subprograms (Typ : Entity_Id) is
10436 procedure Hide_Matching_Homographs
10437 (Subp_Id : Entity_Id;
10438 Start_Elmt : Elmt_Id);
10439 -- Inspect a list of primitive operations starting with Start_Elmt
10440 -- and find matching implicitly declared non-overridden non-fully
10441 -- conformant homographs of Subp_Id. If found, all matches along
10442 -- with Subp_Id are hidden from all visibility.
10444 function Is_Non_Overridden_Or_Null_Procedure
10445 (Subp_Id : Entity_Id) return Boolean;
10446 -- Determine whether subprogram Subp_Id is implicitly declared non-
10447 -- overridden subprogram or an implicitly declared null procedure.
10449 ------------------------------
10450 -- Hide_Matching_Homographs --
10451 ------------------------------
10453 procedure Hide_Matching_Homographs
10454 (Subp_Id : Entity_Id;
10455 Start_Elmt : Elmt_Id)
10458 Prim_Elmt : Elmt_Id;
10461 Prim_Elmt := Start_Elmt;
10462 while Present (Prim_Elmt) loop
10463 Prim := Node (Prim_Elmt);
10465 -- The current primitive is implicitly declared non-overridden
10466 -- non-fully conformant homograph of Subp_Id. Both subprograms
10467 -- must be hidden from visibility.
10469 if Chars (Prim) = Chars (Subp_Id)
10470 and then Is_Non_Overridden_Or_Null_Procedure (Prim)
10471 and then not Fully_Conformant (Prim, Subp_Id)
10473 Set_Is_Hidden_Non_Overridden_Subpgm (Prim);
10474 Set_Is_Immediately_Visible (Prim, False);
10475 Set_Is_Potentially_Use_Visible (Prim, False);
10477 Set_Is_Hidden_Non_Overridden_Subpgm (Subp_Id);
10478 Set_Is_Immediately_Visible (Subp_Id, False);
10479 Set_Is_Potentially_Use_Visible (Subp_Id, False);
10482 Next_Elmt (Prim_Elmt);
10484 end Hide_Matching_Homographs;
10486 -----------------------------------------
10487 -- Is_Non_Overridden_Or_Null_Procedure --
10488 -----------------------------------------
10490 function Is_Non_Overridden_Or_Null_Procedure
10491 (Subp_Id : Entity_Id) return Boolean
10493 Alias_Id : Entity_Id;
10496 -- The subprogram is inherited (implicitly declared), it does not
10497 -- override and does not cover a primitive of an interface.
10499 if Ekind_In (Subp_Id, E_Function, E_Procedure)
10500 and then Present (Alias (Subp_Id))
10501 and then No (Interface_Alias (Subp_Id))
10502 and then No (Overridden_Operation (Subp_Id))
10504 Alias_Id := Alias (Subp_Id);
10506 if Requires_Overriding (Alias_Id) then
10509 elsif Nkind (Parent (Alias_Id)) = N_Procedure_Specification
10510 and then Null_Present (Parent (Alias_Id))
10517 end Is_Non_Overridden_Or_Null_Procedure;
10521 Prim_Ops : constant Elist_Id := Direct_Primitive_Operations (Typ);
10523 Prim_Elmt : Elmt_Id;
10525 -- Start of processing for Hide_Non_Overridden_Subprograms
10528 -- Inspect the list of primitives looking for non-overridden
10531 if Present (Prim_Ops) then
10532 Prim_Elmt := First_Elmt (Prim_Ops);
10533 while Present (Prim_Elmt) loop
10534 Prim := Node (Prim_Elmt);
10535 Next_Elmt (Prim_Elmt);
10537 if Is_Non_Overridden_Or_Null_Procedure (Prim) then
10538 Hide_Matching_Homographs
10540 Start_Elmt => Prim_Elmt);
10544 end Hide_Non_Overridden_Subprograms;
10546 ---------------------
10547 -- Local variables --
10548 ---------------------
10550 E : constant Entity_Id := Entity (N);
10552 Non_Generic_Case : constant Boolean := Nkind (N) = N_Freeze_Entity;
10553 -- True in non-generic case. Some of the processing here is skipped
10554 -- for the generic case since it is not needed. Basically in the
10555 -- generic case, we only need to do stuff that might generate error
10556 -- messages or warnings.
10558 -- Start of processing for Freeze_Entity_Checks
10561 -- Remember that we are processing a freezing entity. Required to
10562 -- ensure correct decoration of internal entities associated with
10563 -- interfaces (see New_Overloaded_Entity).
10565 Inside_Freezing_Actions := Inside_Freezing_Actions + 1;
10567 -- For tagged types covering interfaces add internal entities that link
10568 -- the primitives of the interfaces with the primitives that cover them.
10569 -- Note: These entities were originally generated only when generating
10570 -- code because their main purpose was to provide support to initialize
10571 -- the secondary dispatch tables. They are now generated also when
10572 -- compiling with no code generation to provide ASIS the relationship
10573 -- between interface primitives and tagged type primitives. They are
10574 -- also used to locate primitives covering interfaces when processing
10575 -- generics (see Derive_Subprograms).
10577 -- This is not needed in the generic case
10579 if Ada_Version >= Ada_2005
10580 and then Non_Generic_Case
10581 and then Ekind (E) = E_Record_Type
10582 and then Is_Tagged_Type (E)
10583 and then not Is_Interface (E)
10584 and then Has_Interfaces (E)
10586 -- This would be a good common place to call the routine that checks
10587 -- overriding of interface primitives (and thus factorize calls to
10588 -- Check_Abstract_Overriding located at different contexts in the
10589 -- compiler). However, this is not possible because it causes
10590 -- spurious errors in case of late overriding.
10592 Add_Internal_Interface_Entities (E);
10595 -- After all forms of overriding have been resolved, a tagged type may
10596 -- be left with a set of implicitly declared and possibly erroneous
10597 -- abstract subprograms, null procedures and subprograms that require
10598 -- overriding. If this set contains fully conformat homographs, then one
10599 -- is chosen arbitrarily (already done during resolution), otherwise all
10600 -- remaining non-fully conformant homographs are hidden from visibility
10601 -- (Ada RM 8.3 12.3/2).
10603 if Is_Tagged_Type (E) then
10604 Hide_Non_Overridden_Subprograms (E);
10609 if Ekind (E) = E_Record_Type
10610 and then Is_CPP_Class (E)
10611 and then Is_Tagged_Type (E)
10612 and then Tagged_Type_Expansion
10614 if CPP_Num_Prims (E) = 0 then
10616 -- If the CPP type has user defined components then it must import
10617 -- primitives from C++. This is required because if the C++ class
10618 -- has no primitives then the C++ compiler does not added the _tag
10619 -- component to the type.
10621 if First_Entity (E) /= Last_Entity (E) then
10623 ("'C'P'P type must import at least one primitive from C++??",
10628 -- Check that all its primitives are abstract or imported from C++.
10629 -- Check also availability of the C++ constructor.
10632 Has_Constructors : constant Boolean := Has_CPP_Constructors (E);
10634 Error_Reported : Boolean := False;
10638 Elmt := First_Elmt (Primitive_Operations (E));
10639 while Present (Elmt) loop
10640 Prim := Node (Elmt);
10642 if Comes_From_Source (Prim) then
10643 if Is_Abstract_Subprogram (Prim) then
10646 elsif not Is_Imported (Prim)
10647 or else Convention (Prim) /= Convention_CPP
10650 ("primitives of 'C'P'P types must be imported from C++ "
10651 & "or abstract??", Prim);
10653 elsif not Has_Constructors
10654 and then not Error_Reported
10656 Error_Msg_Name_1 := Chars (E);
10658 ("??'C'P'P constructor required for type %", Prim);
10659 Error_Reported := True;
10668 -- Check Ada derivation of CPP type
10670 if Expander_Active -- why? losing errors in -gnatc mode???
10671 and then Present (Etype (E)) -- defend against errors
10672 and then Tagged_Type_Expansion
10673 and then Ekind (E) = E_Record_Type
10674 and then Etype (E) /= E
10675 and then Is_CPP_Class (Etype (E))
10676 and then CPP_Num_Prims (Etype (E)) > 0
10677 and then not Is_CPP_Class (E)
10678 and then not Has_CPP_Constructors (Etype (E))
10680 -- If the parent has C++ primitives but it has no constructor then
10681 -- check that all the primitives are overridden in this derivation;
10682 -- otherwise the constructor of the parent is needed to build the
10690 Elmt := First_Elmt (Primitive_Operations (E));
10691 while Present (Elmt) loop
10692 Prim := Node (Elmt);
10694 if not Is_Abstract_Subprogram (Prim)
10695 and then No (Interface_Alias (Prim))
10696 and then Find_Dispatching_Type (Ultimate_Alias (Prim)) /= E
10698 Error_Msg_Name_1 := Chars (Etype (E));
10700 ("'C'P'P constructor required for parent type %", E);
10709 Inside_Freezing_Actions := Inside_Freezing_Actions - 1;
10711 -- If we have a type with predicates, build predicate function. This
10712 -- is not needed in the generic case, and is not needed within TSS
10713 -- subprograms and other predefined primitives.
10715 if Non_Generic_Case
10716 and then Is_Type (E)
10717 and then Has_Predicates (E)
10718 and then not Within_Internal_Subprogram
10720 Build_Predicate_Functions (E, N);
10723 -- If type has delayed aspects, this is where we do the preanalysis at
10724 -- the freeze point, as part of the consistent visibility check. Note
10725 -- that this must be done after calling Build_Predicate_Functions or
10726 -- Build_Invariant_Procedure since these subprograms fix occurrences of
10727 -- the subtype name in the saved expression so that they will not cause
10728 -- trouble in the preanalysis.
10730 -- This is also not needed in the generic case
10732 if Non_Generic_Case
10733 and then Has_Delayed_Aspects (E)
10734 and then Scope (E) = Current_Scope
10736 -- Retrieve the visibility to the discriminants in order to properly
10737 -- analyze the aspects.
10739 Push_Scope_And_Install_Discriminants (E);
10745 -- Look for aspect specification entries for this entity
10747 Ritem := First_Rep_Item (E);
10748 while Present (Ritem) loop
10749 if Nkind (Ritem) = N_Aspect_Specification
10750 and then Entity (Ritem) = E
10751 and then Is_Delayed_Aspect (Ritem)
10753 Check_Aspect_At_Freeze_Point (Ritem);
10756 Next_Rep_Item (Ritem);
10760 Uninstall_Discriminants_And_Pop_Scope (E);
10763 -- For a record type, deal with variant parts. This has to be delayed
10764 -- to this point, because of the issue of statically predicated
10765 -- subtypes, which we have to ensure are frozen before checking
10766 -- choices, since we need to have the static choice list set.
10768 if Is_Record_Type (E) then
10769 Check_Variant_Part : declare
10770 D : constant Node_Id := Declaration_Node (E);
10775 Others_Present : Boolean;
10776 pragma Warnings (Off, Others_Present);
10777 -- Indicates others present, not used in this case
10779 procedure Non_Static_Choice_Error (Choice : Node_Id);
10780 -- Error routine invoked by the generic instantiation below when
10781 -- the variant part has a non static choice.
10783 procedure Process_Declarations (Variant : Node_Id);
10784 -- Processes declarations associated with a variant. We analyzed
10785 -- the declarations earlier (in Sem_Ch3.Analyze_Variant_Part),
10786 -- but we still need the recursive call to Check_Choices for any
10787 -- nested variant to get its choices properly processed. This is
10788 -- also where we expand out the choices if expansion is active.
10790 package Variant_Choices_Processing is new
10791 Generic_Check_Choices
10792 (Process_Empty_Choice => No_OP,
10793 Process_Non_Static_Choice => Non_Static_Choice_Error,
10794 Process_Associated_Node => Process_Declarations);
10795 use Variant_Choices_Processing;
10797 -----------------------------
10798 -- Non_Static_Choice_Error --
10799 -----------------------------
10801 procedure Non_Static_Choice_Error (Choice : Node_Id) is
10803 Flag_Non_Static_Expr
10804 ("choice given in variant part is not static!", Choice);
10805 end Non_Static_Choice_Error;
10807 --------------------------
10808 -- Process_Declarations --
10809 --------------------------
10811 procedure Process_Declarations (Variant : Node_Id) is
10812 CL : constant Node_Id := Component_List (Variant);
10816 -- Check for static predicate present in this variant
10818 if Has_SP_Choice (Variant) then
10820 -- Here we expand. You might expect to find this call in
10821 -- Expand_N_Variant_Part, but that is called when we first
10822 -- see the variant part, and we cannot do this expansion
10823 -- earlier than the freeze point, since for statically
10824 -- predicated subtypes, the predicate is not known till
10825 -- the freeze point.
10827 -- Furthermore, we do this expansion even if the expander
10828 -- is not active, because other semantic processing, e.g.
10829 -- for aggregates, requires the expanded list of choices.
10831 -- If the expander is not active, then we can't just clobber
10832 -- the list since it would invalidate the ASIS -gnatct tree.
10833 -- So we have to rewrite the variant part with a Rewrite
10834 -- call that replaces it with a copy and clobber the copy.
10836 if not Expander_Active then
10838 NewV : constant Node_Id := New_Copy (Variant);
10840 Set_Discrete_Choices
10841 (NewV, New_Copy_List (Discrete_Choices (Variant)));
10842 Rewrite (Variant, NewV);
10846 Expand_Static_Predicates_In_Choices (Variant);
10849 -- We don't need to worry about the declarations in the variant
10850 -- (since they were analyzed by Analyze_Choices when we first
10851 -- encountered the variant), but we do need to take care of
10852 -- expansion of any nested variants.
10854 if not Null_Present (CL) then
10855 VP := Variant_Part (CL);
10857 if Present (VP) then
10859 (VP, Variants (VP), Etype (Name (VP)), Others_Present);
10862 end Process_Declarations;
10864 -- Start of processing for Check_Variant_Part
10867 -- Find component list
10871 if Nkind (D) = N_Full_Type_Declaration then
10872 T := Type_Definition (D);
10874 if Nkind (T) = N_Record_Definition then
10875 C := Component_List (T);
10877 elsif Nkind (T) = N_Derived_Type_Definition
10878 and then Present (Record_Extension_Part (T))
10880 C := Component_List (Record_Extension_Part (T));
10884 -- Case of variant part present
10886 if Present (C) and then Present (Variant_Part (C)) then
10887 VP := Variant_Part (C);
10892 (VP, Variants (VP), Etype (Name (VP)), Others_Present);
10894 -- If the last variant does not contain the Others choice,
10895 -- replace it with an N_Others_Choice node since Gigi always
10896 -- wants an Others. Note that we do not bother to call Analyze
10897 -- on the modified variant part, since its only effect would be
10898 -- to compute the Others_Discrete_Choices node laboriously, and
10899 -- of course we already know the list of choices corresponding
10900 -- to the others choice (it's the list we're replacing).
10902 -- We only want to do this if the expander is active, since
10903 -- we do not want to clobber the ASIS tree.
10905 if Expander_Active then
10907 Last_Var : constant Node_Id :=
10908 Last_Non_Pragma (Variants (VP));
10910 Others_Node : Node_Id;
10913 if Nkind (First (Discrete_Choices (Last_Var))) /=
10916 Others_Node := Make_Others_Choice (Sloc (Last_Var));
10917 Set_Others_Discrete_Choices
10918 (Others_Node, Discrete_Choices (Last_Var));
10919 Set_Discrete_Choices
10920 (Last_Var, New_List (Others_Node));
10925 end Check_Variant_Part;
10927 end Freeze_Entity_Checks;
10929 -------------------------
10930 -- Get_Alignment_Value --
10931 -------------------------
10933 function Get_Alignment_Value (Expr : Node_Id) return Uint is
10934 Align : constant Uint := Static_Integer (Expr);
10937 if Align = No_Uint then
10940 elsif Align <= 0 then
10941 Error_Msg_N ("alignment value must be positive", Expr);
10945 for J in Int range 0 .. 64 loop
10947 M : constant Uint := Uint_2 ** J;
10950 exit when M = Align;
10954 ("alignment value must be power of 2", Expr);
10962 end Get_Alignment_Value;
10964 -------------------------------------
10965 -- Inherit_Aspects_At_Freeze_Point --
10966 -------------------------------------
10968 procedure Inherit_Aspects_At_Freeze_Point (Typ : Entity_Id) is
10969 function Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
10970 (Rep_Item : Node_Id) return Boolean;
10971 -- This routine checks if Rep_Item is either a pragma or an aspect
10972 -- specification node whose correponding pragma (if any) is present in
10973 -- the Rep Item chain of the entity it has been specified to.
10975 --------------------------------------------------
10976 -- Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item --
10977 --------------------------------------------------
10979 function Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
10980 (Rep_Item : Node_Id) return Boolean
10984 Nkind (Rep_Item) = N_Pragma
10985 or else Present_In_Rep_Item
10986 (Entity (Rep_Item), Aspect_Rep_Item (Rep_Item));
10987 end Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item;
10989 -- Start of processing for Inherit_Aspects_At_Freeze_Point
10992 -- A representation item is either subtype-specific (Size and Alignment
10993 -- clauses) or type-related (all others). Subtype-specific aspects may
10994 -- differ for different subtypes of the same type (RM 13.1.8).
10996 -- A derived type inherits each type-related representation aspect of
10997 -- its parent type that was directly specified before the declaration of
10998 -- the derived type (RM 13.1.15).
11000 -- A derived subtype inherits each subtype-specific representation
11001 -- aspect of its parent subtype that was directly specified before the
11002 -- declaration of the derived type (RM 13.1.15).
11004 -- The general processing involves inheriting a representation aspect
11005 -- from a parent type whenever the first rep item (aspect specification,
11006 -- attribute definition clause, pragma) corresponding to the given
11007 -- representation aspect in the rep item chain of Typ, if any, isn't
11008 -- directly specified to Typ but to one of its parents.
11010 -- ??? Note that, for now, just a limited number of representation
11011 -- aspects have been inherited here so far. Many of them are
11012 -- still inherited in Sem_Ch3. This will be fixed soon. Here is
11013 -- a non- exhaustive list of aspects that likely also need to
11014 -- be moved to this routine: Alignment, Component_Alignment,
11015 -- Component_Size, Machine_Radix, Object_Size, Pack, Predicates,
11016 -- Preelaborable_Initialization, RM_Size and Small.
11018 -- In addition, Convention must be propagated from base type to subtype,
11019 -- because the subtype may have been declared on an incomplete view.
11021 if Nkind (Parent (Typ)) = N_Private_Extension_Declaration then
11027 if not Has_Rep_Item (Typ, Name_Ada_05, Name_Ada_2005, False)
11028 and then Has_Rep_Item (Typ, Name_Ada_05, Name_Ada_2005)
11029 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
11030 (Get_Rep_Item (Typ, Name_Ada_05, Name_Ada_2005))
11032 Set_Is_Ada_2005_Only (Typ);
11037 if not Has_Rep_Item (Typ, Name_Ada_12, Name_Ada_2012, False)
11038 and then Has_Rep_Item (Typ, Name_Ada_12, Name_Ada_2012)
11039 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
11040 (Get_Rep_Item (Typ, Name_Ada_12, Name_Ada_2012))
11042 Set_Is_Ada_2012_Only (Typ);
11047 if not Has_Rep_Item (Typ, Name_Atomic, Name_Shared, False)
11048 and then Has_Rep_Pragma (Typ, Name_Atomic, Name_Shared)
11049 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
11050 (Get_Rep_Item (Typ, Name_Atomic, Name_Shared))
11052 Set_Is_Atomic (Typ);
11053 Set_Treat_As_Volatile (Typ);
11054 Set_Is_Volatile (Typ);
11059 if Is_Record_Type (Typ)
11060 and then Typ /= Base_Type (Typ) and then Is_Frozen (Base_Type (Typ))
11062 Set_Convention (Typ, Convention (Base_Type (Typ)));
11065 -- Default_Component_Value
11067 -- Verify that there is no rep_item declared for the type, and there
11068 -- is one coming from an ancestor.
11070 if Is_Array_Type (Typ)
11071 and then Is_Base_Type (Typ)
11072 and then not Has_Rep_Item (Typ, Name_Default_Component_Value, False)
11073 and then Has_Rep_Item (Typ, Name_Default_Component_Value)
11075 Set_Default_Aspect_Component_Value (Typ,
11076 Default_Aspect_Component_Value
11077 (Entity (Get_Rep_Item (Typ, Name_Default_Component_Value))));
11082 if Is_Scalar_Type (Typ)
11083 and then Is_Base_Type (Typ)
11084 and then not Has_Rep_Item (Typ, Name_Default_Value, False)
11085 and then Has_Rep_Item (Typ, Name_Default_Value)
11087 Set_Has_Default_Aspect (Typ);
11088 Set_Default_Aspect_Value (Typ,
11089 Default_Aspect_Value
11090 (Entity (Get_Rep_Item (Typ, Name_Default_Value))));
11095 if not Has_Rep_Item (Typ, Name_Discard_Names, False)
11096 and then Has_Rep_Item (Typ, Name_Discard_Names)
11097 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
11098 (Get_Rep_Item (Typ, Name_Discard_Names))
11100 Set_Discard_Names (Typ);
11105 if not Has_Rep_Item (Typ, Name_Invariant, False)
11106 and then Has_Rep_Item (Typ, Name_Invariant)
11107 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
11108 (Get_Rep_Item (Typ, Name_Invariant))
11110 Set_Has_Invariants (Typ);
11112 if Class_Present (Get_Rep_Item (Typ, Name_Invariant)) then
11113 Set_Has_Inheritable_Invariants (Typ);
11116 -- If we have a subtype with invariants, whose base type does not have
11117 -- invariants, copy these invariants to the base type. This happens for
11118 -- the case of implicit base types created for scalar and array types.
11120 elsif Has_Invariants (Typ)
11121 and then not Has_Invariants (Base_Type (Typ))
11123 Set_Has_Invariants (Base_Type (Typ));
11124 Set_Invariant_Procedure (Base_Type (Typ), Invariant_Procedure (Typ));
11129 if not Has_Rep_Item (Typ, Name_Volatile, False)
11130 and then Has_Rep_Item (Typ, Name_Volatile)
11131 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
11132 (Get_Rep_Item (Typ, Name_Volatile))
11134 Set_Treat_As_Volatile (Typ);
11135 Set_Is_Volatile (Typ);
11138 -- Inheritance for derived types only
11140 if Is_Derived_Type (Typ) then
11142 Bas_Typ : constant Entity_Id := Base_Type (Typ);
11143 Imp_Bas_Typ : constant Entity_Id := Implementation_Base_Type (Typ);
11146 -- Atomic_Components
11148 if not Has_Rep_Item (Typ, Name_Atomic_Components, False)
11149 and then Has_Rep_Item (Typ, Name_Atomic_Components)
11150 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
11151 (Get_Rep_Item (Typ, Name_Atomic_Components))
11153 Set_Has_Atomic_Components (Imp_Bas_Typ);
11156 -- Volatile_Components
11158 if not Has_Rep_Item (Typ, Name_Volatile_Components, False)
11159 and then Has_Rep_Item (Typ, Name_Volatile_Components)
11160 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
11161 (Get_Rep_Item (Typ, Name_Volatile_Components))
11163 Set_Has_Volatile_Components (Imp_Bas_Typ);
11166 -- Finalize_Storage_Only
11168 if not Has_Rep_Pragma (Typ, Name_Finalize_Storage_Only, False)
11169 and then Has_Rep_Pragma (Typ, Name_Finalize_Storage_Only)
11171 Set_Finalize_Storage_Only (Bas_Typ);
11174 -- Universal_Aliasing
11176 if not Has_Rep_Item (Typ, Name_Universal_Aliasing, False)
11177 and then Has_Rep_Item (Typ, Name_Universal_Aliasing)
11178 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
11179 (Get_Rep_Item (Typ, Name_Universal_Aliasing))
11181 Set_Universal_Aliasing (Imp_Bas_Typ);
11186 if Is_Record_Type (Typ) then
11187 if not Has_Rep_Item (Typ, Name_Bit_Order, False)
11188 and then Has_Rep_Item (Typ, Name_Bit_Order)
11190 Set_Reverse_Bit_Order (Bas_Typ,
11191 Reverse_Bit_Order (Entity (Name
11192 (Get_Rep_Item (Typ, Name_Bit_Order)))));
11196 -- Scalar_Storage_Order
11198 -- Note: the aspect is specified on a first subtype, but recorded
11199 -- in a flag of the base type!
11201 if (Is_Record_Type (Typ) or else Is_Array_Type (Typ))
11202 and then Typ = Bas_Typ
11204 -- For a type extension, always inherit from parent; otherwise
11205 -- inherit if no default applies. Note: we do not check for
11206 -- an explicit rep item on the parent type when inheriting,
11207 -- because the parent SSO may itself have been set by default.
11209 if not Has_Rep_Item (First_Subtype (Typ),
11210 Name_Scalar_Storage_Order, False)
11211 and then (Is_Tagged_Type (Bas_Typ)
11212 or else not (SSO_Set_Low_By_Default (Bas_Typ)
11214 SSO_Set_High_By_Default (Bas_Typ)))
11216 Set_Reverse_Storage_Order (Bas_Typ,
11217 Reverse_Storage_Order
11218 (Implementation_Base_Type (Etype (Bas_Typ))));
11220 -- Clear default SSO indications, since the inherited aspect
11221 -- which was set explicitly overrides the default.
11223 Set_SSO_Set_Low_By_Default (Bas_Typ, False);
11224 Set_SSO_Set_High_By_Default (Bas_Typ, False);
11229 end Inherit_Aspects_At_Freeze_Point;
11235 procedure Initialize is
11237 Address_Clause_Checks.Init;
11238 Unchecked_Conversions.Init;
11240 if VM_Target /= No_VM or else AAMP_On_Target then
11241 Independence_Checks.Init;
11245 ---------------------------
11246 -- Install_Discriminants --
11247 ---------------------------
11249 procedure Install_Discriminants (E : Entity_Id) is
11253 Disc := First_Discriminant (E);
11254 while Present (Disc) loop
11255 Prev := Current_Entity (Disc);
11256 Set_Current_Entity (Disc);
11257 Set_Is_Immediately_Visible (Disc);
11258 Set_Homonym (Disc, Prev);
11259 Next_Discriminant (Disc);
11261 end Install_Discriminants;
11263 -------------------------
11264 -- Is_Operational_Item --
11265 -------------------------
11267 function Is_Operational_Item (N : Node_Id) return Boolean is
11269 if Nkind (N) /= N_Attribute_Definition_Clause then
11274 Id : constant Attribute_Id := Get_Attribute_Id (Chars (N));
11276 return Id = Attribute_Input
11277 or else Id = Attribute_Output
11278 or else Id = Attribute_Read
11279 or else Id = Attribute_Write
11280 or else Id = Attribute_External_Tag;
11283 end Is_Operational_Item;
11285 -------------------------
11286 -- Is_Predicate_Static --
11287 -------------------------
11289 -- Note: the basic legality of the expression has already been checked, so
11290 -- we don't need to worry about cases or ranges on strings for example.
11292 function Is_Predicate_Static
11294 Nam : Name_Id) return Boolean
11296 function All_Static_Case_Alternatives (L : List_Id) return Boolean;
11297 -- Given a list of case expression alternatives, returns True if all
11298 -- the alternatives are static (have all static choices, and a static
11301 function All_Static_Choices (L : List_Id) return Boolean;
11302 -- Returns true if all elements of the list are OK static choices
11303 -- as defined below for Is_Static_Choice. Used for case expression
11304 -- alternatives and for the right operand of a membership test. An
11305 -- others_choice is static if the corresponding expression is static.
11306 -- The staticness of the bounds is checked separately.
11308 function Is_Static_Choice (N : Node_Id) return Boolean;
11309 -- Returns True if N represents a static choice (static subtype, or
11310 -- static subtype indication, or static expression, or static range).
11312 -- Note that this is a bit more inclusive than we actually need
11313 -- (in particular membership tests do not allow the use of subtype
11314 -- indications). But that doesn't matter, we have already checked
11315 -- that the construct is legal to get this far.
11317 function Is_Type_Ref (N : Node_Id) return Boolean;
11318 pragma Inline (Is_Type_Ref);
11319 -- Returns True if N is a reference to the type for the predicate in the
11320 -- expression (i.e. if it is an identifier whose Chars field matches the
11321 -- Nam given in the call). N must not be parenthesized, if the type name
11322 -- appears in parens, this routine will return False.
11324 ----------------------------------
11325 -- All_Static_Case_Alternatives --
11326 ----------------------------------
11328 function All_Static_Case_Alternatives (L : List_Id) return Boolean is
11333 while Present (N) loop
11334 if not (All_Static_Choices (Discrete_Choices (N))
11335 and then Is_OK_Static_Expression (Expression (N)))
11344 end All_Static_Case_Alternatives;
11346 ------------------------
11347 -- All_Static_Choices --
11348 ------------------------
11350 function All_Static_Choices (L : List_Id) return Boolean is
11355 while Present (N) loop
11356 if not Is_Static_Choice (N) then
11364 end All_Static_Choices;
11366 ----------------------
11367 -- Is_Static_Choice --
11368 ----------------------
11370 function Is_Static_Choice (N : Node_Id) return Boolean is
11372 return Nkind (N) = N_Others_Choice
11373 or else Is_OK_Static_Expression (N)
11374 or else (Is_Entity_Name (N) and then Is_Type (Entity (N))
11375 and then Is_OK_Static_Subtype (Entity (N)))
11376 or else (Nkind (N) = N_Subtype_Indication
11377 and then Is_OK_Static_Subtype (Entity (N)))
11378 or else (Nkind (N) = N_Range and then Is_OK_Static_Range (N));
11379 end Is_Static_Choice;
11385 function Is_Type_Ref (N : Node_Id) return Boolean is
11387 return Nkind (N) = N_Identifier
11388 and then Chars (N) = Nam
11389 and then Paren_Count (N) = 0;
11392 -- Start of processing for Is_Predicate_Static
11395 -- Predicate_Static means one of the following holds. Numbers are the
11396 -- corresponding paragraph numbers in (RM 3.2.4(16-22)).
11398 -- 16: A static expression
11400 if Is_OK_Static_Expression (Expr) then
11403 -- 17: A membership test whose simple_expression is the current
11404 -- instance, and whose membership_choice_list meets the requirements
11405 -- for a static membership test.
11407 elsif Nkind (Expr) in N_Membership_Test
11408 and then ((Present (Right_Opnd (Expr))
11409 and then Is_Static_Choice (Right_Opnd (Expr)))
11411 (Present (Alternatives (Expr))
11412 and then All_Static_Choices (Alternatives (Expr))))
11416 -- 18. A case_expression whose selecting_expression is the current
11417 -- instance, and whose dependent expressions are static expressions.
11419 elsif Nkind (Expr) = N_Case_Expression
11420 and then Is_Type_Ref (Expression (Expr))
11421 and then All_Static_Case_Alternatives (Alternatives (Expr))
11425 -- 19. A call to a predefined equality or ordering operator, where one
11426 -- operand is the current instance, and the other is a static
11429 -- Note: the RM is clearly wrong here in not excluding string types.
11430 -- Without this exclusion, we would allow expressions like X > "ABC"
11431 -- to be considered as predicate-static, which is clearly not intended,
11432 -- since the idea is for predicate-static to be a subset of normal
11433 -- static expressions (and "DEF" > "ABC" is not a static expression).
11435 -- However, we do allow internally generated (not from source) equality
11436 -- and inequality operations to be valid on strings (this helps deal
11437 -- with cases where we transform A in "ABC" to A = "ABC).
11439 elsif Nkind (Expr) in N_Op_Compare
11440 and then ((not Is_String_Type (Etype (Left_Opnd (Expr))))
11441 or else (Nkind_In (Expr, N_Op_Eq, N_Op_Ne)
11442 and then not Comes_From_Source (Expr)))
11443 and then ((Is_Type_Ref (Left_Opnd (Expr))
11444 and then Is_OK_Static_Expression (Right_Opnd (Expr)))
11446 (Is_Type_Ref (Right_Opnd (Expr))
11447 and then Is_OK_Static_Expression (Left_Opnd (Expr))))
11451 -- 20. A call to a predefined boolean logical operator, where each
11452 -- operand is predicate-static.
11454 elsif (Nkind_In (Expr, N_Op_And, N_Op_Or, N_Op_Xor)
11455 and then Is_Predicate_Static (Left_Opnd (Expr), Nam)
11456 and then Is_Predicate_Static (Right_Opnd (Expr), Nam))
11458 (Nkind (Expr) = N_Op_Not
11459 and then Is_Predicate_Static (Right_Opnd (Expr), Nam))
11463 -- 21. A short-circuit control form where both operands are
11464 -- predicate-static.
11466 elsif Nkind (Expr) in N_Short_Circuit
11467 and then Is_Predicate_Static (Left_Opnd (Expr), Nam)
11468 and then Is_Predicate_Static (Right_Opnd (Expr), Nam)
11472 -- 22. A parenthesized predicate-static expression. This does not
11473 -- require any special test, since we just ignore paren levels in
11474 -- all the cases above.
11476 -- One more test that is an implementation artifact caused by the fact
11477 -- that we are analyzing not the original expression, but the generated
11478 -- expression in the body of the predicate function. This can include
11479 -- references to inherited predicates, so that the expression we are
11480 -- processing looks like:
11482 -- expression and then xxPredicate (typ (Inns))
11484 -- Where the call is to a Predicate function for an inherited predicate.
11485 -- We simply ignore such a call, which could be to either a dynamic or
11486 -- a static predicate. Note that if the parent predicate is dynamic then
11487 -- eventually this type will be marked as dynamic, but you are allowed
11488 -- to specify a static predicate for a subtype which is inheriting a
11489 -- dynamic predicate, so the static predicate validation here ignores
11490 -- the inherited predicate even if it is dynamic.
11492 elsif Nkind (Expr) = N_Function_Call
11493 and then Is_Predicate_Function (Entity (Name (Expr)))
11497 -- That's an exhaustive list of tests, all other cases are not
11498 -- predicate-static, so we return False.
11503 end Is_Predicate_Static;
11505 ---------------------
11506 -- Kill_Rep_Clause --
11507 ---------------------
11509 procedure Kill_Rep_Clause (N : Node_Id) is
11511 pragma Assert (Ignore_Rep_Clauses);
11513 -- Note: we use Replace rather than Rewrite, because we don't want
11514 -- ASIS to be able to use Original_Node to dig out the (undecorated)
11515 -- rep clause that is being replaced.
11517 Replace (N, Make_Null_Statement (Sloc (N)));
11519 -- The null statement must be marked as not coming from source. This is
11520 -- so that ASIS ignores it, and also the back end does not expect bogus
11521 -- "from source" null statements in weird places (e.g. in declarative
11522 -- regions where such null statements are not allowed).
11524 Set_Comes_From_Source (N, False);
11525 end Kill_Rep_Clause;
11531 function Minimum_Size
11533 Biased : Boolean := False) return Nat
11535 Lo : Uint := No_Uint;
11536 Hi : Uint := No_Uint;
11537 LoR : Ureal := No_Ureal;
11538 HiR : Ureal := No_Ureal;
11539 LoSet : Boolean := False;
11540 HiSet : Boolean := False;
11543 Ancest : Entity_Id;
11544 R_Typ : constant Entity_Id := Root_Type (T);
11547 -- If bad type, return 0
11549 if T = Any_Type then
11552 -- For generic types, just return zero. There cannot be any legitimate
11553 -- need to know such a size, but this routine may be called with a
11554 -- generic type as part of normal processing.
11556 elsif Is_Generic_Type (R_Typ) or else R_Typ = Any_Type then
11559 -- Access types (cannot have size smaller than System.Address)
11561 elsif Is_Access_Type (T) then
11562 return System_Address_Size;
11564 -- Floating-point types
11566 elsif Is_Floating_Point_Type (T) then
11567 return UI_To_Int (Esize (R_Typ));
11571 elsif Is_Discrete_Type (T) then
11573 -- The following loop is looking for the nearest compile time known
11574 -- bounds following the ancestor subtype chain. The idea is to find
11575 -- the most restrictive known bounds information.
11579 if Ancest = Any_Type or else Etype (Ancest) = Any_Type then
11584 if Compile_Time_Known_Value (Type_Low_Bound (Ancest)) then
11585 Lo := Expr_Rep_Value (Type_Low_Bound (Ancest));
11592 if Compile_Time_Known_Value (Type_High_Bound (Ancest)) then
11593 Hi := Expr_Rep_Value (Type_High_Bound (Ancest));
11599 Ancest := Ancestor_Subtype (Ancest);
11601 if No (Ancest) then
11602 Ancest := Base_Type (T);
11604 if Is_Generic_Type (Ancest) then
11610 -- Fixed-point types. We can't simply use Expr_Value to get the
11611 -- Corresponding_Integer_Value values of the bounds, since these do not
11612 -- get set till the type is frozen, and this routine can be called
11613 -- before the type is frozen. Similarly the test for bounds being static
11614 -- needs to include the case where we have unanalyzed real literals for
11615 -- the same reason.
11617 elsif Is_Fixed_Point_Type (T) then
11619 -- The following loop is looking for the nearest compile time known
11620 -- bounds following the ancestor subtype chain. The idea is to find
11621 -- the most restrictive known bounds information.
11625 if Ancest = Any_Type or else Etype (Ancest) = Any_Type then
11629 -- Note: In the following two tests for LoSet and HiSet, it may
11630 -- seem redundant to test for N_Real_Literal here since normally
11631 -- one would assume that the test for the value being known at
11632 -- compile time includes this case. However, there is a glitch.
11633 -- If the real literal comes from folding a non-static expression,
11634 -- then we don't consider any non- static expression to be known
11635 -- at compile time if we are in configurable run time mode (needed
11636 -- in some cases to give a clearer definition of what is and what
11637 -- is not accepted). So the test is indeed needed. Without it, we
11638 -- would set neither Lo_Set nor Hi_Set and get an infinite loop.
11641 if Nkind (Type_Low_Bound (Ancest)) = N_Real_Literal
11642 or else Compile_Time_Known_Value (Type_Low_Bound (Ancest))
11644 LoR := Expr_Value_R (Type_Low_Bound (Ancest));
11651 if Nkind (Type_High_Bound (Ancest)) = N_Real_Literal
11652 or else Compile_Time_Known_Value (Type_High_Bound (Ancest))
11654 HiR := Expr_Value_R (Type_High_Bound (Ancest));
11660 Ancest := Ancestor_Subtype (Ancest);
11662 if No (Ancest) then
11663 Ancest := Base_Type (T);
11665 if Is_Generic_Type (Ancest) then
11671 Lo := UR_To_Uint (LoR / Small_Value (T));
11672 Hi := UR_To_Uint (HiR / Small_Value (T));
11674 -- No other types allowed
11677 raise Program_Error;
11680 -- Fall through with Hi and Lo set. Deal with biased case
11683 and then not Is_Fixed_Point_Type (T)
11684 and then not (Is_Enumeration_Type (T)
11685 and then Has_Non_Standard_Rep (T)))
11686 or else Has_Biased_Representation (T)
11692 -- Signed case. Note that we consider types like range 1 .. -1 to be
11693 -- signed for the purpose of computing the size, since the bounds have
11694 -- to be accommodated in the base type.
11696 if Lo < 0 or else Hi < 0 then
11700 -- S = size, B = 2 ** (size - 1) (can accommodate -B .. +(B - 1))
11701 -- Note that we accommodate the case where the bounds cross. This
11702 -- can happen either because of the way the bounds are declared
11703 -- or because of the algorithm in Freeze_Fixed_Point_Type.
11717 -- If both bounds are positive, make sure that both are represen-
11718 -- table in the case where the bounds are crossed. This can happen
11719 -- either because of the way the bounds are declared, or because of
11720 -- the algorithm in Freeze_Fixed_Point_Type.
11726 -- S = size, (can accommodate 0 .. (2**size - 1))
11729 while Hi >= Uint_2 ** S loop
11737 ---------------------------
11738 -- New_Stream_Subprogram --
11739 ---------------------------
11741 procedure New_Stream_Subprogram
11745 Nam : TSS_Name_Type)
11747 Loc : constant Source_Ptr := Sloc (N);
11748 Sname : constant Name_Id := Make_TSS_Name (Base_Type (Ent), Nam);
11749 Subp_Id : Entity_Id;
11750 Subp_Decl : Node_Id;
11754 Defer_Declaration : constant Boolean :=
11755 Is_Tagged_Type (Ent) or else Is_Private_Type (Ent);
11756 -- For a tagged type, there is a declaration for each stream attribute
11757 -- at the freeze point, and we must generate only a completion of this
11758 -- declaration. We do the same for private types, because the full view
11759 -- might be tagged. Otherwise we generate a declaration at the point of
11760 -- the attribute definition clause.
11762 function Build_Spec return Node_Id;
11763 -- Used for declaration and renaming declaration, so that this is
11764 -- treated as a renaming_as_body.
11770 function Build_Spec return Node_Id is
11771 Out_P : constant Boolean := (Nam = TSS_Stream_Read);
11774 T_Ref : constant Node_Id := New_Occurrence_Of (Etyp, Loc);
11777 Subp_Id := Make_Defining_Identifier (Loc, Sname);
11779 -- S : access Root_Stream_Type'Class
11781 Formals := New_List (
11782 Make_Parameter_Specification (Loc,
11783 Defining_Identifier =>
11784 Make_Defining_Identifier (Loc, Name_S),
11786 Make_Access_Definition (Loc,
11788 New_Occurrence_Of (
11789 Designated_Type (Etype (F)), Loc))));
11791 if Nam = TSS_Stream_Input then
11793 Make_Function_Specification (Loc,
11794 Defining_Unit_Name => Subp_Id,
11795 Parameter_Specifications => Formals,
11796 Result_Definition => T_Ref);
11800 Append_To (Formals,
11801 Make_Parameter_Specification (Loc,
11802 Defining_Identifier => Make_Defining_Identifier (Loc, Name_V),
11803 Out_Present => Out_P,
11804 Parameter_Type => T_Ref));
11807 Make_Procedure_Specification (Loc,
11808 Defining_Unit_Name => Subp_Id,
11809 Parameter_Specifications => Formals);
11815 -- Start of processing for New_Stream_Subprogram
11818 F := First_Formal (Subp);
11820 if Ekind (Subp) = E_Procedure then
11821 Etyp := Etype (Next_Formal (F));
11823 Etyp := Etype (Subp);
11826 -- Prepare subprogram declaration and insert it as an action on the
11827 -- clause node. The visibility for this entity is used to test for
11828 -- visibility of the attribute definition clause (in the sense of
11829 -- 8.3(23) as amended by AI-195).
11831 if not Defer_Declaration then
11833 Make_Subprogram_Declaration (Loc,
11834 Specification => Build_Spec);
11836 -- For a tagged type, there is always a visible declaration for each
11837 -- stream TSS (it is a predefined primitive operation), and the
11838 -- completion of this declaration occurs at the freeze point, which is
11839 -- not always visible at places where the attribute definition clause is
11840 -- visible. So, we create a dummy entity here for the purpose of
11841 -- tracking the visibility of the attribute definition clause itself.
11845 Make_Defining_Identifier (Loc, New_External_Name (Sname, 'V'));
11847 Make_Object_Declaration (Loc,
11848 Defining_Identifier => Subp_Id,
11849 Object_Definition => New_Occurrence_Of (Standard_Boolean, Loc));
11852 Insert_Action (N, Subp_Decl);
11853 Set_Entity (N, Subp_Id);
11856 Make_Subprogram_Renaming_Declaration (Loc,
11857 Specification => Build_Spec,
11858 Name => New_Occurrence_Of (Subp, Loc));
11860 if Defer_Declaration then
11861 Set_TSS (Base_Type (Ent), Subp_Id);
11863 Insert_Action (N, Subp_Decl);
11864 Copy_TSS (Subp_Id, Base_Type (Ent));
11866 end New_Stream_Subprogram;
11868 ------------------------------------------
11869 -- Push_Scope_And_Install_Discriminants --
11870 ------------------------------------------
11872 procedure Push_Scope_And_Install_Discriminants (E : Entity_Id) is
11874 if Has_Discriminants (E) then
11877 -- Make discriminants visible for type declarations and protected
11878 -- type declarations, not for subtype declarations (RM 13.1.1 (12/3))
11880 if Nkind (Parent (E)) /= N_Subtype_Declaration then
11881 Install_Discriminants (E);
11884 end Push_Scope_And_Install_Discriminants;
11886 ------------------------
11887 -- Rep_Item_Too_Early --
11888 ------------------------
11890 function Rep_Item_Too_Early (T : Entity_Id; N : Node_Id) return Boolean is
11892 -- Cannot apply non-operational rep items to generic types
11894 if Is_Operational_Item (N) then
11898 and then Is_Generic_Type (Root_Type (T))
11899 and then (Nkind (N) /= N_Pragma
11900 or else Get_Pragma_Id (N) /= Pragma_Convention)
11902 Error_Msg_N ("representation item not allowed for generic type", N);
11906 -- Otherwise check for incomplete type
11908 if Is_Incomplete_Or_Private_Type (T)
11909 and then No (Underlying_Type (T))
11911 (Nkind (N) /= N_Pragma
11912 or else Get_Pragma_Id (N) /= Pragma_Import)
11915 ("representation item must be after full type declaration", N);
11918 -- If the type has incomplete components, a representation clause is
11919 -- illegal but stream attributes and Convention pragmas are correct.
11921 elsif Has_Private_Component (T) then
11922 if Nkind (N) = N_Pragma then
11927 ("representation item must appear after type is fully defined",
11934 end Rep_Item_Too_Early;
11936 -----------------------
11937 -- Rep_Item_Too_Late --
11938 -----------------------
11940 function Rep_Item_Too_Late
11943 FOnly : Boolean := False) return Boolean
11946 Parent_Type : Entity_Id;
11948 procedure No_Type_Rep_Item;
11949 -- Output message indicating that no type-related aspects can be
11950 -- specified due to some property of the parent type.
11952 procedure Too_Late;
11953 -- Output message for an aspect being specified too late
11955 -- Note that neither of the above errors is considered a serious one,
11956 -- since the effect is simply that we ignore the representation clause
11958 -- Is this really true? In any case if we make this change we must
11959 -- document the requirement in the spec of Rep_Item_Too_Late that
11960 -- if True is returned, then the rep item must be completely ignored???
11962 ----------------------
11963 -- No_Type_Rep_Item --
11964 ----------------------
11966 procedure No_Type_Rep_Item is
11968 Error_Msg_N ("|type-related representation item not permitted!", N);
11969 end No_Type_Rep_Item;
11975 procedure Too_Late is
11977 -- Other compilers seem more relaxed about rep items appearing too
11978 -- late. Since analysis tools typically don't care about rep items
11979 -- anyway, no reason to be too strict about this.
11981 if not Relaxed_RM_Semantics then
11982 Error_Msg_N ("|representation item appears too late!", N);
11986 -- Start of processing for Rep_Item_Too_Late
11989 -- First make sure entity is not frozen (RM 13.1(9))
11993 -- Exclude imported types, which may be frozen if they appear in a
11994 -- representation clause for a local type.
11996 and then not From_Limited_With (T)
11998 -- Exclude generated entities (not coming from source). The common
11999 -- case is when we generate a renaming which prematurely freezes the
12000 -- renamed internal entity, but we still want to be able to set copies
12001 -- of attribute values such as Size/Alignment.
12003 and then Comes_From_Source (T)
12006 S := First_Subtype (T);
12008 if Present (Freeze_Node (S)) then
12009 if not Relaxed_RM_Semantics then
12011 ("??no more representation items for }", Freeze_Node (S), S);
12017 -- Check for case of untagged derived type whose parent either has
12018 -- primitive operations, or is a by reference type (RM 13.1(10)). In
12019 -- this case we do not output a Too_Late message, since there is no
12020 -- earlier point where the rep item could be placed to make it legal.
12024 and then Is_Derived_Type (T)
12025 and then not Is_Tagged_Type (T)
12027 Parent_Type := Etype (Base_Type (T));
12029 if Has_Primitive_Operations (Parent_Type) then
12032 if not Relaxed_RM_Semantics then
12034 ("\parent type & has primitive operations!", N, Parent_Type);
12039 elsif Is_By_Reference_Type (Parent_Type) then
12042 if not Relaxed_RM_Semantics then
12044 ("\parent type & is a by reference type!", N, Parent_Type);
12051 -- No error, but one more warning to consider. The RM (surprisingly)
12052 -- allows this pattern:
12055 -- primitive operations for S
12056 -- type R is new S;
12057 -- rep clause for S
12059 -- Meaning that calls on the primitive operations of S for values of
12060 -- type R may require possibly expensive implicit conversion operations.
12061 -- This is not an error, but is worth a warning.
12063 if not Relaxed_RM_Semantics and then Is_Type (T) then
12065 DTL : constant Entity_Id := Derived_Type_Link (Base_Type (T));
12069 and then Has_Primitive_Operations (Base_Type (T))
12071 -- For now, do not generate this warning for the case of aspect
12072 -- specification using Ada 2012 syntax, since we get wrong
12073 -- messages we do not understand. The whole business of derived
12074 -- types and rep items seems a bit confused when aspects are
12075 -- used, since the aspects are not evaluated till freeze time.
12077 and then not From_Aspect_Specification (N)
12079 Error_Msg_Sloc := Sloc (DTL);
12081 ("representation item for& appears after derived type "
12082 & "declaration#??", N);
12084 ("\may result in implicit conversions for primitive "
12085 & "operations of&??", N, T);
12087 ("\to change representations when called with arguments "
12088 & "of type&??", N, DTL);
12093 -- No error, link item into head of chain of rep items for the entity,
12094 -- but avoid chaining if we have an overloadable entity, and the pragma
12095 -- is one that can apply to multiple overloaded entities.
12097 if Is_Overloadable (T) and then Nkind (N) = N_Pragma then
12099 Pname : constant Name_Id := Pragma_Name (N);
12101 if Nam_In (Pname, Name_Convention, Name_Import, Name_Export,
12102 Name_External, Name_Interface)
12109 Record_Rep_Item (T, N);
12111 end Rep_Item_Too_Late;
12113 -------------------------------------
12114 -- Replace_Type_References_Generic --
12115 -------------------------------------
12117 procedure Replace_Type_References_Generic (N : Node_Id; T : Entity_Id) is
12118 TName : constant Name_Id := Chars (T);
12120 function Replace_Node (N : Node_Id) return Traverse_Result;
12121 -- Processes a single node in the traversal procedure below, checking
12122 -- if node N should be replaced, and if so, doing the replacement.
12124 procedure Replace_Type_Refs is new Traverse_Proc (Replace_Node);
12125 -- This instantiation provides the body of Replace_Type_References
12131 function Replace_Node (N : Node_Id) return Traverse_Result is
12136 -- Case of identifier
12138 if Nkind (N) = N_Identifier then
12140 -- If not the type name, check whether it is a reference to
12141 -- some other type, which must be frozen before the predicate
12142 -- function is analyzed, i.e. before the freeze node of the
12143 -- type to which the predicate applies.
12145 if Chars (N) /= TName then
12146 if Present (Current_Entity (N))
12147 and then Is_Type (Current_Entity (N))
12149 Freeze_Before (Freeze_Node (T), Current_Entity (N));
12154 -- Otherwise do the replacement and we are done with this node
12157 Replace_Type_Reference (N);
12161 -- Case of selected component (which is what a qualification
12162 -- looks like in the unanalyzed tree, which is what we have.
12164 elsif Nkind (N) = N_Selected_Component then
12166 -- If selector name is not our type, keeping going (we might
12167 -- still have an occurrence of the type in the prefix).
12169 if Nkind (Selector_Name (N)) /= N_Identifier
12170 or else Chars (Selector_Name (N)) /= TName
12174 -- Selector name is our type, check qualification
12177 -- Loop through scopes and prefixes, doing comparison
12179 S := Current_Scope;
12182 -- Continue if no more scopes or scope with no name
12184 if No (S) or else Nkind (S) not in N_Has_Chars then
12188 -- Do replace if prefix is an identifier matching the
12189 -- scope that we are currently looking at.
12191 if Nkind (P) = N_Identifier
12192 and then Chars (P) = Chars (S)
12194 Replace_Type_Reference (N);
12198 -- Go check scope above us if prefix is itself of the
12199 -- form of a selected component, whose selector matches
12200 -- the scope we are currently looking at.
12202 if Nkind (P) = N_Selected_Component
12203 and then Nkind (Selector_Name (P)) = N_Identifier
12204 and then Chars (Selector_Name (P)) = Chars (S)
12209 -- For anything else, we don't have a match, so keep on
12210 -- going, there are still some weird cases where we may
12211 -- still have a replacement within the prefix.
12219 -- Continue for any other node kind
12227 Replace_Type_Refs (N);
12228 end Replace_Type_References_Generic;
12230 -------------------------
12231 -- Same_Representation --
12232 -------------------------
12234 function Same_Representation (Typ1, Typ2 : Entity_Id) return Boolean is
12235 T1 : constant Entity_Id := Underlying_Type (Typ1);
12236 T2 : constant Entity_Id := Underlying_Type (Typ2);
12239 -- A quick check, if base types are the same, then we definitely have
12240 -- the same representation, because the subtype specific representation
12241 -- attributes (Size and Alignment) do not affect representation from
12242 -- the point of view of this test.
12244 if Base_Type (T1) = Base_Type (T2) then
12247 elsif Is_Private_Type (Base_Type (T2))
12248 and then Base_Type (T1) = Full_View (Base_Type (T2))
12253 -- Tagged types never have differing representations
12255 if Is_Tagged_Type (T1) then
12259 -- Representations are definitely different if conventions differ
12261 if Convention (T1) /= Convention (T2) then
12265 -- Representations are different if component alignments or scalar
12266 -- storage orders differ.
12268 if (Is_Record_Type (T1) or else Is_Array_Type (T1))
12270 (Is_Record_Type (T2) or else Is_Array_Type (T2))
12272 (Component_Alignment (T1) /= Component_Alignment (T2)
12273 or else Reverse_Storage_Order (T1) /= Reverse_Storage_Order (T2))
12278 -- For arrays, the only real issue is component size. If we know the
12279 -- component size for both arrays, and it is the same, then that's
12280 -- good enough to know we don't have a change of representation.
12282 if Is_Array_Type (T1) then
12283 if Known_Component_Size (T1)
12284 and then Known_Component_Size (T2)
12285 and then Component_Size (T1) = Component_Size (T2)
12287 if VM_Target = No_VM then
12290 -- In VM targets the representation of arrays with aliased
12291 -- components differs from arrays with non-aliased components
12294 return Has_Aliased_Components (Base_Type (T1))
12296 Has_Aliased_Components (Base_Type (T2));
12301 -- Types definitely have same representation if neither has non-standard
12302 -- representation since default representations are always consistent.
12303 -- If only one has non-standard representation, and the other does not,
12304 -- then we consider that they do not have the same representation. They
12305 -- might, but there is no way of telling early enough.
12307 if Has_Non_Standard_Rep (T1) then
12308 if not Has_Non_Standard_Rep (T2) then
12312 return not Has_Non_Standard_Rep (T2);
12315 -- Here the two types both have non-standard representation, and we need
12316 -- to determine if they have the same non-standard representation.
12318 -- For arrays, we simply need to test if the component sizes are the
12319 -- same. Pragma Pack is reflected in modified component sizes, so this
12320 -- check also deals with pragma Pack.
12322 if Is_Array_Type (T1) then
12323 return Component_Size (T1) = Component_Size (T2);
12325 -- Tagged types always have the same representation, because it is not
12326 -- possible to specify different representations for common fields.
12328 elsif Is_Tagged_Type (T1) then
12331 -- Case of record types
12333 elsif Is_Record_Type (T1) then
12335 -- Packed status must conform
12337 if Is_Packed (T1) /= Is_Packed (T2) then
12340 -- Otherwise we must check components. Typ2 maybe a constrained
12341 -- subtype with fewer components, so we compare the components
12342 -- of the base types.
12345 Record_Case : declare
12346 CD1, CD2 : Entity_Id;
12348 function Same_Rep return Boolean;
12349 -- CD1 and CD2 are either components or discriminants. This
12350 -- function tests whether they have the same representation.
12356 function Same_Rep return Boolean is
12358 if No (Component_Clause (CD1)) then
12359 return No (Component_Clause (CD2));
12361 -- Note: at this point, component clauses have been
12362 -- normalized to the default bit order, so that the
12363 -- comparison of Component_Bit_Offsets is meaningful.
12366 Present (Component_Clause (CD2))
12368 Component_Bit_Offset (CD1) = Component_Bit_Offset (CD2)
12370 Esize (CD1) = Esize (CD2);
12374 -- Start of processing for Record_Case
12377 if Has_Discriminants (T1) then
12379 -- The number of discriminants may be different if the
12380 -- derived type has fewer (constrained by values). The
12381 -- invisible discriminants retain the representation of
12382 -- the original, so the discrepancy does not per se
12383 -- indicate a different representation.
12385 CD1 := First_Discriminant (T1);
12386 CD2 := First_Discriminant (T2);
12387 while Present (CD1) and then Present (CD2) loop
12388 if not Same_Rep then
12391 Next_Discriminant (CD1);
12392 Next_Discriminant (CD2);
12397 CD1 := First_Component (Underlying_Type (Base_Type (T1)));
12398 CD2 := First_Component (Underlying_Type (Base_Type (T2)));
12399 while Present (CD1) loop
12400 if not Same_Rep then
12403 Next_Component (CD1);
12404 Next_Component (CD2);
12412 -- For enumeration types, we must check each literal to see if the
12413 -- representation is the same. Note that we do not permit enumeration
12414 -- representation clauses for Character and Wide_Character, so these
12415 -- cases were already dealt with.
12417 elsif Is_Enumeration_Type (T1) then
12418 Enumeration_Case : declare
12419 L1, L2 : Entity_Id;
12422 L1 := First_Literal (T1);
12423 L2 := First_Literal (T2);
12424 while Present (L1) loop
12425 if Enumeration_Rep (L1) /= Enumeration_Rep (L2) then
12434 end Enumeration_Case;
12436 -- Any other types have the same representation for these purposes
12441 end Same_Representation;
12443 --------------------------------
12444 -- Resolve_Iterable_Operation --
12445 --------------------------------
12447 procedure Resolve_Iterable_Operation
12449 Cursor : Entity_Id;
12458 if not Is_Overloaded (N) then
12459 if not Is_Entity_Name (N)
12460 or else Ekind (Entity (N)) /= E_Function
12461 or else Scope (Entity (N)) /= Scope (Typ)
12462 or else No (First_Formal (Entity (N)))
12463 or else Etype (First_Formal (Entity (N))) /= Typ
12465 Error_Msg_N ("iterable primitive must be local function name "
12466 & "whose first formal is an iterable type", N);
12471 F1 := First_Formal (Ent);
12472 if Nam = Name_First then
12474 -- First (Container) => Cursor
12476 if Etype (Ent) /= Cursor then
12477 Error_Msg_N ("primitive for First must yield a curosr", N);
12480 elsif Nam = Name_Next then
12482 -- Next (Container, Cursor) => Cursor
12484 F2 := Next_Formal (F1);
12486 if Etype (F2) /= Cursor
12487 or else Etype (Ent) /= Cursor
12488 or else Present (Next_Formal (F2))
12490 Error_Msg_N ("no match for Next iterable primitive", N);
12493 elsif Nam = Name_Has_Element then
12495 -- Has_Element (Container, Cursor) => Boolean
12497 F2 := Next_Formal (F1);
12498 if Etype (F2) /= Cursor
12499 or else Etype (Ent) /= Standard_Boolean
12500 or else Present (Next_Formal (F2))
12502 Error_Msg_N ("no match for Has_Element iterable primitive", N);
12505 elsif Nam = Name_Element then
12506 F2 := Next_Formal (F1);
12509 or else Etype (F2) /= Cursor
12510 or else Present (Next_Formal (F2))
12512 Error_Msg_N ("no match for Element iterable primitive", N);
12517 raise Program_Error;
12521 -- Overloaded case: find subprogram with proper signature.
12522 -- Caller will report error if no match is found.
12529 Get_First_Interp (N, I, It);
12530 while Present (It.Typ) loop
12531 if Ekind (It.Nam) = E_Function
12532 and then Scope (It.Nam) = Scope (Typ)
12533 and then Etype (First_Formal (It.Nam)) = Typ
12535 F1 := First_Formal (It.Nam);
12537 if Nam = Name_First then
12538 if Etype (It.Nam) = Cursor
12539 and then No (Next_Formal (F1))
12541 Set_Entity (N, It.Nam);
12545 elsif Nam = Name_Next then
12546 F2 := Next_Formal (F1);
12549 and then No (Next_Formal (F2))
12550 and then Etype (F2) = Cursor
12551 and then Etype (It.Nam) = Cursor
12553 Set_Entity (N, It.Nam);
12557 elsif Nam = Name_Has_Element then
12558 F2 := Next_Formal (F1);
12561 and then No (Next_Formal (F2))
12562 and then Etype (F2) = Cursor
12563 and then Etype (It.Nam) = Standard_Boolean
12565 Set_Entity (N, It.Nam);
12566 F2 := Next_Formal (F1);
12570 elsif Nam = Name_Element then
12571 F2 := Next_Formal (F1);
12574 and then No (Next_Formal (F2))
12575 and then Etype (F2) = Cursor
12577 Set_Entity (N, It.Nam);
12583 Get_Next_Interp (I, It);
12587 end Resolve_Iterable_Operation;
12593 procedure Set_Biased
12597 Biased : Boolean := True)
12601 Set_Has_Biased_Representation (E);
12603 if Warn_On_Biased_Representation then
12605 ("?B?" & Msg & " forces biased representation for&", N, E);
12610 --------------------
12611 -- Set_Enum_Esize --
12612 --------------------
12614 procedure Set_Enum_Esize (T : Entity_Id) is
12620 Init_Alignment (T);
12622 -- Find the minimum standard size (8,16,32,64) that fits
12624 Lo := Enumeration_Rep (Entity (Type_Low_Bound (T)));
12625 Hi := Enumeration_Rep (Entity (Type_High_Bound (T)));
12628 if Lo >= -Uint_2**07 and then Hi < Uint_2**07 then
12629 Sz := Standard_Character_Size; -- May be > 8 on some targets
12631 elsif Lo >= -Uint_2**15 and then Hi < Uint_2**15 then
12634 elsif Lo >= -Uint_2**31 and then Hi < Uint_2**31 then
12637 else pragma Assert (Lo >= -Uint_2**63 and then Hi < Uint_2**63);
12642 if Hi < Uint_2**08 then
12643 Sz := Standard_Character_Size; -- May be > 8 on some targets
12645 elsif Hi < Uint_2**16 then
12648 elsif Hi < Uint_2**32 then
12651 else pragma Assert (Hi < Uint_2**63);
12656 -- That minimum is the proper size unless we have a foreign convention
12657 -- and the size required is 32 or less, in which case we bump the size
12658 -- up to 32. This is required for C and C++ and seems reasonable for
12659 -- all other foreign conventions.
12661 if Has_Foreign_Convention (T)
12662 and then Esize (T) < Standard_Integer_Size
12664 -- Don't do this if Short_Enums on target
12666 and then not Target_Short_Enums
12668 Init_Esize (T, Standard_Integer_Size);
12670 Init_Esize (T, Sz);
12672 end Set_Enum_Esize;
12674 -----------------------------
12675 -- Uninstall_Discriminants --
12676 -----------------------------
12678 procedure Uninstall_Discriminants (E : Entity_Id) is
12684 -- Discriminants have been made visible for type declarations and
12685 -- protected type declarations, not for subtype declarations.
12687 if Nkind (Parent (E)) /= N_Subtype_Declaration then
12688 Disc := First_Discriminant (E);
12689 while Present (Disc) loop
12690 if Disc /= Current_Entity (Disc) then
12691 Prev := Current_Entity (Disc);
12692 while Present (Prev)
12693 and then Present (Homonym (Prev))
12694 and then Homonym (Prev) /= Disc
12696 Prev := Homonym (Prev);
12702 Set_Is_Immediately_Visible (Disc, False);
12704 Outer := Homonym (Disc);
12705 while Present (Outer) and then Scope (Outer) = E loop
12706 Outer := Homonym (Outer);
12709 -- Reset homonym link of other entities, but do not modify link
12710 -- between entities in current scope, so that the back-end can
12711 -- have a proper count of local overloadings.
12714 Set_Name_Entity_Id (Chars (Disc), Outer);
12716 elsif Scope (Prev) /= Scope (Disc) then
12717 Set_Homonym (Prev, Outer);
12720 Next_Discriminant (Disc);
12723 end Uninstall_Discriminants;
12725 -------------------------------------------
12726 -- Uninstall_Discriminants_And_Pop_Scope --
12727 -------------------------------------------
12729 procedure Uninstall_Discriminants_And_Pop_Scope (E : Entity_Id) is
12731 if Has_Discriminants (E) then
12732 Uninstall_Discriminants (E);
12735 end Uninstall_Discriminants_And_Pop_Scope;
12737 ------------------------------
12738 -- Validate_Address_Clauses --
12739 ------------------------------
12741 procedure Validate_Address_Clauses is
12743 for J in Address_Clause_Checks.First .. Address_Clause_Checks.Last loop
12745 ACCR : Address_Clause_Check_Record
12746 renames Address_Clause_Checks.Table (J);
12750 X_Alignment : Uint;
12751 Y_Alignment : Uint;
12757 -- Skip processing of this entry if warning already posted
12759 if not Address_Warning_Posted (ACCR.N) then
12760 Expr := Original_Node (Expression (ACCR.N));
12764 X_Alignment := Alignment (ACCR.X);
12765 Y_Alignment := Alignment (ACCR.Y);
12767 -- Similarly obtain sizes
12769 X_Size := Esize (ACCR.X);
12770 Y_Size := Esize (ACCR.Y);
12772 -- Check for large object overlaying smaller one
12775 and then X_Size > Uint_0
12776 and then X_Size > Y_Size
12779 ("??& overlays smaller object", ACCR.N, ACCR.X);
12781 ("\??program execution may be erroneous", ACCR.N);
12782 Error_Msg_Uint_1 := X_Size;
12784 ("\??size of & is ^", ACCR.N, ACCR.X);
12785 Error_Msg_Uint_1 := Y_Size;
12787 ("\??size of & is ^", ACCR.N, ACCR.Y);
12789 -- Check for inadequate alignment, both of the base object
12790 -- and of the offset, if any. We only do this check if the
12791 -- run-time Alignment_Check is active. No point in warning
12792 -- if this check has been suppressed (or is suppressed by
12793 -- default in the non-strict alignment machine case).
12795 -- Note: we do not check the alignment if we gave a size
12796 -- warning, since it would likely be redundant.
12798 elsif not Alignment_Checks_Suppressed (ACCR.Y)
12799 and then Y_Alignment /= Uint_0
12800 and then (Y_Alignment < X_Alignment
12803 Nkind (Expr) = N_Attribute_Reference
12805 Attribute_Name (Expr) = Name_Address
12807 Has_Compatible_Alignment
12808 (ACCR.X, Prefix (Expr))
12809 /= Known_Compatible))
12812 ("??specified address for& may be inconsistent "
12813 & "with alignment", ACCR.N, ACCR.X);
12815 ("\??program execution may be erroneous (RM 13.3(27))",
12817 Error_Msg_Uint_1 := X_Alignment;
12819 ("\??alignment of & is ^", ACCR.N, ACCR.X);
12820 Error_Msg_Uint_1 := Y_Alignment;
12822 ("\??alignment of & is ^", ACCR.N, ACCR.Y);
12823 if Y_Alignment >= X_Alignment then
12825 ("\??but offset is not multiple of alignment", ACCR.N);
12831 end Validate_Address_Clauses;
12833 ---------------------------
12834 -- Validate_Independence --
12835 ---------------------------
12837 procedure Validate_Independence is
12838 SU : constant Uint := UI_From_Int (System_Storage_Unit);
12846 procedure Check_Array_Type (Atyp : Entity_Id);
12847 -- Checks if the array type Atyp has independent components, and
12848 -- if not, outputs an appropriate set of error messages.
12850 procedure No_Independence;
12851 -- Output message that independence cannot be guaranteed
12853 function OK_Component (C : Entity_Id) return Boolean;
12854 -- Checks one component to see if it is independently accessible, and
12855 -- if so yields True, otherwise yields False if independent access
12856 -- cannot be guaranteed. This is a conservative routine, it only
12857 -- returns True if it knows for sure, it returns False if it knows
12858 -- there is a problem, or it cannot be sure there is no problem.
12860 procedure Reason_Bad_Component (C : Entity_Id);
12861 -- Outputs continuation message if a reason can be determined for
12862 -- the component C being bad.
12864 ----------------------
12865 -- Check_Array_Type --
12866 ----------------------
12868 procedure Check_Array_Type (Atyp : Entity_Id) is
12869 Ctyp : constant Entity_Id := Component_Type (Atyp);
12872 -- OK if no alignment clause, no pack, and no component size
12874 if not Has_Component_Size_Clause (Atyp)
12875 and then not Has_Alignment_Clause (Atyp)
12876 and then not Is_Packed (Atyp)
12881 -- Case of component size is greater than or equal to 64 and the
12882 -- alignment of the array is at least as large as the alignment
12883 -- of the component. We are definitely OK in this situation.
12885 if Known_Component_Size (Atyp)
12886 and then Component_Size (Atyp) >= 64
12887 and then Known_Alignment (Atyp)
12888 and then Known_Alignment (Ctyp)
12889 and then Alignment (Atyp) >= Alignment (Ctyp)
12894 -- Check actual component size
12896 if not Known_Component_Size (Atyp)
12897 or else not (Addressable (Component_Size (Atyp))
12898 and then Component_Size (Atyp) < 64)
12899 or else Component_Size (Atyp) mod Esize (Ctyp) /= 0
12903 -- Bad component size, check reason
12905 if Has_Component_Size_Clause (Atyp) then
12906 P := Get_Attribute_Definition_Clause
12907 (Atyp, Attribute_Component_Size);
12909 if Present (P) then
12910 Error_Msg_Sloc := Sloc (P);
12911 Error_Msg_N ("\because of Component_Size clause#", N);
12916 if Is_Packed (Atyp) then
12917 P := Get_Rep_Pragma (Atyp, Name_Pack);
12919 if Present (P) then
12920 Error_Msg_Sloc := Sloc (P);
12921 Error_Msg_N ("\because of pragma Pack#", N);
12926 -- No reason found, just return
12931 -- Array type is OK independence-wise
12934 end Check_Array_Type;
12936 ---------------------
12937 -- No_Independence --
12938 ---------------------
12940 procedure No_Independence is
12942 if Pragma_Name (N) = Name_Independent then
12943 Error_Msg_NE ("independence cannot be guaranteed for&", N, E);
12946 ("independent components cannot be guaranteed for&", N, E);
12948 end No_Independence;
12954 function OK_Component (C : Entity_Id) return Boolean is
12955 Rec : constant Entity_Id := Scope (C);
12956 Ctyp : constant Entity_Id := Etype (C);
12959 -- OK if no component clause, no Pack, and no alignment clause
12961 if No (Component_Clause (C))
12962 and then not Is_Packed (Rec)
12963 and then not Has_Alignment_Clause (Rec)
12968 -- Here we look at the actual component layout. A component is
12969 -- addressable if its size is a multiple of the Esize of the
12970 -- component type, and its starting position in the record has
12971 -- appropriate alignment, and the record itself has appropriate
12972 -- alignment to guarantee the component alignment.
12974 -- Make sure sizes are static, always assume the worst for any
12975 -- cases where we cannot check static values.
12977 if not (Known_Static_Esize (C)
12979 Known_Static_Esize (Ctyp))
12984 -- Size of component must be addressable or greater than 64 bits
12985 -- and a multiple of bytes.
12987 if not Addressable (Esize (C)) and then Esize (C) < Uint_64 then
12991 -- Check size is proper multiple
12993 if Esize (C) mod Esize (Ctyp) /= 0 then
12997 -- Check alignment of component is OK
12999 if not Known_Component_Bit_Offset (C)
13000 or else Component_Bit_Offset (C) < Uint_0
13001 or else Component_Bit_Offset (C) mod Esize (Ctyp) /= 0
13006 -- Check alignment of record type is OK
13008 if not Known_Alignment (Rec)
13009 or else (Alignment (Rec) * SU) mod Esize (Ctyp) /= 0
13014 -- All tests passed, component is addressable
13019 --------------------------
13020 -- Reason_Bad_Component --
13021 --------------------------
13023 procedure Reason_Bad_Component (C : Entity_Id) is
13024 Rec : constant Entity_Id := Scope (C);
13025 Ctyp : constant Entity_Id := Etype (C);
13028 -- If component clause present assume that's the problem
13030 if Present (Component_Clause (C)) then
13031 Error_Msg_Sloc := Sloc (Component_Clause (C));
13032 Error_Msg_N ("\because of Component_Clause#", N);
13036 -- If pragma Pack clause present, assume that's the problem
13038 if Is_Packed (Rec) then
13039 P := Get_Rep_Pragma (Rec, Name_Pack);
13041 if Present (P) then
13042 Error_Msg_Sloc := Sloc (P);
13043 Error_Msg_N ("\because of pragma Pack#", N);
13048 -- See if record has bad alignment clause
13050 if Has_Alignment_Clause (Rec)
13051 and then Known_Alignment (Rec)
13052 and then (Alignment (Rec) * SU) mod Esize (Ctyp) /= 0
13054 P := Get_Attribute_Definition_Clause (Rec, Attribute_Alignment);
13056 if Present (P) then
13057 Error_Msg_Sloc := Sloc (P);
13058 Error_Msg_N ("\because of Alignment clause#", N);
13062 -- Couldn't find a reason, so return without a message
13065 end Reason_Bad_Component;
13067 -- Start of processing for Validate_Independence
13070 for J in Independence_Checks.First .. Independence_Checks.Last loop
13071 N := Independence_Checks.Table (J).N;
13072 E := Independence_Checks.Table (J).E;
13073 IC := Pragma_Name (N) = Name_Independent_Components;
13075 -- Deal with component case
13077 if Ekind (E) = E_Discriminant or else Ekind (E) = E_Component then
13078 if not OK_Component (E) then
13080 Reason_Bad_Component (E);
13085 -- Deal with record with Independent_Components
13087 if IC and then Is_Record_Type (E) then
13088 Comp := First_Component_Or_Discriminant (E);
13089 while Present (Comp) loop
13090 if not OK_Component (Comp) then
13092 Reason_Bad_Component (Comp);
13096 Next_Component_Or_Discriminant (Comp);
13100 -- Deal with address clause case
13102 if Is_Object (E) then
13103 Addr := Address_Clause (E);
13105 if Present (Addr) then
13107 Error_Msg_Sloc := Sloc (Addr);
13108 Error_Msg_N ("\because of Address clause#", N);
13113 -- Deal with independent components for array type
13115 if IC and then Is_Array_Type (E) then
13116 Check_Array_Type (E);
13119 -- Deal with independent components for array object
13121 if IC and then Is_Object (E) and then Is_Array_Type (Etype (E)) then
13122 Check_Array_Type (Etype (E));
13127 end Validate_Independence;
13129 ------------------------------
13130 -- Validate_Iterable_Aspect --
13131 ------------------------------
13133 procedure Validate_Iterable_Aspect (Typ : Entity_Id; ASN : Node_Id) is
13138 Cursor : constant Entity_Id := Get_Cursor_Type (ASN, Typ);
13140 First_Id : Entity_Id;
13141 Next_Id : Entity_Id;
13142 Has_Element_Id : Entity_Id;
13143 Element_Id : Entity_Id;
13146 -- If previous error aspect is unusable
13148 if Cursor = Any_Type then
13154 Has_Element_Id := Empty;
13155 Element_Id := Empty;
13157 -- Each expression must resolve to a function with the proper signature
13159 Assoc := First (Component_Associations (Expression (ASN)));
13160 while Present (Assoc) loop
13161 Expr := Expression (Assoc);
13164 Prim := First (Choices (Assoc));
13166 if Nkind (Prim) /= N_Identifier or else Present (Next (Prim)) then
13167 Error_Msg_N ("illegal name in association", Prim);
13169 elsif Chars (Prim) = Name_First then
13170 Resolve_Iterable_Operation (Expr, Cursor, Typ, Name_First);
13171 First_Id := Entity (Expr);
13173 elsif Chars (Prim) = Name_Next then
13174 Resolve_Iterable_Operation (Expr, Cursor, Typ, Name_Next);
13175 Next_Id := Entity (Expr);
13177 elsif Chars (Prim) = Name_Has_Element then
13178 Resolve_Iterable_Operation (Expr, Cursor, Typ, Name_Has_Element);
13179 Has_Element_Id := Entity (Expr);
13181 elsif Chars (Prim) = Name_Element then
13182 Resolve_Iterable_Operation (Expr, Cursor, Typ, Name_Element);
13183 Element_Id := Entity (Expr);
13186 Error_Msg_N ("invalid name for iterable function", Prim);
13192 if No (First_Id) then
13193 Error_Msg_N ("match for First primitive not found", ASN);
13195 elsif No (Next_Id) then
13196 Error_Msg_N ("match for Next primitive not found", ASN);
13198 elsif No (Has_Element_Id) then
13199 Error_Msg_N ("match for Has_Element primitive not found", ASN);
13201 elsif No (Element_Id) then
13204 end Validate_Iterable_Aspect;
13206 -----------------------------------
13207 -- Validate_Unchecked_Conversion --
13208 -----------------------------------
13210 procedure Validate_Unchecked_Conversion
13212 Act_Unit : Entity_Id)
13214 Source : Entity_Id;
13215 Target : Entity_Id;
13219 -- Obtain source and target types. Note that we call Ancestor_Subtype
13220 -- here because the processing for generic instantiation always makes
13221 -- subtypes, and we want the original frozen actual types.
13223 -- If we are dealing with private types, then do the check on their
13224 -- fully declared counterparts if the full declarations have been
13225 -- encountered (they don't have to be visible, but they must exist).
13227 Source := Ancestor_Subtype (Etype (First_Formal (Act_Unit)));
13229 if Is_Private_Type (Source)
13230 and then Present (Underlying_Type (Source))
13232 Source := Underlying_Type (Source);
13235 Target := Ancestor_Subtype (Etype (Act_Unit));
13237 -- If either type is generic, the instantiation happens within a generic
13238 -- unit, and there is nothing to check. The proper check will happen
13239 -- when the enclosing generic is instantiated.
13241 if Is_Generic_Type (Source) or else Is_Generic_Type (Target) then
13245 if Is_Private_Type (Target)
13246 and then Present (Underlying_Type (Target))
13248 Target := Underlying_Type (Target);
13251 -- Source may be unconstrained array, but not target
13253 if Is_Array_Type (Target) and then not Is_Constrained (Target) then
13255 ("unchecked conversion to unconstrained array not allowed", N);
13259 -- Warn if conversion between two different convention pointers
13261 if Is_Access_Type (Target)
13262 and then Is_Access_Type (Source)
13263 and then Convention (Target) /= Convention (Source)
13264 and then Warn_On_Unchecked_Conversion
13266 -- Give warnings for subprogram pointers only on most targets
13268 if Is_Access_Subprogram_Type (Target)
13269 or else Is_Access_Subprogram_Type (Source)
13272 ("?z?conversion between pointers with different conventions!",
13277 -- Warn if one of the operands is Ada.Calendar.Time. Do not emit a
13278 -- warning when compiling GNAT-related sources.
13280 if Warn_On_Unchecked_Conversion
13281 and then not In_Predefined_Unit (N)
13282 and then RTU_Loaded (Ada_Calendar)
13283 and then (Chars (Source) = Name_Time
13285 Chars (Target) = Name_Time)
13287 -- If Ada.Calendar is loaded and the name of one of the operands is
13288 -- Time, there is a good chance that this is Ada.Calendar.Time.
13291 Calendar_Time : constant Entity_Id := Full_View (RTE (RO_CA_Time));
13293 pragma Assert (Present (Calendar_Time));
13295 if Source = Calendar_Time or else Target = Calendar_Time then
13297 ("?z?representation of 'Time values may change between "
13298 & "'G'N'A'T versions", N);
13303 -- Make entry in unchecked conversion table for later processing by
13304 -- Validate_Unchecked_Conversions, which will check sizes and alignments
13305 -- (using values set by the back-end where possible). This is only done
13306 -- if the appropriate warning is active.
13308 if Warn_On_Unchecked_Conversion then
13309 Unchecked_Conversions.Append
13310 (New_Val => UC_Entry'(Eloc => Sloc (N),
13313 Act_Unit => Act_Unit));
13315 -- If both sizes are known statically now, then back end annotation
13316 -- is not required to do a proper check but if either size is not
13317 -- known statically, then we need the annotation.
13319 if Known_Static_RM_Size (Source)
13321 Known_Static_RM_Size (Target)
13325 Back_Annotate_Rep_Info := True;
13329 -- If unchecked conversion to access type, and access type is declared
13330 -- in the same unit as the unchecked conversion, then set the flag
13331 -- No_Strict_Aliasing (no strict aliasing is implicit here)
13333 if Is_Access_Type (Target) and then
13334 In_Same_Source_Unit (Target, N)
13336 Set_No_Strict_Aliasing (Implementation_Base_Type (Target));
13339 -- Generate N_Validate_Unchecked_Conversion node for back end in case
13340 -- the back end needs to perform special validation checks.
13342 -- Shouldn't this be in Exp_Ch13, since the check only gets done if we
13343 -- have full expansion and the back end is called ???
13346 Make_Validate_Unchecked_Conversion (Sloc (N));
13347 Set_Source_Type (Vnode, Source);
13348 Set_Target_Type (Vnode, Target);
13350 -- If the unchecked conversion node is in a list, just insert before it.
13351 -- If not we have some strange case, not worth bothering about.
13353 if Is_List_Member (N) then
13354 Insert_After (N, Vnode);
13356 end Validate_Unchecked_Conversion;
13358 ------------------------------------
13359 -- Validate_Unchecked_Conversions --
13360 ------------------------------------
13362 procedure Validate_Unchecked_Conversions is
13364 for N in Unchecked_Conversions.First .. Unchecked_Conversions.Last loop
13366 T : UC_Entry renames Unchecked_Conversions.Table (N);
13368 Eloc : constant Source_Ptr := T.Eloc;
13369 Source : constant Entity_Id := T.Source;
13370 Target : constant Entity_Id := T.Target;
13371 Act_Unit : constant Entity_Id := T.Act_Unit;
13377 -- Skip if function marked as warnings off
13379 if Warnings_Off (Act_Unit) then
13383 -- This validation check, which warns if we have unequal sizes for
13384 -- unchecked conversion, and thus potentially implementation
13385 -- dependent semantics, is one of the few occasions on which we
13386 -- use the official RM size instead of Esize. See description in
13387 -- Einfo "Handling of Type'Size Values" for details.
13389 if Serious_Errors_Detected = 0
13390 and then Known_Static_RM_Size (Source)
13391 and then Known_Static_RM_Size (Target)
13393 -- Don't do the check if warnings off for either type, note the
13394 -- deliberate use of OR here instead of OR ELSE to get the flag
13395 -- Warnings_Off_Used set for both types if appropriate.
13397 and then not (Has_Warnings_Off (Source)
13399 Has_Warnings_Off (Target))
13401 Source_Siz := RM_Size (Source);
13402 Target_Siz := RM_Size (Target);
13404 if Source_Siz /= Target_Siz then
13406 ("?z?types for unchecked conversion have different sizes!",
13409 if All_Errors_Mode then
13410 Error_Msg_Name_1 := Chars (Source);
13411 Error_Msg_Uint_1 := Source_Siz;
13412 Error_Msg_Name_2 := Chars (Target);
13413 Error_Msg_Uint_2 := Target_Siz;
13414 Error_Msg ("\size of % is ^, size of % is ^?z?", Eloc);
13416 Error_Msg_Uint_1 := UI_Abs (Source_Siz - Target_Siz);
13418 if Is_Discrete_Type (Source)
13420 Is_Discrete_Type (Target)
13422 if Source_Siz > Target_Siz then
13424 ("\?z?^ high order bits of source will "
13425 & "be ignored!", Eloc);
13427 elsif Is_Unsigned_Type (Source) then
13429 ("\?z?source will be extended with ^ high order "
13430 & "zero bits!", Eloc);
13434 ("\?z?source will be extended with ^ high order "
13435 & "sign bits!", Eloc);
13438 elsif Source_Siz < Target_Siz then
13439 if Is_Discrete_Type (Target) then
13440 if Bytes_Big_Endian then
13442 ("\?z?target value will include ^ undefined "
13443 & "low order bits!", Eloc);
13446 ("\?z?target value will include ^ undefined "
13447 & "high order bits!", Eloc);
13452 ("\?z?^ trailing bits of target value will be "
13453 & "undefined!", Eloc);
13456 else pragma Assert (Source_Siz > Target_Siz);
13458 ("\?z?^ trailing bits of source will be ignored!",
13465 -- If both types are access types, we need to check the alignment.
13466 -- If the alignment of both is specified, we can do it here.
13468 if Serious_Errors_Detected = 0
13469 and then Is_Access_Type (Source)
13470 and then Is_Access_Type (Target)
13471 and then Target_Strict_Alignment
13472 and then Present (Designated_Type (Source))
13473 and then Present (Designated_Type (Target))
13476 D_Source : constant Entity_Id := Designated_Type (Source);
13477 D_Target : constant Entity_Id := Designated_Type (Target);
13480 if Known_Alignment (D_Source)
13482 Known_Alignment (D_Target)
13485 Source_Align : constant Uint := Alignment (D_Source);
13486 Target_Align : constant Uint := Alignment (D_Target);
13489 if Source_Align < Target_Align
13490 and then not Is_Tagged_Type (D_Source)
13492 -- Suppress warning if warnings suppressed on either
13493 -- type or either designated type. Note the use of
13494 -- OR here instead of OR ELSE. That is intentional,
13495 -- we would like to set flag Warnings_Off_Used in
13496 -- all types for which warnings are suppressed.
13498 and then not (Has_Warnings_Off (D_Source)
13500 Has_Warnings_Off (D_Target)
13502 Has_Warnings_Off (Source)
13504 Has_Warnings_Off (Target))
13506 Error_Msg_Uint_1 := Target_Align;
13507 Error_Msg_Uint_2 := Source_Align;
13508 Error_Msg_Node_1 := D_Target;
13509 Error_Msg_Node_2 := D_Source;
13511 ("?z?alignment of & (^) is stricter than "
13512 & "alignment of & (^)!", Eloc);
13514 ("\?z?resulting access value may have invalid "
13515 & "alignment!", Eloc);
13526 end Validate_Unchecked_Conversions;