1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2012, Free Software Foundation, Inc. --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING3. If not, go to --
19 -- http://www.gnu.org/licenses for a complete copy of the license. --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
24 ------------------------------------------------------------------------------
26 with Aspects; use Aspects;
27 with Atree; use Atree;
28 with Checks; use Checks;
29 with Einfo; use Einfo;
30 with Elists; use Elists;
31 with Errout; use Errout;
32 with Exp_Disp; use Exp_Disp;
33 with Exp_Tss; use Exp_Tss;
34 with Exp_Util; use Exp_Util;
36 with Lib.Xref; use Lib.Xref;
37 with Namet; use Namet;
38 with Nlists; use Nlists;
39 with Nmake; use Nmake;
41 with Restrict; use Restrict;
42 with Rident; use Rident;
43 with Rtsfind; use Rtsfind;
45 with Sem_Aux; use Sem_Aux;
46 with Sem_Ch3; use Sem_Ch3;
47 with Sem_Ch6; use Sem_Ch6;
48 with Sem_Ch8; use Sem_Ch8;
49 with Sem_Ch9; use Sem_Ch9;
50 with Sem_Dim; use Sem_Dim;
51 with Sem_Disp; use Sem_Disp;
52 with Sem_Eval; use Sem_Eval;
53 with Sem_Res; use Sem_Res;
54 with Sem_Type; use Sem_Type;
55 with Sem_Util; use Sem_Util;
56 with Sem_Warn; use Sem_Warn;
57 with Sinput; use Sinput;
58 with Snames; use Snames;
59 with Stand; use Stand;
60 with Sinfo; use Sinfo;
61 with Stringt; use Stringt;
62 with Targparm; use Targparm;
63 with Ttypes; use Ttypes;
64 with Tbuild; use Tbuild;
65 with Urealp; use Urealp;
66 with Warnsw; use Warnsw;
68 with GNAT.Heap_Sort_G;
70 package body Sem_Ch13 is
72 SSU : constant Pos := System_Storage_Unit;
73 -- Convenient short hand for commonly used constant
75 -----------------------
76 -- Local Subprograms --
77 -----------------------
79 procedure Alignment_Check_For_Size_Change (Typ : Entity_Id; Size : Uint);
80 -- This routine is called after setting one of the sizes of type entity
81 -- Typ to Size. The purpose is to deal with the situation of a derived
82 -- type whose inherited alignment is no longer appropriate for the new
83 -- size value. In this case, we reset the Alignment to unknown.
85 procedure Build_Predicate_Function (Typ : Entity_Id; N : Node_Id);
86 -- If Typ has predicates (indicated by Has_Predicates being set for Typ,
87 -- then either there are pragma Invariant entries on the rep chain for the
88 -- type (note that Predicate aspects are converted to pragma Predicate), or
89 -- there are inherited aspects from a parent type, or ancestor subtypes.
90 -- This procedure builds the spec and body for the Predicate function that
91 -- tests these predicates. N is the freeze node for the type. The spec of
92 -- the function is inserted before the freeze node, and the body of the
93 -- function is inserted after the freeze node.
95 procedure Build_Static_Predicate
99 -- Given a predicated type Typ, where Typ is a discrete static subtype,
100 -- whose predicate expression is Expr, tests if Expr is a static predicate,
101 -- and if so, builds the predicate range list. Nam is the name of the one
102 -- argument to the predicate function. Occurrences of the type name in the
103 -- predicate expression have been replaced by identifier references to this
104 -- name, which is unique, so any identifier with Chars matching Nam must be
105 -- a reference to the type. If the predicate is non-static, this procedure
106 -- returns doing nothing. If the predicate is static, then the predicate
107 -- list is stored in Static_Predicate (Typ), and the Expr is rewritten as
108 -- a canonicalized membership operation.
110 function Get_Alignment_Value (Expr : Node_Id) return Uint;
111 -- Given the expression for an alignment value, returns the corresponding
112 -- Uint value. If the value is inappropriate, then error messages are
113 -- posted as required, and a value of No_Uint is returned.
115 function Is_Operational_Item (N : Node_Id) return Boolean;
116 -- A specification for a stream attribute is allowed before the full type
117 -- is declared, as explained in AI-00137 and the corrigendum. Attributes
118 -- that do not specify a representation characteristic are operational
121 procedure New_Stream_Subprogram
125 Nam : TSS_Name_Type);
126 -- Create a subprogram renaming of a given stream attribute to the
127 -- designated subprogram and then in the tagged case, provide this as a
128 -- primitive operation, or in the non-tagged case make an appropriate TSS
129 -- entry. This is more properly an expansion activity than just semantics,
130 -- but the presence of user-defined stream functions for limited types is a
131 -- legality check, which is why this takes place here rather than in
132 -- exp_ch13, where it was previously. Nam indicates the name of the TSS
133 -- function to be generated.
135 -- To avoid elaboration anomalies with freeze nodes, for untagged types
136 -- we generate both a subprogram declaration and a subprogram renaming
137 -- declaration, so that the attribute specification is handled as a
138 -- renaming_as_body. For tagged types, the specification is one of the
142 with procedure Replace_Type_Reference (N : Node_Id);
143 procedure Replace_Type_References_Generic (N : Node_Id; TName : Name_Id);
144 -- This is used to scan an expression for a predicate or invariant aspect
145 -- replacing occurrences of the name TName (the name of the subtype to
146 -- which the aspect applies) with appropriate references to the parameter
147 -- of the predicate function or invariant procedure. The procedure passed
148 -- as a generic parameter does the actual replacement of node N, which is
149 -- either a simple direct reference to TName, or a selected component that
150 -- represents an appropriately qualified occurrence of TName.
156 Biased : Boolean := True);
157 -- If Biased is True, sets Has_Biased_Representation flag for E, and
158 -- outputs a warning message at node N if Warn_On_Biased_Representation is
159 -- is True. This warning inserts the string Msg to describe the construct
162 ----------------------------------------------
163 -- Table for Validate_Unchecked_Conversions --
164 ----------------------------------------------
166 -- The following table collects unchecked conversions for validation.
167 -- Entries are made by Validate_Unchecked_Conversion and then the call
168 -- to Validate_Unchecked_Conversions does the actual error checking and
169 -- posting of warnings. The reason for this delayed processing is to take
170 -- advantage of back-annotations of size and alignment values performed by
173 -- Note: the reason we store a Source_Ptr value instead of a Node_Id is
174 -- that by the time Validate_Unchecked_Conversions is called, Sprint will
175 -- already have modified all Sloc values if the -gnatD option is set.
177 type UC_Entry is record
178 Eloc : Source_Ptr; -- node used for posting warnings
179 Source : Entity_Id; -- source type for unchecked conversion
180 Target : Entity_Id; -- target type for unchecked conversion
183 package Unchecked_Conversions is new Table.Table (
184 Table_Component_Type => UC_Entry,
185 Table_Index_Type => Int,
186 Table_Low_Bound => 1,
188 Table_Increment => 200,
189 Table_Name => "Unchecked_Conversions");
191 ----------------------------------------
192 -- Table for Validate_Address_Clauses --
193 ----------------------------------------
195 -- If an address clause has the form
197 -- for X'Address use Expr
199 -- where Expr is of the form Y'Address or recursively is a reference to a
200 -- constant of either of these forms, and X and Y are entities of objects,
201 -- then if Y has a smaller alignment than X, that merits a warning about
202 -- possible bad alignment. The following table collects address clauses of
203 -- this kind. We put these in a table so that they can be checked after the
204 -- back end has completed annotation of the alignments of objects, since we
205 -- can catch more cases that way.
207 type Address_Clause_Check_Record is record
209 -- The address clause
212 -- The entity of the object overlaying Y
215 -- The entity of the object being overlaid
218 -- Whether the address is offset within Y
221 package Address_Clause_Checks is new Table.Table (
222 Table_Component_Type => Address_Clause_Check_Record,
223 Table_Index_Type => Int,
224 Table_Low_Bound => 1,
226 Table_Increment => 200,
227 Table_Name => "Address_Clause_Checks");
229 -----------------------------------------
230 -- Adjust_Record_For_Reverse_Bit_Order --
231 -----------------------------------------
233 procedure Adjust_Record_For_Reverse_Bit_Order (R : Entity_Id) is
238 -- Processing depends on version of Ada
240 -- For Ada 95, we just renumber bits within a storage unit. We do the
241 -- same for Ada 83 mode, since we recognize the Bit_Order attribute in
242 -- Ada 83, and are free to add this extension.
244 if Ada_Version < Ada_2005 then
245 Comp := First_Component_Or_Discriminant (R);
246 while Present (Comp) loop
247 CC := Component_Clause (Comp);
249 -- If component clause is present, then deal with the non-default
250 -- bit order case for Ada 95 mode.
252 -- We only do this processing for the base type, and in fact that
253 -- is important, since otherwise if there are record subtypes, we
254 -- could reverse the bits once for each subtype, which is wrong.
257 and then Ekind (R) = E_Record_Type
260 CFB : constant Uint := Component_Bit_Offset (Comp);
261 CSZ : constant Uint := Esize (Comp);
262 CLC : constant Node_Id := Component_Clause (Comp);
263 Pos : constant Node_Id := Position (CLC);
264 FB : constant Node_Id := First_Bit (CLC);
266 Storage_Unit_Offset : constant Uint :=
267 CFB / System_Storage_Unit;
269 Start_Bit : constant Uint :=
270 CFB mod System_Storage_Unit;
273 -- Cases where field goes over storage unit boundary
275 if Start_Bit + CSZ > System_Storage_Unit then
277 -- Allow multi-byte field but generate warning
279 if Start_Bit mod System_Storage_Unit = 0
280 and then CSZ mod System_Storage_Unit = 0
283 ("multi-byte field specified with non-standard"
284 & " Bit_Order?", CLC);
286 if Bytes_Big_Endian then
288 ("bytes are not reversed "
289 & "(component is big-endian)?", CLC);
292 ("bytes are not reversed "
293 & "(component is little-endian)?", CLC);
296 -- Do not allow non-contiguous field
300 ("attempt to specify non-contiguous field "
301 & "not permitted", CLC);
303 ("\caused by non-standard Bit_Order "
306 ("\consider possibility of using "
307 & "Ada 2005 mode here", CLC);
310 -- Case where field fits in one storage unit
313 -- Give warning if suspicious component clause
315 if Intval (FB) >= System_Storage_Unit
316 and then Warn_On_Reverse_Bit_Order
319 ("?Bit_Order clause does not affect " &
320 "byte ordering", Pos);
322 Intval (Pos) + Intval (FB) /
325 ("?position normalized to ^ before bit " &
326 "order interpreted", Pos);
329 -- Here is where we fix up the Component_Bit_Offset value
330 -- to account for the reverse bit order. Some examples of
331 -- what needs to be done are:
333 -- First_Bit .. Last_Bit Component_Bit_Offset
345 -- The rule is that the first bit is is obtained by
346 -- subtracting the old ending bit from storage_unit - 1.
348 Set_Component_Bit_Offset
350 (Storage_Unit_Offset * System_Storage_Unit) +
351 (System_Storage_Unit - 1) -
352 (Start_Bit + CSZ - 1));
354 Set_Normalized_First_Bit
356 Component_Bit_Offset (Comp) mod
357 System_Storage_Unit);
362 Next_Component_Or_Discriminant (Comp);
365 -- For Ada 2005, we do machine scalar processing, as fully described In
366 -- AI-133. This involves gathering all components which start at the
367 -- same byte offset and processing them together. Same approach is still
368 -- valid in later versions including Ada 2012.
372 Max_Machine_Scalar_Size : constant Uint :=
374 (Standard_Long_Long_Integer_Size);
375 -- We use this as the maximum machine scalar size
378 SSU : constant Uint := UI_From_Int (System_Storage_Unit);
381 -- This first loop through components does two things. First it
382 -- deals with the case of components with component clauses whose
383 -- length is greater than the maximum machine scalar size (either
384 -- accepting them or rejecting as needed). Second, it counts the
385 -- number of components with component clauses whose length does
386 -- not exceed this maximum for later processing.
389 Comp := First_Component_Or_Discriminant (R);
390 while Present (Comp) loop
391 CC := Component_Clause (Comp);
395 Fbit : constant Uint :=
396 Static_Integer (First_Bit (CC));
397 Lbit : constant Uint :=
398 Static_Integer (Last_Bit (CC));
401 -- Case of component with last bit >= max machine scalar
403 if Lbit >= Max_Machine_Scalar_Size then
405 -- This is allowed only if first bit is zero, and
406 -- last bit + 1 is a multiple of storage unit size.
408 if Fbit = 0 and then (Lbit + 1) mod SSU = 0 then
410 -- This is the case to give a warning if enabled
412 if Warn_On_Reverse_Bit_Order then
414 ("multi-byte field specified with "
415 & " non-standard Bit_Order?", CC);
417 if Bytes_Big_Endian then
419 ("\bytes are not reversed "
420 & "(component is big-endian)?", CC);
423 ("\bytes are not reversed "
424 & "(component is little-endian)?", CC);
428 -- Give error message for RM 13.5.1(10) violation
432 ("machine scalar rules not followed for&",
433 First_Bit (CC), Comp);
435 Error_Msg_Uint_1 := Lbit;
436 Error_Msg_Uint_2 := Max_Machine_Scalar_Size;
438 ("\last bit (^) exceeds maximum machine "
442 if (Lbit + 1) mod SSU /= 0 then
443 Error_Msg_Uint_1 := SSU;
445 ("\and is not a multiple of Storage_Unit (^) "
450 Error_Msg_Uint_1 := Fbit;
452 ("\and first bit (^) is non-zero "
458 -- OK case of machine scalar related component clause,
459 -- For now, just count them.
462 Num_CC := Num_CC + 1;
467 Next_Component_Or_Discriminant (Comp);
470 -- We need to sort the component clauses on the basis of the
471 -- Position values in the clause, so we can group clauses with
472 -- the same Position. together to determine the relevant machine
476 Comps : array (0 .. Num_CC) of Entity_Id;
477 -- Array to collect component and discriminant entities. The
478 -- data starts at index 1, the 0'th entry is for the sort
481 function CP_Lt (Op1, Op2 : Natural) return Boolean;
482 -- Compare routine for Sort
484 procedure CP_Move (From : Natural; To : Natural);
485 -- Move routine for Sort
487 package Sorting is new GNAT.Heap_Sort_G (CP_Move, CP_Lt);
491 -- Start and stop positions in the component list of the set of
492 -- components with the same starting position (that constitute
493 -- components in a single machine scalar).
496 -- Maximum last bit value of any component in this set
499 -- Corresponding machine scalar size
505 function CP_Lt (Op1, Op2 : Natural) return Boolean is
507 return Position (Component_Clause (Comps (Op1))) <
508 Position (Component_Clause (Comps (Op2)));
515 procedure CP_Move (From : Natural; To : Natural) is
517 Comps (To) := Comps (From);
520 -- Start of processing for Sort_CC
523 -- Collect the machine scalar relevant component clauses
526 Comp := First_Component_Or_Discriminant (R);
527 while Present (Comp) loop
529 CC : constant Node_Id := Component_Clause (Comp);
532 -- Collect only component clauses whose last bit is less
533 -- than machine scalar size. Any component clause whose
534 -- last bit exceeds this value does not take part in
535 -- machine scalar layout considerations. The test for
536 -- Error_Posted makes sure we exclude component clauses
537 -- for which we already posted an error.
540 and then not Error_Posted (Last_Bit (CC))
541 and then Static_Integer (Last_Bit (CC)) <
542 Max_Machine_Scalar_Size
544 Num_CC := Num_CC + 1;
545 Comps (Num_CC) := Comp;
549 Next_Component_Or_Discriminant (Comp);
552 -- Sort by ascending position number
554 Sorting.Sort (Num_CC);
556 -- We now have all the components whose size does not exceed
557 -- the max machine scalar value, sorted by starting position.
558 -- In this loop we gather groups of clauses starting at the
559 -- same position, to process them in accordance with AI-133.
562 while Stop < Num_CC loop
567 (Last_Bit (Component_Clause (Comps (Start))));
568 while Stop < Num_CC loop
570 (Position (Component_Clause (Comps (Stop + 1)))) =
572 (Position (Component_Clause (Comps (Stop))))
580 (Component_Clause (Comps (Stop)))));
586 -- Now we have a group of component clauses from Start to
587 -- Stop whose positions are identical, and MaxL is the
588 -- maximum last bit value of any of these components.
590 -- We need to determine the corresponding machine scalar
591 -- size. This loop assumes that machine scalar sizes are
592 -- even, and that each possible machine scalar has twice
593 -- as many bits as the next smaller one.
595 MSS := Max_Machine_Scalar_Size;
597 and then (MSS / 2) >= SSU
598 and then (MSS / 2) > MaxL
603 -- Here is where we fix up the Component_Bit_Offset value
604 -- to account for the reverse bit order. Some examples of
605 -- what needs to be done for the case of a machine scalar
608 -- First_Bit .. Last_Bit Component_Bit_Offset
620 -- The rule is that the first bit is obtained by subtracting
621 -- the old ending bit from machine scalar size - 1.
623 for C in Start .. Stop loop
625 Comp : constant Entity_Id := Comps (C);
626 CC : constant Node_Id :=
627 Component_Clause (Comp);
628 LB : constant Uint :=
629 Static_Integer (Last_Bit (CC));
630 NFB : constant Uint := MSS - Uint_1 - LB;
631 NLB : constant Uint := NFB + Esize (Comp) - 1;
632 Pos : constant Uint :=
633 Static_Integer (Position (CC));
636 if Warn_On_Reverse_Bit_Order then
637 Error_Msg_Uint_1 := MSS;
639 ("info: reverse bit order in machine " &
640 "scalar of length^?", First_Bit (CC));
641 Error_Msg_Uint_1 := NFB;
642 Error_Msg_Uint_2 := NLB;
644 if Bytes_Big_Endian then
646 ("?\info: big-endian range for "
647 & "component & is ^ .. ^",
648 First_Bit (CC), Comp);
651 ("?\info: little-endian range "
652 & "for component & is ^ .. ^",
653 First_Bit (CC), Comp);
657 Set_Component_Bit_Offset (Comp, Pos * SSU + NFB);
658 Set_Normalized_First_Bit (Comp, NFB mod SSU);
665 end Adjust_Record_For_Reverse_Bit_Order;
667 -------------------------------------
668 -- Alignment_Check_For_Size_Change --
669 -------------------------------------
671 procedure Alignment_Check_For_Size_Change (Typ : Entity_Id; Size : Uint) is
673 -- If the alignment is known, and not set by a rep clause, and is
674 -- inconsistent with the size being set, then reset it to unknown,
675 -- we assume in this case that the size overrides the inherited
676 -- alignment, and that the alignment must be recomputed.
678 if Known_Alignment (Typ)
679 and then not Has_Alignment_Clause (Typ)
680 and then Size mod (Alignment (Typ) * SSU) /= 0
682 Init_Alignment (Typ);
684 end Alignment_Check_For_Size_Change;
686 -------------------------------------
687 -- Analyze_Aspects_At_Freeze_Point --
688 -------------------------------------
690 procedure Analyze_Aspects_At_Freeze_Point (E : Entity_Id) is
695 procedure Analyze_Aspect_Default_Value (ASN : Node_Id);
696 -- This routine analyzes an Aspect_Default_[Component_]Value denoted by
697 -- the aspect specification node ASN.
699 procedure Make_Pragma_From_Boolean_Aspect (ASN : Node_Id);
700 -- Given an aspect specification node ASN whose expression is an
701 -- optional Boolean, this routines creates the corresponding pragma
702 -- at the freezing point.
704 ----------------------------------
705 -- Analyze_Aspect_Default_Value --
706 ----------------------------------
708 procedure Analyze_Aspect_Default_Value (ASN : Node_Id) is
709 Ent : constant Entity_Id := Entity (ASN);
710 Expr : constant Node_Id := Expression (ASN);
711 Id : constant Node_Id := Identifier (ASN);
714 Error_Msg_Name_1 := Chars (Id);
716 if not Is_Type (Ent) then
717 Error_Msg_N ("aspect% can only apply to a type", Id);
720 elsif not Is_First_Subtype (Ent) then
721 Error_Msg_N ("aspect% cannot apply to subtype", Id);
724 elsif A_Id = Aspect_Default_Value
725 and then not Is_Scalar_Type (Ent)
727 Error_Msg_N ("aspect% can only be applied to scalar type", Id);
730 elsif A_Id = Aspect_Default_Component_Value then
731 if not Is_Array_Type (Ent) then
732 Error_Msg_N ("aspect% can only be applied to array type", Id);
735 elsif not Is_Scalar_Type (Component_Type (Ent)) then
736 Error_Msg_N ("aspect% requires scalar components", Id);
741 Set_Has_Default_Aspect (Base_Type (Ent));
743 if Is_Scalar_Type (Ent) then
744 Set_Default_Aspect_Value (Ent, Expr);
746 Set_Default_Aspect_Component_Value (Ent, Expr);
748 end Analyze_Aspect_Default_Value;
750 -------------------------------------
751 -- Make_Pragma_From_Boolean_Aspect --
752 -------------------------------------
754 procedure Make_Pragma_From_Boolean_Aspect (ASN : Node_Id) is
755 Ident : constant Node_Id := Identifier (ASN);
756 A_Name : constant Name_Id := Chars (Ident);
757 A_Id : constant Aspect_Id := Get_Aspect_Id (A_Name);
758 Ent : constant Entity_Id := Entity (ASN);
759 Expr : constant Node_Id := Expression (ASN);
760 Loc : constant Source_Ptr := Sloc (ASN);
764 procedure Check_False_Aspect_For_Derived_Type;
765 -- This procedure checks for the case of a false aspect for a derived
766 -- type, which improperly tries to cancel an aspect inherited from
769 -----------------------------------------
770 -- Check_False_Aspect_For_Derived_Type --
771 -----------------------------------------
773 procedure Check_False_Aspect_For_Derived_Type is
777 -- We are only checking derived types
779 if not Is_Derived_Type (E) then
783 Par := Nearest_Ancestor (E);
786 when Aspect_Atomic | Aspect_Shared =>
787 if not Is_Atomic (Par) then
791 when Aspect_Atomic_Components =>
792 if not Has_Atomic_Components (Par) then
796 when Aspect_Discard_Names =>
797 if not Discard_Names (Par) then
802 if not Is_Packed (Par) then
806 when Aspect_Unchecked_Union =>
807 if not Is_Unchecked_Union (Par) then
811 when Aspect_Volatile =>
812 if not Is_Volatile (Par) then
816 when Aspect_Volatile_Components =>
817 if not Has_Volatile_Components (Par) then
825 -- Fall through means we are canceling an inherited aspect
827 Error_Msg_Name_1 := A_Name;
828 Error_Msg_NE ("derived type& inherits aspect%, cannot cancel",
832 end Check_False_Aspect_For_Derived_Type;
834 -- Start of processing for Make_Pragma_From_Boolean_Aspect
837 if Is_False (Static_Boolean (Expr)) then
838 Check_False_Aspect_For_Derived_Type;
843 Pragma_Argument_Associations => New_List (
844 New_Occurrence_Of (Ent, Sloc (Ident))),
846 Make_Identifier (Sloc (Ident), Chars (Ident)));
848 Set_From_Aspect_Specification (Prag, True);
849 Set_Corresponding_Aspect (Prag, ASN);
850 Set_Aspect_Rep_Item (ASN, Prag);
851 Set_Is_Delayed_Aspect (Prag);
852 Set_Parent (Prag, ASN);
854 end Make_Pragma_From_Boolean_Aspect;
856 -- Start of processing for Analyze_Aspects_At_Freeze_Point
859 -- Must be visible in current scope
861 if not Scope_Within_Or_Same (Current_Scope, Scope (E)) then
865 -- Look for aspect specification entries for this entity
867 ASN := First_Rep_Item (E);
868 while Present (ASN) loop
869 if Nkind (ASN) = N_Aspect_Specification
870 and then Entity (ASN) = E
871 and then Is_Delayed_Aspect (ASN)
873 A_Id := Get_Aspect_Id (Chars (Identifier (ASN)));
877 -- For aspects whose expression is an optional Boolean, make
878 -- the corresponding pragma at the freezing point.
880 when Boolean_Aspects |
881 Library_Unit_Aspects =>
882 Make_Pragma_From_Boolean_Aspect (ASN);
884 -- Special handling for aspects that don't correspond to
885 -- pragmas/attributes.
887 when Aspect_Default_Value |
888 Aspect_Default_Component_Value =>
889 Analyze_Aspect_Default_Value (ASN);
895 Ritem := Aspect_Rep_Item (ASN);
897 if Present (Ritem) then
904 end Analyze_Aspects_At_Freeze_Point;
906 -----------------------------------
907 -- Analyze_Aspect_Specifications --
908 -----------------------------------
910 procedure Analyze_Aspect_Specifications (N : Node_Id; E : Entity_Id) is
915 L : constant List_Id := Aspect_Specifications (N);
917 Ins_Node : Node_Id := N;
918 -- Insert pragmas/attribute definition clause after this node when no
919 -- delayed analysis is required.
921 -- The general processing involves building an attribute definition
922 -- clause or a pragma node that corresponds to the aspect. Then in order
923 -- to delay the evaluation of this aspect to the freeze point, we attach
924 -- the corresponding pragma/attribute definition clause to the aspect
925 -- specification node, which is then placed in the Rep Item chain. In
926 -- this case we mark the entity by setting the flag Has_Delayed_Aspects
927 -- and we evaluate the rep item at the freeze point. When the aspect
928 -- doesn't have a corresponding pragma/attribute definition clause, then
929 -- its analysis is simply delayed at the freeze point.
931 -- Some special cases don't require delay analysis, thus the aspect is
932 -- analyzed right now.
934 -- Note that there is a special handling for
935 -- Pre/Post/Test_Case/Contract_Case aspects. In this case, we do not
936 -- have to worry about delay issues, since the pragmas themselves deal
937 -- with delay of visibility for the expression analysis. Thus, we just
938 -- insert the pragma after the node N.
941 pragma Assert (Present (L));
943 -- Loop through aspects
946 Aspect_Loop : while Present (Aspect) loop
948 Expr : constant Node_Id := Expression (Aspect);
949 Id : constant Node_Id := Identifier (Aspect);
950 Loc : constant Source_Ptr := Sloc (Aspect);
951 Nam : constant Name_Id := Chars (Id);
952 A_Id : constant Aspect_Id := Get_Aspect_Id (Nam);
955 Delay_Required : Boolean := True;
956 -- Set False if delay is not required
958 Eloc : Source_Ptr := No_Location;
959 -- Source location of expression, modified when we split PPC's. It
960 -- is set below when Expr is present.
962 procedure Analyze_Aspect_External_Or_Link_Name;
963 -- This routine performs the analysis of the External_Name or
964 -- Link_Name aspects.
966 procedure Analyze_Aspect_Implicit_Dereference;
967 -- This routine performs the analysis of the Implicit_Dereference
970 ------------------------------------------
971 -- Analyze_Aspect_External_Or_Link_Name --
972 ------------------------------------------
974 procedure Analyze_Aspect_External_Or_Link_Name is
976 -- Verify that there is an Import/Export aspect defined for the
977 -- entity. The processing of that aspect in turn checks that
978 -- there is a Convention aspect declared. The pragma is
979 -- constructed when processing the Convention aspect.
986 while Present (A) loop
987 exit when Chars (Identifier (A)) = Name_Export
988 or else Chars (Identifier (A)) = Name_Import;
994 ("Missing Import/Export for Link/External name",
998 end Analyze_Aspect_External_Or_Link_Name;
1000 -----------------------------------------
1001 -- Analyze_Aspect_Implicit_Dereference --
1002 -----------------------------------------
1004 procedure Analyze_Aspect_Implicit_Dereference is
1007 or else not Has_Discriminants (E)
1010 ("Aspect must apply to a type with discriminants", N);
1017 Disc := First_Discriminant (E);
1018 while Present (Disc) loop
1019 if Chars (Expr) = Chars (Disc)
1020 and then Ekind (Etype (Disc)) =
1021 E_Anonymous_Access_Type
1023 Set_Has_Implicit_Dereference (E);
1024 Set_Has_Implicit_Dereference (Disc);
1028 Next_Discriminant (Disc);
1031 -- Error if no proper access discriminant.
1034 ("not an access discriminant of&", Expr, E);
1037 end Analyze_Aspect_Implicit_Dereference;
1040 -- Skip aspect if already analyzed (not clear if this is needed)
1042 if Analyzed (Aspect) then
1046 -- Set the source location of expression, used in the case of
1047 -- a failed precondition/postcondition or invariant. Note that
1048 -- the source location of the expression is not usually the best
1049 -- choice here. For example, it gets located on the last AND
1050 -- keyword in a chain of boolean expressiond AND'ed together.
1051 -- It is best to put the message on the first character of the
1052 -- assertion, which is the effect of the First_Node call here.
1054 if Present (Expr) then
1055 Eloc := Sloc (First_Node (Expr));
1058 -- Check restriction No_Implementation_Aspect_Specifications
1060 if Impl_Defined_Aspects (A_Id) then
1062 (No_Implementation_Aspect_Specifications, Aspect);
1065 -- Check restriction No_Specification_Of_Aspect
1067 Check_Restriction_No_Specification_Of_Aspect (Aspect);
1069 -- Analyze this aspect
1071 Set_Analyzed (Aspect);
1072 Set_Entity (Aspect, E);
1073 Ent := New_Occurrence_Of (E, Sloc (Id));
1075 -- Check for duplicate aspect. Note that the Comes_From_Source
1076 -- test allows duplicate Pre/Post's that we generate internally
1077 -- to escape being flagged here.
1079 if No_Duplicates_Allowed (A_Id) then
1081 while Anod /= Aspect loop
1083 (A_Id, Get_Aspect_Id (Chars (Identifier (Anod))))
1084 and then Comes_From_Source (Aspect)
1086 Error_Msg_Name_1 := Nam;
1087 Error_Msg_Sloc := Sloc (Anod);
1089 -- Case of same aspect specified twice
1091 if Class_Present (Anod) = Class_Present (Aspect) then
1092 if not Class_Present (Anod) then
1094 ("aspect% for & previously given#",
1098 ("aspect `%''Class` for & previously given#",
1108 -- Check some general restrictions on language defined aspects
1110 if not Impl_Defined_Aspects (A_Id) then
1111 Error_Msg_Name_1 := Nam;
1113 -- Not allowed for renaming declarations
1115 if Nkind (N) in N_Renaming_Declaration then
1117 ("aspect % not allowed for renaming declaration",
1121 -- Not allowed for formal type declarations
1123 if Nkind (N) = N_Formal_Type_Declaration then
1125 ("aspect % not allowed for formal type declaration",
1130 -- Copy expression for later processing by the procedures
1131 -- Check_Aspect_At_[Freeze_Point | End_Of_Declarations]
1133 Set_Entity (Id, New_Copy_Tree (Expr));
1135 -- Processing based on specific aspect
1139 -- No_Aspect should be impossible
1142 raise Program_Error;
1144 -- Case 1: Aspects corresponding to attribute definition
1147 when Aspect_Address |
1150 Aspect_Component_Size |
1151 Aspect_Constant_Indexing |
1152 Aspect_Default_Iterator |
1153 Aspect_Dispatching_Domain |
1154 Aspect_External_Tag |
1156 Aspect_Iterator_Element |
1157 Aspect_Machine_Radix |
1158 Aspect_Object_Size |
1161 Aspect_Scalar_Storage_Order |
1164 Aspect_Simple_Storage_Pool |
1165 Aspect_Storage_Pool |
1166 Aspect_Storage_Size |
1167 Aspect_Stream_Size |
1169 Aspect_Variable_Indexing |
1172 -- Indexing aspects apply only to tagged type
1174 if (A_Id = Aspect_Constant_Indexing
1175 or else A_Id = Aspect_Variable_Indexing)
1176 and then not (Is_Type (E)
1177 and then Is_Tagged_Type (E))
1179 Error_Msg_N ("indexing applies to a tagged type", N);
1183 -- Construct the attribute definition clause
1186 Make_Attribute_Definition_Clause (Loc,
1188 Chars => Chars (Id),
1189 Expression => Relocate_Node (Expr));
1191 -- Case 2: Aspects cooresponding to pragmas
1193 -- Case 2a: Aspects corresponding to pragmas with two
1194 -- arguments, where the first argument is a local name
1195 -- referring to the entity, and the second argument is the
1196 -- aspect definition expression.
1198 when Aspect_Suppress |
1199 Aspect_Unsuppress =>
1201 -- Construct the pragma
1205 Pragma_Argument_Associations => New_List (
1206 New_Occurrence_Of (E, Loc),
1207 Relocate_Node (Expr)),
1208 Pragma_Identifier =>
1209 Make_Identifier (Sloc (Id), Chars (Id)));
1211 when Aspect_Synchronization =>
1213 -- The aspect corresponds to pragma Implemented.
1214 -- Construct the pragma
1218 Pragma_Argument_Associations => New_List (
1219 New_Occurrence_Of (E, Loc),
1220 Relocate_Node (Expr)),
1221 Pragma_Identifier =>
1222 Make_Identifier (Sloc (Id), Name_Implemented));
1224 -- No delay is required since the only values are: By_Entry
1225 -- | By_Protected_Procedure | By_Any | Optional which don't
1226 -- get analyzed anyway.
1228 Delay_Required := False;
1230 when Aspect_Attach_Handler =>
1233 Pragma_Identifier =>
1234 Make_Identifier (Sloc (Id), Name_Attach_Handler),
1235 Pragma_Argument_Associations =>
1236 New_List (Ent, Relocate_Node (Expr)));
1238 when Aspect_Dynamic_Predicate |
1240 Aspect_Static_Predicate =>
1242 -- Construct the pragma (always a pragma Predicate, with
1243 -- flags recording whether it is static/dynamic).
1247 Pragma_Argument_Associations =>
1248 New_List (Ent, Relocate_Node (Expr)),
1249 Class_Present => Class_Present (Aspect),
1250 Pragma_Identifier =>
1251 Make_Identifier (Sloc (Id), Name_Predicate));
1253 -- If the type is private, indicate that its completion
1254 -- has a freeze node, because that is the one that will be
1255 -- visible at freeze time.
1257 Set_Has_Predicates (E);
1259 if Is_Private_Type (E)
1260 and then Present (Full_View (E))
1262 Set_Has_Predicates (Full_View (E));
1263 Set_Has_Delayed_Aspects (Full_View (E));
1264 Ensure_Freeze_Node (Full_View (E));
1267 -- Case 2b: Aspects corresponding to pragmas with two
1268 -- arguments, where the second argument is a local name
1269 -- referring to the entity, and the first argument is the
1270 -- aspect definition expression.
1272 when Aspect_Convention =>
1274 -- The aspect may be part of the specification of an import
1275 -- or export pragma. Scan the aspect list to gather the
1276 -- other components, if any. The name of the generated
1277 -- pragma is one of Convention/Import/Export.
1289 P_Name := Chars (Id);
1291 Arg_List := New_List;
1296 while Present (A) loop
1297 A_Name := Chars (Identifier (A));
1299 if A_Name = Name_Import
1300 or else A_Name = Name_Export
1303 Error_Msg_N ("conflicting", A);
1310 elsif A_Name = Name_Link_Name then
1311 L_Assoc := Make_Pragma_Argument_Association (Loc,
1313 Expression => Relocate_Node (Expression (A)));
1315 elsif A_Name = Name_External_Name then
1316 E_Assoc := Make_Pragma_Argument_Association (Loc,
1318 Expression => Relocate_Node (Expression (A)));
1324 Arg_List := New_List (Relocate_Node (Expr), Ent);
1325 if Present (L_Assoc) then
1326 Append_To (Arg_List, L_Assoc);
1329 if Present (E_Assoc) then
1330 Append_To (Arg_List, E_Assoc);
1335 Pragma_Argument_Associations => Arg_List,
1336 Pragma_Identifier =>
1337 Make_Identifier (Loc, P_Name));
1340 -- The following three aspects can be specified for a
1341 -- subprogram body, in which case we generate pragmas for them
1342 -- and insert them ahead of local declarations, rather than
1346 Aspect_Interrupt_Priority |
1348 if Nkind (N) = N_Subprogram_Body then
1351 Pragma_Argument_Associations =>
1352 New_List (Relocate_Node (Expr)),
1353 Pragma_Identifier =>
1354 Make_Identifier (Sloc (Id), Chars (Id)));
1357 Make_Attribute_Definition_Clause (Loc,
1359 Chars => Chars (Id),
1360 Expression => Relocate_Node (Expr));
1363 when Aspect_Warnings =>
1365 -- Construct the pragma
1369 Pragma_Argument_Associations => New_List (
1370 Relocate_Node (Expr),
1371 New_Occurrence_Of (E, Loc)),
1372 Pragma_Identifier =>
1373 Make_Identifier (Sloc (Id), Chars (Id)),
1374 Class_Present => Class_Present (Aspect));
1376 -- We don't have to play the delay game here, since the only
1377 -- values are ON/OFF which don't get analyzed anyway.
1379 Delay_Required := False;
1381 -- Case 2c: Aspects corresponding to pragmas with three
1384 -- Invariant aspects have a first argument that references the
1385 -- entity, a second argument that is the expression and a third
1386 -- argument that is an appropriate message.
1388 when Aspect_Invariant |
1389 Aspect_Type_Invariant =>
1391 -- Analysis of the pragma will verify placement legality:
1392 -- an invariant must apply to a private type, or appear in
1393 -- the private part of a spec and apply to a completion.
1395 -- Construct the pragma
1399 Pragma_Argument_Associations =>
1400 New_List (Ent, Relocate_Node (Expr)),
1401 Class_Present => Class_Present (Aspect),
1402 Pragma_Identifier =>
1403 Make_Identifier (Sloc (Id), Name_Invariant));
1405 -- Add message unless exception messages are suppressed
1407 if not Opt.Exception_Locations_Suppressed then
1408 Append_To (Pragma_Argument_Associations (Aitem),
1409 Make_Pragma_Argument_Association (Eloc,
1410 Chars => Name_Message,
1412 Make_String_Literal (Eloc,
1413 Strval => "failed invariant from "
1414 & Build_Location_String (Eloc))));
1417 -- For Invariant case, insert immediately after the entity
1418 -- declaration. We do not have to worry about delay issues
1419 -- since the pragma processing takes care of this.
1421 Delay_Required := False;
1423 -- Case 3 : Aspects that don't correspond to pragma/attribute
1424 -- definition clause.
1426 -- Case 3a: The aspects listed below don't correspond to
1427 -- pragmas/attributes but do require delayed analysis.
1429 when Aspect_Default_Value |
1430 Aspect_Default_Component_Value =>
1433 -- Case 3b: The aspects listed below don't correspond to
1434 -- pragmas/attributes and don't need delayed analysis.
1436 -- For Implicit_Dereference, External_Name and Link_Name, only
1437 -- the legality checks are done during the analysis, thus no
1438 -- delay is required.
1440 when Aspect_Implicit_Dereference =>
1441 Analyze_Aspect_Implicit_Dereference;
1444 when Aspect_External_Name |
1446 Analyze_Aspect_External_Or_Link_Name;
1449 when Aspect_Dimension =>
1450 Analyze_Aspect_Dimension (N, Id, Expr);
1453 when Aspect_Dimension_System =>
1454 Analyze_Aspect_Dimension_System (N, Id, Expr);
1457 -- Case 4: Special handling for aspects
1458 -- Pre/Post/Test_Case/Contract_Case whose corresponding pragmas
1459 -- take care of the delay.
1461 -- Aspects Pre/Post generate Precondition/Postcondition pragmas
1462 -- with a first argument that is the expression, and a second
1463 -- argument that is an informative message if the test fails.
1464 -- This is inserted right after the declaration, to get the
1465 -- required pragma placement. The processing for the pragmas
1466 -- takes care of the required delay.
1468 when Pre_Post_Aspects => declare
1472 if A_Id = Aspect_Pre or else A_Id = Aspect_Precondition then
1473 Pname := Name_Precondition;
1475 Pname := Name_Postcondition;
1478 -- If the expressions is of the form A and then B, then
1479 -- we generate separate Pre/Post aspects for the separate
1480 -- clauses. Since we allow multiple pragmas, there is no
1481 -- problem in allowing multiple Pre/Post aspects internally.
1482 -- These should be treated in reverse order (B first and
1483 -- A second) since they are later inserted just after N in
1484 -- the order they are treated. This way, the pragma for A
1485 -- ends up preceding the pragma for B, which may have an
1486 -- importance for the error raised (either constraint error
1487 -- or precondition error).
1489 -- We do not do this for Pre'Class, since we have to put
1490 -- these conditions together in a complex OR expression
1492 -- We do not do this in ASIS mode, as ASIS relies on the
1493 -- original node representing the complete expression, when
1494 -- retrieving it through the source aspect table.
1497 and then (Pname = Name_Postcondition
1498 or else not Class_Present (Aspect))
1500 while Nkind (Expr) = N_And_Then loop
1501 Insert_After (Aspect,
1502 Make_Aspect_Specification (Sloc (Left_Opnd (Expr)),
1503 Identifier => Identifier (Aspect),
1504 Expression => Relocate_Node (Left_Opnd (Expr)),
1505 Class_Present => Class_Present (Aspect),
1506 Split_PPC => True));
1507 Rewrite (Expr, Relocate_Node (Right_Opnd (Expr)));
1508 Eloc := Sloc (Expr);
1512 -- Build the precondition/postcondition pragma
1516 Pragma_Identifier =>
1517 Make_Identifier (Sloc (Id), Pname),
1518 Class_Present => Class_Present (Aspect),
1519 Split_PPC => Split_PPC (Aspect),
1520 Pragma_Argument_Associations => New_List (
1521 Make_Pragma_Argument_Association (Eloc,
1522 Chars => Name_Check,
1523 Expression => Relocate_Node (Expr))));
1525 -- Add message unless exception messages are suppressed
1527 if not Opt.Exception_Locations_Suppressed then
1528 Append_To (Pragma_Argument_Associations (Aitem),
1529 Make_Pragma_Argument_Association (Eloc,
1530 Chars => Name_Message,
1532 Make_String_Literal (Eloc,
1534 & Get_Name_String (Pname)
1536 & Build_Location_String (Eloc))));
1539 Set_From_Aspect_Specification (Aitem, True);
1540 Set_Corresponding_Aspect (Aitem, Aspect);
1541 Set_Is_Delayed_Aspect (Aspect);
1543 -- For Pre/Post cases, insert immediately after the entity
1544 -- declaration, since that is the required pragma placement.
1545 -- Note that for these aspects, we do not have to worry
1546 -- about delay issues, since the pragmas themselves deal
1547 -- with delay of visibility for the expression analysis.
1549 -- If the entity is a library-level subprogram, the pre/
1550 -- postconditions must be treated as late pragmas.
1552 if Nkind (Parent (N)) = N_Compilation_Unit then
1553 Add_Global_Declaration (Aitem);
1555 Insert_After (N, Aitem);
1561 when Aspect_Contract_Case |
1565 Comp_Expr : Node_Id;
1566 Comp_Assn : Node_Id;
1572 if Nkind (Parent (N)) = N_Compilation_Unit then
1573 Error_Msg_Name_1 := Nam;
1574 Error_Msg_N ("incorrect placement of aspect `%`", E);
1578 if Nkind (Expr) /= N_Aggregate then
1579 Error_Msg_Name_1 := Nam;
1581 ("wrong syntax for aspect `%` for &", Id, E);
1585 -- Make pragma expressions refer to the original aspect
1586 -- expressions through the Original_Node link. This is
1587 -- used in semantic analysis for ASIS mode, so that the
1588 -- original expression also gets analyzed.
1590 Comp_Expr := First (Expressions (Expr));
1591 while Present (Comp_Expr) loop
1592 New_Expr := Relocate_Node (Comp_Expr);
1593 Set_Original_Node (New_Expr, Comp_Expr);
1595 (Make_Pragma_Argument_Association (Sloc (Comp_Expr),
1596 Expression => New_Expr),
1601 Comp_Assn := First (Component_Associations (Expr));
1602 while Present (Comp_Assn) loop
1603 if List_Length (Choices (Comp_Assn)) /= 1
1605 Nkind (First (Choices (Comp_Assn))) /= N_Identifier
1607 Error_Msg_Name_1 := Nam;
1609 ("wrong syntax for aspect `%` for &", Id, E);
1613 New_Expr := Relocate_Node (Expression (Comp_Assn));
1614 Set_Original_Node (New_Expr, Expression (Comp_Assn));
1615 Append (Make_Pragma_Argument_Association (
1616 Sloc => Sloc (Comp_Assn),
1617 Chars => Chars (First (Choices (Comp_Assn))),
1618 Expression => New_Expr),
1623 -- Build the contract-case or test-case pragma
1627 Pragma_Identifier =>
1628 Make_Identifier (Sloc (Id), Nam),
1629 Pragma_Argument_Associations =>
1632 Delay_Required := False;
1635 -- Case 5: Special handling for aspects with an optional
1636 -- boolean argument.
1638 -- In the general case, the corresponding pragma cannot be
1639 -- generated yet because the evaluation of the boolean needs to
1640 -- be delayed til the freeze point.
1642 when Boolean_Aspects |
1643 Library_Unit_Aspects =>
1645 Set_Is_Boolean_Aspect (Aspect);
1647 -- Lock_Free aspect only apply to protected objects
1649 if A_Id = Aspect_Lock_Free then
1650 if Ekind (E) /= E_Protected_Type then
1651 Error_Msg_Name_1 := Nam;
1653 ("aspect % only applies to a protected object",
1657 -- Set the Uses_Lock_Free flag to True if there is no
1658 -- expression or if the expression is True. ??? The
1659 -- evaluation of this aspect should be delayed to the
1663 or else Is_True (Static_Boolean (Expr))
1665 Set_Uses_Lock_Free (E);
1668 Record_Rep_Item (E, Aspect);
1673 elsif A_Id = Aspect_Import or else A_Id = Aspect_Export then
1675 -- Verify that there is an aspect Convention that will
1676 -- incorporate the Import/Export aspect, and eventual
1677 -- Link/External names.
1684 while Present (A) loop
1685 exit when Chars (Identifier (A)) = Name_Convention;
1691 ("missing Convention aspect for Export/Import",
1699 -- This requires special handling in the case of a package
1700 -- declaration, the pragma needs to be inserted in the list
1701 -- of declarations for the associated package. There is no
1702 -- issue of visibility delay for these aspects.
1704 if A_Id in Library_Unit_Aspects
1705 and then Nkind (N) = N_Package_Declaration
1706 and then Nkind (Parent (N)) /= N_Compilation_Unit
1709 ("incorrect context for library unit aspect&", Id);
1713 -- Special handling when the aspect has no expression. In
1714 -- this case the value is considered to be True. Thus, we
1715 -- simply insert the pragma, no delay is required.
1720 Pragma_Argument_Associations => New_List (Ent),
1721 Pragma_Identifier =>
1722 Make_Identifier (Sloc (Id), Chars (Id)));
1724 Delay_Required := False;
1726 -- In general cases, the corresponding pragma/attribute
1727 -- definition clause will be inserted later at the freezing
1735 -- Attach the corresponding pragma/attribute definition clause to
1736 -- the aspect specification node.
1738 if Present (Aitem) then
1739 Set_From_Aspect_Specification (Aitem, True);
1741 if Nkind (Aitem) = N_Pragma then
1742 Set_Corresponding_Aspect (Aitem, Aspect);
1746 -- In the context of a compilation unit, we directly put the
1747 -- pragma in the Pragmas_After list of the
1748 -- N_Compilation_Unit_Aux node (No delay is required here)
1749 -- except for aspects on a subprogram body (see below).
1751 if Nkind (Parent (N)) = N_Compilation_Unit
1752 and then (Present (Aitem) or else Is_Boolean_Aspect (Aspect))
1755 Aux : constant Node_Id := Aux_Decls_Node (Parent (N));
1758 pragma Assert (Nkind (Aux) = N_Compilation_Unit_Aux);
1760 -- For a Boolean aspect, create the corresponding pragma if
1761 -- no expression or if the value is True.
1763 if Is_Boolean_Aspect (Aspect)
1766 if Is_True (Static_Boolean (Expr)) then
1769 Pragma_Argument_Associations => New_List (Ent),
1770 Pragma_Identifier =>
1771 Make_Identifier (Sloc (Id), Chars (Id)));
1773 Set_From_Aspect_Specification (Aitem, True);
1774 Set_Corresponding_Aspect (Aitem, Aspect);
1781 -- If the aspect is on a subprogram body (relevant aspects
1782 -- are Inline and Priority), add the pragma in front of
1783 -- the declarations.
1785 if Nkind (N) = N_Subprogram_Body then
1786 if No (Declarations (N)) then
1787 Set_Declarations (N, New_List);
1790 Prepend (Aitem, Declarations (N));
1793 if No (Pragmas_After (Aux)) then
1794 Set_Pragmas_After (Aux, Empty_List);
1797 Append (Aitem, Pragmas_After (Aux));
1804 -- The evaluation of the aspect is delayed to the freezing point.
1805 -- The pragma or attribute clause if there is one is then attached
1806 -- to the aspect specification which is placed in the rep item
1809 if Delay_Required then
1810 if Present (Aitem) then
1811 Set_Is_Delayed_Aspect (Aitem);
1812 Set_Aspect_Rep_Item (Aspect, Aitem);
1813 Set_Parent (Aitem, Aspect);
1816 Set_Is_Delayed_Aspect (Aspect);
1817 Set_Has_Delayed_Aspects (E);
1818 Record_Rep_Item (E, Aspect);
1820 -- When delay is not required and the context is not a compilation
1821 -- unit, we simply insert the pragma/attribute definition clause
1825 Insert_After (Ins_Node, Aitem);
1832 end loop Aspect_Loop;
1834 if Has_Delayed_Aspects (E) then
1835 Ensure_Freeze_Node (E);
1837 end Analyze_Aspect_Specifications;
1839 -----------------------
1840 -- Analyze_At_Clause --
1841 -----------------------
1843 -- An at clause is replaced by the corresponding Address attribute
1844 -- definition clause that is the preferred approach in Ada 95.
1846 procedure Analyze_At_Clause (N : Node_Id) is
1847 CS : constant Boolean := Comes_From_Source (N);
1850 -- This is an obsolescent feature
1852 Check_Restriction (No_Obsolescent_Features, N);
1854 if Warn_On_Obsolescent_Feature then
1856 ("at clause is an obsolescent feature (RM J.7(2))?", N);
1858 ("\use address attribute definition clause instead?", N);
1861 -- Rewrite as address clause
1864 Make_Attribute_Definition_Clause (Sloc (N),
1865 Name => Identifier (N),
1866 Chars => Name_Address,
1867 Expression => Expression (N)));
1869 -- We preserve Comes_From_Source, since logically the clause still comes
1870 -- from the source program even though it is changed in form.
1872 Set_Comes_From_Source (N, CS);
1874 -- Analyze rewritten clause
1876 Analyze_Attribute_Definition_Clause (N);
1877 end Analyze_At_Clause;
1879 -----------------------------------------
1880 -- Analyze_Attribute_Definition_Clause --
1881 -----------------------------------------
1883 procedure Analyze_Attribute_Definition_Clause (N : Node_Id) is
1884 Loc : constant Source_Ptr := Sloc (N);
1885 Nam : constant Node_Id := Name (N);
1886 Attr : constant Name_Id := Chars (N);
1887 Expr : constant Node_Id := Expression (N);
1888 Id : constant Attribute_Id := Get_Attribute_Id (Attr);
1891 -- The entity of Nam after it is analyzed. In the case of an incomplete
1892 -- type, this is the underlying type.
1895 -- The underlying entity to which the attribute applies. Generally this
1896 -- is the Underlying_Type of Ent, except in the case where the clause
1897 -- applies to full view of incomplete type or private type in which case
1898 -- U_Ent is just a copy of Ent.
1900 FOnly : Boolean := False;
1901 -- Reset to True for subtype specific attribute (Alignment, Size)
1902 -- and for stream attributes, i.e. those cases where in the call
1903 -- to Rep_Item_Too_Late, FOnly is set True so that only the freezing
1904 -- rules are checked. Note that the case of stream attributes is not
1905 -- clear from the RM, but see AI95-00137. Also, the RM seems to
1906 -- disallow Storage_Size for derived task types, but that is also
1907 -- clearly unintentional.
1909 procedure Analyze_Stream_TSS_Definition (TSS_Nam : TSS_Name_Type);
1910 -- Common processing for 'Read, 'Write, 'Input and 'Output attribute
1911 -- definition clauses.
1913 function Duplicate_Clause return Boolean;
1914 -- This routine checks if the aspect for U_Ent being given by attribute
1915 -- definition clause N is for an aspect that has already been specified,
1916 -- and if so gives an error message. If there is a duplicate, True is
1917 -- returned, otherwise if there is no error, False is returned.
1919 procedure Check_Indexing_Functions;
1920 -- Check that the function in Constant_Indexing or Variable_Indexing
1921 -- attribute has the proper type structure. If the name is overloaded,
1922 -- check that all interpretations are legal.
1924 procedure Check_Iterator_Functions;
1925 -- Check that there is a single function in Default_Iterator attribute
1926 -- has the proper type structure.
1928 function Check_Primitive_Function (Subp : Entity_Id) return Boolean;
1929 -- Common legality check for the previous two
1931 -----------------------------------
1932 -- Analyze_Stream_TSS_Definition --
1933 -----------------------------------
1935 procedure Analyze_Stream_TSS_Definition (TSS_Nam : TSS_Name_Type) is
1936 Subp : Entity_Id := Empty;
1941 Is_Read : constant Boolean := (TSS_Nam = TSS_Stream_Read);
1942 -- True for Read attribute, false for other attributes
1944 function Has_Good_Profile (Subp : Entity_Id) return Boolean;
1945 -- Return true if the entity is a subprogram with an appropriate
1946 -- profile for the attribute being defined.
1948 ----------------------
1949 -- Has_Good_Profile --
1950 ----------------------
1952 function Has_Good_Profile (Subp : Entity_Id) return Boolean is
1954 Is_Function : constant Boolean := (TSS_Nam = TSS_Stream_Input);
1955 Expected_Ekind : constant array (Boolean) of Entity_Kind :=
1956 (False => E_Procedure, True => E_Function);
1960 if Ekind (Subp) /= Expected_Ekind (Is_Function) then
1964 F := First_Formal (Subp);
1967 or else Ekind (Etype (F)) /= E_Anonymous_Access_Type
1968 or else Designated_Type (Etype (F)) /=
1969 Class_Wide_Type (RTE (RE_Root_Stream_Type))
1974 if not Is_Function then
1978 Expected_Mode : constant array (Boolean) of Entity_Kind :=
1979 (False => E_In_Parameter,
1980 True => E_Out_Parameter);
1982 if Parameter_Mode (F) /= Expected_Mode (Is_Read) then
1990 Typ := Etype (Subp);
1993 return Base_Type (Typ) = Base_Type (Ent)
1994 and then No (Next_Formal (F));
1995 end Has_Good_Profile;
1997 -- Start of processing for Analyze_Stream_TSS_Definition
2002 if not Is_Type (U_Ent) then
2003 Error_Msg_N ("local name must be a subtype", Nam);
2007 Pnam := TSS (Base_Type (U_Ent), TSS_Nam);
2009 -- If Pnam is present, it can be either inherited from an ancestor
2010 -- type (in which case it is legal to redefine it for this type), or
2011 -- be a previous definition of the attribute for the same type (in
2012 -- which case it is illegal).
2014 -- In the first case, it will have been analyzed already, and we
2015 -- can check that its profile does not match the expected profile
2016 -- for a stream attribute of U_Ent. In the second case, either Pnam
2017 -- has been analyzed (and has the expected profile), or it has not
2018 -- been analyzed yet (case of a type that has not been frozen yet
2019 -- and for which the stream attribute has been set using Set_TSS).
2022 and then (No (First_Entity (Pnam)) or else Has_Good_Profile (Pnam))
2024 Error_Msg_Sloc := Sloc (Pnam);
2025 Error_Msg_Name_1 := Attr;
2026 Error_Msg_N ("% attribute already defined #", Nam);
2032 if Is_Entity_Name (Expr) then
2033 if not Is_Overloaded (Expr) then
2034 if Has_Good_Profile (Entity (Expr)) then
2035 Subp := Entity (Expr);
2039 Get_First_Interp (Expr, I, It);
2040 while Present (It.Nam) loop
2041 if Has_Good_Profile (It.Nam) then
2046 Get_Next_Interp (I, It);
2051 if Present (Subp) then
2052 if Is_Abstract_Subprogram (Subp) then
2053 Error_Msg_N ("stream subprogram must not be abstract", Expr);
2057 Set_Entity (Expr, Subp);
2058 Set_Etype (Expr, Etype (Subp));
2060 New_Stream_Subprogram (N, U_Ent, Subp, TSS_Nam);
2063 Error_Msg_Name_1 := Attr;
2064 Error_Msg_N ("incorrect expression for% attribute", Expr);
2066 end Analyze_Stream_TSS_Definition;
2068 ------------------------------
2069 -- Check_Indexing_Functions --
2070 ------------------------------
2072 procedure Check_Indexing_Functions is
2074 procedure Check_One_Function (Subp : Entity_Id);
2075 -- Check one possible interpretation
2077 ------------------------
2078 -- Check_One_Function --
2079 ------------------------
2081 procedure Check_One_Function (Subp : Entity_Id) is
2082 Default_Element : constant Node_Id :=
2084 (Etype (First_Formal (Subp)),
2085 Aspect_Iterator_Element);
2088 if not Check_Primitive_Function (Subp) then
2090 ("aspect Indexing requires a function that applies to type&",
2094 -- An indexing function must return either the default element of
2095 -- the container, or a reference type.
2097 if Present (Default_Element) then
2098 Analyze (Default_Element);
2099 if Is_Entity_Name (Default_Element)
2100 and then Covers (Entity (Default_Element), Etype (Subp))
2106 -- Otherwise the return type must be a reference type.
2108 if not Has_Implicit_Dereference (Etype (Subp)) then
2110 ("function for indexing must return a reference type", Subp);
2112 end Check_One_Function;
2114 -- Start of processing for Check_Indexing_Functions
2123 if not Is_Overloaded (Expr) then
2124 Check_One_Function (Entity (Expr));
2132 Get_First_Interp (Expr, I, It);
2133 while Present (It.Nam) loop
2135 -- Note that analysis will have added the interpretation
2136 -- that corresponds to the dereference. We only check the
2137 -- subprogram itself.
2139 if Is_Overloadable (It.Nam) then
2140 Check_One_Function (It.Nam);
2143 Get_Next_Interp (I, It);
2147 end Check_Indexing_Functions;
2149 ------------------------------
2150 -- Check_Iterator_Functions --
2151 ------------------------------
2153 procedure Check_Iterator_Functions is
2154 Default : Entity_Id;
2156 function Valid_Default_Iterator (Subp : Entity_Id) return Boolean;
2157 -- Check one possible interpretation for validity
2159 ----------------------------
2160 -- Valid_Default_Iterator --
2161 ----------------------------
2163 function Valid_Default_Iterator (Subp : Entity_Id) return Boolean is
2167 if not Check_Primitive_Function (Subp) then
2170 Formal := First_Formal (Subp);
2173 -- False if any subsequent formal has no default expression
2175 Formal := Next_Formal (Formal);
2176 while Present (Formal) loop
2177 if No (Expression (Parent (Formal))) then
2181 Next_Formal (Formal);
2184 -- True if all subsequent formals have default expressions
2187 end Valid_Default_Iterator;
2189 -- Start of processing for Check_Iterator_Functions
2194 if not Is_Entity_Name (Expr) then
2195 Error_Msg_N ("aspect Iterator must be a function name", Expr);
2198 if not Is_Overloaded (Expr) then
2199 if not Check_Primitive_Function (Entity (Expr)) then
2201 ("aspect Indexing requires a function that applies to type&",
2202 Entity (Expr), Ent);
2205 if not Valid_Default_Iterator (Entity (Expr)) then
2206 Error_Msg_N ("improper function for default iterator", Expr);
2216 Get_First_Interp (Expr, I, It);
2217 while Present (It.Nam) loop
2218 if not Check_Primitive_Function (It.Nam)
2219 or else not Valid_Default_Iterator (It.Nam)
2223 elsif Present (Default) then
2224 Error_Msg_N ("default iterator must be unique", Expr);
2230 Get_Next_Interp (I, It);
2234 if Present (Default) then
2235 Set_Entity (Expr, Default);
2236 Set_Is_Overloaded (Expr, False);
2239 end Check_Iterator_Functions;
2241 -------------------------------
2242 -- Check_Primitive_Function --
2243 -------------------------------
2245 function Check_Primitive_Function (Subp : Entity_Id) return Boolean is
2249 if Ekind (Subp) /= E_Function then
2253 if No (First_Formal (Subp)) then
2256 Ctrl := Etype (First_Formal (Subp));
2260 or else Ctrl = Class_Wide_Type (Ent)
2262 (Ekind (Ctrl) = E_Anonymous_Access_Type
2264 (Designated_Type (Ctrl) = Ent
2265 or else Designated_Type (Ctrl) = Class_Wide_Type (Ent)))
2274 end Check_Primitive_Function;
2276 ----------------------
2277 -- Duplicate_Clause --
2278 ----------------------
2280 function Duplicate_Clause return Boolean is
2284 -- Nothing to do if this attribute definition clause comes from
2285 -- an aspect specification, since we could not be duplicating an
2286 -- explicit clause, and we dealt with the case of duplicated aspects
2287 -- in Analyze_Aspect_Specifications.
2289 if From_Aspect_Specification (N) then
2293 -- Otherwise current clause may duplicate previous clause, or a
2294 -- previously given pragma or aspect specification for the same
2297 A := Get_Rep_Item (U_Ent, Chars (N), Check_Parents => False);
2300 Error_Msg_Name_1 := Chars (N);
2301 Error_Msg_Sloc := Sloc (A);
2303 Error_Msg_NE ("aspect% for & previously given#", N, U_Ent);
2308 end Duplicate_Clause;
2310 -- Start of processing for Analyze_Attribute_Definition_Clause
2313 -- The following code is a defense against recursion. Not clear that
2314 -- this can happen legitimately, but perhaps some error situations
2315 -- can cause it, and we did see this recursion during testing.
2317 if Analyzed (N) then
2320 Set_Analyzed (N, True);
2323 -- Ignore some selected attributes in CodePeer mode since they are not
2324 -- relevant in this context.
2326 if CodePeer_Mode then
2329 -- Ignore Component_Size in CodePeer mode, to avoid changing the
2330 -- internal representation of types by implicitly packing them.
2332 when Attribute_Component_Size =>
2333 Rewrite (N, Make_Null_Statement (Sloc (N)));
2341 -- Process Ignore_Rep_Clauses option
2343 if Ignore_Rep_Clauses then
2346 -- The following should be ignored. They do not affect legality
2347 -- and may be target dependent. The basic idea of -gnatI is to
2348 -- ignore any rep clauses that may be target dependent but do not
2349 -- affect legality (except possibly to be rejected because they
2350 -- are incompatible with the compilation target).
2352 when Attribute_Alignment |
2353 Attribute_Bit_Order |
2354 Attribute_Component_Size |
2355 Attribute_Machine_Radix |
2356 Attribute_Object_Size |
2358 Attribute_Stream_Size |
2359 Attribute_Value_Size =>
2360 Rewrite (N, Make_Null_Statement (Sloc (N)));
2363 -- Perhaps 'Small should not be ignored by Ignore_Rep_Clauses ???
2365 when Attribute_Small =>
2366 if Ignore_Rep_Clauses then
2367 Rewrite (N, Make_Null_Statement (Sloc (N)));
2371 -- The following should not be ignored, because in the first place
2372 -- they are reasonably portable, and should not cause problems in
2373 -- compiling code from another target, and also they do affect
2374 -- legality, e.g. failing to provide a stream attribute for a
2375 -- type may make a program illegal.
2377 when Attribute_External_Tag |
2381 Attribute_Simple_Storage_Pool |
2382 Attribute_Storage_Pool |
2383 Attribute_Storage_Size |
2387 -- Other cases are errors ("attribute& cannot be set with
2388 -- definition clause"), which will be caught below.
2396 Ent := Entity (Nam);
2398 if Rep_Item_Too_Early (Ent, N) then
2402 -- Rep clause applies to full view of incomplete type or private type if
2403 -- we have one (if not, this is a premature use of the type). However,
2404 -- certain semantic checks need to be done on the specified entity (i.e.
2405 -- the private view), so we save it in Ent.
2407 if Is_Private_Type (Ent)
2408 and then Is_Derived_Type (Ent)
2409 and then not Is_Tagged_Type (Ent)
2410 and then No (Full_View (Ent))
2412 -- If this is a private type whose completion is a derivation from
2413 -- another private type, there is no full view, and the attribute
2414 -- belongs to the type itself, not its underlying parent.
2418 elsif Ekind (Ent) = E_Incomplete_Type then
2420 -- The attribute applies to the full view, set the entity of the
2421 -- attribute definition accordingly.
2423 Ent := Underlying_Type (Ent);
2425 Set_Entity (Nam, Ent);
2428 U_Ent := Underlying_Type (Ent);
2431 -- Avoid cascaded error
2433 if Etype (Nam) = Any_Type then
2436 -- Must be declared in current scope or in case of an aspect
2437 -- specification, must be visible in current scope.
2439 elsif Scope (Ent) /= Current_Scope
2441 not (From_Aspect_Specification (N)
2442 and then Scope_Within_Or_Same (Current_Scope, Scope (Ent)))
2444 Error_Msg_N ("entity must be declared in this scope", Nam);
2447 -- Must not be a source renaming (we do have some cases where the
2448 -- expander generates a renaming, and those cases are OK, in such
2449 -- cases any attribute applies to the renamed object as well).
2451 elsif Is_Object (Ent)
2452 and then Present (Renamed_Object (Ent))
2454 -- Case of renamed object from source, this is an error
2456 if Comes_From_Source (Renamed_Object (Ent)) then
2457 Get_Name_String (Chars (N));
2458 Error_Msg_Strlen := Name_Len;
2459 Error_Msg_String (1 .. Name_Len) := Name_Buffer (1 .. Name_Len);
2461 ("~ clause not allowed for a renaming declaration "
2462 & "(RM 13.1(6))", Nam);
2465 -- For the case of a compiler generated renaming, the attribute
2466 -- definition clause applies to the renamed object created by the
2467 -- expander. The easiest general way to handle this is to create a
2468 -- copy of the attribute definition clause for this object.
2472 Make_Attribute_Definition_Clause (Loc,
2474 New_Occurrence_Of (Entity (Renamed_Object (Ent)), Loc),
2476 Expression => Duplicate_Subexpr (Expression (N))));
2479 -- If no underlying entity, use entity itself, applies to some
2480 -- previously detected error cases ???
2482 elsif No (U_Ent) then
2485 -- Cannot specify for a subtype (exception Object/Value_Size)
2487 elsif Is_Type (U_Ent)
2488 and then not Is_First_Subtype (U_Ent)
2489 and then Id /= Attribute_Object_Size
2490 and then Id /= Attribute_Value_Size
2491 and then not From_At_Mod (N)
2493 Error_Msg_N ("cannot specify attribute for subtype", Nam);
2497 Set_Entity (N, U_Ent);
2499 -- Switch on particular attribute
2507 -- Address attribute definition clause
2509 when Attribute_Address => Address : begin
2511 -- A little error check, catch for X'Address use X'Address;
2513 if Nkind (Nam) = N_Identifier
2514 and then Nkind (Expr) = N_Attribute_Reference
2515 and then Attribute_Name (Expr) = Name_Address
2516 and then Nkind (Prefix (Expr)) = N_Identifier
2517 and then Chars (Nam) = Chars (Prefix (Expr))
2520 ("address for & is self-referencing", Prefix (Expr), Ent);
2524 -- Not that special case, carry on with analysis of expression
2526 Analyze_And_Resolve (Expr, RTE (RE_Address));
2528 -- Even when ignoring rep clauses we need to indicate that the
2529 -- entity has an address clause and thus it is legal to declare
2532 if Ignore_Rep_Clauses then
2533 if Ekind_In (U_Ent, E_Variable, E_Constant) then
2534 Record_Rep_Item (U_Ent, N);
2540 if Duplicate_Clause then
2543 -- Case of address clause for subprogram
2545 elsif Is_Subprogram (U_Ent) then
2546 if Has_Homonym (U_Ent) then
2548 ("address clause cannot be given " &
2549 "for overloaded subprogram",
2554 -- For subprograms, all address clauses are permitted, and we
2555 -- mark the subprogram as having a deferred freeze so that Gigi
2556 -- will not elaborate it too soon.
2558 -- Above needs more comments, what is too soon about???
2560 Set_Has_Delayed_Freeze (U_Ent);
2562 -- Case of address clause for entry
2564 elsif Ekind (U_Ent) = E_Entry then
2565 if Nkind (Parent (N)) = N_Task_Body then
2567 ("entry address must be specified in task spec", Nam);
2571 -- For entries, we require a constant address
2573 Check_Constant_Address_Clause (Expr, U_Ent);
2575 -- Special checks for task types
2577 if Is_Task_Type (Scope (U_Ent))
2578 and then Comes_From_Source (Scope (U_Ent))
2581 ("?entry address declared for entry in task type", N);
2583 ("\?only one task can be declared of this type", N);
2586 -- Entry address clauses are obsolescent
2588 Check_Restriction (No_Obsolescent_Features, N);
2590 if Warn_On_Obsolescent_Feature then
2592 ("attaching interrupt to task entry is an " &
2593 "obsolescent feature (RM J.7.1)?", N);
2595 ("\use interrupt procedure instead?", N);
2598 -- Case of an address clause for a controlled object which we
2599 -- consider to be erroneous.
2601 elsif Is_Controlled (Etype (U_Ent))
2602 or else Has_Controlled_Component (Etype (U_Ent))
2605 ("?controlled object& must not be overlaid", Nam, U_Ent);
2607 ("\?Program_Error will be raised at run time", Nam);
2608 Insert_Action (Declaration_Node (U_Ent),
2609 Make_Raise_Program_Error (Loc,
2610 Reason => PE_Overlaid_Controlled_Object));
2613 -- Case of address clause for a (non-controlled) object
2616 Ekind (U_Ent) = E_Variable
2618 Ekind (U_Ent) = E_Constant
2621 Expr : constant Node_Id := Expression (N);
2626 -- Exported variables cannot have an address clause, because
2627 -- this cancels the effect of the pragma Export.
2629 if Is_Exported (U_Ent) then
2631 ("cannot export object with address clause", Nam);
2635 Find_Overlaid_Entity (N, O_Ent, Off);
2637 -- Overlaying controlled objects is erroneous
2640 and then (Has_Controlled_Component (Etype (O_Ent))
2641 or else Is_Controlled (Etype (O_Ent)))
2644 ("?cannot overlay with controlled object", Expr);
2646 ("\?Program_Error will be raised at run time", Expr);
2647 Insert_Action (Declaration_Node (U_Ent),
2648 Make_Raise_Program_Error (Loc,
2649 Reason => PE_Overlaid_Controlled_Object));
2652 elsif Present (O_Ent)
2653 and then Ekind (U_Ent) = E_Constant
2654 and then not Is_Constant_Object (O_Ent)
2656 Error_Msg_N ("constant overlays a variable?", Expr);
2658 -- Imported variables can have an address clause, but then
2659 -- the import is pretty meaningless except to suppress
2660 -- initializations, so we do not need such variables to
2661 -- be statically allocated (and in fact it causes trouble
2662 -- if the address clause is a local value).
2664 elsif Is_Imported (U_Ent) then
2665 Set_Is_Statically_Allocated (U_Ent, False);
2668 -- We mark a possible modification of a variable with an
2669 -- address clause, since it is likely aliasing is occurring.
2671 Note_Possible_Modification (Nam, Sure => False);
2673 -- Here we are checking for explicit overlap of one variable
2674 -- by another, and if we find this then mark the overlapped
2675 -- variable as also being volatile to prevent unwanted
2676 -- optimizations. This is a significant pessimization so
2677 -- avoid it when there is an offset, i.e. when the object
2678 -- is composite; they cannot be optimized easily anyway.
2681 and then Is_Object (O_Ent)
2684 Set_Treat_As_Volatile (O_Ent);
2687 -- Legality checks on the address clause for initialized
2688 -- objects is deferred until the freeze point, because
2689 -- a subsequent pragma might indicate that the object
2690 -- is imported and thus not initialized.
2692 Set_Has_Delayed_Freeze (U_Ent);
2694 -- If an initialization call has been generated for this
2695 -- object, it needs to be deferred to after the freeze node
2696 -- we have just now added, otherwise GIGI will see a
2697 -- reference to the variable (as actual to the IP call)
2698 -- before its definition.
2701 Init_Call : constant Node_Id := Find_Init_Call (U_Ent, N);
2703 if Present (Init_Call) then
2705 Append_Freeze_Action (U_Ent, Init_Call);
2709 if Is_Exported (U_Ent) then
2711 ("& cannot be exported if an address clause is given",
2714 ("\define and export a variable " &
2715 "that holds its address instead",
2719 -- Entity has delayed freeze, so we will generate an
2720 -- alignment check at the freeze point unless suppressed.
2722 if not Range_Checks_Suppressed (U_Ent)
2723 and then not Alignment_Checks_Suppressed (U_Ent)
2725 Set_Check_Address_Alignment (N);
2728 -- Kill the size check code, since we are not allocating
2729 -- the variable, it is somewhere else.
2731 Kill_Size_Check_Code (U_Ent);
2733 -- If the address clause is of the form:
2735 -- for Y'Address use X'Address
2739 -- Const : constant Address := X'Address;
2741 -- for Y'Address use Const;
2743 -- then we make an entry in the table for checking the size
2744 -- and alignment of the overlaying variable. We defer this
2745 -- check till after code generation to take full advantage
2746 -- of the annotation done by the back end. This entry is
2747 -- only made if the address clause comes from source.
2749 -- If the entity has a generic type, the check will be
2750 -- performed in the instance if the actual type justifies
2751 -- it, and we do not insert the clause in the table to
2752 -- prevent spurious warnings.
2754 if Address_Clause_Overlay_Warnings
2755 and then Comes_From_Source (N)
2756 and then Present (O_Ent)
2757 and then Is_Object (O_Ent)
2759 if not Is_Generic_Type (Etype (U_Ent)) then
2760 Address_Clause_Checks.Append ((N, U_Ent, O_Ent, Off));
2763 -- If variable overlays a constant view, and we are
2764 -- warning on overlays, then mark the variable as
2765 -- overlaying a constant (we will give warnings later
2766 -- if this variable is assigned).
2768 if Is_Constant_Object (O_Ent)
2769 and then Ekind (U_Ent) = E_Variable
2771 Set_Overlays_Constant (U_Ent);
2776 -- Not a valid entity for an address clause
2779 Error_Msg_N ("address cannot be given for &", Nam);
2787 -- Alignment attribute definition clause
2789 when Attribute_Alignment => Alignment : declare
2790 Align : constant Uint := Get_Alignment_Value (Expr);
2791 Max_Align : constant Uint := UI_From_Int (Maximum_Alignment);
2796 if not Is_Type (U_Ent)
2797 and then Ekind (U_Ent) /= E_Variable
2798 and then Ekind (U_Ent) /= E_Constant
2800 Error_Msg_N ("alignment cannot be given for &", Nam);
2802 elsif Duplicate_Clause then
2805 elsif Align /= No_Uint then
2806 Set_Has_Alignment_Clause (U_Ent);
2808 -- Tagged type case, check for attempt to set alignment to a
2809 -- value greater than Max_Align, and reset if so.
2811 if Is_Tagged_Type (U_Ent) and then Align > Max_Align then
2813 ("?alignment for & set to Maximum_Aligment", Nam);
2814 Set_Alignment (U_Ent, Max_Align);
2819 Set_Alignment (U_Ent, Align);
2822 -- For an array type, U_Ent is the first subtype. In that case,
2823 -- also set the alignment of the anonymous base type so that
2824 -- other subtypes (such as the itypes for aggregates of the
2825 -- type) also receive the expected alignment.
2827 if Is_Array_Type (U_Ent) then
2828 Set_Alignment (Base_Type (U_Ent), Align);
2837 -- Bit_Order attribute definition clause
2839 when Attribute_Bit_Order => Bit_Order : declare
2841 if not Is_Record_Type (U_Ent) then
2843 ("Bit_Order can only be defined for record type", Nam);
2845 elsif Duplicate_Clause then
2849 Analyze_And_Resolve (Expr, RTE (RE_Bit_Order));
2851 if Etype (Expr) = Any_Type then
2854 elsif not Is_Static_Expression (Expr) then
2855 Flag_Non_Static_Expr
2856 ("Bit_Order requires static expression!", Expr);
2859 if (Expr_Value (Expr) = 0) /= Bytes_Big_Endian then
2860 Set_Reverse_Bit_Order (U_Ent, True);
2866 --------------------
2867 -- Component_Size --
2868 --------------------
2870 -- Component_Size attribute definition clause
2872 when Attribute_Component_Size => Component_Size_Case : declare
2873 Csize : constant Uint := Static_Integer (Expr);
2877 New_Ctyp : Entity_Id;
2881 if not Is_Array_Type (U_Ent) then
2882 Error_Msg_N ("component size requires array type", Nam);
2886 Btype := Base_Type (U_Ent);
2887 Ctyp := Component_Type (Btype);
2889 if Duplicate_Clause then
2892 elsif Rep_Item_Too_Early (Btype, N) then
2895 elsif Csize /= No_Uint then
2896 Check_Size (Expr, Ctyp, Csize, Biased);
2898 -- For the biased case, build a declaration for a subtype that
2899 -- will be used to represent the biased subtype that reflects
2900 -- the biased representation of components. We need the subtype
2901 -- to get proper conversions on referencing elements of the
2902 -- array. Note: component size clauses are ignored in VM mode.
2904 if VM_Target = No_VM then
2907 Make_Defining_Identifier (Loc,
2909 New_External_Name (Chars (U_Ent), 'C', 0, 'T'));
2912 Make_Subtype_Declaration (Loc,
2913 Defining_Identifier => New_Ctyp,
2914 Subtype_Indication =>
2915 New_Occurrence_Of (Component_Type (Btype), Loc));
2917 Set_Parent (Decl, N);
2918 Analyze (Decl, Suppress => All_Checks);
2920 Set_Has_Delayed_Freeze (New_Ctyp, False);
2921 Set_Esize (New_Ctyp, Csize);
2922 Set_RM_Size (New_Ctyp, Csize);
2923 Init_Alignment (New_Ctyp);
2924 Set_Is_Itype (New_Ctyp, True);
2925 Set_Associated_Node_For_Itype (New_Ctyp, U_Ent);
2927 Set_Component_Type (Btype, New_Ctyp);
2928 Set_Biased (New_Ctyp, N, "component size clause");
2931 Set_Component_Size (Btype, Csize);
2933 -- For VM case, we ignore component size clauses
2936 -- Give a warning unless we are in GNAT mode, in which case
2937 -- the warning is suppressed since it is not useful.
2939 if not GNAT_Mode then
2941 ("?component size ignored in this configuration", N);
2945 -- Deal with warning on overridden size
2947 if Warn_On_Overridden_Size
2948 and then Has_Size_Clause (Ctyp)
2949 and then RM_Size (Ctyp) /= Csize
2952 ("?component size overrides size clause for&",
2956 Set_Has_Component_Size_Clause (Btype, True);
2957 Set_Has_Non_Standard_Rep (Btype, True);
2959 end Component_Size_Case;
2961 -----------------------
2962 -- Constant_Indexing --
2963 -----------------------
2965 when Attribute_Constant_Indexing =>
2966 Check_Indexing_Functions;
2972 when Attribute_CPU => CPU :
2974 -- CPU attribute definition clause not allowed except from aspect
2977 if From_Aspect_Specification (N) then
2978 if not Is_Task_Type (U_Ent) then
2979 Error_Msg_N ("CPU can only be defined for task", Nam);
2981 elsif Duplicate_Clause then
2985 -- The expression must be analyzed in the special manner
2986 -- described in "Handling of Default and Per-Object
2987 -- Expressions" in sem.ads.
2989 -- The visibility to the discriminants must be restored
2991 Push_Scope_And_Install_Discriminants (U_Ent);
2992 Preanalyze_Spec_Expression (Expr, RTE (RE_CPU_Range));
2993 Uninstall_Discriminants_And_Pop_Scope (U_Ent);
2995 if not Is_Static_Expression (Expr) then
2996 Check_Restriction (Static_Priorities, Expr);
3002 ("attribute& cannot be set with definition clause", N);
3006 ----------------------
3007 -- Default_Iterator --
3008 ----------------------
3010 when Attribute_Default_Iterator => Default_Iterator : declare
3014 if not Is_Tagged_Type (U_Ent) then
3016 ("aspect Default_Iterator applies to tagged type", Nam);
3019 Check_Iterator_Functions;
3023 if not Is_Entity_Name (Expr)
3024 or else Ekind (Entity (Expr)) /= E_Function
3026 Error_Msg_N ("aspect Iterator must be a function", Expr);
3028 Func := Entity (Expr);
3031 if No (First_Formal (Func))
3032 or else Etype (First_Formal (Func)) /= U_Ent
3035 ("Default Iterator must be a primitive of&", Func, U_Ent);
3037 end Default_Iterator;
3039 ------------------------
3040 -- Dispatching_Domain --
3041 ------------------------
3043 when Attribute_Dispatching_Domain => Dispatching_Domain :
3045 -- Dispatching_Domain attribute definition clause not allowed
3046 -- except from aspect specification.
3048 if From_Aspect_Specification (N) then
3049 if not Is_Task_Type (U_Ent) then
3050 Error_Msg_N ("Dispatching_Domain can only be defined" &
3054 elsif Duplicate_Clause then
3058 -- The expression must be analyzed in the special manner
3059 -- described in "Handling of Default and Per-Object
3060 -- Expressions" in sem.ads.
3062 -- The visibility to the discriminants must be restored
3064 Push_Scope_And_Install_Discriminants (U_Ent);
3066 Preanalyze_Spec_Expression
3067 (Expr, RTE (RE_Dispatching_Domain));
3069 Uninstall_Discriminants_And_Pop_Scope (U_Ent);
3074 ("attribute& cannot be set with definition clause", N);
3076 end Dispatching_Domain;
3082 when Attribute_External_Tag => External_Tag :
3084 if not Is_Tagged_Type (U_Ent) then
3085 Error_Msg_N ("should be a tagged type", Nam);
3088 if Duplicate_Clause then
3092 Analyze_And_Resolve (Expr, Standard_String);
3094 if not Is_Static_Expression (Expr) then
3095 Flag_Non_Static_Expr
3096 ("static string required for tag name!", Nam);
3099 if VM_Target = No_VM then
3100 Set_Has_External_Tag_Rep_Clause (U_Ent);
3102 Error_Msg_Name_1 := Attr;
3104 ("% attribute unsupported in this configuration", Nam);
3107 if not Is_Library_Level_Entity (U_Ent) then
3109 ("?non-unique external tag supplied for &", N, U_Ent);
3111 ("?\same external tag applies to all subprogram calls", N);
3113 ("?\corresponding internal tag cannot be obtained", N);
3118 --------------------------
3119 -- Implicit_Dereference --
3120 --------------------------
3122 when Attribute_Implicit_Dereference =>
3124 -- Legality checks already performed at the point of the type
3125 -- declaration, aspect is not delayed.
3133 when Attribute_Input =>
3134 Analyze_Stream_TSS_Definition (TSS_Stream_Input);
3135 Set_Has_Specified_Stream_Input (Ent);
3137 ------------------------
3138 -- Interrupt_Priority --
3139 ------------------------
3141 when Attribute_Interrupt_Priority => Interrupt_Priority :
3143 -- Interrupt_Priority attribute definition clause not allowed
3144 -- except from aspect specification.
3146 if From_Aspect_Specification (N) then
3147 if not (Is_Protected_Type (U_Ent)
3148 or else Is_Task_Type (U_Ent))
3151 ("Interrupt_Priority can only be defined for task" &
3152 "and protected object",
3155 elsif Duplicate_Clause then
3159 -- The expression must be analyzed in the special manner
3160 -- described in "Handling of Default and Per-Object
3161 -- Expressions" in sem.ads.
3163 -- The visibility to the discriminants must be restored
3165 Push_Scope_And_Install_Discriminants (U_Ent);
3167 Preanalyze_Spec_Expression
3168 (Expr, RTE (RE_Interrupt_Priority));
3170 Uninstall_Discriminants_And_Pop_Scope (U_Ent);
3175 ("attribute& cannot be set with definition clause", N);
3177 end Interrupt_Priority;
3179 ----------------------
3180 -- Iterator_Element --
3181 ----------------------
3183 when Attribute_Iterator_Element =>
3186 if not Is_Entity_Name (Expr)
3187 or else not Is_Type (Entity (Expr))
3189 Error_Msg_N ("aspect Iterator_Element must be a type", Expr);
3196 -- Machine radix attribute definition clause
3198 when Attribute_Machine_Radix => Machine_Radix : declare
3199 Radix : constant Uint := Static_Integer (Expr);
3202 if not Is_Decimal_Fixed_Point_Type (U_Ent) then
3203 Error_Msg_N ("decimal fixed-point type expected for &", Nam);
3205 elsif Duplicate_Clause then
3208 elsif Radix /= No_Uint then
3209 Set_Has_Machine_Radix_Clause (U_Ent);
3210 Set_Has_Non_Standard_Rep (Base_Type (U_Ent));
3214 elsif Radix = 10 then
3215 Set_Machine_Radix_10 (U_Ent);
3217 Error_Msg_N ("machine radix value must be 2 or 10", Expr);
3226 -- Object_Size attribute definition clause
3228 when Attribute_Object_Size => Object_Size : declare
3229 Size : constant Uint := Static_Integer (Expr);
3232 pragma Warnings (Off, Biased);
3235 if not Is_Type (U_Ent) then
3236 Error_Msg_N ("Object_Size cannot be given for &", Nam);
3238 elsif Duplicate_Clause then
3242 Check_Size (Expr, U_Ent, Size, Biased);
3250 UI_Mod (Size, 64) /= 0
3253 ("Object_Size must be 8, 16, 32, or multiple of 64",
3257 Set_Esize (U_Ent, Size);
3258 Set_Has_Object_Size_Clause (U_Ent);
3259 Alignment_Check_For_Size_Change (U_Ent, Size);
3267 when Attribute_Output =>
3268 Analyze_Stream_TSS_Definition (TSS_Stream_Output);
3269 Set_Has_Specified_Stream_Output (Ent);
3275 when Attribute_Priority => Priority :
3277 -- Priority attribute definition clause not allowed except from
3278 -- aspect specification.
3280 if From_Aspect_Specification (N) then
3281 if not (Is_Protected_Type (U_Ent)
3282 or else Is_Task_Type (U_Ent)
3283 or else Ekind (U_Ent) = E_Procedure)
3286 ("Priority can only be defined for task and protected " &
3290 elsif Duplicate_Clause then
3294 -- The expression must be analyzed in the special manner
3295 -- described in "Handling of Default and Per-Object
3296 -- Expressions" in sem.ads.
3298 -- The visibility to the discriminants must be restored
3300 Push_Scope_And_Install_Discriminants (U_Ent);
3301 Preanalyze_Spec_Expression (Expr, Standard_Integer);
3302 Uninstall_Discriminants_And_Pop_Scope (U_Ent);
3304 if not Is_Static_Expression (Expr) then
3305 Check_Restriction (Static_Priorities, Expr);
3311 ("attribute& cannot be set with definition clause", N);
3319 when Attribute_Read =>
3320 Analyze_Stream_TSS_Definition (TSS_Stream_Read);
3321 Set_Has_Specified_Stream_Read (Ent);
3323 --------------------------
3324 -- Scalar_Storage_Order --
3325 --------------------------
3327 -- Scalar_Storage_Order attribute definition clause
3329 when Attribute_Scalar_Storage_Order => Scalar_Storage_Order : declare
3331 if not (Is_Record_Type (U_Ent) or else Is_Array_Type (U_Ent)) then
3333 ("Scalar_Storage_Order can only be defined for "
3334 & "record or array type", Nam);
3336 elsif Duplicate_Clause then
3340 Analyze_And_Resolve (Expr, RTE (RE_Bit_Order));
3342 if Etype (Expr) = Any_Type then
3345 elsif not Is_Static_Expression (Expr) then
3346 Flag_Non_Static_Expr
3347 ("Scalar_Storage_Order requires static expression!", Expr);
3350 if (Expr_Value (Expr) = 0) /= Bytes_Big_Endian then
3351 Set_Reverse_Storage_Order (Base_Type (U_Ent), True);
3355 end Scalar_Storage_Order;
3361 -- Size attribute definition clause
3363 when Attribute_Size => Size : declare
3364 Size : constant Uint := Static_Integer (Expr);
3371 if Duplicate_Clause then
3374 elsif not Is_Type (U_Ent)
3375 and then Ekind (U_Ent) /= E_Variable
3376 and then Ekind (U_Ent) /= E_Constant
3378 Error_Msg_N ("size cannot be given for &", Nam);
3380 elsif Is_Array_Type (U_Ent)
3381 and then not Is_Constrained (U_Ent)
3384 ("size cannot be given for unconstrained array", Nam);
3386 elsif Size /= No_Uint then
3387 if VM_Target /= No_VM and then not GNAT_Mode then
3389 -- Size clause is not handled properly on VM targets.
3390 -- Display a warning unless we are in GNAT mode, in which
3391 -- case this is useless.
3394 ("?size clauses are ignored in this configuration", N);
3397 if Is_Type (U_Ent) then
3400 Etyp := Etype (U_Ent);
3403 -- Check size, note that Gigi is in charge of checking that the
3404 -- size of an array or record type is OK. Also we do not check
3405 -- the size in the ordinary fixed-point case, since it is too
3406 -- early to do so (there may be subsequent small clause that
3407 -- affects the size). We can check the size if a small clause
3408 -- has already been given.
3410 if not Is_Ordinary_Fixed_Point_Type (U_Ent)
3411 or else Has_Small_Clause (U_Ent)
3413 Check_Size (Expr, Etyp, Size, Biased);
3414 Set_Biased (U_Ent, N, "size clause", Biased);
3417 -- For types set RM_Size and Esize if possible
3419 if Is_Type (U_Ent) then
3420 Set_RM_Size (U_Ent, Size);
3422 -- For elementary types, increase Object_Size to power of 2,
3423 -- but not less than a storage unit in any case (normally
3424 -- this means it will be byte addressable).
3426 -- For all other types, nothing else to do, we leave Esize
3427 -- (object size) unset, the back end will set it from the
3428 -- size and alignment in an appropriate manner.
3430 -- In both cases, we check whether the alignment must be
3431 -- reset in the wake of the size change.
3433 if Is_Elementary_Type (U_Ent) then
3434 if Size <= System_Storage_Unit then
3435 Init_Esize (U_Ent, System_Storage_Unit);
3436 elsif Size <= 16 then
3437 Init_Esize (U_Ent, 16);
3438 elsif Size <= 32 then
3439 Init_Esize (U_Ent, 32);
3441 Set_Esize (U_Ent, (Size + 63) / 64 * 64);
3444 Alignment_Check_For_Size_Change (U_Ent, Esize (U_Ent));
3446 Alignment_Check_For_Size_Change (U_Ent, Size);
3449 -- For objects, set Esize only
3452 if Is_Elementary_Type (Etyp) then
3453 if Size /= System_Storage_Unit
3455 Size /= System_Storage_Unit * 2
3457 Size /= System_Storage_Unit * 4
3459 Size /= System_Storage_Unit * 8
3461 Error_Msg_Uint_1 := UI_From_Int (System_Storage_Unit);
3462 Error_Msg_Uint_2 := Error_Msg_Uint_1 * 8;
3464 ("size for primitive object must be a power of 2"
3465 & " in the range ^-^", N);
3469 Set_Esize (U_Ent, Size);
3472 Set_Has_Size_Clause (U_Ent);
3480 -- Small attribute definition clause
3482 when Attribute_Small => Small : declare
3483 Implicit_Base : constant Entity_Id := Base_Type (U_Ent);
3487 Analyze_And_Resolve (Expr, Any_Real);
3489 if Etype (Expr) = Any_Type then
3492 elsif not Is_Static_Expression (Expr) then
3493 Flag_Non_Static_Expr
3494 ("small requires static expression!", Expr);
3498 Small := Expr_Value_R (Expr);
3500 if Small <= Ureal_0 then
3501 Error_Msg_N ("small value must be greater than zero", Expr);
3507 if not Is_Ordinary_Fixed_Point_Type (U_Ent) then
3509 ("small requires an ordinary fixed point type", Nam);
3511 elsif Has_Small_Clause (U_Ent) then
3512 Error_Msg_N ("small already given for &", Nam);
3514 elsif Small > Delta_Value (U_Ent) then
3516 ("small value must not be greater than delta value", Nam);
3519 Set_Small_Value (U_Ent, Small);
3520 Set_Small_Value (Implicit_Base, Small);
3521 Set_Has_Small_Clause (U_Ent);
3522 Set_Has_Small_Clause (Implicit_Base);
3523 Set_Has_Non_Standard_Rep (Implicit_Base);
3531 -- Storage_Pool attribute definition clause
3533 when Attribute_Storage_Pool | Attribute_Simple_Storage_Pool => declare
3538 if Ekind (U_Ent) = E_Access_Subprogram_Type then
3540 ("storage pool cannot be given for access-to-subprogram type",
3545 Ekind_In (U_Ent, E_Access_Type, E_General_Access_Type)
3548 ("storage pool can only be given for access types", Nam);
3551 elsif Is_Derived_Type (U_Ent) then
3553 ("storage pool cannot be given for a derived access type",
3556 elsif Duplicate_Clause then
3559 elsif Present (Associated_Storage_Pool (U_Ent)) then
3560 Error_Msg_N ("storage pool already given for &", Nam);
3564 if Id = Attribute_Storage_Pool then
3566 (Expr, Class_Wide_Type (RTE (RE_Root_Storage_Pool)));
3568 -- In the Simple_Storage_Pool case, we allow a variable of any
3569 -- simple storage pool type, so we Resolve without imposing an
3573 Analyze_And_Resolve (Expr);
3575 if not Present (Get_Rep_Pragma
3576 (Etype (Expr), Name_Simple_Storage_Pool_Type))
3579 ("expression must be of a simple storage pool type", Expr);
3583 if not Denotes_Variable (Expr) then
3584 Error_Msg_N ("storage pool must be a variable", Expr);
3588 if Nkind (Expr) = N_Type_Conversion then
3589 T := Etype (Expression (Expr));
3594 -- The Stack_Bounded_Pool is used internally for implementing
3595 -- access types with a Storage_Size. Since it only work properly
3596 -- when used on one specific type, we need to check that it is not
3597 -- hijacked improperly:
3599 -- type T is access Integer;
3600 -- for T'Storage_Size use n;
3601 -- type Q is access Float;
3602 -- for Q'Storage_Size use T'Storage_Size; -- incorrect
3604 if RTE_Available (RE_Stack_Bounded_Pool)
3605 and then Base_Type (T) = RTE (RE_Stack_Bounded_Pool)
3607 Error_Msg_N ("non-shareable internal Pool", Expr);
3611 -- If the argument is a name that is not an entity name, then
3612 -- we construct a renaming operation to define an entity of
3613 -- type storage pool.
3615 if not Is_Entity_Name (Expr)
3616 and then Is_Object_Reference (Expr)
3618 Pool := Make_Temporary (Loc, 'P', Expr);
3621 Rnode : constant Node_Id :=
3622 Make_Object_Renaming_Declaration (Loc,
3623 Defining_Identifier => Pool,
3625 New_Occurrence_Of (Etype (Expr), Loc),
3629 Insert_Before (N, Rnode);
3631 Set_Associated_Storage_Pool (U_Ent, Pool);
3634 elsif Is_Entity_Name (Expr) then
3635 Pool := Entity (Expr);
3637 -- If pool is a renamed object, get original one. This can
3638 -- happen with an explicit renaming, and within instances.
3640 while Present (Renamed_Object (Pool))
3641 and then Is_Entity_Name (Renamed_Object (Pool))
3643 Pool := Entity (Renamed_Object (Pool));
3646 if Present (Renamed_Object (Pool))
3647 and then Nkind (Renamed_Object (Pool)) = N_Type_Conversion
3648 and then Is_Entity_Name (Expression (Renamed_Object (Pool)))
3650 Pool := Entity (Expression (Renamed_Object (Pool)));
3653 Set_Associated_Storage_Pool (U_Ent, Pool);
3655 elsif Nkind (Expr) = N_Type_Conversion
3656 and then Is_Entity_Name (Expression (Expr))
3657 and then Nkind (Original_Node (Expr)) = N_Attribute_Reference
3659 Pool := Entity (Expression (Expr));
3660 Set_Associated_Storage_Pool (U_Ent, Pool);
3663 Error_Msg_N ("incorrect reference to a Storage Pool", Expr);
3672 -- Storage_Size attribute definition clause
3674 when Attribute_Storage_Size => Storage_Size : declare
3675 Btype : constant Entity_Id := Base_Type (U_Ent);
3678 if Is_Task_Type (U_Ent) then
3679 Check_Restriction (No_Obsolescent_Features, N);
3681 if Warn_On_Obsolescent_Feature then
3683 ("storage size clause for task is an " &
3684 "obsolescent feature (RM J.9)?", N);
3685 Error_Msg_N ("\use Storage_Size pragma instead?", N);
3691 if not Is_Access_Type (U_Ent)
3692 and then Ekind (U_Ent) /= E_Task_Type
3694 Error_Msg_N ("storage size cannot be given for &", Nam);
3696 elsif Is_Access_Type (U_Ent) and Is_Derived_Type (U_Ent) then
3698 ("storage size cannot be given for a derived access type",
3701 elsif Duplicate_Clause then
3705 Analyze_And_Resolve (Expr, Any_Integer);
3707 if Is_Access_Type (U_Ent) then
3708 if Present (Associated_Storage_Pool (U_Ent)) then
3709 Error_Msg_N ("storage pool already given for &", Nam);
3713 if Is_OK_Static_Expression (Expr)
3714 and then Expr_Value (Expr) = 0
3716 Set_No_Pool_Assigned (Btype);
3720 Set_Has_Storage_Size_Clause (Btype);
3728 when Attribute_Stream_Size => Stream_Size : declare
3729 Size : constant Uint := Static_Integer (Expr);
3732 if Ada_Version <= Ada_95 then
3733 Check_Restriction (No_Implementation_Attributes, N);
3736 if Duplicate_Clause then
3739 elsif Is_Elementary_Type (U_Ent) then
3740 if Size /= System_Storage_Unit
3742 Size /= System_Storage_Unit * 2
3744 Size /= System_Storage_Unit * 4
3746 Size /= System_Storage_Unit * 8
3748 Error_Msg_Uint_1 := UI_From_Int (System_Storage_Unit);
3750 ("stream size for elementary type must be a"
3751 & " power of 2 and at least ^", N);
3753 elsif RM_Size (U_Ent) > Size then
3754 Error_Msg_Uint_1 := RM_Size (U_Ent);
3756 ("stream size for elementary type must be a"
3757 & " power of 2 and at least ^", N);
3760 Set_Has_Stream_Size_Clause (U_Ent);
3763 Error_Msg_N ("Stream_Size cannot be given for &", Nam);
3771 -- Value_Size attribute definition clause
3773 when Attribute_Value_Size => Value_Size : declare
3774 Size : constant Uint := Static_Integer (Expr);
3778 if not Is_Type (U_Ent) then
3779 Error_Msg_N ("Value_Size cannot be given for &", Nam);
3781 elsif Duplicate_Clause then
3784 elsif Is_Array_Type (U_Ent)
3785 and then not Is_Constrained (U_Ent)
3788 ("Value_Size cannot be given for unconstrained array", Nam);
3791 if Is_Elementary_Type (U_Ent) then
3792 Check_Size (Expr, U_Ent, Size, Biased);
3793 Set_Biased (U_Ent, N, "value size clause", Biased);
3796 Set_RM_Size (U_Ent, Size);
3800 -----------------------
3801 -- Variable_Indexing --
3802 -----------------------
3804 when Attribute_Variable_Indexing =>
3805 Check_Indexing_Functions;
3811 when Attribute_Write =>
3812 Analyze_Stream_TSS_Definition (TSS_Stream_Write);
3813 Set_Has_Specified_Stream_Write (Ent);
3815 -- All other attributes cannot be set
3819 ("attribute& cannot be set with definition clause", N);
3822 -- The test for the type being frozen must be performed after any
3823 -- expression the clause has been analyzed since the expression itself
3824 -- might cause freezing that makes the clause illegal.
3826 if Rep_Item_Too_Late (U_Ent, N, FOnly) then
3829 end Analyze_Attribute_Definition_Clause;
3831 ----------------------------
3832 -- Analyze_Code_Statement --
3833 ----------------------------
3835 procedure Analyze_Code_Statement (N : Node_Id) is
3836 HSS : constant Node_Id := Parent (N);
3837 SBody : constant Node_Id := Parent (HSS);
3838 Subp : constant Entity_Id := Current_Scope;
3845 -- Analyze and check we get right type, note that this implements the
3846 -- requirement (RM 13.8(1)) that Machine_Code be with'ed, since that
3847 -- is the only way that Asm_Insn could possibly be visible.
3849 Analyze_And_Resolve (Expression (N));
3851 if Etype (Expression (N)) = Any_Type then
3853 elsif Etype (Expression (N)) /= RTE (RE_Asm_Insn) then
3854 Error_Msg_N ("incorrect type for code statement", N);
3858 Check_Code_Statement (N);
3860 -- Make sure we appear in the handled statement sequence of a
3861 -- subprogram (RM 13.8(3)).
3863 if Nkind (HSS) /= N_Handled_Sequence_Of_Statements
3864 or else Nkind (SBody) /= N_Subprogram_Body
3867 ("code statement can only appear in body of subprogram", N);
3871 -- Do remaining checks (RM 13.8(3)) if not already done
3873 if not Is_Machine_Code_Subprogram (Subp) then
3874 Set_Is_Machine_Code_Subprogram (Subp);
3876 -- No exception handlers allowed
3878 if Present (Exception_Handlers (HSS)) then
3880 ("exception handlers not permitted in machine code subprogram",
3881 First (Exception_Handlers (HSS)));
3884 -- No declarations other than use clauses and pragmas (we allow
3885 -- certain internally generated declarations as well).
3887 Decl := First (Declarations (SBody));
3888 while Present (Decl) loop
3889 DeclO := Original_Node (Decl);
3890 if Comes_From_Source (DeclO)
3891 and not Nkind_In (DeclO, N_Pragma,
3892 N_Use_Package_Clause,
3894 N_Implicit_Label_Declaration)
3897 ("this declaration not allowed in machine code subprogram",
3904 -- No statements other than code statements, pragmas, and labels.
3905 -- Again we allow certain internally generated statements.
3907 -- In Ada 2012, qualified expressions are names, and the code
3908 -- statement is initially parsed as a procedure call.
3910 Stmt := First (Statements (HSS));
3911 while Present (Stmt) loop
3912 StmtO := Original_Node (Stmt);
3914 -- A procedure call transformed into a code statement is OK.
3916 if Ada_Version >= Ada_2012
3917 and then Nkind (StmtO) = N_Procedure_Call_Statement
3918 and then Nkind (Name (StmtO)) = N_Qualified_Expression
3922 elsif Comes_From_Source (StmtO)
3923 and then not Nkind_In (StmtO, N_Pragma,
3928 ("this statement is not allowed in machine code subprogram",
3935 end Analyze_Code_Statement;
3937 -----------------------------------------------
3938 -- Analyze_Enumeration_Representation_Clause --
3939 -----------------------------------------------
3941 procedure Analyze_Enumeration_Representation_Clause (N : Node_Id) is
3942 Ident : constant Node_Id := Identifier (N);
3943 Aggr : constant Node_Id := Array_Aggregate (N);
3944 Enumtype : Entity_Id;
3951 Err : Boolean := False;
3952 -- Set True to avoid cascade errors and crashes on incorrect source code
3954 Lo : constant Uint := Expr_Value (Type_Low_Bound (Universal_Integer));
3955 Hi : constant Uint := Expr_Value (Type_High_Bound (Universal_Integer));
3956 -- Allowed range of universal integer (= allowed range of enum lit vals)
3960 -- Minimum and maximum values of entries
3963 -- Pointer to node for literal providing max value
3966 if Ignore_Rep_Clauses then
3970 -- First some basic error checks
3973 Enumtype := Entity (Ident);
3975 if Enumtype = Any_Type
3976 or else Rep_Item_Too_Early (Enumtype, N)
3980 Enumtype := Underlying_Type (Enumtype);
3983 if not Is_Enumeration_Type (Enumtype) then
3985 ("enumeration type required, found}",
3986 Ident, First_Subtype (Enumtype));
3990 -- Ignore rep clause on generic actual type. This will already have
3991 -- been flagged on the template as an error, and this is the safest
3992 -- way to ensure we don't get a junk cascaded message in the instance.
3994 if Is_Generic_Actual_Type (Enumtype) then
3997 -- Type must be in current scope
3999 elsif Scope (Enumtype) /= Current_Scope then
4000 Error_Msg_N ("type must be declared in this scope", Ident);
4003 -- Type must be a first subtype
4005 elsif not Is_First_Subtype (Enumtype) then
4006 Error_Msg_N ("cannot give enumeration rep clause for subtype", N);
4009 -- Ignore duplicate rep clause
4011 elsif Has_Enumeration_Rep_Clause (Enumtype) then
4012 Error_Msg_N ("duplicate enumeration rep clause ignored", N);
4015 -- Don't allow rep clause for standard [wide_[wide_]]character
4017 elsif Is_Standard_Character_Type (Enumtype) then
4018 Error_Msg_N ("enumeration rep clause not allowed for this type", N);
4021 -- Check that the expression is a proper aggregate (no parentheses)
4023 elsif Paren_Count (Aggr) /= 0 then
4025 ("extra parentheses surrounding aggregate not allowed",
4029 -- All tests passed, so set rep clause in place
4032 Set_Has_Enumeration_Rep_Clause (Enumtype);
4033 Set_Has_Enumeration_Rep_Clause (Base_Type (Enumtype));
4036 -- Now we process the aggregate. Note that we don't use the normal
4037 -- aggregate code for this purpose, because we don't want any of the
4038 -- normal expansion activities, and a number of special semantic
4039 -- rules apply (including the component type being any integer type)
4041 Elit := First_Literal (Enumtype);
4043 -- First the positional entries if any
4045 if Present (Expressions (Aggr)) then
4046 Expr := First (Expressions (Aggr));
4047 while Present (Expr) loop
4049 Error_Msg_N ("too many entries in aggregate", Expr);
4053 Val := Static_Integer (Expr);
4055 -- Err signals that we found some incorrect entries processing
4056 -- the list. The final checks for completeness and ordering are
4057 -- skipped in this case.
4059 if Val = No_Uint then
4061 elsif Val < Lo or else Hi < Val then
4062 Error_Msg_N ("value outside permitted range", Expr);
4066 Set_Enumeration_Rep (Elit, Val);
4067 Set_Enumeration_Rep_Expr (Elit, Expr);
4073 -- Now process the named entries if present
4075 if Present (Component_Associations (Aggr)) then
4076 Assoc := First (Component_Associations (Aggr));
4077 while Present (Assoc) loop
4078 Choice := First (Choices (Assoc));
4080 if Present (Next (Choice)) then
4082 ("multiple choice not allowed here", Next (Choice));
4086 if Nkind (Choice) = N_Others_Choice then
4087 Error_Msg_N ("others choice not allowed here", Choice);
4090 elsif Nkind (Choice) = N_Range then
4092 -- ??? should allow zero/one element range here
4094 Error_Msg_N ("range not allowed here", Choice);
4098 Analyze_And_Resolve (Choice, Enumtype);
4100 if Error_Posted (Choice) then
4105 if Is_Entity_Name (Choice)
4106 and then Is_Type (Entity (Choice))
4108 Error_Msg_N ("subtype name not allowed here", Choice);
4111 -- ??? should allow static subtype with zero/one entry
4113 elsif Etype (Choice) = Base_Type (Enumtype) then
4114 if not Is_Static_Expression (Choice) then
4115 Flag_Non_Static_Expr
4116 ("non-static expression used for choice!", Choice);
4120 Elit := Expr_Value_E (Choice);
4122 if Present (Enumeration_Rep_Expr (Elit)) then
4124 Sloc (Enumeration_Rep_Expr (Elit));
4126 ("representation for& previously given#",
4131 Set_Enumeration_Rep_Expr (Elit, Expression (Assoc));
4133 Expr := Expression (Assoc);
4134 Val := Static_Integer (Expr);
4136 if Val = No_Uint then
4139 elsif Val < Lo or else Hi < Val then
4140 Error_Msg_N ("value outside permitted range", Expr);
4144 Set_Enumeration_Rep (Elit, Val);
4154 -- Aggregate is fully processed. Now we check that a full set of
4155 -- representations was given, and that they are in range and in order.
4156 -- These checks are only done if no other errors occurred.
4162 Elit := First_Literal (Enumtype);
4163 while Present (Elit) loop
4164 if No (Enumeration_Rep_Expr (Elit)) then
4165 Error_Msg_NE ("missing representation for&!", N, Elit);
4168 Val := Enumeration_Rep (Elit);
4170 if Min = No_Uint then
4174 if Val /= No_Uint then
4175 if Max /= No_Uint and then Val <= Max then
4177 ("enumeration value for& not ordered!",
4178 Enumeration_Rep_Expr (Elit), Elit);
4181 Max_Node := Enumeration_Rep_Expr (Elit);
4185 -- If there is at least one literal whose representation is not
4186 -- equal to the Pos value, then note that this enumeration type
4187 -- has a non-standard representation.
4189 if Val /= Enumeration_Pos (Elit) then
4190 Set_Has_Non_Standard_Rep (Base_Type (Enumtype));
4197 -- Now set proper size information
4200 Minsize : Uint := UI_From_Int (Minimum_Size (Enumtype));
4203 if Has_Size_Clause (Enumtype) then
4205 -- All OK, if size is OK now
4207 if RM_Size (Enumtype) >= Minsize then
4211 -- Try if we can get by with biasing
4214 UI_From_Int (Minimum_Size (Enumtype, Biased => True));
4216 -- Error message if even biasing does not work
4218 if RM_Size (Enumtype) < Minsize then
4219 Error_Msg_Uint_1 := RM_Size (Enumtype);
4220 Error_Msg_Uint_2 := Max;
4222 ("previously given size (^) is too small "
4223 & "for this value (^)", Max_Node);
4225 -- If biasing worked, indicate that we now have biased rep
4229 (Enumtype, Size_Clause (Enumtype), "size clause");
4234 Set_RM_Size (Enumtype, Minsize);
4235 Set_Enum_Esize (Enumtype);
4238 Set_RM_Size (Base_Type (Enumtype), RM_Size (Enumtype));
4239 Set_Esize (Base_Type (Enumtype), Esize (Enumtype));
4240 Set_Alignment (Base_Type (Enumtype), Alignment (Enumtype));
4244 -- We repeat the too late test in case it froze itself!
4246 if Rep_Item_Too_Late (Enumtype, N) then
4249 end Analyze_Enumeration_Representation_Clause;
4251 ----------------------------
4252 -- Analyze_Free_Statement --
4253 ----------------------------
4255 procedure Analyze_Free_Statement (N : Node_Id) is
4257 Analyze (Expression (N));
4258 end Analyze_Free_Statement;
4260 ---------------------------
4261 -- Analyze_Freeze_Entity --
4262 ---------------------------
4264 procedure Analyze_Freeze_Entity (N : Node_Id) is
4265 E : constant Entity_Id := Entity (N);
4268 -- Remember that we are processing a freezing entity. Required to
4269 -- ensure correct decoration of internal entities associated with
4270 -- interfaces (see New_Overloaded_Entity).
4272 Inside_Freezing_Actions := Inside_Freezing_Actions + 1;
4274 -- For tagged types covering interfaces add internal entities that link
4275 -- the primitives of the interfaces with the primitives that cover them.
4276 -- Note: These entities were originally generated only when generating
4277 -- code because their main purpose was to provide support to initialize
4278 -- the secondary dispatch tables. They are now generated also when
4279 -- compiling with no code generation to provide ASIS the relationship
4280 -- between interface primitives and tagged type primitives. They are
4281 -- also used to locate primitives covering interfaces when processing
4282 -- generics (see Derive_Subprograms).
4284 if Ada_Version >= Ada_2005
4285 and then Ekind (E) = E_Record_Type
4286 and then Is_Tagged_Type (E)
4287 and then not Is_Interface (E)
4288 and then Has_Interfaces (E)
4290 -- This would be a good common place to call the routine that checks
4291 -- overriding of interface primitives (and thus factorize calls to
4292 -- Check_Abstract_Overriding located at different contexts in the
4293 -- compiler). However, this is not possible because it causes
4294 -- spurious errors in case of late overriding.
4296 Add_Internal_Interface_Entities (E);
4301 if Ekind (E) = E_Record_Type
4302 and then Is_CPP_Class (E)
4303 and then Is_Tagged_Type (E)
4304 and then Tagged_Type_Expansion
4305 and then Expander_Active
4307 if CPP_Num_Prims (E) = 0 then
4309 -- If the CPP type has user defined components then it must import
4310 -- primitives from C++. This is required because if the C++ class
4311 -- has no primitives then the C++ compiler does not added the _tag
4312 -- component to the type.
4314 pragma Assert (Chars (First_Entity (E)) = Name_uTag);
4316 if First_Entity (E) /= Last_Entity (E) then
4318 ("?'C'P'P type must import at least one primitive from C++",
4323 -- Check that all its primitives are abstract or imported from C++.
4324 -- Check also availability of the C++ constructor.
4327 Has_Constructors : constant Boolean := Has_CPP_Constructors (E);
4329 Error_Reported : Boolean := False;
4333 Elmt := First_Elmt (Primitive_Operations (E));
4334 while Present (Elmt) loop
4335 Prim := Node (Elmt);
4337 if Comes_From_Source (Prim) then
4338 if Is_Abstract_Subprogram (Prim) then
4341 elsif not Is_Imported (Prim)
4342 or else Convention (Prim) /= Convention_CPP
4345 ("?primitives of 'C'P'P types must be imported from C++"
4346 & " or abstract", Prim);
4348 elsif not Has_Constructors
4349 and then not Error_Reported
4351 Error_Msg_Name_1 := Chars (E);
4353 ("?'C'P'P constructor required for type %", Prim);
4354 Error_Reported := True;
4363 -- Check Ada derivation of CPP type
4366 and then Tagged_Type_Expansion
4367 and then Ekind (E) = E_Record_Type
4368 and then Etype (E) /= E
4369 and then Is_CPP_Class (Etype (E))
4370 and then CPP_Num_Prims (Etype (E)) > 0
4371 and then not Is_CPP_Class (E)
4372 and then not Has_CPP_Constructors (Etype (E))
4374 -- If the parent has C++ primitives but it has no constructor then
4375 -- check that all the primitives are overridden in this derivation;
4376 -- otherwise the constructor of the parent is needed to build the
4384 Elmt := First_Elmt (Primitive_Operations (E));
4385 while Present (Elmt) loop
4386 Prim := Node (Elmt);
4388 if not Is_Abstract_Subprogram (Prim)
4389 and then No (Interface_Alias (Prim))
4390 and then Find_Dispatching_Type (Ultimate_Alias (Prim)) /= E
4392 Error_Msg_Name_1 := Chars (Etype (E));
4394 ("'C'P'P constructor required for parent type %", E);
4403 Inside_Freezing_Actions := Inside_Freezing_Actions - 1;
4405 -- If we have a type with predicates, build predicate function
4407 if Is_Type (E) and then Has_Predicates (E) then
4408 Build_Predicate_Function (E, N);
4411 -- If type has delayed aspects, this is where we do the preanalysis at
4412 -- the freeze point, as part of the consistent visibility check. Note
4413 -- that this must be done after calling Build_Predicate_Function or
4414 -- Build_Invariant_Procedure since these subprograms fix occurrences of
4415 -- the subtype name in the saved expression so that they will not cause
4416 -- trouble in the preanalysis.
4418 if Has_Delayed_Aspects (E)
4419 and then Scope (E) = Current_Scope
4421 -- Retrieve the visibility to the discriminants in order to properly
4422 -- analyze the aspects.
4424 Push_Scope_And_Install_Discriminants (E);
4430 -- Look for aspect specification entries for this entity
4432 Ritem := First_Rep_Item (E);
4433 while Present (Ritem) loop
4434 if Nkind (Ritem) = N_Aspect_Specification
4435 and then Entity (Ritem) = E
4436 and then Is_Delayed_Aspect (Ritem)
4438 Check_Aspect_At_Freeze_Point (Ritem);
4441 Next_Rep_Item (Ritem);
4445 Uninstall_Discriminants_And_Pop_Scope (E);
4447 end Analyze_Freeze_Entity;
4449 ------------------------------------------
4450 -- Analyze_Record_Representation_Clause --
4451 ------------------------------------------
4453 -- Note: we check as much as we can here, but we can't do any checks
4454 -- based on the position values (e.g. overlap checks) until freeze time
4455 -- because especially in Ada 2005 (machine scalar mode), the processing
4456 -- for non-standard bit order can substantially change the positions.
4457 -- See procedure Check_Record_Representation_Clause (called from Freeze)
4458 -- for the remainder of this processing.
4460 procedure Analyze_Record_Representation_Clause (N : Node_Id) is
4461 Ident : constant Node_Id := Identifier (N);
4466 Hbit : Uint := Uint_0;
4470 Rectype : Entity_Id;
4472 CR_Pragma : Node_Id := Empty;
4473 -- Points to N_Pragma node if Complete_Representation pragma present
4476 if Ignore_Rep_Clauses then
4481 Rectype := Entity (Ident);
4483 if Rectype = Any_Type
4484 or else Rep_Item_Too_Early (Rectype, N)
4488 Rectype := Underlying_Type (Rectype);
4491 -- First some basic error checks
4493 if not Is_Record_Type (Rectype) then
4495 ("record type required, found}", Ident, First_Subtype (Rectype));
4498 elsif Scope (Rectype) /= Current_Scope then
4499 Error_Msg_N ("type must be declared in this scope", N);
4502 elsif not Is_First_Subtype (Rectype) then
4503 Error_Msg_N ("cannot give record rep clause for subtype", N);
4506 elsif Has_Record_Rep_Clause (Rectype) then
4507 Error_Msg_N ("duplicate record rep clause ignored", N);
4510 elsif Rep_Item_Too_Late (Rectype, N) then
4514 if Present (Mod_Clause (N)) then
4516 Loc : constant Source_Ptr := Sloc (N);
4517 M : constant Node_Id := Mod_Clause (N);
4518 P : constant List_Id := Pragmas_Before (M);
4522 pragma Warnings (Off, Mod_Val);
4525 Check_Restriction (No_Obsolescent_Features, Mod_Clause (N));
4527 if Warn_On_Obsolescent_Feature then
4529 ("mod clause is an obsolescent feature (RM J.8)?", N);
4531 ("\use alignment attribute definition clause instead?", N);
4538 -- In ASIS_Mode mode, expansion is disabled, but we must convert
4539 -- the Mod clause into an alignment clause anyway, so that the
4540 -- back-end can compute and back-annotate properly the size and
4541 -- alignment of types that may include this record.
4543 -- This seems dubious, this destroys the source tree in a manner
4544 -- not detectable by ASIS ???
4546 if Operating_Mode = Check_Semantics and then ASIS_Mode then
4548 Make_Attribute_Definition_Clause (Loc,
4549 Name => New_Reference_To (Base_Type (Rectype), Loc),
4550 Chars => Name_Alignment,
4551 Expression => Relocate_Node (Expression (M)));
4553 Set_From_At_Mod (AtM_Nod);
4554 Insert_After (N, AtM_Nod);
4555 Mod_Val := Get_Alignment_Value (Expression (AtM_Nod));
4556 Set_Mod_Clause (N, Empty);
4559 -- Get the alignment value to perform error checking
4561 Mod_Val := Get_Alignment_Value (Expression (M));
4566 -- For untagged types, clear any existing component clauses for the
4567 -- type. If the type is derived, this is what allows us to override
4568 -- a rep clause for the parent. For type extensions, the representation
4569 -- of the inherited components is inherited, so we want to keep previous
4570 -- component clauses for completeness.
4572 if not Is_Tagged_Type (Rectype) then
4573 Comp := First_Component_Or_Discriminant (Rectype);
4574 while Present (Comp) loop
4575 Set_Component_Clause (Comp, Empty);
4576 Next_Component_Or_Discriminant (Comp);
4580 -- All done if no component clauses
4582 CC := First (Component_Clauses (N));
4588 -- A representation like this applies to the base type
4590 Set_Has_Record_Rep_Clause (Base_Type (Rectype));
4591 Set_Has_Non_Standard_Rep (Base_Type (Rectype));
4592 Set_Has_Specified_Layout (Base_Type (Rectype));
4594 -- Process the component clauses
4596 while Present (CC) loop
4600 if Nkind (CC) = N_Pragma then
4603 -- The only pragma of interest is Complete_Representation
4605 if Pragma_Name (CC) = Name_Complete_Representation then
4609 -- Processing for real component clause
4612 Posit := Static_Integer (Position (CC));
4613 Fbit := Static_Integer (First_Bit (CC));
4614 Lbit := Static_Integer (Last_Bit (CC));
4617 and then Fbit /= No_Uint
4618 and then Lbit /= No_Uint
4622 ("position cannot be negative", Position (CC));
4626 ("first bit cannot be negative", First_Bit (CC));
4628 -- The Last_Bit specified in a component clause must not be
4629 -- less than the First_Bit minus one (RM-13.5.1(10)).
4631 elsif Lbit < Fbit - 1 then
4633 ("last bit cannot be less than first bit minus one",
4636 -- Values look OK, so find the corresponding record component
4637 -- Even though the syntax allows an attribute reference for
4638 -- implementation-defined components, GNAT does not allow the
4639 -- tag to get an explicit position.
4641 elsif Nkind (Component_Name (CC)) = N_Attribute_Reference then
4642 if Attribute_Name (Component_Name (CC)) = Name_Tag then
4643 Error_Msg_N ("position of tag cannot be specified", CC);
4645 Error_Msg_N ("illegal component name", CC);
4649 Comp := First_Entity (Rectype);
4650 while Present (Comp) loop
4651 exit when Chars (Comp) = Chars (Component_Name (CC));
4657 -- Maybe component of base type that is absent from
4658 -- statically constrained first subtype.
4660 Comp := First_Entity (Base_Type (Rectype));
4661 while Present (Comp) loop
4662 exit when Chars (Comp) = Chars (Component_Name (CC));
4669 ("component clause is for non-existent field", CC);
4671 -- Ada 2012 (AI05-0026): Any name that denotes a
4672 -- discriminant of an object of an unchecked union type
4673 -- shall not occur within a record_representation_clause.
4675 -- The general restriction of using record rep clauses on
4676 -- Unchecked_Union types has now been lifted. Since it is
4677 -- possible to introduce a record rep clause which mentions
4678 -- the discriminant of an Unchecked_Union in non-Ada 2012
4679 -- code, this check is applied to all versions of the
4682 elsif Ekind (Comp) = E_Discriminant
4683 and then Is_Unchecked_Union (Rectype)
4686 ("cannot reference discriminant of unchecked union",
4687 Component_Name (CC));
4689 elsif Present (Component_Clause (Comp)) then
4691 -- Diagnose duplicate rep clause, or check consistency
4692 -- if this is an inherited component. In a double fault,
4693 -- there may be a duplicate inconsistent clause for an
4694 -- inherited component.
4696 if Scope (Original_Record_Component (Comp)) = Rectype
4697 or else Parent (Component_Clause (Comp)) = N
4699 Error_Msg_Sloc := Sloc (Component_Clause (Comp));
4700 Error_Msg_N ("component clause previously given#", CC);
4704 Rep1 : constant Node_Id := Component_Clause (Comp);
4706 if Intval (Position (Rep1)) /=
4707 Intval (Position (CC))
4708 or else Intval (First_Bit (Rep1)) /=
4709 Intval (First_Bit (CC))
4710 or else Intval (Last_Bit (Rep1)) /=
4711 Intval (Last_Bit (CC))
4713 Error_Msg_N ("component clause inconsistent "
4714 & "with representation of ancestor", CC);
4715 elsif Warn_On_Redundant_Constructs then
4716 Error_Msg_N ("?redundant component clause "
4717 & "for inherited component!", CC);
4722 -- Normal case where this is the first component clause we
4723 -- have seen for this entity, so set it up properly.
4726 -- Make reference for field in record rep clause and set
4727 -- appropriate entity field in the field identifier.
4730 (Comp, Component_Name (CC), Set_Ref => False);
4731 Set_Entity (Component_Name (CC), Comp);
4733 -- Update Fbit and Lbit to the actual bit number
4735 Fbit := Fbit + UI_From_Int (SSU) * Posit;
4736 Lbit := Lbit + UI_From_Int (SSU) * Posit;
4738 if Has_Size_Clause (Rectype)
4739 and then RM_Size (Rectype) <= Lbit
4742 ("bit number out of range of specified size",
4745 Set_Component_Clause (Comp, CC);
4746 Set_Component_Bit_Offset (Comp, Fbit);
4747 Set_Esize (Comp, 1 + (Lbit - Fbit));
4748 Set_Normalized_First_Bit (Comp, Fbit mod SSU);
4749 Set_Normalized_Position (Comp, Fbit / SSU);
4751 if Warn_On_Overridden_Size
4752 and then Has_Size_Clause (Etype (Comp))
4753 and then RM_Size (Etype (Comp)) /= Esize (Comp)
4756 ("?component size overrides size clause for&",
4757 Component_Name (CC), Etype (Comp));
4760 -- This information is also set in the corresponding
4761 -- component of the base type, found by accessing the
4762 -- Original_Record_Component link if it is present.
4764 Ocomp := Original_Record_Component (Comp);
4771 (Component_Name (CC),
4777 (Comp, First_Node (CC), "component clause", Biased);
4779 if Present (Ocomp) then
4780 Set_Component_Clause (Ocomp, CC);
4781 Set_Component_Bit_Offset (Ocomp, Fbit);
4782 Set_Normalized_First_Bit (Ocomp, Fbit mod SSU);
4783 Set_Normalized_Position (Ocomp, Fbit / SSU);
4784 Set_Esize (Ocomp, 1 + (Lbit - Fbit));
4786 Set_Normalized_Position_Max
4787 (Ocomp, Normalized_Position (Ocomp));
4789 -- Note: we don't use Set_Biased here, because we
4790 -- already gave a warning above if needed, and we
4791 -- would get a duplicate for the same name here.
4793 Set_Has_Biased_Representation
4794 (Ocomp, Has_Biased_Representation (Comp));
4797 if Esize (Comp) < 0 then
4798 Error_Msg_N ("component size is negative", CC);
4809 -- Check missing components if Complete_Representation pragma appeared
4811 if Present (CR_Pragma) then
4812 Comp := First_Component_Or_Discriminant (Rectype);
4813 while Present (Comp) loop
4814 if No (Component_Clause (Comp)) then
4816 ("missing component clause for &", CR_Pragma, Comp);
4819 Next_Component_Or_Discriminant (Comp);
4822 -- If no Complete_Representation pragma, warn if missing components
4824 elsif Warn_On_Unrepped_Components then
4826 Num_Repped_Components : Nat := 0;
4827 Num_Unrepped_Components : Nat := 0;
4830 -- First count number of repped and unrepped components
4832 Comp := First_Component_Or_Discriminant (Rectype);
4833 while Present (Comp) loop
4834 if Present (Component_Clause (Comp)) then
4835 Num_Repped_Components := Num_Repped_Components + 1;
4837 Num_Unrepped_Components := Num_Unrepped_Components + 1;
4840 Next_Component_Or_Discriminant (Comp);
4843 -- We are only interested in the case where there is at least one
4844 -- unrepped component, and at least half the components have rep
4845 -- clauses. We figure that if less than half have them, then the
4846 -- partial rep clause is really intentional. If the component
4847 -- type has no underlying type set at this point (as for a generic
4848 -- formal type), we don't know enough to give a warning on the
4851 if Num_Unrepped_Components > 0
4852 and then Num_Unrepped_Components < Num_Repped_Components
4854 Comp := First_Component_Or_Discriminant (Rectype);
4855 while Present (Comp) loop
4856 if No (Component_Clause (Comp))
4857 and then Comes_From_Source (Comp)
4858 and then Present (Underlying_Type (Etype (Comp)))
4859 and then (Is_Scalar_Type (Underlying_Type (Etype (Comp)))
4860 or else Size_Known_At_Compile_Time
4861 (Underlying_Type (Etype (Comp))))
4862 and then not Has_Warnings_Off (Rectype)
4864 Error_Msg_Sloc := Sloc (Comp);
4866 ("?no component clause given for & declared #",
4870 Next_Component_Or_Discriminant (Comp);
4875 end Analyze_Record_Representation_Clause;
4877 -------------------------------
4878 -- Build_Invariant_Procedure --
4879 -------------------------------
4881 -- The procedure that is constructed here has the form
4883 -- procedure typInvariant (Ixxx : typ) is
4885 -- pragma Check (Invariant, exp, "failed invariant from xxx");
4886 -- pragma Check (Invariant, exp, "failed invariant from xxx");
4888 -- pragma Check (Invariant, exp, "failed inherited invariant from xxx");
4890 -- end typInvariant;
4892 procedure Build_Invariant_Procedure (Typ : Entity_Id; N : Node_Id) is
4893 Loc : constant Source_Ptr := Sloc (Typ);
4900 Visible_Decls : constant List_Id := Visible_Declarations (N);
4901 Private_Decls : constant List_Id := Private_Declarations (N);
4903 procedure Add_Invariants (T : Entity_Id; Inherit : Boolean);
4904 -- Appends statements to Stmts for any invariants in the rep item chain
4905 -- of the given type. If Inherit is False, then we only process entries
4906 -- on the chain for the type Typ. If Inherit is True, then we ignore any
4907 -- Invariant aspects, but we process all Invariant'Class aspects, adding
4908 -- "inherited" to the exception message and generating an informational
4909 -- message about the inheritance of an invariant.
4911 Object_Name : constant Name_Id := New_Internal_Name ('I');
4912 -- Name for argument of invariant procedure
4914 Object_Entity : constant Node_Id :=
4915 Make_Defining_Identifier (Loc, Object_Name);
4916 -- The procedure declaration entity for the argument
4918 --------------------
4919 -- Add_Invariants --
4920 --------------------
4922 procedure Add_Invariants (T : Entity_Id; Inherit : Boolean) is
4932 procedure Replace_Type_Reference (N : Node_Id);
4933 -- Replace a single occurrence N of the subtype name with a reference
4934 -- to the formal of the predicate function. N can be an identifier
4935 -- referencing the subtype, or a selected component, representing an
4936 -- appropriately qualified occurrence of the subtype name.
4938 procedure Replace_Type_References is
4939 new Replace_Type_References_Generic (Replace_Type_Reference);
4940 -- Traverse an expression replacing all occurrences of the subtype
4941 -- name with appropriate references to the object that is the formal
4942 -- parameter of the predicate function. Note that we must ensure
4943 -- that the type and entity information is properly set in the
4944 -- replacement node, since we will do a Preanalyze call of this
4945 -- expression without proper visibility of the procedure argument.
4947 ----------------------------
4948 -- Replace_Type_Reference --
4949 ----------------------------
4951 procedure Replace_Type_Reference (N : Node_Id) is
4953 -- Invariant'Class, replace with T'Class (obj)
4955 if Class_Present (Ritem) then
4957 Make_Type_Conversion (Loc,
4959 Make_Attribute_Reference (Loc,
4960 Prefix => New_Occurrence_Of (T, Loc),
4961 Attribute_Name => Name_Class),
4962 Expression => Make_Identifier (Loc, Object_Name)));
4964 Set_Entity (Expression (N), Object_Entity);
4965 Set_Etype (Expression (N), Typ);
4967 -- Invariant, replace with obj
4970 Rewrite (N, Make_Identifier (Loc, Object_Name));
4971 Set_Entity (N, Object_Entity);
4974 end Replace_Type_Reference;
4976 -- Start of processing for Add_Invariants
4979 Ritem := First_Rep_Item (T);
4980 while Present (Ritem) loop
4981 if Nkind (Ritem) = N_Pragma
4982 and then Pragma_Name (Ritem) = Name_Invariant
4984 Arg1 := First (Pragma_Argument_Associations (Ritem));
4985 Arg2 := Next (Arg1);
4986 Arg3 := Next (Arg2);
4988 Arg1 := Get_Pragma_Arg (Arg1);
4989 Arg2 := Get_Pragma_Arg (Arg2);
4991 -- For Inherit case, ignore Invariant, process only Class case
4994 if not Class_Present (Ritem) then
4998 -- For Inherit false, process only item for right type
5001 if Entity (Arg1) /= Typ then
5007 Stmts := Empty_List;
5010 Exp := New_Copy_Tree (Arg2);
5013 -- We need to replace any occurrences of the name of the type
5014 -- with references to the object, converted to type'Class in
5015 -- the case of Invariant'Class aspects.
5017 Replace_Type_References (Exp, Chars (T));
5019 -- If this invariant comes from an aspect, find the aspect
5020 -- specification, and replace the saved expression because
5021 -- we need the subtype references replaced for the calls to
5022 -- Preanalyze_Spec_Expressin in Check_Aspect_At_Freeze_Point
5023 -- and Check_Aspect_At_End_Of_Declarations.
5025 if From_Aspect_Specification (Ritem) then
5030 -- Loop to find corresponding aspect, note that this
5031 -- must be present given the pragma is marked delayed.
5033 Aitem := Next_Rep_Item (Ritem);
5034 while Present (Aitem) loop
5035 if Nkind (Aitem) = N_Aspect_Specification
5036 and then Aspect_Rep_Item (Aitem) = Ritem
5039 (Identifier (Aitem), New_Copy_Tree (Exp));
5043 Aitem := Next_Rep_Item (Aitem);
5048 -- Now we need to preanalyze the expression to properly capture
5049 -- the visibility in the visible part. The expression will not
5050 -- be analyzed for real until the body is analyzed, but that is
5051 -- at the end of the private part and has the wrong visibility.
5053 Set_Parent (Exp, N);
5054 Preanalyze_Assert_Expression (Exp, Standard_Boolean);
5056 -- Build first two arguments for Check pragma
5059 Make_Pragma_Argument_Association (Loc,
5060 Expression => Make_Identifier (Loc, Name_Invariant)),
5061 Make_Pragma_Argument_Association (Loc, Expression => Exp));
5063 -- Add message if present in Invariant pragma
5065 if Present (Arg3) then
5066 Str := Strval (Get_Pragma_Arg (Arg3));
5068 -- If inherited case, and message starts "failed invariant",
5069 -- change it to be "failed inherited invariant".
5072 String_To_Name_Buffer (Str);
5074 if Name_Buffer (1 .. 16) = "failed invariant" then
5075 Insert_Str_In_Name_Buffer ("inherited ", 8);
5076 Str := String_From_Name_Buffer;
5081 Make_Pragma_Argument_Association (Loc,
5082 Expression => Make_String_Literal (Loc, Str)));
5085 -- Add Check pragma to list of statements
5089 Pragma_Identifier =>
5090 Make_Identifier (Loc, Name_Check),
5091 Pragma_Argument_Associations => Assoc));
5093 -- If Inherited case and option enabled, output info msg. Note
5094 -- that we know this is a case of Invariant'Class.
5096 if Inherit and Opt.List_Inherited_Aspects then
5097 Error_Msg_Sloc := Sloc (Ritem);
5099 ("?info: & inherits `Invariant''Class` aspect from #",
5105 Next_Rep_Item (Ritem);
5109 -- Start of processing for Build_Invariant_Procedure
5115 Set_Etype (Object_Entity, Typ);
5117 -- Add invariants for the current type
5119 Add_Invariants (Typ, Inherit => False);
5121 -- Add invariants for parent types
5124 Current_Typ : Entity_Id;
5125 Parent_Typ : Entity_Id;
5130 Parent_Typ := Etype (Current_Typ);
5132 if Is_Private_Type (Parent_Typ)
5133 and then Present (Full_View (Base_Type (Parent_Typ)))
5135 Parent_Typ := Full_View (Base_Type (Parent_Typ));
5138 exit when Parent_Typ = Current_Typ;
5140 Current_Typ := Parent_Typ;
5141 Add_Invariants (Current_Typ, Inherit => True);
5145 -- Build the procedure if we generated at least one Check pragma
5147 if Stmts /= No_List then
5149 -- Build procedure declaration
5152 Make_Defining_Identifier (Loc,
5153 Chars => New_External_Name (Chars (Typ), "Invariant"));
5154 Set_Has_Invariants (SId);
5155 Set_Invariant_Procedure (Typ, SId);
5158 Make_Procedure_Specification (Loc,
5159 Defining_Unit_Name => SId,
5160 Parameter_Specifications => New_List (
5161 Make_Parameter_Specification (Loc,
5162 Defining_Identifier => Object_Entity,
5163 Parameter_Type => New_Occurrence_Of (Typ, Loc))));
5165 PDecl := Make_Subprogram_Declaration (Loc, Specification => Spec);
5167 -- Build procedure body
5170 Make_Defining_Identifier (Loc,
5171 Chars => New_External_Name (Chars (Typ), "Invariant"));
5174 Make_Procedure_Specification (Loc,
5175 Defining_Unit_Name => SId,
5176 Parameter_Specifications => New_List (
5177 Make_Parameter_Specification (Loc,
5178 Defining_Identifier =>
5179 Make_Defining_Identifier (Loc, Object_Name),
5180 Parameter_Type => New_Occurrence_Of (Typ, Loc))));
5183 Make_Subprogram_Body (Loc,
5184 Specification => Spec,
5185 Declarations => Empty_List,
5186 Handled_Statement_Sequence =>
5187 Make_Handled_Sequence_Of_Statements (Loc,
5188 Statements => Stmts));
5190 -- Insert procedure declaration and spec at the appropriate points.
5192 if Present (Private_Decls) then
5194 -- The spec goes at the end of visible declarations, but they have
5195 -- already been analyzed, so we need to explicitly do the analyze.
5197 Append_To (Visible_Decls, PDecl);
5200 -- The body goes at the end of the private declarations, which we
5201 -- have not analyzed yet, so we do not need to perform an explicit
5202 -- analyze call. We skip this if there are no private declarations
5203 -- (this is an error that will be caught elsewhere);
5205 Append_To (Private_Decls, PBody);
5207 -- If the invariant appears on the full view of a type, the
5208 -- analysis of the private part is complete, and we must
5209 -- analyze the new body explicitly.
5211 if In_Private_Part (Current_Scope) then
5215 -- If there are no private declarations this may be an error that
5216 -- will be diagnosed elsewhere. However, if this is a non-private
5217 -- type that inherits invariants, it needs no completion and there
5218 -- may be no private part. In this case insert invariant procedure
5219 -- at end of current declarative list, and analyze at once, given
5220 -- that the type is about to be frozen.
5222 elsif not Is_Private_Type (Typ) then
5223 Append_To (Visible_Decls, PDecl);
5224 Append_To (Visible_Decls, PBody);
5229 end Build_Invariant_Procedure;
5231 ------------------------------
5232 -- Build_Predicate_Function --
5233 ------------------------------
5235 -- The procedure that is constructed here has the form:
5237 -- function typPredicate (Ixxx : typ) return Boolean is
5240 -- exp1 and then exp2 and then ...
5241 -- and then typ1Predicate (typ1 (Ixxx))
5242 -- and then typ2Predicate (typ2 (Ixxx))
5244 -- end typPredicate;
5246 -- Here exp1, and exp2 are expressions from Predicate pragmas. Note that
5247 -- this is the point at which these expressions get analyzed, providing the
5248 -- required delay, and typ1, typ2, are entities from which predicates are
5249 -- inherited. Note that we do NOT generate Check pragmas, that's because we
5250 -- use this function even if checks are off, e.g. for membership tests.
5252 procedure Build_Predicate_Function (Typ : Entity_Id; N : Node_Id) is
5253 Loc : constant Source_Ptr := Sloc (Typ);
5260 -- This is the expression for the return statement in the function. It
5261 -- is build by connecting the component predicates with AND THEN.
5263 procedure Add_Call (T : Entity_Id);
5264 -- Includes a call to the predicate function for type T in Expr if T
5265 -- has predicates and Predicate_Function (T) is non-empty.
5267 procedure Add_Predicates;
5268 -- Appends expressions for any Predicate pragmas in the rep item chain
5269 -- Typ to Expr. Note that we look only at items for this exact entity.
5270 -- Inheritance of predicates for the parent type is done by calling the
5271 -- Predicate_Function of the parent type, using Add_Call above.
5273 Object_Name : constant Name_Id := New_Internal_Name ('I');
5274 -- Name for argument of Predicate procedure
5276 Object_Entity : constant Entity_Id :=
5277 Make_Defining_Identifier (Loc, Object_Name);
5278 -- The entity for the spec entity for the argument
5280 Dynamic_Predicate_Present : Boolean := False;
5281 -- Set True if a dynamic predicate is present, results in the entire
5282 -- predicate being considered dynamic even if it looks static
5284 Static_Predicate_Present : Node_Id := Empty;
5285 -- Set to N_Pragma node for a static predicate if one is encountered.
5291 procedure Add_Call (T : Entity_Id) is
5295 if Present (T) and then Present (Predicate_Function (T)) then
5296 Set_Has_Predicates (Typ);
5298 -- Build the call to the predicate function of T
5302 (T, Convert_To (T, Make_Identifier (Loc, Object_Name)));
5304 -- Add call to evolving expression, using AND THEN if needed
5311 Left_Opnd => Relocate_Node (Expr),
5315 -- Output info message on inheritance if required. Note we do not
5316 -- give this information for generic actual types, since it is
5317 -- unwelcome noise in that case in instantiations. We also
5318 -- generally suppress the message in instantiations, and also
5319 -- if it involves internal names.
5321 if Opt.List_Inherited_Aspects
5322 and then not Is_Generic_Actual_Type (Typ)
5323 and then Instantiation_Depth (Sloc (Typ)) = 0
5324 and then not Is_Internal_Name (Chars (T))
5325 and then not Is_Internal_Name (Chars (Typ))
5327 Error_Msg_Sloc := Sloc (Predicate_Function (T));
5328 Error_Msg_Node_2 := T;
5329 Error_Msg_N ("?info: & inherits predicate from & #", Typ);
5334 --------------------
5335 -- Add_Predicates --
5336 --------------------
5338 procedure Add_Predicates is
5343 procedure Replace_Type_Reference (N : Node_Id);
5344 -- Replace a single occurrence N of the subtype name with a reference
5345 -- to the formal of the predicate function. N can be an identifier
5346 -- referencing the subtype, or a selected component, representing an
5347 -- appropriately qualified occurrence of the subtype name.
5349 procedure Replace_Type_References is
5350 new Replace_Type_References_Generic (Replace_Type_Reference);
5351 -- Traverse an expression changing every occurrence of an identifier
5352 -- whose name matches the name of the subtype with a reference to
5353 -- the formal parameter of the predicate function.
5355 ----------------------------
5356 -- Replace_Type_Reference --
5357 ----------------------------
5359 procedure Replace_Type_Reference (N : Node_Id) is
5361 Rewrite (N, Make_Identifier (Loc, Object_Name));
5362 Set_Entity (N, Object_Entity);
5364 end Replace_Type_Reference;
5366 -- Start of processing for Add_Predicates
5369 Ritem := First_Rep_Item (Typ);
5370 while Present (Ritem) loop
5371 if Nkind (Ritem) = N_Pragma
5372 and then Pragma_Name (Ritem) = Name_Predicate
5374 if Present (Corresponding_Aspect (Ritem)) then
5375 case Chars (Identifier (Corresponding_Aspect (Ritem))) is
5376 when Name_Dynamic_Predicate =>
5377 Dynamic_Predicate_Present := True;
5378 when Name_Static_Predicate =>
5379 Static_Predicate_Present := Ritem;
5385 -- Acquire arguments
5387 Arg1 := First (Pragma_Argument_Associations (Ritem));
5388 Arg2 := Next (Arg1);
5390 Arg1 := Get_Pragma_Arg (Arg1);
5391 Arg2 := Get_Pragma_Arg (Arg2);
5393 -- See if this predicate pragma is for the current type or for
5394 -- its full view. A predicate on a private completion is placed
5395 -- on the partial view beause this is the visible entity that
5398 if Entity (Arg1) = Typ
5399 or else Full_View (Entity (Arg1)) = Typ
5402 -- We have a match, this entry is for our subtype
5404 -- We need to replace any occurrences of the name of the
5405 -- type with references to the object.
5407 Replace_Type_References (Arg2, Chars (Typ));
5409 -- If this predicate comes from an aspect, find the aspect
5410 -- specification, and replace the saved expression because
5411 -- we need the subtype references replaced for the calls to
5412 -- Preanalyze_Spec_Expressin in Check_Aspect_At_Freeze_Point
5413 -- and Check_Aspect_At_End_Of_Declarations.
5415 if From_Aspect_Specification (Ritem) then
5420 -- Loop to find corresponding aspect, note that this
5421 -- must be present given the pragma is marked delayed.
5423 Aitem := Next_Rep_Item (Ritem);
5425 if Nkind (Aitem) = N_Aspect_Specification
5426 and then Aspect_Rep_Item (Aitem) = Ritem
5429 (Identifier (Aitem), New_Copy_Tree (Arg2));
5433 Aitem := Next_Rep_Item (Aitem);
5438 -- Now we can add the expression
5441 Expr := Relocate_Node (Arg2);
5443 -- There already was a predicate, so add to it
5448 Left_Opnd => Relocate_Node (Expr),
5449 Right_Opnd => Relocate_Node (Arg2));
5454 Next_Rep_Item (Ritem);
5458 -- Start of processing for Build_Predicate_Function
5461 -- Initialize for construction of statement list
5465 -- Return if already built or if type does not have predicates
5467 if not Has_Predicates (Typ)
5468 or else Present (Predicate_Function (Typ))
5473 -- Add Predicates for the current type
5477 -- Add predicates for ancestor if present
5480 Atyp : constant Entity_Id := Nearest_Ancestor (Typ);
5482 if Present (Atyp) then
5487 -- If we have predicates, build the function
5489 if Present (Expr) then
5491 -- Build function declaration
5493 pragma Assert (Has_Predicates (Typ));
5495 Make_Defining_Identifier (Loc,
5496 Chars => New_External_Name (Chars (Typ), "Predicate"));
5497 Set_Has_Predicates (SId);
5498 Set_Predicate_Function (Typ, SId);
5500 -- The predicate function is shared between views of a type.
5502 if Is_Private_Type (Typ) and then Present (Full_View (Typ)) then
5503 Set_Predicate_Function (Full_View (Typ), SId);
5507 Make_Function_Specification (Loc,
5508 Defining_Unit_Name => SId,
5509 Parameter_Specifications => New_List (
5510 Make_Parameter_Specification (Loc,
5511 Defining_Identifier => Object_Entity,
5512 Parameter_Type => New_Occurrence_Of (Typ, Loc))),
5513 Result_Definition =>
5514 New_Occurrence_Of (Standard_Boolean, Loc));
5516 FDecl := Make_Subprogram_Declaration (Loc, Specification => Spec);
5518 -- Build function body
5521 Make_Defining_Identifier (Loc,
5522 Chars => New_External_Name (Chars (Typ), "Predicate"));
5525 Make_Function_Specification (Loc,
5526 Defining_Unit_Name => SId,
5527 Parameter_Specifications => New_List (
5528 Make_Parameter_Specification (Loc,
5529 Defining_Identifier =>
5530 Make_Defining_Identifier (Loc, Object_Name),
5532 New_Occurrence_Of (Typ, Loc))),
5533 Result_Definition =>
5534 New_Occurrence_Of (Standard_Boolean, Loc));
5537 Make_Subprogram_Body (Loc,
5538 Specification => Spec,
5539 Declarations => Empty_List,
5540 Handled_Statement_Sequence =>
5541 Make_Handled_Sequence_Of_Statements (Loc,
5542 Statements => New_List (
5543 Make_Simple_Return_Statement (Loc,
5544 Expression => Expr))));
5546 -- Insert declaration before freeze node and body after
5548 Insert_Before_And_Analyze (N, FDecl);
5549 Insert_After_And_Analyze (N, FBody);
5551 -- Deal with static predicate case
5553 if Ekind_In (Typ, E_Enumeration_Subtype,
5554 E_Modular_Integer_Subtype,
5555 E_Signed_Integer_Subtype)
5556 and then Is_Static_Subtype (Typ)
5557 and then not Dynamic_Predicate_Present
5559 Build_Static_Predicate (Typ, Expr, Object_Name);
5561 if Present (Static_Predicate_Present)
5562 and No (Static_Predicate (Typ))
5565 ("expression does not have required form for "
5566 & "static predicate",
5567 Next (First (Pragma_Argument_Associations
5568 (Static_Predicate_Present))));
5572 end Build_Predicate_Function;
5574 ----------------------------
5575 -- Build_Static_Predicate --
5576 ----------------------------
5578 procedure Build_Static_Predicate
5583 Loc : constant Source_Ptr := Sloc (Expr);
5585 Non_Static : exception;
5586 -- Raised if something non-static is found
5588 Btyp : constant Entity_Id := Base_Type (Typ);
5590 BLo : constant Uint := Expr_Value (Type_Low_Bound (Btyp));
5591 BHi : constant Uint := Expr_Value (Type_High_Bound (Btyp));
5592 -- Low bound and high bound value of base type of Typ
5594 TLo : constant Uint := Expr_Value (Type_Low_Bound (Typ));
5595 THi : constant Uint := Expr_Value (Type_High_Bound (Typ));
5596 -- Low bound and high bound values of static subtype Typ
5601 -- One entry in a Rlist value, a single REnt (range entry) value
5602 -- denotes one range from Lo to Hi. To represent a single value
5603 -- range Lo = Hi = value.
5605 type RList is array (Nat range <>) of REnt;
5606 -- A list of ranges. The ranges are sorted in increasing order,
5607 -- and are disjoint (there is a gap of at least one value between
5608 -- each range in the table). A value is in the set of ranges in
5609 -- Rlist if it lies within one of these ranges
5611 False_Range : constant RList :=
5612 RList'(1 .. 0 => REnt'(No_Uint, No_Uint));
5613 -- An empty set of ranges represents a range list that can never be
5614 -- satisfied, since there are no ranges in which the value could lie,
5615 -- so it does not lie in any of them. False_Range is a canonical value
5616 -- for this empty set, but general processing should test for an Rlist
5617 -- with length zero (see Is_False predicate), since other null ranges
5618 -- may appear which must be treated as False.
5620 True_Range : constant RList := RList'(1 => REnt'(BLo, BHi));
5621 -- Range representing True, value must be in the base range
5623 function "and" (Left, Right : RList) return RList;
5624 -- And's together two range lists, returning a range list. This is
5625 -- a set intersection operation.
5627 function "or" (Left, Right : RList) return RList;
5628 -- Or's together two range lists, returning a range list. This is a
5629 -- set union operation.
5631 function "not" (Right : RList) return RList;
5632 -- Returns complement of a given range list, i.e. a range list
5633 -- representing all the values in TLo .. THi that are not in the
5634 -- input operand Right.
5636 function Build_Val (V : Uint) return Node_Id;
5637 -- Return an analyzed N_Identifier node referencing this value, suitable
5638 -- for use as an entry in the Static_Predicate list. This node is typed
5639 -- with the base type.
5641 function Build_Range (Lo, Hi : Uint) return Node_Id;
5642 -- Return an analyzed N_Range node referencing this range, suitable
5643 -- for use as an entry in the Static_Predicate list. This node is typed
5644 -- with the base type.
5646 function Get_RList (Exp : Node_Id) return RList;
5647 -- This is a recursive routine that converts the given expression into
5648 -- a list of ranges, suitable for use in building the static predicate.
5650 function Is_False (R : RList) return Boolean;
5651 pragma Inline (Is_False);
5652 -- Returns True if the given range list is empty, and thus represents
5653 -- a False list of ranges that can never be satisfied.
5655 function Is_True (R : RList) return Boolean;
5656 -- Returns True if R trivially represents the True predicate by having
5657 -- a single range from BLo to BHi.
5659 function Is_Type_Ref (N : Node_Id) return Boolean;
5660 pragma Inline (Is_Type_Ref);
5661 -- Returns if True if N is a reference to the type for the predicate in
5662 -- the expression (i.e. if it is an identifier whose Chars field matches
5663 -- the Nam given in the call).
5665 function Lo_Val (N : Node_Id) return Uint;
5666 -- Given static expression or static range from a Static_Predicate list,
5667 -- gets expression value or low bound of range.
5669 function Hi_Val (N : Node_Id) return Uint;
5670 -- Given static expression or static range from a Static_Predicate list,
5671 -- gets expression value of high bound of range.
5673 function Membership_Entry (N : Node_Id) return RList;
5674 -- Given a single membership entry (range, value, or subtype), returns
5675 -- the corresponding range list. Raises Static_Error if not static.
5677 function Membership_Entries (N : Node_Id) return RList;
5678 -- Given an element on an alternatives list of a membership operation,
5679 -- returns the range list corresponding to this entry and all following
5680 -- entries (i.e. returns the "or" of this list of values).
5682 function Stat_Pred (Typ : Entity_Id) return RList;
5683 -- Given a type, if it has a static predicate, then return the predicate
5684 -- as a range list, otherwise raise Non_Static.
5690 function "and" (Left, Right : RList) return RList is
5692 -- First range of result
5694 SLeft : Nat := Left'First;
5695 -- Start of rest of left entries
5697 SRight : Nat := Right'First;
5698 -- Start of rest of right entries
5701 -- If either range is True, return the other
5703 if Is_True (Left) then
5705 elsif Is_True (Right) then
5709 -- If either range is False, return False
5711 if Is_False (Left) or else Is_False (Right) then
5715 -- Loop to remove entries at start that are disjoint, and thus
5716 -- just get discarded from the result entirely.
5719 -- If no operands left in either operand, result is false
5721 if SLeft > Left'Last or else SRight > Right'Last then
5724 -- Discard first left operand entry if disjoint with right
5726 elsif Left (SLeft).Hi < Right (SRight).Lo then
5729 -- Discard first right operand entry if disjoint with left
5731 elsif Right (SRight).Hi < Left (SLeft).Lo then
5732 SRight := SRight + 1;
5734 -- Otherwise we have an overlapping entry
5741 -- Now we have two non-null operands, and first entries overlap.
5742 -- The first entry in the result will be the overlapping part of
5743 -- these two entries.
5745 FEnt := REnt'(Lo => UI_Max (Left (SLeft).Lo, Right (SRight).Lo),
5746 Hi => UI_Min (Left (SLeft).Hi, Right (SRight).Hi));
5748 -- Now we can remove the entry that ended at a lower value, since
5749 -- its contribution is entirely contained in Fent.
5751 if Left (SLeft).Hi <= Right (SRight).Hi then
5754 SRight := SRight + 1;
5757 -- Compute result by concatenating this first entry with the "and"
5758 -- of the remaining parts of the left and right operands. Note that
5759 -- if either of these is empty, "and" will yield empty, so that we
5760 -- will end up with just Fent, which is what we want in that case.
5763 FEnt & (Left (SLeft .. Left'Last) and Right (SRight .. Right'Last));
5770 function "not" (Right : RList) return RList is
5772 -- Return True if False range
5774 if Is_False (Right) then
5778 -- Return False if True range
5780 if Is_True (Right) then
5784 -- Here if not trivial case
5787 Result : RList (1 .. Right'Length + 1);
5788 -- May need one more entry for gap at beginning and end
5791 -- Number of entries stored in Result
5796 if Right (Right'First).Lo > TLo then
5798 Result (Count) := REnt'(TLo, Right (Right'First).Lo - 1);
5801 -- Gaps between ranges
5803 for J in Right'First .. Right'Last - 1 loop
5806 REnt'(Right (J).Hi + 1, Right (J + 1).Lo - 1);
5811 if Right (Right'Last).Hi < THi then
5813 Result (Count) := REnt'(Right (Right'Last).Hi + 1, THi);
5816 return Result (1 .. Count);
5824 function "or" (Left, Right : RList) return RList is
5826 -- First range of result
5828 SLeft : Nat := Left'First;
5829 -- Start of rest of left entries
5831 SRight : Nat := Right'First;
5832 -- Start of rest of right entries
5835 -- If either range is True, return True
5837 if Is_True (Left) or else Is_True (Right) then
5841 -- If either range is False (empty), return the other
5843 if Is_False (Left) then
5845 elsif Is_False (Right) then
5849 -- Initialize result first entry from left or right operand
5850 -- depending on which starts with the lower range.
5852 if Left (SLeft).Lo < Right (SRight).Lo then
5853 FEnt := Left (SLeft);
5856 FEnt := Right (SRight);
5857 SRight := SRight + 1;
5860 -- This loop eats ranges from left and right operands that
5861 -- are contiguous with the first range we are gathering.
5864 -- Eat first entry in left operand if contiguous or
5865 -- overlapped by gathered first operand of result.
5867 if SLeft <= Left'Last
5868 and then Left (SLeft).Lo <= FEnt.Hi + 1
5870 FEnt.Hi := UI_Max (FEnt.Hi, Left (SLeft).Hi);
5873 -- Eat first entry in right operand if contiguous or
5874 -- overlapped by gathered right operand of result.
5876 elsif SRight <= Right'Last
5877 and then Right (SRight).Lo <= FEnt.Hi + 1
5879 FEnt.Hi := UI_Max (FEnt.Hi, Right (SRight).Hi);
5880 SRight := SRight + 1;
5882 -- All done if no more entries to eat!
5889 -- Obtain result as the first entry we just computed, concatenated
5890 -- to the "or" of the remaining results (if one operand is empty,
5891 -- this will just concatenate with the other
5894 FEnt & (Left (SLeft .. Left'Last) or Right (SRight .. Right'Last));
5901 function Build_Range (Lo, Hi : Uint) return Node_Id is
5905 return Build_Val (Hi);
5909 Low_Bound => Build_Val (Lo),
5910 High_Bound => Build_Val (Hi));
5911 Set_Etype (Result, Btyp);
5912 Set_Analyzed (Result);
5921 function Build_Val (V : Uint) return Node_Id is
5925 if Is_Enumeration_Type (Typ) then
5926 Result := Get_Enum_Lit_From_Pos (Typ, V, Loc);
5928 Result := Make_Integer_Literal (Loc, V);
5931 Set_Etype (Result, Btyp);
5932 Set_Is_Static_Expression (Result);
5933 Set_Analyzed (Result);
5941 function Get_RList (Exp : Node_Id) return RList is
5946 -- Static expression can only be true or false
5948 if Is_OK_Static_Expression (Exp) then
5952 if Expr_Value (Exp) = 0 then
5959 -- Otherwise test node type
5967 when N_Op_And | N_And_Then =>
5968 return Get_RList (Left_Opnd (Exp))
5970 Get_RList (Right_Opnd (Exp));
5974 when N_Op_Or | N_Or_Else =>
5975 return Get_RList (Left_Opnd (Exp))
5977 Get_RList (Right_Opnd (Exp));
5982 return not Get_RList (Right_Opnd (Exp));
5984 -- Comparisons of type with static value
5986 when N_Op_Compare =>
5987 -- Type is left operand
5989 if Is_Type_Ref (Left_Opnd (Exp))
5990 and then Is_OK_Static_Expression (Right_Opnd (Exp))
5992 Val := Expr_Value (Right_Opnd (Exp));
5994 -- Typ is right operand
5996 elsif Is_Type_Ref (Right_Opnd (Exp))
5997 and then Is_OK_Static_Expression (Left_Opnd (Exp))
5999 Val := Expr_Value (Left_Opnd (Exp));
6001 -- Invert sense of comparison
6004 when N_Op_Gt => Op := N_Op_Lt;
6005 when N_Op_Lt => Op := N_Op_Gt;
6006 when N_Op_Ge => Op := N_Op_Le;
6007 when N_Op_Le => Op := N_Op_Ge;
6008 when others => null;
6011 -- Other cases are non-static
6017 -- Construct range according to comparison operation
6021 return RList'(1 => REnt'(Val, Val));
6024 return RList'(1 => REnt'(Val, BHi));
6027 return RList'(1 => REnt'(Val + 1, BHi));
6030 return RList'(1 => REnt'(BLo, Val));
6033 return RList'(1 => REnt'(BLo, Val - 1));
6036 return RList'(REnt'(BLo, Val - 1),
6037 REnt'(Val + 1, BHi));
6040 raise Program_Error;
6046 if not Is_Type_Ref (Left_Opnd (Exp)) then
6050 if Present (Right_Opnd (Exp)) then
6051 return Membership_Entry (Right_Opnd (Exp));
6053 return Membership_Entries (First (Alternatives (Exp)));
6056 -- Negative membership (NOT IN)
6059 if not Is_Type_Ref (Left_Opnd (Exp)) then
6063 if Present (Right_Opnd (Exp)) then
6064 return not Membership_Entry (Right_Opnd (Exp));
6066 return not Membership_Entries (First (Alternatives (Exp)));
6069 -- Function call, may be call to static predicate
6071 when N_Function_Call =>
6072 if Is_Entity_Name (Name (Exp)) then
6074 Ent : constant Entity_Id := Entity (Name (Exp));
6076 if Has_Predicates (Ent) then
6077 return Stat_Pred (Etype (First_Formal (Ent)));
6082 -- Other function call cases are non-static
6086 -- Qualified expression, dig out the expression
6088 when N_Qualified_Expression =>
6089 return Get_RList (Expression (Exp));
6094 return (Get_RList (Left_Opnd (Exp))
6095 and not Get_RList (Right_Opnd (Exp)))
6096 or (Get_RList (Right_Opnd (Exp))
6097 and not Get_RList (Left_Opnd (Exp)));
6099 -- Any other node type is non-static
6110 function Hi_Val (N : Node_Id) return Uint is
6112 if Is_Static_Expression (N) then
6113 return Expr_Value (N);
6115 pragma Assert (Nkind (N) = N_Range);
6116 return Expr_Value (High_Bound (N));
6124 function Is_False (R : RList) return Boolean is
6126 return R'Length = 0;
6133 function Is_True (R : RList) return Boolean is
6136 and then R (R'First).Lo = BLo
6137 and then R (R'First).Hi = BHi;
6144 function Is_Type_Ref (N : Node_Id) return Boolean is
6146 return Nkind (N) = N_Identifier and then Chars (N) = Nam;
6153 function Lo_Val (N : Node_Id) return Uint is
6155 if Is_Static_Expression (N) then
6156 return Expr_Value (N);
6158 pragma Assert (Nkind (N) = N_Range);
6159 return Expr_Value (Low_Bound (N));
6163 ------------------------
6164 -- Membership_Entries --
6165 ------------------------
6167 function Membership_Entries (N : Node_Id) return RList is
6169 if No (Next (N)) then
6170 return Membership_Entry (N);
6172 return Membership_Entry (N) or Membership_Entries (Next (N));
6174 end Membership_Entries;
6176 ----------------------
6177 -- Membership_Entry --
6178 ----------------------
6180 function Membership_Entry (N : Node_Id) return RList is
6188 if Nkind (N) = N_Range then
6189 if not Is_Static_Expression (Low_Bound (N))
6191 not Is_Static_Expression (High_Bound (N))
6195 SLo := Expr_Value (Low_Bound (N));
6196 SHi := Expr_Value (High_Bound (N));
6197 return RList'(1 => REnt'(SLo, SHi));
6200 -- Static expression case
6202 elsif Is_Static_Expression (N) then
6203 Val := Expr_Value (N);
6204 return RList'(1 => REnt'(Val, Val));
6206 -- Identifier (other than static expression) case
6208 else pragma Assert (Nkind (N) = N_Identifier);
6212 if Is_Type (Entity (N)) then
6214 -- If type has predicates, process them
6216 if Has_Predicates (Entity (N)) then
6217 return Stat_Pred (Entity (N));
6219 -- For static subtype without predicates, get range
6221 elsif Is_Static_Subtype (Entity (N)) then
6222 SLo := Expr_Value (Type_Low_Bound (Entity (N)));
6223 SHi := Expr_Value (Type_High_Bound (Entity (N)));
6224 return RList'(1 => REnt'(SLo, SHi));
6226 -- Any other type makes us non-static
6232 -- Any other kind of identifier in predicate (e.g. a non-static
6233 -- expression value) means this is not a static predicate.
6239 end Membership_Entry;
6245 function Stat_Pred (Typ : Entity_Id) return RList is
6247 -- Not static if type does not have static predicates
6249 if not Has_Predicates (Typ)
6250 or else No (Static_Predicate (Typ))
6255 -- Otherwise we convert the predicate list to a range list
6258 Result : RList (1 .. List_Length (Static_Predicate (Typ)));
6262 P := First (Static_Predicate (Typ));
6263 for J in Result'Range loop
6264 Result (J) := REnt'(Lo_Val (P), Hi_Val (P));
6272 -- Start of processing for Build_Static_Predicate
6275 -- Now analyze the expression to see if it is a static predicate
6278 Ranges : constant RList := Get_RList (Expr);
6279 -- Range list from expression if it is static
6284 -- Convert range list into a form for the static predicate. In the
6285 -- Ranges array, we just have raw ranges, these must be converted
6286 -- to properly typed and analyzed static expressions or range nodes.
6288 -- Note: here we limit ranges to the ranges of the subtype, so that
6289 -- a predicate is always false for values outside the subtype. That
6290 -- seems fine, such values are invalid anyway, and considering them
6291 -- to fail the predicate seems allowed and friendly, and furthermore
6292 -- simplifies processing for case statements and loops.
6296 for J in Ranges'Range loop
6298 Lo : Uint := Ranges (J).Lo;
6299 Hi : Uint := Ranges (J).Hi;
6302 -- Ignore completely out of range entry
6304 if Hi < TLo or else Lo > THi then
6307 -- Otherwise process entry
6310 -- Adjust out of range value to subtype range
6320 -- Convert range into required form
6323 Append_To (Plist, Build_Val (Lo));
6325 Append_To (Plist, Build_Range (Lo, Hi));
6331 -- Processing was successful and all entries were static, so now we
6332 -- can store the result as the predicate list.
6334 Set_Static_Predicate (Typ, Plist);
6336 -- The processing for static predicates put the expression into
6337 -- canonical form as a series of ranges. It also eliminated
6338 -- duplicates and collapsed and combined ranges. We might as well
6339 -- replace the alternatives list of the right operand of the
6340 -- membership test with the static predicate list, which will
6341 -- usually be more efficient.
6344 New_Alts : constant List_Id := New_List;
6349 Old_Node := First (Plist);
6350 while Present (Old_Node) loop
6351 New_Node := New_Copy (Old_Node);
6353 if Nkind (New_Node) = N_Range then
6354 Set_Low_Bound (New_Node, New_Copy (Low_Bound (Old_Node)));
6355 Set_High_Bound (New_Node, New_Copy (High_Bound (Old_Node)));
6358 Append_To (New_Alts, New_Node);
6362 -- If empty list, replace by False
6364 if Is_Empty_List (New_Alts) then
6365 Rewrite (Expr, New_Occurrence_Of (Standard_False, Loc));
6367 -- Else replace by set membership test
6372 Left_Opnd => Make_Identifier (Loc, Nam),
6373 Right_Opnd => Empty,
6374 Alternatives => New_Alts));
6376 -- Resolve new expression in function context
6378 Install_Formals (Predicate_Function (Typ));
6379 Push_Scope (Predicate_Function (Typ));
6380 Analyze_And_Resolve (Expr, Standard_Boolean);
6386 -- If non-static, return doing nothing
6391 end Build_Static_Predicate;
6393 -----------------------------------------
6394 -- Check_Aspect_At_End_Of_Declarations --
6395 -----------------------------------------
6397 procedure Check_Aspect_At_End_Of_Declarations (ASN : Node_Id) is
6398 Ent : constant Entity_Id := Entity (ASN);
6399 Ident : constant Node_Id := Identifier (ASN);
6400 A_Id : constant Aspect_Id := Get_Aspect_Id (Chars (Ident));
6402 End_Decl_Expr : constant Node_Id := Entity (Ident);
6403 -- Expression to be analyzed at end of declarations
6405 Freeze_Expr : constant Node_Id := Expression (ASN);
6406 -- Expression from call to Check_Aspect_At_Freeze_Point
6408 T : constant Entity_Id := Etype (Freeze_Expr);
6409 -- Type required for preanalyze call
6412 -- Set False if error
6414 -- On entry to this procedure, Entity (Ident) contains a copy of the
6415 -- original expression from the aspect, saved for this purpose, and
6416 -- but Expression (Ident) is a preanalyzed copy of the expression,
6417 -- preanalyzed just after the freeze point.
6419 procedure Check_Overloaded_Name;
6420 -- For aspects whose expression is simply a name, this routine checks if
6421 -- the name is overloaded or not. If so, it verifies there is an
6422 -- interpretation that matches the entity obtained at the freeze point,
6423 -- otherwise the compiler complains.
6425 ---------------------------
6426 -- Check_Overloaded_Name --
6427 ---------------------------
6429 procedure Check_Overloaded_Name is
6431 if not Is_Overloaded (End_Decl_Expr) then
6432 Err := Entity (End_Decl_Expr) /= Entity (Freeze_Expr);
6438 Index : Interp_Index;
6442 Get_First_Interp (End_Decl_Expr, Index, It);
6443 while Present (It.Typ) loop
6444 if It.Nam = Entity (Freeze_Expr) then
6449 Get_Next_Interp (Index, It);
6453 end Check_Overloaded_Name;
6455 -- Start of processing for Check_Aspect_At_End_Of_Declarations
6458 -- Case of aspects Dimension, Dimension_System and Synchronization
6460 if A_Id = Aspect_Synchronization then
6463 -- Case of stream attributes, just have to compare entities. However,
6464 -- the expression is just a name (possibly overloaded), and there may
6465 -- be stream operations declared for unrelated types, so we just need
6466 -- to verify that one of these interpretations is the one available at
6467 -- at the freeze point.
6469 elsif A_Id = Aspect_Input or else
6470 A_Id = Aspect_Output or else
6471 A_Id = Aspect_Read or else
6474 Analyze (End_Decl_Expr);
6475 Check_Overloaded_Name;
6477 elsif A_Id = Aspect_Variable_Indexing or else
6478 A_Id = Aspect_Constant_Indexing or else
6479 A_Id = Aspect_Default_Iterator or else
6480 A_Id = Aspect_Iterator_Element
6482 -- Make type unfrozen before analysis, to prevent spurious errors
6483 -- about late attributes.
6485 Set_Is_Frozen (Ent, False);
6486 Analyze (End_Decl_Expr);
6487 Set_Is_Frozen (Ent, True);
6489 -- If the end of declarations comes before any other freeze
6490 -- point, the Freeze_Expr is not analyzed: no check needed.
6492 if Analyzed (Freeze_Expr) and then not In_Instance then
6493 Check_Overloaded_Name;
6501 -- In a generic context the aspect expressions have not been
6502 -- preanalyzed, so do it now. There are no conformance checks
6503 -- to perform in this case.
6506 Check_Aspect_At_Freeze_Point (ASN);
6509 -- The default values attributes may be defined in the private part,
6510 -- and the analysis of the expression may take place when only the
6511 -- partial view is visible. The expression must be scalar, so use
6512 -- the full view to resolve.
6514 elsif (A_Id = Aspect_Default_Value
6516 A_Id = Aspect_Default_Component_Value)
6517 and then Is_Private_Type (T)
6519 Preanalyze_Spec_Expression (End_Decl_Expr, Full_View (T));
6521 Preanalyze_Spec_Expression (End_Decl_Expr, T);
6524 Err := not Fully_Conformant_Expressions (End_Decl_Expr, Freeze_Expr);
6527 -- Output error message if error
6531 ("visibility of aspect for& changes after freeze point",
6534 ("?info: & is frozen here, aspects evaluated at this point",
6535 Freeze_Node (Ent), Ent);
6537 end Check_Aspect_At_End_Of_Declarations;
6539 ----------------------------------
6540 -- Check_Aspect_At_Freeze_Point --
6541 ----------------------------------
6543 procedure Check_Aspect_At_Freeze_Point (ASN : Node_Id) is
6544 Ident : constant Node_Id := Identifier (ASN);
6545 -- Identifier (use Entity field to save expression)
6547 A_Id : constant Aspect_Id := Get_Aspect_Id (Chars (Ident));
6549 T : Entity_Id := Empty;
6550 -- Type required for preanalyze call
6553 -- On entry to this procedure, Entity (Ident) contains a copy of the
6554 -- original expression from the aspect, saved for this purpose.
6556 -- On exit from this procedure Entity (Ident) is unchanged, still
6557 -- containing that copy, but Expression (Ident) is a preanalyzed copy
6558 -- of the expression, preanalyzed just after the freeze point.
6560 -- Make a copy of the expression to be preanalyed
6562 Set_Expression (ASN, New_Copy_Tree (Entity (Ident)));
6564 -- Find type for preanalyze call
6568 -- No_Aspect should be impossible
6571 raise Program_Error;
6573 -- Aspects taking an optional boolean argument
6575 when Boolean_Aspects |
6576 Library_Unit_Aspects =>
6577 T := Standard_Boolean;
6579 when Aspect_Attach_Handler =>
6580 T := RTE (RE_Interrupt_ID);
6582 when Aspect_Convention =>
6585 -- Default_Value is resolved with the type entity in question
6587 when Aspect_Default_Value =>
6590 -- Default_Component_Value is resolved with the component type
6592 when Aspect_Default_Component_Value =>
6593 T := Component_Type (Entity (ASN));
6595 -- Aspects corresponding to attribute definition clauses
6597 when Aspect_Address =>
6598 T := RTE (RE_Address);
6600 when Aspect_Bit_Order | Aspect_Scalar_Storage_Order =>
6601 T := RTE (RE_Bit_Order);
6604 T := RTE (RE_CPU_Range);
6606 when Aspect_Dispatching_Domain =>
6607 T := RTE (RE_Dispatching_Domain);
6609 when Aspect_External_Tag =>
6610 T := Standard_String;
6612 when Aspect_External_Name =>
6613 T := Standard_String;
6615 when Aspect_Link_Name =>
6616 T := Standard_String;
6618 when Aspect_Priority | Aspect_Interrupt_Priority =>
6619 T := Standard_Integer;
6621 when Aspect_Small =>
6622 T := Universal_Real;
6624 -- For a simple storage pool, we have to retrieve the type of the
6625 -- pool object associated with the aspect's corresponding attribute
6626 -- definition clause.
6628 when Aspect_Simple_Storage_Pool =>
6629 T := Etype (Expression (Aspect_Rep_Item (ASN)));
6631 when Aspect_Storage_Pool =>
6632 T := Class_Wide_Type (RTE (RE_Root_Storage_Pool));
6634 when Aspect_Alignment |
6635 Aspect_Component_Size |
6636 Aspect_Machine_Radix |
6637 Aspect_Object_Size |
6639 Aspect_Storage_Size |
6640 Aspect_Stream_Size |
6641 Aspect_Value_Size =>
6644 when Aspect_Synchronization =>
6647 -- Special case, the expression of these aspects is just an entity
6648 -- that does not need any resolution, so just analyze.
6657 Analyze (Expression (ASN));
6660 -- Same for Iterator aspects, where the expression is a function
6661 -- name. Legality rules are checked separately.
6663 when Aspect_Constant_Indexing |
6664 Aspect_Default_Iterator |
6665 Aspect_Iterator_Element |
6666 Aspect_Variable_Indexing =>
6667 Analyze (Expression (ASN));
6670 -- Invariant/Predicate take boolean expressions
6672 when Aspect_Dynamic_Predicate |
6675 Aspect_Static_Predicate |
6676 Aspect_Type_Invariant =>
6677 T := Standard_Boolean;
6679 -- Here is the list of aspects that don't require delay analysis.
6681 when Aspect_Contract_Case |
6683 Aspect_Dimension_System |
6684 Aspect_Implicit_Dereference |
6686 Aspect_Postcondition |
6688 Aspect_Precondition |
6690 raise Program_Error;
6694 -- Do the preanalyze call
6696 Preanalyze_Spec_Expression (Expression (ASN), T);
6697 end Check_Aspect_At_Freeze_Point;
6699 -----------------------------------
6700 -- Check_Constant_Address_Clause --
6701 -----------------------------------
6703 procedure Check_Constant_Address_Clause
6707 procedure Check_At_Constant_Address (Nod : Node_Id);
6708 -- Checks that the given node N represents a name whose 'Address is
6709 -- constant (in the same sense as OK_Constant_Address_Clause, i.e. the
6710 -- address value is the same at the point of declaration of U_Ent and at
6711 -- the time of elaboration of the address clause.
6713 procedure Check_Expr_Constants (Nod : Node_Id);
6714 -- Checks that Nod meets the requirements for a constant address clause
6715 -- in the sense of the enclosing procedure.
6717 procedure Check_List_Constants (Lst : List_Id);
6718 -- Check that all elements of list Lst meet the requirements for a
6719 -- constant address clause in the sense of the enclosing procedure.
6721 -------------------------------
6722 -- Check_At_Constant_Address --
6723 -------------------------------
6725 procedure Check_At_Constant_Address (Nod : Node_Id) is
6727 if Is_Entity_Name (Nod) then
6728 if Present (Address_Clause (Entity ((Nod)))) then
6730 ("invalid address clause for initialized object &!",
6733 ("address for& cannot" &
6734 " depend on another address clause! (RM 13.1(22))!",
6737 elsif In_Same_Source_Unit (Entity (Nod), U_Ent)
6738 and then Sloc (U_Ent) < Sloc (Entity (Nod))
6741 ("invalid address clause for initialized object &!",
6743 Error_Msg_Node_2 := U_Ent;
6745 ("\& must be defined before & (RM 13.1(22))!",
6749 elsif Nkind (Nod) = N_Selected_Component then
6751 T : constant Entity_Id := Etype (Prefix (Nod));
6754 if (Is_Record_Type (T)
6755 and then Has_Discriminants (T))
6758 and then Is_Record_Type (Designated_Type (T))
6759 and then Has_Discriminants (Designated_Type (T)))
6762 ("invalid address clause for initialized object &!",
6765 ("\address cannot depend on component" &
6766 " of discriminated record (RM 13.1(22))!",
6769 Check_At_Constant_Address (Prefix (Nod));
6773 elsif Nkind (Nod) = N_Indexed_Component then
6774 Check_At_Constant_Address (Prefix (Nod));
6775 Check_List_Constants (Expressions (Nod));
6778 Check_Expr_Constants (Nod);
6780 end Check_At_Constant_Address;
6782 --------------------------
6783 -- Check_Expr_Constants --
6784 --------------------------
6786 procedure Check_Expr_Constants (Nod : Node_Id) is
6787 Loc_U_Ent : constant Source_Ptr := Sloc (U_Ent);
6788 Ent : Entity_Id := Empty;
6791 if Nkind (Nod) in N_Has_Etype
6792 and then Etype (Nod) = Any_Type
6798 when N_Empty | N_Error =>
6801 when N_Identifier | N_Expanded_Name =>
6802 Ent := Entity (Nod);
6804 -- We need to look at the original node if it is different
6805 -- from the node, since we may have rewritten things and
6806 -- substituted an identifier representing the rewrite.
6808 if Original_Node (Nod) /= Nod then
6809 Check_Expr_Constants (Original_Node (Nod));
6811 -- If the node is an object declaration without initial
6812 -- value, some code has been expanded, and the expression
6813 -- is not constant, even if the constituents might be
6814 -- acceptable, as in A'Address + offset.
6816 if Ekind (Ent) = E_Variable
6818 Nkind (Declaration_Node (Ent)) = N_Object_Declaration
6820 No (Expression (Declaration_Node (Ent)))
6823 ("invalid address clause for initialized object &!",
6826 -- If entity is constant, it may be the result of expanding
6827 -- a check. We must verify that its declaration appears
6828 -- before the object in question, else we also reject the
6831 elsif Ekind (Ent) = E_Constant
6832 and then In_Same_Source_Unit (Ent, U_Ent)
6833 and then Sloc (Ent) > Loc_U_Ent
6836 ("invalid address clause for initialized object &!",
6843 -- Otherwise look at the identifier and see if it is OK
6845 if Ekind_In (Ent, E_Named_Integer, E_Named_Real)
6846 or else Is_Type (Ent)
6851 Ekind (Ent) = E_Constant
6853 Ekind (Ent) = E_In_Parameter
6855 -- This is the case where we must have Ent defined before
6856 -- U_Ent. Clearly if they are in different units this
6857 -- requirement is met since the unit containing Ent is
6858 -- already processed.
6860 if not In_Same_Source_Unit (Ent, U_Ent) then
6863 -- Otherwise location of Ent must be before the location
6864 -- of U_Ent, that's what prior defined means.
6866 elsif Sloc (Ent) < Loc_U_Ent then
6871 ("invalid address clause for initialized object &!",
6873 Error_Msg_Node_2 := U_Ent;
6875 ("\& must be defined before & (RM 13.1(22))!",
6879 elsif Nkind (Original_Node (Nod)) = N_Function_Call then
6880 Check_Expr_Constants (Original_Node (Nod));
6884 ("invalid address clause for initialized object &!",
6887 if Comes_From_Source (Ent) then
6889 ("\reference to variable& not allowed"
6890 & " (RM 13.1(22))!", Nod, Ent);
6893 ("non-static expression not allowed"
6894 & " (RM 13.1(22))!", Nod);
6898 when N_Integer_Literal =>
6900 -- If this is a rewritten unchecked conversion, in a system
6901 -- where Address is an integer type, always use the base type
6902 -- for a literal value. This is user-friendly and prevents
6903 -- order-of-elaboration issues with instances of unchecked
6906 if Nkind (Original_Node (Nod)) = N_Function_Call then
6907 Set_Etype (Nod, Base_Type (Etype (Nod)));
6910 when N_Real_Literal |
6912 N_Character_Literal =>
6916 Check_Expr_Constants (Low_Bound (Nod));
6917 Check_Expr_Constants (High_Bound (Nod));
6919 when N_Explicit_Dereference =>
6920 Check_Expr_Constants (Prefix (Nod));
6922 when N_Indexed_Component =>
6923 Check_Expr_Constants (Prefix (Nod));
6924 Check_List_Constants (Expressions (Nod));
6927 Check_Expr_Constants (Prefix (Nod));
6928 Check_Expr_Constants (Discrete_Range (Nod));
6930 when N_Selected_Component =>
6931 Check_Expr_Constants (Prefix (Nod));
6933 when N_Attribute_Reference =>
6934 if Attribute_Name (Nod) = Name_Address
6936 Attribute_Name (Nod) = Name_Access
6938 Attribute_Name (Nod) = Name_Unchecked_Access
6940 Attribute_Name (Nod) = Name_Unrestricted_Access
6942 Check_At_Constant_Address (Prefix (Nod));
6945 Check_Expr_Constants (Prefix (Nod));
6946 Check_List_Constants (Expressions (Nod));
6950 Check_List_Constants (Component_Associations (Nod));
6951 Check_List_Constants (Expressions (Nod));
6953 when N_Component_Association =>
6954 Check_Expr_Constants (Expression (Nod));
6956 when N_Extension_Aggregate =>
6957 Check_Expr_Constants (Ancestor_Part (Nod));
6958 Check_List_Constants (Component_Associations (Nod));
6959 Check_List_Constants (Expressions (Nod));
6964 when N_Binary_Op | N_Short_Circuit | N_Membership_Test =>
6965 Check_Expr_Constants (Left_Opnd (Nod));
6966 Check_Expr_Constants (Right_Opnd (Nod));
6969 Check_Expr_Constants (Right_Opnd (Nod));
6971 when N_Type_Conversion |
6972 N_Qualified_Expression |
6974 Check_Expr_Constants (Expression (Nod));
6976 when N_Unchecked_Type_Conversion =>
6977 Check_Expr_Constants (Expression (Nod));
6979 -- If this is a rewritten unchecked conversion, subtypes in
6980 -- this node are those created within the instance. To avoid
6981 -- order of elaboration issues, replace them with their base
6982 -- types. Note that address clauses can cause order of
6983 -- elaboration problems because they are elaborated by the
6984 -- back-end at the point of definition, and may mention
6985 -- entities declared in between (as long as everything is
6986 -- static). It is user-friendly to allow unchecked conversions
6989 if Nkind (Original_Node (Nod)) = N_Function_Call then
6990 Set_Etype (Expression (Nod),
6991 Base_Type (Etype (Expression (Nod))));
6992 Set_Etype (Nod, Base_Type (Etype (Nod)));
6995 when N_Function_Call =>
6996 if not Is_Pure (Entity (Name (Nod))) then
6998 ("invalid address clause for initialized object &!",
7002 ("\function & is not pure (RM 13.1(22))!",
7003 Nod, Entity (Name (Nod)));
7006 Check_List_Constants (Parameter_Associations (Nod));
7009 when N_Parameter_Association =>
7010 Check_Expr_Constants (Explicit_Actual_Parameter (Nod));
7014 ("invalid address clause for initialized object &!",
7017 ("\must be constant defined before& (RM 13.1(22))!",
7020 end Check_Expr_Constants;
7022 --------------------------
7023 -- Check_List_Constants --
7024 --------------------------
7026 procedure Check_List_Constants (Lst : List_Id) is
7030 if Present (Lst) then
7031 Nod1 := First (Lst);
7032 while Present (Nod1) loop
7033 Check_Expr_Constants (Nod1);
7037 end Check_List_Constants;
7039 -- Start of processing for Check_Constant_Address_Clause
7042 -- If rep_clauses are to be ignored, no need for legality checks. In
7043 -- particular, no need to pester user about rep clauses that violate
7044 -- the rule on constant addresses, given that these clauses will be
7045 -- removed by Freeze before they reach the back end.
7047 if not Ignore_Rep_Clauses then
7048 Check_Expr_Constants (Expr);
7050 end Check_Constant_Address_Clause;
7052 ----------------------------------------
7053 -- Check_Record_Representation_Clause --
7054 ----------------------------------------
7056 procedure Check_Record_Representation_Clause (N : Node_Id) is
7057 Loc : constant Source_Ptr := Sloc (N);
7058 Ident : constant Node_Id := Identifier (N);
7059 Rectype : Entity_Id;
7064 Hbit : Uint := Uint_0;
7068 Max_Bit_So_Far : Uint;
7069 -- Records the maximum bit position so far. If all field positions
7070 -- are monotonically increasing, then we can skip the circuit for
7071 -- checking for overlap, since no overlap is possible.
7073 Tagged_Parent : Entity_Id := Empty;
7074 -- This is set in the case of a derived tagged type for which we have
7075 -- Is_Fully_Repped_Tagged_Type True (indicating that all components are
7076 -- positioned by record representation clauses). In this case we must
7077 -- check for overlap between components of this tagged type, and the
7078 -- components of its parent. Tagged_Parent will point to this parent
7079 -- type. For all other cases Tagged_Parent is left set to Empty.
7081 Parent_Last_Bit : Uint;
7082 -- Relevant only if Tagged_Parent is set, Parent_Last_Bit indicates the
7083 -- last bit position for any field in the parent type. We only need to
7084 -- check overlap for fields starting below this point.
7086 Overlap_Check_Required : Boolean;
7087 -- Used to keep track of whether or not an overlap check is required
7089 Overlap_Detected : Boolean := False;
7090 -- Set True if an overlap is detected
7092 Ccount : Natural := 0;
7093 -- Number of component clauses in record rep clause
7095 procedure Check_Component_Overlap (C1_Ent, C2_Ent : Entity_Id);
7096 -- Given two entities for record components or discriminants, checks
7097 -- if they have overlapping component clauses and issues errors if so.
7099 procedure Find_Component;
7100 -- Finds component entity corresponding to current component clause (in
7101 -- CC), and sets Comp to the entity, and Fbit/Lbit to the zero origin
7102 -- start/stop bits for the field. If there is no matching component or
7103 -- if the matching component does not have a component clause, then
7104 -- that's an error and Comp is set to Empty, but no error message is
7105 -- issued, since the message was already given. Comp is also set to
7106 -- Empty if the current "component clause" is in fact a pragma.
7108 -----------------------------
7109 -- Check_Component_Overlap --
7110 -----------------------------
7112 procedure Check_Component_Overlap (C1_Ent, C2_Ent : Entity_Id) is
7113 CC1 : constant Node_Id := Component_Clause (C1_Ent);
7114 CC2 : constant Node_Id := Component_Clause (C2_Ent);
7117 if Present (CC1) and then Present (CC2) then
7119 -- Exclude odd case where we have two tag fields in the same
7120 -- record, both at location zero. This seems a bit strange, but
7121 -- it seems to happen in some circumstances, perhaps on an error.
7123 if Chars (C1_Ent) = Name_uTag
7125 Chars (C2_Ent) = Name_uTag
7130 -- Here we check if the two fields overlap
7133 S1 : constant Uint := Component_Bit_Offset (C1_Ent);
7134 S2 : constant Uint := Component_Bit_Offset (C2_Ent);
7135 E1 : constant Uint := S1 + Esize (C1_Ent);
7136 E2 : constant Uint := S2 + Esize (C2_Ent);
7139 if E2 <= S1 or else E1 <= S2 then
7142 Error_Msg_Node_2 := Component_Name (CC2);
7143 Error_Msg_Sloc := Sloc (Error_Msg_Node_2);
7144 Error_Msg_Node_1 := Component_Name (CC1);
7146 ("component& overlaps & #", Component_Name (CC1));
7147 Overlap_Detected := True;
7151 end Check_Component_Overlap;
7153 --------------------
7154 -- Find_Component --
7155 --------------------
7157 procedure Find_Component is
7159 procedure Search_Component (R : Entity_Id);
7160 -- Search components of R for a match. If found, Comp is set.
7162 ----------------------
7163 -- Search_Component --
7164 ----------------------
7166 procedure Search_Component (R : Entity_Id) is
7168 Comp := First_Component_Or_Discriminant (R);
7169 while Present (Comp) loop
7171 -- Ignore error of attribute name for component name (we
7172 -- already gave an error message for this, so no need to
7175 if Nkind (Component_Name (CC)) = N_Attribute_Reference then
7178 exit when Chars (Comp) = Chars (Component_Name (CC));
7181 Next_Component_Or_Discriminant (Comp);
7183 end Search_Component;
7185 -- Start of processing for Find_Component
7188 -- Return with Comp set to Empty if we have a pragma
7190 if Nkind (CC) = N_Pragma then
7195 -- Search current record for matching component
7197 Search_Component (Rectype);
7199 -- If not found, maybe component of base type that is absent from
7200 -- statically constrained first subtype.
7203 Search_Component (Base_Type (Rectype));
7206 -- If no component, or the component does not reference the component
7207 -- clause in question, then there was some previous error for which
7208 -- we already gave a message, so just return with Comp Empty.
7211 or else Component_Clause (Comp) /= CC
7215 -- Normal case where we have a component clause
7218 Fbit := Component_Bit_Offset (Comp);
7219 Lbit := Fbit + Esize (Comp) - 1;
7223 -- Start of processing for Check_Record_Representation_Clause
7227 Rectype := Entity (Ident);
7229 if Rectype = Any_Type then
7232 Rectype := Underlying_Type (Rectype);
7235 -- See if we have a fully repped derived tagged type
7238 PS : constant Entity_Id := Parent_Subtype (Rectype);
7241 if Present (PS) and then Is_Fully_Repped_Tagged_Type (PS) then
7242 Tagged_Parent := PS;
7244 -- Find maximum bit of any component of the parent type
7246 Parent_Last_Bit := UI_From_Int (System_Address_Size - 1);
7247 Pcomp := First_Entity (Tagged_Parent);
7248 while Present (Pcomp) loop
7249 if Ekind_In (Pcomp, E_Discriminant, E_Component) then
7250 if Component_Bit_Offset (Pcomp) /= No_Uint
7251 and then Known_Static_Esize (Pcomp)
7256 Component_Bit_Offset (Pcomp) + Esize (Pcomp) - 1);
7259 Next_Entity (Pcomp);
7265 -- All done if no component clauses
7267 CC := First (Component_Clauses (N));
7273 -- If a tag is present, then create a component clause that places it
7274 -- at the start of the record (otherwise gigi may place it after other
7275 -- fields that have rep clauses).
7277 Fent := First_Entity (Rectype);
7279 if Nkind (Fent) = N_Defining_Identifier
7280 and then Chars (Fent) = Name_uTag
7282 Set_Component_Bit_Offset (Fent, Uint_0);
7283 Set_Normalized_Position (Fent, Uint_0);
7284 Set_Normalized_First_Bit (Fent, Uint_0);
7285 Set_Normalized_Position_Max (Fent, Uint_0);
7286 Init_Esize (Fent, System_Address_Size);
7288 Set_Component_Clause (Fent,
7289 Make_Component_Clause (Loc,
7290 Component_Name => Make_Identifier (Loc, Name_uTag),
7292 Position => Make_Integer_Literal (Loc, Uint_0),
7293 First_Bit => Make_Integer_Literal (Loc, Uint_0),
7295 Make_Integer_Literal (Loc,
7296 UI_From_Int (System_Address_Size))));
7298 Ccount := Ccount + 1;
7301 Max_Bit_So_Far := Uint_Minus_1;
7302 Overlap_Check_Required := False;
7304 -- Process the component clauses
7306 while Present (CC) loop
7309 if Present (Comp) then
7310 Ccount := Ccount + 1;
7312 -- We need a full overlap check if record positions non-monotonic
7314 if Fbit <= Max_Bit_So_Far then
7315 Overlap_Check_Required := True;
7318 Max_Bit_So_Far := Lbit;
7320 -- Check bit position out of range of specified size
7322 if Has_Size_Clause (Rectype)
7323 and then RM_Size (Rectype) <= Lbit
7326 ("bit number out of range of specified size",
7329 -- Check for overlap with tag field
7332 if Is_Tagged_Type (Rectype)
7333 and then Fbit < System_Address_Size
7336 ("component overlaps tag field of&",
7337 Component_Name (CC), Rectype);
7338 Overlap_Detected := True;
7346 -- Check parent overlap if component might overlap parent field
7348 if Present (Tagged_Parent)
7349 and then Fbit <= Parent_Last_Bit
7351 Pcomp := First_Component_Or_Discriminant (Tagged_Parent);
7352 while Present (Pcomp) loop
7353 if not Is_Tag (Pcomp)
7354 and then Chars (Pcomp) /= Name_uParent
7356 Check_Component_Overlap (Comp, Pcomp);
7359 Next_Component_Or_Discriminant (Pcomp);
7367 -- Now that we have processed all the component clauses, check for
7368 -- overlap. We have to leave this till last, since the components can
7369 -- appear in any arbitrary order in the representation clause.
7371 -- We do not need this check if all specified ranges were monotonic,
7372 -- as recorded by Overlap_Check_Required being False at this stage.
7374 -- This first section checks if there are any overlapping entries at
7375 -- all. It does this by sorting all entries and then seeing if there are
7376 -- any overlaps. If there are none, then that is decisive, but if there
7377 -- are overlaps, they may still be OK (they may result from fields in
7378 -- different variants).
7380 if Overlap_Check_Required then
7381 Overlap_Check1 : declare
7383 OC_Fbit : array (0 .. Ccount) of Uint;
7384 -- First-bit values for component clauses, the value is the offset
7385 -- of the first bit of the field from start of record. The zero
7386 -- entry is for use in sorting.
7388 OC_Lbit : array (0 .. Ccount) of Uint;
7389 -- Last-bit values for component clauses, the value is the offset
7390 -- of the last bit of the field from start of record. The zero
7391 -- entry is for use in sorting.
7393 OC_Count : Natural := 0;
7394 -- Count of entries in OC_Fbit and OC_Lbit
7396 function OC_Lt (Op1, Op2 : Natural) return Boolean;
7397 -- Compare routine for Sort
7399 procedure OC_Move (From : Natural; To : Natural);
7400 -- Move routine for Sort
7402 package Sorting is new GNAT.Heap_Sort_G (OC_Move, OC_Lt);
7408 function OC_Lt (Op1, Op2 : Natural) return Boolean is
7410 return OC_Fbit (Op1) < OC_Fbit (Op2);
7417 procedure OC_Move (From : Natural; To : Natural) is
7419 OC_Fbit (To) := OC_Fbit (From);
7420 OC_Lbit (To) := OC_Lbit (From);
7423 -- Start of processing for Overlap_Check
7426 CC := First (Component_Clauses (N));
7427 while Present (CC) loop
7429 -- Exclude component clause already marked in error
7431 if not Error_Posted (CC) then
7434 if Present (Comp) then
7435 OC_Count := OC_Count + 1;
7436 OC_Fbit (OC_Count) := Fbit;
7437 OC_Lbit (OC_Count) := Lbit;
7444 Sorting.Sort (OC_Count);
7446 Overlap_Check_Required := False;
7447 for J in 1 .. OC_Count - 1 loop
7448 if OC_Lbit (J) >= OC_Fbit (J + 1) then
7449 Overlap_Check_Required := True;
7456 -- If Overlap_Check_Required is still True, then we have to do the full
7457 -- scale overlap check, since we have at least two fields that do
7458 -- overlap, and we need to know if that is OK since they are in
7459 -- different variant, or whether we have a definite problem.
7461 if Overlap_Check_Required then
7462 Overlap_Check2 : declare
7463 C1_Ent, C2_Ent : Entity_Id;
7464 -- Entities of components being checked for overlap
7467 -- Component_List node whose Component_Items are being checked
7470 -- Component declaration for component being checked
7473 C1_Ent := First_Entity (Base_Type (Rectype));
7475 -- Loop through all components in record. For each component check
7476 -- for overlap with any of the preceding elements on the component
7477 -- list containing the component and also, if the component is in
7478 -- a variant, check against components outside the case structure.
7479 -- This latter test is repeated recursively up the variant tree.
7481 Main_Component_Loop : while Present (C1_Ent) loop
7482 if not Ekind_In (C1_Ent, E_Component, E_Discriminant) then
7483 goto Continue_Main_Component_Loop;
7486 -- Skip overlap check if entity has no declaration node. This
7487 -- happens with discriminants in constrained derived types.
7488 -- Possibly we are missing some checks as a result, but that
7489 -- does not seem terribly serious.
7491 if No (Declaration_Node (C1_Ent)) then
7492 goto Continue_Main_Component_Loop;
7495 Clist := Parent (List_Containing (Declaration_Node (C1_Ent)));
7497 -- Loop through component lists that need checking. Check the
7498 -- current component list and all lists in variants above us.
7500 Component_List_Loop : loop
7502 -- If derived type definition, go to full declaration
7503 -- If at outer level, check discriminants if there are any.
7505 if Nkind (Clist) = N_Derived_Type_Definition then
7506 Clist := Parent (Clist);
7509 -- Outer level of record definition, check discriminants
7511 if Nkind_In (Clist, N_Full_Type_Declaration,
7512 N_Private_Type_Declaration)
7514 if Has_Discriminants (Defining_Identifier (Clist)) then
7516 First_Discriminant (Defining_Identifier (Clist));
7517 while Present (C2_Ent) loop
7518 exit when C1_Ent = C2_Ent;
7519 Check_Component_Overlap (C1_Ent, C2_Ent);
7520 Next_Discriminant (C2_Ent);
7524 -- Record extension case
7526 elsif Nkind (Clist) = N_Derived_Type_Definition then
7529 -- Otherwise check one component list
7532 Citem := First (Component_Items (Clist));
7533 while Present (Citem) loop
7534 if Nkind (Citem) = N_Component_Declaration then
7535 C2_Ent := Defining_Identifier (Citem);
7536 exit when C1_Ent = C2_Ent;
7537 Check_Component_Overlap (C1_Ent, C2_Ent);
7544 -- Check for variants above us (the parent of the Clist can
7545 -- be a variant, in which case its parent is a variant part,
7546 -- and the parent of the variant part is a component list
7547 -- whose components must all be checked against the current
7548 -- component for overlap).
7550 if Nkind (Parent (Clist)) = N_Variant then
7551 Clist := Parent (Parent (Parent (Clist)));
7553 -- Check for possible discriminant part in record, this
7554 -- is treated essentially as another level in the
7555 -- recursion. For this case the parent of the component
7556 -- list is the record definition, and its parent is the
7557 -- full type declaration containing the discriminant
7560 elsif Nkind (Parent (Clist)) = N_Record_Definition then
7561 Clist := Parent (Parent ((Clist)));
7563 -- If neither of these two cases, we are at the top of
7567 exit Component_List_Loop;
7569 end loop Component_List_Loop;
7571 <<Continue_Main_Component_Loop>>
7572 Next_Entity (C1_Ent);
7574 end loop Main_Component_Loop;
7578 -- The following circuit deals with warning on record holes (gaps). We
7579 -- skip this check if overlap was detected, since it makes sense for the
7580 -- programmer to fix this illegality before worrying about warnings.
7582 if not Overlap_Detected and Warn_On_Record_Holes then
7583 Record_Hole_Check : declare
7584 Decl : constant Node_Id := Declaration_Node (Base_Type (Rectype));
7585 -- Full declaration of record type
7587 procedure Check_Component_List
7591 -- Check component list CL for holes. The starting bit should be
7592 -- Sbit. which is zero for the main record component list and set
7593 -- appropriately for recursive calls for variants. DS is set to
7594 -- a list of discriminant specifications to be included in the
7595 -- consideration of components. It is No_List if none to consider.
7597 --------------------------
7598 -- Check_Component_List --
7599 --------------------------
7601 procedure Check_Component_List
7609 Compl := Integer (List_Length (Component_Items (CL)));
7611 if DS /= No_List then
7612 Compl := Compl + Integer (List_Length (DS));
7616 Comps : array (Natural range 0 .. Compl) of Entity_Id;
7617 -- Gather components (zero entry is for sort routine)
7619 Ncomps : Natural := 0;
7620 -- Number of entries stored in Comps (starting at Comps (1))
7623 -- One component item or discriminant specification
7626 -- Starting bit for next component
7634 function Lt (Op1, Op2 : Natural) return Boolean;
7635 -- Compare routine for Sort
7637 procedure Move (From : Natural; To : Natural);
7638 -- Move routine for Sort
7640 package Sorting is new GNAT.Heap_Sort_G (Move, Lt);
7646 function Lt (Op1, Op2 : Natural) return Boolean is
7648 return Component_Bit_Offset (Comps (Op1))
7650 Component_Bit_Offset (Comps (Op2));
7657 procedure Move (From : Natural; To : Natural) is
7659 Comps (To) := Comps (From);
7663 -- Gather discriminants into Comp
7665 if DS /= No_List then
7666 Citem := First (DS);
7667 while Present (Citem) loop
7668 if Nkind (Citem) = N_Discriminant_Specification then
7670 Ent : constant Entity_Id :=
7671 Defining_Identifier (Citem);
7673 if Ekind (Ent) = E_Discriminant then
7674 Ncomps := Ncomps + 1;
7675 Comps (Ncomps) := Ent;
7684 -- Gather component entities into Comp
7686 Citem := First (Component_Items (CL));
7687 while Present (Citem) loop
7688 if Nkind (Citem) = N_Component_Declaration then
7689 Ncomps := Ncomps + 1;
7690 Comps (Ncomps) := Defining_Identifier (Citem);
7696 -- Now sort the component entities based on the first bit.
7697 -- Note we already know there are no overlapping components.
7699 Sorting.Sort (Ncomps);
7701 -- Loop through entries checking for holes
7704 for J in 1 .. Ncomps loop
7706 Error_Msg_Uint_1 := Component_Bit_Offset (CEnt) - Nbit;
7708 if Error_Msg_Uint_1 > 0 then
7710 ("?^-bit gap before component&",
7711 Component_Name (Component_Clause (CEnt)), CEnt);
7714 Nbit := Component_Bit_Offset (CEnt) + Esize (CEnt);
7717 -- Process variant parts recursively if present
7719 if Present (Variant_Part (CL)) then
7720 Variant := First (Variants (Variant_Part (CL)));
7721 while Present (Variant) loop
7722 Check_Component_List
7723 (Component_List (Variant), Nbit, No_List);
7728 end Check_Component_List;
7730 -- Start of processing for Record_Hole_Check
7737 if Is_Tagged_Type (Rectype) then
7738 Sbit := UI_From_Int (System_Address_Size);
7743 if Nkind (Decl) = N_Full_Type_Declaration
7744 and then Nkind (Type_Definition (Decl)) = N_Record_Definition
7746 Check_Component_List
7747 (Component_List (Type_Definition (Decl)),
7749 Discriminant_Specifications (Decl));
7752 end Record_Hole_Check;
7755 -- For records that have component clauses for all components, and whose
7756 -- size is less than or equal to 32, we need to know the size in the
7757 -- front end to activate possible packed array processing where the
7758 -- component type is a record.
7760 -- At this stage Hbit + 1 represents the first unused bit from all the
7761 -- component clauses processed, so if the component clauses are
7762 -- complete, then this is the length of the record.
7764 -- For records longer than System.Storage_Unit, and for those where not
7765 -- all components have component clauses, the back end determines the
7766 -- length (it may for example be appropriate to round up the size
7767 -- to some convenient boundary, based on alignment considerations, etc).
7769 if Unknown_RM_Size (Rectype) and then Hbit + 1 <= 32 then
7771 -- Nothing to do if at least one component has no component clause
7773 Comp := First_Component_Or_Discriminant (Rectype);
7774 while Present (Comp) loop
7775 exit when No (Component_Clause (Comp));
7776 Next_Component_Or_Discriminant (Comp);
7779 -- If we fall out of loop, all components have component clauses
7780 -- and so we can set the size to the maximum value.
7783 Set_RM_Size (Rectype, Hbit + 1);
7786 end Check_Record_Representation_Clause;
7792 procedure Check_Size
7796 Biased : out Boolean)
7798 UT : constant Entity_Id := Underlying_Type (T);
7804 -- Reject patently improper size values.
7806 if Is_Elementary_Type (T)
7807 and then Siz > UI_From_Int (Int'Last)
7809 Error_Msg_N ("Size value too large for elementary type", N);
7811 if Nkind (Original_Node (N)) = N_Op_Expon then
7813 ("\maybe '* was meant, rather than '*'*", Original_Node (N));
7817 -- Dismiss cases for generic types or types with previous errors
7820 or else UT = Any_Type
7821 or else Is_Generic_Type (UT)
7822 or else Is_Generic_Type (Root_Type (UT))
7826 -- Check case of bit packed array
7828 elsif Is_Array_Type (UT)
7829 and then Known_Static_Component_Size (UT)
7830 and then Is_Bit_Packed_Array (UT)
7838 Asiz := Component_Size (UT);
7839 Indx := First_Index (UT);
7841 Ityp := Etype (Indx);
7843 -- If non-static bound, then we are not in the business of
7844 -- trying to check the length, and indeed an error will be
7845 -- issued elsewhere, since sizes of non-static array types
7846 -- cannot be set implicitly or explicitly.
7848 if not Is_Static_Subtype (Ityp) then
7852 -- Otherwise accumulate next dimension
7854 Asiz := Asiz * (Expr_Value (Type_High_Bound (Ityp)) -
7855 Expr_Value (Type_Low_Bound (Ityp)) +
7859 exit when No (Indx);
7865 Error_Msg_Uint_1 := Asiz;
7867 ("size for& too small, minimum allowed is ^", N, T);
7868 Set_Esize (T, Asiz);
7869 Set_RM_Size (T, Asiz);
7873 -- All other composite types are ignored
7875 elsif Is_Composite_Type (UT) then
7878 -- For fixed-point types, don't check minimum if type is not frozen,
7879 -- since we don't know all the characteristics of the type that can
7880 -- affect the size (e.g. a specified small) till freeze time.
7882 elsif Is_Fixed_Point_Type (UT)
7883 and then not Is_Frozen (UT)
7887 -- Cases for which a minimum check is required
7890 -- Ignore if specified size is correct for the type
7892 if Known_Esize (UT) and then Siz = Esize (UT) then
7896 -- Otherwise get minimum size
7898 M := UI_From_Int (Minimum_Size (UT));
7902 -- Size is less than minimum size, but one possibility remains
7903 -- that we can manage with the new size if we bias the type.
7905 M := UI_From_Int (Minimum_Size (UT, Biased => True));
7908 Error_Msg_Uint_1 := M;
7910 ("size for& too small, minimum allowed is ^", N, T);
7920 -------------------------
7921 -- Get_Alignment_Value --
7922 -------------------------
7924 function Get_Alignment_Value (Expr : Node_Id) return Uint is
7925 Align : constant Uint := Static_Integer (Expr);
7928 if Align = No_Uint then
7931 elsif Align <= 0 then
7932 Error_Msg_N ("alignment value must be positive", Expr);
7936 for J in Int range 0 .. 64 loop
7938 M : constant Uint := Uint_2 ** J;
7941 exit when M = Align;
7945 ("alignment value must be power of 2", Expr);
7953 end Get_Alignment_Value;
7955 -------------------------------------
7956 -- Inherit_Aspects_At_Freeze_Point --
7957 -------------------------------------
7959 procedure Inherit_Aspects_At_Freeze_Point (Typ : Entity_Id) is
7960 function Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
7961 (Rep_Item : Node_Id) return Boolean;
7962 -- This routine checks if Rep_Item is either a pragma or an aspect
7963 -- specification node whose correponding pragma (if any) is present in
7964 -- the Rep Item chain of the entity it has been specified to.
7966 --------------------------------------------------
7967 -- Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item --
7968 --------------------------------------------------
7970 function Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
7971 (Rep_Item : Node_Id) return Boolean
7974 return Nkind (Rep_Item) = N_Pragma
7975 or else Present_In_Rep_Item
7976 (Entity (Rep_Item), Aspect_Rep_Item (Rep_Item));
7977 end Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item;
7979 -- Start of processing for Inherit_Aspects_At_Freeze_Point
7982 -- A representation item is either subtype-specific (Size and Alignment
7983 -- clauses) or type-related (all others). Subtype-specific aspects may
7984 -- differ for different subtypes of the same type (RM 13.1.8).
7986 -- A derived type inherits each type-related representation aspect of
7987 -- its parent type that was directly specified before the declaration of
7988 -- the derived type (RM 13.1.15).
7990 -- A derived subtype inherits each subtype-specific representation
7991 -- aspect of its parent subtype that was directly specified before the
7992 -- declaration of the derived type (RM 13.1.15).
7994 -- The general processing involves inheriting a representation aspect
7995 -- from a parent type whenever the first rep item (aspect specification,
7996 -- attribute definition clause, pragma) corresponding to the given
7997 -- representation aspect in the rep item chain of Typ, if any, isn't
7998 -- directly specified to Typ but to one of its parents.
8000 -- ??? Note that, for now, just a limited number of representation
8001 -- aspects have been inherited here so far. Many of them are
8002 -- still inherited in Sem_Ch3. This will be fixed soon. Here is
8003 -- a non- exhaustive list of aspects that likely also need to
8004 -- be moved to this routine: Alignment, Component_Alignment,
8005 -- Component_Size, Machine_Radix, Object_Size, Pack, Predicates,
8006 -- Preelaborable_Initialization, RM_Size and Small.
8008 if Nkind (Parent (Typ)) = N_Private_Extension_Declaration then
8014 if not Has_Rep_Item (Typ, Name_Ada_05, Name_Ada_2005, False)
8015 and then Has_Rep_Item (Typ, Name_Ada_05, Name_Ada_2005)
8016 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
8017 (Get_Rep_Item (Typ, Name_Ada_05, Name_Ada_2005))
8019 Set_Is_Ada_2005_Only (Typ);
8024 if not Has_Rep_Item (Typ, Name_Ada_12, Name_Ada_2012, False)
8025 and then Has_Rep_Item (Typ, Name_Ada_12, Name_Ada_2012)
8026 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
8027 (Get_Rep_Item (Typ, Name_Ada_12, Name_Ada_2012))
8029 Set_Is_Ada_2012_Only (Typ);
8034 if not Has_Rep_Item (Typ, Name_Atomic, Name_Shared, False)
8035 and then Has_Rep_Pragma (Typ, Name_Atomic, Name_Shared)
8036 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
8037 (Get_Rep_Item (Typ, Name_Atomic, Name_Shared))
8039 Set_Is_Atomic (Typ);
8040 Set_Treat_As_Volatile (Typ);
8041 Set_Is_Volatile (Typ);
8044 -- Default_Component_Value
8046 if Is_Array_Type (Typ)
8047 and then Has_Rep_Item (Typ, Name_Default_Component_Value, False)
8048 and then Has_Rep_Item (Typ, Name_Default_Component_Value)
8050 Set_Default_Aspect_Component_Value (Typ,
8051 Default_Aspect_Component_Value
8052 (Entity (Get_Rep_Item (Typ, Name_Default_Component_Value))));
8057 if Is_Scalar_Type (Typ)
8058 and then Has_Rep_Item (Typ, Name_Default_Value, False)
8059 and then Has_Rep_Item (Typ, Name_Default_Value)
8061 Set_Default_Aspect_Value (Typ,
8062 Default_Aspect_Value
8063 (Entity (Get_Rep_Item (Typ, Name_Default_Value))));
8068 if not Has_Rep_Item (Typ, Name_Discard_Names, False)
8069 and then Has_Rep_Item (Typ, Name_Discard_Names)
8070 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
8071 (Get_Rep_Item (Typ, Name_Discard_Names))
8073 Set_Discard_Names (Typ);
8078 if not Has_Rep_Item (Typ, Name_Invariant, False)
8079 and then Has_Rep_Item (Typ, Name_Invariant)
8080 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
8081 (Get_Rep_Item (Typ, Name_Invariant))
8083 Set_Has_Invariants (Typ);
8085 if Class_Present (Get_Rep_Item (Typ, Name_Invariant)) then
8086 Set_Has_Inheritable_Invariants (Typ);
8092 if not Has_Rep_Item (Typ, Name_Volatile, False)
8093 and then Has_Rep_Item (Typ, Name_Volatile)
8094 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
8095 (Get_Rep_Item (Typ, Name_Volatile))
8097 Set_Treat_As_Volatile (Typ);
8098 Set_Is_Volatile (Typ);
8101 -- Inheritance for derived types only
8103 if Is_Derived_Type (Typ) then
8105 Bas_Typ : constant Entity_Id := Base_Type (Typ);
8106 Imp_Bas_Typ : constant Entity_Id := Implementation_Base_Type (Typ);
8109 -- Atomic_Components
8111 if not Has_Rep_Item (Typ, Name_Atomic_Components, False)
8112 and then Has_Rep_Item (Typ, Name_Atomic_Components)
8113 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
8114 (Get_Rep_Item (Typ, Name_Atomic_Components))
8116 Set_Has_Atomic_Components (Imp_Bas_Typ);
8119 -- Volatile_Components
8121 if not Has_Rep_Item (Typ, Name_Volatile_Components, False)
8122 and then Has_Rep_Item (Typ, Name_Volatile_Components)
8123 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
8124 (Get_Rep_Item (Typ, Name_Volatile_Components))
8126 Set_Has_Volatile_Components (Imp_Bas_Typ);
8129 -- Finalize_Storage_Only.
8131 if not Has_Rep_Pragma (Typ, Name_Finalize_Storage_Only, False)
8132 and then Has_Rep_Pragma (Typ, Name_Finalize_Storage_Only)
8134 Set_Finalize_Storage_Only (Bas_Typ);
8137 -- Universal_Aliasing
8139 if not Has_Rep_Item (Typ, Name_Universal_Aliasing, False)
8140 and then Has_Rep_Item (Typ, Name_Universal_Aliasing)
8141 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
8142 (Get_Rep_Item (Typ, Name_Universal_Aliasing))
8144 Set_Universal_Aliasing (Imp_Bas_Typ);
8147 -- Record type specific aspects
8149 if Is_Record_Type (Typ) then
8153 if not Has_Rep_Item (Typ, Name_Bit_Order, False)
8154 and then Has_Rep_Item (Typ, Name_Bit_Order)
8156 Set_Reverse_Bit_Order (Bas_Typ,
8157 Reverse_Bit_Order (Entity (Name
8158 (Get_Rep_Item (Typ, Name_Bit_Order)))));
8161 -- Scalar_Storage_Order
8163 if not Has_Rep_Item (Typ, Name_Scalar_Storage_Order, False)
8164 and then Has_Rep_Item (Typ, Name_Scalar_Storage_Order)
8166 Set_Reverse_Storage_Order (Bas_Typ,
8167 Reverse_Storage_Order (Entity (Name
8168 (Get_Rep_Item (Typ, Name_Scalar_Storage_Order)))));
8173 end Inherit_Aspects_At_Freeze_Point;
8179 procedure Initialize is
8181 Address_Clause_Checks.Init;
8182 Independence_Checks.Init;
8183 Unchecked_Conversions.Init;
8186 -------------------------
8187 -- Is_Operational_Item --
8188 -------------------------
8190 function Is_Operational_Item (N : Node_Id) return Boolean is
8192 if Nkind (N) /= N_Attribute_Definition_Clause then
8196 Id : constant Attribute_Id := Get_Attribute_Id (Chars (N));
8198 return Id = Attribute_Input
8199 or else Id = Attribute_Output
8200 or else Id = Attribute_Read
8201 or else Id = Attribute_Write
8202 or else Id = Attribute_External_Tag;
8205 end Is_Operational_Item;
8211 function Minimum_Size
8213 Biased : Boolean := False) return Nat
8215 Lo : Uint := No_Uint;
8216 Hi : Uint := No_Uint;
8217 LoR : Ureal := No_Ureal;
8218 HiR : Ureal := No_Ureal;
8219 LoSet : Boolean := False;
8220 HiSet : Boolean := False;
8224 R_Typ : constant Entity_Id := Root_Type (T);
8227 -- If bad type, return 0
8229 if T = Any_Type then
8232 -- For generic types, just return zero. There cannot be any legitimate
8233 -- need to know such a size, but this routine may be called with a
8234 -- generic type as part of normal processing.
8236 elsif Is_Generic_Type (R_Typ)
8237 or else R_Typ = Any_Type
8241 -- Access types. Normally an access type cannot have a size smaller
8242 -- than the size of System.Address. The exception is on VMS, where
8243 -- we have short and long addresses, and it is possible for an access
8244 -- type to have a short address size (and thus be less than the size
8245 -- of System.Address itself). We simply skip the check for VMS, and
8246 -- leave it to the back end to do the check.
8248 elsif Is_Access_Type (T) then
8249 if OpenVMS_On_Target then
8252 return System_Address_Size;
8255 -- Floating-point types
8257 elsif Is_Floating_Point_Type (T) then
8258 return UI_To_Int (Esize (R_Typ));
8262 elsif Is_Discrete_Type (T) then
8264 -- The following loop is looking for the nearest compile time known
8265 -- bounds following the ancestor subtype chain. The idea is to find
8266 -- the most restrictive known bounds information.
8270 if Ancest = Any_Type or else Etype (Ancest) = Any_Type then
8275 if Compile_Time_Known_Value (Type_Low_Bound (Ancest)) then
8276 Lo := Expr_Rep_Value (Type_Low_Bound (Ancest));
8283 if Compile_Time_Known_Value (Type_High_Bound (Ancest)) then
8284 Hi := Expr_Rep_Value (Type_High_Bound (Ancest));
8290 Ancest := Ancestor_Subtype (Ancest);
8293 Ancest := Base_Type (T);
8295 if Is_Generic_Type (Ancest) then
8301 -- Fixed-point types. We can't simply use Expr_Value to get the
8302 -- Corresponding_Integer_Value values of the bounds, since these do not
8303 -- get set till the type is frozen, and this routine can be called
8304 -- before the type is frozen. Similarly the test for bounds being static
8305 -- needs to include the case where we have unanalyzed real literals for
8308 elsif Is_Fixed_Point_Type (T) then
8310 -- The following loop is looking for the nearest compile time known
8311 -- bounds following the ancestor subtype chain. The idea is to find
8312 -- the most restrictive known bounds information.
8316 if Ancest = Any_Type or else Etype (Ancest) = Any_Type then
8320 -- Note: In the following two tests for LoSet and HiSet, it may
8321 -- seem redundant to test for N_Real_Literal here since normally
8322 -- one would assume that the test for the value being known at
8323 -- compile time includes this case. However, there is a glitch.
8324 -- If the real literal comes from folding a non-static expression,
8325 -- then we don't consider any non- static expression to be known
8326 -- at compile time if we are in configurable run time mode (needed
8327 -- in some cases to give a clearer definition of what is and what
8328 -- is not accepted). So the test is indeed needed. Without it, we
8329 -- would set neither Lo_Set nor Hi_Set and get an infinite loop.
8332 if Nkind (Type_Low_Bound (Ancest)) = N_Real_Literal
8333 or else Compile_Time_Known_Value (Type_Low_Bound (Ancest))
8335 LoR := Expr_Value_R (Type_Low_Bound (Ancest));
8342 if Nkind (Type_High_Bound (Ancest)) = N_Real_Literal
8343 or else Compile_Time_Known_Value (Type_High_Bound (Ancest))
8345 HiR := Expr_Value_R (Type_High_Bound (Ancest));
8351 Ancest := Ancestor_Subtype (Ancest);
8354 Ancest := Base_Type (T);
8356 if Is_Generic_Type (Ancest) then
8362 Lo := UR_To_Uint (LoR / Small_Value (T));
8363 Hi := UR_To_Uint (HiR / Small_Value (T));
8365 -- No other types allowed
8368 raise Program_Error;
8371 -- Fall through with Hi and Lo set. Deal with biased case
8374 and then not Is_Fixed_Point_Type (T)
8375 and then not (Is_Enumeration_Type (T)
8376 and then Has_Non_Standard_Rep (T)))
8377 or else Has_Biased_Representation (T)
8383 -- Signed case. Note that we consider types like range 1 .. -1 to be
8384 -- signed for the purpose of computing the size, since the bounds have
8385 -- to be accommodated in the base type.
8387 if Lo < 0 or else Hi < 0 then
8391 -- S = size, B = 2 ** (size - 1) (can accommodate -B .. +(B - 1))
8392 -- Note that we accommodate the case where the bounds cross. This
8393 -- can happen either because of the way the bounds are declared
8394 -- or because of the algorithm in Freeze_Fixed_Point_Type.
8408 -- If both bounds are positive, make sure that both are represen-
8409 -- table in the case where the bounds are crossed. This can happen
8410 -- either because of the way the bounds are declared, or because of
8411 -- the algorithm in Freeze_Fixed_Point_Type.
8417 -- S = size, (can accommodate 0 .. (2**size - 1))
8420 while Hi >= Uint_2 ** S loop
8428 ---------------------------
8429 -- New_Stream_Subprogram --
8430 ---------------------------
8432 procedure New_Stream_Subprogram
8436 Nam : TSS_Name_Type)
8438 Loc : constant Source_Ptr := Sloc (N);
8439 Sname : constant Name_Id := Make_TSS_Name (Base_Type (Ent), Nam);
8440 Subp_Id : Entity_Id;
8441 Subp_Decl : Node_Id;
8445 Defer_Declaration : constant Boolean :=
8446 Is_Tagged_Type (Ent) or else Is_Private_Type (Ent);
8447 -- For a tagged type, there is a declaration for each stream attribute
8448 -- at the freeze point, and we must generate only a completion of this
8449 -- declaration. We do the same for private types, because the full view
8450 -- might be tagged. Otherwise we generate a declaration at the point of
8451 -- the attribute definition clause.
8453 function Build_Spec return Node_Id;
8454 -- Used for declaration and renaming declaration, so that this is
8455 -- treated as a renaming_as_body.
8461 function Build_Spec return Node_Id is
8462 Out_P : constant Boolean := (Nam = TSS_Stream_Read);
8465 T_Ref : constant Node_Id := New_Reference_To (Etyp, Loc);
8468 Subp_Id := Make_Defining_Identifier (Loc, Sname);
8470 -- S : access Root_Stream_Type'Class
8472 Formals := New_List (
8473 Make_Parameter_Specification (Loc,
8474 Defining_Identifier =>
8475 Make_Defining_Identifier (Loc, Name_S),
8477 Make_Access_Definition (Loc,
8480 Designated_Type (Etype (F)), Loc))));
8482 if Nam = TSS_Stream_Input then
8483 Spec := Make_Function_Specification (Loc,
8484 Defining_Unit_Name => Subp_Id,
8485 Parameter_Specifications => Formals,
8486 Result_Definition => T_Ref);
8491 Make_Parameter_Specification (Loc,
8492 Defining_Identifier => Make_Defining_Identifier (Loc, Name_V),
8493 Out_Present => Out_P,
8494 Parameter_Type => T_Ref));
8497 Make_Procedure_Specification (Loc,
8498 Defining_Unit_Name => Subp_Id,
8499 Parameter_Specifications => Formals);
8505 -- Start of processing for New_Stream_Subprogram
8508 F := First_Formal (Subp);
8510 if Ekind (Subp) = E_Procedure then
8511 Etyp := Etype (Next_Formal (F));
8513 Etyp := Etype (Subp);
8516 -- Prepare subprogram declaration and insert it as an action on the
8517 -- clause node. The visibility for this entity is used to test for
8518 -- visibility of the attribute definition clause (in the sense of
8519 -- 8.3(23) as amended by AI-195).
8521 if not Defer_Declaration then
8523 Make_Subprogram_Declaration (Loc,
8524 Specification => Build_Spec);
8526 -- For a tagged type, there is always a visible declaration for each
8527 -- stream TSS (it is a predefined primitive operation), and the
8528 -- completion of this declaration occurs at the freeze point, which is
8529 -- not always visible at places where the attribute definition clause is
8530 -- visible. So, we create a dummy entity here for the purpose of
8531 -- tracking the visibility of the attribute definition clause itself.
8535 Make_Defining_Identifier (Loc, New_External_Name (Sname, 'V'));
8537 Make_Object_Declaration (Loc,
8538 Defining_Identifier => Subp_Id,
8539 Object_Definition => New_Occurrence_Of (Standard_Boolean, Loc));
8542 Insert_Action (N, Subp_Decl);
8543 Set_Entity (N, Subp_Id);
8546 Make_Subprogram_Renaming_Declaration (Loc,
8547 Specification => Build_Spec,
8548 Name => New_Reference_To (Subp, Loc));
8550 if Defer_Declaration then
8551 Set_TSS (Base_Type (Ent), Subp_Id);
8553 Insert_Action (N, Subp_Decl);
8554 Copy_TSS (Subp_Id, Base_Type (Ent));
8556 end New_Stream_Subprogram;
8558 ------------------------
8559 -- Rep_Item_Too_Early --
8560 ------------------------
8562 function Rep_Item_Too_Early (T : Entity_Id; N : Node_Id) return Boolean is
8564 -- Cannot apply non-operational rep items to generic types
8566 if Is_Operational_Item (N) then
8570 and then Is_Generic_Type (Root_Type (T))
8572 Error_Msg_N ("representation item not allowed for generic type", N);
8576 -- Otherwise check for incomplete type
8578 if Is_Incomplete_Or_Private_Type (T)
8579 and then No (Underlying_Type (T))
8581 (Nkind (N) /= N_Pragma
8582 or else Get_Pragma_Id (N) /= Pragma_Import)
8585 ("representation item must be after full type declaration", N);
8588 -- If the type has incomplete components, a representation clause is
8589 -- illegal but stream attributes and Convention pragmas are correct.
8591 elsif Has_Private_Component (T) then
8592 if Nkind (N) = N_Pragma then
8596 ("representation item must appear after type is fully defined",
8603 end Rep_Item_Too_Early;
8605 -----------------------
8606 -- Rep_Item_Too_Late --
8607 -----------------------
8609 function Rep_Item_Too_Late
8612 FOnly : Boolean := False) return Boolean
8615 Parent_Type : Entity_Id;
8618 -- Output the too late message. Note that this is not considered a
8619 -- serious error, since the effect is simply that we ignore the
8620 -- representation clause in this case.
8626 procedure Too_Late is
8628 Error_Msg_N ("|representation item appears too late!", N);
8631 -- Start of processing for Rep_Item_Too_Late
8634 -- First make sure entity is not frozen (RM 13.1(9))
8638 -- Exclude imported types, which may be frozen if they appear in a
8639 -- representation clause for a local type.
8641 and then not From_With_Type (T)
8643 -- Exclude generated entitiesa (not coming from source). The common
8644 -- case is when we generate a renaming which prematurely freezes the
8645 -- renamed internal entity, but we still want to be able to set copies
8646 -- of attribute values such as Size/Alignment.
8648 and then Comes_From_Source (T)
8651 S := First_Subtype (T);
8653 if Present (Freeze_Node (S)) then
8655 ("?no more representation items for }", Freeze_Node (S), S);
8660 -- Check for case of non-tagged derived type whose parent either has
8661 -- primitive operations, or is a by reference type (RM 13.1(10)).
8665 and then Is_Derived_Type (T)
8666 and then not Is_Tagged_Type (T)
8668 Parent_Type := Etype (Base_Type (T));
8670 if Has_Primitive_Operations (Parent_Type) then
8673 ("primitive operations already defined for&!", N, Parent_Type);
8676 elsif Is_By_Reference_Type (Parent_Type) then
8679 ("parent type & is a by reference type!", N, Parent_Type);
8684 -- No error, link item into head of chain of rep items for the entity,
8685 -- but avoid chaining if we have an overloadable entity, and the pragma
8686 -- is one that can apply to multiple overloaded entities.
8688 if Is_Overloadable (T)
8689 and then Nkind (N) = N_Pragma
8692 Pname : constant Name_Id := Pragma_Name (N);
8694 if Pname = Name_Convention or else
8695 Pname = Name_Import or else
8696 Pname = Name_Export or else
8697 Pname = Name_External or else
8698 Pname = Name_Interface
8705 Record_Rep_Item (T, N);
8707 end Rep_Item_Too_Late;
8709 -------------------------------------
8710 -- Replace_Type_References_Generic --
8711 -------------------------------------
8713 procedure Replace_Type_References_Generic (N : Node_Id; TName : Name_Id) is
8715 function Replace_Node (N : Node_Id) return Traverse_Result;
8716 -- Processes a single node in the traversal procedure below, checking
8717 -- if node N should be replaced, and if so, doing the replacement.
8719 procedure Replace_Type_Refs is new Traverse_Proc (Replace_Node);
8720 -- This instantiation provides the body of Replace_Type_References
8726 function Replace_Node (N : Node_Id) return Traverse_Result is
8731 -- Case of identifier
8733 if Nkind (N) = N_Identifier then
8735 -- If not the type name, all done with this node
8737 if Chars (N) /= TName then
8740 -- Otherwise do the replacement and we are done with this node
8743 Replace_Type_Reference (N);
8747 -- Case of selected component (which is what a qualification
8748 -- looks like in the unanalyzed tree, which is what we have.
8750 elsif Nkind (N) = N_Selected_Component then
8752 -- If selector name is not our type, keeping going (we might
8753 -- still have an occurrence of the type in the prefix).
8755 if Nkind (Selector_Name (N)) /= N_Identifier
8756 or else Chars (Selector_Name (N)) /= TName
8760 -- Selector name is our type, check qualification
8763 -- Loop through scopes and prefixes, doing comparison
8768 -- Continue if no more scopes or scope with no name
8770 if No (S) or else Nkind (S) not in N_Has_Chars then
8774 -- Do replace if prefix is an identifier matching the
8775 -- scope that we are currently looking at.
8777 if Nkind (P) = N_Identifier
8778 and then Chars (P) = Chars (S)
8780 Replace_Type_Reference (N);
8784 -- Go check scope above us if prefix is itself of the
8785 -- form of a selected component, whose selector matches
8786 -- the scope we are currently looking at.
8788 if Nkind (P) = N_Selected_Component
8789 and then Nkind (Selector_Name (P)) = N_Identifier
8790 and then Chars (Selector_Name (P)) = Chars (S)
8795 -- For anything else, we don't have a match, so keep on
8796 -- going, there are still some weird cases where we may
8797 -- still have a replacement within the prefix.
8805 -- Continue for any other node kind
8813 Replace_Type_Refs (N);
8814 end Replace_Type_References_Generic;
8816 -------------------------
8817 -- Same_Representation --
8818 -------------------------
8820 function Same_Representation (Typ1, Typ2 : Entity_Id) return Boolean is
8821 T1 : constant Entity_Id := Underlying_Type (Typ1);
8822 T2 : constant Entity_Id := Underlying_Type (Typ2);
8825 -- A quick check, if base types are the same, then we definitely have
8826 -- the same representation, because the subtype specific representation
8827 -- attributes (Size and Alignment) do not affect representation from
8828 -- the point of view of this test.
8830 if Base_Type (T1) = Base_Type (T2) then
8833 elsif Is_Private_Type (Base_Type (T2))
8834 and then Base_Type (T1) = Full_View (Base_Type (T2))
8839 -- Tagged types never have differing representations
8841 if Is_Tagged_Type (T1) then
8845 -- Representations are definitely different if conventions differ
8847 if Convention (T1) /= Convention (T2) then
8851 -- Representations are different if component alignments differ
8853 if (Is_Record_Type (T1) or else Is_Array_Type (T1))
8855 (Is_Record_Type (T2) or else Is_Array_Type (T2))
8856 and then Component_Alignment (T1) /= Component_Alignment (T2)
8861 -- For arrays, the only real issue is component size. If we know the
8862 -- component size for both arrays, and it is the same, then that's
8863 -- good enough to know we don't have a change of representation.
8865 if Is_Array_Type (T1) then
8866 if Known_Component_Size (T1)
8867 and then Known_Component_Size (T2)
8868 and then Component_Size (T1) = Component_Size (T2)
8870 if VM_Target = No_VM then
8873 -- In VM targets the representation of arrays with aliased
8874 -- components differs from arrays with non-aliased components
8877 return Has_Aliased_Components (Base_Type (T1))
8879 Has_Aliased_Components (Base_Type (T2));
8884 -- Types definitely have same representation if neither has non-standard
8885 -- representation since default representations are always consistent.
8886 -- If only one has non-standard representation, and the other does not,
8887 -- then we consider that they do not have the same representation. They
8888 -- might, but there is no way of telling early enough.
8890 if Has_Non_Standard_Rep (T1) then
8891 if not Has_Non_Standard_Rep (T2) then
8895 return not Has_Non_Standard_Rep (T2);
8898 -- Here the two types both have non-standard representation, and we need
8899 -- to determine if they have the same non-standard representation.
8901 -- For arrays, we simply need to test if the component sizes are the
8902 -- same. Pragma Pack is reflected in modified component sizes, so this
8903 -- check also deals with pragma Pack.
8905 if Is_Array_Type (T1) then
8906 return Component_Size (T1) = Component_Size (T2);
8908 -- Tagged types always have the same representation, because it is not
8909 -- possible to specify different representations for common fields.
8911 elsif Is_Tagged_Type (T1) then
8914 -- Case of record types
8916 elsif Is_Record_Type (T1) then
8918 -- Packed status must conform
8920 if Is_Packed (T1) /= Is_Packed (T2) then
8923 -- Otherwise we must check components. Typ2 maybe a constrained
8924 -- subtype with fewer components, so we compare the components
8925 -- of the base types.
8928 Record_Case : declare
8929 CD1, CD2 : Entity_Id;
8931 function Same_Rep return Boolean;
8932 -- CD1 and CD2 are either components or discriminants. This
8933 -- function tests whether the two have the same representation
8939 function Same_Rep return Boolean is
8941 if No (Component_Clause (CD1)) then
8942 return No (Component_Clause (CD2));
8946 Present (Component_Clause (CD2))
8948 Component_Bit_Offset (CD1) = Component_Bit_Offset (CD2)
8950 Esize (CD1) = Esize (CD2);
8954 -- Start of processing for Record_Case
8957 if Has_Discriminants (T1) then
8958 CD1 := First_Discriminant (T1);
8959 CD2 := First_Discriminant (T2);
8961 -- The number of discriminants may be different if the
8962 -- derived type has fewer (constrained by values). The
8963 -- invisible discriminants retain the representation of
8964 -- the original, so the discrepancy does not per se
8965 -- indicate a different representation.
8968 and then Present (CD2)
8970 if not Same_Rep then
8973 Next_Discriminant (CD1);
8974 Next_Discriminant (CD2);
8979 CD1 := First_Component (Underlying_Type (Base_Type (T1)));
8980 CD2 := First_Component (Underlying_Type (Base_Type (T2)));
8982 while Present (CD1) loop
8983 if not Same_Rep then
8986 Next_Component (CD1);
8987 Next_Component (CD2);
8995 -- For enumeration types, we must check each literal to see if the
8996 -- representation is the same. Note that we do not permit enumeration
8997 -- representation clauses for Character and Wide_Character, so these
8998 -- cases were already dealt with.
9000 elsif Is_Enumeration_Type (T1) then
9001 Enumeration_Case : declare
9005 L1 := First_Literal (T1);
9006 L2 := First_Literal (T2);
9008 while Present (L1) loop
9009 if Enumeration_Rep (L1) /= Enumeration_Rep (L2) then
9019 end Enumeration_Case;
9021 -- Any other types have the same representation for these purposes
9026 end Same_Representation;
9032 procedure Set_Biased
9036 Biased : Boolean := True)
9040 Set_Has_Biased_Representation (E);
9042 if Warn_On_Biased_Representation then
9044 ("?" & Msg & " forces biased representation for&", N, E);
9049 --------------------
9050 -- Set_Enum_Esize --
9051 --------------------
9053 procedure Set_Enum_Esize (T : Entity_Id) is
9061 -- Find the minimum standard size (8,16,32,64) that fits
9063 Lo := Enumeration_Rep (Entity (Type_Low_Bound (T)));
9064 Hi := Enumeration_Rep (Entity (Type_High_Bound (T)));
9067 if Lo >= -Uint_2**07 and then Hi < Uint_2**07 then
9068 Sz := Standard_Character_Size; -- May be > 8 on some targets
9070 elsif Lo >= -Uint_2**15 and then Hi < Uint_2**15 then
9073 elsif Lo >= -Uint_2**31 and then Hi < Uint_2**31 then
9076 else pragma Assert (Lo >= -Uint_2**63 and then Hi < Uint_2**63);
9081 if Hi < Uint_2**08 then
9082 Sz := Standard_Character_Size; -- May be > 8 on some targets
9084 elsif Hi < Uint_2**16 then
9087 elsif Hi < Uint_2**32 then
9090 else pragma Assert (Hi < Uint_2**63);
9095 -- That minimum is the proper size unless we have a foreign convention
9096 -- and the size required is 32 or less, in which case we bump the size
9097 -- up to 32. This is required for C and C++ and seems reasonable for
9098 -- all other foreign conventions.
9100 if Has_Foreign_Convention (T)
9101 and then Esize (T) < Standard_Integer_Size
9103 Init_Esize (T, Standard_Integer_Size);
9109 ------------------------------
9110 -- Validate_Address_Clauses --
9111 ------------------------------
9113 procedure Validate_Address_Clauses is
9115 for J in Address_Clause_Checks.First .. Address_Clause_Checks.Last loop
9117 ACCR : Address_Clause_Check_Record
9118 renames Address_Clause_Checks.Table (J);
9129 -- Skip processing of this entry if warning already posted
9131 if not Address_Warning_Posted (ACCR.N) then
9133 Expr := Original_Node (Expression (ACCR.N));
9137 X_Alignment := Alignment (ACCR.X);
9138 Y_Alignment := Alignment (ACCR.Y);
9140 -- Similarly obtain sizes
9142 X_Size := Esize (ACCR.X);
9143 Y_Size := Esize (ACCR.Y);
9145 -- Check for large object overlaying smaller one
9148 and then X_Size > Uint_0
9149 and then X_Size > Y_Size
9152 ("?& overlays smaller object", ACCR.N, ACCR.X);
9154 ("\?program execution may be erroneous", ACCR.N);
9155 Error_Msg_Uint_1 := X_Size;
9157 ("\?size of & is ^", ACCR.N, ACCR.X);
9158 Error_Msg_Uint_1 := Y_Size;
9160 ("\?size of & is ^", ACCR.N, ACCR.Y);
9162 -- Check for inadequate alignment, both of the base object
9163 -- and of the offset, if any.
9165 -- Note: we do not check the alignment if we gave a size
9166 -- warning, since it would likely be redundant.
9168 elsif Y_Alignment /= Uint_0
9169 and then (Y_Alignment < X_Alignment
9172 Nkind (Expr) = N_Attribute_Reference
9174 Attribute_Name (Expr) = Name_Address
9176 Has_Compatible_Alignment
9177 (ACCR.X, Prefix (Expr))
9178 /= Known_Compatible))
9181 ("?specified address for& may be inconsistent "
9185 ("\?program execution may be erroneous (RM 13.3(27))",
9187 Error_Msg_Uint_1 := X_Alignment;
9189 ("\?alignment of & is ^",
9191 Error_Msg_Uint_1 := Y_Alignment;
9193 ("\?alignment of & is ^",
9195 if Y_Alignment >= X_Alignment then
9197 ("\?but offset is not multiple of alignment",
9204 end Validate_Address_Clauses;
9206 ---------------------------
9207 -- Validate_Independence --
9208 ---------------------------
9210 procedure Validate_Independence is
9211 SU : constant Uint := UI_From_Int (System_Storage_Unit);
9219 procedure Check_Array_Type (Atyp : Entity_Id);
9220 -- Checks if the array type Atyp has independent components, and
9221 -- if not, outputs an appropriate set of error messages.
9223 procedure No_Independence;
9224 -- Output message that independence cannot be guaranteed
9226 function OK_Component (C : Entity_Id) return Boolean;
9227 -- Checks one component to see if it is independently accessible, and
9228 -- if so yields True, otherwise yields False if independent access
9229 -- cannot be guaranteed. This is a conservative routine, it only
9230 -- returns True if it knows for sure, it returns False if it knows
9231 -- there is a problem, or it cannot be sure there is no problem.
9233 procedure Reason_Bad_Component (C : Entity_Id);
9234 -- Outputs continuation message if a reason can be determined for
9235 -- the component C being bad.
9237 ----------------------
9238 -- Check_Array_Type --
9239 ----------------------
9241 procedure Check_Array_Type (Atyp : Entity_Id) is
9242 Ctyp : constant Entity_Id := Component_Type (Atyp);
9245 -- OK if no alignment clause, no pack, and no component size
9247 if not Has_Component_Size_Clause (Atyp)
9248 and then not Has_Alignment_Clause (Atyp)
9249 and then not Is_Packed (Atyp)
9254 -- Check actual component size
9256 if not Known_Component_Size (Atyp)
9257 or else not (Addressable (Component_Size (Atyp))
9258 and then Component_Size (Atyp) < 64)
9259 or else Component_Size (Atyp) mod Esize (Ctyp) /= 0
9263 -- Bad component size, check reason
9265 if Has_Component_Size_Clause (Atyp) then
9267 Get_Attribute_Definition_Clause
9268 (Atyp, Attribute_Component_Size);
9271 Error_Msg_Sloc := Sloc (P);
9272 Error_Msg_N ("\because of Component_Size clause#", N);
9277 if Is_Packed (Atyp) then
9278 P := Get_Rep_Pragma (Atyp, Name_Pack);
9281 Error_Msg_Sloc := Sloc (P);
9282 Error_Msg_N ("\because of pragma Pack#", N);
9287 -- No reason found, just return
9292 -- Array type is OK independence-wise
9295 end Check_Array_Type;
9297 ---------------------
9298 -- No_Independence --
9299 ---------------------
9301 procedure No_Independence is
9303 if Pragma_Name (N) = Name_Independent then
9305 ("independence cannot be guaranteed for&", N, E);
9308 ("independent components cannot be guaranteed for&", N, E);
9310 end No_Independence;
9316 function OK_Component (C : Entity_Id) return Boolean is
9317 Rec : constant Entity_Id := Scope (C);
9318 Ctyp : constant Entity_Id := Etype (C);
9321 -- OK if no component clause, no Pack, and no alignment clause
9323 if No (Component_Clause (C))
9324 and then not Is_Packed (Rec)
9325 and then not Has_Alignment_Clause (Rec)
9330 -- Here we look at the actual component layout. A component is
9331 -- addressable if its size is a multiple of the Esize of the
9332 -- component type, and its starting position in the record has
9333 -- appropriate alignment, and the record itself has appropriate
9334 -- alignment to guarantee the component alignment.
9336 -- Make sure sizes are static, always assume the worst for any
9337 -- cases where we cannot check static values.
9339 if not (Known_Static_Esize (C)
9340 and then Known_Static_Esize (Ctyp))
9345 -- Size of component must be addressable or greater than 64 bits
9346 -- and a multiple of bytes.
9348 if not Addressable (Esize (C))
9349 and then Esize (C) < Uint_64
9354 -- Check size is proper multiple
9356 if Esize (C) mod Esize (Ctyp) /= 0 then
9360 -- Check alignment of component is OK
9362 if not Known_Component_Bit_Offset (C)
9363 or else Component_Bit_Offset (C) < Uint_0
9364 or else Component_Bit_Offset (C) mod Esize (Ctyp) /= 0
9369 -- Check alignment of record type is OK
9371 if not Known_Alignment (Rec)
9372 or else (Alignment (Rec) * SU) mod Esize (Ctyp) /= 0
9377 -- All tests passed, component is addressable
9382 --------------------------
9383 -- Reason_Bad_Component --
9384 --------------------------
9386 procedure Reason_Bad_Component (C : Entity_Id) is
9387 Rec : constant Entity_Id := Scope (C);
9388 Ctyp : constant Entity_Id := Etype (C);
9391 -- If component clause present assume that's the problem
9393 if Present (Component_Clause (C)) then
9394 Error_Msg_Sloc := Sloc (Component_Clause (C));
9395 Error_Msg_N ("\because of Component_Clause#", N);
9399 -- If pragma Pack clause present, assume that's the problem
9401 if Is_Packed (Rec) then
9402 P := Get_Rep_Pragma (Rec, Name_Pack);
9405 Error_Msg_Sloc := Sloc (P);
9406 Error_Msg_N ("\because of pragma Pack#", N);
9411 -- See if record has bad alignment clause
9413 if Has_Alignment_Clause (Rec)
9414 and then Known_Alignment (Rec)
9415 and then (Alignment (Rec) * SU) mod Esize (Ctyp) /= 0
9417 P := Get_Attribute_Definition_Clause (Rec, Attribute_Alignment);
9420 Error_Msg_Sloc := Sloc (P);
9421 Error_Msg_N ("\because of Alignment clause#", N);
9425 -- Couldn't find a reason, so return without a message
9428 end Reason_Bad_Component;
9430 -- Start of processing for Validate_Independence
9433 for J in Independence_Checks.First .. Independence_Checks.Last loop
9434 N := Independence_Checks.Table (J).N;
9435 E := Independence_Checks.Table (J).E;
9436 IC := Pragma_Name (N) = Name_Independent_Components;
9438 -- Deal with component case
9440 if Ekind (E) = E_Discriminant or else Ekind (E) = E_Component then
9441 if not OK_Component (E) then
9443 Reason_Bad_Component (E);
9448 -- Deal with record with Independent_Components
9450 if IC and then Is_Record_Type (E) then
9451 Comp := First_Component_Or_Discriminant (E);
9452 while Present (Comp) loop
9453 if not OK_Component (Comp) then
9455 Reason_Bad_Component (Comp);
9459 Next_Component_Or_Discriminant (Comp);
9463 -- Deal with address clause case
9465 if Is_Object (E) then
9466 Addr := Address_Clause (E);
9468 if Present (Addr) then
9470 Error_Msg_Sloc := Sloc (Addr);
9471 Error_Msg_N ("\because of Address clause#", N);
9476 -- Deal with independent components for array type
9478 if IC and then Is_Array_Type (E) then
9479 Check_Array_Type (E);
9482 -- Deal with independent components for array object
9484 if IC and then Is_Object (E) and then Is_Array_Type (Etype (E)) then
9485 Check_Array_Type (Etype (E));
9490 end Validate_Independence;
9492 -----------------------------------
9493 -- Validate_Unchecked_Conversion --
9494 -----------------------------------
9496 procedure Validate_Unchecked_Conversion
9498 Act_Unit : Entity_Id)
9505 -- Obtain source and target types. Note that we call Ancestor_Subtype
9506 -- here because the processing for generic instantiation always makes
9507 -- subtypes, and we want the original frozen actual types.
9509 -- If we are dealing with private types, then do the check on their
9510 -- fully declared counterparts if the full declarations have been
9511 -- encountered (they don't have to be visible, but they must exist!)
9513 Source := Ancestor_Subtype (Etype (First_Formal (Act_Unit)));
9515 if Is_Private_Type (Source)
9516 and then Present (Underlying_Type (Source))
9518 Source := Underlying_Type (Source);
9521 Target := Ancestor_Subtype (Etype (Act_Unit));
9523 -- If either type is generic, the instantiation happens within a generic
9524 -- unit, and there is nothing to check. The proper check will happen
9525 -- when the enclosing generic is instantiated.
9527 if Is_Generic_Type (Source) or else Is_Generic_Type (Target) then
9531 if Is_Private_Type (Target)
9532 and then Present (Underlying_Type (Target))
9534 Target := Underlying_Type (Target);
9537 -- Source may be unconstrained array, but not target
9539 if Is_Array_Type (Target)
9540 and then not Is_Constrained (Target)
9543 ("unchecked conversion to unconstrained array not allowed", N);
9547 -- Warn if conversion between two different convention pointers
9549 if Is_Access_Type (Target)
9550 and then Is_Access_Type (Source)
9551 and then Convention (Target) /= Convention (Source)
9552 and then Warn_On_Unchecked_Conversion
9554 -- Give warnings for subprogram pointers only on most targets. The
9555 -- exception is VMS, where data pointers can have different lengths
9556 -- depending on the pointer convention.
9558 if Is_Access_Subprogram_Type (Target)
9559 or else Is_Access_Subprogram_Type (Source)
9560 or else OpenVMS_On_Target
9563 ("?conversion between pointers with different conventions!", N);
9567 -- Warn if one of the operands is Ada.Calendar.Time. Do not emit a
9568 -- warning when compiling GNAT-related sources.
9570 if Warn_On_Unchecked_Conversion
9571 and then not In_Predefined_Unit (N)
9572 and then RTU_Loaded (Ada_Calendar)
9574 (Chars (Source) = Name_Time
9576 Chars (Target) = Name_Time)
9578 -- If Ada.Calendar is loaded and the name of one of the operands is
9579 -- Time, there is a good chance that this is Ada.Calendar.Time.
9582 Calendar_Time : constant Entity_Id :=
9583 Full_View (RTE (RO_CA_Time));
9585 pragma Assert (Present (Calendar_Time));
9587 if Source = Calendar_Time
9588 or else Target = Calendar_Time
9591 ("?representation of 'Time values may change between " &
9592 "'G'N'A'T versions", N);
9597 -- Make entry in unchecked conversion table for later processing by
9598 -- Validate_Unchecked_Conversions, which will check sizes and alignments
9599 -- (using values set by the back-end where possible). This is only done
9600 -- if the appropriate warning is active.
9602 if Warn_On_Unchecked_Conversion then
9603 Unchecked_Conversions.Append
9604 (New_Val => UC_Entry'
9609 -- If both sizes are known statically now, then back end annotation
9610 -- is not required to do a proper check but if either size is not
9611 -- known statically, then we need the annotation.
9613 if Known_Static_RM_Size (Source)
9614 and then Known_Static_RM_Size (Target)
9618 Back_Annotate_Rep_Info := True;
9622 -- If unchecked conversion to access type, and access type is declared
9623 -- in the same unit as the unchecked conversion, then set the flag
9624 -- No_Strict_Aliasing (no strict aliasing is implicit here)
9626 if Is_Access_Type (Target) and then
9627 In_Same_Source_Unit (Target, N)
9629 Set_No_Strict_Aliasing (Implementation_Base_Type (Target));
9632 -- Generate N_Validate_Unchecked_Conversion node for back end in case
9633 -- the back end needs to perform special validation checks.
9635 -- Shouldn't this be in Exp_Ch13, since the check only gets done if we
9636 -- have full expansion and the back end is called ???
9639 Make_Validate_Unchecked_Conversion (Sloc (N));
9640 Set_Source_Type (Vnode, Source);
9641 Set_Target_Type (Vnode, Target);
9643 -- If the unchecked conversion node is in a list, just insert before it.
9644 -- If not we have some strange case, not worth bothering about.
9646 if Is_List_Member (N) then
9647 Insert_After (N, Vnode);
9649 end Validate_Unchecked_Conversion;
9651 ------------------------------------
9652 -- Validate_Unchecked_Conversions --
9653 ------------------------------------
9655 procedure Validate_Unchecked_Conversions is
9657 for N in Unchecked_Conversions.First .. Unchecked_Conversions.Last loop
9659 T : UC_Entry renames Unchecked_Conversions.Table (N);
9661 Eloc : constant Source_Ptr := T.Eloc;
9662 Source : constant Entity_Id := T.Source;
9663 Target : constant Entity_Id := T.Target;
9669 -- This validation check, which warns if we have unequal sizes for
9670 -- unchecked conversion, and thus potentially implementation
9671 -- dependent semantics, is one of the few occasions on which we
9672 -- use the official RM size instead of Esize. See description in
9673 -- Einfo "Handling of Type'Size Values" for details.
9675 if Serious_Errors_Detected = 0
9676 and then Known_Static_RM_Size (Source)
9677 and then Known_Static_RM_Size (Target)
9679 -- Don't do the check if warnings off for either type, note the
9680 -- deliberate use of OR here instead of OR ELSE to get the flag
9681 -- Warnings_Off_Used set for both types if appropriate.
9683 and then not (Has_Warnings_Off (Source)
9685 Has_Warnings_Off (Target))
9687 Source_Siz := RM_Size (Source);
9688 Target_Siz := RM_Size (Target);
9690 if Source_Siz /= Target_Siz then
9692 ("?types for unchecked conversion have different sizes!",
9695 if All_Errors_Mode then
9696 Error_Msg_Name_1 := Chars (Source);
9697 Error_Msg_Uint_1 := Source_Siz;
9698 Error_Msg_Name_2 := Chars (Target);
9699 Error_Msg_Uint_2 := Target_Siz;
9700 Error_Msg ("\size of % is ^, size of % is ^?", Eloc);
9702 Error_Msg_Uint_1 := UI_Abs (Source_Siz - Target_Siz);
9704 if Is_Discrete_Type (Source)
9705 and then Is_Discrete_Type (Target)
9707 if Source_Siz > Target_Siz then
9709 ("\?^ high order bits of source will be ignored!",
9712 elsif Is_Unsigned_Type (Source) then
9714 ("\?source will be extended with ^ high order " &
9715 "zero bits?!", Eloc);
9719 ("\?source will be extended with ^ high order " &
9724 elsif Source_Siz < Target_Siz then
9725 if Is_Discrete_Type (Target) then
9726 if Bytes_Big_Endian then
9728 ("\?target value will include ^ undefined " &
9733 ("\?target value will include ^ undefined " &
9740 ("\?^ trailing bits of target value will be " &
9741 "undefined!", Eloc);
9744 else pragma Assert (Source_Siz > Target_Siz);
9746 ("\?^ trailing bits of source will be ignored!",
9753 -- If both types are access types, we need to check the alignment.
9754 -- If the alignment of both is specified, we can do it here.
9756 if Serious_Errors_Detected = 0
9757 and then Ekind (Source) in Access_Kind
9758 and then Ekind (Target) in Access_Kind
9759 and then Target_Strict_Alignment
9760 and then Present (Designated_Type (Source))
9761 and then Present (Designated_Type (Target))
9764 D_Source : constant Entity_Id := Designated_Type (Source);
9765 D_Target : constant Entity_Id := Designated_Type (Target);
9768 if Known_Alignment (D_Source)
9769 and then Known_Alignment (D_Target)
9772 Source_Align : constant Uint := Alignment (D_Source);
9773 Target_Align : constant Uint := Alignment (D_Target);
9776 if Source_Align < Target_Align
9777 and then not Is_Tagged_Type (D_Source)
9779 -- Suppress warning if warnings suppressed on either
9780 -- type or either designated type. Note the use of
9781 -- OR here instead of OR ELSE. That is intentional,
9782 -- we would like to set flag Warnings_Off_Used in
9783 -- all types for which warnings are suppressed.
9785 and then not (Has_Warnings_Off (D_Source)
9787 Has_Warnings_Off (D_Target)
9789 Has_Warnings_Off (Source)
9791 Has_Warnings_Off (Target))
9793 Error_Msg_Uint_1 := Target_Align;
9794 Error_Msg_Uint_2 := Source_Align;
9795 Error_Msg_Node_1 := D_Target;
9796 Error_Msg_Node_2 := D_Source;
9798 ("?alignment of & (^) is stricter than " &
9799 "alignment of & (^)!", Eloc);
9801 ("\?resulting access value may have invalid " &
9802 "alignment!", Eloc);
9810 end Validate_Unchecked_Conversions;