1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2013, Free Software Foundation, Inc. --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING3. If not, go to --
19 -- http://www.gnu.org/licenses for a complete copy of the license. --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
24 ------------------------------------------------------------------------------
26 with Aspects; use Aspects;
27 with Atree; use Atree;
28 with Checks; use Checks;
29 with Debug; use Debug;
30 with Einfo; use Einfo;
31 with Elists; use Elists;
32 with Errout; use Errout;
33 with Exp_Disp; use Exp_Disp;
34 with Exp_Tss; use Exp_Tss;
35 with Exp_Util; use Exp_Util;
37 with Lib.Xref; use Lib.Xref;
38 with Namet; use Namet;
39 with Nlists; use Nlists;
40 with Nmake; use Nmake;
42 with Restrict; use Restrict;
43 with Rident; use Rident;
44 with Rtsfind; use Rtsfind;
46 with Sem_Aux; use Sem_Aux;
47 with Sem_Case; use Sem_Case;
48 with Sem_Ch3; use Sem_Ch3;
49 with Sem_Ch6; use Sem_Ch6;
50 with Sem_Ch8; use Sem_Ch8;
51 with Sem_Ch9; use Sem_Ch9;
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_Predicate_Functions (Typ : Entity_Id; N : Node_Id);
89 -- If Typ has predicates (indicated by Has_Predicates being set for Typ,
90 -- then either there are pragma Predicate entries on the rep chain for the
91 -- type (note that Predicate aspects are converted to pragma Predicate), or
92 -- there are inherited aspects from a parent type, or ancestor subtypes.
93 -- This procedure builds the spec and body for the Predicate function that
94 -- tests these predicates. N is the freeze node for the type. The spec of
95 -- the function is inserted before the freeze node, and the body of the
96 -- function is inserted after the freeze node. If the predicate expression
97 -- has at least one Raise_Expression, then this procedure also builds the
98 -- M version of the predicate function for use in membership tests.
100 procedure Build_Static_Predicate
104 -- Given a predicated type Typ, where Typ is a discrete static subtype,
105 -- whose predicate expression is Expr, tests if Expr is a static predicate,
106 -- and if so, builds the predicate range list. Nam is the name of the one
107 -- argument to the predicate function. Occurrences of the type name in the
108 -- predicate expression have been replaced by identifier references to this
109 -- name, which is unique, so any identifier with Chars matching Nam must be
110 -- a reference to the type. If the predicate is non-static, this procedure
111 -- returns doing nothing. If the predicate is static, then the predicate
112 -- list is stored in Static_Predicate (Typ), and the Expr is rewritten as
113 -- a canonicalized membership operation.
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 procedure New_Stream_Subprogram
141 Nam : TSS_Name_Type);
142 -- Create a subprogram renaming of a given stream attribute to the
143 -- designated subprogram and then in the tagged case, provide this as a
144 -- primitive operation, or in the non-tagged case make an appropriate TSS
145 -- entry. This is more properly an expansion activity than just semantics,
146 -- but the presence of user-defined stream functions for limited types is a
147 -- legality check, which is why this takes place here rather than in
148 -- exp_ch13, where it was previously. Nam indicates the name of the TSS
149 -- function to be generated.
151 -- To avoid elaboration anomalies with freeze nodes, for untagged types
152 -- we generate both a subprogram declaration and a subprogram renaming
153 -- declaration, so that the attribute specification is handled as a
154 -- renaming_as_body. For tagged types, the specification is one of the
158 with procedure Replace_Type_Reference (N : Node_Id);
159 procedure Replace_Type_References_Generic (N : Node_Id; TName : Name_Id);
160 -- This is used to scan an expression for a predicate or invariant aspect
161 -- replacing occurrences of the name TName (the name of the subtype to
162 -- which the aspect applies) with appropriate references to the parameter
163 -- of the predicate function or invariant procedure. The procedure passed
164 -- as a generic parameter does the actual replacement of node N, which is
165 -- either a simple direct reference to TName, or a selected component that
166 -- represents an appropriately qualified occurrence of TName.
172 Biased : Boolean := True);
173 -- If Biased is True, sets Has_Biased_Representation flag for E, and
174 -- outputs a warning message at node N if Warn_On_Biased_Representation is
175 -- is True. This warning inserts the string Msg to describe the construct
178 ----------------------------------------------
179 -- Table for Validate_Unchecked_Conversions --
180 ----------------------------------------------
182 -- The following table collects unchecked conversions for validation.
183 -- Entries are made by Validate_Unchecked_Conversion and then the call
184 -- to Validate_Unchecked_Conversions does the actual error checking and
185 -- posting of warnings. The reason for this delayed processing is to take
186 -- advantage of back-annotations of size and alignment values performed by
189 -- Note: the reason we store a Source_Ptr value instead of a Node_Id is
190 -- that by the time Validate_Unchecked_Conversions is called, Sprint will
191 -- already have modified all Sloc values if the -gnatD option is set.
193 type UC_Entry is record
194 Eloc : Source_Ptr; -- node used for posting warnings
195 Source : Entity_Id; -- source type for unchecked conversion
196 Target : Entity_Id; -- target type for unchecked conversion
199 package Unchecked_Conversions is new Table.Table (
200 Table_Component_Type => UC_Entry,
201 Table_Index_Type => Int,
202 Table_Low_Bound => 1,
204 Table_Increment => 200,
205 Table_Name => "Unchecked_Conversions");
207 ----------------------------------------
208 -- Table for Validate_Address_Clauses --
209 ----------------------------------------
211 -- If an address clause has the form
213 -- for X'Address use Expr
215 -- where Expr is of the form Y'Address or recursively is a reference to a
216 -- constant of either of these forms, and X and Y are entities of objects,
217 -- then if Y has a smaller alignment than X, that merits a warning about
218 -- possible bad alignment. The following table collects address clauses of
219 -- this kind. We put these in a table so that they can be checked after the
220 -- back end has completed annotation of the alignments of objects, since we
221 -- can catch more cases that way.
223 type Address_Clause_Check_Record is record
225 -- The address clause
228 -- The entity of the object overlaying Y
231 -- The entity of the object being overlaid
234 -- Whether the address is offset within Y
237 package Address_Clause_Checks is new Table.Table (
238 Table_Component_Type => Address_Clause_Check_Record,
239 Table_Index_Type => Int,
240 Table_Low_Bound => 1,
242 Table_Increment => 200,
243 Table_Name => "Address_Clause_Checks");
245 -----------------------------------------
246 -- Adjust_Record_For_Reverse_Bit_Order --
247 -----------------------------------------
249 procedure Adjust_Record_For_Reverse_Bit_Order (R : Entity_Id) is
254 -- Processing depends on version of Ada
256 -- For Ada 95, we just renumber bits within a storage unit. We do the
257 -- same for Ada 83 mode, since we recognize the Bit_Order attribute in
258 -- Ada 83, and are free to add this extension.
260 if Ada_Version < Ada_2005 then
261 Comp := First_Component_Or_Discriminant (R);
262 while Present (Comp) loop
263 CC := Component_Clause (Comp);
265 -- If component clause is present, then deal with the non-default
266 -- bit order case for Ada 95 mode.
268 -- We only do this processing for the base type, and in fact that
269 -- is important, since otherwise if there are record subtypes, we
270 -- could reverse the bits once for each subtype, which is wrong.
272 if Present (CC) and then Ekind (R) = E_Record_Type then
274 CFB : constant Uint := Component_Bit_Offset (Comp);
275 CSZ : constant Uint := Esize (Comp);
276 CLC : constant Node_Id := Component_Clause (Comp);
277 Pos : constant Node_Id := Position (CLC);
278 FB : constant Node_Id := First_Bit (CLC);
280 Storage_Unit_Offset : constant Uint :=
281 CFB / System_Storage_Unit;
283 Start_Bit : constant Uint :=
284 CFB mod System_Storage_Unit;
287 -- Cases where field goes over storage unit boundary
289 if Start_Bit + CSZ > System_Storage_Unit then
291 -- Allow multi-byte field but generate warning
293 if Start_Bit mod System_Storage_Unit = 0
294 and then CSZ mod System_Storage_Unit = 0
297 ("multi-byte field specified with non-standard"
298 & " Bit_Order??", CLC);
300 if Bytes_Big_Endian then
302 ("bytes are not reversed "
303 & "(component is big-endian)??", CLC);
306 ("bytes are not reversed "
307 & "(component is little-endian)??", CLC);
310 -- Do not allow non-contiguous field
314 ("attempt to specify non-contiguous field "
315 & "not permitted", CLC);
317 ("\caused by non-standard Bit_Order "
320 ("\consider possibility of using "
321 & "Ada 2005 mode here", CLC);
324 -- Case where field fits in one storage unit
327 -- Give warning if suspicious component clause
329 if Intval (FB) >= System_Storage_Unit
330 and then Warn_On_Reverse_Bit_Order
333 ("Bit_Order clause does not affect " &
334 "byte ordering?V?", Pos);
336 Intval (Pos) + Intval (FB) /
339 ("position normalized to ^ before bit " &
340 "order interpreted?V?", Pos);
343 -- Here is where we fix up the Component_Bit_Offset value
344 -- to account for the reverse bit order. Some examples of
345 -- what needs to be done are:
347 -- First_Bit .. Last_Bit Component_Bit_Offset
359 -- The rule is that the first bit is is obtained by
360 -- subtracting the old ending bit from storage_unit - 1.
362 Set_Component_Bit_Offset
364 (Storage_Unit_Offset * System_Storage_Unit) +
365 (System_Storage_Unit - 1) -
366 (Start_Bit + CSZ - 1));
368 Set_Normalized_First_Bit
370 Component_Bit_Offset (Comp) mod
371 System_Storage_Unit);
376 Next_Component_Or_Discriminant (Comp);
379 -- For Ada 2005, we do machine scalar processing, as fully described In
380 -- AI-133. This involves gathering all components which start at the
381 -- same byte offset and processing them together. Same approach is still
382 -- valid in later versions including Ada 2012.
386 Max_Machine_Scalar_Size : constant Uint :=
388 (Standard_Long_Long_Integer_Size);
389 -- We use this as the maximum machine scalar size
392 SSU : constant Uint := UI_From_Int (System_Storage_Unit);
395 -- This first loop through components does two things. First it
396 -- deals with the case of components with component clauses whose
397 -- length is greater than the maximum machine scalar size (either
398 -- accepting them or rejecting as needed). Second, it counts the
399 -- number of components with component clauses whose length does
400 -- not exceed this maximum for later processing.
403 Comp := First_Component_Or_Discriminant (R);
404 while Present (Comp) loop
405 CC := Component_Clause (Comp);
409 Fbit : constant Uint := Static_Integer (First_Bit (CC));
410 Lbit : constant Uint := Static_Integer (Last_Bit (CC));
413 -- Case of component with last bit >= max machine scalar
415 if Lbit >= Max_Machine_Scalar_Size then
417 -- This is allowed only if first bit is zero, and
418 -- last bit + 1 is a multiple of storage unit size.
420 if Fbit = 0 and then (Lbit + 1) mod SSU = 0 then
422 -- This is the case to give a warning if enabled
424 if Warn_On_Reverse_Bit_Order then
426 ("multi-byte field specified with "
427 & " non-standard Bit_Order?V?", CC);
429 if Bytes_Big_Endian then
431 ("\bytes are not reversed "
432 & "(component is big-endian)?V?", CC);
435 ("\bytes are not reversed "
436 & "(component is little-endian)?V?", CC);
440 -- Give error message for RM 13.5.1(10) violation
444 ("machine scalar rules not followed for&",
445 First_Bit (CC), Comp);
447 Error_Msg_Uint_1 := Lbit;
448 Error_Msg_Uint_2 := Max_Machine_Scalar_Size;
450 ("\last bit (^) exceeds maximum machine "
454 if (Lbit + 1) mod SSU /= 0 then
455 Error_Msg_Uint_1 := SSU;
457 ("\and is not a multiple of Storage_Unit (^) "
462 Error_Msg_Uint_1 := Fbit;
464 ("\and first bit (^) is non-zero "
470 -- OK case of machine scalar related component clause,
471 -- For now, just count them.
474 Num_CC := Num_CC + 1;
479 Next_Component_Or_Discriminant (Comp);
482 -- We need to sort the component clauses on the basis of the
483 -- Position values in the clause, so we can group clauses with
484 -- the same Position. together to determine the relevant machine
488 Comps : array (0 .. Num_CC) of Entity_Id;
489 -- Array to collect component and discriminant entities. The
490 -- data starts at index 1, the 0'th entry is for the sort
493 function CP_Lt (Op1, Op2 : Natural) return Boolean;
494 -- Compare routine for Sort
496 procedure CP_Move (From : Natural; To : Natural);
497 -- Move routine for Sort
499 package Sorting is new GNAT.Heap_Sort_G (CP_Move, CP_Lt);
503 -- Start and stop positions in the component list of the set of
504 -- components with the same starting position (that constitute
505 -- components in a single machine scalar).
508 -- Maximum last bit value of any component in this set
511 -- Corresponding machine scalar size
517 function CP_Lt (Op1, Op2 : Natural) return Boolean is
519 return Position (Component_Clause (Comps (Op1))) <
520 Position (Component_Clause (Comps (Op2)));
527 procedure CP_Move (From : Natural; To : Natural) is
529 Comps (To) := Comps (From);
532 -- Start of processing for Sort_CC
535 -- Collect the machine scalar relevant component clauses
538 Comp := First_Component_Or_Discriminant (R);
539 while Present (Comp) loop
541 CC : constant Node_Id := Component_Clause (Comp);
544 -- Collect only component clauses whose last bit is less
545 -- than machine scalar size. Any component clause whose
546 -- last bit exceeds this value does not take part in
547 -- machine scalar layout considerations. The test for
548 -- Error_Posted makes sure we exclude component clauses
549 -- for which we already posted an error.
552 and then not Error_Posted (Last_Bit (CC))
553 and then Static_Integer (Last_Bit (CC)) <
554 Max_Machine_Scalar_Size
556 Num_CC := Num_CC + 1;
557 Comps (Num_CC) := Comp;
561 Next_Component_Or_Discriminant (Comp);
564 -- Sort by ascending position number
566 Sorting.Sort (Num_CC);
568 -- We now have all the components whose size does not exceed
569 -- the max machine scalar value, sorted by starting position.
570 -- In this loop we gather groups of clauses starting at the
571 -- same position, to process them in accordance with AI-133.
574 while Stop < Num_CC loop
579 (Last_Bit (Component_Clause (Comps (Start))));
580 while Stop < Num_CC loop
582 (Position (Component_Clause (Comps (Stop + 1)))) =
584 (Position (Component_Clause (Comps (Stop))))
592 (Component_Clause (Comps (Stop)))));
598 -- Now we have a group of component clauses from Start to
599 -- Stop whose positions are identical, and MaxL is the
600 -- maximum last bit value of any of these components.
602 -- We need to determine the corresponding machine scalar
603 -- size. This loop assumes that machine scalar sizes are
604 -- even, and that each possible machine scalar has twice
605 -- as many bits as the next smaller one.
607 MSS := Max_Machine_Scalar_Size;
609 and then (MSS / 2) >= SSU
610 and then (MSS / 2) > MaxL
615 -- Here is where we fix up the Component_Bit_Offset value
616 -- to account for the reverse bit order. Some examples of
617 -- what needs to be done for the case of a machine scalar
620 -- First_Bit .. Last_Bit Component_Bit_Offset
632 -- The rule is that the first bit is obtained by subtracting
633 -- the old ending bit from machine scalar size - 1.
635 for C in Start .. Stop loop
637 Comp : constant Entity_Id := Comps (C);
638 CC : constant Node_Id := Component_Clause (Comp);
640 LB : constant Uint := Static_Integer (Last_Bit (CC));
641 NFB : constant Uint := MSS - Uint_1 - LB;
642 NLB : constant Uint := NFB + Esize (Comp) - 1;
643 Pos : constant Uint := Static_Integer (Position (CC));
646 if Warn_On_Reverse_Bit_Order then
647 Error_Msg_Uint_1 := MSS;
649 ("info: reverse bit order in machine " &
650 "scalar of length^?V?", First_Bit (CC));
651 Error_Msg_Uint_1 := NFB;
652 Error_Msg_Uint_2 := NLB;
654 if Bytes_Big_Endian then
656 ("\info: big-endian range for "
657 & "component & is ^ .. ^?V?",
658 First_Bit (CC), Comp);
661 ("\info: little-endian range "
662 & "for component & is ^ .. ^?V?",
663 First_Bit (CC), Comp);
667 Set_Component_Bit_Offset (Comp, Pos * SSU + NFB);
668 Set_Normalized_First_Bit (Comp, NFB mod SSU);
675 end Adjust_Record_For_Reverse_Bit_Order;
677 -------------------------------------
678 -- Alignment_Check_For_Size_Change --
679 -------------------------------------
681 procedure Alignment_Check_For_Size_Change (Typ : Entity_Id; Size : Uint) is
683 -- If the alignment is known, and not set by a rep clause, and is
684 -- inconsistent with the size being set, then reset it to unknown,
685 -- we assume in this case that the size overrides the inherited
686 -- alignment, and that the alignment must be recomputed.
688 if Known_Alignment (Typ)
689 and then not Has_Alignment_Clause (Typ)
690 and then Size mod (Alignment (Typ) * SSU) /= 0
692 Init_Alignment (Typ);
694 end Alignment_Check_For_Size_Change;
696 -------------------------------------
697 -- Analyze_Aspects_At_Freeze_Point --
698 -------------------------------------
700 procedure Analyze_Aspects_At_Freeze_Point (E : Entity_Id) is
705 procedure Analyze_Aspect_Default_Value (ASN : Node_Id);
706 -- This routine analyzes an Aspect_Default_[Component_]Value denoted by
707 -- the aspect specification node ASN.
709 procedure Inherit_Delayed_Rep_Aspects (ASN : Node_Id);
710 -- As discussed in the spec of Aspects (see Aspect_Delay declaration),
711 -- a derived type can inherit aspects from its parent which have been
712 -- specified at the time of the derivation using an aspect, as in:
714 -- type A is range 1 .. 10
715 -- with Size => Not_Defined_Yet;
719 -- Not_Defined_Yet : constant := 64;
721 -- In this example, the Size of A is considered to be specified prior
722 -- to the derivation, and thus inherited, even though the value is not
723 -- known at the time of derivation. To deal with this, we use two entity
724 -- flags. The flag Has_Derived_Rep_Aspects is set in the parent type (A
725 -- here), and then the flag May_Inherit_Delayed_Rep_Aspects is set in
726 -- the derived type (B here). If this flag is set when the derived type
727 -- is frozen, then this procedure is called to ensure proper inheritance
728 -- of all delayed aspects from the parent type. The derived type is E,
729 -- the argument to Analyze_Aspects_At_Freeze_Point. ASN is the first
730 -- aspect specification node in the Rep_Item chain for the parent type.
732 procedure Make_Pragma_From_Boolean_Aspect (ASN : Node_Id);
733 -- Given an aspect specification node ASN whose expression is an
734 -- optional Boolean, this routines creates the corresponding pragma
735 -- at the freezing point.
737 ----------------------------------
738 -- Analyze_Aspect_Default_Value --
739 ----------------------------------
741 procedure Analyze_Aspect_Default_Value (ASN : Node_Id) is
742 Ent : constant Entity_Id := Entity (ASN);
743 Expr : constant Node_Id := Expression (ASN);
744 Id : constant Node_Id := Identifier (ASN);
747 Error_Msg_Name_1 := Chars (Id);
749 if not Is_Type (Ent) then
750 Error_Msg_N ("aspect% can only apply to a type", Id);
753 elsif not Is_First_Subtype (Ent) then
754 Error_Msg_N ("aspect% cannot apply to subtype", Id);
757 elsif A_Id = Aspect_Default_Value
758 and then not Is_Scalar_Type (Ent)
760 Error_Msg_N ("aspect% can only be applied to scalar type", Id);
763 elsif A_Id = Aspect_Default_Component_Value then
764 if not Is_Array_Type (Ent) then
765 Error_Msg_N ("aspect% can only be applied to array type", Id);
768 elsif not Is_Scalar_Type (Component_Type (Ent)) then
769 Error_Msg_N ("aspect% requires scalar components", Id);
774 Set_Has_Default_Aspect (Base_Type (Ent));
776 if Is_Scalar_Type (Ent) then
777 Set_Default_Aspect_Value (Base_Type (Ent), Expr);
779 Set_Default_Aspect_Component_Value (Base_Type (Ent), Expr);
781 end Analyze_Aspect_Default_Value;
783 ---------------------------------
784 -- Inherit_Delayed_Rep_Aspects --
785 ---------------------------------
787 procedure Inherit_Delayed_Rep_Aspects (ASN : Node_Id) is
788 P : constant Entity_Id := Entity (ASN);
789 -- Entithy for parent type
792 -- Item from Rep_Item chain
797 -- Loop through delayed aspects for the parent type
800 while Present (N) loop
801 if Nkind (N) = N_Aspect_Specification then
802 exit when Entity (N) /= P;
804 if Is_Delayed_Aspect (N) then
805 A := Get_Aspect_Id (Chars (Identifier (N)));
807 -- Process delayed rep aspect. For Boolean attributes it is
808 -- not possible to cancel an attribute once set (the attempt
809 -- to use an aspect with xxx => False is an error) for a
810 -- derived type. So for those cases, we do not have to check
811 -- if a clause has been given for the derived type, since it
812 -- is harmless to set it again if it is already set.
818 when Aspect_Alignment =>
819 if not Has_Alignment_Clause (E) then
820 Set_Alignment (E, Alignment (P));
825 when Aspect_Atomic =>
826 if Is_Atomic (P) then
832 when Aspect_Atomic_Components =>
833 if Has_Atomic_Components (P) then
834 Set_Has_Atomic_Components (Base_Type (E));
839 when Aspect_Bit_Order =>
840 if Is_Record_Type (E)
841 and then No (Get_Attribute_Definition_Clause
842 (E, Attribute_Bit_Order))
843 and then Reverse_Bit_Order (P)
845 Set_Reverse_Bit_Order (Base_Type (E));
850 when Aspect_Component_Size =>
852 and then not Has_Component_Size_Clause (E)
855 (Base_Type (E), Component_Size (P));
860 when Aspect_Machine_Radix =>
861 if Is_Decimal_Fixed_Point_Type (E)
862 and then not Has_Machine_Radix_Clause (E)
864 Set_Machine_Radix_10 (E, Machine_Radix_10 (P));
867 -- Object_Size (also Size which also sets Object_Size)
869 when Aspect_Object_Size | Aspect_Size =>
870 if not Has_Size_Clause (E)
872 No (Get_Attribute_Definition_Clause
873 (E, Attribute_Object_Size))
875 Set_Esize (E, Esize (P));
881 if not Is_Packed (E) then
882 Set_Is_Packed (Base_Type (E));
884 if Is_Bit_Packed_Array (P) then
885 Set_Is_Bit_Packed_Array (Base_Type (E));
886 Set_Packed_Array_Type (E, Packed_Array_Type (P));
890 -- Scalar_Storage_Order
892 when Aspect_Scalar_Storage_Order =>
893 if (Is_Record_Type (E) or else Is_Array_Type (E))
894 and then No (Get_Attribute_Definition_Clause
895 (E, Attribute_Scalar_Storage_Order))
896 and then Reverse_Storage_Order (P)
898 Set_Reverse_Storage_Order (Base_Type (E));
904 if Is_Fixed_Point_Type (E)
905 and then not Has_Small_Clause (E)
907 Set_Small_Value (E, Small_Value (P));
912 when Aspect_Storage_Size =>
913 if (Is_Access_Type (E) or else Is_Task_Type (E))
914 and then not Has_Storage_Size_Clause (E)
916 Set_Storage_Size_Variable
917 (Base_Type (E), Storage_Size_Variable (P));
922 when Aspect_Value_Size =>
924 -- Value_Size is never inherited, it is either set by
925 -- default, or it is explicitly set for the derived
926 -- type. So nothing to do here.
932 when Aspect_Volatile =>
933 if Is_Volatile (P) then
937 -- Volatile_Components
939 when Aspect_Volatile_Components =>
940 if Has_Volatile_Components (P) then
941 Set_Has_Volatile_Components (Base_Type (E));
944 -- That should be all the Rep Aspects
947 pragma Assert (Aspect_Delay (A_Id) /= Rep_Aspect);
954 N := Next_Rep_Item (N);
956 end Inherit_Delayed_Rep_Aspects;
958 -------------------------------------
959 -- Make_Pragma_From_Boolean_Aspect --
960 -------------------------------------
962 procedure Make_Pragma_From_Boolean_Aspect (ASN : Node_Id) is
963 Ident : constant Node_Id := Identifier (ASN);
964 A_Name : constant Name_Id := Chars (Ident);
965 A_Id : constant Aspect_Id := Get_Aspect_Id (A_Name);
966 Ent : constant Entity_Id := Entity (ASN);
967 Expr : constant Node_Id := Expression (ASN);
968 Loc : constant Source_Ptr := Sloc (ASN);
972 procedure Check_False_Aspect_For_Derived_Type;
973 -- This procedure checks for the case of a false aspect for a derived
974 -- type, which improperly tries to cancel an aspect inherited from
977 -----------------------------------------
978 -- Check_False_Aspect_For_Derived_Type --
979 -----------------------------------------
981 procedure Check_False_Aspect_For_Derived_Type is
985 -- We are only checking derived types
987 if not Is_Derived_Type (E) then
991 Par := Nearest_Ancestor (E);
994 when Aspect_Atomic | Aspect_Shared =>
995 if not Is_Atomic (Par) then
999 when Aspect_Atomic_Components =>
1000 if not Has_Atomic_Components (Par) then
1004 when Aspect_Discard_Names =>
1005 if not Discard_Names (Par) then
1010 if not Is_Packed (Par) then
1014 when Aspect_Unchecked_Union =>
1015 if not Is_Unchecked_Union (Par) then
1019 when Aspect_Volatile =>
1020 if not Is_Volatile (Par) then
1024 when Aspect_Volatile_Components =>
1025 if not Has_Volatile_Components (Par) then
1033 -- Fall through means we are canceling an inherited aspect
1035 Error_Msg_Name_1 := A_Name;
1037 ("derived type& inherits aspect%, cannot cancel", Expr, E);
1039 end Check_False_Aspect_For_Derived_Type;
1041 -- Start of processing for Make_Pragma_From_Boolean_Aspect
1044 -- Note that we know Expr is present, because for a missing Expr
1045 -- argument, we knew it was True and did not need to delay the
1046 -- evaluation to the freeze point.
1048 if Is_False (Static_Boolean (Expr)) then
1049 Check_False_Aspect_For_Derived_Type;
1054 Pragma_Argument_Associations => New_List (
1055 Make_Pragma_Argument_Association (Sloc (Ident),
1056 Expression => New_Occurrence_Of (Ent, Sloc (Ident)))),
1058 Pragma_Identifier =>
1059 Make_Identifier (Sloc (Ident), Chars (Ident)));
1061 Set_From_Aspect_Specification (Prag, True);
1062 Set_Corresponding_Aspect (Prag, ASN);
1063 Set_Aspect_Rep_Item (ASN, Prag);
1064 Set_Is_Delayed_Aspect (Prag);
1065 Set_Parent (Prag, ASN);
1067 end Make_Pragma_From_Boolean_Aspect;
1069 -- Start of processing for Analyze_Aspects_At_Freeze_Point
1072 -- Must be visible in current scope
1074 if not Scope_Within_Or_Same (Current_Scope, Scope (E)) then
1078 -- Look for aspect specification entries for this entity
1080 ASN := First_Rep_Item (E);
1081 while Present (ASN) loop
1082 if Nkind (ASN) = N_Aspect_Specification then
1083 exit when Entity (ASN) /= E;
1085 if Is_Delayed_Aspect (ASN) then
1086 A_Id := Get_Aspect_Id (ASN);
1090 -- For aspects whose expression is an optional Boolean, make
1091 -- the corresponding pragma at the freezing point.
1093 when Boolean_Aspects |
1094 Library_Unit_Aspects =>
1095 Make_Pragma_From_Boolean_Aspect (ASN);
1097 -- Special handling for aspects that don't correspond to
1098 -- pragmas/attributes.
1100 when Aspect_Default_Value |
1101 Aspect_Default_Component_Value =>
1102 Analyze_Aspect_Default_Value (ASN);
1104 -- Ditto for iterator aspects, because the corresponding
1105 -- attributes may not have been analyzed yet.
1107 when Aspect_Constant_Indexing |
1108 Aspect_Variable_Indexing |
1109 Aspect_Default_Iterator |
1110 Aspect_Iterator_Element =>
1111 Analyze (Expression (ASN));
1117 Ritem := Aspect_Rep_Item (ASN);
1119 if Present (Ritem) then
1125 Next_Rep_Item (ASN);
1128 -- This is where we inherit delayed rep aspects from our parent. Note
1129 -- that if we fell out of the above loop with ASN non-empty, it means
1130 -- we hit an aspect for an entity other than E, and it must be the
1131 -- type from which we were derived.
1133 if May_Inherit_Delayed_Rep_Aspects (E) then
1134 Inherit_Delayed_Rep_Aspects (ASN);
1136 end Analyze_Aspects_At_Freeze_Point;
1138 -----------------------------------
1139 -- Analyze_Aspect_Specifications --
1140 -----------------------------------
1142 procedure Analyze_Aspect_Specifications (N : Node_Id; E : Entity_Id) is
1143 procedure Decorate_Delayed_Aspect_And_Pragma
1146 -- Establish the linkages between a delayed aspect and its corresponding
1147 -- pragma. Set all delay-related flags on both constructs.
1149 procedure Insert_Delayed_Pragma (Prag : Node_Id);
1150 -- Insert a postcondition-like pragma into the tree depending on the
1151 -- context. Prag must denote one of the following: Pre, Post, Depends,
1152 -- Global or Contract_Cases.
1154 ----------------------------------------
1155 -- Decorate_Delayed_Aspect_And_Pragma --
1156 ----------------------------------------
1158 procedure Decorate_Delayed_Aspect_And_Pragma
1163 Set_Aspect_Rep_Item (Asp, Prag);
1164 Set_Corresponding_Aspect (Prag, Asp);
1165 Set_From_Aspect_Specification (Prag);
1166 Set_Is_Delayed_Aspect (Prag);
1167 Set_Is_Delayed_Aspect (Asp);
1168 Set_Parent (Prag, Asp);
1169 end Decorate_Delayed_Aspect_And_Pragma;
1171 ---------------------------
1172 -- Insert_Delayed_Pragma --
1173 ---------------------------
1175 procedure Insert_Delayed_Pragma (Prag : Node_Id) is
1179 -- When the context is a library unit, the pragma is added to the
1180 -- Pragmas_After list.
1182 if Nkind (Parent (N)) = N_Compilation_Unit then
1183 Aux := Aux_Decls_Node (Parent (N));
1185 if No (Pragmas_After (Aux)) then
1186 Set_Pragmas_After (Aux, New_List);
1189 Prepend (Prag, Pragmas_After (Aux));
1191 -- Pragmas associated with subprogram bodies are inserted in the
1192 -- declarative part.
1194 elsif Nkind (N) = N_Subprogram_Body then
1195 if No (Declarations (N)) then
1196 Set_Declarations (N, New_List (Prag));
1202 -- There may be several aspects associated with the body;
1203 -- preserve the ordering of the corresponding pragmas.
1205 D := First (Declarations (N));
1206 while Present (D) loop
1207 exit when Nkind (D) /= N_Pragma
1208 or else not From_Aspect_Specification (D);
1213 Append (Prag, Declarations (N));
1215 Insert_Before (D, Prag);
1223 Insert_After (N, Prag);
1225 -- Analyze the pragma before analyzing the proper body of a stub.
1226 -- This ensures that the pragma will appear on the proper contract
1227 -- list (see N_Contract).
1229 if Nkind (N) = N_Subprogram_Body_Stub then
1233 end Insert_Delayed_Pragma;
1241 L : constant List_Id := Aspect_Specifications (N);
1243 Ins_Node : Node_Id := N;
1244 -- Insert pragmas/attribute definition clause after this node when no
1245 -- delayed analysis is required.
1247 -- Start of processing for Analyze_Aspect_Specifications
1249 -- The general processing involves building an attribute definition
1250 -- clause or a pragma node that corresponds to the aspect. Then in order
1251 -- to delay the evaluation of this aspect to the freeze point, we attach
1252 -- the corresponding pragma/attribute definition clause to the aspect
1253 -- specification node, which is then placed in the Rep Item chain. In
1254 -- this case we mark the entity by setting the flag Has_Delayed_Aspects
1255 -- and we evaluate the rep item at the freeze point. When the aspect
1256 -- doesn't have a corresponding pragma/attribute definition clause, then
1257 -- its analysis is simply delayed at the freeze point.
1259 -- Some special cases don't require delay analysis, thus the aspect is
1260 -- analyzed right now.
1262 -- Note that there is a special handling for Pre, Post, Test_Case,
1263 -- Contract_Cases aspects. In these cases, we do not have to worry
1264 -- about delay issues, since the pragmas themselves deal with delay
1265 -- of visibility for the expression analysis. Thus, we just insert
1266 -- the pragma after the node N.
1269 pragma Assert (Present (L));
1271 -- Loop through aspects
1273 Aspect := First (L);
1274 Aspect_Loop : while Present (Aspect) loop
1275 Analyze_One_Aspect : declare
1276 Expr : constant Node_Id := Expression (Aspect);
1277 Id : constant Node_Id := Identifier (Aspect);
1278 Loc : constant Source_Ptr := Sloc (Aspect);
1279 Nam : constant Name_Id := Chars (Id);
1280 A_Id : constant Aspect_Id := Get_Aspect_Id (Nam);
1283 Delay_Required : Boolean;
1284 -- Set False if delay is not required
1286 Eloc : Source_Ptr := No_Location;
1287 -- Source location of expression, modified when we split PPC's. It
1288 -- is set below when Expr is present.
1290 procedure Analyze_Aspect_External_Or_Link_Name;
1291 -- Perform analysis of the External_Name or Link_Name aspects
1293 procedure Analyze_Aspect_Implicit_Dereference;
1294 -- Perform analysis of the Implicit_Dereference aspects
1296 procedure Make_Aitem_Pragma
1297 (Pragma_Argument_Associations : List_Id;
1298 Pragma_Name : Name_Id);
1299 -- This is a wrapper for Make_Pragma used for converting aspects
1300 -- to pragmas. It takes care of Sloc (set from Loc) and building
1301 -- the pragma identifier from the given name. In addition the
1302 -- flags Class_Present and Split_PPC are set from the aspect
1303 -- node, as well as Is_Ignored. This routine also sets the
1304 -- From_Aspect_Specification in the resulting pragma node to
1305 -- True, and sets Corresponding_Aspect to point to the aspect.
1306 -- The resulting pragma is assigned to Aitem.
1308 ------------------------------------------
1309 -- Analyze_Aspect_External_Or_Link_Name --
1310 ------------------------------------------
1312 procedure Analyze_Aspect_External_Or_Link_Name is
1314 -- Verify that there is an Import/Export aspect defined for the
1315 -- entity. The processing of that aspect in turn checks that
1316 -- there is a Convention aspect declared. The pragma is
1317 -- constructed when processing the Convention aspect.
1324 while Present (A) loop
1325 exit when Nam_In (Chars (Identifier (A)), Name_Export,
1332 ("missing Import/Export for Link/External name",
1336 end Analyze_Aspect_External_Or_Link_Name;
1338 -----------------------------------------
1339 -- Analyze_Aspect_Implicit_Dereference --
1340 -----------------------------------------
1342 procedure Analyze_Aspect_Implicit_Dereference is
1344 if not Is_Type (E) or else not Has_Discriminants (E) then
1346 ("aspect must apply to a type with discriminants", N);
1353 Disc := First_Discriminant (E);
1354 while Present (Disc) loop
1355 if Chars (Expr) = Chars (Disc)
1356 and then Ekind (Etype (Disc)) =
1357 E_Anonymous_Access_Type
1359 Set_Has_Implicit_Dereference (E);
1360 Set_Has_Implicit_Dereference (Disc);
1364 Next_Discriminant (Disc);
1367 -- Error if no proper access discriminant.
1370 ("not an access discriminant of&", Expr, E);
1373 end Analyze_Aspect_Implicit_Dereference;
1375 -----------------------
1376 -- Make_Aitem_Pragma --
1377 -----------------------
1379 procedure Make_Aitem_Pragma
1380 (Pragma_Argument_Associations : List_Id;
1381 Pragma_Name : Name_Id)
1383 Args : List_Id := Pragma_Argument_Associations;
1386 -- We should never get here if aspect was disabled
1388 pragma Assert (not Is_Disabled (Aspect));
1390 -- Certain aspects allow for an optional name or expression. Do
1391 -- not generate a pragma with empty argument association list.
1393 if No (Args) or else No (Expression (First (Args))) then
1401 Pragma_Argument_Associations => Args,
1402 Pragma_Identifier =>
1403 Make_Identifier (Sloc (Id), Pragma_Name),
1404 Class_Present => Class_Present (Aspect),
1405 Split_PPC => Split_PPC (Aspect));
1407 -- Set additional semantic fields
1409 if Is_Ignored (Aspect) then
1410 Set_Is_Ignored (Aitem);
1411 elsif Is_Checked (Aspect) then
1412 Set_Is_Checked (Aitem);
1415 Set_Corresponding_Aspect (Aitem, Aspect);
1416 Set_From_Aspect_Specification (Aitem, True);
1417 end Make_Aitem_Pragma;
1419 -- Start of processing for Analyze_One_Aspect
1422 -- Skip aspect if already analyzed (not clear if this is needed)
1424 if Analyzed (Aspect) then
1428 -- Skip looking at aspect if it is totally disabled. Just mark it
1429 -- as such for later reference in the tree. This also sets the
1430 -- Is_Ignored and Is_Checked flags appropriately.
1432 Check_Applicable_Policy (Aspect);
1434 if Is_Disabled (Aspect) then
1438 -- Set the source location of expression, used in the case of
1439 -- a failed precondition/postcondition or invariant. Note that
1440 -- the source location of the expression is not usually the best
1441 -- choice here. For example, it gets located on the last AND
1442 -- keyword in a chain of boolean expressiond AND'ed together.
1443 -- It is best to put the message on the first character of the
1444 -- assertion, which is the effect of the First_Node call here.
1446 if Present (Expr) then
1447 Eloc := Sloc (First_Node (Expr));
1450 -- Check restriction No_Implementation_Aspect_Specifications
1452 if Implementation_Defined_Aspect (A_Id) then
1454 (No_Implementation_Aspect_Specifications, Aspect);
1457 -- Check restriction No_Specification_Of_Aspect
1459 Check_Restriction_No_Specification_Of_Aspect (Aspect);
1461 -- Analyze this aspect (actual analysis is delayed till later)
1463 Set_Analyzed (Aspect);
1464 Set_Entity (Aspect, E);
1465 Ent := New_Occurrence_Of (E, Sloc (Id));
1467 -- Check for duplicate aspect. Note that the Comes_From_Source
1468 -- test allows duplicate Pre/Post's that we generate internally
1469 -- to escape being flagged here.
1471 if No_Duplicates_Allowed (A_Id) then
1473 while Anod /= Aspect loop
1474 if Comes_From_Source (Aspect)
1475 and then Same_Aspect (A_Id, Get_Aspect_Id (Anod))
1477 Error_Msg_Name_1 := Nam;
1478 Error_Msg_Sloc := Sloc (Anod);
1480 -- Case of same aspect specified twice
1482 if Class_Present (Anod) = Class_Present (Aspect) then
1483 if not Class_Present (Anod) then
1485 ("aspect% for & previously given#",
1489 ("aspect `%''Class` for & previously given#",
1499 -- Check some general restrictions on language defined aspects
1501 if not Implementation_Defined_Aspect (A_Id) then
1502 Error_Msg_Name_1 := Nam;
1504 -- Not allowed for renaming declarations
1506 if Nkind (N) in N_Renaming_Declaration then
1508 ("aspect % not allowed for renaming declaration",
1512 -- Not allowed for formal type declarations
1514 if Nkind (N) = N_Formal_Type_Declaration then
1516 ("aspect % not allowed for formal type declaration",
1521 -- Copy expression for later processing by the procedures
1522 -- Check_Aspect_At_[Freeze_Point | End_Of_Declarations]
1524 Set_Entity (Id, New_Copy_Tree (Expr));
1526 -- Set Delay_Required as appropriate to aspect
1528 case Aspect_Delay (A_Id) is
1529 when Always_Delay =>
1530 Delay_Required := True;
1533 Delay_Required := False;
1537 -- If expression has the form of an integer literal, then
1538 -- do not delay, since we know the value cannot change.
1539 -- This optimization catches most rep clause cases.
1541 if (Present (Expr) and then Nkind (Expr) = N_Integer_Literal)
1542 or else (A_Id in Boolean_Aspects and then No (Expr))
1544 Delay_Required := False;
1546 Delay_Required := True;
1547 Set_Has_Delayed_Rep_Aspects (E);
1551 -- Processing based on specific aspect
1555 -- No_Aspect should be impossible
1558 raise Program_Error;
1560 -- Case 1: Aspects corresponding to attribute definition
1563 when Aspect_Address |
1566 Aspect_Component_Size |
1567 Aspect_Constant_Indexing |
1568 Aspect_Default_Iterator |
1569 Aspect_Dispatching_Domain |
1570 Aspect_External_Tag |
1572 Aspect_Iterator_Element |
1573 Aspect_Machine_Radix |
1574 Aspect_Object_Size |
1577 Aspect_Scalar_Storage_Order |
1580 Aspect_Simple_Storage_Pool |
1581 Aspect_Storage_Pool |
1582 Aspect_Stream_Size |
1584 Aspect_Variable_Indexing |
1587 -- Indexing aspects apply only to tagged type
1589 if (A_Id = Aspect_Constant_Indexing
1591 A_Id = Aspect_Variable_Indexing)
1592 and then not (Is_Type (E)
1593 and then Is_Tagged_Type (E))
1595 Error_Msg_N ("indexing applies to a tagged type", N);
1599 -- For case of address aspect, we don't consider that we
1600 -- know the entity is never set in the source, since it is
1601 -- is likely aliasing is occurring.
1603 -- Note: one might think that the analysis of the resulting
1604 -- attribute definition clause would take care of that, but
1605 -- that's not the case since it won't be from source.
1607 if A_Id = Aspect_Address then
1608 Set_Never_Set_In_Source (E, False);
1611 -- Construct the attribute definition clause
1614 Make_Attribute_Definition_Clause (Loc,
1616 Chars => Chars (Id),
1617 Expression => Relocate_Node (Expr));
1619 -- If the address is specified, then we treat the entity as
1620 -- referenced, to avoid spurious warnings. This is analogous
1621 -- to what is done with an attribute definition clause, but
1622 -- here we don't want to generate a reference because this
1623 -- is the point of definition of the entity.
1625 if A_Id = Aspect_Address then
1629 -- Case 2: Aspects corresponding to pragmas
1631 -- Case 2a: Aspects corresponding to pragmas with two
1632 -- arguments, where the first argument is a local name
1633 -- referring to the entity, and the second argument is the
1634 -- aspect definition expression.
1636 -- Suppress/Unsuppress
1638 when Aspect_Suppress |
1639 Aspect_Unsuppress =>
1642 (Pragma_Argument_Associations => New_List (
1643 Make_Pragma_Argument_Association (Loc,
1644 Expression => New_Occurrence_Of (E, Loc)),
1645 Make_Pragma_Argument_Association (Sloc (Expr),
1646 Expression => Relocate_Node (Expr))),
1647 Pragma_Name => Chars (Id));
1651 -- Corresponds to pragma Implemented, construct the pragma
1653 when Aspect_Synchronization =>
1656 (Pragma_Argument_Associations => New_List (
1657 Make_Pragma_Argument_Association (Loc,
1658 Expression => New_Occurrence_Of (E, Loc)),
1659 Make_Pragma_Argument_Association (Sloc (Expr),
1660 Expression => Relocate_Node (Expr))),
1661 Pragma_Name => Name_Implemented);
1665 when Aspect_Attach_Handler =>
1667 (Pragma_Argument_Associations => New_List (
1668 Make_Pragma_Argument_Association (Sloc (Ent),
1670 Make_Pragma_Argument_Association (Sloc (Expr),
1671 Expression => Relocate_Node (Expr))),
1672 Pragma_Name => Name_Attach_Handler);
1674 -- Dynamic_Predicate, Predicate, Static_Predicate
1676 when Aspect_Dynamic_Predicate |
1678 Aspect_Static_Predicate =>
1680 -- Construct the pragma (always a pragma Predicate, with
1681 -- flags recording whether it is static/dynamic). We also
1682 -- set flags recording this in the type itself.
1685 (Pragma_Argument_Associations => New_List (
1686 Make_Pragma_Argument_Association (Sloc (Ent),
1688 Make_Pragma_Argument_Association (Sloc (Expr),
1689 Expression => Relocate_Node (Expr))),
1690 Pragma_Name => Name_Predicate);
1692 -- Mark type has predicates, and remember what kind of
1693 -- aspect lead to this predicate (we need this to access
1694 -- the right set of check policies later on).
1696 Set_Has_Predicates (E);
1698 if A_Id = Aspect_Dynamic_Predicate then
1699 Set_Has_Dynamic_Predicate_Aspect (E);
1700 elsif A_Id = Aspect_Static_Predicate then
1701 Set_Has_Static_Predicate_Aspect (E);
1704 -- If the type is private, indicate that its completion
1705 -- has a freeze node, because that is the one that will
1706 -- be visible at freeze time.
1708 if Is_Private_Type (E) and then Present (Full_View (E)) then
1709 Set_Has_Predicates (Full_View (E));
1711 if A_Id = Aspect_Dynamic_Predicate then
1712 Set_Has_Dynamic_Predicate_Aspect (Full_View (E));
1713 elsif A_Id = Aspect_Static_Predicate then
1714 Set_Has_Static_Predicate_Aspect (Full_View (E));
1717 Set_Has_Delayed_Aspects (Full_View (E));
1718 Ensure_Freeze_Node (Full_View (E));
1721 -- Case 2b: Aspects corresponding to pragmas with two
1722 -- arguments, where the second argument is a local name
1723 -- referring to the entity, and the first argument is the
1724 -- aspect definition expression.
1728 when Aspect_Convention =>
1730 -- The aspect may be part of the specification of an import
1731 -- or export pragma. Scan the aspect list to gather the
1732 -- other components, if any. The name of the generated
1733 -- pragma is one of Convention/Import/Export.
1745 P_Name := Chars (Id);
1747 Arg_List := New_List;
1752 while Present (A) loop
1753 A_Name := Chars (Identifier (A));
1755 if Nam_In (A_Name, Name_Import, Name_Export) then
1757 Error_Msg_N ("conflicting", A);
1764 elsif A_Name = Name_Link_Name then
1766 Make_Pragma_Argument_Association (Loc,
1768 Expression => Relocate_Node (Expression (A)));
1770 elsif A_Name = Name_External_Name then
1772 Make_Pragma_Argument_Association (Loc,
1774 Expression => Relocate_Node (Expression (A)));
1780 Arg_List := New_List (
1781 Make_Pragma_Argument_Association (Sloc (Expr),
1782 Expression => Relocate_Node (Expr)),
1783 Make_Pragma_Argument_Association (Sloc (Ent),
1784 Expression => Ent));
1786 if Present (L_Assoc) then
1787 Append_To (Arg_List, L_Assoc);
1790 if Present (E_Assoc) then
1791 Append_To (Arg_List, E_Assoc);
1795 (Pragma_Argument_Associations => Arg_List,
1796 Pragma_Name => P_Name);
1799 -- CPU, Interrupt_Priority, Priority
1801 -- These three aspects can be specified for a subprogram spec
1802 -- or body, in which case we analyze the expression and export
1803 -- the value of the aspect.
1805 -- Previously, we generated an equivalent pragma for bodies
1806 -- (note that the specs cannot contain these pragmas). The
1807 -- pragma was inserted ahead of local declarations, rather than
1808 -- after the body. This leads to a certain duplication between
1809 -- the processing performed for the aspect and the pragma, but
1810 -- given the straightforward handling required it is simpler
1811 -- to duplicate than to translate the aspect in the spec into
1812 -- a pragma in the declarative part of the body.
1815 Aspect_Interrupt_Priority |
1818 if Nkind_In (N, N_Subprogram_Body,
1819 N_Subprogram_Declaration)
1821 -- Analyze the aspect expression
1823 Analyze_And_Resolve (Expr, Standard_Integer);
1825 -- Interrupt_Priority aspect not allowed for main
1826 -- subprograms. ARM D.1 does not forbid this explicitly,
1827 -- but ARM J.15.11 (6/3) does not permit pragma
1828 -- Interrupt_Priority for subprograms.
1830 if A_Id = Aspect_Interrupt_Priority then
1832 ("Interrupt_Priority aspect cannot apply to "
1833 & "subprogram", Expr);
1835 -- The expression must be static
1837 elsif not Is_Static_Expression (Expr) then
1838 Flag_Non_Static_Expr
1839 ("aspect requires static expression!", Expr);
1841 -- Check whether this is the main subprogram. Issue a
1842 -- warning only if it is obviously not a main program
1843 -- (when it has parameters or when the subprogram is
1844 -- within a package).
1846 elsif Present (Parameter_Specifications
1847 (Specification (N)))
1848 or else not Is_Compilation_Unit (Defining_Entity (N))
1850 -- See ARM D.1 (14/3) and D.16 (12/3)
1853 ("aspect applied to subprogram other than the "
1854 & "main subprogram has no effect??", Expr);
1856 -- Otherwise check in range and export the value
1858 -- For the CPU aspect
1860 elsif A_Id = Aspect_CPU then
1861 if Is_In_Range (Expr, RTE (RE_CPU_Range)) then
1863 -- Value is correct so we export the value to make
1864 -- it available at execution time.
1867 (Main_Unit, UI_To_Int (Expr_Value (Expr)));
1871 ("main subprogram CPU is out of range", Expr);
1874 -- For the Priority aspect
1876 elsif A_Id = Aspect_Priority then
1877 if Is_In_Range (Expr, RTE (RE_Priority)) then
1879 -- Value is correct so we export the value to make
1880 -- it available at execution time.
1883 (Main_Unit, UI_To_Int (Expr_Value (Expr)));
1887 ("main subprogram priority is out of range",
1892 -- Load an arbitrary entity from System.Tasking.Stages
1893 -- or System.Tasking.Restricted.Stages (depending on
1894 -- the supported profile) to make sure that one of these
1895 -- packages is implicitly with'ed, since we need to have
1896 -- the tasking run time active for the pragma Priority to
1897 -- have any effect. Previously with with'ed the package
1898 -- System.Tasking, but this package does not trigger the
1899 -- required initialization of the run-time library.
1902 Discard : Entity_Id;
1903 pragma Warnings (Off, Discard);
1905 if Restricted_Profile then
1906 Discard := RTE (RE_Activate_Restricted_Tasks);
1908 Discard := RTE (RE_Activate_Tasks);
1912 -- Handling for these Aspects in subprograms is complete
1919 -- Pass the aspect as an attribute
1922 Make_Attribute_Definition_Clause (Loc,
1924 Chars => Chars (Id),
1925 Expression => Relocate_Node (Expr));
1930 when Aspect_Warnings =>
1932 (Pragma_Argument_Associations => New_List (
1933 Make_Pragma_Argument_Association (Sloc (Expr),
1934 Expression => Relocate_Node (Expr)),
1935 Make_Pragma_Argument_Association (Loc,
1936 Expression => New_Occurrence_Of (E, Loc))),
1937 Pragma_Name => Chars (Id));
1939 -- Case 2c: Aspects corresponding to pragmas with three
1942 -- Invariant aspects have a first argument that references the
1943 -- entity, a second argument that is the expression and a third
1944 -- argument that is an appropriate message.
1946 -- Invariant, Type_Invariant
1948 when Aspect_Invariant |
1949 Aspect_Type_Invariant =>
1951 -- Analysis of the pragma will verify placement legality:
1952 -- an invariant must apply to a private type, or appear in
1953 -- the private part of a spec and apply to a completion.
1956 (Pragma_Argument_Associations => New_List (
1957 Make_Pragma_Argument_Association (Sloc (Ent),
1959 Make_Pragma_Argument_Association (Sloc (Expr),
1960 Expression => Relocate_Node (Expr))),
1961 Pragma_Name => Name_Invariant);
1963 -- Add message unless exception messages are suppressed
1965 if not Opt.Exception_Locations_Suppressed then
1966 Append_To (Pragma_Argument_Associations (Aitem),
1967 Make_Pragma_Argument_Association (Eloc,
1968 Chars => Name_Message,
1970 Make_String_Literal (Eloc,
1971 Strval => "failed invariant from "
1972 & Build_Location_String (Eloc))));
1975 -- For Invariant case, insert immediately after the entity
1976 -- declaration. We do not have to worry about delay issues
1977 -- since the pragma processing takes care of this.
1979 Delay_Required := False;
1981 -- Case 2d : Aspects that correspond to a pragma with one
1986 -- Aspect Abstract_State introduces implicit declarations for
1987 -- all state abstraction entities it defines. To emulate this
1988 -- behavior, insert the pragma at the beginning of the visible
1989 -- declarations of the related package so that it is analyzed
1992 when Aspect_Abstract_State => Abstract_State : declare
1996 if Nkind_In (N, N_Generic_Package_Declaration,
1997 N_Package_Declaration)
1999 Decls := Visible_Declarations (Specification (N));
2002 (Pragma_Argument_Associations => New_List (
2003 Make_Pragma_Argument_Association (Loc,
2004 Expression => Relocate_Node (Expr))),
2005 Pragma_Name => Name_Abstract_State);
2006 Decorate_Delayed_Aspect_And_Pragma (Aspect, Aitem);
2010 Set_Visible_Declarations (N, Decls);
2013 Prepend_To (Decls, Aitem);
2017 ("aspect & must apply to a package declaration",
2026 -- Aspect Depends must be delayed because it mentions names
2027 -- of inputs and output that are classified by aspect Global.
2028 -- The aspect and pragma are treated the same way as a post
2031 when Aspect_Depends =>
2033 (Pragma_Argument_Associations => New_List (
2034 Make_Pragma_Argument_Association (Loc,
2035 Expression => Relocate_Node (Expr))),
2036 Pragma_Name => Name_Depends);
2038 Decorate_Delayed_Aspect_And_Pragma (Aspect, Aitem);
2039 Insert_Delayed_Pragma (Aitem);
2044 -- Aspect Global must be delayed because it can mention names
2045 -- and benefit from the forward visibility rules applicable to
2046 -- aspects of subprograms. The aspect and pragma are treated
2047 -- the same way as a post condition.
2049 when Aspect_Global =>
2051 (Pragma_Argument_Associations => New_List (
2052 Make_Pragma_Argument_Association (Loc,
2053 Expression => Relocate_Node (Expr))),
2054 Pragma_Name => Name_Global);
2056 Decorate_Delayed_Aspect_And_Pragma (Aspect, Aitem);
2057 Insert_Delayed_Pragma (Aitem);
2060 -- Initial_Condition
2062 -- Aspect Initial_Condition covers the visible declarations of
2063 -- a package and all hidden states through functions. As such,
2064 -- it must be evaluated at the end of the said declarations.
2066 when Aspect_Initial_Condition => Initial_Condition : declare
2070 if Nkind_In (N, N_Generic_Package_Declaration,
2071 N_Package_Declaration)
2073 Decls := Visible_Declarations (Specification (N));
2076 (Pragma_Argument_Associations => New_List (
2077 Make_Pragma_Argument_Association (Loc,
2078 Expression => Relocate_Node (Expr))),
2080 Name_Initial_Condition);
2081 Decorate_Delayed_Aspect_And_Pragma (Aspect, Aitem);
2085 Set_Visible_Declarations (N, Decls);
2088 Prepend_To (Decls, Aitem);
2092 ("aspect & must apply to a package declaration",
2097 end Initial_Condition;
2101 -- Aspect Initializes coverts the visible declarations of a
2102 -- package. As such, it must be evaluated at the end of the
2103 -- said declarations.
2105 when Aspect_Initializes => Initializes : declare
2109 if Nkind_In (N, N_Generic_Package_Declaration,
2110 N_Package_Declaration)
2112 Decls := Visible_Declarations (Specification (N));
2115 (Pragma_Argument_Associations => New_List (
2116 Make_Pragma_Argument_Association (Loc,
2117 Expression => Relocate_Node (Expr))),
2118 Pragma_Name => Name_Initializes);
2119 Decorate_Delayed_Aspect_And_Pragma (Aspect, Aitem);
2123 Set_Visible_Declarations (N, Decls);
2126 Prepend_To (Decls, Aitem);
2130 ("aspect & must apply to a package declaration",
2139 when Aspect_SPARK_Mode => SPARK_Mode : declare
2144 (Pragma_Argument_Associations => New_List (
2145 Make_Pragma_Argument_Association (Loc,
2146 Expression => Relocate_Node (Expr))),
2147 Pragma_Name => Name_SPARK_Mode);
2149 -- When the aspect appears on a package body, insert the
2150 -- generated pragma at the top of the body declarations to
2151 -- emulate the behavior of a source pragma.
2153 if Nkind (N) = N_Package_Body then
2154 Decorate_Delayed_Aspect_And_Pragma (Aspect, Aitem);
2155 Decls := Declarations (N);
2159 Set_Declarations (N, Decls);
2162 Prepend_To (Decls, Aitem);
2165 -- When the aspect is associated with package declaration,
2166 -- insert the generated pragma at the top of the visible
2167 -- declarations to emulate the behavior of a source pragma.
2169 elsif Nkind (N) = N_Package_Declaration then
2170 Decorate_Delayed_Aspect_And_Pragma (Aspect, Aitem);
2171 Decls := Visible_Declarations (Specification (N));
2175 Set_Visible_Declarations (Specification (N), Decls);
2178 Prepend_To (Decls, Aitem);
2185 -- Aspect Refined_Depends must be delayed because it can
2186 -- mention state refinements introduced by aspect Refined_State
2187 -- and further classified by aspect Refined_Global. Since both
2188 -- those aspects are delayed, so is Refined_Depends.
2190 when Aspect_Refined_Depends =>
2192 (Pragma_Argument_Associations => New_List (
2193 Make_Pragma_Argument_Association (Loc,
2194 Expression => Relocate_Node (Expr))),
2195 Pragma_Name => Name_Refined_Depends);
2197 Decorate_Delayed_Aspect_And_Pragma (Aspect, Aitem);
2198 Insert_Delayed_Pragma (Aitem);
2203 -- Aspect Refined_Global must be delayed because it can mention
2204 -- state refinements introduced by aspect Refined_State. Since
2205 -- Refined_State is already delayed due to forward references,
2206 -- so is Refined_Global.
2208 when Aspect_Refined_Global =>
2210 (Pragma_Argument_Associations => New_List (
2211 Make_Pragma_Argument_Association (Loc,
2212 Expression => Relocate_Node (Expr))),
2213 Pragma_Name => Name_Refined_Global);
2215 Decorate_Delayed_Aspect_And_Pragma (Aspect, Aitem);
2216 Insert_Delayed_Pragma (Aitem);
2221 when Aspect_Refined_Post =>
2223 (Pragma_Argument_Associations => New_List (
2224 Make_Pragma_Argument_Association (Loc,
2225 Expression => Relocate_Node (Expr))),
2226 Pragma_Name => Name_Refined_Post);
2230 when Aspect_Refined_State => Refined_State : declare
2234 -- The corresponding pragma for Refined_State is inserted in
2235 -- the declarations of the related package body. This action
2236 -- synchronizes both the source and from-aspect versions of
2239 if Nkind (N) = N_Package_Body then
2240 Decls := Declarations (N);
2243 (Pragma_Argument_Associations => New_List (
2244 Make_Pragma_Argument_Association (Loc,
2245 Expression => Relocate_Node (Expr))),
2246 Pragma_Name => Name_Refined_State);
2247 Decorate_Delayed_Aspect_And_Pragma (Aspect, Aitem);
2251 Set_Declarations (N, Decls);
2254 Prepend_To (Decls, Aitem);
2258 ("aspect & must apply to a package body", Aspect, Id);
2264 -- Relative_Deadline
2266 when Aspect_Relative_Deadline =>
2268 (Pragma_Argument_Associations => New_List (
2269 Make_Pragma_Argument_Association (Loc,
2270 Expression => Relocate_Node (Expr))),
2271 Pragma_Name => Name_Relative_Deadline);
2273 -- If the aspect applies to a task, the corresponding pragma
2274 -- must appear within its declarations, not after.
2276 if Nkind (N) = N_Task_Type_Declaration then
2282 if No (Task_Definition (N)) then
2283 Set_Task_Definition (N,
2284 Make_Task_Definition (Loc,
2285 Visible_Declarations => New_List,
2286 End_Label => Empty));
2289 Def := Task_Definition (N);
2290 V := Visible_Declarations (Def);
2291 if not Is_Empty_List (V) then
2292 Insert_Before (First (V), Aitem);
2295 Set_Visible_Declarations (Def, New_List (Aitem));
2302 -- Case 3 : Aspects that don't correspond to pragma/attribute
2303 -- definition clause.
2305 -- Case 3a: The aspects listed below don't correspond to
2306 -- pragmas/attributes but do require delayed analysis.
2308 -- Default_Value, Default_Component_Value
2310 when Aspect_Default_Value |
2311 Aspect_Default_Component_Value =>
2314 -- Case 3b: The aspects listed below don't correspond to
2315 -- pragmas/attributes and don't need delayed analysis.
2317 -- Implicit_Dereference
2319 -- For Implicit_Dereference, External_Name and Link_Name, only
2320 -- the legality checks are done during the analysis, thus no
2321 -- delay is required.
2323 when Aspect_Implicit_Dereference =>
2324 Analyze_Aspect_Implicit_Dereference;
2327 -- External_Name, Link_Name
2329 when Aspect_External_Name |
2331 Analyze_Aspect_External_Or_Link_Name;
2336 when Aspect_Dimension =>
2337 Analyze_Aspect_Dimension (N, Id, Expr);
2342 when Aspect_Dimension_System =>
2343 Analyze_Aspect_Dimension_System (N, Id, Expr);
2346 -- Case 4: Aspects requiring special handling
2348 -- Pre/Post/Test_Case/Contract_Cases whose corresponding
2349 -- pragmas take care of the delay.
2353 -- Aspects Pre/Post generate Precondition/Postcondition pragmas
2354 -- with a first argument that is the expression, and a second
2355 -- argument that is an informative message if the test fails.
2356 -- This is inserted right after the declaration, to get the
2357 -- required pragma placement. The processing for the pragmas
2358 -- takes care of the required delay.
2360 when Pre_Post_Aspects => Pre_Post : declare
2364 if A_Id = Aspect_Pre or else A_Id = Aspect_Precondition then
2365 Pname := Name_Precondition;
2367 Pname := Name_Postcondition;
2370 -- If the expressions is of the form A and then B, then
2371 -- we generate separate Pre/Post aspects for the separate
2372 -- clauses. Since we allow multiple pragmas, there is no
2373 -- problem in allowing multiple Pre/Post aspects internally.
2374 -- These should be treated in reverse order (B first and
2375 -- A second) since they are later inserted just after N in
2376 -- the order they are treated. This way, the pragma for A
2377 -- ends up preceding the pragma for B, which may have an
2378 -- importance for the error raised (either constraint error
2379 -- or precondition error).
2381 -- We do not do this for Pre'Class, since we have to put
2382 -- these conditions together in a complex OR expression
2384 -- We do not do this in ASIS mode, as ASIS relies on the
2385 -- original node representing the complete expression, when
2386 -- retrieving it through the source aspect table.
2389 and then (Pname = Name_Postcondition
2390 or else not Class_Present (Aspect))
2392 while Nkind (Expr) = N_And_Then loop
2393 Insert_After (Aspect,
2394 Make_Aspect_Specification (Sloc (Left_Opnd (Expr)),
2395 Identifier => Identifier (Aspect),
2396 Expression => Relocate_Node (Left_Opnd (Expr)),
2397 Class_Present => Class_Present (Aspect),
2398 Split_PPC => True));
2399 Rewrite (Expr, Relocate_Node (Right_Opnd (Expr)));
2400 Eloc := Sloc (Expr);
2404 -- Build the precondition/postcondition pragma
2406 -- Add note about why we do NOT need Copy_Tree here ???
2409 (Pragma_Argument_Associations => New_List (
2410 Make_Pragma_Argument_Association (Eloc,
2411 Chars => Name_Check,
2412 Expression => Relocate_Node (Expr))),
2413 Pragma_Name => Pname);
2415 -- Add message unless exception messages are suppressed
2417 if not Opt.Exception_Locations_Suppressed then
2418 Append_To (Pragma_Argument_Associations (Aitem),
2419 Make_Pragma_Argument_Association (Eloc,
2420 Chars => Name_Message,
2422 Make_String_Literal (Eloc,
2424 & Get_Name_String (Pname)
2426 & Build_Location_String (Eloc))));
2429 Set_Is_Delayed_Aspect (Aspect);
2431 -- For Pre/Post cases, insert immediately after the entity
2432 -- declaration, since that is the required pragma placement.
2433 -- Note that for these aspects, we do not have to worry
2434 -- about delay issues, since the pragmas themselves deal
2435 -- with delay of visibility for the expression analysis.
2437 Insert_Delayed_Pragma (Aitem);
2443 when Aspect_Test_Case => Test_Case : declare
2445 Comp_Expr : Node_Id;
2446 Comp_Assn : Node_Id;
2452 if Nkind (Parent (N)) = N_Compilation_Unit then
2453 Error_Msg_Name_1 := Nam;
2454 Error_Msg_N ("incorrect placement of aspect `%`", E);
2458 if Nkind (Expr) /= N_Aggregate then
2459 Error_Msg_Name_1 := Nam;
2461 ("wrong syntax for aspect `%` for &", Id, E);
2465 -- Make pragma expressions refer to the original aspect
2466 -- expressions through the Original_Node link. This is
2467 -- used in semantic analysis for ASIS mode, so that the
2468 -- original expression also gets analyzed.
2470 Comp_Expr := First (Expressions (Expr));
2471 while Present (Comp_Expr) loop
2472 New_Expr := Relocate_Node (Comp_Expr);
2473 Set_Original_Node (New_Expr, Comp_Expr);
2475 Make_Pragma_Argument_Association (Sloc (Comp_Expr),
2476 Expression => New_Expr));
2480 Comp_Assn := First (Component_Associations (Expr));
2481 while Present (Comp_Assn) loop
2482 if List_Length (Choices (Comp_Assn)) /= 1
2484 Nkind (First (Choices (Comp_Assn))) /= N_Identifier
2486 Error_Msg_Name_1 := Nam;
2488 ("wrong syntax for aspect `%` for &", Id, E);
2492 New_Expr := Relocate_Node (Expression (Comp_Assn));
2493 Set_Original_Node (New_Expr, Expression (Comp_Assn));
2495 Make_Pragma_Argument_Association (Sloc (Comp_Assn),
2496 Chars => Chars (First (Choices (Comp_Assn))),
2497 Expression => New_Expr));
2501 -- Build the test-case pragma
2504 (Pragma_Argument_Associations => Args,
2505 Pragma_Name => Nam);
2510 when Aspect_Contract_Cases =>
2512 (Pragma_Argument_Associations => New_List (
2513 Make_Pragma_Argument_Association (Loc,
2514 Expression => Relocate_Node (Expr))),
2515 Pragma_Name => Nam);
2517 Decorate_Delayed_Aspect_And_Pragma (Aspect, Aitem);
2518 Insert_Delayed_Pragma (Aitem);
2521 -- Case 5: Special handling for aspects with an optional
2522 -- boolean argument.
2524 -- In the general case, the corresponding pragma cannot be
2525 -- generated yet because the evaluation of the boolean needs
2526 -- to be delayed till the freeze point.
2528 when Boolean_Aspects |
2529 Library_Unit_Aspects =>
2531 Set_Is_Boolean_Aspect (Aspect);
2533 -- Lock_Free aspect only apply to protected objects
2535 if A_Id = Aspect_Lock_Free then
2536 if Ekind (E) /= E_Protected_Type then
2537 Error_Msg_Name_1 := Nam;
2539 ("aspect % only applies to a protected object",
2543 -- Set the Uses_Lock_Free flag to True if there is no
2544 -- expression or if the expression is True. The
2545 -- evaluation of this aspect should be delayed to the
2546 -- freeze point (why???)
2549 or else Is_True (Static_Boolean (Expr))
2551 Set_Uses_Lock_Free (E);
2554 Record_Rep_Item (E, Aspect);
2559 elsif A_Id = Aspect_Import or else A_Id = Aspect_Export then
2561 -- Verify that there is an aspect Convention that will
2562 -- incorporate the Import/Export aspect, and eventual
2563 -- Link/External names.
2570 while Present (A) loop
2571 exit when Chars (Identifier (A)) = Name_Convention;
2575 -- It is legal to specify Import for a variable, in
2576 -- order to suppress initialization for it, without
2577 -- specifying explicitly its convention. However this
2578 -- is only legal if the convention of the object type
2579 -- is Ada or similar.
2582 if Ekind (E) = E_Variable
2583 and then A_Id = Aspect_Import
2586 C : constant Convention_Id :=
2587 Convention (Etype (E));
2589 if C = Convention_Ada or else
2590 C = Convention_Ada_Pass_By_Copy or else
2591 C = Convention_Ada_Pass_By_Reference
2598 -- Otherwise, Convention must be specified
2601 ("missing Convention aspect for Export/Import",
2609 -- Library unit aspects require special handling in the case
2610 -- of a package declaration, the pragma needs to be inserted
2611 -- in the list of declarations for the associated package.
2612 -- There is no issue of visibility delay for these aspects.
2614 if A_Id in Library_Unit_Aspects
2616 Nkind_In (N, N_Package_Declaration,
2617 N_Generic_Package_Declaration)
2618 and then Nkind (Parent (N)) /= N_Compilation_Unit
2621 ("incorrect context for library unit aspect&", Id);
2625 -- Cases where we do not delay, includes all cases where
2626 -- the expression is missing other than the above cases.
2628 if not Delay_Required or else No (Expr) then
2630 (Pragma_Argument_Associations => New_List (
2631 Make_Pragma_Argument_Association (Sloc (Ent),
2632 Expression => Ent)),
2633 Pragma_Name => Chars (Id));
2634 Delay_Required := False;
2636 -- In general cases, the corresponding pragma/attribute
2637 -- definition clause will be inserted later at the freezing
2638 -- point, and we do not need to build it now
2646 -- This is special because for access types we need to generate
2647 -- an attribute definition clause. This also works for single
2648 -- task declarations, but it does not work for task type
2649 -- declarations, because we have the case where the expression
2650 -- references a discriminant of the task type. That can't use
2651 -- an attribute definition clause because we would not have
2652 -- visibility on the discriminant. For that case we must
2653 -- generate a pragma in the task definition.
2655 when Aspect_Storage_Size =>
2659 if Ekind (E) = E_Task_Type then
2661 Decl : constant Node_Id := Declaration_Node (E);
2664 pragma Assert (Nkind (Decl) = N_Task_Type_Declaration);
2666 -- If no task definition, create one
2668 if No (Task_Definition (Decl)) then
2669 Set_Task_Definition (Decl,
2670 Make_Task_Definition (Loc,
2671 Visible_Declarations => Empty_List,
2672 End_Label => Empty));
2675 -- Create a pragma and put it at the start of the
2676 -- task definition for the task type declaration.
2679 (Pragma_Argument_Associations => New_List (
2680 Make_Pragma_Argument_Association (Loc,
2681 Expression => Relocate_Node (Expr))),
2682 Pragma_Name => Name_Storage_Size);
2686 Visible_Declarations (Task_Definition (Decl)));
2690 -- All other cases, generate attribute definition
2694 Make_Attribute_Definition_Clause (Loc,
2696 Chars => Chars (Id),
2697 Expression => Relocate_Node (Expr));
2701 -- Attach the corresponding pragma/attribute definition clause to
2702 -- the aspect specification node.
2704 if Present (Aitem) then
2705 Set_From_Aspect_Specification (Aitem, True);
2708 -- In the context of a compilation unit, we directly put the
2709 -- pragma in the Pragmas_After list of the N_Compilation_Unit_Aux
2710 -- node (no delay is required here) except for aspects on a
2711 -- subprogram body (see below) and a generic package, for which
2712 -- we need to introduce the pragma before building the generic
2713 -- copy (see sem_ch12), and for package instantiations, where
2714 -- the library unit pragmas are better handled early.
2716 if Nkind (Parent (N)) = N_Compilation_Unit
2717 and then (Present (Aitem) or else Is_Boolean_Aspect (Aspect))
2720 Aux : constant Node_Id := Aux_Decls_Node (Parent (N));
2723 pragma Assert (Nkind (Aux) = N_Compilation_Unit_Aux);
2725 -- For a Boolean aspect, create the corresponding pragma if
2726 -- no expression or if the value is True.
2728 if Is_Boolean_Aspect (Aspect) and then No (Aitem) then
2729 if Is_True (Static_Boolean (Expr)) then
2731 (Pragma_Argument_Associations => New_List (
2732 Make_Pragma_Argument_Association (Sloc (Ent),
2733 Expression => Ent)),
2734 Pragma_Name => Chars (Id));
2736 Set_From_Aspect_Specification (Aitem, True);
2737 Set_Corresponding_Aspect (Aitem, Aspect);
2744 -- If the aspect is on a subprogram body (relevant aspect
2745 -- is Inline), add the pragma in front of the declarations.
2747 if Nkind (N) = N_Subprogram_Body then
2748 if No (Declarations (N)) then
2749 Set_Declarations (N, New_List);
2752 Prepend (Aitem, Declarations (N));
2754 elsif Nkind (N) = N_Generic_Package_Declaration then
2755 if No (Visible_Declarations (Specification (N))) then
2756 Set_Visible_Declarations (Specification (N), New_List);
2760 Visible_Declarations (Specification (N)));
2762 elsif Nkind (N) = N_Package_Instantiation then
2764 Spec : constant Node_Id :=
2765 Specification (Instance_Spec (N));
2767 if No (Visible_Declarations (Spec)) then
2768 Set_Visible_Declarations (Spec, New_List);
2771 Prepend (Aitem, Visible_Declarations (Spec));
2775 if No (Pragmas_After (Aux)) then
2776 Set_Pragmas_After (Aux, New_List);
2779 Append (Aitem, Pragmas_After (Aux));
2786 -- The evaluation of the aspect is delayed to the freezing point.
2787 -- The pragma or attribute clause if there is one is then attached
2788 -- to the aspect specification which is put in the rep item list.
2790 if Delay_Required then
2791 if Present (Aitem) then
2792 Set_Is_Delayed_Aspect (Aitem);
2793 Set_Aspect_Rep_Item (Aspect, Aitem);
2794 Set_Parent (Aitem, Aspect);
2797 Set_Is_Delayed_Aspect (Aspect);
2799 -- In the case of Default_Value, link the aspect to base type
2800 -- as well, even though it appears on a first subtype. This is
2801 -- mandated by the semantics of the aspect. Do not establish
2802 -- the link when processing the base type itself as this leads
2803 -- to a rep item circularity. Verify that we are dealing with
2804 -- a scalar type to prevent cascaded errors.
2806 if A_Id = Aspect_Default_Value
2807 and then Is_Scalar_Type (E)
2808 and then Base_Type (E) /= E
2810 Set_Has_Delayed_Aspects (Base_Type (E));
2811 Record_Rep_Item (Base_Type (E), Aspect);
2814 Set_Has_Delayed_Aspects (E);
2815 Record_Rep_Item (E, Aspect);
2817 -- When delay is not required and the context is a package or a
2818 -- subprogram body, insert the pragma in the body declarations.
2820 elsif Nkind_In (N, N_Package_Body, N_Subprogram_Body) then
2821 if No (Declarations (N)) then
2822 Set_Declarations (N, New_List);
2825 -- The pragma is added before source declarations
2827 Prepend_To (Declarations (N), Aitem);
2829 -- When delay is not required and the context is not a compilation
2830 -- unit, we simply insert the pragma/attribute definition clause
2834 Insert_After (Ins_Node, Aitem);
2837 end Analyze_One_Aspect;
2841 end loop Aspect_Loop;
2843 if Has_Delayed_Aspects (E) then
2844 Ensure_Freeze_Node (E);
2846 end Analyze_Aspect_Specifications;
2848 -----------------------
2849 -- Analyze_At_Clause --
2850 -----------------------
2852 -- An at clause is replaced by the corresponding Address attribute
2853 -- definition clause that is the preferred approach in Ada 95.
2855 procedure Analyze_At_Clause (N : Node_Id) is
2856 CS : constant Boolean := Comes_From_Source (N);
2859 -- This is an obsolescent feature
2861 Check_Restriction (No_Obsolescent_Features, N);
2863 if Warn_On_Obsolescent_Feature then
2865 ("?j?at clause is an obsolescent feature (RM J.7(2))", N);
2867 ("\?j?use address attribute definition clause instead", N);
2870 -- Rewrite as address clause
2873 Make_Attribute_Definition_Clause (Sloc (N),
2874 Name => Identifier (N),
2875 Chars => Name_Address,
2876 Expression => Expression (N)));
2878 -- We preserve Comes_From_Source, since logically the clause still comes
2879 -- from the source program even though it is changed in form.
2881 Set_Comes_From_Source (N, CS);
2883 -- Analyze rewritten clause
2885 Analyze_Attribute_Definition_Clause (N);
2886 end Analyze_At_Clause;
2888 -----------------------------------------
2889 -- Analyze_Attribute_Definition_Clause --
2890 -----------------------------------------
2892 procedure Analyze_Attribute_Definition_Clause (N : Node_Id) is
2893 Loc : constant Source_Ptr := Sloc (N);
2894 Nam : constant Node_Id := Name (N);
2895 Attr : constant Name_Id := Chars (N);
2896 Expr : constant Node_Id := Expression (N);
2897 Id : constant Attribute_Id := Get_Attribute_Id (Attr);
2900 -- The entity of Nam after it is analyzed. In the case of an incomplete
2901 -- type, this is the underlying type.
2904 -- The underlying entity to which the attribute applies. Generally this
2905 -- is the Underlying_Type of Ent, except in the case where the clause
2906 -- applies to full view of incomplete type or private type in which case
2907 -- U_Ent is just a copy of Ent.
2909 FOnly : Boolean := False;
2910 -- Reset to True for subtype specific attribute (Alignment, Size)
2911 -- and for stream attributes, i.e. those cases where in the call
2912 -- to Rep_Item_Too_Late, FOnly is set True so that only the freezing
2913 -- rules are checked. Note that the case of stream attributes is not
2914 -- clear from the RM, but see AI95-00137. Also, the RM seems to
2915 -- disallow Storage_Size for derived task types, but that is also
2916 -- clearly unintentional.
2918 procedure Analyze_Stream_TSS_Definition (TSS_Nam : TSS_Name_Type);
2919 -- Common processing for 'Read, 'Write, 'Input and 'Output attribute
2920 -- definition clauses.
2922 function Duplicate_Clause return Boolean;
2923 -- This routine checks if the aspect for U_Ent being given by attribute
2924 -- definition clause N is for an aspect that has already been specified,
2925 -- and if so gives an error message. If there is a duplicate, True is
2926 -- returned, otherwise if there is no error, False is returned.
2928 procedure Check_Indexing_Functions;
2929 -- Check that the function in Constant_Indexing or Variable_Indexing
2930 -- attribute has the proper type structure. If the name is overloaded,
2931 -- check that some interpretation is legal.
2933 procedure Check_Iterator_Functions;
2934 -- Check that there is a single function in Default_Iterator attribute
2935 -- has the proper type structure.
2937 function Check_Primitive_Function (Subp : Entity_Id) return Boolean;
2938 -- Common legality check for the previous two
2940 -----------------------------------
2941 -- Analyze_Stream_TSS_Definition --
2942 -----------------------------------
2944 procedure Analyze_Stream_TSS_Definition (TSS_Nam : TSS_Name_Type) is
2945 Subp : Entity_Id := Empty;
2950 Is_Read : constant Boolean := (TSS_Nam = TSS_Stream_Read);
2951 -- True for Read attribute, false for other attributes
2953 function Has_Good_Profile (Subp : Entity_Id) return Boolean;
2954 -- Return true if the entity is a subprogram with an appropriate
2955 -- profile for the attribute being defined.
2957 ----------------------
2958 -- Has_Good_Profile --
2959 ----------------------
2961 function Has_Good_Profile (Subp : Entity_Id) return Boolean is
2963 Is_Function : constant Boolean := (TSS_Nam = TSS_Stream_Input);
2964 Expected_Ekind : constant array (Boolean) of Entity_Kind :=
2965 (False => E_Procedure, True => E_Function);
2969 if Ekind (Subp) /= Expected_Ekind (Is_Function) then
2973 F := First_Formal (Subp);
2976 or else Ekind (Etype (F)) /= E_Anonymous_Access_Type
2977 or else Designated_Type (Etype (F)) /=
2978 Class_Wide_Type (RTE (RE_Root_Stream_Type))
2983 if not Is_Function then
2987 Expected_Mode : constant array (Boolean) of Entity_Kind :=
2988 (False => E_In_Parameter,
2989 True => E_Out_Parameter);
2991 if Parameter_Mode (F) /= Expected_Mode (Is_Read) then
2999 Typ := Etype (Subp);
3002 return Base_Type (Typ) = Base_Type (Ent)
3003 and then No (Next_Formal (F));
3004 end Has_Good_Profile;
3006 -- Start of processing for Analyze_Stream_TSS_Definition
3011 if not Is_Type (U_Ent) then
3012 Error_Msg_N ("local name must be a subtype", Nam);
3016 Pnam := TSS (Base_Type (U_Ent), TSS_Nam);
3018 -- If Pnam is present, it can be either inherited from an ancestor
3019 -- type (in which case it is legal to redefine it for this type), or
3020 -- be a previous definition of the attribute for the same type (in
3021 -- which case it is illegal).
3023 -- In the first case, it will have been analyzed already, and we
3024 -- can check that its profile does not match the expected profile
3025 -- for a stream attribute of U_Ent. In the second case, either Pnam
3026 -- has been analyzed (and has the expected profile), or it has not
3027 -- been analyzed yet (case of a type that has not been frozen yet
3028 -- and for which the stream attribute has been set using Set_TSS).
3031 and then (No (First_Entity (Pnam)) or else Has_Good_Profile (Pnam))
3033 Error_Msg_Sloc := Sloc (Pnam);
3034 Error_Msg_Name_1 := Attr;
3035 Error_Msg_N ("% attribute already defined #", Nam);
3041 if Is_Entity_Name (Expr) then
3042 if not Is_Overloaded (Expr) then
3043 if Has_Good_Profile (Entity (Expr)) then
3044 Subp := Entity (Expr);
3048 Get_First_Interp (Expr, I, It);
3049 while Present (It.Nam) loop
3050 if Has_Good_Profile (It.Nam) then
3055 Get_Next_Interp (I, It);
3060 if Present (Subp) then
3061 if Is_Abstract_Subprogram (Subp) then
3062 Error_Msg_N ("stream subprogram must not be abstract", Expr);
3066 Set_Entity (Expr, Subp);
3067 Set_Etype (Expr, Etype (Subp));
3069 New_Stream_Subprogram (N, U_Ent, Subp, TSS_Nam);
3072 Error_Msg_Name_1 := Attr;
3073 Error_Msg_N ("incorrect expression for% attribute", Expr);
3075 end Analyze_Stream_TSS_Definition;
3077 ------------------------------
3078 -- Check_Indexing_Functions --
3079 ------------------------------
3081 procedure Check_Indexing_Functions is
3082 Indexing_Found : Boolean;
3084 procedure Check_One_Function (Subp : Entity_Id);
3085 -- Check one possible interpretation. Sets Indexing_Found True if an
3086 -- indexing function is found.
3088 ------------------------
3089 -- Check_One_Function --
3090 ------------------------
3092 procedure Check_One_Function (Subp : Entity_Id) is
3093 Default_Element : constant Node_Id :=
3094 Find_Value_Of_Aspect
3095 (Etype (First_Formal (Subp)),
3096 Aspect_Iterator_Element);
3099 if not Check_Primitive_Function (Subp)
3100 and then not Is_Overloaded (Expr)
3103 ("aspect Indexing requires a function that applies to type&",
3107 -- An indexing function must return either the default element of
3108 -- the container, or a reference type. For variable indexing it
3109 -- must be the latter.
3111 if Present (Default_Element) then
3112 Analyze (Default_Element);
3114 if Is_Entity_Name (Default_Element)
3115 and then Covers (Entity (Default_Element), Etype (Subp))
3117 Indexing_Found := True;
3122 -- For variable_indexing the return type must be a reference type
3124 if Attr = Name_Variable_Indexing
3125 and then not Has_Implicit_Dereference (Etype (Subp))
3128 ("function for indexing must return a reference type", Subp);
3131 Indexing_Found := True;
3133 end Check_One_Function;
3135 -- Start of processing for Check_Indexing_Functions
3144 if not Is_Overloaded (Expr) then
3145 Check_One_Function (Entity (Expr));
3153 Indexing_Found := False;
3154 Get_First_Interp (Expr, I, It);
3155 while Present (It.Nam) loop
3157 -- Note that analysis will have added the interpretation
3158 -- that corresponds to the dereference. We only check the
3159 -- subprogram itself.
3161 if Is_Overloadable (It.Nam) then
3162 Check_One_Function (It.Nam);
3165 Get_Next_Interp (I, It);
3168 if not Indexing_Found then
3170 ("aspect Indexing requires a function that "
3171 & "applies to type&", Expr, Ent);
3175 end Check_Indexing_Functions;
3177 ------------------------------
3178 -- Check_Iterator_Functions --
3179 ------------------------------
3181 procedure Check_Iterator_Functions is
3182 Default : Entity_Id;
3184 function Valid_Default_Iterator (Subp : Entity_Id) return Boolean;
3185 -- Check one possible interpretation for validity
3187 ----------------------------
3188 -- Valid_Default_Iterator --
3189 ----------------------------
3191 function Valid_Default_Iterator (Subp : Entity_Id) return Boolean is
3195 if not Check_Primitive_Function (Subp) then
3198 Formal := First_Formal (Subp);
3201 -- False if any subsequent formal has no default expression
3203 Formal := Next_Formal (Formal);
3204 while Present (Formal) loop
3205 if No (Expression (Parent (Formal))) then
3209 Next_Formal (Formal);
3212 -- True if all subsequent formals have default expressions
3215 end Valid_Default_Iterator;
3217 -- Start of processing for Check_Iterator_Functions
3222 if not Is_Entity_Name (Expr) then
3223 Error_Msg_N ("aspect Iterator must be a function name", Expr);
3226 if not Is_Overloaded (Expr) then
3227 if not Check_Primitive_Function (Entity (Expr)) then
3229 ("aspect Indexing requires a function that applies to type&",
3230 Entity (Expr), Ent);
3233 if not Valid_Default_Iterator (Entity (Expr)) then
3234 Error_Msg_N ("improper function for default iterator", Expr);
3244 Get_First_Interp (Expr, I, It);
3245 while Present (It.Nam) loop
3246 if not Check_Primitive_Function (It.Nam)
3247 or else not Valid_Default_Iterator (It.Nam)
3251 elsif Present (Default) then
3252 Error_Msg_N ("default iterator must be unique", Expr);
3258 Get_Next_Interp (I, It);
3262 if Present (Default) then
3263 Set_Entity (Expr, Default);
3264 Set_Is_Overloaded (Expr, False);
3267 end Check_Iterator_Functions;
3269 -------------------------------
3270 -- Check_Primitive_Function --
3271 -------------------------------
3273 function Check_Primitive_Function (Subp : Entity_Id) return Boolean is
3277 if Ekind (Subp) /= E_Function then
3281 if No (First_Formal (Subp)) then
3284 Ctrl := Etype (First_Formal (Subp));
3288 or else Ctrl = Class_Wide_Type (Ent)
3290 (Ekind (Ctrl) = E_Anonymous_Access_Type
3292 (Designated_Type (Ctrl) = Ent
3293 or else Designated_Type (Ctrl) = Class_Wide_Type (Ent)))
3302 end Check_Primitive_Function;
3304 ----------------------
3305 -- Duplicate_Clause --
3306 ----------------------
3308 function Duplicate_Clause return Boolean is
3312 -- Nothing to do if this attribute definition clause comes from
3313 -- an aspect specification, since we could not be duplicating an
3314 -- explicit clause, and we dealt with the case of duplicated aspects
3315 -- in Analyze_Aspect_Specifications.
3317 if From_Aspect_Specification (N) then
3321 -- Otherwise current clause may duplicate previous clause, or a
3322 -- previously given pragma or aspect specification for the same
3325 A := Get_Rep_Item (U_Ent, Chars (N), Check_Parents => False);
3328 Error_Msg_Name_1 := Chars (N);
3329 Error_Msg_Sloc := Sloc (A);
3331 Error_Msg_NE ("aspect% for & previously given#", N, U_Ent);
3336 end Duplicate_Clause;
3338 -- Start of processing for Analyze_Attribute_Definition_Clause
3341 -- The following code is a defense against recursion. Not clear that
3342 -- this can happen legitimately, but perhaps some error situations
3343 -- can cause it, and we did see this recursion during testing.
3345 if Analyzed (N) then
3348 Set_Analyzed (N, True);
3351 -- Ignore some selected attributes in CodePeer mode since they are not
3352 -- relevant in this context.
3354 if CodePeer_Mode then
3357 -- Ignore Component_Size in CodePeer mode, to avoid changing the
3358 -- internal representation of types by implicitly packing them.
3360 when Attribute_Component_Size =>
3361 Rewrite (N, Make_Null_Statement (Sloc (N)));
3369 -- Process Ignore_Rep_Clauses option
3371 if Ignore_Rep_Clauses then
3374 -- The following should be ignored. They do not affect legality
3375 -- and may be target dependent. The basic idea of -gnatI is to
3376 -- ignore any rep clauses that may be target dependent but do not
3377 -- affect legality (except possibly to be rejected because they
3378 -- are incompatible with the compilation target).
3380 when Attribute_Alignment |
3381 Attribute_Bit_Order |
3382 Attribute_Component_Size |
3383 Attribute_Machine_Radix |
3384 Attribute_Object_Size |
3386 Attribute_Stream_Size |
3387 Attribute_Value_Size =>
3388 Rewrite (N, Make_Null_Statement (Sloc (N)));
3391 -- Perhaps 'Small should not be ignored by Ignore_Rep_Clauses ???
3393 when Attribute_Small =>
3394 if Ignore_Rep_Clauses then
3395 Rewrite (N, Make_Null_Statement (Sloc (N)));
3399 -- The following should not be ignored, because in the first place
3400 -- they are reasonably portable, and should not cause problems in
3401 -- compiling code from another target, and also they do affect
3402 -- legality, e.g. failing to provide a stream attribute for a
3403 -- type may make a program illegal.
3405 when Attribute_External_Tag |
3409 Attribute_Simple_Storage_Pool |
3410 Attribute_Storage_Pool |
3411 Attribute_Storage_Size |
3415 -- Other cases are errors ("attribute& cannot be set with
3416 -- definition clause"), which will be caught below.
3424 Ent := Entity (Nam);
3426 if Rep_Item_Too_Early (Ent, N) then
3430 -- Rep clause applies to full view of incomplete type or private type if
3431 -- we have one (if not, this is a premature use of the type). However,
3432 -- certain semantic checks need to be done on the specified entity (i.e.
3433 -- the private view), so we save it in Ent.
3435 if Is_Private_Type (Ent)
3436 and then Is_Derived_Type (Ent)
3437 and then not Is_Tagged_Type (Ent)
3438 and then No (Full_View (Ent))
3440 -- If this is a private type whose completion is a derivation from
3441 -- another private type, there is no full view, and the attribute
3442 -- belongs to the type itself, not its underlying parent.
3446 elsif Ekind (Ent) = E_Incomplete_Type then
3448 -- The attribute applies to the full view, set the entity of the
3449 -- attribute definition accordingly.
3451 Ent := Underlying_Type (Ent);
3453 Set_Entity (Nam, Ent);
3456 U_Ent := Underlying_Type (Ent);
3459 -- Avoid cascaded error
3461 if Etype (Nam) = Any_Type then
3464 -- Must be declared in current scope or in case of an aspect
3465 -- specification, must be visible in current scope.
3467 elsif Scope (Ent) /= Current_Scope
3469 not (From_Aspect_Specification (N)
3470 and then Scope_Within_Or_Same (Current_Scope, Scope (Ent)))
3472 Error_Msg_N ("entity must be declared in this scope", Nam);
3475 -- Must not be a source renaming (we do have some cases where the
3476 -- expander generates a renaming, and those cases are OK, in such
3477 -- cases any attribute applies to the renamed object as well).
3479 elsif Is_Object (Ent)
3480 and then Present (Renamed_Object (Ent))
3482 -- Case of renamed object from source, this is an error
3484 if Comes_From_Source (Renamed_Object (Ent)) then
3485 Get_Name_String (Chars (N));
3486 Error_Msg_Strlen := Name_Len;
3487 Error_Msg_String (1 .. Name_Len) := Name_Buffer (1 .. Name_Len);
3489 ("~ clause not allowed for a renaming declaration "
3490 & "(RM 13.1(6))", Nam);
3493 -- For the case of a compiler generated renaming, the attribute
3494 -- definition clause applies to the renamed object created by the
3495 -- expander. The easiest general way to handle this is to create a
3496 -- copy of the attribute definition clause for this object.
3500 Make_Attribute_Definition_Clause (Loc,
3502 New_Occurrence_Of (Entity (Renamed_Object (Ent)), Loc),
3504 Expression => Duplicate_Subexpr (Expression (N))));
3507 -- If no underlying entity, use entity itself, applies to some
3508 -- previously detected error cases ???
3510 elsif No (U_Ent) then
3513 -- Cannot specify for a subtype (exception Object/Value_Size)
3515 elsif Is_Type (U_Ent)
3516 and then not Is_First_Subtype (U_Ent)
3517 and then Id /= Attribute_Object_Size
3518 and then Id /= Attribute_Value_Size
3519 and then not From_At_Mod (N)
3521 Error_Msg_N ("cannot specify attribute for subtype", Nam);
3525 Set_Entity (N, U_Ent);
3526 Check_Restriction_No_Use_Of_Attribute (N);
3528 -- Switch on particular attribute
3536 -- Address attribute definition clause
3538 when Attribute_Address => Address : begin
3540 -- A little error check, catch for X'Address use X'Address;
3542 if Nkind (Nam) = N_Identifier
3543 and then Nkind (Expr) = N_Attribute_Reference
3544 and then Attribute_Name (Expr) = Name_Address
3545 and then Nkind (Prefix (Expr)) = N_Identifier
3546 and then Chars (Nam) = Chars (Prefix (Expr))
3549 ("address for & is self-referencing", Prefix (Expr), Ent);
3553 -- Not that special case, carry on with analysis of expression
3555 Analyze_And_Resolve (Expr, RTE (RE_Address));
3557 -- Even when ignoring rep clauses we need to indicate that the
3558 -- entity has an address clause and thus it is legal to declare
3561 if Ignore_Rep_Clauses then
3562 if Ekind_In (U_Ent, E_Variable, E_Constant) then
3563 Record_Rep_Item (U_Ent, N);
3569 if Duplicate_Clause then
3572 -- Case of address clause for subprogram
3574 elsif Is_Subprogram (U_Ent) then
3575 if Has_Homonym (U_Ent) then
3577 ("address clause cannot be given " &
3578 "for overloaded subprogram",
3583 -- For subprograms, all address clauses are permitted, and we
3584 -- mark the subprogram as having a deferred freeze so that Gigi
3585 -- will not elaborate it too soon.
3587 -- Above needs more comments, what is too soon about???
3589 Set_Has_Delayed_Freeze (U_Ent);
3591 -- Case of address clause for entry
3593 elsif Ekind (U_Ent) = E_Entry then
3594 if Nkind (Parent (N)) = N_Task_Body then
3596 ("entry address must be specified in task spec", Nam);
3600 -- For entries, we require a constant address
3602 Check_Constant_Address_Clause (Expr, U_Ent);
3604 -- Special checks for task types
3606 if Is_Task_Type (Scope (U_Ent))
3607 and then Comes_From_Source (Scope (U_Ent))
3610 ("??entry address declared for entry in task type", N);
3612 ("\??only one task can be declared of this type", N);
3615 -- Entry address clauses are obsolescent
3617 Check_Restriction (No_Obsolescent_Features, N);
3619 if Warn_On_Obsolescent_Feature then
3621 ("?j?attaching interrupt to task entry is an " &
3622 "obsolescent feature (RM J.7.1)", N);
3624 ("\?j?use interrupt procedure instead", N);
3627 -- Case of an address clause for a controlled object which we
3628 -- consider to be erroneous.
3630 elsif Is_Controlled (Etype (U_Ent))
3631 or else Has_Controlled_Component (Etype (U_Ent))
3634 ("??controlled object& must not be overlaid", Nam, U_Ent);
3636 ("\??Program_Error will be raised at run time", Nam);
3637 Insert_Action (Declaration_Node (U_Ent),
3638 Make_Raise_Program_Error (Loc,
3639 Reason => PE_Overlaid_Controlled_Object));
3642 -- Case of address clause for a (non-controlled) object
3645 Ekind (U_Ent) = E_Variable
3647 Ekind (U_Ent) = E_Constant
3650 Expr : constant Node_Id := Expression (N);
3655 -- Exported variables cannot have an address clause, because
3656 -- this cancels the effect of the pragma Export.
3658 if Is_Exported (U_Ent) then
3660 ("cannot export object with address clause", Nam);
3664 Find_Overlaid_Entity (N, O_Ent, Off);
3666 -- Overlaying controlled objects is erroneous
3669 and then (Has_Controlled_Component (Etype (O_Ent))
3670 or else Is_Controlled (Etype (O_Ent)))
3673 ("??cannot overlay with controlled object", Expr);
3675 ("\??Program_Error will be raised at run time", Expr);
3676 Insert_Action (Declaration_Node (U_Ent),
3677 Make_Raise_Program_Error (Loc,
3678 Reason => PE_Overlaid_Controlled_Object));
3681 elsif Present (O_Ent)
3682 and then Ekind (U_Ent) = E_Constant
3683 and then not Is_Constant_Object (O_Ent)
3685 Error_Msg_N ("??constant overlays a variable", Expr);
3687 -- Imported variables can have an address clause, but then
3688 -- the import is pretty meaningless except to suppress
3689 -- initializations, so we do not need such variables to
3690 -- be statically allocated (and in fact it causes trouble
3691 -- if the address clause is a local value).
3693 elsif Is_Imported (U_Ent) then
3694 Set_Is_Statically_Allocated (U_Ent, False);
3697 -- We mark a possible modification of a variable with an
3698 -- address clause, since it is likely aliasing is occurring.
3700 Note_Possible_Modification (Nam, Sure => False);
3702 -- Here we are checking for explicit overlap of one variable
3703 -- by another, and if we find this then mark the overlapped
3704 -- variable as also being volatile to prevent unwanted
3705 -- optimizations. This is a significant pessimization so
3706 -- avoid it when there is an offset, i.e. when the object
3707 -- is composite; they cannot be optimized easily anyway.
3710 and then Is_Object (O_Ent)
3713 -- The following test is an expedient solution to what
3714 -- is really a problem in CodePeer. Suppressing the
3715 -- Set_Treat_As_Volatile call here prevents later
3716 -- generation (in some cases) of trees that CodePeer
3717 -- should, but currently does not, handle correctly.
3718 -- This test should probably be removed when CodePeer
3719 -- is improved, just because we want the tree CodePeer
3720 -- analyzes to match the tree for which we generate code
3721 -- as closely as is practical. ???
3723 and then not CodePeer_Mode
3725 -- ??? O_Ent might not be in current unit
3727 Set_Treat_As_Volatile (O_Ent);
3730 -- Legality checks on the address clause for initialized
3731 -- objects is deferred until the freeze point, because
3732 -- a subsequent pragma might indicate that the object
3733 -- is imported and thus not initialized. Also, the address
3734 -- clause might involve entities that have yet to be
3737 Set_Has_Delayed_Freeze (U_Ent);
3739 -- If an initialization call has been generated for this
3740 -- object, it needs to be deferred to after the freeze node
3741 -- we have just now added, otherwise GIGI will see a
3742 -- reference to the variable (as actual to the IP call)
3743 -- before its definition.
3746 Init_Call : constant Node_Id :=
3747 Remove_Init_Call (U_Ent, N);
3750 if Present (Init_Call) then
3752 -- If the init call is an expression with actions with
3753 -- null expression, just extract the actions.
3755 if Nkind (Init_Call) = N_Expression_With_Actions
3757 Nkind (Expression (Init_Call)) = N_Null_Statement
3759 Append_Freeze_Actions (U_Ent, Actions (Init_Call));
3761 -- General case: move Init_Call to freeze actions
3764 Append_Freeze_Action (U_Ent, Init_Call);
3769 if Is_Exported (U_Ent) then
3771 ("& cannot be exported if an address clause is given",
3774 ("\define and export a variable "
3775 & "that holds its address instead", Nam);
3778 -- Entity has delayed freeze, so we will generate an
3779 -- alignment check at the freeze point unless suppressed.
3781 if not Range_Checks_Suppressed (U_Ent)
3782 and then not Alignment_Checks_Suppressed (U_Ent)
3784 Set_Check_Address_Alignment (N);
3787 -- Kill the size check code, since we are not allocating
3788 -- the variable, it is somewhere else.
3790 Kill_Size_Check_Code (U_Ent);
3792 -- If the address clause is of the form:
3794 -- for Y'Address use X'Address
3798 -- Const : constant Address := X'Address;
3800 -- for Y'Address use Const;
3802 -- then we make an entry in the table for checking the size
3803 -- and alignment of the overlaying variable. We defer this
3804 -- check till after code generation to take full advantage
3805 -- of the annotation done by the back end.
3807 -- If the entity has a generic type, the check will be
3808 -- performed in the instance if the actual type justifies
3809 -- it, and we do not insert the clause in the table to
3810 -- prevent spurious warnings.
3812 -- Note: we used to test Comes_From_Source and only give
3813 -- this warning for source entities, but we have removed
3814 -- this test. It really seems bogus to generate overlays
3815 -- that would trigger this warning in generated code.
3816 -- Furthermore, by removing the test, we handle the
3817 -- aspect case properly.
3819 if Address_Clause_Overlay_Warnings
3820 and then Present (O_Ent)
3821 and then Is_Object (O_Ent)
3823 if not Is_Generic_Type (Etype (U_Ent)) then
3824 Address_Clause_Checks.Append ((N, U_Ent, O_Ent, Off));
3827 -- If variable overlays a constant view, and we are
3828 -- warning on overlays, then mark the variable as
3829 -- overlaying a constant (we will give warnings later
3830 -- if this variable is assigned).
3832 if Is_Constant_Object (O_Ent)
3833 and then Ekind (U_Ent) = E_Variable
3835 Set_Overlays_Constant (U_Ent);
3840 -- Not a valid entity for an address clause
3843 Error_Msg_N ("address cannot be given for &", Nam);
3851 -- Alignment attribute definition clause
3853 when Attribute_Alignment => Alignment : declare
3854 Align : constant Uint := Get_Alignment_Value (Expr);
3855 Max_Align : constant Uint := UI_From_Int (Maximum_Alignment);
3860 if not Is_Type (U_Ent)
3861 and then Ekind (U_Ent) /= E_Variable
3862 and then Ekind (U_Ent) /= E_Constant
3864 Error_Msg_N ("alignment cannot be given for &", Nam);
3866 elsif Duplicate_Clause then
3869 elsif Align /= No_Uint then
3870 Set_Has_Alignment_Clause (U_Ent);
3872 -- Tagged type case, check for attempt to set alignment to a
3873 -- value greater than Max_Align, and reset if so.
3875 if Is_Tagged_Type (U_Ent) and then Align > Max_Align then
3877 ("alignment for & set to Maximum_Aligment??", Nam);
3878 Set_Alignment (U_Ent, Max_Align);
3883 Set_Alignment (U_Ent, Align);
3886 -- For an array type, U_Ent is the first subtype. In that case,
3887 -- also set the alignment of the anonymous base type so that
3888 -- other subtypes (such as the itypes for aggregates of the
3889 -- type) also receive the expected alignment.
3891 if Is_Array_Type (U_Ent) then
3892 Set_Alignment (Base_Type (U_Ent), Align);
3901 -- Bit_Order attribute definition clause
3903 when Attribute_Bit_Order => Bit_Order : declare
3905 if not Is_Record_Type (U_Ent) then
3907 ("Bit_Order can only be defined for record type", Nam);
3909 elsif Duplicate_Clause then
3913 Analyze_And_Resolve (Expr, RTE (RE_Bit_Order));
3915 if Etype (Expr) = Any_Type then
3918 elsif not Is_Static_Expression (Expr) then
3919 Flag_Non_Static_Expr
3920 ("Bit_Order requires static expression!", Expr);
3923 if (Expr_Value (Expr) = 0) /= Bytes_Big_Endian then
3924 Set_Reverse_Bit_Order (U_Ent, True);
3930 --------------------
3931 -- Component_Size --
3932 --------------------
3934 -- Component_Size attribute definition clause
3936 when Attribute_Component_Size => Component_Size_Case : declare
3937 Csize : constant Uint := Static_Integer (Expr);
3941 New_Ctyp : Entity_Id;
3945 if not Is_Array_Type (U_Ent) then
3946 Error_Msg_N ("component size requires array type", Nam);
3950 Btype := Base_Type (U_Ent);
3951 Ctyp := Component_Type (Btype);
3953 if Duplicate_Clause then
3956 elsif Rep_Item_Too_Early (Btype, N) then
3959 elsif Csize /= No_Uint then
3960 Check_Size (Expr, Ctyp, Csize, Biased);
3962 -- For the biased case, build a declaration for a subtype that
3963 -- will be used to represent the biased subtype that reflects
3964 -- the biased representation of components. We need the subtype
3965 -- to get proper conversions on referencing elements of the
3966 -- array. Note: component size clauses are ignored in VM mode.
3968 if VM_Target = No_VM then
3971 Make_Defining_Identifier (Loc,
3973 New_External_Name (Chars (U_Ent), 'C', 0, 'T'));
3976 Make_Subtype_Declaration (Loc,
3977 Defining_Identifier => New_Ctyp,
3978 Subtype_Indication =>
3979 New_Occurrence_Of (Component_Type (Btype), Loc));
3981 Set_Parent (Decl, N);
3982 Analyze (Decl, Suppress => All_Checks);
3984 Set_Has_Delayed_Freeze (New_Ctyp, False);
3985 Set_Esize (New_Ctyp, Csize);
3986 Set_RM_Size (New_Ctyp, Csize);
3987 Init_Alignment (New_Ctyp);
3988 Set_Is_Itype (New_Ctyp, True);
3989 Set_Associated_Node_For_Itype (New_Ctyp, U_Ent);
3991 Set_Component_Type (Btype, New_Ctyp);
3992 Set_Biased (New_Ctyp, N, "component size clause");
3995 Set_Component_Size (Btype, Csize);
3997 -- For VM case, we ignore component size clauses
4000 -- Give a warning unless we are in GNAT mode, in which case
4001 -- the warning is suppressed since it is not useful.
4003 if not GNAT_Mode then
4005 ("component size ignored in this configuration??", N);
4009 -- Deal with warning on overridden size
4011 if Warn_On_Overridden_Size
4012 and then Has_Size_Clause (Ctyp)
4013 and then RM_Size (Ctyp) /= Csize
4016 ("component size overrides size clause for&?S?", N, Ctyp);
4019 Set_Has_Component_Size_Clause (Btype, True);
4020 Set_Has_Non_Standard_Rep (Btype, True);
4022 end Component_Size_Case;
4024 -----------------------
4025 -- Constant_Indexing --
4026 -----------------------
4028 when Attribute_Constant_Indexing =>
4029 Check_Indexing_Functions;
4035 when Attribute_CPU => CPU :
4037 -- CPU attribute definition clause not allowed except from aspect
4040 if From_Aspect_Specification (N) then
4041 if not Is_Task_Type (U_Ent) then
4042 Error_Msg_N ("CPU can only be defined for task", Nam);
4044 elsif Duplicate_Clause then
4048 -- The expression must be analyzed in the special manner
4049 -- described in "Handling of Default and Per-Object
4050 -- Expressions" in sem.ads.
4052 -- The visibility to the discriminants must be restored
4054 Push_Scope_And_Install_Discriminants (U_Ent);
4055 Preanalyze_Spec_Expression (Expr, RTE (RE_CPU_Range));
4056 Uninstall_Discriminants_And_Pop_Scope (U_Ent);
4058 if not Is_Static_Expression (Expr) then
4059 Check_Restriction (Static_Priorities, Expr);
4065 ("attribute& cannot be set with definition clause", N);
4069 ----------------------
4070 -- Default_Iterator --
4071 ----------------------
4073 when Attribute_Default_Iterator => Default_Iterator : declare
4077 if not Is_Tagged_Type (U_Ent) then
4079 ("aspect Default_Iterator applies to tagged type", Nam);
4082 Check_Iterator_Functions;
4086 if not Is_Entity_Name (Expr)
4087 or else Ekind (Entity (Expr)) /= E_Function
4089 Error_Msg_N ("aspect Iterator must be a function", Expr);
4091 Func := Entity (Expr);
4094 if No (First_Formal (Func))
4095 or else Etype (First_Formal (Func)) /= U_Ent
4098 ("Default Iterator must be a primitive of&", Func, U_Ent);
4100 end Default_Iterator;
4102 ------------------------
4103 -- Dispatching_Domain --
4104 ------------------------
4106 when Attribute_Dispatching_Domain => Dispatching_Domain :
4108 -- Dispatching_Domain attribute definition clause not allowed
4109 -- except from aspect specification.
4111 if From_Aspect_Specification (N) then
4112 if not Is_Task_Type (U_Ent) then
4113 Error_Msg_N ("Dispatching_Domain can only be defined" &
4117 elsif Duplicate_Clause then
4121 -- The expression must be analyzed in the special manner
4122 -- described in "Handling of Default and Per-Object
4123 -- Expressions" in sem.ads.
4125 -- The visibility to the discriminants must be restored
4127 Push_Scope_And_Install_Discriminants (U_Ent);
4129 Preanalyze_Spec_Expression
4130 (Expr, RTE (RE_Dispatching_Domain));
4132 Uninstall_Discriminants_And_Pop_Scope (U_Ent);
4137 ("attribute& cannot be set with definition clause", N);
4139 end Dispatching_Domain;
4145 when Attribute_External_Tag => External_Tag :
4147 if not Is_Tagged_Type (U_Ent) then
4148 Error_Msg_N ("should be a tagged type", Nam);
4151 if Duplicate_Clause then
4155 Analyze_And_Resolve (Expr, Standard_String);
4157 if not Is_Static_Expression (Expr) then
4158 Flag_Non_Static_Expr
4159 ("static string required for tag name!", Nam);
4162 if VM_Target = No_VM then
4163 Set_Has_External_Tag_Rep_Clause (U_Ent);
4165 Error_Msg_Name_1 := Attr;
4167 ("% attribute unsupported in this configuration", Nam);
4170 if not Is_Library_Level_Entity (U_Ent) then
4172 ("??non-unique external tag supplied for &", N, U_Ent);
4174 ("\??same external tag applies to all "
4175 & "subprogram calls", N);
4177 ("\??corresponding internal tag cannot be obtained", N);
4182 --------------------------
4183 -- Implicit_Dereference --
4184 --------------------------
4186 when Attribute_Implicit_Dereference =>
4188 -- Legality checks already performed at the point of the type
4189 -- declaration, aspect is not delayed.
4197 when Attribute_Input =>
4198 Analyze_Stream_TSS_Definition (TSS_Stream_Input);
4199 Set_Has_Specified_Stream_Input (Ent);
4201 ------------------------
4202 -- Interrupt_Priority --
4203 ------------------------
4205 when Attribute_Interrupt_Priority => Interrupt_Priority :
4207 -- Interrupt_Priority attribute definition clause not allowed
4208 -- except from aspect specification.
4210 if From_Aspect_Specification (N) then
4211 if not (Is_Protected_Type (U_Ent)
4212 or else Is_Task_Type (U_Ent))
4215 ("Interrupt_Priority can only be defined for task" &
4216 "and protected object",
4219 elsif Duplicate_Clause then
4223 -- The expression must be analyzed in the special manner
4224 -- described in "Handling of Default and Per-Object
4225 -- Expressions" in sem.ads.
4227 -- The visibility to the discriminants must be restored
4229 Push_Scope_And_Install_Discriminants (U_Ent);
4231 Preanalyze_Spec_Expression
4232 (Expr, RTE (RE_Interrupt_Priority));
4234 Uninstall_Discriminants_And_Pop_Scope (U_Ent);
4239 ("attribute& cannot be set with definition clause", N);
4241 end Interrupt_Priority;
4243 ----------------------
4244 -- Iterator_Element --
4245 ----------------------
4247 when Attribute_Iterator_Element =>
4250 if not Is_Entity_Name (Expr)
4251 or else not Is_Type (Entity (Expr))
4253 Error_Msg_N ("aspect Iterator_Element must be a type", Expr);
4260 -- Machine radix attribute definition clause
4262 when Attribute_Machine_Radix => Machine_Radix : declare
4263 Radix : constant Uint := Static_Integer (Expr);
4266 if not Is_Decimal_Fixed_Point_Type (U_Ent) then
4267 Error_Msg_N ("decimal fixed-point type expected for &", Nam);
4269 elsif Duplicate_Clause then
4272 elsif Radix /= No_Uint then
4273 Set_Has_Machine_Radix_Clause (U_Ent);
4274 Set_Has_Non_Standard_Rep (Base_Type (U_Ent));
4278 elsif Radix = 10 then
4279 Set_Machine_Radix_10 (U_Ent);
4281 Error_Msg_N ("machine radix value must be 2 or 10", Expr);
4290 -- Object_Size attribute definition clause
4292 when Attribute_Object_Size => Object_Size : declare
4293 Size : constant Uint := Static_Integer (Expr);
4296 pragma Warnings (Off, Biased);
4299 if not Is_Type (U_Ent) then
4300 Error_Msg_N ("Object_Size cannot be given for &", Nam);
4302 elsif Duplicate_Clause then
4306 Check_Size (Expr, U_Ent, Size, Biased);
4314 UI_Mod (Size, 64) /= 0
4317 ("Object_Size must be 8, 16, 32, or multiple of 64",
4321 Set_Esize (U_Ent, Size);
4322 Set_Has_Object_Size_Clause (U_Ent);
4323 Alignment_Check_For_Size_Change (U_Ent, Size);
4331 when Attribute_Output =>
4332 Analyze_Stream_TSS_Definition (TSS_Stream_Output);
4333 Set_Has_Specified_Stream_Output (Ent);
4339 when Attribute_Priority => Priority :
4341 -- Priority attribute definition clause not allowed except from
4342 -- aspect specification.
4344 if From_Aspect_Specification (N) then
4345 if not (Is_Protected_Type (U_Ent)
4346 or else Is_Task_Type (U_Ent)
4347 or else Ekind (U_Ent) = E_Procedure)
4350 ("Priority can only be defined for task and protected " &
4354 elsif Duplicate_Clause then
4358 -- The expression must be analyzed in the special manner
4359 -- described in "Handling of Default and Per-Object
4360 -- Expressions" in sem.ads.
4362 -- The visibility to the discriminants must be restored
4364 Push_Scope_And_Install_Discriminants (U_Ent);
4365 Preanalyze_Spec_Expression (Expr, Standard_Integer);
4366 Uninstall_Discriminants_And_Pop_Scope (U_Ent);
4368 if not Is_Static_Expression (Expr) then
4369 Check_Restriction (Static_Priorities, Expr);
4375 ("attribute& cannot be set with definition clause", N);
4383 when Attribute_Read =>
4384 Analyze_Stream_TSS_Definition (TSS_Stream_Read);
4385 Set_Has_Specified_Stream_Read (Ent);
4387 --------------------------
4388 -- Scalar_Storage_Order --
4389 --------------------------
4391 -- Scalar_Storage_Order attribute definition clause
4393 when Attribute_Scalar_Storage_Order => Scalar_Storage_Order : declare
4395 if not (Is_Record_Type (U_Ent) or else Is_Array_Type (U_Ent)) then
4397 ("Scalar_Storage_Order can only be defined for "
4398 & "record or array type", Nam);
4400 elsif Duplicate_Clause then
4404 Analyze_And_Resolve (Expr, RTE (RE_Bit_Order));
4406 if Etype (Expr) = Any_Type then
4409 elsif not Is_Static_Expression (Expr) then
4410 Flag_Non_Static_Expr
4411 ("Scalar_Storage_Order requires static expression!", Expr);
4413 elsif (Expr_Value (Expr) = 0) /= Bytes_Big_Endian then
4415 -- Here for the case of a non-default (i.e. non-confirming)
4416 -- Scalar_Storage_Order attribute definition.
4418 if Support_Nondefault_SSO_On_Target then
4419 Set_Reverse_Storage_Order (Base_Type (U_Ent), True);
4422 ("non-default Scalar_Storage_Order "
4423 & "not supported on target", Expr);
4427 end Scalar_Storage_Order;
4433 -- Size attribute definition clause
4435 when Attribute_Size => Size : declare
4436 Size : constant Uint := Static_Integer (Expr);
4443 if Duplicate_Clause then
4446 elsif not Is_Type (U_Ent)
4447 and then Ekind (U_Ent) /= E_Variable
4448 and then Ekind (U_Ent) /= E_Constant
4450 Error_Msg_N ("size cannot be given for &", Nam);
4452 elsif Is_Array_Type (U_Ent)
4453 and then not Is_Constrained (U_Ent)
4456 ("size cannot be given for unconstrained array", Nam);
4458 elsif Size /= No_Uint then
4459 if VM_Target /= No_VM and then not GNAT_Mode then
4461 -- Size clause is not handled properly on VM targets.
4462 -- Display a warning unless we are in GNAT mode, in which
4463 -- case this is useless.
4466 ("size clauses are ignored in this configuration??", N);
4469 if Is_Type (U_Ent) then
4472 Etyp := Etype (U_Ent);
4475 -- Check size, note that Gigi is in charge of checking that the
4476 -- size of an array or record type is OK. Also we do not check
4477 -- the size in the ordinary fixed-point case, since it is too
4478 -- early to do so (there may be subsequent small clause that
4479 -- affects the size). We can check the size if a small clause
4480 -- has already been given.
4482 if not Is_Ordinary_Fixed_Point_Type (U_Ent)
4483 or else Has_Small_Clause (U_Ent)
4485 Check_Size (Expr, Etyp, Size, Biased);
4486 Set_Biased (U_Ent, N, "size clause", Biased);
4489 -- For types set RM_Size and Esize if possible
4491 if Is_Type (U_Ent) then
4492 Set_RM_Size (U_Ent, Size);
4494 -- For elementary types, increase Object_Size to power of 2,
4495 -- but not less than a storage unit in any case (normally
4496 -- this means it will be byte addressable).
4498 -- For all other types, nothing else to do, we leave Esize
4499 -- (object size) unset, the back end will set it from the
4500 -- size and alignment in an appropriate manner.
4502 -- In both cases, we check whether the alignment must be
4503 -- reset in the wake of the size change.
4505 if Is_Elementary_Type (U_Ent) then
4506 if Size <= System_Storage_Unit then
4507 Init_Esize (U_Ent, System_Storage_Unit);
4508 elsif Size <= 16 then
4509 Init_Esize (U_Ent, 16);
4510 elsif Size <= 32 then
4511 Init_Esize (U_Ent, 32);
4513 Set_Esize (U_Ent, (Size + 63) / 64 * 64);
4516 Alignment_Check_For_Size_Change (U_Ent, Esize (U_Ent));
4518 Alignment_Check_For_Size_Change (U_Ent, Size);
4521 -- For objects, set Esize only
4524 if Is_Elementary_Type (Etyp) then
4525 if Size /= System_Storage_Unit
4527 Size /= System_Storage_Unit * 2
4529 Size /= System_Storage_Unit * 4
4531 Size /= System_Storage_Unit * 8
4533 Error_Msg_Uint_1 := UI_From_Int (System_Storage_Unit);
4534 Error_Msg_Uint_2 := Error_Msg_Uint_1 * 8;
4536 ("size for primitive object must be a power of 2"
4537 & " in the range ^-^", N);
4541 Set_Esize (U_Ent, Size);
4544 Set_Has_Size_Clause (U_Ent);
4552 -- Small attribute definition clause
4554 when Attribute_Small => Small : declare
4555 Implicit_Base : constant Entity_Id := Base_Type (U_Ent);
4559 Analyze_And_Resolve (Expr, Any_Real);
4561 if Etype (Expr) = Any_Type then
4564 elsif not Is_Static_Expression (Expr) then
4565 Flag_Non_Static_Expr
4566 ("small requires static expression!", Expr);
4570 Small := Expr_Value_R (Expr);
4572 if Small <= Ureal_0 then
4573 Error_Msg_N ("small value must be greater than zero", Expr);
4579 if not Is_Ordinary_Fixed_Point_Type (U_Ent) then
4581 ("small requires an ordinary fixed point type", Nam);
4583 elsif Has_Small_Clause (U_Ent) then
4584 Error_Msg_N ("small already given for &", Nam);
4586 elsif Small > Delta_Value (U_Ent) then
4588 ("small value must not be greater than delta value", Nam);
4591 Set_Small_Value (U_Ent, Small);
4592 Set_Small_Value (Implicit_Base, Small);
4593 Set_Has_Small_Clause (U_Ent);
4594 Set_Has_Small_Clause (Implicit_Base);
4595 Set_Has_Non_Standard_Rep (Implicit_Base);
4603 -- Storage_Pool attribute definition clause
4605 when Attribute_Storage_Pool | Attribute_Simple_Storage_Pool => declare
4610 if Ekind (U_Ent) = E_Access_Subprogram_Type then
4612 ("storage pool cannot be given for access-to-subprogram type",
4617 Ekind_In (U_Ent, E_Access_Type, E_General_Access_Type)
4620 ("storage pool can only be given for access types", Nam);
4623 elsif Is_Derived_Type (U_Ent) then
4625 ("storage pool cannot be given for a derived access type",
4628 elsif Duplicate_Clause then
4631 elsif Present (Associated_Storage_Pool (U_Ent)) then
4632 Error_Msg_N ("storage pool already given for &", Nam);
4636 -- Check for Storage_Size previously given
4639 SS : constant Node_Id :=
4640 Get_Attribute_Definition_Clause
4641 (U_Ent, Attribute_Storage_Size);
4643 if Present (SS) then
4644 Check_Pool_Size_Clash (U_Ent, N, SS);
4648 -- Storage_Pool case
4650 if Id = Attribute_Storage_Pool then
4652 (Expr, Class_Wide_Type (RTE (RE_Root_Storage_Pool)));
4654 -- In the Simple_Storage_Pool case, we allow a variable of any
4655 -- simple storage pool type, so we Resolve without imposing an
4659 Analyze_And_Resolve (Expr);
4661 if not Present (Get_Rep_Pragma
4662 (Etype (Expr), Name_Simple_Storage_Pool_Type))
4665 ("expression must be of a simple storage pool type", Expr);
4669 if not Denotes_Variable (Expr) then
4670 Error_Msg_N ("storage pool must be a variable", Expr);
4674 if Nkind (Expr) = N_Type_Conversion then
4675 T := Etype (Expression (Expr));
4680 -- The Stack_Bounded_Pool is used internally for implementing
4681 -- access types with a Storage_Size. Since it only work properly
4682 -- when used on one specific type, we need to check that it is not
4683 -- hijacked improperly:
4685 -- type T is access Integer;
4686 -- for T'Storage_Size use n;
4687 -- type Q is access Float;
4688 -- for Q'Storage_Size use T'Storage_Size; -- incorrect
4690 if RTE_Available (RE_Stack_Bounded_Pool)
4691 and then Base_Type (T) = RTE (RE_Stack_Bounded_Pool)
4693 Error_Msg_N ("non-shareable internal Pool", Expr);
4697 -- If the argument is a name that is not an entity name, then
4698 -- we construct a renaming operation to define an entity of
4699 -- type storage pool.
4701 if not Is_Entity_Name (Expr)
4702 and then Is_Object_Reference (Expr)
4704 Pool := Make_Temporary (Loc, 'P', Expr);
4707 Rnode : constant Node_Id :=
4708 Make_Object_Renaming_Declaration (Loc,
4709 Defining_Identifier => Pool,
4711 New_Occurrence_Of (Etype (Expr), Loc),
4715 -- If the attribute definition clause comes from an aspect
4716 -- clause, then insert the renaming before the associated
4717 -- entity's declaration, since the attribute clause has
4718 -- not yet been appended to the declaration list.
4720 if From_Aspect_Specification (N) then
4721 Insert_Before (Parent (Entity (N)), Rnode);
4723 Insert_Before (N, Rnode);
4727 Set_Associated_Storage_Pool (U_Ent, Pool);
4730 elsif Is_Entity_Name (Expr) then
4731 Pool := Entity (Expr);
4733 -- If pool is a renamed object, get original one. This can
4734 -- happen with an explicit renaming, and within instances.
4736 while Present (Renamed_Object (Pool))
4737 and then Is_Entity_Name (Renamed_Object (Pool))
4739 Pool := Entity (Renamed_Object (Pool));
4742 if Present (Renamed_Object (Pool))
4743 and then Nkind (Renamed_Object (Pool)) = N_Type_Conversion
4744 and then Is_Entity_Name (Expression (Renamed_Object (Pool)))
4746 Pool := Entity (Expression (Renamed_Object (Pool)));
4749 Set_Associated_Storage_Pool (U_Ent, Pool);
4751 elsif Nkind (Expr) = N_Type_Conversion
4752 and then Is_Entity_Name (Expression (Expr))
4753 and then Nkind (Original_Node (Expr)) = N_Attribute_Reference
4755 Pool := Entity (Expression (Expr));
4756 Set_Associated_Storage_Pool (U_Ent, Pool);
4759 Error_Msg_N ("incorrect reference to a Storage Pool", Expr);
4768 -- Storage_Size attribute definition clause
4770 when Attribute_Storage_Size => Storage_Size : declare
4771 Btype : constant Entity_Id := Base_Type (U_Ent);
4774 if Is_Task_Type (U_Ent) then
4776 -- Check obsolescent (but never obsolescent if from aspect!)
4778 if not From_Aspect_Specification (N) then
4779 Check_Restriction (No_Obsolescent_Features, N);
4781 if Warn_On_Obsolescent_Feature then
4783 ("?j?storage size clause for task is an " &
4784 "obsolescent feature (RM J.9)", N);
4785 Error_Msg_N ("\?j?use Storage_Size pragma instead", N);
4792 if not Is_Access_Type (U_Ent)
4793 and then Ekind (U_Ent) /= E_Task_Type
4795 Error_Msg_N ("storage size cannot be given for &", Nam);
4797 elsif Is_Access_Type (U_Ent) and Is_Derived_Type (U_Ent) then
4799 ("storage size cannot be given for a derived access type",
4802 elsif Duplicate_Clause then
4806 Analyze_And_Resolve (Expr, Any_Integer);
4808 if Is_Access_Type (U_Ent) then
4810 -- Check for Storage_Pool previously given
4813 SP : constant Node_Id :=
4814 Get_Attribute_Definition_Clause
4815 (U_Ent, Attribute_Storage_Pool);
4818 if Present (SP) then
4819 Check_Pool_Size_Clash (U_Ent, SP, N);
4823 -- Special case of for x'Storage_Size use 0
4825 if Is_OK_Static_Expression (Expr)
4826 and then Expr_Value (Expr) = 0
4828 Set_No_Pool_Assigned (Btype);
4832 Set_Has_Storage_Size_Clause (Btype);
4840 when Attribute_Stream_Size => Stream_Size : declare
4841 Size : constant Uint := Static_Integer (Expr);
4844 if Ada_Version <= Ada_95 then
4845 Check_Restriction (No_Implementation_Attributes, N);
4848 if Duplicate_Clause then
4851 elsif Is_Elementary_Type (U_Ent) then
4852 if Size /= System_Storage_Unit
4854 Size /= System_Storage_Unit * 2
4856 Size /= System_Storage_Unit * 4
4858 Size /= System_Storage_Unit * 8
4860 Error_Msg_Uint_1 := UI_From_Int (System_Storage_Unit);
4862 ("stream size for elementary type must be a"
4863 & " power of 2 and at least ^", N);
4865 elsif RM_Size (U_Ent) > Size then
4866 Error_Msg_Uint_1 := RM_Size (U_Ent);
4868 ("stream size for elementary type must be a"
4869 & " power of 2 and at least ^", N);
4872 Set_Has_Stream_Size_Clause (U_Ent);
4875 Error_Msg_N ("Stream_Size cannot be given for &", Nam);
4883 -- Value_Size attribute definition clause
4885 when Attribute_Value_Size => Value_Size : declare
4886 Size : constant Uint := Static_Integer (Expr);
4890 if not Is_Type (U_Ent) then
4891 Error_Msg_N ("Value_Size cannot be given for &", Nam);
4893 elsif Duplicate_Clause then
4896 elsif Is_Array_Type (U_Ent)
4897 and then not Is_Constrained (U_Ent)
4900 ("Value_Size cannot be given for unconstrained array", Nam);
4903 if Is_Elementary_Type (U_Ent) then
4904 Check_Size (Expr, U_Ent, Size, Biased);
4905 Set_Biased (U_Ent, N, "value size clause", Biased);
4908 Set_RM_Size (U_Ent, Size);
4912 -----------------------
4913 -- Variable_Indexing --
4914 -----------------------
4916 when Attribute_Variable_Indexing =>
4917 Check_Indexing_Functions;
4923 when Attribute_Write =>
4924 Analyze_Stream_TSS_Definition (TSS_Stream_Write);
4925 Set_Has_Specified_Stream_Write (Ent);
4927 -- All other attributes cannot be set
4931 ("attribute& cannot be set with definition clause", N);
4934 -- The test for the type being frozen must be performed after any
4935 -- expression the clause has been analyzed since the expression itself
4936 -- might cause freezing that makes the clause illegal.
4938 if Rep_Item_Too_Late (U_Ent, N, FOnly) then
4941 end Analyze_Attribute_Definition_Clause;
4943 ----------------------------
4944 -- Analyze_Code_Statement --
4945 ----------------------------
4947 procedure Analyze_Code_Statement (N : Node_Id) is
4948 HSS : constant Node_Id := Parent (N);
4949 SBody : constant Node_Id := Parent (HSS);
4950 Subp : constant Entity_Id := Current_Scope;
4957 -- Analyze and check we get right type, note that this implements the
4958 -- requirement (RM 13.8(1)) that Machine_Code be with'ed, since that
4959 -- is the only way that Asm_Insn could possibly be visible.
4961 Analyze_And_Resolve (Expression (N));
4963 if Etype (Expression (N)) = Any_Type then
4965 elsif Etype (Expression (N)) /= RTE (RE_Asm_Insn) then
4966 Error_Msg_N ("incorrect type for code statement", N);
4970 Check_Code_Statement (N);
4972 -- Make sure we appear in the handled statement sequence of a
4973 -- subprogram (RM 13.8(3)).
4975 if Nkind (HSS) /= N_Handled_Sequence_Of_Statements
4976 or else Nkind (SBody) /= N_Subprogram_Body
4979 ("code statement can only appear in body of subprogram", N);
4983 -- Do remaining checks (RM 13.8(3)) if not already done
4985 if not Is_Machine_Code_Subprogram (Subp) then
4986 Set_Is_Machine_Code_Subprogram (Subp);
4988 -- No exception handlers allowed
4990 if Present (Exception_Handlers (HSS)) then
4992 ("exception handlers not permitted in machine code subprogram",
4993 First (Exception_Handlers (HSS)));
4996 -- No declarations other than use clauses and pragmas (we allow
4997 -- certain internally generated declarations as well).
4999 Decl := First (Declarations (SBody));
5000 while Present (Decl) loop
5001 DeclO := Original_Node (Decl);
5002 if Comes_From_Source (DeclO)
5003 and not Nkind_In (DeclO, N_Pragma,
5004 N_Use_Package_Clause,
5006 N_Implicit_Label_Declaration)
5009 ("this declaration not allowed in machine code subprogram",
5016 -- No statements other than code statements, pragmas, and labels.
5017 -- Again we allow certain internally generated statements.
5019 -- In Ada 2012, qualified expressions are names, and the code
5020 -- statement is initially parsed as a procedure call.
5022 Stmt := First (Statements (HSS));
5023 while Present (Stmt) loop
5024 StmtO := Original_Node (Stmt);
5026 -- A procedure call transformed into a code statement is OK.
5028 if Ada_Version >= Ada_2012
5029 and then Nkind (StmtO) = N_Procedure_Call_Statement
5030 and then Nkind (Name (StmtO)) = N_Qualified_Expression
5034 elsif Comes_From_Source (StmtO)
5035 and then not Nkind_In (StmtO, N_Pragma,
5040 ("this statement is not allowed in machine code subprogram",
5047 end Analyze_Code_Statement;
5049 -----------------------------------------------
5050 -- Analyze_Enumeration_Representation_Clause --
5051 -----------------------------------------------
5053 procedure Analyze_Enumeration_Representation_Clause (N : Node_Id) is
5054 Ident : constant Node_Id := Identifier (N);
5055 Aggr : constant Node_Id := Array_Aggregate (N);
5056 Enumtype : Entity_Id;
5063 Err : Boolean := False;
5064 -- Set True to avoid cascade errors and crashes on incorrect source code
5066 Lo : constant Uint := Expr_Value (Type_Low_Bound (Universal_Integer));
5067 Hi : constant Uint := Expr_Value (Type_High_Bound (Universal_Integer));
5068 -- Allowed range of universal integer (= allowed range of enum lit vals)
5072 -- Minimum and maximum values of entries
5075 -- Pointer to node for literal providing max value
5078 if Ignore_Rep_Clauses then
5082 -- Ignore enumeration rep clauses by default in CodePeer mode,
5083 -- unless -gnatd.I is specified, as a work around for potential false
5084 -- positive messages.
5086 if CodePeer_Mode and not Debug_Flag_Dot_II then
5090 -- First some basic error checks
5093 Enumtype := Entity (Ident);
5095 if Enumtype = Any_Type
5096 or else Rep_Item_Too_Early (Enumtype, N)
5100 Enumtype := Underlying_Type (Enumtype);
5103 if not Is_Enumeration_Type (Enumtype) then
5105 ("enumeration type required, found}",
5106 Ident, First_Subtype (Enumtype));
5110 -- Ignore rep clause on generic actual type. This will already have
5111 -- been flagged on the template as an error, and this is the safest
5112 -- way to ensure we don't get a junk cascaded message in the instance.
5114 if Is_Generic_Actual_Type (Enumtype) then
5117 -- Type must be in current scope
5119 elsif Scope (Enumtype) /= Current_Scope then
5120 Error_Msg_N ("type must be declared in this scope", Ident);
5123 -- Type must be a first subtype
5125 elsif not Is_First_Subtype (Enumtype) then
5126 Error_Msg_N ("cannot give enumeration rep clause for subtype", N);
5129 -- Ignore duplicate rep clause
5131 elsif Has_Enumeration_Rep_Clause (Enumtype) then
5132 Error_Msg_N ("duplicate enumeration rep clause ignored", N);
5135 -- Don't allow rep clause for standard [wide_[wide_]]character
5137 elsif Is_Standard_Character_Type (Enumtype) then
5138 Error_Msg_N ("enumeration rep clause not allowed for this type", N);
5141 -- Check that the expression is a proper aggregate (no parentheses)
5143 elsif Paren_Count (Aggr) /= 0 then
5145 ("extra parentheses surrounding aggregate not allowed",
5149 -- All tests passed, so set rep clause in place
5152 Set_Has_Enumeration_Rep_Clause (Enumtype);
5153 Set_Has_Enumeration_Rep_Clause (Base_Type (Enumtype));
5156 -- Now we process the aggregate. Note that we don't use the normal
5157 -- aggregate code for this purpose, because we don't want any of the
5158 -- normal expansion activities, and a number of special semantic
5159 -- rules apply (including the component type being any integer type)
5161 Elit := First_Literal (Enumtype);
5163 -- First the positional entries if any
5165 if Present (Expressions (Aggr)) then
5166 Expr := First (Expressions (Aggr));
5167 while Present (Expr) loop
5169 Error_Msg_N ("too many entries in aggregate", Expr);
5173 Val := Static_Integer (Expr);
5175 -- Err signals that we found some incorrect entries processing
5176 -- the list. The final checks for completeness and ordering are
5177 -- skipped in this case.
5179 if Val = No_Uint then
5181 elsif Val < Lo or else Hi < Val then
5182 Error_Msg_N ("value outside permitted range", Expr);
5186 Set_Enumeration_Rep (Elit, Val);
5187 Set_Enumeration_Rep_Expr (Elit, Expr);
5193 -- Now process the named entries if present
5195 if Present (Component_Associations (Aggr)) then
5196 Assoc := First (Component_Associations (Aggr));
5197 while Present (Assoc) loop
5198 Choice := First (Choices (Assoc));
5200 if Present (Next (Choice)) then
5202 ("multiple choice not allowed here", Next (Choice));
5206 if Nkind (Choice) = N_Others_Choice then
5207 Error_Msg_N ("others choice not allowed here", Choice);
5210 elsif Nkind (Choice) = N_Range then
5212 -- ??? should allow zero/one element range here
5214 Error_Msg_N ("range not allowed here", Choice);
5218 Analyze_And_Resolve (Choice, Enumtype);
5220 if Error_Posted (Choice) then
5225 if Is_Entity_Name (Choice)
5226 and then Is_Type (Entity (Choice))
5228 Error_Msg_N ("subtype name not allowed here", Choice);
5231 -- ??? should allow static subtype with zero/one entry
5233 elsif Etype (Choice) = Base_Type (Enumtype) then
5234 if not Is_Static_Expression (Choice) then
5235 Flag_Non_Static_Expr
5236 ("non-static expression used for choice!", Choice);
5240 Elit := Expr_Value_E (Choice);
5242 if Present (Enumeration_Rep_Expr (Elit)) then
5244 Sloc (Enumeration_Rep_Expr (Elit));
5246 ("representation for& previously given#",
5251 Set_Enumeration_Rep_Expr (Elit, Expression (Assoc));
5253 Expr := Expression (Assoc);
5254 Val := Static_Integer (Expr);
5256 if Val = No_Uint then
5259 elsif Val < Lo or else Hi < Val then
5260 Error_Msg_N ("value outside permitted range", Expr);
5264 Set_Enumeration_Rep (Elit, Val);
5274 -- Aggregate is fully processed. Now we check that a full set of
5275 -- representations was given, and that they are in range and in order.
5276 -- These checks are only done if no other errors occurred.
5282 Elit := First_Literal (Enumtype);
5283 while Present (Elit) loop
5284 if No (Enumeration_Rep_Expr (Elit)) then
5285 Error_Msg_NE ("missing representation for&!", N, Elit);
5288 Val := Enumeration_Rep (Elit);
5290 if Min = No_Uint then
5294 if Val /= No_Uint then
5295 if Max /= No_Uint and then Val <= Max then
5297 ("enumeration value for& not ordered!",
5298 Enumeration_Rep_Expr (Elit), Elit);
5301 Max_Node := Enumeration_Rep_Expr (Elit);
5305 -- If there is at least one literal whose representation is not
5306 -- equal to the Pos value, then note that this enumeration type
5307 -- has a non-standard representation.
5309 if Val /= Enumeration_Pos (Elit) then
5310 Set_Has_Non_Standard_Rep (Base_Type (Enumtype));
5317 -- Now set proper size information
5320 Minsize : Uint := UI_From_Int (Minimum_Size (Enumtype));
5323 if Has_Size_Clause (Enumtype) then
5325 -- All OK, if size is OK now
5327 if RM_Size (Enumtype) >= Minsize then
5331 -- Try if we can get by with biasing
5334 UI_From_Int (Minimum_Size (Enumtype, Biased => True));
5336 -- Error message if even biasing does not work
5338 if RM_Size (Enumtype) < Minsize then
5339 Error_Msg_Uint_1 := RM_Size (Enumtype);
5340 Error_Msg_Uint_2 := Max;
5342 ("previously given size (^) is too small "
5343 & "for this value (^)", Max_Node);
5345 -- If biasing worked, indicate that we now have biased rep
5349 (Enumtype, Size_Clause (Enumtype), "size clause");
5354 Set_RM_Size (Enumtype, Minsize);
5355 Set_Enum_Esize (Enumtype);
5358 Set_RM_Size (Base_Type (Enumtype), RM_Size (Enumtype));
5359 Set_Esize (Base_Type (Enumtype), Esize (Enumtype));
5360 Set_Alignment (Base_Type (Enumtype), Alignment (Enumtype));
5364 -- We repeat the too late test in case it froze itself!
5366 if Rep_Item_Too_Late (Enumtype, N) then
5369 end Analyze_Enumeration_Representation_Clause;
5371 ----------------------------
5372 -- Analyze_Free_Statement --
5373 ----------------------------
5375 procedure Analyze_Free_Statement (N : Node_Id) is
5377 Analyze (Expression (N));
5378 end Analyze_Free_Statement;
5380 ---------------------------
5381 -- Analyze_Freeze_Entity --
5382 ---------------------------
5384 procedure Analyze_Freeze_Entity (N : Node_Id) is
5386 Freeze_Entity_Checks (N);
5387 end Analyze_Freeze_Entity;
5389 -----------------------------------
5390 -- Analyze_Freeze_Generic_Entity --
5391 -----------------------------------
5393 procedure Analyze_Freeze_Generic_Entity (N : Node_Id) is
5395 Freeze_Entity_Checks (N);
5396 end Analyze_Freeze_Generic_Entity;
5398 ------------------------------------------
5399 -- Analyze_Record_Representation_Clause --
5400 ------------------------------------------
5402 -- Note: we check as much as we can here, but we can't do any checks
5403 -- based on the position values (e.g. overlap checks) until freeze time
5404 -- because especially in Ada 2005 (machine scalar mode), the processing
5405 -- for non-standard bit order can substantially change the positions.
5406 -- See procedure Check_Record_Representation_Clause (called from Freeze)
5407 -- for the remainder of this processing.
5409 procedure Analyze_Record_Representation_Clause (N : Node_Id) is
5410 Ident : constant Node_Id := Identifier (N);
5415 Hbit : Uint := Uint_0;
5419 Rectype : Entity_Id;
5422 function Is_Inherited (Comp : Entity_Id) return Boolean;
5423 -- True if Comp is an inherited component in a record extension
5429 function Is_Inherited (Comp : Entity_Id) return Boolean is
5430 Comp_Base : Entity_Id;
5433 if Ekind (Rectype) = E_Record_Subtype then
5434 Comp_Base := Original_Record_Component (Comp);
5439 return Comp_Base /= Original_Record_Component (Comp_Base);
5444 Is_Record_Extension : Boolean;
5445 -- True if Rectype is a record extension
5447 CR_Pragma : Node_Id := Empty;
5448 -- Points to N_Pragma node if Complete_Representation pragma present
5450 -- Start of processing for Analyze_Record_Representation_Clause
5453 if Ignore_Rep_Clauses then
5458 Rectype := Entity (Ident);
5460 if Rectype = Any_Type or else Rep_Item_Too_Early (Rectype, N) then
5463 Rectype := Underlying_Type (Rectype);
5466 -- First some basic error checks
5468 if not Is_Record_Type (Rectype) then
5470 ("record type required, found}", Ident, First_Subtype (Rectype));
5473 elsif Scope (Rectype) /= Current_Scope then
5474 Error_Msg_N ("type must be declared in this scope", N);
5477 elsif not Is_First_Subtype (Rectype) then
5478 Error_Msg_N ("cannot give record rep clause for subtype", N);
5481 elsif Has_Record_Rep_Clause (Rectype) then
5482 Error_Msg_N ("duplicate record rep clause ignored", N);
5485 elsif Rep_Item_Too_Late (Rectype, N) then
5489 -- We know we have a first subtype, now possibly go the the anonymous
5490 -- base type to determine whether Rectype is a record extension.
5492 Recdef := Type_Definition (Declaration_Node (Base_Type (Rectype)));
5493 Is_Record_Extension :=
5494 Nkind (Recdef) = N_Derived_Type_Definition
5495 and then Present (Record_Extension_Part (Recdef));
5497 if Present (Mod_Clause (N)) then
5499 Loc : constant Source_Ptr := Sloc (N);
5500 M : constant Node_Id := Mod_Clause (N);
5501 P : constant List_Id := Pragmas_Before (M);
5505 pragma Warnings (Off, Mod_Val);
5508 Check_Restriction (No_Obsolescent_Features, Mod_Clause (N));
5510 if Warn_On_Obsolescent_Feature then
5512 ("?j?mod clause is an obsolescent feature (RM J.8)", N);
5514 ("\?j?use alignment attribute definition clause instead", N);
5521 -- In ASIS_Mode mode, expansion is disabled, but we must convert
5522 -- the Mod clause into an alignment clause anyway, so that the
5523 -- back-end can compute and back-annotate properly the size and
5524 -- alignment of types that may include this record.
5526 -- This seems dubious, this destroys the source tree in a manner
5527 -- not detectable by ASIS ???
5529 if Operating_Mode = Check_Semantics and then ASIS_Mode then
5531 Make_Attribute_Definition_Clause (Loc,
5532 Name => New_Reference_To (Base_Type (Rectype), Loc),
5533 Chars => Name_Alignment,
5534 Expression => Relocate_Node (Expression (M)));
5536 Set_From_At_Mod (AtM_Nod);
5537 Insert_After (N, AtM_Nod);
5538 Mod_Val := Get_Alignment_Value (Expression (AtM_Nod));
5539 Set_Mod_Clause (N, Empty);
5542 -- Get the alignment value to perform error checking
5544 Mod_Val := Get_Alignment_Value (Expression (M));
5549 -- For untagged types, clear any existing component clauses for the
5550 -- type. If the type is derived, this is what allows us to override
5551 -- a rep clause for the parent. For type extensions, the representation
5552 -- of the inherited components is inherited, so we want to keep previous
5553 -- component clauses for completeness.
5555 if not Is_Tagged_Type (Rectype) then
5556 Comp := First_Component_Or_Discriminant (Rectype);
5557 while Present (Comp) loop
5558 Set_Component_Clause (Comp, Empty);
5559 Next_Component_Or_Discriminant (Comp);
5563 -- All done if no component clauses
5565 CC := First (Component_Clauses (N));
5571 -- A representation like this applies to the base type
5573 Set_Has_Record_Rep_Clause (Base_Type (Rectype));
5574 Set_Has_Non_Standard_Rep (Base_Type (Rectype));
5575 Set_Has_Specified_Layout (Base_Type (Rectype));
5577 -- Process the component clauses
5579 while Present (CC) loop
5583 if Nkind (CC) = N_Pragma then
5586 -- The only pragma of interest is Complete_Representation
5588 if Pragma_Name (CC) = Name_Complete_Representation then
5592 -- Processing for real component clause
5595 Posit := Static_Integer (Position (CC));
5596 Fbit := Static_Integer (First_Bit (CC));
5597 Lbit := Static_Integer (Last_Bit (CC));
5600 and then Fbit /= No_Uint
5601 and then Lbit /= No_Uint
5605 ("position cannot be negative", Position (CC));
5609 ("first bit cannot be negative", First_Bit (CC));
5611 -- The Last_Bit specified in a component clause must not be
5612 -- less than the First_Bit minus one (RM-13.5.1(10)).
5614 elsif Lbit < Fbit - 1 then
5616 ("last bit cannot be less than first bit minus one",
5619 -- Values look OK, so find the corresponding record component
5620 -- Even though the syntax allows an attribute reference for
5621 -- implementation-defined components, GNAT does not allow the
5622 -- tag to get an explicit position.
5624 elsif Nkind (Component_Name (CC)) = N_Attribute_Reference then
5625 if Attribute_Name (Component_Name (CC)) = Name_Tag then
5626 Error_Msg_N ("position of tag cannot be specified", CC);
5628 Error_Msg_N ("illegal component name", CC);
5632 Comp := First_Entity (Rectype);
5633 while Present (Comp) loop
5634 exit when Chars (Comp) = Chars (Component_Name (CC));
5640 -- Maybe component of base type that is absent from
5641 -- statically constrained first subtype.
5643 Comp := First_Entity (Base_Type (Rectype));
5644 while Present (Comp) loop
5645 exit when Chars (Comp) = Chars (Component_Name (CC));
5652 ("component clause is for non-existent field", CC);
5654 -- Ada 2012 (AI05-0026): Any name that denotes a
5655 -- discriminant of an object of an unchecked union type
5656 -- shall not occur within a record_representation_clause.
5658 -- The general restriction of using record rep clauses on
5659 -- Unchecked_Union types has now been lifted. Since it is
5660 -- possible to introduce a record rep clause which mentions
5661 -- the discriminant of an Unchecked_Union in non-Ada 2012
5662 -- code, this check is applied to all versions of the
5665 elsif Ekind (Comp) = E_Discriminant
5666 and then Is_Unchecked_Union (Rectype)
5669 ("cannot reference discriminant of unchecked union",
5670 Component_Name (CC));
5672 elsif Is_Record_Extension and then Is_Inherited (Comp) then
5674 ("component clause not allowed for inherited "
5675 & "component&", CC, Comp);
5677 elsif Present (Component_Clause (Comp)) then
5679 -- Diagnose duplicate rep clause, or check consistency
5680 -- if this is an inherited component. In a double fault,
5681 -- there may be a duplicate inconsistent clause for an
5682 -- inherited component.
5684 if Scope (Original_Record_Component (Comp)) = Rectype
5685 or else Parent (Component_Clause (Comp)) = N
5687 Error_Msg_Sloc := Sloc (Component_Clause (Comp));
5688 Error_Msg_N ("component clause previously given#", CC);
5692 Rep1 : constant Node_Id := Component_Clause (Comp);
5694 if Intval (Position (Rep1)) /=
5695 Intval (Position (CC))
5696 or else Intval (First_Bit (Rep1)) /=
5697 Intval (First_Bit (CC))
5698 or else Intval (Last_Bit (Rep1)) /=
5699 Intval (Last_Bit (CC))
5702 ("component clause inconsistent "
5703 & "with representation of ancestor", CC);
5705 elsif Warn_On_Redundant_Constructs then
5707 ("?r?redundant confirming component clause "
5708 & "for component!", CC);
5713 -- Normal case where this is the first component clause we
5714 -- have seen for this entity, so set it up properly.
5717 -- Make reference for field in record rep clause and set
5718 -- appropriate entity field in the field identifier.
5721 (Comp, Component_Name (CC), Set_Ref => False);
5722 Set_Entity (Component_Name (CC), Comp);
5724 -- Update Fbit and Lbit to the actual bit number
5726 Fbit := Fbit + UI_From_Int (SSU) * Posit;
5727 Lbit := Lbit + UI_From_Int (SSU) * Posit;
5729 if Has_Size_Clause (Rectype)
5730 and then RM_Size (Rectype) <= Lbit
5733 ("bit number out of range of specified size",
5736 Set_Component_Clause (Comp, CC);
5737 Set_Component_Bit_Offset (Comp, Fbit);
5738 Set_Esize (Comp, 1 + (Lbit - Fbit));
5739 Set_Normalized_First_Bit (Comp, Fbit mod SSU);
5740 Set_Normalized_Position (Comp, Fbit / SSU);
5742 if Warn_On_Overridden_Size
5743 and then Has_Size_Clause (Etype (Comp))
5744 and then RM_Size (Etype (Comp)) /= Esize (Comp)
5747 ("?S?component size overrides size clause for&",
5748 Component_Name (CC), Etype (Comp));
5751 -- This information is also set in the corresponding
5752 -- component of the base type, found by accessing the
5753 -- Original_Record_Component link if it is present.
5755 Ocomp := Original_Record_Component (Comp);
5762 (Component_Name (CC),
5768 (Comp, First_Node (CC), "component clause", Biased);
5770 if Present (Ocomp) then
5771 Set_Component_Clause (Ocomp, CC);
5772 Set_Component_Bit_Offset (Ocomp, Fbit);
5773 Set_Normalized_First_Bit (Ocomp, Fbit mod SSU);
5774 Set_Normalized_Position (Ocomp, Fbit / SSU);
5775 Set_Esize (Ocomp, 1 + (Lbit - Fbit));
5777 Set_Normalized_Position_Max
5778 (Ocomp, Normalized_Position (Ocomp));
5780 -- Note: we don't use Set_Biased here, because we
5781 -- already gave a warning above if needed, and we
5782 -- would get a duplicate for the same name here.
5784 Set_Has_Biased_Representation
5785 (Ocomp, Has_Biased_Representation (Comp));
5788 if Esize (Comp) < 0 then
5789 Error_Msg_N ("component size is negative", CC);
5800 -- Check missing components if Complete_Representation pragma appeared
5802 if Present (CR_Pragma) then
5803 Comp := First_Component_Or_Discriminant (Rectype);
5804 while Present (Comp) loop
5805 if No (Component_Clause (Comp)) then
5807 ("missing component clause for &", CR_Pragma, Comp);
5810 Next_Component_Or_Discriminant (Comp);
5813 -- Give missing components warning if required
5815 elsif Warn_On_Unrepped_Components then
5817 Num_Repped_Components : Nat := 0;
5818 Num_Unrepped_Components : Nat := 0;
5821 -- First count number of repped and unrepped components
5823 Comp := First_Component_Or_Discriminant (Rectype);
5824 while Present (Comp) loop
5825 if Present (Component_Clause (Comp)) then
5826 Num_Repped_Components := Num_Repped_Components + 1;
5828 Num_Unrepped_Components := Num_Unrepped_Components + 1;
5831 Next_Component_Or_Discriminant (Comp);
5834 -- We are only interested in the case where there is at least one
5835 -- unrepped component, and at least half the components have rep
5836 -- clauses. We figure that if less than half have them, then the
5837 -- partial rep clause is really intentional. If the component
5838 -- type has no underlying type set at this point (as for a generic
5839 -- formal type), we don't know enough to give a warning on the
5842 if Num_Unrepped_Components > 0
5843 and then Num_Unrepped_Components < Num_Repped_Components
5845 Comp := First_Component_Or_Discriminant (Rectype);
5846 while Present (Comp) loop
5847 if No (Component_Clause (Comp))
5848 and then Comes_From_Source (Comp)
5849 and then Present (Underlying_Type (Etype (Comp)))
5850 and then (Is_Scalar_Type (Underlying_Type (Etype (Comp)))
5851 or else Size_Known_At_Compile_Time
5852 (Underlying_Type (Etype (Comp))))
5853 and then not Has_Warnings_Off (Rectype)
5855 Error_Msg_Sloc := Sloc (Comp);
5857 ("?C?no component clause given for & declared #",
5861 Next_Component_Or_Discriminant (Comp);
5866 end Analyze_Record_Representation_Clause;
5868 -------------------------------------------
5869 -- Build_Invariant_Procedure_Declaration --
5870 -------------------------------------------
5872 function Build_Invariant_Procedure_Declaration
5873 (Typ : Entity_Id) return Node_Id
5875 Loc : constant Source_Ptr := Sloc (Typ);
5876 Object_Entity : constant Entity_Id :=
5877 Make_Defining_Identifier (Loc, New_Internal_Name ('I'));
5882 Set_Etype (Object_Entity, Typ);
5884 -- Check for duplicate definiations.
5886 if Has_Invariants (Typ) and then Present (Invariant_Procedure (Typ)) then
5891 Make_Defining_Identifier (Loc,
5892 Chars => New_External_Name (Chars (Typ), "Invariant"));
5893 Set_Has_Invariants (Typ);
5894 Set_Ekind (SId, E_Procedure);
5895 Set_Is_Invariant_Procedure (SId);
5896 Set_Invariant_Procedure (Typ, SId);
5899 Make_Procedure_Specification (Loc,
5900 Defining_Unit_Name => SId,
5901 Parameter_Specifications => New_List (
5902 Make_Parameter_Specification (Loc,
5903 Defining_Identifier => Object_Entity,
5904 Parameter_Type => New_Occurrence_Of (Typ, Loc))));
5906 return Make_Subprogram_Declaration (Loc, Specification => Spec);
5907 end Build_Invariant_Procedure_Declaration;
5909 -------------------------------
5910 -- Build_Invariant_Procedure --
5911 -------------------------------
5913 -- The procedure that is constructed here has the form
5915 -- procedure typInvariant (Ixxx : typ) is
5917 -- pragma Check (Invariant, exp, "failed invariant from xxx");
5918 -- pragma Check (Invariant, exp, "failed invariant from xxx");
5920 -- pragma Check (Invariant, exp, "failed inherited invariant from xxx");
5922 -- end typInvariant;
5924 procedure Build_Invariant_Procedure (Typ : Entity_Id; N : Node_Id) is
5925 Loc : constant Source_Ptr := Sloc (Typ);
5932 Visible_Decls : constant List_Id := Visible_Declarations (N);
5933 Private_Decls : constant List_Id := Private_Declarations (N);
5935 procedure Add_Invariants (T : Entity_Id; Inherit : Boolean);
5936 -- Appends statements to Stmts for any invariants in the rep item chain
5937 -- of the given type. If Inherit is False, then we only process entries
5938 -- on the chain for the type Typ. If Inherit is True, then we ignore any
5939 -- Invariant aspects, but we process all Invariant'Class aspects, adding
5940 -- "inherited" to the exception message and generating an informational
5941 -- message about the inheritance of an invariant.
5943 Object_Name : Name_Id;
5944 -- Name for argument of invariant procedure
5946 Object_Entity : Node_Id;
5947 -- The entity of the formal for the procedure
5949 --------------------
5950 -- Add_Invariants --
5951 --------------------
5953 procedure Add_Invariants (T : Entity_Id; Inherit : Boolean) is
5963 procedure Replace_Type_Reference (N : Node_Id);
5964 -- Replace a single occurrence N of the subtype name with a reference
5965 -- to the formal of the predicate function. N can be an identifier
5966 -- referencing the subtype, or a selected component, representing an
5967 -- appropriately qualified occurrence of the subtype name.
5969 procedure Replace_Type_References is
5970 new Replace_Type_References_Generic (Replace_Type_Reference);
5971 -- Traverse an expression replacing all occurrences of the subtype
5972 -- name with appropriate references to the object that is the formal
5973 -- parameter of the predicate function. Note that we must ensure
5974 -- that the type and entity information is properly set in the
5975 -- replacement node, since we will do a Preanalyze call of this
5976 -- expression without proper visibility of the procedure argument.
5978 ----------------------------
5979 -- Replace_Type_Reference --
5980 ----------------------------
5982 -- Note: See comments in Add_Predicates.Replace_Type_Reference
5983 -- regarding handling of Sloc and Comes_From_Source.
5985 procedure Replace_Type_Reference (N : Node_Id) is
5988 -- Add semantic information to node to be rewritten, for ASIS
5989 -- navigation needs.
5991 if Nkind (N) = N_Identifier then
5995 elsif Nkind (N) = N_Selected_Component then
5996 Analyze (Prefix (N));
5997 Set_Entity (Selector_Name (N), T);
5998 Set_Etype (Selector_Name (N), T);
6001 -- Invariant'Class, replace with T'Class (obj)
6003 if Class_Present (Ritem) then
6005 Make_Type_Conversion (Sloc (N),
6007 Make_Attribute_Reference (Sloc (N),
6008 Prefix => New_Occurrence_Of (T, Sloc (N)),
6009 Attribute_Name => Name_Class),
6010 Expression => Make_Identifier (Sloc (N), Object_Name)));
6012 Set_Entity (Expression (N), Object_Entity);
6013 Set_Etype (Expression (N), Typ);
6015 -- Invariant, replace with obj
6018 Rewrite (N, Make_Identifier (Sloc (N), Object_Name));
6019 Set_Entity (N, Object_Entity);
6023 Set_Comes_From_Source (N, True);
6024 end Replace_Type_Reference;
6026 -- Start of processing for Add_Invariants
6029 Ritem := First_Rep_Item (T);
6030 while Present (Ritem) loop
6031 if Nkind (Ritem) = N_Pragma
6032 and then Pragma_Name (Ritem) = Name_Invariant
6034 Arg1 := First (Pragma_Argument_Associations (Ritem));
6035 Arg2 := Next (Arg1);
6036 Arg3 := Next (Arg2);
6038 Arg1 := Get_Pragma_Arg (Arg1);
6039 Arg2 := Get_Pragma_Arg (Arg2);
6041 -- For Inherit case, ignore Invariant, process only Class case
6044 if not Class_Present (Ritem) then
6048 -- For Inherit false, process only item for right type
6051 if Entity (Arg1) /= Typ then
6057 Stmts := Empty_List;
6060 Exp := New_Copy_Tree (Arg2);
6062 -- Preserve sloc of original pragma Invariant
6064 Loc := Sloc (Ritem);
6066 -- We need to replace any occurrences of the name of the type
6067 -- with references to the object, converted to type'Class in
6068 -- the case of Invariant'Class aspects.
6070 Replace_Type_References (Exp, Chars (T));
6072 -- If this invariant comes from an aspect, find the aspect
6073 -- specification, and replace the saved expression because
6074 -- we need the subtype references replaced for the calls to
6075 -- Preanalyze_Spec_Expressin in Check_Aspect_At_Freeze_Point
6076 -- and Check_Aspect_At_End_Of_Declarations.
6078 if From_Aspect_Specification (Ritem) then
6083 -- Loop to find corresponding aspect, note that this
6084 -- must be present given the pragma is marked delayed.
6086 Aitem := Next_Rep_Item (Ritem);
6087 while Present (Aitem) loop
6088 if Nkind (Aitem) = N_Aspect_Specification
6089 and then Aspect_Rep_Item (Aitem) = Ritem
6092 (Identifier (Aitem), New_Copy_Tree (Exp));
6096 Aitem := Next_Rep_Item (Aitem);
6101 -- Now we need to preanalyze the expression to properly capture
6102 -- the visibility in the visible part. The expression will not
6103 -- be analyzed for real until the body is analyzed, but that is
6104 -- at the end of the private part and has the wrong visibility.
6106 Set_Parent (Exp, N);
6107 Preanalyze_Assert_Expression (Exp, Standard_Boolean);
6109 -- In ASIS mode, even if assertions are not enabled, we must
6110 -- analyze the original expression in the aspect specification
6111 -- because it is part of the original tree.
6115 Inv : constant Node_Id :=
6116 Expression (Corresponding_Aspect (Ritem));
6118 Replace_Type_References (Inv, Chars (T));
6119 Preanalyze_Assert_Expression (Inv, Standard_Boolean);
6123 -- Build first two arguments for Check pragma
6126 Make_Pragma_Argument_Association (Loc,
6127 Expression => Make_Identifier (Loc, Name_Invariant)),
6128 Make_Pragma_Argument_Association (Loc,
6129 Expression => Exp));
6131 -- Add message if present in Invariant pragma
6133 if Present (Arg3) then
6134 Str := Strval (Get_Pragma_Arg (Arg3));
6136 -- If inherited case, and message starts "failed invariant",
6137 -- change it to be "failed inherited invariant".
6140 String_To_Name_Buffer (Str);
6142 if Name_Buffer (1 .. 16) = "failed invariant" then
6143 Insert_Str_In_Name_Buffer ("inherited ", 8);
6144 Str := String_From_Name_Buffer;
6149 Make_Pragma_Argument_Association (Loc,
6150 Expression => Make_String_Literal (Loc, Str)));
6153 -- Add Check pragma to list of statements
6157 Pragma_Identifier =>
6158 Make_Identifier (Loc, Name_Check),
6159 Pragma_Argument_Associations => Assoc));
6161 -- If Inherited case and option enabled, output info msg. Note
6162 -- that we know this is a case of Invariant'Class.
6164 if Inherit and Opt.List_Inherited_Aspects then
6165 Error_Msg_Sloc := Sloc (Ritem);
6167 ("?L?info: & inherits `Invariant''Class` aspect from #",
6173 Next_Rep_Item (Ritem);
6177 -- Start of processing for Build_Invariant_Procedure
6185 -- If the aspect specification exists for some view of the type, the
6186 -- declaration for the procedure has been created.
6188 if Has_Invariants (Typ) then
6189 SId := Invariant_Procedure (Typ);
6192 if Present (SId) then
6193 PDecl := Unit_Declaration_Node (SId);
6195 PDecl := Build_Invariant_Procedure_Declaration (Typ);
6198 -- Recover formal of procedure, for use in the calls to invariant
6199 -- functions (including inherited ones).
6203 (First (Parameter_Specifications (Specification (PDecl))));
6204 Object_Name := Chars (Object_Entity);
6206 -- Add invariants for the current type
6208 Add_Invariants (Typ, Inherit => False);
6210 -- Add invariants for parent types
6213 Current_Typ : Entity_Id;
6214 Parent_Typ : Entity_Id;
6219 Parent_Typ := Etype (Current_Typ);
6221 if Is_Private_Type (Parent_Typ)
6222 and then Present (Full_View (Base_Type (Parent_Typ)))
6224 Parent_Typ := Full_View (Base_Type (Parent_Typ));
6227 exit when Parent_Typ = Current_Typ;
6229 Current_Typ := Parent_Typ;
6230 Add_Invariants (Current_Typ, Inherit => True);
6234 -- Build the procedure if we generated at least one Check pragma
6236 if Stmts /= No_List then
6237 Spec := Copy_Separate_Tree (Specification (PDecl));
6240 Make_Subprogram_Body (Loc,
6241 Specification => Spec,
6242 Declarations => Empty_List,
6243 Handled_Statement_Sequence =>
6244 Make_Handled_Sequence_Of_Statements (Loc,
6245 Statements => Stmts));
6247 -- Insert procedure declaration and spec at the appropriate points.
6248 -- If declaration is already analyzed, it was processed by the
6249 -- generated pragma.
6251 if Present (Private_Decls) then
6253 -- The spec goes at the end of visible declarations, but they have
6254 -- already been analyzed, so we need to explicitly do the analyze.
6256 if not Analyzed (PDecl) then
6257 Append_To (Visible_Decls, PDecl);
6261 -- The body goes at the end of the private declarations, which we
6262 -- have not analyzed yet, so we do not need to perform an explicit
6263 -- analyze call. We skip this if there are no private declarations
6264 -- (this is an error that will be caught elsewhere);
6266 Append_To (Private_Decls, PBody);
6268 -- If the invariant appears on the full view of a type, the
6269 -- analysis of the private part is complete, and we must
6270 -- analyze the new body explicitly.
6272 if In_Private_Part (Current_Scope) then
6276 -- If there are no private declarations this may be an error that
6277 -- will be diagnosed elsewhere. However, if this is a non-private
6278 -- type that inherits invariants, it needs no completion and there
6279 -- may be no private part. In this case insert invariant procedure
6280 -- at end of current declarative list, and analyze at once, given
6281 -- that the type is about to be frozen.
6283 elsif not Is_Private_Type (Typ) then
6284 Append_To (Visible_Decls, PDecl);
6285 Append_To (Visible_Decls, PBody);
6290 end Build_Invariant_Procedure;
6292 -------------------------------
6293 -- Build_Predicate_Functions --
6294 -------------------------------
6296 -- The procedures that are constructed here have the form:
6298 -- function typPredicate (Ixxx : typ) return Boolean is
6301 -- exp1 and then exp2 and then ...
6302 -- and then typ1Predicate (typ1 (Ixxx))
6303 -- and then typ2Predicate (typ2 (Ixxx))
6305 -- end typPredicate;
6307 -- Here exp1, and exp2 are expressions from Predicate pragmas. Note that
6308 -- this is the point at which these expressions get analyzed, providing the
6309 -- required delay, and typ1, typ2, are entities from which predicates are
6310 -- inherited. Note that we do NOT generate Check pragmas, that's because we
6311 -- use this function even if checks are off, e.g. for membership tests.
6313 -- If the expression has at least one Raise_Expression, then we also build
6314 -- the typPredicateM version of the function, in which any occurrence of a
6315 -- Raise_Expression is converted to "return False".
6317 procedure Build_Predicate_Functions (Typ : Entity_Id; N : Node_Id) is
6318 Loc : constant Source_Ptr := Sloc (Typ);
6321 -- This is the expression for the result of the function. It is
6322 -- is build by connecting the component predicates with AND THEN.
6325 -- This is the corresponding return expression for the Predicate_M
6326 -- function. It differs in that raise expressions are marked for
6327 -- special expansion (see Process_REs).
6329 Object_Name : constant Name_Id := New_Internal_Name ('I');
6330 -- Name for argument of Predicate procedure. Note that we use the same
6331 -- name for both predicate procedure. That way the reference within the
6332 -- predicate expression is the same in both functions.
6334 Object_Entity : constant Entity_Id :=
6335 Make_Defining_Identifier (Loc, Chars => Object_Name);
6336 -- Entity for argument of Predicate procedure
6338 Object_Entity_M : constant Entity_Id :=
6339 Make_Defining_Identifier (Loc, Chars => Object_Name);
6340 -- Entity for argument of Predicate_M procedure
6342 Raise_Expression_Present : Boolean := False;
6343 -- Set True if Expr has at least one Raise_Expression
6345 Static_Predic : Node_Id := Empty;
6346 -- Set to N_Pragma node for a static predicate if one is encountered
6348 procedure Add_Call (T : Entity_Id);
6349 -- Includes a call to the predicate function for type T in Expr if T
6350 -- has predicates and Predicate_Function (T) is non-empty.
6352 procedure Add_Predicates;
6353 -- Appends expressions for any Predicate pragmas in the rep item chain
6354 -- Typ to Expr. Note that we look only at items for this exact entity.
6355 -- Inheritance of predicates for the parent type is done by calling the
6356 -- Predicate_Function of the parent type, using Add_Call above.
6358 function Test_RE (N : Node_Id) return Traverse_Result;
6359 -- Used in Test_REs, tests one node for being a raise expression, and if
6360 -- so sets Raise_Expression_Present True.
6362 procedure Test_REs is new Traverse_Proc (Test_RE);
6363 -- Tests to see if Expr contains any raise expressions
6365 function Process_RE (N : Node_Id) return Traverse_Result;
6366 -- Used in Process REs, tests if node N is a raise expression, and if
6367 -- so, marks it to be converted to return False.
6369 procedure Process_REs is new Traverse_Proc (Process_RE);
6370 -- Marks any raise expressions in Expr_M to return False
6376 procedure Add_Call (T : Entity_Id) is
6380 if Present (T) and then Present (Predicate_Function (T)) then
6381 Set_Has_Predicates (Typ);
6383 -- Build the call to the predicate function of T
6387 (T, Convert_To (T, Make_Identifier (Loc, Object_Name)));
6389 -- Add call to evolving expression, using AND THEN if needed
6396 Left_Opnd => Relocate_Node (Expr),
6400 -- Output info message on inheritance if required. Note we do not
6401 -- give this information for generic actual types, since it is
6402 -- unwelcome noise in that case in instantiations. We also
6403 -- generally suppress the message in instantiations, and also
6404 -- if it involves internal names.
6406 if Opt.List_Inherited_Aspects
6407 and then not Is_Generic_Actual_Type (Typ)
6408 and then Instantiation_Depth (Sloc (Typ)) = 0
6409 and then not Is_Internal_Name (Chars (T))
6410 and then not Is_Internal_Name (Chars (Typ))
6412 Error_Msg_Sloc := Sloc (Predicate_Function (T));
6413 Error_Msg_Node_2 := T;
6414 Error_Msg_N ("info: & inherits predicate from & #?L?", Typ);
6419 --------------------
6420 -- Add_Predicates --
6421 --------------------
6423 procedure Add_Predicates is
6428 procedure Replace_Type_Reference (N : Node_Id);
6429 -- Replace a single occurrence N of the subtype name with a reference
6430 -- to the formal of the predicate function. N can be an identifier
6431 -- referencing the subtype, or a selected component, representing an
6432 -- appropriately qualified occurrence of the subtype name.
6434 procedure Replace_Type_References is
6435 new Replace_Type_References_Generic (Replace_Type_Reference);
6436 -- Traverse an expression changing every occurrence of an identifier
6437 -- whose name matches the name of the subtype with a reference to
6438 -- the formal parameter of the predicate function.
6440 ----------------------------
6441 -- Replace_Type_Reference --
6442 ----------------------------
6444 procedure Replace_Type_Reference (N : Node_Id) is
6446 Rewrite (N, Make_Identifier (Sloc (N), Object_Name));
6447 -- Use the Sloc of the usage name, not the defining name
6450 Set_Entity (N, Object_Entity);
6452 -- We want to treat the node as if it comes from source, so that
6453 -- ASIS will not ignore it
6455 Set_Comes_From_Source (N, True);
6456 end Replace_Type_Reference;
6458 -- Start of processing for Add_Predicates
6461 Ritem := First_Rep_Item (Typ);
6462 while Present (Ritem) loop
6463 if Nkind (Ritem) = N_Pragma
6464 and then Pragma_Name (Ritem) = Name_Predicate
6466 -- Save the static predicate of the type for diagnostics and
6467 -- error reporting purposes.
6469 if Present (Corresponding_Aspect (Ritem))
6470 and then Chars (Identifier (Corresponding_Aspect (Ritem))) =
6471 Name_Static_Predicate
6473 Static_Predic := Ritem;
6476 -- Acquire arguments
6478 Arg1 := First (Pragma_Argument_Associations (Ritem));
6479 Arg2 := Next (Arg1);
6481 Arg1 := Get_Pragma_Arg (Arg1);
6482 Arg2 := Get_Pragma_Arg (Arg2);
6484 -- See if this predicate pragma is for the current type or for
6485 -- its full view. A predicate on a private completion is placed
6486 -- on the partial view beause this is the visible entity that
6489 if Entity (Arg1) = Typ
6490 or else Full_View (Entity (Arg1)) = Typ
6492 -- We have a match, this entry is for our subtype
6494 -- We need to replace any occurrences of the name of the
6495 -- type with references to the object.
6497 Replace_Type_References (Arg2, Chars (Typ));
6499 -- If this predicate comes from an aspect, find the aspect
6500 -- specification, and replace the saved expression because
6501 -- we need the subtype references replaced for the calls to
6502 -- Preanalyze_Spec_Expressin in Check_Aspect_At_Freeze_Point
6503 -- and Check_Aspect_At_End_Of_Declarations.
6505 if From_Aspect_Specification (Ritem) then
6510 -- Loop to find corresponding aspect, note that this
6511 -- must be present given the pragma is marked delayed.
6513 Aitem := Next_Rep_Item (Ritem);
6515 if Nkind (Aitem) = N_Aspect_Specification
6516 and then Aspect_Rep_Item (Aitem) = Ritem
6519 (Identifier (Aitem), New_Copy_Tree (Arg2));
6523 Aitem := Next_Rep_Item (Aitem);
6528 -- Now we can add the expression
6531 Expr := Relocate_Node (Arg2);
6533 -- There already was a predicate, so add to it
6538 Left_Opnd => Relocate_Node (Expr),
6539 Right_Opnd => Relocate_Node (Arg2));
6544 Next_Rep_Item (Ritem);
6552 function Process_RE (N : Node_Id) return Traverse_Result is
6554 if Nkind (N) = N_Raise_Expression then
6555 Set_Convert_To_Return_False (N);
6566 function Test_RE (N : Node_Id) return Traverse_Result is
6568 if Nkind (N) = N_Raise_Expression then
6569 Raise_Expression_Present := True;
6576 -- Start of processing for Build_Predicate_Functions
6579 -- Return if already built or if type does not have predicates
6581 if not Has_Predicates (Typ)
6582 or else Present (Predicate_Function (Typ))
6587 -- Prepare to construct predicate expression
6591 -- Add Predicates for the current type
6595 -- Add predicates for ancestor if present
6598 Atyp : constant Entity_Id := Nearest_Ancestor (Typ);
6600 if Present (Atyp) then
6605 -- Case where predicates are present
6607 if Present (Expr) then
6609 -- Test for raise expression present
6613 -- If raise expression is present, capture a copy of Expr for use
6614 -- in building the predicateM function version later on. For this
6615 -- copy we replace references to Object_Entity by Object_Entity_M.
6617 if Raise_Expression_Present then
6619 Map : constant Elist_Id := New_Elmt_List;
6621 Append_Elmt (Object_Entity, Map);
6622 Append_Elmt (Object_Entity_M, Map);
6623 Expr_M := New_Copy_Tree (Expr, Map => Map);
6627 -- Build the main predicate function
6630 SId : constant Entity_Id :=
6631 Make_Defining_Identifier (Loc,
6632 Chars => New_External_Name (Chars (Typ), "Predicate"));
6633 -- The entity for the the function spec
6635 SIdB : constant Entity_Id :=
6636 Make_Defining_Identifier (Loc,
6637 Chars => New_External_Name (Chars (Typ), "Predicate"));
6638 -- The entity for the function body
6645 -- Build function declaration
6647 Set_Ekind (SId, E_Function);
6648 Set_Is_Predicate_Function (SId);
6649 Set_Predicate_Function (Typ, SId);
6651 -- The predicate function is shared between views of a type
6653 if Is_Private_Type (Typ) and then Present (Full_View (Typ)) then
6654 Set_Predicate_Function (Full_View (Typ), SId);
6658 Make_Function_Specification (Loc,
6659 Defining_Unit_Name => SId,
6660 Parameter_Specifications => New_List (
6661 Make_Parameter_Specification (Loc,
6662 Defining_Identifier => Object_Entity,
6663 Parameter_Type => New_Occurrence_Of (Typ, Loc))),
6664 Result_Definition =>
6665 New_Occurrence_Of (Standard_Boolean, Loc));
6668 Make_Subprogram_Declaration (Loc,
6669 Specification => Spec);
6671 -- Build function body
6674 Make_Function_Specification (Loc,
6675 Defining_Unit_Name => SIdB,
6676 Parameter_Specifications => New_List (
6677 Make_Parameter_Specification (Loc,
6678 Defining_Identifier =>
6679 Make_Defining_Identifier (Loc, Object_Name),
6681 New_Occurrence_Of (Typ, Loc))),
6682 Result_Definition =>
6683 New_Occurrence_Of (Standard_Boolean, Loc));
6686 Make_Subprogram_Body (Loc,
6687 Specification => Spec,
6688 Declarations => Empty_List,
6689 Handled_Statement_Sequence =>
6690 Make_Handled_Sequence_Of_Statements (Loc,
6691 Statements => New_List (
6692 Make_Simple_Return_Statement (Loc,
6693 Expression => Expr))));
6695 -- Insert declaration before freeze node and body after
6697 Insert_Before_And_Analyze (N, FDecl);
6698 Insert_After_And_Analyze (N, FBody);
6701 -- Test for raise expressions present and if so build M version
6703 if Raise_Expression_Present then
6705 SId : constant Entity_Id :=
6706 Make_Defining_Identifier (Loc,
6707 Chars => New_External_Name (Chars (Typ), "PredicateM"));
6708 -- The entity for the the function spec
6710 SIdB : constant Entity_Id :=
6711 Make_Defining_Identifier (Loc,
6712 Chars => New_External_Name (Chars (Typ), "PredicateM"));
6713 -- The entity for the function body
6721 -- Mark any raise expressions for special expansion
6723 Process_REs (Expr_M);
6725 -- Build function declaration
6727 Set_Ekind (SId, E_Function);
6728 Set_Is_Predicate_Function_M (SId);
6729 Set_Predicate_Function_M (Typ, SId);
6731 -- The predicate function is shared between views of a type
6733 if Is_Private_Type (Typ) and then Present (Full_View (Typ)) then
6734 Set_Predicate_Function_M (Full_View (Typ), SId);
6738 Make_Function_Specification (Loc,
6739 Defining_Unit_Name => SId,
6740 Parameter_Specifications => New_List (
6741 Make_Parameter_Specification (Loc,
6742 Defining_Identifier => Object_Entity_M,
6743 Parameter_Type => New_Occurrence_Of (Typ, Loc))),
6744 Result_Definition =>
6745 New_Occurrence_Of (Standard_Boolean, Loc));
6748 Make_Subprogram_Declaration (Loc,
6749 Specification => Spec);
6751 -- Build function body
6754 Make_Function_Specification (Loc,
6755 Defining_Unit_Name => SIdB,
6756 Parameter_Specifications => New_List (
6757 Make_Parameter_Specification (Loc,
6758 Defining_Identifier =>
6759 Make_Defining_Identifier (Loc, Object_Name),
6761 New_Occurrence_Of (Typ, Loc))),
6762 Result_Definition =>
6763 New_Occurrence_Of (Standard_Boolean, Loc));
6765 -- Build the body, we declare the boolean expression before
6766 -- doing the return, because we are not really confident of
6767 -- what happens if a return appears within a return!
6770 Make_Defining_Identifier (Loc,
6771 Chars => New_Internal_Name ('B'));
6774 Make_Subprogram_Body (Loc,
6775 Specification => Spec,
6777 Declarations => New_List (
6778 Make_Object_Declaration (Loc,
6779 Defining_Identifier => BTemp,
6780 Constant_Present => True,
6781 Object_Definition =>
6782 New_Reference_To (Standard_Boolean, Loc),
6783 Expression => Expr_M)),
6785 Handled_Statement_Sequence =>
6786 Make_Handled_Sequence_Of_Statements (Loc,
6787 Statements => New_List (
6788 Make_Simple_Return_Statement (Loc,
6789 Expression => New_Reference_To (BTemp, Loc)))));
6791 -- Insert declaration before freeze node and body after
6793 Insert_Before_And_Analyze (N, FDecl);
6794 Insert_After_And_Analyze (N, FBody);
6798 if Is_Scalar_Type (Typ) then
6800 -- Attempt to build a static predicate for a discrete or a real
6801 -- subtype. This action may fail because the actual expression may
6802 -- not be static. Note that the presence of an inherited or
6803 -- explicitly declared dynamic predicate is orthogonal to this
6804 -- check because we are only interested in the static predicate.
6806 if Ekind_In (Typ, E_Decimal_Fixed_Point_Subtype,
6807 E_Enumeration_Subtype,
6808 E_Floating_Point_Subtype,
6809 E_Modular_Integer_Subtype,
6810 E_Ordinary_Fixed_Point_Subtype,
6811 E_Signed_Integer_Subtype)
6813 Build_Static_Predicate (Typ, Expr, Object_Name);
6815 -- Emit an error when the predicate is categorized as static
6816 -- but its expression is dynamic.
6818 if Present (Static_Predic)
6819 and then No (Static_Predicate (Typ))
6822 ("expression does not have required form for "
6823 & "static predicate",
6824 Next (First (Pragma_Argument_Associations
6829 -- If a static predicate applies on other types, that's an error:
6830 -- either the type is scalar but non-static, or it's not even a
6831 -- scalar type. We do not issue an error on generated types, as
6832 -- these may be duplicates of the same error on a source type.
6834 elsif Present (Static_Predic) and then Comes_From_Source (Typ) then
6835 if Is_Scalar_Type (Typ) then
6837 ("static predicate not allowed for non-static type&",
6841 ("static predicate not allowed for non-scalar type&",
6846 end Build_Predicate_Functions;
6848 ----------------------------
6849 -- Build_Static_Predicate --
6850 ----------------------------
6852 procedure Build_Static_Predicate
6857 Loc : constant Source_Ptr := Sloc (Expr);
6859 Non_Static : exception;
6860 -- Raised if something non-static is found
6862 Btyp : constant Entity_Id := Base_Type (Typ);
6864 BLo : constant Uint := Expr_Value (Type_Low_Bound (Btyp));
6865 BHi : constant Uint := Expr_Value (Type_High_Bound (Btyp));
6866 -- Low bound and high bound value of base type of Typ
6868 TLo : constant Uint := Expr_Value (Type_Low_Bound (Typ));
6869 THi : constant Uint := Expr_Value (Type_High_Bound (Typ));
6870 -- Low bound and high bound values of static subtype Typ
6875 -- One entry in a Rlist value, a single REnt (range entry) value denotes
6876 -- one range from Lo to Hi. To represent a single value range Lo = Hi =
6879 type RList is array (Nat range <>) of REnt;
6880 -- A list of ranges. The ranges are sorted in increasing order, and are
6881 -- disjoint (there is a gap of at least one value between each range in
6882 -- the table). A value is in the set of ranges in Rlist if it lies
6883 -- within one of these ranges.
6885 False_Range : constant RList :=
6886 RList'(1 .. 0 => REnt'(No_Uint, No_Uint));
6887 -- An empty set of ranges represents a range list that can never be
6888 -- satisfied, since there are no ranges in which the value could lie,
6889 -- so it does not lie in any of them. False_Range is a canonical value
6890 -- for this empty set, but general processing should test for an Rlist
6891 -- with length zero (see Is_False predicate), since other null ranges
6892 -- may appear which must be treated as False.
6894 True_Range : constant RList := RList'(1 => REnt'(BLo, BHi));
6895 -- Range representing True, value must be in the base range
6897 function "and" (Left : RList; Right : RList) return RList;
6898 -- And's together two range lists, returning a range list. This is a set
6899 -- intersection operation.
6901 function "or" (Left : RList; Right : RList) return RList;
6902 -- Or's together two range lists, returning a range list. This is a set
6905 function "not" (Right : RList) return RList;
6906 -- Returns complement of a given range list, i.e. a range list
6907 -- representing all the values in TLo .. THi that are not in the input
6910 function Build_Val (V : Uint) return Node_Id;
6911 -- Return an analyzed N_Identifier node referencing this value, suitable
6912 -- for use as an entry in the Static_Predicate list. This node is typed
6913 -- with the base type.
6915 function Build_Range (Lo : Uint; Hi : Uint) return Node_Id;
6916 -- Return an analyzed N_Range node referencing this range, suitable for
6917 -- use as an entry in the Static_Predicate list. This node is typed with
6920 function Get_RList (Exp : Node_Id) return RList;
6921 -- This is a recursive routine that converts the given expression into a
6922 -- list of ranges, suitable for use in building the static predicate.
6924 function Is_False (R : RList) return Boolean;
6925 pragma Inline (Is_False);
6926 -- Returns True if the given range list is empty, and thus represents a
6927 -- False list of ranges that can never be satisfied.
6929 function Is_True (R : RList) return Boolean;
6930 -- Returns True if R trivially represents the True predicate by having a
6931 -- single range from BLo to BHi.
6933 function Is_Type_Ref (N : Node_Id) return Boolean;
6934 pragma Inline (Is_Type_Ref);
6935 -- Returns if True if N is a reference to the type for the predicate in
6936 -- the expression (i.e. if it is an identifier whose Chars field matches
6937 -- the Nam given in the call).
6939 function Lo_Val (N : Node_Id) return Uint;
6940 -- Given static expression or static range from a Static_Predicate list,
6941 -- gets expression value or low bound of range.
6943 function Hi_Val (N : Node_Id) return Uint;
6944 -- Given static expression or static range from a Static_Predicate list,
6945 -- gets expression value of high bound of range.
6947 function Membership_Entry (N : Node_Id) return RList;
6948 -- Given a single membership entry (range, value, or subtype), returns
6949 -- the corresponding range list. Raises Static_Error if not static.
6951 function Membership_Entries (N : Node_Id) return RList;
6952 -- Given an element on an alternatives list of a membership operation,
6953 -- returns the range list corresponding to this entry and all following
6954 -- entries (i.e. returns the "or" of this list of values).
6956 function Stat_Pred (Typ : Entity_Id) return RList;
6957 -- Given a type, if it has a static predicate, then return the predicate
6958 -- as a range list, otherwise raise Non_Static.
6964 function "and" (Left : RList; Right : RList) return RList is
6966 -- First range of result
6968 SLeft : Nat := Left'First;
6969 -- Start of rest of left entries
6971 SRight : Nat := Right'First;
6972 -- Start of rest of right entries
6975 -- If either range is True, return the other
6977 if Is_True (Left) then
6979 elsif Is_True (Right) then
6983 -- If either range is False, return False
6985 if Is_False (Left) or else Is_False (Right) then
6989 -- Loop to remove entries at start that are disjoint, and thus just
6990 -- get discarded from the result entirely.
6993 -- If no operands left in either operand, result is false
6995 if SLeft > Left'Last or else SRight > Right'Last then
6998 -- Discard first left operand entry if disjoint with right
7000 elsif Left (SLeft).Hi < Right (SRight).Lo then
7003 -- Discard first right operand entry if disjoint with left
7005 elsif Right (SRight).Hi < Left (SLeft).Lo then
7006 SRight := SRight + 1;
7008 -- Otherwise we have an overlapping entry
7015 -- Now we have two non-null operands, and first entries overlap. The
7016 -- first entry in the result will be the overlapping part of these
7019 FEnt := REnt'(Lo => UI_Max (Left (SLeft).Lo, Right (SRight).Lo),
7020 Hi => UI_Min (Left (SLeft).Hi, Right (SRight).Hi));
7022 -- Now we can remove the entry that ended at a lower value, since its
7023 -- contribution is entirely contained in Fent.
7025 if Left (SLeft).Hi <= Right (SRight).Hi then
7028 SRight := SRight + 1;
7031 -- Compute result by concatenating this first entry with the "and" of
7032 -- the remaining parts of the left and right operands. Note that if
7033 -- either of these is empty, "and" will yield empty, so that we will
7034 -- end up with just Fent, which is what we want in that case.
7037 FEnt & (Left (SLeft .. Left'Last) and Right (SRight .. Right'Last));
7044 function "not" (Right : RList) return RList is
7046 -- Return True if False range
7048 if Is_False (Right) then
7052 -- Return False if True range
7054 if Is_True (Right) then
7058 -- Here if not trivial case
7061 Result : RList (1 .. Right'Length + 1);
7062 -- May need one more entry for gap at beginning and end
7065 -- Number of entries stored in Result
7070 if Right (Right'First).Lo > TLo then
7072 Result (Count) := REnt'(TLo, Right (Right'First).Lo - 1);
7075 -- Gaps between ranges
7077 for J in Right'First .. Right'Last - 1 loop
7080 REnt'(Right (J).Hi + 1, Right (J + 1).Lo - 1);
7085 if Right (Right'Last).Hi < THi then
7087 Result (Count) := REnt'(Right (Right'Last).Hi + 1, THi);
7090 return Result (1 .. Count);
7098 function "or" (Left : RList; Right : RList) return RList is
7100 -- First range of result
7102 SLeft : Nat := Left'First;
7103 -- Start of rest of left entries
7105 SRight : Nat := Right'First;
7106 -- Start of rest of right entries
7109 -- If either range is True, return True
7111 if Is_True (Left) or else Is_True (Right) then
7115 -- If either range is False (empty), return the other
7117 if Is_False (Left) then
7119 elsif Is_False (Right) then
7123 -- Initialize result first entry from left or right operand depending
7124 -- on which starts with the lower range.
7126 if Left (SLeft).Lo < Right (SRight).Lo then
7127 FEnt := Left (SLeft);
7130 FEnt := Right (SRight);
7131 SRight := SRight + 1;
7134 -- This loop eats ranges from left and right operands that are
7135 -- contiguous with the first range we are gathering.
7138 -- Eat first entry in left operand if contiguous or overlapped by
7139 -- gathered first operand of result.
7141 if SLeft <= Left'Last
7142 and then Left (SLeft).Lo <= FEnt.Hi + 1
7144 FEnt.Hi := UI_Max (FEnt.Hi, Left (SLeft).Hi);
7147 -- Eat first entry in right operand if contiguous or overlapped by
7148 -- gathered right operand of result.
7150 elsif SRight <= Right'Last
7151 and then Right (SRight).Lo <= FEnt.Hi + 1
7153 FEnt.Hi := UI_Max (FEnt.Hi, Right (SRight).Hi);
7154 SRight := SRight + 1;
7156 -- All done if no more entries to eat
7163 -- Obtain result as the first entry we just computed, concatenated
7164 -- to the "or" of the remaining results (if one operand is empty,
7165 -- this will just concatenate with the other
7168 FEnt & (Left (SLeft .. Left'Last) or Right (SRight .. Right'Last));
7175 function Build_Range (Lo : Uint; Hi : Uint) return Node_Id is
7181 Low_Bound => Build_Val (Lo),
7182 High_Bound => Build_Val (Hi));
7183 Set_Etype (Result, Btyp);
7184 Set_Analyzed (Result);
7193 function Build_Val (V : Uint) return Node_Id is
7197 if Is_Enumeration_Type (Typ) then
7198 Result := Get_Enum_Lit_From_Pos (Typ, V, Loc);
7200 Result := Make_Integer_Literal (Loc, V);
7203 Set_Etype (Result, Btyp);
7204 Set_Is_Static_Expression (Result);
7205 Set_Analyzed (Result);
7213 function Get_RList (Exp : Node_Id) return RList is
7218 -- Static expression can only be true or false
7220 if Is_OK_Static_Expression (Exp) then
7224 if Expr_Value (Exp) = 0 then
7231 -- Otherwise test node type
7239 when N_Op_And | N_And_Then =>
7240 return Get_RList (Left_Opnd (Exp))
7242 Get_RList (Right_Opnd (Exp));
7246 when N_Op_Or | N_Or_Else =>
7247 return Get_RList (Left_Opnd (Exp))
7249 Get_RList (Right_Opnd (Exp));
7254 return not Get_RList (Right_Opnd (Exp));
7256 -- Comparisons of type with static value
7258 when N_Op_Compare =>
7260 -- Type is left operand
7262 if Is_Type_Ref (Left_Opnd (Exp))
7263 and then Is_OK_Static_Expression (Right_Opnd (Exp))
7265 Val := Expr_Value (Right_Opnd (Exp));
7267 -- Typ is right operand
7269 elsif Is_Type_Ref (Right_Opnd (Exp))
7270 and then Is_OK_Static_Expression (Left_Opnd (Exp))
7272 Val := Expr_Value (Left_Opnd (Exp));
7274 -- Invert sense of comparison
7277 when N_Op_Gt => Op := N_Op_Lt;
7278 when N_Op_Lt => Op := N_Op_Gt;
7279 when N_Op_Ge => Op := N_Op_Le;
7280 when N_Op_Le => Op := N_Op_Ge;
7281 when others => null;
7284 -- Other cases are non-static
7290 -- Construct range according to comparison operation
7294 return RList'(1 => REnt'(Val, Val));
7297 return RList'(1 => REnt'(Val, BHi));
7300 return RList'(1 => REnt'(Val + 1, BHi));
7303 return RList'(1 => REnt'(BLo, Val));
7306 return RList'(1 => REnt'(BLo, Val - 1));
7309 return RList'(REnt'(BLo, Val - 1),
7310 REnt'(Val + 1, BHi));
7313 raise Program_Error;
7319 if not Is_Type_Ref (Left_Opnd (Exp)) then
7323 if Present (Right_Opnd (Exp)) then
7324 return Membership_Entry (Right_Opnd (Exp));
7326 return Membership_Entries (First (Alternatives (Exp)));
7329 -- Negative membership (NOT IN)
7332 if not Is_Type_Ref (Left_Opnd (Exp)) then
7336 if Present (Right_Opnd (Exp)) then
7337 return not Membership_Entry (Right_Opnd (Exp));
7339 return not Membership_Entries (First (Alternatives (Exp)));
7342 -- Function call, may be call to static predicate
7344 when N_Function_Call =>
7345 if Is_Entity_Name (Name (Exp)) then
7347 Ent : constant Entity_Id := Entity (Name (Exp));
7349 if Is_Predicate_Function (Ent)
7351 Is_Predicate_Function_M (Ent)
7353 return Stat_Pred (Etype (First_Formal (Ent)));
7358 -- Other function call cases are non-static
7362 -- Qualified expression, dig out the expression
7364 when N_Qualified_Expression =>
7365 return Get_RList (Expression (Exp));
7367 -- Expression with actions: if no actions, dig out expression
7369 when N_Expression_With_Actions =>
7370 if Is_Empty_List (Actions (Exp)) then
7371 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_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 and then Chars (N) = Nam;
7439 function Lo_Val (N : Node_Id) return Uint is
7441 if Is_Static_Expression (N) then
7442 return Expr_Value (N);
7444 pragma Assert (Nkind (N) = N_Range);
7445 return Expr_Value (Low_Bound (N));
7449 ------------------------
7450 -- Membership_Entries --
7451 ------------------------
7453 function Membership_Entries (N : Node_Id) return RList is
7455 if No (Next (N)) then
7456 return Membership_Entry (N);
7458 return Membership_Entry (N) or Membership_Entries (Next (N));
7460 end Membership_Entries;
7462 ----------------------
7463 -- Membership_Entry --
7464 ----------------------
7466 function Membership_Entry (N : Node_Id) return RList is
7474 if Nkind (N) = N_Range then
7475 if not Is_Static_Expression (Low_Bound (N))
7477 not Is_Static_Expression (High_Bound (N))
7481 SLo := Expr_Value (Low_Bound (N));
7482 SHi := Expr_Value (High_Bound (N));
7483 return RList'(1 => REnt'(SLo, SHi));
7486 -- Static expression case
7488 elsif Is_Static_Expression (N) then
7489 Val := Expr_Value (N);
7490 return RList'(1 => REnt'(Val, Val));
7492 -- Identifier (other than static expression) case
7494 else pragma Assert (Nkind (N) = N_Identifier);
7498 if Is_Type (Entity (N)) then
7500 -- If type has predicates, process them
7502 if Has_Predicates (Entity (N)) then
7503 return Stat_Pred (Entity (N));
7505 -- For static subtype without predicates, get range
7507 elsif Is_Static_Subtype (Entity (N)) then
7508 SLo := Expr_Value (Type_Low_Bound (Entity (N)));
7509 SHi := Expr_Value (Type_High_Bound (Entity (N)));
7510 return RList'(1 => REnt'(SLo, SHi));
7512 -- Any other type makes us non-static
7518 -- Any other kind of identifier in predicate (e.g. a non-static
7519 -- expression value) means this is not a static predicate.
7525 end Membership_Entry;
7531 function Stat_Pred (Typ : Entity_Id) return RList is
7533 -- Not static if type does not have static predicates
7535 if not Has_Predicates (Typ) or else No (Static_Predicate (Typ)) then
7539 -- Otherwise we convert the predicate list to a range list
7542 Result : RList (1 .. List_Length (Static_Predicate (Typ)));
7546 P := First (Static_Predicate (Typ));
7547 for J in Result'Range loop
7548 Result (J) := REnt'(Lo_Val (P), Hi_Val (P));
7556 -- Start of processing for Build_Static_Predicate
7559 -- Now analyze the expression to see if it is a static predicate
7562 Ranges : constant RList := Get_RList (Expr);
7563 -- Range list from expression if it is static
7568 -- Convert range list into a form for the static predicate. In the
7569 -- Ranges array, we just have raw ranges, these must be converted
7570 -- to properly typed and analyzed static expressions or range nodes.
7572 -- Note: here we limit ranges to the ranges of the subtype, so that
7573 -- a predicate is always false for values outside the subtype. That
7574 -- seems fine, such values are invalid anyway, and considering them
7575 -- to fail the predicate seems allowed and friendly, and furthermore
7576 -- simplifies processing for case statements and loops.
7580 for J in Ranges'Range loop
7582 Lo : Uint := Ranges (J).Lo;
7583 Hi : Uint := Ranges (J).Hi;
7586 -- Ignore completely out of range entry
7588 if Hi < TLo or else Lo > THi then
7591 -- Otherwise process entry
7594 -- Adjust out of range value to subtype range
7604 -- Convert range into required form
7606 Append_To (Plist, Build_Range (Lo, Hi));
7611 -- Processing was successful and all entries were static, so now we
7612 -- can store the result as the predicate list.
7614 Set_Static_Predicate (Typ, Plist);
7616 -- The processing for static predicates put the expression into
7617 -- canonical form as a series of ranges. It also eliminated
7618 -- duplicates and collapsed and combined ranges. We might as well
7619 -- replace the alternatives list of the right operand of the
7620 -- membership test with the static predicate list, which will
7621 -- usually be more efficient.
7624 New_Alts : constant List_Id := New_List;
7629 Old_Node := First (Plist);
7630 while Present (Old_Node) loop
7631 New_Node := New_Copy (Old_Node);
7633 if Nkind (New_Node) = N_Range then
7634 Set_Low_Bound (New_Node, New_Copy (Low_Bound (Old_Node)));
7635 Set_High_Bound (New_Node, New_Copy (High_Bound (Old_Node)));
7638 Append_To (New_Alts, New_Node);
7642 -- If empty list, replace by False
7644 if Is_Empty_List (New_Alts) then
7645 Rewrite (Expr, New_Occurrence_Of (Standard_False, Loc));
7647 -- Else replace by set membership test
7652 Left_Opnd => Make_Identifier (Loc, Nam),
7653 Right_Opnd => Empty,
7654 Alternatives => New_Alts));
7656 -- Resolve new expression in function context
7658 Install_Formals (Predicate_Function (Typ));
7659 Push_Scope (Predicate_Function (Typ));
7660 Analyze_And_Resolve (Expr, Standard_Boolean);
7666 -- If non-static, return doing nothing
7671 end Build_Static_Predicate;
7673 -----------------------------------------
7674 -- Check_Aspect_At_End_Of_Declarations --
7675 -----------------------------------------
7677 procedure Check_Aspect_At_End_Of_Declarations (ASN : Node_Id) is
7678 Ent : constant Entity_Id := Entity (ASN);
7679 Ident : constant Node_Id := Identifier (ASN);
7680 A_Id : constant Aspect_Id := Get_Aspect_Id (Chars (Ident));
7682 End_Decl_Expr : constant Node_Id := Entity (Ident);
7683 -- Expression to be analyzed at end of declarations
7685 Freeze_Expr : constant Node_Id := Expression (ASN);
7686 -- Expression from call to Check_Aspect_At_Freeze_Point
7688 T : constant Entity_Id := Etype (Freeze_Expr);
7689 -- Type required for preanalyze call
7692 -- Set False if error
7694 -- On entry to this procedure, Entity (Ident) contains a copy of the
7695 -- original expression from the aspect, saved for this purpose, and
7696 -- but Expression (Ident) is a preanalyzed copy of the expression,
7697 -- preanalyzed just after the freeze point.
7699 procedure Check_Overloaded_Name;
7700 -- For aspects whose expression is simply a name, this routine checks if
7701 -- the name is overloaded or not. If so, it verifies there is an
7702 -- interpretation that matches the entity obtained at the freeze point,
7703 -- otherwise the compiler complains.
7705 ---------------------------
7706 -- Check_Overloaded_Name --
7707 ---------------------------
7709 procedure Check_Overloaded_Name is
7711 if not Is_Overloaded (End_Decl_Expr) then
7712 Err := Entity (End_Decl_Expr) /= Entity (Freeze_Expr);
7718 Index : Interp_Index;
7722 Get_First_Interp (End_Decl_Expr, Index, It);
7723 while Present (It.Typ) loop
7724 if It.Nam = Entity (Freeze_Expr) then
7729 Get_Next_Interp (Index, It);
7733 end Check_Overloaded_Name;
7735 -- Start of processing for Check_Aspect_At_End_Of_Declarations
7738 -- Case of aspects Dimension, Dimension_System and Synchronization
7740 if A_Id = Aspect_Synchronization then
7743 -- Case of stream attributes, just have to compare entities. However,
7744 -- the expression is just a name (possibly overloaded), and there may
7745 -- be stream operations declared for unrelated types, so we just need
7746 -- to verify that one of these interpretations is the one available at
7747 -- at the freeze point.
7749 elsif A_Id = Aspect_Input or else
7750 A_Id = Aspect_Output or else
7751 A_Id = Aspect_Read or else
7754 Analyze (End_Decl_Expr);
7755 Check_Overloaded_Name;
7757 elsif A_Id = Aspect_Variable_Indexing or else
7758 A_Id = Aspect_Constant_Indexing or else
7759 A_Id = Aspect_Default_Iterator or else
7760 A_Id = Aspect_Iterator_Element
7762 -- Make type unfrozen before analysis, to prevent spurious errors
7763 -- about late attributes.
7765 Set_Is_Frozen (Ent, False);
7766 Analyze (End_Decl_Expr);
7767 Set_Is_Frozen (Ent, True);
7769 -- If the end of declarations comes before any other freeze
7770 -- point, the Freeze_Expr is not analyzed: no check needed.
7772 if Analyzed (Freeze_Expr) and then not In_Instance then
7773 Check_Overloaded_Name;
7781 -- In a generic context the aspect expressions have not been
7782 -- preanalyzed, so do it now. There are no conformance checks
7783 -- to perform in this case.
7786 Check_Aspect_At_Freeze_Point (ASN);
7789 -- The default values attributes may be defined in the private part,
7790 -- and the analysis of the expression may take place when only the
7791 -- partial view is visible. The expression must be scalar, so use
7792 -- the full view to resolve.
7794 elsif (A_Id = Aspect_Default_Value
7796 A_Id = Aspect_Default_Component_Value)
7797 and then Is_Private_Type (T)
7799 Preanalyze_Spec_Expression (End_Decl_Expr, Full_View (T));
7801 Preanalyze_Spec_Expression (End_Decl_Expr, T);
7804 Err := not Fully_Conformant_Expressions (End_Decl_Expr, Freeze_Expr);
7807 -- Output error message if error
7811 ("visibility of aspect for& changes after freeze point",
7814 ("info: & is frozen here, aspects evaluated at this point??",
7815 Freeze_Node (Ent), Ent);
7817 end Check_Aspect_At_End_Of_Declarations;
7819 ----------------------------------
7820 -- Check_Aspect_At_Freeze_Point --
7821 ----------------------------------
7823 procedure Check_Aspect_At_Freeze_Point (ASN : Node_Id) is
7824 Ident : constant Node_Id := Identifier (ASN);
7825 -- Identifier (use Entity field to save expression)
7827 A_Id : constant Aspect_Id := Get_Aspect_Id (Chars (Ident));
7829 T : Entity_Id := Empty;
7830 -- Type required for preanalyze call
7833 -- On entry to this procedure, Entity (Ident) contains a copy of the
7834 -- original expression from the aspect, saved for this purpose.
7836 -- On exit from this procedure Entity (Ident) is unchanged, still
7837 -- containing that copy, but Expression (Ident) is a preanalyzed copy
7838 -- of the expression, preanalyzed just after the freeze point.
7840 -- Make a copy of the expression to be preanalyzed
7842 Set_Expression (ASN, New_Copy_Tree (Entity (Ident)));
7844 -- Find type for preanalyze call
7848 -- No_Aspect should be impossible
7851 raise Program_Error;
7853 -- Aspects taking an optional boolean argument
7855 when Boolean_Aspects |
7856 Library_Unit_Aspects =>
7858 T := Standard_Boolean;
7860 -- Aspects corresponding to attribute definition clauses
7862 when Aspect_Address =>
7863 T := RTE (RE_Address);
7865 when Aspect_Attach_Handler =>
7866 T := RTE (RE_Interrupt_ID);
7868 when Aspect_Bit_Order | Aspect_Scalar_Storage_Order =>
7869 T := RTE (RE_Bit_Order);
7871 when Aspect_Convention =>
7875 T := RTE (RE_CPU_Range);
7877 -- Default_Component_Value is resolved with the component type
7879 when Aspect_Default_Component_Value =>
7880 T := Component_Type (Entity (ASN));
7882 -- Default_Value is resolved with the type entity in question
7884 when Aspect_Default_Value =>
7887 -- Depends is a delayed aspect because it mentiones names first
7888 -- introduced by aspect Global which is already delayed. There is
7889 -- no action to be taken with respect to the aspect itself as the
7890 -- analysis is done by the corresponding pragma.
7892 when Aspect_Depends =>
7895 when Aspect_Dispatching_Domain =>
7896 T := RTE (RE_Dispatching_Domain);
7898 when Aspect_External_Tag =>
7899 T := Standard_String;
7901 when Aspect_External_Name =>
7902 T := Standard_String;
7904 -- Global is a delayed aspect because it may reference names that
7905 -- have not been declared yet. There is no action to be taken with
7906 -- respect to the aspect itself as the reference checking is done
7907 -- on the corresponding pragma.
7909 when Aspect_Global =>
7912 when Aspect_Link_Name =>
7913 T := Standard_String;
7915 when Aspect_Priority | Aspect_Interrupt_Priority =>
7916 T := Standard_Integer;
7918 when Aspect_Relative_Deadline =>
7919 T := RTE (RE_Time_Span);
7921 when Aspect_Small =>
7922 T := Universal_Real;
7924 -- For a simple storage pool, we have to retrieve the type of the
7925 -- pool object associated with the aspect's corresponding attribute
7926 -- definition clause.
7928 when Aspect_Simple_Storage_Pool =>
7929 T := Etype (Expression (Aspect_Rep_Item (ASN)));
7931 when Aspect_Storage_Pool =>
7932 T := Class_Wide_Type (RTE (RE_Root_Storage_Pool));
7934 when Aspect_Alignment |
7935 Aspect_Component_Size |
7936 Aspect_Machine_Radix |
7937 Aspect_Object_Size |
7939 Aspect_Storage_Size |
7940 Aspect_Stream_Size |
7941 Aspect_Value_Size =>
7944 when Aspect_Synchronization =>
7947 -- Special case, the expression of these aspects is just an entity
7948 -- that does not need any resolution, so just analyze.
7957 Analyze (Expression (ASN));
7960 -- Same for Iterator aspects, where the expression is a function
7961 -- name. Legality rules are checked separately.
7963 when Aspect_Constant_Indexing |
7964 Aspect_Default_Iterator |
7965 Aspect_Iterator_Element |
7966 Aspect_Variable_Indexing =>
7967 Analyze (Expression (ASN));
7970 -- Invariant/Predicate take boolean expressions
7972 when Aspect_Dynamic_Predicate |
7975 Aspect_Static_Predicate |
7976 Aspect_Type_Invariant =>
7977 T := Standard_Boolean;
7979 -- Here is the list of aspects that don't require delay analysis
7981 when Aspect_Abstract_State |
7982 Aspect_Contract_Cases |
7984 Aspect_Dimension_System |
7985 Aspect_Implicit_Dereference |
7986 Aspect_Initial_Condition |
7987 Aspect_Initializes |
7989 Aspect_Postcondition |
7991 Aspect_Precondition |
7992 Aspect_Refined_Depends |
7993 Aspect_Refined_Global |
7994 Aspect_Refined_Post |
7995 Aspect_Refined_State |
7998 raise Program_Error;
8002 -- Do the preanalyze call
8004 Preanalyze_Spec_Expression (Expression (ASN), T);
8005 end Check_Aspect_At_Freeze_Point;
8007 -----------------------------------
8008 -- Check_Constant_Address_Clause --
8009 -----------------------------------
8011 procedure Check_Constant_Address_Clause
8015 procedure Check_At_Constant_Address (Nod : Node_Id);
8016 -- Checks that the given node N represents a name whose 'Address is
8017 -- constant (in the same sense as OK_Constant_Address_Clause, i.e. the
8018 -- address value is the same at the point of declaration of U_Ent and at
8019 -- the time of elaboration of the address clause.
8021 procedure Check_Expr_Constants (Nod : Node_Id);
8022 -- Checks that Nod meets the requirements for a constant address clause
8023 -- in the sense of the enclosing procedure.
8025 procedure Check_List_Constants (Lst : List_Id);
8026 -- Check that all elements of list Lst meet the requirements for a
8027 -- constant address clause in the sense of the enclosing procedure.
8029 -------------------------------
8030 -- Check_At_Constant_Address --
8031 -------------------------------
8033 procedure Check_At_Constant_Address (Nod : Node_Id) is
8035 if Is_Entity_Name (Nod) then
8036 if Present (Address_Clause (Entity ((Nod)))) then
8038 ("invalid address clause for initialized object &!",
8041 ("address for& cannot" &
8042 " depend on another address clause! (RM 13.1(22))!",
8045 elsif In_Same_Source_Unit (Entity (Nod), U_Ent)
8046 and then Sloc (U_Ent) < Sloc (Entity (Nod))
8049 ("invalid address clause for initialized object &!",
8051 Error_Msg_Node_2 := U_Ent;
8053 ("\& must be defined before & (RM 13.1(22))!",
8057 elsif Nkind (Nod) = N_Selected_Component then
8059 T : constant Entity_Id := Etype (Prefix (Nod));
8062 if (Is_Record_Type (T)
8063 and then Has_Discriminants (T))
8066 and then Is_Record_Type (Designated_Type (T))
8067 and then Has_Discriminants (Designated_Type (T)))
8070 ("invalid address clause for initialized object &!",
8073 ("\address cannot depend on component" &
8074 " of discriminated record (RM 13.1(22))!",
8077 Check_At_Constant_Address (Prefix (Nod));
8081 elsif Nkind (Nod) = N_Indexed_Component then
8082 Check_At_Constant_Address (Prefix (Nod));
8083 Check_List_Constants (Expressions (Nod));
8086 Check_Expr_Constants (Nod);
8088 end Check_At_Constant_Address;
8090 --------------------------
8091 -- Check_Expr_Constants --
8092 --------------------------
8094 procedure Check_Expr_Constants (Nod : Node_Id) is
8095 Loc_U_Ent : constant Source_Ptr := Sloc (U_Ent);
8096 Ent : Entity_Id := Empty;
8099 if Nkind (Nod) in N_Has_Etype
8100 and then Etype (Nod) = Any_Type
8106 when N_Empty | N_Error =>
8109 when N_Identifier | N_Expanded_Name =>
8110 Ent := Entity (Nod);
8112 -- We need to look at the original node if it is different
8113 -- from the node, since we may have rewritten things and
8114 -- substituted an identifier representing the rewrite.
8116 if Original_Node (Nod) /= Nod then
8117 Check_Expr_Constants (Original_Node (Nod));
8119 -- If the node is an object declaration without initial
8120 -- value, some code has been expanded, and the expression
8121 -- is not constant, even if the constituents might be
8122 -- acceptable, as in A'Address + offset.
8124 if Ekind (Ent) = E_Variable
8126 Nkind (Declaration_Node (Ent)) = N_Object_Declaration
8128 No (Expression (Declaration_Node (Ent)))
8131 ("invalid address clause for initialized object &!",
8134 -- If entity is constant, it may be the result of expanding
8135 -- a check. We must verify that its declaration appears
8136 -- before the object in question, else we also reject the
8139 elsif Ekind (Ent) = E_Constant
8140 and then In_Same_Source_Unit (Ent, U_Ent)
8141 and then Sloc (Ent) > Loc_U_Ent
8144 ("invalid address clause for initialized object &!",
8151 -- Otherwise look at the identifier and see if it is OK
8153 if Ekind_In (Ent, E_Named_Integer, E_Named_Real)
8154 or else Is_Type (Ent)
8159 Ekind (Ent) = E_Constant
8161 Ekind (Ent) = E_In_Parameter
8163 -- This is the case where we must have Ent defined before
8164 -- U_Ent. Clearly if they are in different units this
8165 -- requirement is met since the unit containing Ent is
8166 -- already processed.
8168 if not In_Same_Source_Unit (Ent, U_Ent) then
8171 -- Otherwise location of Ent must be before the location
8172 -- of U_Ent, that's what prior defined means.
8174 elsif Sloc (Ent) < Loc_U_Ent then
8179 ("invalid address clause for initialized object &!",
8181 Error_Msg_Node_2 := U_Ent;
8183 ("\& must be defined before & (RM 13.1(22))!",
8187 elsif Nkind (Original_Node (Nod)) = N_Function_Call then
8188 Check_Expr_Constants (Original_Node (Nod));
8192 ("invalid address clause for initialized object &!",
8195 if Comes_From_Source (Ent) then
8197 ("\reference to variable& not allowed"
8198 & " (RM 13.1(22))!", Nod, Ent);
8201 ("non-static expression not allowed"
8202 & " (RM 13.1(22))!", Nod);
8206 when N_Integer_Literal =>
8208 -- If this is a rewritten unchecked conversion, in a system
8209 -- where Address is an integer type, always use the base type
8210 -- for a literal value. This is user-friendly and prevents
8211 -- order-of-elaboration issues with instances of unchecked
8214 if Nkind (Original_Node (Nod)) = N_Function_Call then
8215 Set_Etype (Nod, Base_Type (Etype (Nod)));
8218 when N_Real_Literal |
8220 N_Character_Literal =>
8224 Check_Expr_Constants (Low_Bound (Nod));
8225 Check_Expr_Constants (High_Bound (Nod));
8227 when N_Explicit_Dereference =>
8228 Check_Expr_Constants (Prefix (Nod));
8230 when N_Indexed_Component =>
8231 Check_Expr_Constants (Prefix (Nod));
8232 Check_List_Constants (Expressions (Nod));
8235 Check_Expr_Constants (Prefix (Nod));
8236 Check_Expr_Constants (Discrete_Range (Nod));
8238 when N_Selected_Component =>
8239 Check_Expr_Constants (Prefix (Nod));
8241 when N_Attribute_Reference =>
8242 if Nam_In (Attribute_Name (Nod), Name_Address,
8244 Name_Unchecked_Access,
8245 Name_Unrestricted_Access)
8247 Check_At_Constant_Address (Prefix (Nod));
8250 Check_Expr_Constants (Prefix (Nod));
8251 Check_List_Constants (Expressions (Nod));
8255 Check_List_Constants (Component_Associations (Nod));
8256 Check_List_Constants (Expressions (Nod));
8258 when N_Component_Association =>
8259 Check_Expr_Constants (Expression (Nod));
8261 when N_Extension_Aggregate =>
8262 Check_Expr_Constants (Ancestor_Part (Nod));
8263 Check_List_Constants (Component_Associations (Nod));
8264 Check_List_Constants (Expressions (Nod));
8269 when N_Binary_Op | N_Short_Circuit | N_Membership_Test =>
8270 Check_Expr_Constants (Left_Opnd (Nod));
8271 Check_Expr_Constants (Right_Opnd (Nod));
8274 Check_Expr_Constants (Right_Opnd (Nod));
8276 when N_Type_Conversion |
8277 N_Qualified_Expression |
8279 N_Unchecked_Type_Conversion =>
8280 Check_Expr_Constants (Expression (Nod));
8282 when N_Function_Call =>
8283 if not Is_Pure (Entity (Name (Nod))) then
8285 ("invalid address clause for initialized object &!",
8289 ("\function & is not pure (RM 13.1(22))!",
8290 Nod, Entity (Name (Nod)));
8293 Check_List_Constants (Parameter_Associations (Nod));
8296 when N_Parameter_Association =>
8297 Check_Expr_Constants (Explicit_Actual_Parameter (Nod));
8301 ("invalid address clause for initialized object &!",
8304 ("\must be constant defined before& (RM 13.1(22))!",
8307 end Check_Expr_Constants;
8309 --------------------------
8310 -- Check_List_Constants --
8311 --------------------------
8313 procedure Check_List_Constants (Lst : List_Id) is
8317 if Present (Lst) then
8318 Nod1 := First (Lst);
8319 while Present (Nod1) loop
8320 Check_Expr_Constants (Nod1);
8324 end Check_List_Constants;
8326 -- Start of processing for Check_Constant_Address_Clause
8329 -- If rep_clauses are to be ignored, no need for legality checks. In
8330 -- particular, no need to pester user about rep clauses that violate
8331 -- the rule on constant addresses, given that these clauses will be
8332 -- removed by Freeze before they reach the back end.
8334 if not Ignore_Rep_Clauses then
8335 Check_Expr_Constants (Expr);
8337 end Check_Constant_Address_Clause;
8339 ---------------------------
8340 -- Check_Pool_Size_Clash --
8341 ---------------------------
8343 procedure Check_Pool_Size_Clash (Ent : Entity_Id; SP, SS : Node_Id) is
8347 -- We need to find out which one came first. Note that in the case of
8348 -- aspects mixed with pragmas there are cases where the processing order
8349 -- is reversed, which is why we do the check here.
8351 if Sloc (SP) < Sloc (SS) then
8352 Error_Msg_Sloc := Sloc (SP);
8354 Error_Msg_NE ("Storage_Pool previously given for&#", Post, Ent);
8357 Error_Msg_Sloc := Sloc (SS);
8359 Error_Msg_NE ("Storage_Size previously given for&#", Post, Ent);
8363 ("\cannot have Storage_Size and Storage_Pool (RM 13.11(3))", Post);
8364 end Check_Pool_Size_Clash;
8366 ----------------------------------------
8367 -- Check_Record_Representation_Clause --
8368 ----------------------------------------
8370 procedure Check_Record_Representation_Clause (N : Node_Id) is
8371 Loc : constant Source_Ptr := Sloc (N);
8372 Ident : constant Node_Id := Identifier (N);
8373 Rectype : Entity_Id;
8378 Hbit : Uint := Uint_0;
8382 Max_Bit_So_Far : Uint;
8383 -- Records the maximum bit position so far. If all field positions
8384 -- are monotonically increasing, then we can skip the circuit for
8385 -- checking for overlap, since no overlap is possible.
8387 Tagged_Parent : Entity_Id := Empty;
8388 -- This is set in the case of a derived tagged type for which we have
8389 -- Is_Fully_Repped_Tagged_Type True (indicating that all components are
8390 -- positioned by record representation clauses). In this case we must
8391 -- check for overlap between components of this tagged type, and the
8392 -- components of its parent. Tagged_Parent will point to this parent
8393 -- type. For all other cases Tagged_Parent is left set to Empty.
8395 Parent_Last_Bit : Uint;
8396 -- Relevant only if Tagged_Parent is set, Parent_Last_Bit indicates the
8397 -- last bit position for any field in the parent type. We only need to
8398 -- check overlap for fields starting below this point.
8400 Overlap_Check_Required : Boolean;
8401 -- Used to keep track of whether or not an overlap check is required
8403 Overlap_Detected : Boolean := False;
8404 -- Set True if an overlap is detected
8406 Ccount : Natural := 0;
8407 -- Number of component clauses in record rep clause
8409 procedure Check_Component_Overlap (C1_Ent, C2_Ent : Entity_Id);
8410 -- Given two entities for record components or discriminants, checks
8411 -- if they have overlapping component clauses and issues errors if so.
8413 procedure Find_Component;
8414 -- Finds component entity corresponding to current component clause (in
8415 -- CC), and sets Comp to the entity, and Fbit/Lbit to the zero origin
8416 -- start/stop bits for the field. If there is no matching component or
8417 -- if the matching component does not have a component clause, then
8418 -- that's an error and Comp is set to Empty, but no error message is
8419 -- issued, since the message was already given. Comp is also set to
8420 -- Empty if the current "component clause" is in fact a pragma.
8422 -----------------------------
8423 -- Check_Component_Overlap --
8424 -----------------------------
8426 procedure Check_Component_Overlap (C1_Ent, C2_Ent : Entity_Id) is
8427 CC1 : constant Node_Id := Component_Clause (C1_Ent);
8428 CC2 : constant Node_Id := Component_Clause (C2_Ent);
8431 if Present (CC1) and then Present (CC2) then
8433 -- Exclude odd case where we have two tag components in the same
8434 -- record, both at location zero. This seems a bit strange, but
8435 -- it seems to happen in some circumstances, perhaps on an error.
8437 if Nam_In (Chars (C1_Ent), Name_uTag, Name_uTag) then
8441 -- Here we check if the two fields overlap
8444 S1 : constant Uint := Component_Bit_Offset (C1_Ent);
8445 S2 : constant Uint := Component_Bit_Offset (C2_Ent);
8446 E1 : constant Uint := S1 + Esize (C1_Ent);
8447 E2 : constant Uint := S2 + Esize (C2_Ent);
8450 if E2 <= S1 or else E1 <= S2 then
8453 Error_Msg_Node_2 := Component_Name (CC2);
8454 Error_Msg_Sloc := Sloc (Error_Msg_Node_2);
8455 Error_Msg_Node_1 := Component_Name (CC1);
8457 ("component& overlaps & #", Component_Name (CC1));
8458 Overlap_Detected := True;
8462 end Check_Component_Overlap;
8464 --------------------
8465 -- Find_Component --
8466 --------------------
8468 procedure Find_Component is
8470 procedure Search_Component (R : Entity_Id);
8471 -- Search components of R for a match. If found, Comp is set
8473 ----------------------
8474 -- Search_Component --
8475 ----------------------
8477 procedure Search_Component (R : Entity_Id) is
8479 Comp := First_Component_Or_Discriminant (R);
8480 while Present (Comp) loop
8482 -- Ignore error of attribute name for component name (we
8483 -- already gave an error message for this, so no need to
8486 if Nkind (Component_Name (CC)) = N_Attribute_Reference then
8489 exit when Chars (Comp) = Chars (Component_Name (CC));
8492 Next_Component_Or_Discriminant (Comp);
8494 end Search_Component;
8496 -- Start of processing for Find_Component
8499 -- Return with Comp set to Empty if we have a pragma
8501 if Nkind (CC) = N_Pragma then
8506 -- Search current record for matching component
8508 Search_Component (Rectype);
8510 -- If not found, maybe component of base type discriminant that is
8511 -- absent from statically constrained first subtype.
8514 Search_Component (Base_Type (Rectype));
8517 -- If no component, or the component does not reference the component
8518 -- clause in question, then there was some previous error for which
8519 -- we already gave a message, so just return with Comp Empty.
8521 if No (Comp) or else Component_Clause (Comp) /= CC then
8522 Check_Error_Detected;
8525 -- Normal case where we have a component clause
8528 Fbit := Component_Bit_Offset (Comp);
8529 Lbit := Fbit + Esize (Comp) - 1;
8533 -- Start of processing for Check_Record_Representation_Clause
8537 Rectype := Entity (Ident);
8539 if Rectype = Any_Type then
8542 Rectype := Underlying_Type (Rectype);
8545 -- See if we have a fully repped derived tagged type
8548 PS : constant Entity_Id := Parent_Subtype (Rectype);
8551 if Present (PS) and then Is_Fully_Repped_Tagged_Type (PS) then
8552 Tagged_Parent := PS;
8554 -- Find maximum bit of any component of the parent type
8556 Parent_Last_Bit := UI_From_Int (System_Address_Size - 1);
8557 Pcomp := First_Entity (Tagged_Parent);
8558 while Present (Pcomp) loop
8559 if Ekind_In (Pcomp, E_Discriminant, E_Component) then
8560 if Component_Bit_Offset (Pcomp) /= No_Uint
8561 and then Known_Static_Esize (Pcomp)
8566 Component_Bit_Offset (Pcomp) + Esize (Pcomp) - 1);
8569 Next_Entity (Pcomp);
8575 -- All done if no component clauses
8577 CC := First (Component_Clauses (N));
8583 -- If a tag is present, then create a component clause that places it
8584 -- at the start of the record (otherwise gigi may place it after other
8585 -- fields that have rep clauses).
8587 Fent := First_Entity (Rectype);
8589 if Nkind (Fent) = N_Defining_Identifier
8590 and then Chars (Fent) = Name_uTag
8592 Set_Component_Bit_Offset (Fent, Uint_0);
8593 Set_Normalized_Position (Fent, Uint_0);
8594 Set_Normalized_First_Bit (Fent, Uint_0);
8595 Set_Normalized_Position_Max (Fent, Uint_0);
8596 Init_Esize (Fent, System_Address_Size);
8598 Set_Component_Clause (Fent,
8599 Make_Component_Clause (Loc,
8600 Component_Name => Make_Identifier (Loc, Name_uTag),
8602 Position => Make_Integer_Literal (Loc, Uint_0),
8603 First_Bit => Make_Integer_Literal (Loc, Uint_0),
8605 Make_Integer_Literal (Loc,
8606 UI_From_Int (System_Address_Size))));
8608 Ccount := Ccount + 1;
8611 Max_Bit_So_Far := Uint_Minus_1;
8612 Overlap_Check_Required := False;
8614 -- Process the component clauses
8616 while Present (CC) loop
8619 if Present (Comp) then
8620 Ccount := Ccount + 1;
8622 -- We need a full overlap check if record positions non-monotonic
8624 if Fbit <= Max_Bit_So_Far then
8625 Overlap_Check_Required := True;
8628 Max_Bit_So_Far := Lbit;
8630 -- Check bit position out of range of specified size
8632 if Has_Size_Clause (Rectype)
8633 and then RM_Size (Rectype) <= Lbit
8636 ("bit number out of range of specified size",
8639 -- Check for overlap with tag component
8642 if Is_Tagged_Type (Rectype)
8643 and then Fbit < System_Address_Size
8646 ("component overlaps tag field of&",
8647 Component_Name (CC), Rectype);
8648 Overlap_Detected := True;
8656 -- Check parent overlap if component might overlap parent field
8658 if Present (Tagged_Parent) and then Fbit <= Parent_Last_Bit then
8659 Pcomp := First_Component_Or_Discriminant (Tagged_Parent);
8660 while Present (Pcomp) loop
8661 if not Is_Tag (Pcomp)
8662 and then Chars (Pcomp) /= Name_uParent
8664 Check_Component_Overlap (Comp, Pcomp);
8667 Next_Component_Or_Discriminant (Pcomp);
8675 -- Now that we have processed all the component clauses, check for
8676 -- overlap. We have to leave this till last, since the components can
8677 -- appear in any arbitrary order in the representation clause.
8679 -- We do not need this check if all specified ranges were monotonic,
8680 -- as recorded by Overlap_Check_Required being False at this stage.
8682 -- This first section checks if there are any overlapping entries at
8683 -- all. It does this by sorting all entries and then seeing if there are
8684 -- any overlaps. If there are none, then that is decisive, but if there
8685 -- are overlaps, they may still be OK (they may result from fields in
8686 -- different variants).
8688 if Overlap_Check_Required then
8689 Overlap_Check1 : declare
8691 OC_Fbit : array (0 .. Ccount) of Uint;
8692 -- First-bit values for component clauses, the value is the offset
8693 -- of the first bit of the field from start of record. The zero
8694 -- entry is for use in sorting.
8696 OC_Lbit : array (0 .. Ccount) of Uint;
8697 -- Last-bit values for component clauses, the value is the offset
8698 -- of the last bit of the field from start of record. The zero
8699 -- entry is for use in sorting.
8701 OC_Count : Natural := 0;
8702 -- Count of entries in OC_Fbit and OC_Lbit
8704 function OC_Lt (Op1, Op2 : Natural) return Boolean;
8705 -- Compare routine for Sort
8707 procedure OC_Move (From : Natural; To : Natural);
8708 -- Move routine for Sort
8710 package Sorting is new GNAT.Heap_Sort_G (OC_Move, OC_Lt);
8716 function OC_Lt (Op1, Op2 : Natural) return Boolean is
8718 return OC_Fbit (Op1) < OC_Fbit (Op2);
8725 procedure OC_Move (From : Natural; To : Natural) is
8727 OC_Fbit (To) := OC_Fbit (From);
8728 OC_Lbit (To) := OC_Lbit (From);
8731 -- Start of processing for Overlap_Check
8734 CC := First (Component_Clauses (N));
8735 while Present (CC) loop
8737 -- Exclude component clause already marked in error
8739 if not Error_Posted (CC) then
8742 if Present (Comp) then
8743 OC_Count := OC_Count + 1;
8744 OC_Fbit (OC_Count) := Fbit;
8745 OC_Lbit (OC_Count) := Lbit;
8752 Sorting.Sort (OC_Count);
8754 Overlap_Check_Required := False;
8755 for J in 1 .. OC_Count - 1 loop
8756 if OC_Lbit (J) >= OC_Fbit (J + 1) then
8757 Overlap_Check_Required := True;
8764 -- If Overlap_Check_Required is still True, then we have to do the full
8765 -- scale overlap check, since we have at least two fields that do
8766 -- overlap, and we need to know if that is OK since they are in
8767 -- different variant, or whether we have a definite problem.
8769 if Overlap_Check_Required then
8770 Overlap_Check2 : declare
8771 C1_Ent, C2_Ent : Entity_Id;
8772 -- Entities of components being checked for overlap
8775 -- Component_List node whose Component_Items are being checked
8778 -- Component declaration for component being checked
8781 C1_Ent := First_Entity (Base_Type (Rectype));
8783 -- Loop through all components in record. For each component check
8784 -- for overlap with any of the preceding elements on the component
8785 -- list containing the component and also, if the component is in
8786 -- a variant, check against components outside the case structure.
8787 -- This latter test is repeated recursively up the variant tree.
8789 Main_Component_Loop : while Present (C1_Ent) loop
8790 if not Ekind_In (C1_Ent, E_Component, E_Discriminant) then
8791 goto Continue_Main_Component_Loop;
8794 -- Skip overlap check if entity has no declaration node. This
8795 -- happens with discriminants in constrained derived types.
8796 -- Possibly we are missing some checks as a result, but that
8797 -- does not seem terribly serious.
8799 if No (Declaration_Node (C1_Ent)) then
8800 goto Continue_Main_Component_Loop;
8803 Clist := Parent (List_Containing (Declaration_Node (C1_Ent)));
8805 -- Loop through component lists that need checking. Check the
8806 -- current component list and all lists in variants above us.
8808 Component_List_Loop : loop
8810 -- If derived type definition, go to full declaration
8811 -- If at outer level, check discriminants if there are any.
8813 if Nkind (Clist) = N_Derived_Type_Definition then
8814 Clist := Parent (Clist);
8817 -- Outer level of record definition, check discriminants
8819 if Nkind_In (Clist, N_Full_Type_Declaration,
8820 N_Private_Type_Declaration)
8822 if Has_Discriminants (Defining_Identifier (Clist)) then
8824 First_Discriminant (Defining_Identifier (Clist));
8825 while Present (C2_Ent) loop
8826 exit when C1_Ent = C2_Ent;
8827 Check_Component_Overlap (C1_Ent, C2_Ent);
8828 Next_Discriminant (C2_Ent);
8832 -- Record extension case
8834 elsif Nkind (Clist) = N_Derived_Type_Definition then
8837 -- Otherwise check one component list
8840 Citem := First (Component_Items (Clist));
8841 while Present (Citem) loop
8842 if Nkind (Citem) = N_Component_Declaration then
8843 C2_Ent := Defining_Identifier (Citem);
8844 exit when C1_Ent = C2_Ent;
8845 Check_Component_Overlap (C1_Ent, C2_Ent);
8852 -- Check for variants above us (the parent of the Clist can
8853 -- be a variant, in which case its parent is a variant part,
8854 -- and the parent of the variant part is a component list
8855 -- whose components must all be checked against the current
8856 -- component for overlap).
8858 if Nkind (Parent (Clist)) = N_Variant then
8859 Clist := Parent (Parent (Parent (Clist)));
8861 -- Check for possible discriminant part in record, this
8862 -- is treated essentially as another level in the
8863 -- recursion. For this case the parent of the component
8864 -- list is the record definition, and its parent is the
8865 -- full type declaration containing the discriminant
8868 elsif Nkind (Parent (Clist)) = N_Record_Definition then
8869 Clist := Parent (Parent ((Clist)));
8871 -- If neither of these two cases, we are at the top of
8875 exit Component_List_Loop;
8877 end loop Component_List_Loop;
8879 <<Continue_Main_Component_Loop>>
8880 Next_Entity (C1_Ent);
8882 end loop Main_Component_Loop;
8886 -- The following circuit deals with warning on record holes (gaps). We
8887 -- skip this check if overlap was detected, since it makes sense for the
8888 -- programmer to fix this illegality before worrying about warnings.
8890 if not Overlap_Detected and Warn_On_Record_Holes then
8891 Record_Hole_Check : declare
8892 Decl : constant Node_Id := Declaration_Node (Base_Type (Rectype));
8893 -- Full declaration of record type
8895 procedure Check_Component_List
8899 -- Check component list CL for holes. The starting bit should be
8900 -- Sbit. which is zero for the main record component list and set
8901 -- appropriately for recursive calls for variants. DS is set to
8902 -- a list of discriminant specifications to be included in the
8903 -- consideration of components. It is No_List if none to consider.
8905 --------------------------
8906 -- Check_Component_List --
8907 --------------------------
8909 procedure Check_Component_List
8917 Compl := Integer (List_Length (Component_Items (CL)));
8919 if DS /= No_List then
8920 Compl := Compl + Integer (List_Length (DS));
8924 Comps : array (Natural range 0 .. Compl) of Entity_Id;
8925 -- Gather components (zero entry is for sort routine)
8927 Ncomps : Natural := 0;
8928 -- Number of entries stored in Comps (starting at Comps (1))
8931 -- One component item or discriminant specification
8934 -- Starting bit for next component
8942 function Lt (Op1, Op2 : Natural) return Boolean;
8943 -- Compare routine for Sort
8945 procedure Move (From : Natural; To : Natural);
8946 -- Move routine for Sort
8948 package Sorting is new GNAT.Heap_Sort_G (Move, Lt);
8954 function Lt (Op1, Op2 : Natural) return Boolean is
8956 return Component_Bit_Offset (Comps (Op1))
8958 Component_Bit_Offset (Comps (Op2));
8965 procedure Move (From : Natural; To : Natural) is
8967 Comps (To) := Comps (From);
8971 -- Gather discriminants into Comp
8973 if DS /= No_List then
8974 Citem := First (DS);
8975 while Present (Citem) loop
8976 if Nkind (Citem) = N_Discriminant_Specification then
8978 Ent : constant Entity_Id :=
8979 Defining_Identifier (Citem);
8981 if Ekind (Ent) = E_Discriminant then
8982 Ncomps := Ncomps + 1;
8983 Comps (Ncomps) := Ent;
8992 -- Gather component entities into Comp
8994 Citem := First (Component_Items (CL));
8995 while Present (Citem) loop
8996 if Nkind (Citem) = N_Component_Declaration then
8997 Ncomps := Ncomps + 1;
8998 Comps (Ncomps) := Defining_Identifier (Citem);
9004 -- Now sort the component entities based on the first bit.
9005 -- Note we already know there are no overlapping components.
9007 Sorting.Sort (Ncomps);
9009 -- Loop through entries checking for holes
9012 for J in 1 .. Ncomps loop
9014 Error_Msg_Uint_1 := Component_Bit_Offset (CEnt) - Nbit;
9016 if Error_Msg_Uint_1 > 0 then
9018 ("?H?^-bit gap before component&",
9019 Component_Name (Component_Clause (CEnt)), CEnt);
9022 Nbit := Component_Bit_Offset (CEnt) + Esize (CEnt);
9025 -- Process variant parts recursively if present
9027 if Present (Variant_Part (CL)) then
9028 Variant := First (Variants (Variant_Part (CL)));
9029 while Present (Variant) loop
9030 Check_Component_List
9031 (Component_List (Variant), Nbit, No_List);
9036 end Check_Component_List;
9038 -- Start of processing for Record_Hole_Check
9045 if Is_Tagged_Type (Rectype) then
9046 Sbit := UI_From_Int (System_Address_Size);
9051 if Nkind (Decl) = N_Full_Type_Declaration
9052 and then Nkind (Type_Definition (Decl)) = N_Record_Definition
9054 Check_Component_List
9055 (Component_List (Type_Definition (Decl)),
9057 Discriminant_Specifications (Decl));
9060 end Record_Hole_Check;
9063 -- For records that have component clauses for all components, and whose
9064 -- size is less than or equal to 32, we need to know the size in the
9065 -- front end to activate possible packed array processing where the
9066 -- component type is a record.
9068 -- At this stage Hbit + 1 represents the first unused bit from all the
9069 -- component clauses processed, so if the component clauses are
9070 -- complete, then this is the length of the record.
9072 -- For records longer than System.Storage_Unit, and for those where not
9073 -- all components have component clauses, the back end determines the
9074 -- length (it may for example be appropriate to round up the size
9075 -- to some convenient boundary, based on alignment considerations, etc).
9077 if Unknown_RM_Size (Rectype) and then Hbit + 1 <= 32 then
9079 -- Nothing to do if at least one component has no component clause
9081 Comp := First_Component_Or_Discriminant (Rectype);
9082 while Present (Comp) loop
9083 exit when No (Component_Clause (Comp));
9084 Next_Component_Or_Discriminant (Comp);
9087 -- If we fall out of loop, all components have component clauses
9088 -- and so we can set the size to the maximum value.
9091 Set_RM_Size (Rectype, Hbit + 1);
9094 end Check_Record_Representation_Clause;
9100 procedure Check_Size
9104 Biased : out Boolean)
9106 UT : constant Entity_Id := Underlying_Type (T);
9112 -- Reject patently improper size values.
9114 if Is_Elementary_Type (T)
9115 and then Siz > UI_From_Int (Int'Last)
9117 Error_Msg_N ("Size value too large for elementary type", N);
9119 if Nkind (Original_Node (N)) = N_Op_Expon then
9121 ("\maybe '* was meant, rather than '*'*", Original_Node (N));
9125 -- Dismiss generic types
9127 if Is_Generic_Type (T)
9129 Is_Generic_Type (UT)
9131 Is_Generic_Type (Root_Type (UT))
9135 -- Guard against previous errors
9137 elsif No (UT) or else UT = Any_Type then
9138 Check_Error_Detected;
9141 -- Check case of bit packed array
9143 elsif Is_Array_Type (UT)
9144 and then Known_Static_Component_Size (UT)
9145 and then Is_Bit_Packed_Array (UT)
9153 Asiz := Component_Size (UT);
9154 Indx := First_Index (UT);
9156 Ityp := Etype (Indx);
9158 -- If non-static bound, then we are not in the business of
9159 -- trying to check the length, and indeed an error will be
9160 -- issued elsewhere, since sizes of non-static array types
9161 -- cannot be set implicitly or explicitly.
9163 if not Is_Static_Subtype (Ityp) then
9167 -- Otherwise accumulate next dimension
9169 Asiz := Asiz * (Expr_Value (Type_High_Bound (Ityp)) -
9170 Expr_Value (Type_Low_Bound (Ityp)) +
9174 exit when No (Indx);
9181 Error_Msg_Uint_1 := Asiz;
9183 ("size for& too small, minimum allowed is ^", N, T);
9184 Set_Esize (T, Asiz);
9185 Set_RM_Size (T, Asiz);
9189 -- All other composite types are ignored
9191 elsif Is_Composite_Type (UT) then
9194 -- For fixed-point types, don't check minimum if type is not frozen,
9195 -- since we don't know all the characteristics of the type that can
9196 -- affect the size (e.g. a specified small) till freeze time.
9198 elsif Is_Fixed_Point_Type (UT)
9199 and then not Is_Frozen (UT)
9203 -- Cases for which a minimum check is required
9206 -- Ignore if specified size is correct for the type
9208 if Known_Esize (UT) and then Siz = Esize (UT) then
9212 -- Otherwise get minimum size
9214 M := UI_From_Int (Minimum_Size (UT));
9218 -- Size is less than minimum size, but one possibility remains
9219 -- that we can manage with the new size if we bias the type.
9221 M := UI_From_Int (Minimum_Size (UT, Biased => True));
9224 Error_Msg_Uint_1 := M;
9226 ("size for& too small, minimum allowed is ^", N, T);
9236 --------------------------
9237 -- Freeze_Entity_Checks --
9238 --------------------------
9240 procedure Freeze_Entity_Checks (N : Node_Id) is
9241 E : constant Entity_Id := Entity (N);
9243 Non_Generic_Case : constant Boolean := Nkind (N) = N_Freeze_Entity;
9244 -- True in non-generic case. Some of the processing here is skipped
9245 -- for the generic case since it is not needed. Basically in the
9246 -- generic case, we only need to do stuff that might generate error
9247 -- messages or warnings.
9249 -- Remember that we are processing a freezing entity. Required to
9250 -- ensure correct decoration of internal entities associated with
9251 -- interfaces (see New_Overloaded_Entity).
9253 Inside_Freezing_Actions := Inside_Freezing_Actions + 1;
9255 -- For tagged types covering interfaces add internal entities that link
9256 -- the primitives of the interfaces with the primitives that cover them.
9257 -- Note: These entities were originally generated only when generating
9258 -- code because their main purpose was to provide support to initialize
9259 -- the secondary dispatch tables. They are now generated also when
9260 -- compiling with no code generation to provide ASIS the relationship
9261 -- between interface primitives and tagged type primitives. They are
9262 -- also used to locate primitives covering interfaces when processing
9263 -- generics (see Derive_Subprograms).
9265 -- This is not needed in the generic case
9267 if Ada_Version >= Ada_2005
9268 and then Non_Generic_Case
9269 and then Ekind (E) = E_Record_Type
9270 and then Is_Tagged_Type (E)
9271 and then not Is_Interface (E)
9272 and then Has_Interfaces (E)
9274 -- This would be a good common place to call the routine that checks
9275 -- overriding of interface primitives (and thus factorize calls to
9276 -- Check_Abstract_Overriding located at different contexts in the
9277 -- compiler). However, this is not possible because it causes
9278 -- spurious errors in case of late overriding.
9280 Add_Internal_Interface_Entities (E);
9285 if Ekind (E) = E_Record_Type
9286 and then Is_CPP_Class (E)
9287 and then Is_Tagged_Type (E)
9288 and then Tagged_Type_Expansion
9290 if CPP_Num_Prims (E) = 0 then
9292 -- If the CPP type has user defined components then it must import
9293 -- primitives from C++. This is required because if the C++ class
9294 -- has no primitives then the C++ compiler does not added the _tag
9295 -- component to the type.
9297 if First_Entity (E) /= Last_Entity (E) then
9299 ("'C'P'P type must import at least one primitive from C++??",
9304 -- Check that all its primitives are abstract or imported from C++.
9305 -- Check also availability of the C++ constructor.
9308 Has_Constructors : constant Boolean := Has_CPP_Constructors (E);
9310 Error_Reported : Boolean := False;
9314 Elmt := First_Elmt (Primitive_Operations (E));
9315 while Present (Elmt) loop
9316 Prim := Node (Elmt);
9318 if Comes_From_Source (Prim) then
9319 if Is_Abstract_Subprogram (Prim) then
9322 elsif not Is_Imported (Prim)
9323 or else Convention (Prim) /= Convention_CPP
9326 ("primitives of 'C'P'P types must be imported from C++ "
9327 & "or abstract??", Prim);
9329 elsif not Has_Constructors
9330 and then not Error_Reported
9332 Error_Msg_Name_1 := Chars (E);
9334 ("??'C'P'P constructor required for type %", Prim);
9335 Error_Reported := True;
9344 -- Check Ada derivation of CPP type
9346 if Expander_Active -- why? losing errors in -gnatc mode???
9347 and then Tagged_Type_Expansion
9348 and then Ekind (E) = E_Record_Type
9349 and then Etype (E) /= E
9350 and then Is_CPP_Class (Etype (E))
9351 and then CPP_Num_Prims (Etype (E)) > 0
9352 and then not Is_CPP_Class (E)
9353 and then not Has_CPP_Constructors (Etype (E))
9355 -- If the parent has C++ primitives but it has no constructor then
9356 -- check that all the primitives are overridden in this derivation;
9357 -- otherwise the constructor of the parent is needed to build the
9365 Elmt := First_Elmt (Primitive_Operations (E));
9366 while Present (Elmt) loop
9367 Prim := Node (Elmt);
9369 if not Is_Abstract_Subprogram (Prim)
9370 and then No (Interface_Alias (Prim))
9371 and then Find_Dispatching_Type (Ultimate_Alias (Prim)) /= E
9373 Error_Msg_Name_1 := Chars (Etype (E));
9375 ("'C'P'P constructor required for parent type %", E);
9384 Inside_Freezing_Actions := Inside_Freezing_Actions - 1;
9386 -- If we have a type with predicates, build predicate function. This
9387 -- is not needed in the generic casee
9389 if Non_Generic_Case and then Is_Type (E) and then Has_Predicates (E) then
9390 Build_Predicate_Functions (E, N);
9393 -- If type has delayed aspects, this is where we do the preanalysis at
9394 -- the freeze point, as part of the consistent visibility check. Note
9395 -- that this must be done after calling Build_Predicate_Functions or
9396 -- Build_Invariant_Procedure since these subprograms fix occurrences of
9397 -- the subtype name in the saved expression so that they will not cause
9398 -- trouble in the preanalysis.
9400 -- This is also not needed in the generic case
9403 and then Has_Delayed_Aspects (E)
9404 and then Scope (E) = Current_Scope
9406 -- Retrieve the visibility to the discriminants in order to properly
9407 -- analyze the aspects.
9409 Push_Scope_And_Install_Discriminants (E);
9415 -- Look for aspect specification entries for this entity
9417 Ritem := First_Rep_Item (E);
9418 while Present (Ritem) loop
9419 if Nkind (Ritem) = N_Aspect_Specification
9420 and then Entity (Ritem) = E
9421 and then Is_Delayed_Aspect (Ritem)
9423 Check_Aspect_At_Freeze_Point (Ritem);
9426 Next_Rep_Item (Ritem);
9430 Uninstall_Discriminants_And_Pop_Scope (E);
9433 -- For a record type, deal with variant parts. This has to be delayed
9434 -- to this point, because of the issue of statically precicated
9435 -- subtypes, which we have to ensure are frozen before checking
9436 -- choices, since we need to have the static choice list set.
9438 if Is_Record_Type (E) then
9439 Check_Variant_Part : declare
9440 D : constant Node_Id := Declaration_Node (E);
9445 Others_Present : Boolean;
9446 pragma Warnings (Off, Others_Present);
9447 -- Indicates others present, not used in this case
9449 procedure Non_Static_Choice_Error (Choice : Node_Id);
9450 -- Error routine invoked by the generic instantiation below when
9451 -- the variant part has a non static choice.
9453 procedure Process_Declarations (Variant : Node_Id);
9454 -- Processes declarations associated with a variant. We analyzed
9455 -- the declarations earlier (in Sem_Ch3.Analyze_Variant_Part),
9456 -- but we still need the recursive call to Check_Choices for any
9457 -- nested variant to get its choices properly processed. This is
9458 -- also where we expand out the choices if expansion is active.
9460 package Variant_Choices_Processing is new
9461 Generic_Check_Choices
9462 (Process_Empty_Choice => No_OP,
9463 Process_Non_Static_Choice => Non_Static_Choice_Error,
9464 Process_Associated_Node => Process_Declarations);
9465 use Variant_Choices_Processing;
9467 -----------------------------
9468 -- Non_Static_Choice_Error --
9469 -----------------------------
9471 procedure Non_Static_Choice_Error (Choice : Node_Id) is
9473 Flag_Non_Static_Expr
9474 ("choice given in variant part is not static!", Choice);
9475 end Non_Static_Choice_Error;
9477 --------------------------
9478 -- Process_Declarations --
9479 --------------------------
9481 procedure Process_Declarations (Variant : Node_Id) is
9482 CL : constant Node_Id := Component_List (Variant);
9486 -- Check for static predicate present in this variant
9488 if Has_SP_Choice (Variant) then
9490 -- Here we expand. You might expect to find this call in
9491 -- Expand_N_Variant_Part, but that is called when we first
9492 -- see the variant part, and we cannot do this expansion
9493 -- earlier than the freeze point, since for statically
9494 -- predicated subtypes, the predicate is not known till
9495 -- the freeze point.
9497 -- Furthermore, we do this expansion even if the expander
9498 -- is not active, because other semantic processing, e.g.
9499 -- for aggregates, requires the expanded list of choices.
9501 -- If the expander is not active, then we can't just clobber
9502 -- the list since it would invalidate the ASIS -gnatct tree.
9503 -- So we have to rewrite the variant part with a Rewrite
9504 -- call that replaces it with a copy and clobber the copy.
9506 if not Expander_Active then
9508 NewV : constant Node_Id := New_Copy (Variant);
9510 Set_Discrete_Choices
9511 (NewV, New_Copy_List (Discrete_Choices (Variant)));
9512 Rewrite (Variant, NewV);
9516 Expand_Static_Predicates_In_Choices (Variant);
9519 -- We don't need to worry about the declarations in the variant
9520 -- (since they were analyzed by Analyze_Choices when we first
9521 -- encountered the variant), but we do need to take care of
9522 -- expansion of any nested variants.
9524 if not Null_Present (CL) then
9525 VP := Variant_Part (CL);
9527 if Present (VP) then
9529 (VP, Variants (VP), Etype (Name (VP)), Others_Present);
9532 end Process_Declarations;
9534 -- Start of processing for Check_Variant_Part
9537 -- Find component list
9541 if Nkind (D) = N_Full_Type_Declaration then
9542 T := Type_Definition (D);
9544 if Nkind (T) = N_Record_Definition then
9545 C := Component_List (T);
9547 elsif Nkind (T) = N_Derived_Type_Definition
9548 and then Present (Record_Extension_Part (T))
9550 C := Component_List (Record_Extension_Part (T));
9554 -- Case of variant part present
9556 if Present (C) and then Present (Variant_Part (C)) then
9557 VP := Variant_Part (C);
9562 (VP, Variants (VP), Etype (Name (VP)), Others_Present);
9564 -- If the last variant does not contain the Others choice,
9565 -- replace it with an N_Others_Choice node since Gigi always
9566 -- wants an Others. Note that we do not bother to call Analyze
9567 -- on the modified variant part, since its only effect would be
9568 -- to compute the Others_Discrete_Choices node laboriously, and
9569 -- of course we already know the list of choices corresponding
9570 -- to the others choice (it's the list we're replacing!)
9572 -- We only want to do this if the expander is active, since
9573 -- we do not want to clobber the ASIS tree!
9575 if Expander_Active then
9577 Last_Var : constant Node_Id :=
9578 Last_Non_Pragma (Variants (VP));
9580 Others_Node : Node_Id;
9583 if Nkind (First (Discrete_Choices (Last_Var))) /=
9586 Others_Node := Make_Others_Choice (Sloc (Last_Var));
9587 Set_Others_Discrete_Choices
9588 (Others_Node, Discrete_Choices (Last_Var));
9589 Set_Discrete_Choices
9590 (Last_Var, New_List (Others_Node));
9595 end Check_Variant_Part;
9597 end Freeze_Entity_Checks;
9599 -------------------------
9600 -- Get_Alignment_Value --
9601 -------------------------
9603 function Get_Alignment_Value (Expr : Node_Id) return Uint is
9604 Align : constant Uint := Static_Integer (Expr);
9607 if Align = No_Uint then
9610 elsif Align <= 0 then
9611 Error_Msg_N ("alignment value must be positive", Expr);
9615 for J in Int range 0 .. 64 loop
9617 M : constant Uint := Uint_2 ** J;
9620 exit when M = Align;
9624 ("alignment value must be power of 2", Expr);
9632 end Get_Alignment_Value;
9634 -------------------------------------
9635 -- Inherit_Aspects_At_Freeze_Point --
9636 -------------------------------------
9638 procedure Inherit_Aspects_At_Freeze_Point (Typ : Entity_Id) is
9639 function Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
9640 (Rep_Item : Node_Id) return Boolean;
9641 -- This routine checks if Rep_Item is either a pragma or an aspect
9642 -- specification node whose correponding pragma (if any) is present in
9643 -- the Rep Item chain of the entity it has been specified to.
9645 --------------------------------------------------
9646 -- Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item --
9647 --------------------------------------------------
9649 function Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
9650 (Rep_Item : Node_Id) return Boolean
9653 return Nkind (Rep_Item) = N_Pragma
9654 or else Present_In_Rep_Item
9655 (Entity (Rep_Item), Aspect_Rep_Item (Rep_Item));
9656 end Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item;
9658 -- Start of processing for Inherit_Aspects_At_Freeze_Point
9661 -- A representation item is either subtype-specific (Size and Alignment
9662 -- clauses) or type-related (all others). Subtype-specific aspects may
9663 -- differ for different subtypes of the same type (RM 13.1.8).
9665 -- A derived type inherits each type-related representation aspect of
9666 -- its parent type that was directly specified before the declaration of
9667 -- the derived type (RM 13.1.15).
9669 -- A derived subtype inherits each subtype-specific representation
9670 -- aspect of its parent subtype that was directly specified before the
9671 -- declaration of the derived type (RM 13.1.15).
9673 -- The general processing involves inheriting a representation aspect
9674 -- from a parent type whenever the first rep item (aspect specification,
9675 -- attribute definition clause, pragma) corresponding to the given
9676 -- representation aspect in the rep item chain of Typ, if any, isn't
9677 -- directly specified to Typ but to one of its parents.
9679 -- ??? Note that, for now, just a limited number of representation
9680 -- aspects have been inherited here so far. Many of them are
9681 -- still inherited in Sem_Ch3. This will be fixed soon. Here is
9682 -- a non- exhaustive list of aspects that likely also need to
9683 -- be moved to this routine: Alignment, Component_Alignment,
9684 -- Component_Size, Machine_Radix, Object_Size, Pack, Predicates,
9685 -- Preelaborable_Initialization, RM_Size and Small.
9687 if Nkind (Parent (Typ)) = N_Private_Extension_Declaration then
9693 if not Has_Rep_Item (Typ, Name_Ada_05, Name_Ada_2005, False)
9694 and then Has_Rep_Item (Typ, Name_Ada_05, Name_Ada_2005)
9695 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
9696 (Get_Rep_Item (Typ, Name_Ada_05, Name_Ada_2005))
9698 Set_Is_Ada_2005_Only (Typ);
9703 if not Has_Rep_Item (Typ, Name_Ada_12, Name_Ada_2012, False)
9704 and then Has_Rep_Item (Typ, Name_Ada_12, Name_Ada_2012)
9705 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
9706 (Get_Rep_Item (Typ, Name_Ada_12, Name_Ada_2012))
9708 Set_Is_Ada_2012_Only (Typ);
9713 if not Has_Rep_Item (Typ, Name_Atomic, Name_Shared, False)
9714 and then Has_Rep_Pragma (Typ, Name_Atomic, Name_Shared)
9715 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
9716 (Get_Rep_Item (Typ, Name_Atomic, Name_Shared))
9718 Set_Is_Atomic (Typ);
9719 Set_Treat_As_Volatile (Typ);
9720 Set_Is_Volatile (Typ);
9723 -- Default_Component_Value
9725 if Is_Array_Type (Typ)
9726 and then Is_Base_Type (Typ)
9727 and then Has_Rep_Item (Typ, Name_Default_Component_Value, False)
9728 and then Has_Rep_Item (Typ, Name_Default_Component_Value)
9730 Set_Default_Aspect_Component_Value (Typ,
9731 Default_Aspect_Component_Value
9732 (Entity (Get_Rep_Item (Typ, Name_Default_Component_Value))));
9737 if Is_Scalar_Type (Typ)
9738 and then Is_Base_Type (Typ)
9739 and then Has_Rep_Item (Typ, Name_Default_Value, False)
9740 and then Has_Rep_Item (Typ, Name_Default_Value)
9742 Set_Default_Aspect_Value (Typ,
9743 Default_Aspect_Value
9744 (Entity (Get_Rep_Item (Typ, Name_Default_Value))));
9749 if not Has_Rep_Item (Typ, Name_Discard_Names, False)
9750 and then Has_Rep_Item (Typ, Name_Discard_Names)
9751 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
9752 (Get_Rep_Item (Typ, Name_Discard_Names))
9754 Set_Discard_Names (Typ);
9759 if not Has_Rep_Item (Typ, Name_Invariant, False)
9760 and then Has_Rep_Item (Typ, Name_Invariant)
9761 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
9762 (Get_Rep_Item (Typ, Name_Invariant))
9764 Set_Has_Invariants (Typ);
9766 if Class_Present (Get_Rep_Item (Typ, Name_Invariant)) then
9767 Set_Has_Inheritable_Invariants (Typ);
9773 if not Has_Rep_Item (Typ, Name_Volatile, False)
9774 and then Has_Rep_Item (Typ, Name_Volatile)
9775 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
9776 (Get_Rep_Item (Typ, Name_Volatile))
9778 Set_Treat_As_Volatile (Typ);
9779 Set_Is_Volatile (Typ);
9782 -- Inheritance for derived types only
9784 if Is_Derived_Type (Typ) then
9786 Bas_Typ : constant Entity_Id := Base_Type (Typ);
9787 Imp_Bas_Typ : constant Entity_Id := Implementation_Base_Type (Typ);
9790 -- Atomic_Components
9792 if not Has_Rep_Item (Typ, Name_Atomic_Components, False)
9793 and then Has_Rep_Item (Typ, Name_Atomic_Components)
9794 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
9795 (Get_Rep_Item (Typ, Name_Atomic_Components))
9797 Set_Has_Atomic_Components (Imp_Bas_Typ);
9800 -- Volatile_Components
9802 if not Has_Rep_Item (Typ, Name_Volatile_Components, False)
9803 and then Has_Rep_Item (Typ, Name_Volatile_Components)
9804 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
9805 (Get_Rep_Item (Typ, Name_Volatile_Components))
9807 Set_Has_Volatile_Components (Imp_Bas_Typ);
9810 -- Finalize_Storage_Only.
9812 if not Has_Rep_Pragma (Typ, Name_Finalize_Storage_Only, False)
9813 and then Has_Rep_Pragma (Typ, Name_Finalize_Storage_Only)
9815 Set_Finalize_Storage_Only (Bas_Typ);
9818 -- Universal_Aliasing
9820 if not Has_Rep_Item (Typ, Name_Universal_Aliasing, False)
9821 and then Has_Rep_Item (Typ, Name_Universal_Aliasing)
9822 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
9823 (Get_Rep_Item (Typ, Name_Universal_Aliasing))
9825 Set_Universal_Aliasing (Imp_Bas_Typ);
9828 -- Record type specific aspects
9830 if Is_Record_Type (Typ) then
9834 if not Has_Rep_Item (Typ, Name_Bit_Order, False)
9835 and then Has_Rep_Item (Typ, Name_Bit_Order)
9837 Set_Reverse_Bit_Order (Bas_Typ,
9838 Reverse_Bit_Order (Entity (Name
9839 (Get_Rep_Item (Typ, Name_Bit_Order)))));
9842 -- Scalar_Storage_Order
9844 if not Has_Rep_Item (Typ, Name_Scalar_Storage_Order, False)
9845 and then Has_Rep_Item (Typ, Name_Scalar_Storage_Order)
9847 Set_Reverse_Storage_Order (Bas_Typ,
9848 Reverse_Storage_Order (Entity (Name
9849 (Get_Rep_Item (Typ, Name_Scalar_Storage_Order)))));
9854 end Inherit_Aspects_At_Freeze_Point;
9860 procedure Initialize is
9862 Address_Clause_Checks.Init;
9863 Independence_Checks.Init;
9864 Unchecked_Conversions.Init;
9867 -------------------------
9868 -- Is_Operational_Item --
9869 -------------------------
9871 function Is_Operational_Item (N : Node_Id) return Boolean is
9873 if Nkind (N) /= N_Attribute_Definition_Clause then
9878 Id : constant Attribute_Id := Get_Attribute_Id (Chars (N));
9880 return Id = Attribute_Input
9881 or else Id = Attribute_Output
9882 or else Id = Attribute_Read
9883 or else Id = Attribute_Write
9884 or else Id = Attribute_External_Tag;
9887 end Is_Operational_Item;
9893 function Minimum_Size
9895 Biased : Boolean := False) return Nat
9897 Lo : Uint := No_Uint;
9898 Hi : Uint := No_Uint;
9899 LoR : Ureal := No_Ureal;
9900 HiR : Ureal := No_Ureal;
9901 LoSet : Boolean := False;
9902 HiSet : Boolean := False;
9906 R_Typ : constant Entity_Id := Root_Type (T);
9909 -- If bad type, return 0
9911 if T = Any_Type then
9914 -- For generic types, just return zero. There cannot be any legitimate
9915 -- need to know such a size, but this routine may be called with a
9916 -- generic type as part of normal processing.
9918 elsif Is_Generic_Type (R_Typ)
9919 or else R_Typ = Any_Type
9923 -- Access types. Normally an access type cannot have a size smaller
9924 -- than the size of System.Address. The exception is on VMS, where
9925 -- we have short and long addresses, and it is possible for an access
9926 -- type to have a short address size (and thus be less than the size
9927 -- of System.Address itself). We simply skip the check for VMS, and
9928 -- leave it to the back end to do the check.
9930 elsif Is_Access_Type (T) then
9931 if OpenVMS_On_Target then
9934 return System_Address_Size;
9937 -- Floating-point types
9939 elsif Is_Floating_Point_Type (T) then
9940 return UI_To_Int (Esize (R_Typ));
9944 elsif Is_Discrete_Type (T) then
9946 -- The following loop is looking for the nearest compile time known
9947 -- bounds following the ancestor subtype chain. The idea is to find
9948 -- the most restrictive known bounds information.
9952 if Ancest = Any_Type or else Etype (Ancest) = Any_Type then
9957 if Compile_Time_Known_Value (Type_Low_Bound (Ancest)) then
9958 Lo := Expr_Rep_Value (Type_Low_Bound (Ancest));
9965 if Compile_Time_Known_Value (Type_High_Bound (Ancest)) then
9966 Hi := Expr_Rep_Value (Type_High_Bound (Ancest));
9972 Ancest := Ancestor_Subtype (Ancest);
9975 Ancest := Base_Type (T);
9977 if Is_Generic_Type (Ancest) then
9983 -- Fixed-point types. We can't simply use Expr_Value to get the
9984 -- Corresponding_Integer_Value values of the bounds, since these do not
9985 -- get set till the type is frozen, and this routine can be called
9986 -- before the type is frozen. Similarly the test for bounds being static
9987 -- needs to include the case where we have unanalyzed real literals for
9990 elsif Is_Fixed_Point_Type (T) then
9992 -- The following loop is looking for the nearest compile time known
9993 -- bounds following the ancestor subtype chain. The idea is to find
9994 -- the most restrictive known bounds information.
9998 if Ancest = Any_Type or else Etype (Ancest) = Any_Type then
10002 -- Note: In the following two tests for LoSet and HiSet, it may
10003 -- seem redundant to test for N_Real_Literal here since normally
10004 -- one would assume that the test for the value being known at
10005 -- compile time includes this case. However, there is a glitch.
10006 -- If the real literal comes from folding a non-static expression,
10007 -- then we don't consider any non- static expression to be known
10008 -- at compile time if we are in configurable run time mode (needed
10009 -- in some cases to give a clearer definition of what is and what
10010 -- is not accepted). So the test is indeed needed. Without it, we
10011 -- would set neither Lo_Set nor Hi_Set and get an infinite loop.
10014 if Nkind (Type_Low_Bound (Ancest)) = N_Real_Literal
10015 or else Compile_Time_Known_Value (Type_Low_Bound (Ancest))
10017 LoR := Expr_Value_R (Type_Low_Bound (Ancest));
10024 if Nkind (Type_High_Bound (Ancest)) = N_Real_Literal
10025 or else Compile_Time_Known_Value (Type_High_Bound (Ancest))
10027 HiR := Expr_Value_R (Type_High_Bound (Ancest));
10033 Ancest := Ancestor_Subtype (Ancest);
10035 if No (Ancest) then
10036 Ancest := Base_Type (T);
10038 if Is_Generic_Type (Ancest) then
10044 Lo := UR_To_Uint (LoR / Small_Value (T));
10045 Hi := UR_To_Uint (HiR / Small_Value (T));
10047 -- No other types allowed
10050 raise Program_Error;
10053 -- Fall through with Hi and Lo set. Deal with biased case
10056 and then not Is_Fixed_Point_Type (T)
10057 and then not (Is_Enumeration_Type (T)
10058 and then Has_Non_Standard_Rep (T)))
10059 or else Has_Biased_Representation (T)
10065 -- Signed case. Note that we consider types like range 1 .. -1 to be
10066 -- signed for the purpose of computing the size, since the bounds have
10067 -- to be accommodated in the base type.
10069 if Lo < 0 or else Hi < 0 then
10073 -- S = size, B = 2 ** (size - 1) (can accommodate -B .. +(B - 1))
10074 -- Note that we accommodate the case where the bounds cross. This
10075 -- can happen either because of the way the bounds are declared
10076 -- or because of the algorithm in Freeze_Fixed_Point_Type.
10090 -- If both bounds are positive, make sure that both are represen-
10091 -- table in the case where the bounds are crossed. This can happen
10092 -- either because of the way the bounds are declared, or because of
10093 -- the algorithm in Freeze_Fixed_Point_Type.
10099 -- S = size, (can accommodate 0 .. (2**size - 1))
10102 while Hi >= Uint_2 ** S loop
10110 ---------------------------
10111 -- New_Stream_Subprogram --
10112 ---------------------------
10114 procedure New_Stream_Subprogram
10118 Nam : TSS_Name_Type)
10120 Loc : constant Source_Ptr := Sloc (N);
10121 Sname : constant Name_Id := Make_TSS_Name (Base_Type (Ent), Nam);
10122 Subp_Id : Entity_Id;
10123 Subp_Decl : Node_Id;
10127 Defer_Declaration : constant Boolean :=
10128 Is_Tagged_Type (Ent) or else Is_Private_Type (Ent);
10129 -- For a tagged type, there is a declaration for each stream attribute
10130 -- at the freeze point, and we must generate only a completion of this
10131 -- declaration. We do the same for private types, because the full view
10132 -- might be tagged. Otherwise we generate a declaration at the point of
10133 -- the attribute definition clause.
10135 function Build_Spec return Node_Id;
10136 -- Used for declaration and renaming declaration, so that this is
10137 -- treated as a renaming_as_body.
10143 function Build_Spec return Node_Id is
10144 Out_P : constant Boolean := (Nam = TSS_Stream_Read);
10147 T_Ref : constant Node_Id := New_Reference_To (Etyp, Loc);
10150 Subp_Id := Make_Defining_Identifier (Loc, Sname);
10152 -- S : access Root_Stream_Type'Class
10154 Formals := New_List (
10155 Make_Parameter_Specification (Loc,
10156 Defining_Identifier =>
10157 Make_Defining_Identifier (Loc, Name_S),
10159 Make_Access_Definition (Loc,
10162 Designated_Type (Etype (F)), Loc))));
10164 if Nam = TSS_Stream_Input then
10166 Make_Function_Specification (Loc,
10167 Defining_Unit_Name => Subp_Id,
10168 Parameter_Specifications => Formals,
10169 Result_Definition => T_Ref);
10173 Append_To (Formals,
10174 Make_Parameter_Specification (Loc,
10175 Defining_Identifier => Make_Defining_Identifier (Loc, Name_V),
10176 Out_Present => Out_P,
10177 Parameter_Type => T_Ref));
10180 Make_Procedure_Specification (Loc,
10181 Defining_Unit_Name => Subp_Id,
10182 Parameter_Specifications => Formals);
10188 -- Start of processing for New_Stream_Subprogram
10191 F := First_Formal (Subp);
10193 if Ekind (Subp) = E_Procedure then
10194 Etyp := Etype (Next_Formal (F));
10196 Etyp := Etype (Subp);
10199 -- Prepare subprogram declaration and insert it as an action on the
10200 -- clause node. The visibility for this entity is used to test for
10201 -- visibility of the attribute definition clause (in the sense of
10202 -- 8.3(23) as amended by AI-195).
10204 if not Defer_Declaration then
10206 Make_Subprogram_Declaration (Loc,
10207 Specification => Build_Spec);
10209 -- For a tagged type, there is always a visible declaration for each
10210 -- stream TSS (it is a predefined primitive operation), and the
10211 -- completion of this declaration occurs at the freeze point, which is
10212 -- not always visible at places where the attribute definition clause is
10213 -- visible. So, we create a dummy entity here for the purpose of
10214 -- tracking the visibility of the attribute definition clause itself.
10218 Make_Defining_Identifier (Loc, New_External_Name (Sname, 'V'));
10220 Make_Object_Declaration (Loc,
10221 Defining_Identifier => Subp_Id,
10222 Object_Definition => New_Occurrence_Of (Standard_Boolean, Loc));
10225 Insert_Action (N, Subp_Decl);
10226 Set_Entity (N, Subp_Id);
10229 Make_Subprogram_Renaming_Declaration (Loc,
10230 Specification => Build_Spec,
10231 Name => New_Reference_To (Subp, Loc));
10233 if Defer_Declaration then
10234 Set_TSS (Base_Type (Ent), Subp_Id);
10236 Insert_Action (N, Subp_Decl);
10237 Copy_TSS (Subp_Id, Base_Type (Ent));
10239 end New_Stream_Subprogram;
10241 ------------------------
10242 -- Rep_Item_Too_Early --
10243 ------------------------
10245 function Rep_Item_Too_Early (T : Entity_Id; N : Node_Id) return Boolean is
10247 -- Cannot apply non-operational rep items to generic types
10249 if Is_Operational_Item (N) then
10253 and then Is_Generic_Type (Root_Type (T))
10255 Error_Msg_N ("representation item not allowed for generic type", N);
10259 -- Otherwise check for incomplete type
10261 if Is_Incomplete_Or_Private_Type (T)
10262 and then No (Underlying_Type (T))
10264 (Nkind (N) /= N_Pragma
10265 or else Get_Pragma_Id (N) /= Pragma_Import)
10268 ("representation item must be after full type declaration", N);
10271 -- If the type has incomplete components, a representation clause is
10272 -- illegal but stream attributes and Convention pragmas are correct.
10274 elsif Has_Private_Component (T) then
10275 if Nkind (N) = N_Pragma then
10280 ("representation item must appear after type is fully defined",
10287 end Rep_Item_Too_Early;
10289 -----------------------
10290 -- Rep_Item_Too_Late --
10291 -----------------------
10293 function Rep_Item_Too_Late
10296 FOnly : Boolean := False) return Boolean
10299 Parent_Type : Entity_Id;
10301 procedure Too_Late;
10302 -- Output the too late message. Note that this is not considered a
10303 -- serious error, since the effect is simply that we ignore the
10304 -- representation clause in this case.
10310 procedure Too_Late is
10312 -- Other compilers seem more relaxed about rep items appearing too
10313 -- late. Since analysis tools typically don't care about rep items
10314 -- anyway, no reason to be too strict about this.
10316 if not Relaxed_RM_Semantics then
10317 Error_Msg_N ("|representation item appears too late!", N);
10321 -- Start of processing for Rep_Item_Too_Late
10324 -- First make sure entity is not frozen (RM 13.1(9))
10328 -- Exclude imported types, which may be frozen if they appear in a
10329 -- representation clause for a local type.
10331 and then not From_Limited_With (T)
10333 -- Exclude generated entities (not coming from source). The common
10334 -- case is when we generate a renaming which prematurely freezes the
10335 -- renamed internal entity, but we still want to be able to set copies
10336 -- of attribute values such as Size/Alignment.
10338 and then Comes_From_Source (T)
10341 S := First_Subtype (T);
10343 if Present (Freeze_Node (S)) then
10345 ("??no more representation items for }", Freeze_Node (S), S);
10350 -- Check for case of non-tagged derived type whose parent either has
10351 -- primitive operations, or is a by reference type (RM 13.1(10)).
10355 and then Is_Derived_Type (T)
10356 and then not Is_Tagged_Type (T)
10358 Parent_Type := Etype (Base_Type (T));
10360 if Has_Primitive_Operations (Parent_Type) then
10363 ("primitive operations already defined for&!", N, Parent_Type);
10366 elsif Is_By_Reference_Type (Parent_Type) then
10369 ("parent type & is a by reference type!", N, Parent_Type);
10374 -- No error, link item into head of chain of rep items for the entity,
10375 -- but avoid chaining if we have an overloadable entity, and the pragma
10376 -- is one that can apply to multiple overloaded entities.
10378 if Is_Overloadable (T) and then Nkind (N) = N_Pragma then
10380 Pname : constant Name_Id := Pragma_Name (N);
10382 if Nam_In (Pname, Name_Convention, Name_Import, Name_Export,
10383 Name_External, Name_Interface)
10390 Record_Rep_Item (T, N);
10392 end Rep_Item_Too_Late;
10394 -------------------------------------
10395 -- Replace_Type_References_Generic --
10396 -------------------------------------
10398 procedure Replace_Type_References_Generic (N : Node_Id; TName : Name_Id) is
10400 function Replace_Node (N : Node_Id) return Traverse_Result;
10401 -- Processes a single node in the traversal procedure below, checking
10402 -- if node N should be replaced, and if so, doing the replacement.
10404 procedure Replace_Type_Refs is new Traverse_Proc (Replace_Node);
10405 -- This instantiation provides the body of Replace_Type_References
10411 function Replace_Node (N : Node_Id) return Traverse_Result is
10416 -- Case of identifier
10418 if Nkind (N) = N_Identifier then
10420 -- If not the type name, all done with this node
10422 if Chars (N) /= TName then
10425 -- Otherwise do the replacement and we are done with this node
10428 Replace_Type_Reference (N);
10432 -- Case of selected component (which is what a qualification
10433 -- looks like in the unanalyzed tree, which is what we have.
10435 elsif Nkind (N) = N_Selected_Component then
10437 -- If selector name is not our type, keeping going (we might
10438 -- still have an occurrence of the type in the prefix).
10440 if Nkind (Selector_Name (N)) /= N_Identifier
10441 or else Chars (Selector_Name (N)) /= TName
10445 -- Selector name is our type, check qualification
10448 -- Loop through scopes and prefixes, doing comparison
10450 S := Current_Scope;
10453 -- Continue if no more scopes or scope with no name
10455 if No (S) or else Nkind (S) not in N_Has_Chars then
10459 -- Do replace if prefix is an identifier matching the
10460 -- scope that we are currently looking at.
10462 if Nkind (P) = N_Identifier
10463 and then Chars (P) = Chars (S)
10465 Replace_Type_Reference (N);
10469 -- Go check scope above us if prefix is itself of the
10470 -- form of a selected component, whose selector matches
10471 -- the scope we are currently looking at.
10473 if Nkind (P) = N_Selected_Component
10474 and then Nkind (Selector_Name (P)) = N_Identifier
10475 and then Chars (Selector_Name (P)) = Chars (S)
10480 -- For anything else, we don't have a match, so keep on
10481 -- going, there are still some weird cases where we may
10482 -- still have a replacement within the prefix.
10490 -- Continue for any other node kind
10498 Replace_Type_Refs (N);
10499 end Replace_Type_References_Generic;
10501 -------------------------
10502 -- Same_Representation --
10503 -------------------------
10505 function Same_Representation (Typ1, Typ2 : Entity_Id) return Boolean is
10506 T1 : constant Entity_Id := Underlying_Type (Typ1);
10507 T2 : constant Entity_Id := Underlying_Type (Typ2);
10510 -- A quick check, if base types are the same, then we definitely have
10511 -- the same representation, because the subtype specific representation
10512 -- attributes (Size and Alignment) do not affect representation from
10513 -- the point of view of this test.
10515 if Base_Type (T1) = Base_Type (T2) then
10518 elsif Is_Private_Type (Base_Type (T2))
10519 and then Base_Type (T1) = Full_View (Base_Type (T2))
10524 -- Tagged types never have differing representations
10526 if Is_Tagged_Type (T1) then
10530 -- Representations are definitely different if conventions differ
10532 if Convention (T1) /= Convention (T2) then
10536 -- Representations are different if component alignments or scalar
10537 -- storage orders differ.
10539 if (Is_Record_Type (T1) or else Is_Array_Type (T1))
10541 (Is_Record_Type (T2) or else Is_Array_Type (T2))
10543 (Component_Alignment (T1) /= Component_Alignment (T2)
10545 Reverse_Storage_Order (T1) /= Reverse_Storage_Order (T2))
10550 -- For arrays, the only real issue is component size. If we know the
10551 -- component size for both arrays, and it is the same, then that's
10552 -- good enough to know we don't have a change of representation.
10554 if Is_Array_Type (T1) then
10555 if Known_Component_Size (T1)
10556 and then Known_Component_Size (T2)
10557 and then Component_Size (T1) = Component_Size (T2)
10559 if VM_Target = No_VM then
10562 -- In VM targets the representation of arrays with aliased
10563 -- components differs from arrays with non-aliased components
10566 return Has_Aliased_Components (Base_Type (T1))
10568 Has_Aliased_Components (Base_Type (T2));
10573 -- Types definitely have same representation if neither has non-standard
10574 -- representation since default representations are always consistent.
10575 -- If only one has non-standard representation, and the other does not,
10576 -- then we consider that they do not have the same representation. They
10577 -- might, but there is no way of telling early enough.
10579 if Has_Non_Standard_Rep (T1) then
10580 if not Has_Non_Standard_Rep (T2) then
10584 return not Has_Non_Standard_Rep (T2);
10587 -- Here the two types both have non-standard representation, and we need
10588 -- to determine if they have the same non-standard representation.
10590 -- For arrays, we simply need to test if the component sizes are the
10591 -- same. Pragma Pack is reflected in modified component sizes, so this
10592 -- check also deals with pragma Pack.
10594 if Is_Array_Type (T1) then
10595 return Component_Size (T1) = Component_Size (T2);
10597 -- Tagged types always have the same representation, because it is not
10598 -- possible to specify different representations for common fields.
10600 elsif Is_Tagged_Type (T1) then
10603 -- Case of record types
10605 elsif Is_Record_Type (T1) then
10607 -- Packed status must conform
10609 if Is_Packed (T1) /= Is_Packed (T2) then
10612 -- Otherwise we must check components. Typ2 maybe a constrained
10613 -- subtype with fewer components, so we compare the components
10614 -- of the base types.
10617 Record_Case : declare
10618 CD1, CD2 : Entity_Id;
10620 function Same_Rep return Boolean;
10621 -- CD1 and CD2 are either components or discriminants. This
10622 -- function tests whether they have the same representation.
10628 function Same_Rep return Boolean is
10630 if No (Component_Clause (CD1)) then
10631 return No (Component_Clause (CD2));
10633 -- Note: at this point, component clauses have been
10634 -- normalized to the default bit order, so that the
10635 -- comparison of Component_Bit_Offsets is meaningful.
10638 Present (Component_Clause (CD2))
10640 Component_Bit_Offset (CD1) = Component_Bit_Offset (CD2)
10642 Esize (CD1) = Esize (CD2);
10646 -- Start of processing for Record_Case
10649 if Has_Discriminants (T1) then
10651 -- The number of discriminants may be different if the
10652 -- derived type has fewer (constrained by values). The
10653 -- invisible discriminants retain the representation of
10654 -- the original, so the discrepancy does not per se
10655 -- indicate a different representation.
10657 CD1 := First_Discriminant (T1);
10658 CD2 := First_Discriminant (T2);
10659 while Present (CD1) and then Present (CD2) loop
10660 if not Same_Rep then
10663 Next_Discriminant (CD1);
10664 Next_Discriminant (CD2);
10669 CD1 := First_Component (Underlying_Type (Base_Type (T1)));
10670 CD2 := First_Component (Underlying_Type (Base_Type (T2)));
10671 while Present (CD1) loop
10672 if not Same_Rep then
10675 Next_Component (CD1);
10676 Next_Component (CD2);
10684 -- For enumeration types, we must check each literal to see if the
10685 -- representation is the same. Note that we do not permit enumeration
10686 -- representation clauses for Character and Wide_Character, so these
10687 -- cases were already dealt with.
10689 elsif Is_Enumeration_Type (T1) then
10690 Enumeration_Case : declare
10691 L1, L2 : Entity_Id;
10694 L1 := First_Literal (T1);
10695 L2 := First_Literal (T2);
10696 while Present (L1) loop
10697 if Enumeration_Rep (L1) /= Enumeration_Rep (L2) then
10706 end Enumeration_Case;
10708 -- Any other types have the same representation for these purposes
10713 end Same_Representation;
10719 procedure Set_Biased
10723 Biased : Boolean := True)
10727 Set_Has_Biased_Representation (E);
10729 if Warn_On_Biased_Representation then
10731 ("?B?" & Msg & " forces biased representation for&", N, E);
10736 --------------------
10737 -- Set_Enum_Esize --
10738 --------------------
10740 procedure Set_Enum_Esize (T : Entity_Id) is
10746 Init_Alignment (T);
10748 -- Find the minimum standard size (8,16,32,64) that fits
10750 Lo := Enumeration_Rep (Entity (Type_Low_Bound (T)));
10751 Hi := Enumeration_Rep (Entity (Type_High_Bound (T)));
10754 if Lo >= -Uint_2**07 and then Hi < Uint_2**07 then
10755 Sz := Standard_Character_Size; -- May be > 8 on some targets
10757 elsif Lo >= -Uint_2**15 and then Hi < Uint_2**15 then
10760 elsif Lo >= -Uint_2**31 and then Hi < Uint_2**31 then
10763 else pragma Assert (Lo >= -Uint_2**63 and then Hi < Uint_2**63);
10768 if Hi < Uint_2**08 then
10769 Sz := Standard_Character_Size; -- May be > 8 on some targets
10771 elsif Hi < Uint_2**16 then
10774 elsif Hi < Uint_2**32 then
10777 else pragma Assert (Hi < Uint_2**63);
10782 -- That minimum is the proper size unless we have a foreign convention
10783 -- and the size required is 32 or less, in which case we bump the size
10784 -- up to 32. This is required for C and C++ and seems reasonable for
10785 -- all other foreign conventions.
10787 if Has_Foreign_Convention (T)
10788 and then Esize (T) < Standard_Integer_Size
10790 Init_Esize (T, Standard_Integer_Size);
10792 Init_Esize (T, Sz);
10794 end Set_Enum_Esize;
10796 ------------------------------
10797 -- Validate_Address_Clauses --
10798 ------------------------------
10800 procedure Validate_Address_Clauses is
10802 for J in Address_Clause_Checks.First .. Address_Clause_Checks.Last loop
10804 ACCR : Address_Clause_Check_Record
10805 renames Address_Clause_Checks.Table (J);
10809 X_Alignment : Uint;
10810 Y_Alignment : Uint;
10816 -- Skip processing of this entry if warning already posted
10818 if not Address_Warning_Posted (ACCR.N) then
10819 Expr := Original_Node (Expression (ACCR.N));
10823 X_Alignment := Alignment (ACCR.X);
10824 Y_Alignment := Alignment (ACCR.Y);
10826 -- Similarly obtain sizes
10828 X_Size := Esize (ACCR.X);
10829 Y_Size := Esize (ACCR.Y);
10831 -- Check for large object overlaying smaller one
10834 and then X_Size > Uint_0
10835 and then X_Size > Y_Size
10838 ("?& overlays smaller object", ACCR.N, ACCR.X);
10840 ("\??program execution may be erroneous", ACCR.N);
10841 Error_Msg_Uint_1 := X_Size;
10843 ("\??size of & is ^", ACCR.N, ACCR.X);
10844 Error_Msg_Uint_1 := Y_Size;
10846 ("\??size of & is ^", ACCR.N, ACCR.Y);
10848 -- Check for inadequate alignment, both of the base object
10849 -- and of the offset, if any.
10851 -- Note: we do not check the alignment if we gave a size
10852 -- warning, since it would likely be redundant.
10854 elsif Y_Alignment /= Uint_0
10855 and then (Y_Alignment < X_Alignment
10858 Nkind (Expr) = N_Attribute_Reference
10860 Attribute_Name (Expr) = Name_Address
10862 Has_Compatible_Alignment
10863 (ACCR.X, Prefix (Expr))
10864 /= Known_Compatible))
10867 ("??specified address for& may be inconsistent "
10868 & "with alignment", ACCR.N, ACCR.X);
10870 ("\??program execution may be erroneous (RM 13.3(27))",
10872 Error_Msg_Uint_1 := X_Alignment;
10874 ("\??alignment of & is ^", ACCR.N, ACCR.X);
10875 Error_Msg_Uint_1 := Y_Alignment;
10877 ("\??alignment of & is ^", ACCR.N, ACCR.Y);
10878 if Y_Alignment >= X_Alignment then
10880 ("\??but offset is not multiple of alignment", ACCR.N);
10886 end Validate_Address_Clauses;
10888 ---------------------------
10889 -- Validate_Independence --
10890 ---------------------------
10892 procedure Validate_Independence is
10893 SU : constant Uint := UI_From_Int (System_Storage_Unit);
10901 procedure Check_Array_Type (Atyp : Entity_Id);
10902 -- Checks if the array type Atyp has independent components, and
10903 -- if not, outputs an appropriate set of error messages.
10905 procedure No_Independence;
10906 -- Output message that independence cannot be guaranteed
10908 function OK_Component (C : Entity_Id) return Boolean;
10909 -- Checks one component to see if it is independently accessible, and
10910 -- if so yields True, otherwise yields False if independent access
10911 -- cannot be guaranteed. This is a conservative routine, it only
10912 -- returns True if it knows for sure, it returns False if it knows
10913 -- there is a problem, or it cannot be sure there is no problem.
10915 procedure Reason_Bad_Component (C : Entity_Id);
10916 -- Outputs continuation message if a reason can be determined for
10917 -- the component C being bad.
10919 ----------------------
10920 -- Check_Array_Type --
10921 ----------------------
10923 procedure Check_Array_Type (Atyp : Entity_Id) is
10924 Ctyp : constant Entity_Id := Component_Type (Atyp);
10927 -- OK if no alignment clause, no pack, and no component size
10929 if not Has_Component_Size_Clause (Atyp)
10930 and then not Has_Alignment_Clause (Atyp)
10931 and then not Is_Packed (Atyp)
10936 -- Check actual component size
10938 if not Known_Component_Size (Atyp)
10939 or else not (Addressable (Component_Size (Atyp))
10940 and then Component_Size (Atyp) < 64)
10941 or else Component_Size (Atyp) mod Esize (Ctyp) /= 0
10945 -- Bad component size, check reason
10947 if Has_Component_Size_Clause (Atyp) then
10948 P := Get_Attribute_Definition_Clause
10949 (Atyp, Attribute_Component_Size);
10951 if Present (P) then
10952 Error_Msg_Sloc := Sloc (P);
10953 Error_Msg_N ("\because of Component_Size clause#", N);
10958 if Is_Packed (Atyp) then
10959 P := Get_Rep_Pragma (Atyp, Name_Pack);
10961 if Present (P) then
10962 Error_Msg_Sloc := Sloc (P);
10963 Error_Msg_N ("\because of pragma Pack#", N);
10968 -- No reason found, just return
10973 -- Array type is OK independence-wise
10976 end Check_Array_Type;
10978 ---------------------
10979 -- No_Independence --
10980 ---------------------
10982 procedure No_Independence is
10984 if Pragma_Name (N) = Name_Independent then
10985 Error_Msg_NE ("independence cannot be guaranteed for&", N, E);
10988 ("independent components cannot be guaranteed for&", N, E);
10990 end No_Independence;
10996 function OK_Component (C : Entity_Id) return Boolean is
10997 Rec : constant Entity_Id := Scope (C);
10998 Ctyp : constant Entity_Id := Etype (C);
11001 -- OK if no component clause, no Pack, and no alignment clause
11003 if No (Component_Clause (C))
11004 and then not Is_Packed (Rec)
11005 and then not Has_Alignment_Clause (Rec)
11010 -- Here we look at the actual component layout. A component is
11011 -- addressable if its size is a multiple of the Esize of the
11012 -- component type, and its starting position in the record has
11013 -- appropriate alignment, and the record itself has appropriate
11014 -- alignment to guarantee the component alignment.
11016 -- Make sure sizes are static, always assume the worst for any
11017 -- cases where we cannot check static values.
11019 if not (Known_Static_Esize (C)
11021 Known_Static_Esize (Ctyp))
11026 -- Size of component must be addressable or greater than 64 bits
11027 -- and a multiple of bytes.
11029 if not Addressable (Esize (C)) and then Esize (C) < Uint_64 then
11033 -- Check size is proper multiple
11035 if Esize (C) mod Esize (Ctyp) /= 0 then
11039 -- Check alignment of component is OK
11041 if not Known_Component_Bit_Offset (C)
11042 or else Component_Bit_Offset (C) < Uint_0
11043 or else Component_Bit_Offset (C) mod Esize (Ctyp) /= 0
11048 -- Check alignment of record type is OK
11050 if not Known_Alignment (Rec)
11051 or else (Alignment (Rec) * SU) mod Esize (Ctyp) /= 0
11056 -- All tests passed, component is addressable
11061 --------------------------
11062 -- Reason_Bad_Component --
11063 --------------------------
11065 procedure Reason_Bad_Component (C : Entity_Id) is
11066 Rec : constant Entity_Id := Scope (C);
11067 Ctyp : constant Entity_Id := Etype (C);
11070 -- If component clause present assume that's the problem
11072 if Present (Component_Clause (C)) then
11073 Error_Msg_Sloc := Sloc (Component_Clause (C));
11074 Error_Msg_N ("\because of Component_Clause#", N);
11078 -- If pragma Pack clause present, assume that's the problem
11080 if Is_Packed (Rec) then
11081 P := Get_Rep_Pragma (Rec, Name_Pack);
11083 if Present (P) then
11084 Error_Msg_Sloc := Sloc (P);
11085 Error_Msg_N ("\because of pragma Pack#", N);
11090 -- See if record has bad alignment clause
11092 if Has_Alignment_Clause (Rec)
11093 and then Known_Alignment (Rec)
11094 and then (Alignment (Rec) * SU) mod Esize (Ctyp) /= 0
11096 P := Get_Attribute_Definition_Clause (Rec, Attribute_Alignment);
11098 if Present (P) then
11099 Error_Msg_Sloc := Sloc (P);
11100 Error_Msg_N ("\because of Alignment clause#", N);
11104 -- Couldn't find a reason, so return without a message
11107 end Reason_Bad_Component;
11109 -- Start of processing for Validate_Independence
11112 for J in Independence_Checks.First .. Independence_Checks.Last loop
11113 N := Independence_Checks.Table (J).N;
11114 E := Independence_Checks.Table (J).E;
11115 IC := Pragma_Name (N) = Name_Independent_Components;
11117 -- Deal with component case
11119 if Ekind (E) = E_Discriminant or else Ekind (E) = E_Component then
11120 if not OK_Component (E) then
11122 Reason_Bad_Component (E);
11127 -- Deal with record with Independent_Components
11129 if IC and then Is_Record_Type (E) then
11130 Comp := First_Component_Or_Discriminant (E);
11131 while Present (Comp) loop
11132 if not OK_Component (Comp) then
11134 Reason_Bad_Component (Comp);
11138 Next_Component_Or_Discriminant (Comp);
11142 -- Deal with address clause case
11144 if Is_Object (E) then
11145 Addr := Address_Clause (E);
11147 if Present (Addr) then
11149 Error_Msg_Sloc := Sloc (Addr);
11150 Error_Msg_N ("\because of Address clause#", N);
11155 -- Deal with independent components for array type
11157 if IC and then Is_Array_Type (E) then
11158 Check_Array_Type (E);
11161 -- Deal with independent components for array object
11163 if IC and then Is_Object (E) and then Is_Array_Type (Etype (E)) then
11164 Check_Array_Type (Etype (E));
11169 end Validate_Independence;
11171 -----------------------------------
11172 -- Validate_Unchecked_Conversion --
11173 -----------------------------------
11175 procedure Validate_Unchecked_Conversion
11177 Act_Unit : Entity_Id)
11179 Source : Entity_Id;
11180 Target : Entity_Id;
11184 -- Obtain source and target types. Note that we call Ancestor_Subtype
11185 -- here because the processing for generic instantiation always makes
11186 -- subtypes, and we want the original frozen actual types.
11188 -- If we are dealing with private types, then do the check on their
11189 -- fully declared counterparts if the full declarations have been
11190 -- encountered (they don't have to be visible, but they must exist!)
11192 Source := Ancestor_Subtype (Etype (First_Formal (Act_Unit)));
11194 if Is_Private_Type (Source)
11195 and then Present (Underlying_Type (Source))
11197 Source := Underlying_Type (Source);
11200 Target := Ancestor_Subtype (Etype (Act_Unit));
11202 -- If either type is generic, the instantiation happens within a generic
11203 -- unit, and there is nothing to check. The proper check will happen
11204 -- when the enclosing generic is instantiated.
11206 if Is_Generic_Type (Source) or else Is_Generic_Type (Target) then
11210 if Is_Private_Type (Target)
11211 and then Present (Underlying_Type (Target))
11213 Target := Underlying_Type (Target);
11216 -- Source may be unconstrained array, but not target
11218 if Is_Array_Type (Target) and then not Is_Constrained (Target) then
11220 ("unchecked conversion to unconstrained array not allowed", N);
11224 -- Warn if conversion between two different convention pointers
11226 if Is_Access_Type (Target)
11227 and then Is_Access_Type (Source)
11228 and then Convention (Target) /= Convention (Source)
11229 and then Warn_On_Unchecked_Conversion
11231 -- Give warnings for subprogram pointers only on most targets. The
11232 -- exception is VMS, where data pointers can have different lengths
11233 -- depending on the pointer convention.
11235 if Is_Access_Subprogram_Type (Target)
11236 or else Is_Access_Subprogram_Type (Source)
11237 or else OpenVMS_On_Target
11240 ("?z?conversion between pointers with different conventions!",
11245 -- Warn if one of the operands is Ada.Calendar.Time. Do not emit a
11246 -- warning when compiling GNAT-related sources.
11248 if Warn_On_Unchecked_Conversion
11249 and then not In_Predefined_Unit (N)
11250 and then RTU_Loaded (Ada_Calendar)
11252 (Chars (Source) = Name_Time
11254 Chars (Target) = Name_Time)
11256 -- If Ada.Calendar is loaded and the name of one of the operands is
11257 -- Time, there is a good chance that this is Ada.Calendar.Time.
11260 Calendar_Time : constant Entity_Id :=
11261 Full_View (RTE (RO_CA_Time));
11263 pragma Assert (Present (Calendar_Time));
11265 if Source = Calendar_Time or else Target = Calendar_Time then
11267 ("?z?representation of 'Time values may change between " &
11268 "'G'N'A'T versions", N);
11273 -- Make entry in unchecked conversion table for later processing by
11274 -- Validate_Unchecked_Conversions, which will check sizes and alignments
11275 -- (using values set by the back-end where possible). This is only done
11276 -- if the appropriate warning is active.
11278 if Warn_On_Unchecked_Conversion then
11279 Unchecked_Conversions.Append
11280 (New_Val => UC_Entry'(Eloc => Sloc (N),
11282 Target => Target));
11284 -- If both sizes are known statically now, then back end annotation
11285 -- is not required to do a proper check but if either size is not
11286 -- known statically, then we need the annotation.
11288 if Known_Static_RM_Size (Source)
11290 Known_Static_RM_Size (Target)
11294 Back_Annotate_Rep_Info := True;
11298 -- If unchecked conversion to access type, and access type is declared
11299 -- in the same unit as the unchecked conversion, then set the flag
11300 -- No_Strict_Aliasing (no strict aliasing is implicit here)
11302 if Is_Access_Type (Target) and then
11303 In_Same_Source_Unit (Target, N)
11305 Set_No_Strict_Aliasing (Implementation_Base_Type (Target));
11308 -- Generate N_Validate_Unchecked_Conversion node for back end in case
11309 -- the back end needs to perform special validation checks.
11311 -- Shouldn't this be in Exp_Ch13, since the check only gets done if we
11312 -- have full expansion and the back end is called ???
11315 Make_Validate_Unchecked_Conversion (Sloc (N));
11316 Set_Source_Type (Vnode, Source);
11317 Set_Target_Type (Vnode, Target);
11319 -- If the unchecked conversion node is in a list, just insert before it.
11320 -- If not we have some strange case, not worth bothering about.
11322 if Is_List_Member (N) then
11323 Insert_After (N, Vnode);
11325 end Validate_Unchecked_Conversion;
11327 ------------------------------------
11328 -- Validate_Unchecked_Conversions --
11329 ------------------------------------
11331 procedure Validate_Unchecked_Conversions is
11333 for N in Unchecked_Conversions.First .. Unchecked_Conversions.Last loop
11335 T : UC_Entry renames Unchecked_Conversions.Table (N);
11337 Eloc : constant Source_Ptr := T.Eloc;
11338 Source : constant Entity_Id := T.Source;
11339 Target : constant Entity_Id := T.Target;
11345 -- This validation check, which warns if we have unequal sizes for
11346 -- unchecked conversion, and thus potentially implementation
11347 -- dependent semantics, is one of the few occasions on which we
11348 -- use the official RM size instead of Esize. See description in
11349 -- Einfo "Handling of Type'Size Values" for details.
11351 if Serious_Errors_Detected = 0
11352 and then Known_Static_RM_Size (Source)
11353 and then Known_Static_RM_Size (Target)
11355 -- Don't do the check if warnings off for either type, note the
11356 -- deliberate use of OR here instead of OR ELSE to get the flag
11357 -- Warnings_Off_Used set for both types if appropriate.
11359 and then not (Has_Warnings_Off (Source)
11361 Has_Warnings_Off (Target))
11363 Source_Siz := RM_Size (Source);
11364 Target_Siz := RM_Size (Target);
11366 if Source_Siz /= Target_Siz then
11368 ("?z?types for unchecked conversion have different sizes!",
11371 if All_Errors_Mode then
11372 Error_Msg_Name_1 := Chars (Source);
11373 Error_Msg_Uint_1 := Source_Siz;
11374 Error_Msg_Name_2 := Chars (Target);
11375 Error_Msg_Uint_2 := Target_Siz;
11376 Error_Msg ("\size of % is ^, size of % is ^?z?", Eloc);
11378 Error_Msg_Uint_1 := UI_Abs (Source_Siz - Target_Siz);
11380 if Is_Discrete_Type (Source)
11382 Is_Discrete_Type (Target)
11384 if Source_Siz > Target_Siz then
11386 ("\?z?^ high order bits of source will "
11387 & "be ignored!", Eloc);
11389 elsif Is_Unsigned_Type (Source) then
11391 ("\?z?source will be extended with ^ high order "
11392 & "zero bits?!", Eloc);
11396 ("\?z?source will be extended with ^ high order "
11397 & "sign bits!", Eloc);
11400 elsif Source_Siz < Target_Siz then
11401 if Is_Discrete_Type (Target) then
11402 if Bytes_Big_Endian then
11404 ("\?z?target value will include ^ undefined "
11405 & "low order bits!", Eloc);
11408 ("\?z?target value will include ^ undefined "
11409 & "high order bits!", Eloc);
11414 ("\?z?^ trailing bits of target value will be "
11415 & "undefined!", Eloc);
11418 else pragma Assert (Source_Siz > Target_Siz);
11420 ("\?z?^ trailing bits of source will be ignored!",
11427 -- If both types are access types, we need to check the alignment.
11428 -- If the alignment of both is specified, we can do it here.
11430 if Serious_Errors_Detected = 0
11431 and then Ekind (Source) in Access_Kind
11432 and then Ekind (Target) in Access_Kind
11433 and then Target_Strict_Alignment
11434 and then Present (Designated_Type (Source))
11435 and then Present (Designated_Type (Target))
11438 D_Source : constant Entity_Id := Designated_Type (Source);
11439 D_Target : constant Entity_Id := Designated_Type (Target);
11442 if Known_Alignment (D_Source)
11444 Known_Alignment (D_Target)
11447 Source_Align : constant Uint := Alignment (D_Source);
11448 Target_Align : constant Uint := Alignment (D_Target);
11451 if Source_Align < Target_Align
11452 and then not Is_Tagged_Type (D_Source)
11454 -- Suppress warning if warnings suppressed on either
11455 -- type or either designated type. Note the use of
11456 -- OR here instead of OR ELSE. That is intentional,
11457 -- we would like to set flag Warnings_Off_Used in
11458 -- all types for which warnings are suppressed.
11460 and then not (Has_Warnings_Off (D_Source)
11462 Has_Warnings_Off (D_Target)
11464 Has_Warnings_Off (Source)
11466 Has_Warnings_Off (Target))
11468 Error_Msg_Uint_1 := Target_Align;
11469 Error_Msg_Uint_2 := Source_Align;
11470 Error_Msg_Node_1 := D_Target;
11471 Error_Msg_Node_2 := D_Source;
11473 ("?z?alignment of & (^) is stricter than "
11474 & "alignment of & (^)!", Eloc);
11476 ("\?z?resulting access value may have invalid "
11477 & "alignment!", Eloc);
11485 end Validate_Unchecked_Conversions;