1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2013, Free Software Foundation, Inc. --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING3. If not, go to --
19 -- http://www.gnu.org/licenses for a complete copy of the license. --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
24 ------------------------------------------------------------------------------
26 with Aspects; use Aspects;
27 with Atree; use Atree;
28 with Checks; use Checks;
29 with Debug; use Debug;
30 with Einfo; use Einfo;
31 with Elists; use Elists;
32 with Errout; use Errout;
33 with Exp_Disp; use Exp_Disp;
34 with Exp_Tss; use Exp_Tss;
35 with Exp_Util; use Exp_Util;
37 with Lib.Xref; use Lib.Xref;
38 with Namet; use Namet;
39 with Nlists; use Nlists;
40 with Nmake; use Nmake;
42 with Restrict; use Restrict;
43 with Rident; use Rident;
44 with Rtsfind; use Rtsfind;
46 with Sem_Aux; use Sem_Aux;
47 with Sem_Ch3; use Sem_Ch3;
48 with Sem_Ch6; use Sem_Ch6;
49 with Sem_Ch8; use Sem_Ch8;
50 with Sem_Ch9; use Sem_Ch9;
51 with Sem_Dim; use Sem_Dim;
52 with Sem_Disp; use Sem_Disp;
53 with Sem_Eval; use Sem_Eval;
54 with Sem_Res; use Sem_Res;
55 with Sem_Type; use Sem_Type;
56 with Sem_Util; use Sem_Util;
57 with Sem_Warn; use Sem_Warn;
58 with Sinput; use Sinput;
59 with Snames; use Snames;
60 with Stand; use Stand;
61 with Sinfo; use Sinfo;
62 with Stringt; use Stringt;
63 with Targparm; use Targparm;
64 with Ttypes; use Ttypes;
65 with Tbuild; use Tbuild;
66 with Urealp; use Urealp;
67 with Warnsw; use Warnsw;
69 with GNAT.Heap_Sort_G;
71 package body Sem_Ch13 is
73 SSU : constant Pos := System_Storage_Unit;
74 -- Convenient short hand for commonly used constant
76 -----------------------
77 -- Local Subprograms --
78 -----------------------
80 procedure Alignment_Check_For_Size_Change (Typ : Entity_Id; Size : Uint);
81 -- This routine is called after setting one of the sizes of type entity
82 -- Typ to Size. The purpose is to deal with the situation of a derived
83 -- type whose inherited alignment is no longer appropriate for the new
84 -- size value. In this case, we reset the Alignment to unknown.
86 procedure Build_Predicate_Functions (Typ : Entity_Id; N : Node_Id);
87 -- If Typ has predicates (indicated by Has_Predicates being set for Typ,
88 -- then either there are pragma Predicate entries on the rep chain for the
89 -- type (note that Predicate aspects are converted to pragma Predicate), or
90 -- there are inherited aspects from a parent type, or ancestor subtypes.
91 -- This procedure builds the spec and body for the Predicate function that
92 -- tests these predicates. N is the freeze node for the type. The spec of
93 -- the function is inserted before the freeze node, and the body of the
94 -- function is inserted after the freeze node. If the predicate expression
95 -- has at least one Raise_Expression, then this procedure also builds the
96 -- M version of the predicate function for use in membership tests.
98 procedure Build_Static_Predicate
102 -- Given a predicated type Typ, where Typ is a discrete static subtype,
103 -- whose predicate expression is Expr, tests if Expr is a static predicate,
104 -- and if so, builds the predicate range list. Nam is the name of the one
105 -- argument to the predicate function. Occurrences of the type name in the
106 -- predicate expression have been replaced by identifier references to this
107 -- name, which is unique, so any identifier with Chars matching Nam must be
108 -- a reference to the type. If the predicate is non-static, this procedure
109 -- returns doing nothing. If the predicate is static, then the predicate
110 -- list is stored in Static_Predicate (Typ), and the Expr is rewritten as
111 -- a canonicalized membership operation.
113 function Get_Alignment_Value (Expr : Node_Id) return Uint;
114 -- Given the expression for an alignment value, returns the corresponding
115 -- Uint value. If the value is inappropriate, then error messages are
116 -- posted as required, and a value of No_Uint is returned.
118 function Is_Operational_Item (N : Node_Id) return Boolean;
119 -- A specification for a stream attribute is allowed before the full type
120 -- is declared, as explained in AI-00137 and the corrigendum. Attributes
121 -- that do not specify a representation characteristic are operational
124 procedure New_Stream_Subprogram
128 Nam : TSS_Name_Type);
129 -- Create a subprogram renaming of a given stream attribute to the
130 -- designated subprogram and then in the tagged case, provide this as a
131 -- primitive operation, or in the non-tagged case make an appropriate TSS
132 -- entry. This is more properly an expansion activity than just semantics,
133 -- but the presence of user-defined stream functions for limited types is a
134 -- legality check, which is why this takes place here rather than in
135 -- exp_ch13, where it was previously. Nam indicates the name of the TSS
136 -- function to be generated.
138 -- To avoid elaboration anomalies with freeze nodes, for untagged types
139 -- we generate both a subprogram declaration and a subprogram renaming
140 -- declaration, so that the attribute specification is handled as a
141 -- renaming_as_body. For tagged types, the specification is one of the
145 with procedure Replace_Type_Reference (N : Node_Id);
146 procedure Replace_Type_References_Generic (N : Node_Id; TName : Name_Id);
147 -- This is used to scan an expression for a predicate or invariant aspect
148 -- replacing occurrences of the name TName (the name of the subtype to
149 -- which the aspect applies) with appropriate references to the parameter
150 -- of the predicate function or invariant procedure. The procedure passed
151 -- as a generic parameter does the actual replacement of node N, which is
152 -- either a simple direct reference to TName, or a selected component that
153 -- represents an appropriately qualified occurrence of TName.
159 Biased : Boolean := True);
160 -- If Biased is True, sets Has_Biased_Representation flag for E, and
161 -- outputs a warning message at node N if Warn_On_Biased_Representation is
162 -- is True. This warning inserts the string Msg to describe the construct
165 ----------------------------------------------
166 -- Table for Validate_Unchecked_Conversions --
167 ----------------------------------------------
169 -- The following table collects unchecked conversions for validation.
170 -- Entries are made by Validate_Unchecked_Conversion and then the call
171 -- to Validate_Unchecked_Conversions does the actual error checking and
172 -- posting of warnings. The reason for this delayed processing is to take
173 -- advantage of back-annotations of size and alignment values performed by
176 -- Note: the reason we store a Source_Ptr value instead of a Node_Id is
177 -- that by the time Validate_Unchecked_Conversions is called, Sprint will
178 -- already have modified all Sloc values if the -gnatD option is set.
180 type UC_Entry is record
181 Eloc : Source_Ptr; -- node used for posting warnings
182 Source : Entity_Id; -- source type for unchecked conversion
183 Target : Entity_Id; -- target type for unchecked conversion
186 package Unchecked_Conversions is new Table.Table (
187 Table_Component_Type => UC_Entry,
188 Table_Index_Type => Int,
189 Table_Low_Bound => 1,
191 Table_Increment => 200,
192 Table_Name => "Unchecked_Conversions");
194 ----------------------------------------
195 -- Table for Validate_Address_Clauses --
196 ----------------------------------------
198 -- If an address clause has the form
200 -- for X'Address use Expr
202 -- where Expr is of the form Y'Address or recursively is a reference to a
203 -- constant of either of these forms, and X and Y are entities of objects,
204 -- then if Y has a smaller alignment than X, that merits a warning about
205 -- possible bad alignment. The following table collects address clauses of
206 -- this kind. We put these in a table so that they can be checked after the
207 -- back end has completed annotation of the alignments of objects, since we
208 -- can catch more cases that way.
210 type Address_Clause_Check_Record is record
212 -- The address clause
215 -- The entity of the object overlaying Y
218 -- The entity of the object being overlaid
221 -- Whether the address is offset within Y
224 package Address_Clause_Checks is new Table.Table (
225 Table_Component_Type => Address_Clause_Check_Record,
226 Table_Index_Type => Int,
227 Table_Low_Bound => 1,
229 Table_Increment => 200,
230 Table_Name => "Address_Clause_Checks");
232 -----------------------------------------
233 -- Adjust_Record_For_Reverse_Bit_Order --
234 -----------------------------------------
236 procedure Adjust_Record_For_Reverse_Bit_Order (R : Entity_Id) is
241 -- Processing depends on version of Ada
243 -- For Ada 95, we just renumber bits within a storage unit. We do the
244 -- same for Ada 83 mode, since we recognize the Bit_Order attribute in
245 -- Ada 83, and are free to add this extension.
247 if Ada_Version < Ada_2005 then
248 Comp := First_Component_Or_Discriminant (R);
249 while Present (Comp) loop
250 CC := Component_Clause (Comp);
252 -- If component clause is present, then deal with the non-default
253 -- bit order case for Ada 95 mode.
255 -- We only do this processing for the base type, and in fact that
256 -- is important, since otherwise if there are record subtypes, we
257 -- could reverse the bits once for each subtype, which is wrong.
259 if Present (CC) and then Ekind (R) = E_Record_Type then
261 CFB : constant Uint := Component_Bit_Offset (Comp);
262 CSZ : constant Uint := Esize (Comp);
263 CLC : constant Node_Id := Component_Clause (Comp);
264 Pos : constant Node_Id := Position (CLC);
265 FB : constant Node_Id := First_Bit (CLC);
267 Storage_Unit_Offset : constant Uint :=
268 CFB / System_Storage_Unit;
270 Start_Bit : constant Uint :=
271 CFB mod System_Storage_Unit;
274 -- Cases where field goes over storage unit boundary
276 if Start_Bit + CSZ > System_Storage_Unit then
278 -- Allow multi-byte field but generate warning
280 if Start_Bit mod System_Storage_Unit = 0
281 and then CSZ mod System_Storage_Unit = 0
284 ("multi-byte field specified with non-standard"
285 & " Bit_Order??", CLC);
287 if Bytes_Big_Endian then
289 ("bytes are not reversed "
290 & "(component is big-endian)??", CLC);
293 ("bytes are not reversed "
294 & "(component is little-endian)??", CLC);
297 -- Do not allow non-contiguous field
301 ("attempt to specify non-contiguous field "
302 & "not permitted", CLC);
304 ("\caused by non-standard Bit_Order "
307 ("\consider possibility of using "
308 & "Ada 2005 mode here", CLC);
311 -- Case where field fits in one storage unit
314 -- Give warning if suspicious component clause
316 if Intval (FB) >= System_Storage_Unit
317 and then Warn_On_Reverse_Bit_Order
320 ("Bit_Order clause does not affect " &
321 "byte ordering?V?", Pos);
323 Intval (Pos) + Intval (FB) /
326 ("position normalized to ^ before bit " &
327 "order interpreted?V?", Pos);
330 -- Here is where we fix up the Component_Bit_Offset value
331 -- to account for the reverse bit order. Some examples of
332 -- what needs to be done are:
334 -- First_Bit .. Last_Bit Component_Bit_Offset
346 -- The rule is that the first bit is is obtained by
347 -- subtracting the old ending bit from storage_unit - 1.
349 Set_Component_Bit_Offset
351 (Storage_Unit_Offset * System_Storage_Unit) +
352 (System_Storage_Unit - 1) -
353 (Start_Bit + CSZ - 1));
355 Set_Normalized_First_Bit
357 Component_Bit_Offset (Comp) mod
358 System_Storage_Unit);
363 Next_Component_Or_Discriminant (Comp);
366 -- For Ada 2005, we do machine scalar processing, as fully described In
367 -- AI-133. This involves gathering all components which start at the
368 -- same byte offset and processing them together. Same approach is still
369 -- valid in later versions including Ada 2012.
373 Max_Machine_Scalar_Size : constant Uint :=
375 (Standard_Long_Long_Integer_Size);
376 -- We use this as the maximum machine scalar size
379 SSU : constant Uint := UI_From_Int (System_Storage_Unit);
382 -- This first loop through components does two things. First it
383 -- deals with the case of components with component clauses whose
384 -- length is greater than the maximum machine scalar size (either
385 -- accepting them or rejecting as needed). Second, it counts the
386 -- number of components with component clauses whose length does
387 -- not exceed this maximum for later processing.
390 Comp := First_Component_Or_Discriminant (R);
391 while Present (Comp) loop
392 CC := Component_Clause (Comp);
396 Fbit : constant Uint := Static_Integer (First_Bit (CC));
397 Lbit : constant Uint := Static_Integer (Last_Bit (CC));
400 -- Case of component with last bit >= max machine scalar
402 if Lbit >= Max_Machine_Scalar_Size then
404 -- This is allowed only if first bit is zero, and
405 -- last bit + 1 is a multiple of storage unit size.
407 if Fbit = 0 and then (Lbit + 1) mod SSU = 0 then
409 -- This is the case to give a warning if enabled
411 if Warn_On_Reverse_Bit_Order then
413 ("multi-byte field specified with "
414 & " non-standard Bit_Order?V?", CC);
416 if Bytes_Big_Endian then
418 ("\bytes are not reversed "
419 & "(component is big-endian)?V?", CC);
422 ("\bytes are not reversed "
423 & "(component is little-endian)?V?", CC);
427 -- Give error message for RM 13.5.1(10) violation
431 ("machine scalar rules not followed for&",
432 First_Bit (CC), Comp);
434 Error_Msg_Uint_1 := Lbit;
435 Error_Msg_Uint_2 := Max_Machine_Scalar_Size;
437 ("\last bit (^) exceeds maximum machine "
441 if (Lbit + 1) mod SSU /= 0 then
442 Error_Msg_Uint_1 := SSU;
444 ("\and is not a multiple of Storage_Unit (^) "
449 Error_Msg_Uint_1 := Fbit;
451 ("\and first bit (^) is non-zero "
457 -- OK case of machine scalar related component clause,
458 -- For now, just count them.
461 Num_CC := Num_CC + 1;
466 Next_Component_Or_Discriminant (Comp);
469 -- We need to sort the component clauses on the basis of the
470 -- Position values in the clause, so we can group clauses with
471 -- the same Position. together to determine the relevant machine
475 Comps : array (0 .. Num_CC) of Entity_Id;
476 -- Array to collect component and discriminant entities. The
477 -- data starts at index 1, the 0'th entry is for the sort
480 function CP_Lt (Op1, Op2 : Natural) return Boolean;
481 -- Compare routine for Sort
483 procedure CP_Move (From : Natural; To : Natural);
484 -- Move routine for Sort
486 package Sorting is new GNAT.Heap_Sort_G (CP_Move, CP_Lt);
490 -- Start and stop positions in the component list of the set of
491 -- components with the same starting position (that constitute
492 -- components in a single machine scalar).
495 -- Maximum last bit value of any component in this set
498 -- Corresponding machine scalar size
504 function CP_Lt (Op1, Op2 : Natural) return Boolean is
506 return Position (Component_Clause (Comps (Op1))) <
507 Position (Component_Clause (Comps (Op2)));
514 procedure CP_Move (From : Natural; To : Natural) is
516 Comps (To) := Comps (From);
519 -- Start of processing for Sort_CC
522 -- Collect the machine scalar relevant component clauses
525 Comp := First_Component_Or_Discriminant (R);
526 while Present (Comp) loop
528 CC : constant Node_Id := Component_Clause (Comp);
531 -- Collect only component clauses whose last bit is less
532 -- than machine scalar size. Any component clause whose
533 -- last bit exceeds this value does not take part in
534 -- machine scalar layout considerations. The test for
535 -- Error_Posted makes sure we exclude component clauses
536 -- for which we already posted an error.
539 and then not Error_Posted (Last_Bit (CC))
540 and then Static_Integer (Last_Bit (CC)) <
541 Max_Machine_Scalar_Size
543 Num_CC := Num_CC + 1;
544 Comps (Num_CC) := Comp;
548 Next_Component_Or_Discriminant (Comp);
551 -- Sort by ascending position number
553 Sorting.Sort (Num_CC);
555 -- We now have all the components whose size does not exceed
556 -- the max machine scalar value, sorted by starting position.
557 -- In this loop we gather groups of clauses starting at the
558 -- same position, to process them in accordance with AI-133.
561 while Stop < Num_CC loop
566 (Last_Bit (Component_Clause (Comps (Start))));
567 while Stop < Num_CC loop
569 (Position (Component_Clause (Comps (Stop + 1)))) =
571 (Position (Component_Clause (Comps (Stop))))
579 (Component_Clause (Comps (Stop)))));
585 -- Now we have a group of component clauses from Start to
586 -- Stop whose positions are identical, and MaxL is the
587 -- maximum last bit value of any of these components.
589 -- We need to determine the corresponding machine scalar
590 -- size. This loop assumes that machine scalar sizes are
591 -- even, and that each possible machine scalar has twice
592 -- as many bits as the next smaller one.
594 MSS := Max_Machine_Scalar_Size;
596 and then (MSS / 2) >= SSU
597 and then (MSS / 2) > MaxL
602 -- Here is where we fix up the Component_Bit_Offset value
603 -- to account for the reverse bit order. Some examples of
604 -- what needs to be done for the case of a machine scalar
607 -- First_Bit .. Last_Bit Component_Bit_Offset
619 -- The rule is that the first bit is obtained by subtracting
620 -- the old ending bit from machine scalar size - 1.
622 for C in Start .. Stop loop
624 Comp : constant Entity_Id := Comps (C);
625 CC : constant Node_Id := Component_Clause (Comp);
627 LB : constant Uint := Static_Integer (Last_Bit (CC));
628 NFB : constant Uint := MSS - Uint_1 - LB;
629 NLB : constant Uint := NFB + Esize (Comp) - 1;
630 Pos : constant Uint := Static_Integer (Position (CC));
633 if Warn_On_Reverse_Bit_Order then
634 Error_Msg_Uint_1 := MSS;
636 ("info: reverse bit order in machine " &
637 "scalar of length^?V?", First_Bit (CC));
638 Error_Msg_Uint_1 := NFB;
639 Error_Msg_Uint_2 := NLB;
641 if Bytes_Big_Endian then
643 ("\info: big-endian range for "
644 & "component & is ^ .. ^?V?",
645 First_Bit (CC), Comp);
648 ("\info: little-endian range "
649 & "for component & is ^ .. ^?V?",
650 First_Bit (CC), Comp);
654 Set_Component_Bit_Offset (Comp, Pos * SSU + NFB);
655 Set_Normalized_First_Bit (Comp, NFB mod SSU);
662 end Adjust_Record_For_Reverse_Bit_Order;
664 -------------------------------------
665 -- Alignment_Check_For_Size_Change --
666 -------------------------------------
668 procedure Alignment_Check_For_Size_Change (Typ : Entity_Id; Size : Uint) is
670 -- If the alignment is known, and not set by a rep clause, and is
671 -- inconsistent with the size being set, then reset it to unknown,
672 -- we assume in this case that the size overrides the inherited
673 -- alignment, and that the alignment must be recomputed.
675 if Known_Alignment (Typ)
676 and then not Has_Alignment_Clause (Typ)
677 and then Size mod (Alignment (Typ) * SSU) /= 0
679 Init_Alignment (Typ);
681 end Alignment_Check_For_Size_Change;
683 -------------------------------------
684 -- Analyze_Aspects_At_Freeze_Point --
685 -------------------------------------
687 procedure Analyze_Aspects_At_Freeze_Point (E : Entity_Id) is
692 procedure Analyze_Aspect_Default_Value (ASN : Node_Id);
693 -- This routine analyzes an Aspect_Default_[Component_]Value denoted by
694 -- the aspect specification node ASN.
696 procedure Make_Pragma_From_Boolean_Aspect (ASN : Node_Id);
697 -- Given an aspect specification node ASN whose expression is an
698 -- optional Boolean, this routines creates the corresponding pragma
699 -- at the freezing point.
701 ----------------------------------
702 -- Analyze_Aspect_Default_Value --
703 ----------------------------------
705 procedure Analyze_Aspect_Default_Value (ASN : Node_Id) is
706 Ent : constant Entity_Id := Entity (ASN);
707 Expr : constant Node_Id := Expression (ASN);
708 Id : constant Node_Id := Identifier (ASN);
711 Error_Msg_Name_1 := Chars (Id);
713 if not Is_Type (Ent) then
714 Error_Msg_N ("aspect% can only apply to a type", Id);
717 elsif not Is_First_Subtype (Ent) then
718 Error_Msg_N ("aspect% cannot apply to subtype", Id);
721 elsif A_Id = Aspect_Default_Value
722 and then not Is_Scalar_Type (Ent)
724 Error_Msg_N ("aspect% can only be applied to scalar type", Id);
727 elsif A_Id = Aspect_Default_Component_Value then
728 if not Is_Array_Type (Ent) then
729 Error_Msg_N ("aspect% can only be applied to array type", Id);
732 elsif not Is_Scalar_Type (Component_Type (Ent)) then
733 Error_Msg_N ("aspect% requires scalar components", Id);
738 Set_Has_Default_Aspect (Base_Type (Ent));
740 if Is_Scalar_Type (Ent) then
741 Set_Default_Aspect_Value (Ent, Expr);
743 -- Place default value of base type as well, because that is
744 -- the semantics of the aspect. It is convenient to link the
745 -- aspect to both the (possibly anonymous) base type and to
746 -- the given first subtype.
748 Set_Default_Aspect_Value (Base_Type (Ent), Expr);
751 Set_Default_Aspect_Component_Value (Ent, Expr);
753 end Analyze_Aspect_Default_Value;
755 -------------------------------------
756 -- Make_Pragma_From_Boolean_Aspect --
757 -------------------------------------
759 procedure Make_Pragma_From_Boolean_Aspect (ASN : Node_Id) is
760 Ident : constant Node_Id := Identifier (ASN);
761 A_Name : constant Name_Id := Chars (Ident);
762 A_Id : constant Aspect_Id := Get_Aspect_Id (A_Name);
763 Ent : constant Entity_Id := Entity (ASN);
764 Expr : constant Node_Id := Expression (ASN);
765 Loc : constant Source_Ptr := Sloc (ASN);
769 procedure Check_False_Aspect_For_Derived_Type;
770 -- This procedure checks for the case of a false aspect for a derived
771 -- type, which improperly tries to cancel an aspect inherited from
774 -----------------------------------------
775 -- Check_False_Aspect_For_Derived_Type --
776 -----------------------------------------
778 procedure Check_False_Aspect_For_Derived_Type is
782 -- We are only checking derived types
784 if not Is_Derived_Type (E) then
788 Par := Nearest_Ancestor (E);
791 when Aspect_Atomic | Aspect_Shared =>
792 if not Is_Atomic (Par) then
796 when Aspect_Atomic_Components =>
797 if not Has_Atomic_Components (Par) then
801 when Aspect_Discard_Names =>
802 if not Discard_Names (Par) then
807 if not Is_Packed (Par) then
811 when Aspect_Unchecked_Union =>
812 if not Is_Unchecked_Union (Par) then
816 when Aspect_Volatile =>
817 if not Is_Volatile (Par) then
821 when Aspect_Volatile_Components =>
822 if not Has_Volatile_Components (Par) then
830 -- Fall through means we are canceling an inherited aspect
832 Error_Msg_Name_1 := A_Name;
833 Error_Msg_NE ("derived type& inherits aspect%, cannot cancel",
837 end Check_False_Aspect_For_Derived_Type;
839 -- Start of processing for Make_Pragma_From_Boolean_Aspect
842 if Is_False (Static_Boolean (Expr)) then
843 Check_False_Aspect_For_Derived_Type;
848 Pragma_Argument_Associations => New_List (
849 Make_Pragma_Argument_Association (Sloc (Ident),
850 Expression => New_Occurrence_Of (Ent, Sloc (Ident)))),
853 Make_Identifier (Sloc (Ident), Chars (Ident)));
855 Set_From_Aspect_Specification (Prag, True);
856 Set_Corresponding_Aspect (Prag, ASN);
857 Set_Aspect_Rep_Item (ASN, Prag);
858 Set_Is_Delayed_Aspect (Prag);
859 Set_Parent (Prag, ASN);
861 end Make_Pragma_From_Boolean_Aspect;
863 -- Start of processing for Analyze_Aspects_At_Freeze_Point
866 -- Must be visible in current scope
868 if not Scope_Within_Or_Same (Current_Scope, Scope (E)) then
872 -- Look for aspect specification entries for this entity
874 ASN := First_Rep_Item (E);
875 while Present (ASN) loop
876 if Nkind (ASN) = N_Aspect_Specification
877 and then Entity (ASN) = E
878 and then Is_Delayed_Aspect (ASN)
880 A_Id := Get_Aspect_Id (Chars (Identifier (ASN)));
884 -- For aspects whose expression is an optional Boolean, make
885 -- the corresponding pragma at the freezing point.
887 when Boolean_Aspects |
888 Library_Unit_Aspects =>
889 Make_Pragma_From_Boolean_Aspect (ASN);
891 -- Special handling for aspects that don't correspond to
892 -- pragmas/attributes.
894 when Aspect_Default_Value |
895 Aspect_Default_Component_Value =>
896 Analyze_Aspect_Default_Value (ASN);
898 -- Ditto for iterator aspects, because the corresponding
899 -- attributes may not have been analyzed yet.
901 when Aspect_Constant_Indexing |
902 Aspect_Variable_Indexing |
903 Aspect_Default_Iterator |
904 Aspect_Iterator_Element =>
905 Analyze (Expression (ASN));
911 Ritem := Aspect_Rep_Item (ASN);
913 if Present (Ritem) then
920 end Analyze_Aspects_At_Freeze_Point;
922 -----------------------------------
923 -- Analyze_Aspect_Specifications --
924 -----------------------------------
926 procedure Analyze_Aspect_Specifications (N : Node_Id; E : Entity_Id) is
931 L : constant List_Id := Aspect_Specifications (N);
933 Ins_Node : Node_Id := N;
934 -- Insert pragmas/attribute definition clause after this node when no
935 -- delayed analysis is required.
937 -- The general processing involves building an attribute definition
938 -- clause or a pragma node that corresponds to the aspect. Then in order
939 -- to delay the evaluation of this aspect to the freeze point, we attach
940 -- the corresponding pragma/attribute definition clause to the aspect
941 -- specification node, which is then placed in the Rep Item chain. In
942 -- this case we mark the entity by setting the flag Has_Delayed_Aspects
943 -- and we evaluate the rep item at the freeze point. When the aspect
944 -- doesn't have a corresponding pragma/attribute definition clause, then
945 -- its analysis is simply delayed at the freeze point.
947 -- Some special cases don't require delay analysis, thus the aspect is
948 -- analyzed right now.
950 -- Note that there is a special handling for
951 -- Pre/Post/Test_Case/Contract_Case aspects. In this case, we do not
952 -- have to worry about delay issues, since the pragmas themselves deal
953 -- with delay of visibility for the expression analysis. Thus, we just
954 -- insert the pragma after the node N.
957 pragma Assert (Present (L));
959 -- Loop through aspects
962 Aspect_Loop : while Present (Aspect) loop
964 Expr : constant Node_Id := Expression (Aspect);
965 Id : constant Node_Id := Identifier (Aspect);
966 Loc : constant Source_Ptr := Sloc (Aspect);
967 Nam : constant Name_Id := Chars (Id);
968 A_Id : constant Aspect_Id := Get_Aspect_Id (Nam);
971 Delay_Required : Boolean := True;
972 -- Set False if delay is not required
974 Eloc : Source_Ptr := No_Location;
975 -- Source location of expression, modified when we split PPC's. It
976 -- is set below when Expr is present.
978 procedure Analyze_Aspect_External_Or_Link_Name;
979 -- This routine performs the analysis of the External_Name or
980 -- Link_Name aspects.
982 procedure Analyze_Aspect_Implicit_Dereference;
983 -- This routine performs the analysis of the Implicit_Dereference
986 ------------------------------------------
987 -- Analyze_Aspect_External_Or_Link_Name --
988 ------------------------------------------
990 procedure Analyze_Aspect_External_Or_Link_Name is
992 -- Verify that there is an Import/Export aspect defined for the
993 -- entity. The processing of that aspect in turn checks that
994 -- there is a Convention aspect declared. The pragma is
995 -- constructed when processing the Convention aspect.
1002 while Present (A) loop
1003 exit when Chars (Identifier (A)) = Name_Export
1004 or else Chars (Identifier (A)) = Name_Import;
1010 ("Missing Import/Export for Link/External name",
1014 end Analyze_Aspect_External_Or_Link_Name;
1016 -----------------------------------------
1017 -- Analyze_Aspect_Implicit_Dereference --
1018 -----------------------------------------
1020 procedure Analyze_Aspect_Implicit_Dereference is
1022 if not Is_Type (E) or else not Has_Discriminants (E) then
1024 ("Aspect must apply to a type with discriminants", N);
1031 Disc := First_Discriminant (E);
1032 while Present (Disc) loop
1033 if Chars (Expr) = Chars (Disc)
1034 and then Ekind (Etype (Disc)) =
1035 E_Anonymous_Access_Type
1037 Set_Has_Implicit_Dereference (E);
1038 Set_Has_Implicit_Dereference (Disc);
1042 Next_Discriminant (Disc);
1045 -- Error if no proper access discriminant.
1048 ("not an access discriminant of&", Expr, E);
1051 end Analyze_Aspect_Implicit_Dereference;
1054 -- Skip aspect if already analyzed (not clear if this is needed)
1056 if Analyzed (Aspect) then
1060 -- Set the source location of expression, used in the case of
1061 -- a failed precondition/postcondition or invariant. Note that
1062 -- the source location of the expression is not usually the best
1063 -- choice here. For example, it gets located on the last AND
1064 -- keyword in a chain of boolean expressiond AND'ed together.
1065 -- It is best to put the message on the first character of the
1066 -- assertion, which is the effect of the First_Node call here.
1068 if Present (Expr) then
1069 Eloc := Sloc (First_Node (Expr));
1072 -- Check restriction No_Implementation_Aspect_Specifications
1074 if Impl_Defined_Aspects (A_Id) then
1076 (No_Implementation_Aspect_Specifications, Aspect);
1079 -- Check restriction No_Specification_Of_Aspect
1081 Check_Restriction_No_Specification_Of_Aspect (Aspect);
1083 -- Analyze this aspect
1085 Set_Analyzed (Aspect);
1086 Set_Entity (Aspect, E);
1087 Ent := New_Occurrence_Of (E, Sloc (Id));
1089 -- Check for duplicate aspect. Note that the Comes_From_Source
1090 -- test allows duplicate Pre/Post's that we generate internally
1091 -- to escape being flagged here.
1093 if No_Duplicates_Allowed (A_Id) then
1095 while Anod /= Aspect loop
1097 (A_Id, Get_Aspect_Id (Chars (Identifier (Anod))))
1098 and then Comes_From_Source (Aspect)
1100 Error_Msg_Name_1 := Nam;
1101 Error_Msg_Sloc := Sloc (Anod);
1103 -- Case of same aspect specified twice
1105 if Class_Present (Anod) = Class_Present (Aspect) then
1106 if not Class_Present (Anod) then
1108 ("aspect% for & previously given#",
1112 ("aspect `%''Class` for & previously given#",
1122 -- Check some general restrictions on language defined aspects
1124 if not Impl_Defined_Aspects (A_Id) then
1125 Error_Msg_Name_1 := Nam;
1127 -- Not allowed for renaming declarations
1129 if Nkind (N) in N_Renaming_Declaration then
1131 ("aspect % not allowed for renaming declaration",
1135 -- Not allowed for formal type declarations
1137 if Nkind (N) = N_Formal_Type_Declaration then
1139 ("aspect % not allowed for formal type declaration",
1144 -- Copy expression for later processing by the procedures
1145 -- Check_Aspect_At_[Freeze_Point | End_Of_Declarations]
1147 Set_Entity (Id, New_Copy_Tree (Expr));
1149 -- Processing based on specific aspect
1153 -- No_Aspect should be impossible
1156 raise Program_Error;
1158 -- Case 1: Aspects corresponding to attribute definition
1161 when Aspect_Address |
1164 Aspect_Component_Size |
1165 Aspect_Constant_Indexing |
1166 Aspect_Default_Iterator |
1167 Aspect_Dispatching_Domain |
1168 Aspect_External_Tag |
1170 Aspect_Iterator_Element |
1171 Aspect_Machine_Radix |
1172 Aspect_Object_Size |
1175 Aspect_Scalar_Storage_Order |
1178 Aspect_Simple_Storage_Pool |
1179 Aspect_Storage_Pool |
1180 Aspect_Storage_Size |
1181 Aspect_Stream_Size |
1183 Aspect_Variable_Indexing |
1186 -- Indexing aspects apply only to tagged type
1188 if (A_Id = Aspect_Constant_Indexing
1189 or else A_Id = Aspect_Variable_Indexing)
1190 and then not (Is_Type (E)
1191 and then Is_Tagged_Type (E))
1193 Error_Msg_N ("indexing applies to a tagged type", N);
1197 -- Construct the attribute definition clause
1200 Make_Attribute_Definition_Clause (Loc,
1202 Chars => Chars (Id),
1203 Expression => Relocate_Node (Expr));
1205 -- Case 2: Aspects cooresponding to pragmas
1207 -- Case 2a: Aspects corresponding to pragmas with two
1208 -- arguments, where the first argument is a local name
1209 -- referring to the entity, and the second argument is the
1210 -- aspect definition expression.
1212 when Aspect_Suppress |
1213 Aspect_Unsuppress =>
1215 -- Construct the pragma
1219 Pragma_Argument_Associations => New_List (
1220 Make_Pragma_Argument_Association (Loc,
1221 Expression => New_Occurrence_Of (E, Loc)),
1223 Make_Pragma_Argument_Association (Sloc (Expr),
1224 Expression => Relocate_Node (Expr))),
1226 Pragma_Identifier =>
1227 Make_Identifier (Sloc (Id), Chars (Id)));
1229 when Aspect_Synchronization =>
1231 -- The aspect corresponds to pragma Implemented.
1232 -- Construct the pragma.
1236 Pragma_Argument_Associations => New_List (
1237 Make_Pragma_Argument_Association (Loc,
1238 Expression => New_Occurrence_Of (E, Loc)),
1240 Make_Pragma_Argument_Association (Sloc (Expr),
1241 Expression => Relocate_Node (Expr))),
1243 Pragma_Identifier =>
1244 Make_Identifier (Sloc (Id), Name_Implemented));
1246 -- No delay is required since the only values are: By_Entry
1247 -- | By_Protected_Procedure | By_Any | Optional which don't
1248 -- get analyzed anyway.
1250 Delay_Required := False;
1252 when Aspect_Attach_Handler =>
1255 Pragma_Identifier =>
1256 Make_Identifier (Sloc (Id), Name_Attach_Handler),
1257 Pragma_Argument_Associations => New_List (
1258 Make_Pragma_Argument_Association (Sloc (Ent),
1260 Make_Pragma_Argument_Association (Sloc (Expr),
1261 Expression => Relocate_Node (Expr))));
1263 when Aspect_Dynamic_Predicate |
1265 Aspect_Static_Predicate =>
1267 -- Construct the pragma (always a pragma Predicate, with
1268 -- flags recording whether it is static/dynamic).
1272 Pragma_Argument_Associations => New_List (
1273 Make_Pragma_Argument_Association (Sloc (Ent),
1275 Make_Pragma_Argument_Association (Sloc (Expr),
1276 Expression => Relocate_Node (Expr))),
1277 Class_Present => Class_Present (Aspect),
1278 Pragma_Identifier =>
1279 Make_Identifier (Sloc (Id), Name_Predicate));
1281 -- If the type is private, indicate that its completion
1282 -- has a freeze node, because that is the one that will be
1283 -- visible at freeze time.
1285 Set_Has_Predicates (E);
1287 if Is_Private_Type (E)
1288 and then Present (Full_View (E))
1290 Set_Has_Predicates (Full_View (E));
1291 Set_Has_Delayed_Aspects (Full_View (E));
1292 Ensure_Freeze_Node (Full_View (E));
1295 -- Case 2b: Aspects corresponding to pragmas with two
1296 -- arguments, where the second argument is a local name
1297 -- referring to the entity, and the first argument is the
1298 -- aspect definition expression.
1300 when Aspect_Convention =>
1302 -- The aspect may be part of the specification of an import
1303 -- or export pragma. Scan the aspect list to gather the
1304 -- other components, if any. The name of the generated
1305 -- pragma is one of Convention/Import/Export.
1317 P_Name := Chars (Id);
1319 Arg_List := New_List;
1324 while Present (A) loop
1325 A_Name := Chars (Identifier (A));
1327 if A_Name = Name_Import or else
1328 A_Name = Name_Export
1331 Error_Msg_N ("conflicting", A);
1338 elsif A_Name = Name_Link_Name then
1340 Make_Pragma_Argument_Association (Loc,
1342 Expression => Relocate_Node (Expression (A)));
1344 elsif A_Name = Name_External_Name then
1346 Make_Pragma_Argument_Association (Loc,
1348 Expression => Relocate_Node (Expression (A)));
1354 Arg_List := New_List (
1355 Make_Pragma_Argument_Association (Sloc (Expr),
1356 Expression => Relocate_Node (Expr)),
1357 Make_Pragma_Argument_Association (Sloc (Ent),
1358 Expression => Ent));
1360 if Present (L_Assoc) then
1361 Append_To (Arg_List, L_Assoc);
1364 if Present (E_Assoc) then
1365 Append_To (Arg_List, E_Assoc);
1370 Pragma_Argument_Associations => Arg_List,
1371 Pragma_Identifier =>
1372 Make_Identifier (Loc, P_Name));
1375 -- The following three aspects can be specified for a
1376 -- subprogram body, in which case we generate pragmas for them
1377 -- and insert them ahead of local declarations, rather than
1381 Aspect_Interrupt_Priority |
1383 if Nkind (N) = N_Subprogram_Body then
1386 Pragma_Argument_Associations => New_List (
1387 Make_Pragma_Argument_Association (Sloc (Expr),
1388 Expression => Relocate_Node (Expr))),
1389 Pragma_Identifier =>
1390 Make_Identifier (Sloc (Id), Chars (Id)));
1393 Make_Attribute_Definition_Clause (Loc,
1395 Chars => Chars (Id),
1396 Expression => Relocate_Node (Expr));
1399 when Aspect_Warnings =>
1401 -- Construct the pragma
1405 Pragma_Argument_Associations => New_List (
1406 Make_Pragma_Argument_Association (Sloc (Expr),
1407 Expression => Relocate_Node (Expr)),
1408 Make_Pragma_Argument_Association (Loc,
1409 Expression => New_Occurrence_Of (E, Loc))),
1410 Pragma_Identifier =>
1411 Make_Identifier (Sloc (Id), Chars (Id)),
1412 Class_Present => Class_Present (Aspect));
1414 -- We don't have to play the delay game here, since the only
1415 -- values are ON/OFF which don't get analyzed anyway.
1417 Delay_Required := False;
1419 -- Case 2c: Aspects corresponding to pragmas with three
1422 -- Invariant aspects have a first argument that references the
1423 -- entity, a second argument that is the expression and a third
1424 -- argument that is an appropriate message.
1426 when Aspect_Invariant |
1427 Aspect_Type_Invariant =>
1429 -- Analysis of the pragma will verify placement legality:
1430 -- an invariant must apply to a private type, or appear in
1431 -- the private part of a spec and apply to a completion.
1433 -- Construct the pragma
1437 Pragma_Argument_Associations => New_List (
1438 Make_Pragma_Argument_Association (Sloc (Ent),
1440 Make_Pragma_Argument_Association (Sloc (Expr),
1441 Expression => Relocate_Node (Expr))),
1442 Class_Present => Class_Present (Aspect),
1443 Pragma_Identifier =>
1444 Make_Identifier (Sloc (Id), Name_Invariant));
1446 -- Add message unless exception messages are suppressed
1448 if not Opt.Exception_Locations_Suppressed then
1449 Append_To (Pragma_Argument_Associations (Aitem),
1450 Make_Pragma_Argument_Association (Eloc,
1451 Chars => Name_Message,
1453 Make_String_Literal (Eloc,
1454 Strval => "failed invariant from "
1455 & Build_Location_String (Eloc))));
1458 -- For Invariant case, insert immediately after the entity
1459 -- declaration. We do not have to worry about delay issues
1460 -- since the pragma processing takes care of this.
1462 Delay_Required := False;
1464 -- Case 2d : Aspects that correspond to a pragma with one
1467 when Aspect_Abstract_State =>
1470 Pragma_Identifier =>
1471 Make_Identifier (Sloc (Id), Name_Abstract_State),
1472 Pragma_Argument_Associations => New_List (
1473 Make_Pragma_Argument_Association (Loc,
1474 Expression => Relocate_Node (Expr))));
1476 Delay_Required := False;
1478 -- Aspect Depends must be delayed because it mentions names
1479 -- of inputs and output that are classified by aspect Global.
1481 when Aspect_Depends =>
1484 Pragma_Identifier =>
1485 Make_Identifier (Sloc (Id), Name_Depends),
1486 Pragma_Argument_Associations => New_List (
1487 Make_Pragma_Argument_Association (Loc,
1488 Expression => Relocate_Node (Expr))));
1490 -- Aspect Global must be delayed because it can mention names
1491 -- and benefit from the forward visibility rules applicable to
1492 -- aspects of subprograms.
1494 when Aspect_Global =>
1497 Pragma_Identifier =>
1498 Make_Identifier (Sloc (Id), Name_Global),
1499 Pragma_Argument_Associations => New_List (
1500 Make_Pragma_Argument_Association (Loc,
1501 Expression => Relocate_Node (Expr))));
1503 when Aspect_Relative_Deadline =>
1506 Pragma_Argument_Associations => New_List (
1507 Make_Pragma_Argument_Association (Loc,
1508 Expression => Relocate_Node (Expr))),
1509 Pragma_Identifier =>
1510 Make_Identifier (Sloc (Id), Name_Relative_Deadline));
1512 -- If the aspect applies to a task, the corresponding pragma
1513 -- must appear within its declarations, not after.
1515 if Nkind (N) = N_Task_Type_Declaration then
1521 if No (Task_Definition (N)) then
1522 Set_Task_Definition (N,
1523 Make_Task_Definition (Loc,
1524 Visible_Declarations => New_List,
1525 End_Label => Empty));
1528 Def := Task_Definition (N);
1529 V := Visible_Declarations (Def);
1530 if not Is_Empty_List (V) then
1531 Insert_Before (First (V), Aitem);
1534 Set_Visible_Declarations (Def, New_List (Aitem));
1541 -- Case 3 : Aspects that don't correspond to pragma/attribute
1542 -- definition clause.
1544 -- Case 3a: The aspects listed below don't correspond to
1545 -- pragmas/attributes but do require delayed analysis.
1547 when Aspect_Default_Value |
1548 Aspect_Default_Component_Value =>
1551 -- Case 3b: The aspects listed below don't correspond to
1552 -- pragmas/attributes and don't need delayed analysis.
1554 -- For Implicit_Dereference, External_Name and Link_Name, only
1555 -- the legality checks are done during the analysis, thus no
1556 -- delay is required.
1558 when Aspect_Implicit_Dereference =>
1559 Analyze_Aspect_Implicit_Dereference;
1562 when Aspect_External_Name |
1564 Analyze_Aspect_External_Or_Link_Name;
1567 when Aspect_Dimension =>
1568 Analyze_Aspect_Dimension (N, Id, Expr);
1571 when Aspect_Dimension_System =>
1572 Analyze_Aspect_Dimension_System (N, Id, Expr);
1575 -- Case 4: Special handling for aspects
1576 -- Pre/Post/Test_Case/Contract_Case whose corresponding pragmas
1577 -- take care of the delay.
1579 -- Aspects Pre/Post generate Precondition/Postcondition pragmas
1580 -- with a first argument that is the expression, and a second
1581 -- argument that is an informative message if the test fails.
1582 -- This is inserted right after the declaration, to get the
1583 -- required pragma placement. The processing for the pragmas
1584 -- takes care of the required delay.
1586 when Pre_Post_Aspects => declare
1590 if A_Id = Aspect_Pre or else A_Id = Aspect_Precondition then
1591 Pname := Name_Precondition;
1593 Pname := Name_Postcondition;
1596 -- If the expressions is of the form A and then B, then
1597 -- we generate separate Pre/Post aspects for the separate
1598 -- clauses. Since we allow multiple pragmas, there is no
1599 -- problem in allowing multiple Pre/Post aspects internally.
1600 -- These should be treated in reverse order (B first and
1601 -- A second) since they are later inserted just after N in
1602 -- the order they are treated. This way, the pragma for A
1603 -- ends up preceding the pragma for B, which may have an
1604 -- importance for the error raised (either constraint error
1605 -- or precondition error).
1607 -- We do not do this for Pre'Class, since we have to put
1608 -- these conditions together in a complex OR expression
1610 -- We do not do this in ASIS mode, as ASIS relies on the
1611 -- original node representing the complete expression, when
1612 -- retrieving it through the source aspect table.
1615 and then (Pname = Name_Postcondition
1616 or else not Class_Present (Aspect))
1618 while Nkind (Expr) = N_And_Then loop
1619 Insert_After (Aspect,
1620 Make_Aspect_Specification (Sloc (Left_Opnd (Expr)),
1621 Identifier => Identifier (Aspect),
1622 Expression => Relocate_Node (Left_Opnd (Expr)),
1623 Class_Present => Class_Present (Aspect),
1624 Split_PPC => True));
1625 Rewrite (Expr, Relocate_Node (Right_Opnd (Expr)));
1626 Eloc := Sloc (Expr);
1630 -- Build the precondition/postcondition pragma
1634 Pragma_Identifier =>
1635 Make_Identifier (Sloc (Id), Pname),
1636 Class_Present => Class_Present (Aspect),
1637 Split_PPC => Split_PPC (Aspect),
1638 Pragma_Argument_Associations => New_List (
1639 Make_Pragma_Argument_Association (Eloc,
1640 Chars => Name_Check,
1641 Expression => Relocate_Node (Expr))));
1643 -- Add message unless exception messages are suppressed
1645 if not Opt.Exception_Locations_Suppressed then
1646 Append_To (Pragma_Argument_Associations (Aitem),
1647 Make_Pragma_Argument_Association (Eloc,
1648 Chars => Name_Message,
1650 Make_String_Literal (Eloc,
1652 & Get_Name_String (Pname)
1654 & Build_Location_String (Eloc))));
1657 Set_From_Aspect_Specification (Aitem, True);
1658 Set_Corresponding_Aspect (Aitem, Aspect);
1659 Set_Is_Delayed_Aspect (Aspect);
1661 -- For Pre/Post cases, insert immediately after the entity
1662 -- declaration, since that is the required pragma placement.
1663 -- Note that for these aspects, we do not have to worry
1664 -- about delay issues, since the pragmas themselves deal
1665 -- with delay of visibility for the expression analysis.
1667 -- If the entity is a library-level subprogram, the pre/
1668 -- postconditions must be treated as late pragmas. Note
1669 -- that they must be prepended, not appended, to the list,
1670 -- so that split AND THEN sections are processed in the
1673 if Nkind (Parent (N)) = N_Compilation_Unit then
1675 Aux : constant Node_Id := Aux_Decls_Node (Parent (N));
1678 if No (Pragmas_After (Aux)) then
1679 Set_Pragmas_After (Aux, New_List);
1682 Prepend (Aitem, Pragmas_After (Aux));
1685 -- If it is a subprogram body, add pragmas to list of
1686 -- declarations in body.
1688 elsif Nkind (N) = N_Subprogram_Body then
1689 if No (Declarations (N)) then
1690 Set_Declarations (N, New_List);
1693 Append (Aitem, Declarations (N));
1696 Insert_After (N, Aitem);
1698 -- Pre/Postconditions on stubs are analyzed at once,
1699 -- because the proper body is analyzed next, and the
1700 -- contract must be captured before the body.
1702 if Nkind (N) = N_Subprogram_Body_Stub then
1710 when Aspect_Contract_Case |
1715 Comp_Expr : Node_Id;
1716 Comp_Assn : Node_Id;
1722 if Nkind (Parent (N)) = N_Compilation_Unit then
1723 Error_Msg_Name_1 := Nam;
1724 Error_Msg_N ("incorrect placement of aspect `%`", E);
1728 if Nkind (Expr) /= N_Aggregate then
1729 Error_Msg_Name_1 := Nam;
1731 ("wrong syntax for aspect `%` for &", Id, E);
1735 -- Make pragma expressions refer to the original aspect
1736 -- expressions through the Original_Node link. This is
1737 -- used in semantic analysis for ASIS mode, so that the
1738 -- original expression also gets analyzed.
1740 Comp_Expr := First (Expressions (Expr));
1741 while Present (Comp_Expr) loop
1742 New_Expr := Relocate_Node (Comp_Expr);
1743 Set_Original_Node (New_Expr, Comp_Expr);
1745 Make_Pragma_Argument_Association (Sloc (Comp_Expr),
1746 Expression => New_Expr));
1750 Comp_Assn := First (Component_Associations (Expr));
1751 while Present (Comp_Assn) loop
1752 if List_Length (Choices (Comp_Assn)) /= 1
1754 Nkind (First (Choices (Comp_Assn))) /= N_Identifier
1756 Error_Msg_Name_1 := Nam;
1758 ("wrong syntax for aspect `%` for &", Id, E);
1762 New_Expr := Relocate_Node (Expression (Comp_Assn));
1763 Set_Original_Node (New_Expr, Expression (Comp_Assn));
1765 Make_Pragma_Argument_Association (Sloc (Comp_Assn),
1766 Chars => Chars (First (Choices (Comp_Assn))),
1767 Expression => New_Expr));
1771 -- Build the contract-case or test-case pragma
1775 Pragma_Identifier =>
1776 Make_Identifier (Sloc (Id), Nam),
1777 Pragma_Argument_Associations => Args);
1779 Delay_Required := False;
1782 when Aspect_Contract_Cases => Contract_Cases : declare
1783 Case_Guard : Node_Id;
1785 Others_Seen : Boolean := False;
1786 Post_Case : Node_Id;
1789 if Nkind (Parent (N)) = N_Compilation_Unit then
1790 Error_Msg_Name_1 := Nam;
1791 Error_Msg_N ("incorrect placement of aspect `%`", E);
1795 if Nkind (Expr) /= N_Aggregate then
1796 Error_Msg_Name_1 := Nam;
1798 ("wrong syntax for aspect `%` for &", Id, E);
1802 -- Verify the legality of individual post cases
1804 Post_Case := First (Component_Associations (Expr));
1805 while Present (Post_Case) loop
1806 if Nkind (Post_Case) /= N_Component_Association then
1807 Error_Msg_N ("wrong syntax in post case", Post_Case);
1811 -- Each post case must have exactly one case guard
1813 Case_Guard := First (Choices (Post_Case));
1814 Extra := Next (Case_Guard);
1816 if Present (Extra) then
1818 ("post case may have only one case guard", Extra);
1822 -- Check the placement of "others" (if available)
1824 if Nkind (Case_Guard) = N_Others_Choice then
1826 Error_Msg_Name_1 := Nam;
1828 ("only one others choice allowed in aspect %",
1832 Others_Seen := True;
1835 elsif Others_Seen then
1836 Error_Msg_Name_1 := Nam;
1838 ("others must be the last choice in aspect %", N);
1845 -- Transform the aspect into a pragma
1849 Pragma_Identifier =>
1850 Make_Identifier (Loc, Nam),
1851 Pragma_Argument_Associations => New_List (
1852 Make_Pragma_Argument_Association (Loc,
1853 Expression => Relocate_Node (Expr))));
1855 Delay_Required := False;
1858 -- Case 5: Special handling for aspects with an optional
1859 -- boolean argument.
1861 -- In the general case, the corresponding pragma cannot be
1862 -- generated yet because the evaluation of the boolean needs to
1863 -- be delayed til the freeze point.
1865 when Boolean_Aspects |
1866 Library_Unit_Aspects =>
1868 Set_Is_Boolean_Aspect (Aspect);
1870 -- Lock_Free aspect only apply to protected objects
1872 if A_Id = Aspect_Lock_Free then
1873 if Ekind (E) /= E_Protected_Type then
1874 Error_Msg_Name_1 := Nam;
1876 ("aspect % only applies to a protected object",
1880 -- Set the Uses_Lock_Free flag to True if there is no
1881 -- expression or if the expression is True. ??? The
1882 -- evaluation of this aspect should be delayed to the
1886 or else Is_True (Static_Boolean (Expr))
1888 Set_Uses_Lock_Free (E);
1891 Record_Rep_Item (E, Aspect);
1896 elsif A_Id = Aspect_Import or else A_Id = Aspect_Export then
1898 -- Verify that there is an aspect Convention that will
1899 -- incorporate the Import/Export aspect, and eventual
1900 -- Link/External names.
1907 while Present (A) loop
1908 exit when Chars (Identifier (A)) = Name_Convention;
1914 ("missing Convention aspect for Export/Import",
1922 -- This requires special handling in the case of a package
1923 -- declaration, the pragma needs to be inserted in the list
1924 -- of declarations for the associated package. There is no
1925 -- issue of visibility delay for these aspects.
1927 if A_Id in Library_Unit_Aspects
1928 and then Nkind (N) = N_Package_Declaration
1929 and then Nkind (Parent (N)) /= N_Compilation_Unit
1932 ("incorrect context for library unit aspect&", Id);
1936 -- Special handling when the aspect has no expression. In
1937 -- this case the value is considered to be True. Thus, we
1938 -- simply insert the pragma, no delay is required.
1943 Pragma_Argument_Associations => New_List (
1944 Make_Pragma_Argument_Association (Sloc (Ent),
1945 Expression => Ent)),
1946 Pragma_Identifier =>
1947 Make_Identifier (Sloc (Id), Chars (Id)));
1949 Delay_Required := False;
1951 -- In general cases, the corresponding pragma/attribute
1952 -- definition clause will be inserted later at the freezing
1960 -- Attach the corresponding pragma/attribute definition clause to
1961 -- the aspect specification node.
1963 if Present (Aitem) then
1964 Set_From_Aspect_Specification (Aitem, True);
1966 if Nkind (Aitem) = N_Pragma then
1967 Set_Corresponding_Aspect (Aitem, Aspect);
1971 -- Aspect Abstract_State introduces implicit declarations for all
1972 -- state abstraction entities it defines. To emulate this behavior
1973 -- insert the pragma at the start of the visible declarations of
1974 -- the related package.
1976 if Nam = Name_Abstract_State
1977 and then Nkind (N) = N_Package_Declaration
1979 if No (Visible_Declarations (Specification (N))) then
1980 Set_Visible_Declarations (Specification (N), New_List);
1983 Prepend (Aitem, Visible_Declarations (Specification (N)));
1986 -- In the context of a compilation unit, we directly put the
1987 -- pragma in the Pragmas_After list of the
1988 -- N_Compilation_Unit_Aux node (no delay is required here)
1989 -- except for aspects on a subprogram body (see below).
1991 elsif Nkind (Parent (N)) = N_Compilation_Unit
1992 and then (Present (Aitem) or else Is_Boolean_Aspect (Aspect))
1995 Aux : constant Node_Id := Aux_Decls_Node (Parent (N));
1998 pragma Assert (Nkind (Aux) = N_Compilation_Unit_Aux);
2000 -- For a Boolean aspect, create the corresponding pragma if
2001 -- no expression or if the value is True.
2003 if Is_Boolean_Aspect (Aspect) and then No (Aitem) then
2004 if Is_True (Static_Boolean (Expr)) then
2007 Pragma_Argument_Associations => New_List (
2008 Make_Pragma_Argument_Association (Sloc (Ent),
2009 Expression => Ent)),
2010 Pragma_Identifier =>
2011 Make_Identifier (Sloc (Id), Chars (Id)));
2013 Set_From_Aspect_Specification (Aitem, True);
2014 Set_Corresponding_Aspect (Aitem, Aspect);
2021 -- If the aspect is on a subprogram body (relevant aspects
2022 -- are Inline and Priority), add the pragma in front of
2023 -- the declarations.
2025 if Nkind (N) = N_Subprogram_Body then
2026 if No (Declarations (N)) then
2027 Set_Declarations (N, New_List);
2030 Prepend (Aitem, Declarations (N));
2033 if No (Pragmas_After (Aux)) then
2034 Set_Pragmas_After (Aux, New_List);
2037 Append (Aitem, Pragmas_After (Aux));
2044 -- The evaluation of the aspect is delayed to the freezing point.
2045 -- The pragma or attribute clause if there is one is then attached
2046 -- to the aspect specification which is placed in the rep item
2049 if Delay_Required then
2050 if Present (Aitem) then
2051 Set_Is_Delayed_Aspect (Aitem);
2052 Set_Aspect_Rep_Item (Aspect, Aitem);
2053 Set_Parent (Aitem, Aspect);
2056 Set_Is_Delayed_Aspect (Aspect);
2058 -- In the case of Default_Value, link the aspect to base type
2059 -- as well, even though it appears on a first subtype. This is
2060 -- mandated by the semantics of the aspect. Do not establish
2061 -- the link when processing the base type itself as this leads
2062 -- to a rep item circularity. Verify that we are dealing with
2063 -- a scalar type to prevent cascaded errors.
2065 if A_Id = Aspect_Default_Value
2066 and then Is_Scalar_Type (E)
2067 and then Base_Type (E) /= E
2069 Set_Has_Delayed_Aspects (Base_Type (E));
2070 Record_Rep_Item (Base_Type (E), Aspect);
2073 Set_Has_Delayed_Aspects (E);
2074 Record_Rep_Item (E, Aspect);
2076 -- When delay is not required and the context is not a compilation
2077 -- unit, we simply insert the pragma/attribute definition clause
2081 Insert_After (Ins_Node, Aitem);
2088 end loop Aspect_Loop;
2090 if Has_Delayed_Aspects (E) then
2091 Ensure_Freeze_Node (E);
2093 end Analyze_Aspect_Specifications;
2095 -----------------------
2096 -- Analyze_At_Clause --
2097 -----------------------
2099 -- An at clause is replaced by the corresponding Address attribute
2100 -- definition clause that is the preferred approach in Ada 95.
2102 procedure Analyze_At_Clause (N : Node_Id) is
2103 CS : constant Boolean := Comes_From_Source (N);
2106 -- This is an obsolescent feature
2108 Check_Restriction (No_Obsolescent_Features, N);
2110 if Warn_On_Obsolescent_Feature then
2112 ("?j?at clause is an obsolescent feature (RM J.7(2))", N);
2114 ("\?j?use address attribute definition clause instead", N);
2117 -- Rewrite as address clause
2120 Make_Attribute_Definition_Clause (Sloc (N),
2121 Name => Identifier (N),
2122 Chars => Name_Address,
2123 Expression => Expression (N)));
2125 -- We preserve Comes_From_Source, since logically the clause still comes
2126 -- from the source program even though it is changed in form.
2128 Set_Comes_From_Source (N, CS);
2130 -- Analyze rewritten clause
2132 Analyze_Attribute_Definition_Clause (N);
2133 end Analyze_At_Clause;
2135 -----------------------------------------
2136 -- Analyze_Attribute_Definition_Clause --
2137 -----------------------------------------
2139 procedure Analyze_Attribute_Definition_Clause (N : Node_Id) is
2140 Loc : constant Source_Ptr := Sloc (N);
2141 Nam : constant Node_Id := Name (N);
2142 Attr : constant Name_Id := Chars (N);
2143 Expr : constant Node_Id := Expression (N);
2144 Id : constant Attribute_Id := Get_Attribute_Id (Attr);
2147 -- The entity of Nam after it is analyzed. In the case of an incomplete
2148 -- type, this is the underlying type.
2151 -- The underlying entity to which the attribute applies. Generally this
2152 -- is the Underlying_Type of Ent, except in the case where the clause
2153 -- applies to full view of incomplete type or private type in which case
2154 -- U_Ent is just a copy of Ent.
2156 FOnly : Boolean := False;
2157 -- Reset to True for subtype specific attribute (Alignment, Size)
2158 -- and for stream attributes, i.e. those cases where in the call
2159 -- to Rep_Item_Too_Late, FOnly is set True so that only the freezing
2160 -- rules are checked. Note that the case of stream attributes is not
2161 -- clear from the RM, but see AI95-00137. Also, the RM seems to
2162 -- disallow Storage_Size for derived task types, but that is also
2163 -- clearly unintentional.
2165 procedure Analyze_Stream_TSS_Definition (TSS_Nam : TSS_Name_Type);
2166 -- Common processing for 'Read, 'Write, 'Input and 'Output attribute
2167 -- definition clauses.
2169 function Duplicate_Clause return Boolean;
2170 -- This routine checks if the aspect for U_Ent being given by attribute
2171 -- definition clause N is for an aspect that has already been specified,
2172 -- and if so gives an error message. If there is a duplicate, True is
2173 -- returned, otherwise if there is no error, False is returned.
2175 procedure Check_Indexing_Functions;
2176 -- Check that the function in Constant_Indexing or Variable_Indexing
2177 -- attribute has the proper type structure. If the name is overloaded,
2178 -- check that some interpretation is legal.
2180 procedure Check_Iterator_Functions;
2181 -- Check that there is a single function in Default_Iterator attribute
2182 -- has the proper type structure.
2184 function Check_Primitive_Function (Subp : Entity_Id) return Boolean;
2185 -- Common legality check for the previous two
2187 -----------------------------------
2188 -- Analyze_Stream_TSS_Definition --
2189 -----------------------------------
2191 procedure Analyze_Stream_TSS_Definition (TSS_Nam : TSS_Name_Type) is
2192 Subp : Entity_Id := Empty;
2197 Is_Read : constant Boolean := (TSS_Nam = TSS_Stream_Read);
2198 -- True for Read attribute, false for other attributes
2200 function Has_Good_Profile (Subp : Entity_Id) return Boolean;
2201 -- Return true if the entity is a subprogram with an appropriate
2202 -- profile for the attribute being defined.
2204 ----------------------
2205 -- Has_Good_Profile --
2206 ----------------------
2208 function Has_Good_Profile (Subp : Entity_Id) return Boolean is
2210 Is_Function : constant Boolean := (TSS_Nam = TSS_Stream_Input);
2211 Expected_Ekind : constant array (Boolean) of Entity_Kind :=
2212 (False => E_Procedure, True => E_Function);
2216 if Ekind (Subp) /= Expected_Ekind (Is_Function) then
2220 F := First_Formal (Subp);
2223 or else Ekind (Etype (F)) /= E_Anonymous_Access_Type
2224 or else Designated_Type (Etype (F)) /=
2225 Class_Wide_Type (RTE (RE_Root_Stream_Type))
2230 if not Is_Function then
2234 Expected_Mode : constant array (Boolean) of Entity_Kind :=
2235 (False => E_In_Parameter,
2236 True => E_Out_Parameter);
2238 if Parameter_Mode (F) /= Expected_Mode (Is_Read) then
2246 Typ := Etype (Subp);
2249 return Base_Type (Typ) = Base_Type (Ent)
2250 and then No (Next_Formal (F));
2251 end Has_Good_Profile;
2253 -- Start of processing for Analyze_Stream_TSS_Definition
2258 if not Is_Type (U_Ent) then
2259 Error_Msg_N ("local name must be a subtype", Nam);
2263 Pnam := TSS (Base_Type (U_Ent), TSS_Nam);
2265 -- If Pnam is present, it can be either inherited from an ancestor
2266 -- type (in which case it is legal to redefine it for this type), or
2267 -- be a previous definition of the attribute for the same type (in
2268 -- which case it is illegal).
2270 -- In the first case, it will have been analyzed already, and we
2271 -- can check that its profile does not match the expected profile
2272 -- for a stream attribute of U_Ent. In the second case, either Pnam
2273 -- has been analyzed (and has the expected profile), or it has not
2274 -- been analyzed yet (case of a type that has not been frozen yet
2275 -- and for which the stream attribute has been set using Set_TSS).
2278 and then (No (First_Entity (Pnam)) or else Has_Good_Profile (Pnam))
2280 Error_Msg_Sloc := Sloc (Pnam);
2281 Error_Msg_Name_1 := Attr;
2282 Error_Msg_N ("% attribute already defined #", Nam);
2288 if Is_Entity_Name (Expr) then
2289 if not Is_Overloaded (Expr) then
2290 if Has_Good_Profile (Entity (Expr)) then
2291 Subp := Entity (Expr);
2295 Get_First_Interp (Expr, I, It);
2296 while Present (It.Nam) loop
2297 if Has_Good_Profile (It.Nam) then
2302 Get_Next_Interp (I, It);
2307 if Present (Subp) then
2308 if Is_Abstract_Subprogram (Subp) then
2309 Error_Msg_N ("stream subprogram must not be abstract", Expr);
2313 Set_Entity (Expr, Subp);
2314 Set_Etype (Expr, Etype (Subp));
2316 New_Stream_Subprogram (N, U_Ent, Subp, TSS_Nam);
2319 Error_Msg_Name_1 := Attr;
2320 Error_Msg_N ("incorrect expression for% attribute", Expr);
2322 end Analyze_Stream_TSS_Definition;
2324 ------------------------------
2325 -- Check_Indexing_Functions --
2326 ------------------------------
2328 procedure Check_Indexing_Functions is
2329 Indexing_Found : Boolean;
2331 procedure Check_One_Function (Subp : Entity_Id);
2332 -- Check one possible interpretation. Sets Indexing_Found True if an
2333 -- indexing function is found.
2335 ------------------------
2336 -- Check_One_Function --
2337 ------------------------
2339 procedure Check_One_Function (Subp : Entity_Id) is
2340 Default_Element : constant Node_Id :=
2342 (Etype (First_Formal (Subp)),
2343 Aspect_Iterator_Element);
2346 if not Check_Primitive_Function (Subp)
2347 and then not Is_Overloaded (Expr)
2350 ("aspect Indexing requires a function that applies to type&",
2354 -- An indexing function must return either the default element of
2355 -- the container, or a reference type. For variable indexing it
2356 -- must be the latter.
2358 if Present (Default_Element) then
2359 Analyze (Default_Element);
2361 if Is_Entity_Name (Default_Element)
2362 and then Covers (Entity (Default_Element), Etype (Subp))
2364 Indexing_Found := True;
2369 -- For variable_indexing the return type must be a reference type
2371 if Attr = Name_Variable_Indexing
2372 and then not Has_Implicit_Dereference (Etype (Subp))
2375 ("function for indexing must return a reference type", Subp);
2378 Indexing_Found := True;
2380 end Check_One_Function;
2382 -- Start of processing for Check_Indexing_Functions
2391 if not Is_Overloaded (Expr) then
2392 Check_One_Function (Entity (Expr));
2400 Indexing_Found := False;
2401 Get_First_Interp (Expr, I, It);
2402 while Present (It.Nam) loop
2404 -- Note that analysis will have added the interpretation
2405 -- that corresponds to the dereference. We only check the
2406 -- subprogram itself.
2408 if Is_Overloadable (It.Nam) then
2409 Check_One_Function (It.Nam);
2412 Get_Next_Interp (I, It);
2415 if not Indexing_Found then
2417 ("aspect Indexing requires a function that "
2418 & "applies to type&", Expr, Ent);
2422 end Check_Indexing_Functions;
2424 ------------------------------
2425 -- Check_Iterator_Functions --
2426 ------------------------------
2428 procedure Check_Iterator_Functions is
2429 Default : Entity_Id;
2431 function Valid_Default_Iterator (Subp : Entity_Id) return Boolean;
2432 -- Check one possible interpretation for validity
2434 ----------------------------
2435 -- Valid_Default_Iterator --
2436 ----------------------------
2438 function Valid_Default_Iterator (Subp : Entity_Id) return Boolean is
2442 if not Check_Primitive_Function (Subp) then
2445 Formal := First_Formal (Subp);
2448 -- False if any subsequent formal has no default expression
2450 Formal := Next_Formal (Formal);
2451 while Present (Formal) loop
2452 if No (Expression (Parent (Formal))) then
2456 Next_Formal (Formal);
2459 -- True if all subsequent formals have default expressions
2462 end Valid_Default_Iterator;
2464 -- Start of processing for Check_Iterator_Functions
2469 if not Is_Entity_Name (Expr) then
2470 Error_Msg_N ("aspect Iterator must be a function name", Expr);
2473 if not Is_Overloaded (Expr) then
2474 if not Check_Primitive_Function (Entity (Expr)) then
2476 ("aspect Indexing requires a function that applies to type&",
2477 Entity (Expr), Ent);
2480 if not Valid_Default_Iterator (Entity (Expr)) then
2481 Error_Msg_N ("improper function for default iterator", Expr);
2491 Get_First_Interp (Expr, I, It);
2492 while Present (It.Nam) loop
2493 if not Check_Primitive_Function (It.Nam)
2494 or else not Valid_Default_Iterator (It.Nam)
2498 elsif Present (Default) then
2499 Error_Msg_N ("default iterator must be unique", Expr);
2505 Get_Next_Interp (I, It);
2509 if Present (Default) then
2510 Set_Entity (Expr, Default);
2511 Set_Is_Overloaded (Expr, False);
2514 end Check_Iterator_Functions;
2516 -------------------------------
2517 -- Check_Primitive_Function --
2518 -------------------------------
2520 function Check_Primitive_Function (Subp : Entity_Id) return Boolean is
2524 if Ekind (Subp) /= E_Function then
2528 if No (First_Formal (Subp)) then
2531 Ctrl := Etype (First_Formal (Subp));
2535 or else Ctrl = Class_Wide_Type (Ent)
2537 (Ekind (Ctrl) = E_Anonymous_Access_Type
2539 (Designated_Type (Ctrl) = Ent
2540 or else Designated_Type (Ctrl) = Class_Wide_Type (Ent)))
2549 end Check_Primitive_Function;
2551 ----------------------
2552 -- Duplicate_Clause --
2553 ----------------------
2555 function Duplicate_Clause return Boolean is
2559 -- Nothing to do if this attribute definition clause comes from
2560 -- an aspect specification, since we could not be duplicating an
2561 -- explicit clause, and we dealt with the case of duplicated aspects
2562 -- in Analyze_Aspect_Specifications.
2564 if From_Aspect_Specification (N) then
2568 -- Otherwise current clause may duplicate previous clause, or a
2569 -- previously given pragma or aspect specification for the same
2572 A := Get_Rep_Item (U_Ent, Chars (N), Check_Parents => False);
2575 Error_Msg_Name_1 := Chars (N);
2576 Error_Msg_Sloc := Sloc (A);
2578 Error_Msg_NE ("aspect% for & previously given#", N, U_Ent);
2583 end Duplicate_Clause;
2585 -- Start of processing for Analyze_Attribute_Definition_Clause
2588 -- The following code is a defense against recursion. Not clear that
2589 -- this can happen legitimately, but perhaps some error situations
2590 -- can cause it, and we did see this recursion during testing.
2592 if Analyzed (N) then
2595 Set_Analyzed (N, True);
2598 -- Ignore some selected attributes in CodePeer mode since they are not
2599 -- relevant in this context.
2601 if CodePeer_Mode then
2604 -- Ignore Component_Size in CodePeer mode, to avoid changing the
2605 -- internal representation of types by implicitly packing them.
2607 when Attribute_Component_Size =>
2608 Rewrite (N, Make_Null_Statement (Sloc (N)));
2616 -- Process Ignore_Rep_Clauses option
2618 if Ignore_Rep_Clauses then
2621 -- The following should be ignored. They do not affect legality
2622 -- and may be target dependent. The basic idea of -gnatI is to
2623 -- ignore any rep clauses that may be target dependent but do not
2624 -- affect legality (except possibly to be rejected because they
2625 -- are incompatible with the compilation target).
2627 when Attribute_Alignment |
2628 Attribute_Bit_Order |
2629 Attribute_Component_Size |
2630 Attribute_Machine_Radix |
2631 Attribute_Object_Size |
2633 Attribute_Stream_Size |
2634 Attribute_Value_Size =>
2635 Rewrite (N, Make_Null_Statement (Sloc (N)));
2638 -- Perhaps 'Small should not be ignored by Ignore_Rep_Clauses ???
2640 when Attribute_Small =>
2641 if Ignore_Rep_Clauses then
2642 Rewrite (N, Make_Null_Statement (Sloc (N)));
2646 -- The following should not be ignored, because in the first place
2647 -- they are reasonably portable, and should not cause problems in
2648 -- compiling code from another target, and also they do affect
2649 -- legality, e.g. failing to provide a stream attribute for a
2650 -- type may make a program illegal.
2652 when Attribute_External_Tag |
2656 Attribute_Simple_Storage_Pool |
2657 Attribute_Storage_Pool |
2658 Attribute_Storage_Size |
2662 -- Other cases are errors ("attribute& cannot be set with
2663 -- definition clause"), which will be caught below.
2671 Ent := Entity (Nam);
2673 if Rep_Item_Too_Early (Ent, N) then
2677 -- Rep clause applies to full view of incomplete type or private type if
2678 -- we have one (if not, this is a premature use of the type). However,
2679 -- certain semantic checks need to be done on the specified entity (i.e.
2680 -- the private view), so we save it in Ent.
2682 if Is_Private_Type (Ent)
2683 and then Is_Derived_Type (Ent)
2684 and then not Is_Tagged_Type (Ent)
2685 and then No (Full_View (Ent))
2687 -- If this is a private type whose completion is a derivation from
2688 -- another private type, there is no full view, and the attribute
2689 -- belongs to the type itself, not its underlying parent.
2693 elsif Ekind (Ent) = E_Incomplete_Type then
2695 -- The attribute applies to the full view, set the entity of the
2696 -- attribute definition accordingly.
2698 Ent := Underlying_Type (Ent);
2700 Set_Entity (Nam, Ent);
2703 U_Ent := Underlying_Type (Ent);
2706 -- Avoid cascaded error
2708 if Etype (Nam) = Any_Type then
2711 -- Must be declared in current scope or in case of an aspect
2712 -- specification, must be visible in current scope.
2714 elsif Scope (Ent) /= Current_Scope
2716 not (From_Aspect_Specification (N)
2717 and then Scope_Within_Or_Same (Current_Scope, Scope (Ent)))
2719 Error_Msg_N ("entity must be declared in this scope", Nam);
2722 -- Must not be a source renaming (we do have some cases where the
2723 -- expander generates a renaming, and those cases are OK, in such
2724 -- cases any attribute applies to the renamed object as well).
2726 elsif Is_Object (Ent)
2727 and then Present (Renamed_Object (Ent))
2729 -- Case of renamed object from source, this is an error
2731 if Comes_From_Source (Renamed_Object (Ent)) then
2732 Get_Name_String (Chars (N));
2733 Error_Msg_Strlen := Name_Len;
2734 Error_Msg_String (1 .. Name_Len) := Name_Buffer (1 .. Name_Len);
2736 ("~ clause not allowed for a renaming declaration "
2737 & "(RM 13.1(6))", Nam);
2740 -- For the case of a compiler generated renaming, the attribute
2741 -- definition clause applies to the renamed object created by the
2742 -- expander. The easiest general way to handle this is to create a
2743 -- copy of the attribute definition clause for this object.
2747 Make_Attribute_Definition_Clause (Loc,
2749 New_Occurrence_Of (Entity (Renamed_Object (Ent)), Loc),
2751 Expression => Duplicate_Subexpr (Expression (N))));
2754 -- If no underlying entity, use entity itself, applies to some
2755 -- previously detected error cases ???
2757 elsif No (U_Ent) then
2760 -- Cannot specify for a subtype (exception Object/Value_Size)
2762 elsif Is_Type (U_Ent)
2763 and then not Is_First_Subtype (U_Ent)
2764 and then Id /= Attribute_Object_Size
2765 and then Id /= Attribute_Value_Size
2766 and then not From_At_Mod (N)
2768 Error_Msg_N ("cannot specify attribute for subtype", Nam);
2772 Set_Entity (N, U_Ent);
2773 Check_Restriction_No_Use_Of_Attribute (N);
2775 -- Switch on particular attribute
2783 -- Address attribute definition clause
2785 when Attribute_Address => Address : begin
2787 -- A little error check, catch for X'Address use X'Address;
2789 if Nkind (Nam) = N_Identifier
2790 and then Nkind (Expr) = N_Attribute_Reference
2791 and then Attribute_Name (Expr) = Name_Address
2792 and then Nkind (Prefix (Expr)) = N_Identifier
2793 and then Chars (Nam) = Chars (Prefix (Expr))
2796 ("address for & is self-referencing", Prefix (Expr), Ent);
2800 -- Not that special case, carry on with analysis of expression
2802 Analyze_And_Resolve (Expr, RTE (RE_Address));
2804 -- Even when ignoring rep clauses we need to indicate that the
2805 -- entity has an address clause and thus it is legal to declare
2808 if Ignore_Rep_Clauses then
2809 if Ekind_In (U_Ent, E_Variable, E_Constant) then
2810 Record_Rep_Item (U_Ent, N);
2816 if Duplicate_Clause then
2819 -- Case of address clause for subprogram
2821 elsif Is_Subprogram (U_Ent) then
2822 if Has_Homonym (U_Ent) then
2824 ("address clause cannot be given " &
2825 "for overloaded subprogram",
2830 -- For subprograms, all address clauses are permitted, and we
2831 -- mark the subprogram as having a deferred freeze so that Gigi
2832 -- will not elaborate it too soon.
2834 -- Above needs more comments, what is too soon about???
2836 Set_Has_Delayed_Freeze (U_Ent);
2838 -- Case of address clause for entry
2840 elsif Ekind (U_Ent) = E_Entry then
2841 if Nkind (Parent (N)) = N_Task_Body then
2843 ("entry address must be specified in task spec", Nam);
2847 -- For entries, we require a constant address
2849 Check_Constant_Address_Clause (Expr, U_Ent);
2851 -- Special checks for task types
2853 if Is_Task_Type (Scope (U_Ent))
2854 and then Comes_From_Source (Scope (U_Ent))
2857 ("??entry address declared for entry in task type", N);
2859 ("\??only one task can be declared of this type", N);
2862 -- Entry address clauses are obsolescent
2864 Check_Restriction (No_Obsolescent_Features, N);
2866 if Warn_On_Obsolescent_Feature then
2868 ("?j?attaching interrupt to task entry is an " &
2869 "obsolescent feature (RM J.7.1)", N);
2871 ("\?j?use interrupt procedure instead", N);
2874 -- Case of an address clause for a controlled object which we
2875 -- consider to be erroneous.
2877 elsif Is_Controlled (Etype (U_Ent))
2878 or else Has_Controlled_Component (Etype (U_Ent))
2881 ("??controlled object& must not be overlaid", Nam, U_Ent);
2883 ("\??Program_Error will be raised at run time", Nam);
2884 Insert_Action (Declaration_Node (U_Ent),
2885 Make_Raise_Program_Error (Loc,
2886 Reason => PE_Overlaid_Controlled_Object));
2889 -- Case of address clause for a (non-controlled) object
2892 Ekind (U_Ent) = E_Variable
2894 Ekind (U_Ent) = E_Constant
2897 Expr : constant Node_Id := Expression (N);
2902 -- Exported variables cannot have an address clause, because
2903 -- this cancels the effect of the pragma Export.
2905 if Is_Exported (U_Ent) then
2907 ("cannot export object with address clause", Nam);
2911 Find_Overlaid_Entity (N, O_Ent, Off);
2913 -- Overlaying controlled objects is erroneous
2916 and then (Has_Controlled_Component (Etype (O_Ent))
2917 or else Is_Controlled (Etype (O_Ent)))
2920 ("??cannot overlay with controlled object", Expr);
2922 ("\??Program_Error will be raised at run time", Expr);
2923 Insert_Action (Declaration_Node (U_Ent),
2924 Make_Raise_Program_Error (Loc,
2925 Reason => PE_Overlaid_Controlled_Object));
2928 elsif Present (O_Ent)
2929 and then Ekind (U_Ent) = E_Constant
2930 and then not Is_Constant_Object (O_Ent)
2932 Error_Msg_N ("??constant overlays a variable", Expr);
2934 -- Imported variables can have an address clause, but then
2935 -- the import is pretty meaningless except to suppress
2936 -- initializations, so we do not need such variables to
2937 -- be statically allocated (and in fact it causes trouble
2938 -- if the address clause is a local value).
2940 elsif Is_Imported (U_Ent) then
2941 Set_Is_Statically_Allocated (U_Ent, False);
2944 -- We mark a possible modification of a variable with an
2945 -- address clause, since it is likely aliasing is occurring.
2947 Note_Possible_Modification (Nam, Sure => False);
2949 -- Here we are checking for explicit overlap of one variable
2950 -- by another, and if we find this then mark the overlapped
2951 -- variable as also being volatile to prevent unwanted
2952 -- optimizations. This is a significant pessimization so
2953 -- avoid it when there is an offset, i.e. when the object
2954 -- is composite; they cannot be optimized easily anyway.
2957 and then Is_Object (O_Ent)
2960 -- The following test is an expedient solution to what
2961 -- is really a problem in CodePeer. Suppressing the
2962 -- Set_Treat_As_Volatile call here prevents later
2963 -- generation (in some cases) of trees that CodePeer
2964 -- should, but currently does not, handle correctly.
2965 -- This test should probably be removed when CodePeer
2966 -- is improved, just because we want the tree CodePeer
2967 -- analyzes to match the tree for which we generate code
2968 -- as closely as is practical. ???
2970 and then not CodePeer_Mode
2972 -- ??? O_Ent might not be in current unit
2974 Set_Treat_As_Volatile (O_Ent);
2977 -- Legality checks on the address clause for initialized
2978 -- objects is deferred until the freeze point, because
2979 -- a subsequent pragma might indicate that the object
2980 -- is imported and thus not initialized. Also, the address
2981 -- clause might involve entities that have yet to be
2984 Set_Has_Delayed_Freeze (U_Ent);
2986 -- If an initialization call has been generated for this
2987 -- object, it needs to be deferred to after the freeze node
2988 -- we have just now added, otherwise GIGI will see a
2989 -- reference to the variable (as actual to the IP call)
2990 -- before its definition.
2993 Init_Call : constant Node_Id :=
2994 Remove_Init_Call (U_Ent, N);
2997 if Present (Init_Call) then
2999 -- If the init call is an expression with actions with
3000 -- null expression, just extract the actions.
3002 if Nkind (Init_Call) = N_Expression_With_Actions
3004 Nkind (Expression (Init_Call)) = N_Null_Statement
3006 Append_Freeze_Actions (U_Ent, Actions (Init_Call));
3008 -- General case: move Init_Call to freeze actions
3011 Append_Freeze_Action (U_Ent, Init_Call);
3016 if Is_Exported (U_Ent) then
3018 ("& cannot be exported if an address clause is given",
3021 ("\define and export a variable "
3022 & "that holds its address instead", Nam);
3025 -- Entity has delayed freeze, so we will generate an
3026 -- alignment check at the freeze point unless suppressed.
3028 if not Range_Checks_Suppressed (U_Ent)
3029 and then not Alignment_Checks_Suppressed (U_Ent)
3031 Set_Check_Address_Alignment (N);
3034 -- Kill the size check code, since we are not allocating
3035 -- the variable, it is somewhere else.
3037 Kill_Size_Check_Code (U_Ent);
3039 -- If the address clause is of the form:
3041 -- for Y'Address use X'Address
3045 -- Const : constant Address := X'Address;
3047 -- for Y'Address use Const;
3049 -- then we make an entry in the table for checking the size
3050 -- and alignment of the overlaying variable. We defer this
3051 -- check till after code generation to take full advantage
3052 -- of the annotation done by the back end. This entry is
3053 -- only made if the address clause comes from source.
3055 -- If the entity has a generic type, the check will be
3056 -- performed in the instance if the actual type justifies
3057 -- it, and we do not insert the clause in the table to
3058 -- prevent spurious warnings.
3060 if Address_Clause_Overlay_Warnings
3061 and then Comes_From_Source (N)
3062 and then Present (O_Ent)
3063 and then Is_Object (O_Ent)
3065 if not Is_Generic_Type (Etype (U_Ent)) then
3066 Address_Clause_Checks.Append ((N, U_Ent, O_Ent, Off));
3069 -- If variable overlays a constant view, and we are
3070 -- warning on overlays, then mark the variable as
3071 -- overlaying a constant (we will give warnings later
3072 -- if this variable is assigned).
3074 if Is_Constant_Object (O_Ent)
3075 and then Ekind (U_Ent) = E_Variable
3077 Set_Overlays_Constant (U_Ent);
3082 -- Not a valid entity for an address clause
3085 Error_Msg_N ("address cannot be given for &", Nam);
3093 -- Alignment attribute definition clause
3095 when Attribute_Alignment => Alignment : declare
3096 Align : constant Uint := Get_Alignment_Value (Expr);
3097 Max_Align : constant Uint := UI_From_Int (Maximum_Alignment);
3102 if not Is_Type (U_Ent)
3103 and then Ekind (U_Ent) /= E_Variable
3104 and then Ekind (U_Ent) /= E_Constant
3106 Error_Msg_N ("alignment cannot be given for &", Nam);
3108 elsif Duplicate_Clause then
3111 elsif Align /= No_Uint then
3112 Set_Has_Alignment_Clause (U_Ent);
3114 -- Tagged type case, check for attempt to set alignment to a
3115 -- value greater than Max_Align, and reset if so.
3117 if Is_Tagged_Type (U_Ent) and then Align > Max_Align then
3119 ("alignment for & set to Maximum_Aligment??", Nam);
3120 Set_Alignment (U_Ent, Max_Align);
3125 Set_Alignment (U_Ent, Align);
3128 -- For an array type, U_Ent is the first subtype. In that case,
3129 -- also set the alignment of the anonymous base type so that
3130 -- other subtypes (such as the itypes for aggregates of the
3131 -- type) also receive the expected alignment.
3133 if Is_Array_Type (U_Ent) then
3134 Set_Alignment (Base_Type (U_Ent), Align);
3143 -- Bit_Order attribute definition clause
3145 when Attribute_Bit_Order => Bit_Order : declare
3147 if not Is_Record_Type (U_Ent) then
3149 ("Bit_Order can only be defined for record type", Nam);
3151 elsif Duplicate_Clause then
3155 Analyze_And_Resolve (Expr, RTE (RE_Bit_Order));
3157 if Etype (Expr) = Any_Type then
3160 elsif not Is_Static_Expression (Expr) then
3161 Flag_Non_Static_Expr
3162 ("Bit_Order requires static expression!", Expr);
3165 if (Expr_Value (Expr) = 0) /= Bytes_Big_Endian then
3166 Set_Reverse_Bit_Order (U_Ent, True);
3172 --------------------
3173 -- Component_Size --
3174 --------------------
3176 -- Component_Size attribute definition clause
3178 when Attribute_Component_Size => Component_Size_Case : declare
3179 Csize : constant Uint := Static_Integer (Expr);
3183 New_Ctyp : Entity_Id;
3187 if not Is_Array_Type (U_Ent) then
3188 Error_Msg_N ("component size requires array type", Nam);
3192 Btype := Base_Type (U_Ent);
3193 Ctyp := Component_Type (Btype);
3195 if Duplicate_Clause then
3198 elsif Rep_Item_Too_Early (Btype, N) then
3201 elsif Csize /= No_Uint then
3202 Check_Size (Expr, Ctyp, Csize, Biased);
3204 -- For the biased case, build a declaration for a subtype that
3205 -- will be used to represent the biased subtype that reflects
3206 -- the biased representation of components. We need the subtype
3207 -- to get proper conversions on referencing elements of the
3208 -- array. Note: component size clauses are ignored in VM mode.
3210 if VM_Target = No_VM then
3213 Make_Defining_Identifier (Loc,
3215 New_External_Name (Chars (U_Ent), 'C', 0, 'T'));
3218 Make_Subtype_Declaration (Loc,
3219 Defining_Identifier => New_Ctyp,
3220 Subtype_Indication =>
3221 New_Occurrence_Of (Component_Type (Btype), Loc));
3223 Set_Parent (Decl, N);
3224 Analyze (Decl, Suppress => All_Checks);
3226 Set_Has_Delayed_Freeze (New_Ctyp, False);
3227 Set_Esize (New_Ctyp, Csize);
3228 Set_RM_Size (New_Ctyp, Csize);
3229 Init_Alignment (New_Ctyp);
3230 Set_Is_Itype (New_Ctyp, True);
3231 Set_Associated_Node_For_Itype (New_Ctyp, U_Ent);
3233 Set_Component_Type (Btype, New_Ctyp);
3234 Set_Biased (New_Ctyp, N, "component size clause");
3237 Set_Component_Size (Btype, Csize);
3239 -- For VM case, we ignore component size clauses
3242 -- Give a warning unless we are in GNAT mode, in which case
3243 -- the warning is suppressed since it is not useful.
3245 if not GNAT_Mode then
3247 ("component size ignored in this configuration??", N);
3251 -- Deal with warning on overridden size
3253 if Warn_On_Overridden_Size
3254 and then Has_Size_Clause (Ctyp)
3255 and then RM_Size (Ctyp) /= Csize
3258 ("component size overrides size clause for&?S?", N, Ctyp);
3261 Set_Has_Component_Size_Clause (Btype, True);
3262 Set_Has_Non_Standard_Rep (Btype, True);
3264 end Component_Size_Case;
3266 -----------------------
3267 -- Constant_Indexing --
3268 -----------------------
3270 when Attribute_Constant_Indexing =>
3271 Check_Indexing_Functions;
3277 when Attribute_CPU => CPU :
3279 -- CPU attribute definition clause not allowed except from aspect
3282 if From_Aspect_Specification (N) then
3283 if not Is_Task_Type (U_Ent) then
3284 Error_Msg_N ("CPU can only be defined for task", Nam);
3286 elsif Duplicate_Clause then
3290 -- The expression must be analyzed in the special manner
3291 -- described in "Handling of Default and Per-Object
3292 -- Expressions" in sem.ads.
3294 -- The visibility to the discriminants must be restored
3296 Push_Scope_And_Install_Discriminants (U_Ent);
3297 Preanalyze_Spec_Expression (Expr, RTE (RE_CPU_Range));
3298 Uninstall_Discriminants_And_Pop_Scope (U_Ent);
3300 if not Is_Static_Expression (Expr) then
3301 Check_Restriction (Static_Priorities, Expr);
3307 ("attribute& cannot be set with definition clause", N);
3311 ----------------------
3312 -- Default_Iterator --
3313 ----------------------
3315 when Attribute_Default_Iterator => Default_Iterator : declare
3319 if not Is_Tagged_Type (U_Ent) then
3321 ("aspect Default_Iterator applies to tagged type", Nam);
3324 Check_Iterator_Functions;
3328 if not Is_Entity_Name (Expr)
3329 or else Ekind (Entity (Expr)) /= E_Function
3331 Error_Msg_N ("aspect Iterator must be a function", Expr);
3333 Func := Entity (Expr);
3336 if No (First_Formal (Func))
3337 or else Etype (First_Formal (Func)) /= U_Ent
3340 ("Default Iterator must be a primitive of&", Func, U_Ent);
3342 end Default_Iterator;
3344 ------------------------
3345 -- Dispatching_Domain --
3346 ------------------------
3348 when Attribute_Dispatching_Domain => Dispatching_Domain :
3350 -- Dispatching_Domain attribute definition clause not allowed
3351 -- except from aspect specification.
3353 if From_Aspect_Specification (N) then
3354 if not Is_Task_Type (U_Ent) then
3355 Error_Msg_N ("Dispatching_Domain can only be defined" &
3359 elsif Duplicate_Clause then
3363 -- The expression must be analyzed in the special manner
3364 -- described in "Handling of Default and Per-Object
3365 -- Expressions" in sem.ads.
3367 -- The visibility to the discriminants must be restored
3369 Push_Scope_And_Install_Discriminants (U_Ent);
3371 Preanalyze_Spec_Expression
3372 (Expr, RTE (RE_Dispatching_Domain));
3374 Uninstall_Discriminants_And_Pop_Scope (U_Ent);
3379 ("attribute& cannot be set with definition clause", N);
3381 end Dispatching_Domain;
3387 when Attribute_External_Tag => External_Tag :
3389 if not Is_Tagged_Type (U_Ent) then
3390 Error_Msg_N ("should be a tagged type", Nam);
3393 if Duplicate_Clause then
3397 Analyze_And_Resolve (Expr, Standard_String);
3399 if not Is_Static_Expression (Expr) then
3400 Flag_Non_Static_Expr
3401 ("static string required for tag name!", Nam);
3404 if VM_Target = No_VM then
3405 Set_Has_External_Tag_Rep_Clause (U_Ent);
3407 Error_Msg_Name_1 := Attr;
3409 ("% attribute unsupported in this configuration", Nam);
3412 if not Is_Library_Level_Entity (U_Ent) then
3414 ("??non-unique external tag supplied for &", N, U_Ent);
3416 ("\??same external tag applies to all "
3417 & "subprogram calls", N);
3419 ("\??corresponding internal tag cannot be obtained", N);
3424 --------------------------
3425 -- Implicit_Dereference --
3426 --------------------------
3428 when Attribute_Implicit_Dereference =>
3430 -- Legality checks already performed at the point of the type
3431 -- declaration, aspect is not delayed.
3439 when Attribute_Input =>
3440 Analyze_Stream_TSS_Definition (TSS_Stream_Input);
3441 Set_Has_Specified_Stream_Input (Ent);
3443 ------------------------
3444 -- Interrupt_Priority --
3445 ------------------------
3447 when Attribute_Interrupt_Priority => Interrupt_Priority :
3449 -- Interrupt_Priority attribute definition clause not allowed
3450 -- except from aspect specification.
3452 if From_Aspect_Specification (N) then
3453 if not (Is_Protected_Type (U_Ent)
3454 or else Is_Task_Type (U_Ent))
3457 ("Interrupt_Priority can only be defined for task" &
3458 "and protected object",
3461 elsif Duplicate_Clause then
3465 -- The expression must be analyzed in the special manner
3466 -- described in "Handling of Default and Per-Object
3467 -- Expressions" in sem.ads.
3469 -- The visibility to the discriminants must be restored
3471 Push_Scope_And_Install_Discriminants (U_Ent);
3473 Preanalyze_Spec_Expression
3474 (Expr, RTE (RE_Interrupt_Priority));
3476 Uninstall_Discriminants_And_Pop_Scope (U_Ent);
3481 ("attribute& cannot be set with definition clause", N);
3483 end Interrupt_Priority;
3485 ----------------------
3486 -- Iterator_Element --
3487 ----------------------
3489 when Attribute_Iterator_Element =>
3492 if not Is_Entity_Name (Expr)
3493 or else not Is_Type (Entity (Expr))
3495 Error_Msg_N ("aspect Iterator_Element must be a type", Expr);
3502 -- Machine radix attribute definition clause
3504 when Attribute_Machine_Radix => Machine_Radix : declare
3505 Radix : constant Uint := Static_Integer (Expr);
3508 if not Is_Decimal_Fixed_Point_Type (U_Ent) then
3509 Error_Msg_N ("decimal fixed-point type expected for &", Nam);
3511 elsif Duplicate_Clause then
3514 elsif Radix /= No_Uint then
3515 Set_Has_Machine_Radix_Clause (U_Ent);
3516 Set_Has_Non_Standard_Rep (Base_Type (U_Ent));
3520 elsif Radix = 10 then
3521 Set_Machine_Radix_10 (U_Ent);
3523 Error_Msg_N ("machine radix value must be 2 or 10", Expr);
3532 -- Object_Size attribute definition clause
3534 when Attribute_Object_Size => Object_Size : declare
3535 Size : constant Uint := Static_Integer (Expr);
3538 pragma Warnings (Off, Biased);
3541 if not Is_Type (U_Ent) then
3542 Error_Msg_N ("Object_Size cannot be given for &", Nam);
3544 elsif Duplicate_Clause then
3548 Check_Size (Expr, U_Ent, Size, Biased);
3556 UI_Mod (Size, 64) /= 0
3559 ("Object_Size must be 8, 16, 32, or multiple of 64",
3563 Set_Esize (U_Ent, Size);
3564 Set_Has_Object_Size_Clause (U_Ent);
3565 Alignment_Check_For_Size_Change (U_Ent, Size);
3573 when Attribute_Output =>
3574 Analyze_Stream_TSS_Definition (TSS_Stream_Output);
3575 Set_Has_Specified_Stream_Output (Ent);
3581 when Attribute_Priority => Priority :
3583 -- Priority attribute definition clause not allowed except from
3584 -- aspect specification.
3586 if From_Aspect_Specification (N) then
3587 if not (Is_Protected_Type (U_Ent)
3588 or else Is_Task_Type (U_Ent)
3589 or else Ekind (U_Ent) = E_Procedure)
3592 ("Priority can only be defined for task and protected " &
3596 elsif Duplicate_Clause then
3600 -- The expression must be analyzed in the special manner
3601 -- described in "Handling of Default and Per-Object
3602 -- Expressions" in sem.ads.
3604 -- The visibility to the discriminants must be restored
3606 Push_Scope_And_Install_Discriminants (U_Ent);
3607 Preanalyze_Spec_Expression (Expr, Standard_Integer);
3608 Uninstall_Discriminants_And_Pop_Scope (U_Ent);
3610 if not Is_Static_Expression (Expr) then
3611 Check_Restriction (Static_Priorities, Expr);
3617 ("attribute& cannot be set with definition clause", N);
3625 when Attribute_Read =>
3626 Analyze_Stream_TSS_Definition (TSS_Stream_Read);
3627 Set_Has_Specified_Stream_Read (Ent);
3629 --------------------------
3630 -- Scalar_Storage_Order --
3631 --------------------------
3633 -- Scalar_Storage_Order attribute definition clause
3635 when Attribute_Scalar_Storage_Order => Scalar_Storage_Order : declare
3637 if not (Is_Record_Type (U_Ent) or else Is_Array_Type (U_Ent)) then
3639 ("Scalar_Storage_Order can only be defined for "
3640 & "record or array type", Nam);
3642 elsif Duplicate_Clause then
3646 Analyze_And_Resolve (Expr, RTE (RE_Bit_Order));
3648 if Etype (Expr) = Any_Type then
3651 elsif not Is_Static_Expression (Expr) then
3652 Flag_Non_Static_Expr
3653 ("Scalar_Storage_Order requires static expression!", Expr);
3655 elsif (Expr_Value (Expr) = 0) /= Bytes_Big_Endian then
3657 -- Here for the case of a non-default (i.e. non-confirming)
3658 -- Scalar_Storage_Order attribute definition.
3660 if Support_Nondefault_SSO_On_Target then
3661 Set_Reverse_Storage_Order (Base_Type (U_Ent), True);
3664 ("non-default Scalar_Storage_Order "
3665 & "not supported on target", Expr);
3669 end Scalar_Storage_Order;
3675 -- Size attribute definition clause
3677 when Attribute_Size => Size : declare
3678 Size : constant Uint := Static_Integer (Expr);
3685 if Duplicate_Clause then
3688 elsif not Is_Type (U_Ent)
3689 and then Ekind (U_Ent) /= E_Variable
3690 and then Ekind (U_Ent) /= E_Constant
3692 Error_Msg_N ("size cannot be given for &", Nam);
3694 elsif Is_Array_Type (U_Ent)
3695 and then not Is_Constrained (U_Ent)
3698 ("size cannot be given for unconstrained array", Nam);
3700 elsif Size /= No_Uint then
3701 if VM_Target /= No_VM and then not GNAT_Mode then
3703 -- Size clause is not handled properly on VM targets.
3704 -- Display a warning unless we are in GNAT mode, in which
3705 -- case this is useless.
3708 ("size clauses are ignored in this configuration??", N);
3711 if Is_Type (U_Ent) then
3714 Etyp := Etype (U_Ent);
3717 -- Check size, note that Gigi is in charge of checking that the
3718 -- size of an array or record type is OK. Also we do not check
3719 -- the size in the ordinary fixed-point case, since it is too
3720 -- early to do so (there may be subsequent small clause that
3721 -- affects the size). We can check the size if a small clause
3722 -- has already been given.
3724 if not Is_Ordinary_Fixed_Point_Type (U_Ent)
3725 or else Has_Small_Clause (U_Ent)
3727 Check_Size (Expr, Etyp, Size, Biased);
3728 Set_Biased (U_Ent, N, "size clause", Biased);
3731 -- For types set RM_Size and Esize if possible
3733 if Is_Type (U_Ent) then
3734 Set_RM_Size (U_Ent, Size);
3736 -- For elementary types, increase Object_Size to power of 2,
3737 -- but not less than a storage unit in any case (normally
3738 -- this means it will be byte addressable).
3740 -- For all other types, nothing else to do, we leave Esize
3741 -- (object size) unset, the back end will set it from the
3742 -- size and alignment in an appropriate manner.
3744 -- In both cases, we check whether the alignment must be
3745 -- reset in the wake of the size change.
3747 if Is_Elementary_Type (U_Ent) then
3748 if Size <= System_Storage_Unit then
3749 Init_Esize (U_Ent, System_Storage_Unit);
3750 elsif Size <= 16 then
3751 Init_Esize (U_Ent, 16);
3752 elsif Size <= 32 then
3753 Init_Esize (U_Ent, 32);
3755 Set_Esize (U_Ent, (Size + 63) / 64 * 64);
3758 Alignment_Check_For_Size_Change (U_Ent, Esize (U_Ent));
3760 Alignment_Check_For_Size_Change (U_Ent, Size);
3763 -- For objects, set Esize only
3766 if Is_Elementary_Type (Etyp) then
3767 if Size /= System_Storage_Unit
3769 Size /= System_Storage_Unit * 2
3771 Size /= System_Storage_Unit * 4
3773 Size /= System_Storage_Unit * 8
3775 Error_Msg_Uint_1 := UI_From_Int (System_Storage_Unit);
3776 Error_Msg_Uint_2 := Error_Msg_Uint_1 * 8;
3778 ("size for primitive object must be a power of 2"
3779 & " in the range ^-^", N);
3783 Set_Esize (U_Ent, Size);
3786 Set_Has_Size_Clause (U_Ent);
3794 -- Small attribute definition clause
3796 when Attribute_Small => Small : declare
3797 Implicit_Base : constant Entity_Id := Base_Type (U_Ent);
3801 Analyze_And_Resolve (Expr, Any_Real);
3803 if Etype (Expr) = Any_Type then
3806 elsif not Is_Static_Expression (Expr) then
3807 Flag_Non_Static_Expr
3808 ("small requires static expression!", Expr);
3812 Small := Expr_Value_R (Expr);
3814 if Small <= Ureal_0 then
3815 Error_Msg_N ("small value must be greater than zero", Expr);
3821 if not Is_Ordinary_Fixed_Point_Type (U_Ent) then
3823 ("small requires an ordinary fixed point type", Nam);
3825 elsif Has_Small_Clause (U_Ent) then
3826 Error_Msg_N ("small already given for &", Nam);
3828 elsif Small > Delta_Value (U_Ent) then
3830 ("small value must not be greater than delta value", Nam);
3833 Set_Small_Value (U_Ent, Small);
3834 Set_Small_Value (Implicit_Base, Small);
3835 Set_Has_Small_Clause (U_Ent);
3836 Set_Has_Small_Clause (Implicit_Base);
3837 Set_Has_Non_Standard_Rep (Implicit_Base);
3845 -- Storage_Pool attribute definition clause
3847 when Attribute_Storage_Pool | Attribute_Simple_Storage_Pool => declare
3852 if Ekind (U_Ent) = E_Access_Subprogram_Type then
3854 ("storage pool cannot be given for access-to-subprogram type",
3859 Ekind_In (U_Ent, E_Access_Type, E_General_Access_Type)
3862 ("storage pool can only be given for access types", Nam);
3865 elsif Is_Derived_Type (U_Ent) then
3867 ("storage pool cannot be given for a derived access type",
3870 elsif Duplicate_Clause then
3873 elsif Present (Associated_Storage_Pool (U_Ent)) then
3874 Error_Msg_N ("storage pool already given for &", Nam);
3878 if Id = Attribute_Storage_Pool then
3880 (Expr, Class_Wide_Type (RTE (RE_Root_Storage_Pool)));
3882 -- In the Simple_Storage_Pool case, we allow a variable of any
3883 -- simple storage pool type, so we Resolve without imposing an
3887 Analyze_And_Resolve (Expr);
3889 if not Present (Get_Rep_Pragma
3890 (Etype (Expr), Name_Simple_Storage_Pool_Type))
3893 ("expression must be of a simple storage pool type", Expr);
3897 if not Denotes_Variable (Expr) then
3898 Error_Msg_N ("storage pool must be a variable", Expr);
3902 if Nkind (Expr) = N_Type_Conversion then
3903 T := Etype (Expression (Expr));
3908 -- The Stack_Bounded_Pool is used internally for implementing
3909 -- access types with a Storage_Size. Since it only work properly
3910 -- when used on one specific type, we need to check that it is not
3911 -- hijacked improperly:
3913 -- type T is access Integer;
3914 -- for T'Storage_Size use n;
3915 -- type Q is access Float;
3916 -- for Q'Storage_Size use T'Storage_Size; -- incorrect
3918 if RTE_Available (RE_Stack_Bounded_Pool)
3919 and then Base_Type (T) = RTE (RE_Stack_Bounded_Pool)
3921 Error_Msg_N ("non-shareable internal Pool", Expr);
3925 -- If the argument is a name that is not an entity name, then
3926 -- we construct a renaming operation to define an entity of
3927 -- type storage pool.
3929 if not Is_Entity_Name (Expr)
3930 and then Is_Object_Reference (Expr)
3932 Pool := Make_Temporary (Loc, 'P', Expr);
3935 Rnode : constant Node_Id :=
3936 Make_Object_Renaming_Declaration (Loc,
3937 Defining_Identifier => Pool,
3939 New_Occurrence_Of (Etype (Expr), Loc),
3943 Insert_Before (N, Rnode);
3945 Set_Associated_Storage_Pool (U_Ent, Pool);
3948 elsif Is_Entity_Name (Expr) then
3949 Pool := Entity (Expr);
3951 -- If pool is a renamed object, get original one. This can
3952 -- happen with an explicit renaming, and within instances.
3954 while Present (Renamed_Object (Pool))
3955 and then Is_Entity_Name (Renamed_Object (Pool))
3957 Pool := Entity (Renamed_Object (Pool));
3960 if Present (Renamed_Object (Pool))
3961 and then Nkind (Renamed_Object (Pool)) = N_Type_Conversion
3962 and then Is_Entity_Name (Expression (Renamed_Object (Pool)))
3964 Pool := Entity (Expression (Renamed_Object (Pool)));
3967 Set_Associated_Storage_Pool (U_Ent, Pool);
3969 elsif Nkind (Expr) = N_Type_Conversion
3970 and then Is_Entity_Name (Expression (Expr))
3971 and then Nkind (Original_Node (Expr)) = N_Attribute_Reference
3973 Pool := Entity (Expression (Expr));
3974 Set_Associated_Storage_Pool (U_Ent, Pool);
3977 Error_Msg_N ("incorrect reference to a Storage Pool", Expr);
3986 -- Storage_Size attribute definition clause
3988 when Attribute_Storage_Size => Storage_Size : declare
3989 Btype : constant Entity_Id := Base_Type (U_Ent);
3992 if Is_Task_Type (U_Ent) then
3993 Check_Restriction (No_Obsolescent_Features, N);
3995 if Warn_On_Obsolescent_Feature then
3997 ("?j?storage size clause for task is an " &
3998 "obsolescent feature (RM J.9)", N);
3999 Error_Msg_N ("\?j?use Storage_Size pragma instead", N);
4005 if not Is_Access_Type (U_Ent)
4006 and then Ekind (U_Ent) /= E_Task_Type
4008 Error_Msg_N ("storage size cannot be given for &", Nam);
4010 elsif Is_Access_Type (U_Ent) and Is_Derived_Type (U_Ent) then
4012 ("storage size cannot be given for a derived access type",
4015 elsif Duplicate_Clause then
4019 Analyze_And_Resolve (Expr, Any_Integer);
4021 if Is_Access_Type (U_Ent) then
4022 if Present (Associated_Storage_Pool (U_Ent)) then
4023 Error_Msg_N ("storage pool already given for &", Nam);
4027 if Is_OK_Static_Expression (Expr)
4028 and then Expr_Value (Expr) = 0
4030 Set_No_Pool_Assigned (Btype);
4034 Set_Has_Storage_Size_Clause (Btype);
4042 when Attribute_Stream_Size => Stream_Size : declare
4043 Size : constant Uint := Static_Integer (Expr);
4046 if Ada_Version <= Ada_95 then
4047 Check_Restriction (No_Implementation_Attributes, N);
4050 if Duplicate_Clause then
4053 elsif Is_Elementary_Type (U_Ent) then
4054 if Size /= System_Storage_Unit
4056 Size /= System_Storage_Unit * 2
4058 Size /= System_Storage_Unit * 4
4060 Size /= System_Storage_Unit * 8
4062 Error_Msg_Uint_1 := UI_From_Int (System_Storage_Unit);
4064 ("stream size for elementary type must be a"
4065 & " power of 2 and at least ^", N);
4067 elsif RM_Size (U_Ent) > Size then
4068 Error_Msg_Uint_1 := RM_Size (U_Ent);
4070 ("stream size for elementary type must be a"
4071 & " power of 2 and at least ^", N);
4074 Set_Has_Stream_Size_Clause (U_Ent);
4077 Error_Msg_N ("Stream_Size cannot be given for &", Nam);
4085 -- Value_Size attribute definition clause
4087 when Attribute_Value_Size => Value_Size : declare
4088 Size : constant Uint := Static_Integer (Expr);
4092 if not Is_Type (U_Ent) then
4093 Error_Msg_N ("Value_Size cannot be given for &", Nam);
4095 elsif Duplicate_Clause then
4098 elsif Is_Array_Type (U_Ent)
4099 and then not Is_Constrained (U_Ent)
4102 ("Value_Size cannot be given for unconstrained array", Nam);
4105 if Is_Elementary_Type (U_Ent) then
4106 Check_Size (Expr, U_Ent, Size, Biased);
4107 Set_Biased (U_Ent, N, "value size clause", Biased);
4110 Set_RM_Size (U_Ent, Size);
4114 -----------------------
4115 -- Variable_Indexing --
4116 -----------------------
4118 when Attribute_Variable_Indexing =>
4119 Check_Indexing_Functions;
4125 when Attribute_Write =>
4126 Analyze_Stream_TSS_Definition (TSS_Stream_Write);
4127 Set_Has_Specified_Stream_Write (Ent);
4129 -- All other attributes cannot be set
4133 ("attribute& cannot be set with definition clause", N);
4136 -- The test for the type being frozen must be performed after any
4137 -- expression the clause has been analyzed since the expression itself
4138 -- might cause freezing that makes the clause illegal.
4140 if Rep_Item_Too_Late (U_Ent, N, FOnly) then
4143 end Analyze_Attribute_Definition_Clause;
4145 ----------------------------
4146 -- Analyze_Code_Statement --
4147 ----------------------------
4149 procedure Analyze_Code_Statement (N : Node_Id) is
4150 HSS : constant Node_Id := Parent (N);
4151 SBody : constant Node_Id := Parent (HSS);
4152 Subp : constant Entity_Id := Current_Scope;
4159 -- Analyze and check we get right type, note that this implements the
4160 -- requirement (RM 13.8(1)) that Machine_Code be with'ed, since that
4161 -- is the only way that Asm_Insn could possibly be visible.
4163 Analyze_And_Resolve (Expression (N));
4165 if Etype (Expression (N)) = Any_Type then
4167 elsif Etype (Expression (N)) /= RTE (RE_Asm_Insn) then
4168 Error_Msg_N ("incorrect type for code statement", N);
4172 Check_Code_Statement (N);
4174 -- Make sure we appear in the handled statement sequence of a
4175 -- subprogram (RM 13.8(3)).
4177 if Nkind (HSS) /= N_Handled_Sequence_Of_Statements
4178 or else Nkind (SBody) /= N_Subprogram_Body
4181 ("code statement can only appear in body of subprogram", N);
4185 -- Do remaining checks (RM 13.8(3)) if not already done
4187 if not Is_Machine_Code_Subprogram (Subp) then
4188 Set_Is_Machine_Code_Subprogram (Subp);
4190 -- No exception handlers allowed
4192 if Present (Exception_Handlers (HSS)) then
4194 ("exception handlers not permitted in machine code subprogram",
4195 First (Exception_Handlers (HSS)));
4198 -- No declarations other than use clauses and pragmas (we allow
4199 -- certain internally generated declarations as well).
4201 Decl := First (Declarations (SBody));
4202 while Present (Decl) loop
4203 DeclO := Original_Node (Decl);
4204 if Comes_From_Source (DeclO)
4205 and not Nkind_In (DeclO, N_Pragma,
4206 N_Use_Package_Clause,
4208 N_Implicit_Label_Declaration)
4211 ("this declaration not allowed in machine code subprogram",
4218 -- No statements other than code statements, pragmas, and labels.
4219 -- Again we allow certain internally generated statements.
4221 -- In Ada 2012, qualified expressions are names, and the code
4222 -- statement is initially parsed as a procedure call.
4224 Stmt := First (Statements (HSS));
4225 while Present (Stmt) loop
4226 StmtO := Original_Node (Stmt);
4228 -- A procedure call transformed into a code statement is OK.
4230 if Ada_Version >= Ada_2012
4231 and then Nkind (StmtO) = N_Procedure_Call_Statement
4232 and then Nkind (Name (StmtO)) = N_Qualified_Expression
4236 elsif Comes_From_Source (StmtO)
4237 and then not Nkind_In (StmtO, N_Pragma,
4242 ("this statement is not allowed in machine code subprogram",
4249 end Analyze_Code_Statement;
4251 -----------------------------------------------
4252 -- Analyze_Enumeration_Representation_Clause --
4253 -----------------------------------------------
4255 procedure Analyze_Enumeration_Representation_Clause (N : Node_Id) is
4256 Ident : constant Node_Id := Identifier (N);
4257 Aggr : constant Node_Id := Array_Aggregate (N);
4258 Enumtype : Entity_Id;
4265 Err : Boolean := False;
4266 -- Set True to avoid cascade errors and crashes on incorrect source code
4268 Lo : constant Uint := Expr_Value (Type_Low_Bound (Universal_Integer));
4269 Hi : constant Uint := Expr_Value (Type_High_Bound (Universal_Integer));
4270 -- Allowed range of universal integer (= allowed range of enum lit vals)
4274 -- Minimum and maximum values of entries
4277 -- Pointer to node for literal providing max value
4280 if Ignore_Rep_Clauses then
4284 -- Ignore enumeration rep clauses by default in CodePeer mode,
4285 -- unless -gnatd.I is specified, as a work around for potential false
4286 -- positive messages.
4288 if CodePeer_Mode and not Debug_Flag_Dot_II then
4292 -- First some basic error checks
4295 Enumtype := Entity (Ident);
4297 if Enumtype = Any_Type
4298 or else Rep_Item_Too_Early (Enumtype, N)
4302 Enumtype := Underlying_Type (Enumtype);
4305 if not Is_Enumeration_Type (Enumtype) then
4307 ("enumeration type required, found}",
4308 Ident, First_Subtype (Enumtype));
4312 -- Ignore rep clause on generic actual type. This will already have
4313 -- been flagged on the template as an error, and this is the safest
4314 -- way to ensure we don't get a junk cascaded message in the instance.
4316 if Is_Generic_Actual_Type (Enumtype) then
4319 -- Type must be in current scope
4321 elsif Scope (Enumtype) /= Current_Scope then
4322 Error_Msg_N ("type must be declared in this scope", Ident);
4325 -- Type must be a first subtype
4327 elsif not Is_First_Subtype (Enumtype) then
4328 Error_Msg_N ("cannot give enumeration rep clause for subtype", N);
4331 -- Ignore duplicate rep clause
4333 elsif Has_Enumeration_Rep_Clause (Enumtype) then
4334 Error_Msg_N ("duplicate enumeration rep clause ignored", N);
4337 -- Don't allow rep clause for standard [wide_[wide_]]character
4339 elsif Is_Standard_Character_Type (Enumtype) then
4340 Error_Msg_N ("enumeration rep clause not allowed for this type", N);
4343 -- Check that the expression is a proper aggregate (no parentheses)
4345 elsif Paren_Count (Aggr) /= 0 then
4347 ("extra parentheses surrounding aggregate not allowed",
4351 -- All tests passed, so set rep clause in place
4354 Set_Has_Enumeration_Rep_Clause (Enumtype);
4355 Set_Has_Enumeration_Rep_Clause (Base_Type (Enumtype));
4358 -- Now we process the aggregate. Note that we don't use the normal
4359 -- aggregate code for this purpose, because we don't want any of the
4360 -- normal expansion activities, and a number of special semantic
4361 -- rules apply (including the component type being any integer type)
4363 Elit := First_Literal (Enumtype);
4365 -- First the positional entries if any
4367 if Present (Expressions (Aggr)) then
4368 Expr := First (Expressions (Aggr));
4369 while Present (Expr) loop
4371 Error_Msg_N ("too many entries in aggregate", Expr);
4375 Val := Static_Integer (Expr);
4377 -- Err signals that we found some incorrect entries processing
4378 -- the list. The final checks for completeness and ordering are
4379 -- skipped in this case.
4381 if Val = No_Uint then
4383 elsif Val < Lo or else Hi < Val then
4384 Error_Msg_N ("value outside permitted range", Expr);
4388 Set_Enumeration_Rep (Elit, Val);
4389 Set_Enumeration_Rep_Expr (Elit, Expr);
4395 -- Now process the named entries if present
4397 if Present (Component_Associations (Aggr)) then
4398 Assoc := First (Component_Associations (Aggr));
4399 while Present (Assoc) loop
4400 Choice := First (Choices (Assoc));
4402 if Present (Next (Choice)) then
4404 ("multiple choice not allowed here", Next (Choice));
4408 if Nkind (Choice) = N_Others_Choice then
4409 Error_Msg_N ("others choice not allowed here", Choice);
4412 elsif Nkind (Choice) = N_Range then
4414 -- ??? should allow zero/one element range here
4416 Error_Msg_N ("range not allowed here", Choice);
4420 Analyze_And_Resolve (Choice, Enumtype);
4422 if Error_Posted (Choice) then
4427 if Is_Entity_Name (Choice)
4428 and then Is_Type (Entity (Choice))
4430 Error_Msg_N ("subtype name not allowed here", Choice);
4433 -- ??? should allow static subtype with zero/one entry
4435 elsif Etype (Choice) = Base_Type (Enumtype) then
4436 if not Is_Static_Expression (Choice) then
4437 Flag_Non_Static_Expr
4438 ("non-static expression used for choice!", Choice);
4442 Elit := Expr_Value_E (Choice);
4444 if Present (Enumeration_Rep_Expr (Elit)) then
4446 Sloc (Enumeration_Rep_Expr (Elit));
4448 ("representation for& previously given#",
4453 Set_Enumeration_Rep_Expr (Elit, Expression (Assoc));
4455 Expr := Expression (Assoc);
4456 Val := Static_Integer (Expr);
4458 if Val = No_Uint then
4461 elsif Val < Lo or else Hi < Val then
4462 Error_Msg_N ("value outside permitted range", Expr);
4466 Set_Enumeration_Rep (Elit, Val);
4476 -- Aggregate is fully processed. Now we check that a full set of
4477 -- representations was given, and that they are in range and in order.
4478 -- These checks are only done if no other errors occurred.
4484 Elit := First_Literal (Enumtype);
4485 while Present (Elit) loop
4486 if No (Enumeration_Rep_Expr (Elit)) then
4487 Error_Msg_NE ("missing representation for&!", N, Elit);
4490 Val := Enumeration_Rep (Elit);
4492 if Min = No_Uint then
4496 if Val /= No_Uint then
4497 if Max /= No_Uint and then Val <= Max then
4499 ("enumeration value for& not ordered!",
4500 Enumeration_Rep_Expr (Elit), Elit);
4503 Max_Node := Enumeration_Rep_Expr (Elit);
4507 -- If there is at least one literal whose representation is not
4508 -- equal to the Pos value, then note that this enumeration type
4509 -- has a non-standard representation.
4511 if Val /= Enumeration_Pos (Elit) then
4512 Set_Has_Non_Standard_Rep (Base_Type (Enumtype));
4519 -- Now set proper size information
4522 Minsize : Uint := UI_From_Int (Minimum_Size (Enumtype));
4525 if Has_Size_Clause (Enumtype) then
4527 -- All OK, if size is OK now
4529 if RM_Size (Enumtype) >= Minsize then
4533 -- Try if we can get by with biasing
4536 UI_From_Int (Minimum_Size (Enumtype, Biased => True));
4538 -- Error message if even biasing does not work
4540 if RM_Size (Enumtype) < Minsize then
4541 Error_Msg_Uint_1 := RM_Size (Enumtype);
4542 Error_Msg_Uint_2 := Max;
4544 ("previously given size (^) is too small "
4545 & "for this value (^)", Max_Node);
4547 -- If biasing worked, indicate that we now have biased rep
4551 (Enumtype, Size_Clause (Enumtype), "size clause");
4556 Set_RM_Size (Enumtype, Minsize);
4557 Set_Enum_Esize (Enumtype);
4560 Set_RM_Size (Base_Type (Enumtype), RM_Size (Enumtype));
4561 Set_Esize (Base_Type (Enumtype), Esize (Enumtype));
4562 Set_Alignment (Base_Type (Enumtype), Alignment (Enumtype));
4566 -- We repeat the too late test in case it froze itself!
4568 if Rep_Item_Too_Late (Enumtype, N) then
4571 end Analyze_Enumeration_Representation_Clause;
4573 ----------------------------
4574 -- Analyze_Free_Statement --
4575 ----------------------------
4577 procedure Analyze_Free_Statement (N : Node_Id) is
4579 Analyze (Expression (N));
4580 end Analyze_Free_Statement;
4582 ---------------------------
4583 -- Analyze_Freeze_Entity --
4584 ---------------------------
4586 procedure Analyze_Freeze_Entity (N : Node_Id) is
4587 E : constant Entity_Id := Entity (N);
4590 -- Remember that we are processing a freezing entity. Required to
4591 -- ensure correct decoration of internal entities associated with
4592 -- interfaces (see New_Overloaded_Entity).
4594 Inside_Freezing_Actions := Inside_Freezing_Actions + 1;
4596 -- For tagged types covering interfaces add internal entities that link
4597 -- the primitives of the interfaces with the primitives that cover them.
4598 -- Note: These entities were originally generated only when generating
4599 -- code because their main purpose was to provide support to initialize
4600 -- the secondary dispatch tables. They are now generated also when
4601 -- compiling with no code generation to provide ASIS the relationship
4602 -- between interface primitives and tagged type primitives. They are
4603 -- also used to locate primitives covering interfaces when processing
4604 -- generics (see Derive_Subprograms).
4606 if Ada_Version >= Ada_2005
4607 and then Ekind (E) = E_Record_Type
4608 and then Is_Tagged_Type (E)
4609 and then not Is_Interface (E)
4610 and then Has_Interfaces (E)
4612 -- This would be a good common place to call the routine that checks
4613 -- overriding of interface primitives (and thus factorize calls to
4614 -- Check_Abstract_Overriding located at different contexts in the
4615 -- compiler). However, this is not possible because it causes
4616 -- spurious errors in case of late overriding.
4618 Add_Internal_Interface_Entities (E);
4623 if Ekind (E) = E_Record_Type
4624 and then Is_CPP_Class (E)
4625 and then Is_Tagged_Type (E)
4626 and then Tagged_Type_Expansion
4627 and then Expander_Active
4629 if CPP_Num_Prims (E) = 0 then
4631 -- If the CPP type has user defined components then it must import
4632 -- primitives from C++. This is required because if the C++ class
4633 -- has no primitives then the C++ compiler does not added the _tag
4634 -- component to the type.
4636 pragma Assert (Chars (First_Entity (E)) = Name_uTag);
4638 if First_Entity (E) /= Last_Entity (E) then
4640 ("'C'P'P type must import at least one primitive from C++??",
4645 -- Check that all its primitives are abstract or imported from C++.
4646 -- Check also availability of the C++ constructor.
4649 Has_Constructors : constant Boolean := Has_CPP_Constructors (E);
4651 Error_Reported : Boolean := False;
4655 Elmt := First_Elmt (Primitive_Operations (E));
4656 while Present (Elmt) loop
4657 Prim := Node (Elmt);
4659 if Comes_From_Source (Prim) then
4660 if Is_Abstract_Subprogram (Prim) then
4663 elsif not Is_Imported (Prim)
4664 or else Convention (Prim) /= Convention_CPP
4667 ("primitives of 'C'P'P types must be imported from C++ "
4668 & "or abstract??", Prim);
4670 elsif not Has_Constructors
4671 and then not Error_Reported
4673 Error_Msg_Name_1 := Chars (E);
4675 ("??'C'P'P constructor required for type %", Prim);
4676 Error_Reported := True;
4685 -- Check Ada derivation of CPP type
4688 and then Tagged_Type_Expansion
4689 and then Ekind (E) = E_Record_Type
4690 and then Etype (E) /= E
4691 and then Is_CPP_Class (Etype (E))
4692 and then CPP_Num_Prims (Etype (E)) > 0
4693 and then not Is_CPP_Class (E)
4694 and then not Has_CPP_Constructors (Etype (E))
4696 -- If the parent has C++ primitives but it has no constructor then
4697 -- check that all the primitives are overridden in this derivation;
4698 -- otherwise the constructor of the parent is needed to build the
4706 Elmt := First_Elmt (Primitive_Operations (E));
4707 while Present (Elmt) loop
4708 Prim := Node (Elmt);
4710 if not Is_Abstract_Subprogram (Prim)
4711 and then No (Interface_Alias (Prim))
4712 and then Find_Dispatching_Type (Ultimate_Alias (Prim)) /= E
4714 Error_Msg_Name_1 := Chars (Etype (E));
4716 ("'C'P'P constructor required for parent type %", E);
4725 Inside_Freezing_Actions := Inside_Freezing_Actions - 1;
4727 -- If we have a type with predicates, build predicate function
4729 if Is_Type (E) and then Has_Predicates (E) then
4730 Build_Predicate_Functions (E, N);
4733 -- If type has delayed aspects, this is where we do the preanalysis at
4734 -- the freeze point, as part of the consistent visibility check. Note
4735 -- that this must be done after calling Build_Predicate_Functions or
4736 -- Build_Invariant_Procedure since these subprograms fix occurrences of
4737 -- the subtype name in the saved expression so that they will not cause
4738 -- trouble in the preanalysis.
4740 if Has_Delayed_Aspects (E)
4741 and then Scope (E) = Current_Scope
4743 -- Retrieve the visibility to the discriminants in order to properly
4744 -- analyze the aspects.
4746 Push_Scope_And_Install_Discriminants (E);
4752 -- Look for aspect specification entries for this entity
4754 Ritem := First_Rep_Item (E);
4755 while Present (Ritem) loop
4756 if Nkind (Ritem) = N_Aspect_Specification
4757 and then Entity (Ritem) = E
4758 and then Is_Delayed_Aspect (Ritem)
4760 Check_Aspect_At_Freeze_Point (Ritem);
4763 Next_Rep_Item (Ritem);
4767 Uninstall_Discriminants_And_Pop_Scope (E);
4769 end Analyze_Freeze_Entity;
4771 ------------------------------------------
4772 -- Analyze_Record_Representation_Clause --
4773 ------------------------------------------
4775 -- Note: we check as much as we can here, but we can't do any checks
4776 -- based on the position values (e.g. overlap checks) until freeze time
4777 -- because especially in Ada 2005 (machine scalar mode), the processing
4778 -- for non-standard bit order can substantially change the positions.
4779 -- See procedure Check_Record_Representation_Clause (called from Freeze)
4780 -- for the remainder of this processing.
4782 procedure Analyze_Record_Representation_Clause (N : Node_Id) is
4783 Ident : constant Node_Id := Identifier (N);
4788 Hbit : Uint := Uint_0;
4792 Rectype : Entity_Id;
4795 function Is_Inherited (Comp : Entity_Id) return Boolean;
4796 -- True if Comp is an inherited component in a record extension
4802 function Is_Inherited (Comp : Entity_Id) return Boolean is
4803 Comp_Base : Entity_Id;
4806 if Ekind (Rectype) = E_Record_Subtype then
4807 Comp_Base := Original_Record_Component (Comp);
4812 return Comp_Base /= Original_Record_Component (Comp_Base);
4817 Is_Record_Extension : Boolean;
4818 -- True if Rectype is a record extension
4820 CR_Pragma : Node_Id := Empty;
4821 -- Points to N_Pragma node if Complete_Representation pragma present
4823 -- Start of processing for Analyze_Record_Representation_Clause
4826 if Ignore_Rep_Clauses then
4831 Rectype := Entity (Ident);
4833 if Rectype = Any_Type or else Rep_Item_Too_Early (Rectype, N) then
4836 Rectype := Underlying_Type (Rectype);
4839 -- First some basic error checks
4841 if not Is_Record_Type (Rectype) then
4843 ("record type required, found}", Ident, First_Subtype (Rectype));
4846 elsif Scope (Rectype) /= Current_Scope then
4847 Error_Msg_N ("type must be declared in this scope", N);
4850 elsif not Is_First_Subtype (Rectype) then
4851 Error_Msg_N ("cannot give record rep clause for subtype", N);
4854 elsif Has_Record_Rep_Clause (Rectype) then
4855 Error_Msg_N ("duplicate record rep clause ignored", N);
4858 elsif Rep_Item_Too_Late (Rectype, N) then
4862 -- We know we have a first subtype, now possibly go the the anonymous
4863 -- base type to determine whether Rectype is a record extension.
4865 Recdef := Type_Definition (Declaration_Node (Base_Type (Rectype)));
4866 Is_Record_Extension :=
4867 Nkind (Recdef) = N_Derived_Type_Definition
4868 and then Present (Record_Extension_Part (Recdef));
4870 if Present (Mod_Clause (N)) then
4872 Loc : constant Source_Ptr := Sloc (N);
4873 M : constant Node_Id := Mod_Clause (N);
4874 P : constant List_Id := Pragmas_Before (M);
4878 pragma Warnings (Off, Mod_Val);
4881 Check_Restriction (No_Obsolescent_Features, Mod_Clause (N));
4883 if Warn_On_Obsolescent_Feature then
4885 ("?j?mod clause is an obsolescent feature (RM J.8)", N);
4887 ("\?j?use alignment attribute definition clause instead", N);
4894 -- In ASIS_Mode mode, expansion is disabled, but we must convert
4895 -- the Mod clause into an alignment clause anyway, so that the
4896 -- back-end can compute and back-annotate properly the size and
4897 -- alignment of types that may include this record.
4899 -- This seems dubious, this destroys the source tree in a manner
4900 -- not detectable by ASIS ???
4902 if Operating_Mode = Check_Semantics and then ASIS_Mode then
4904 Make_Attribute_Definition_Clause (Loc,
4905 Name => New_Reference_To (Base_Type (Rectype), Loc),
4906 Chars => Name_Alignment,
4907 Expression => Relocate_Node (Expression (M)));
4909 Set_From_At_Mod (AtM_Nod);
4910 Insert_After (N, AtM_Nod);
4911 Mod_Val := Get_Alignment_Value (Expression (AtM_Nod));
4912 Set_Mod_Clause (N, Empty);
4915 -- Get the alignment value to perform error checking
4917 Mod_Val := Get_Alignment_Value (Expression (M));
4922 -- For untagged types, clear any existing component clauses for the
4923 -- type. If the type is derived, this is what allows us to override
4924 -- a rep clause for the parent. For type extensions, the representation
4925 -- of the inherited components is inherited, so we want to keep previous
4926 -- component clauses for completeness.
4928 if not Is_Tagged_Type (Rectype) then
4929 Comp := First_Component_Or_Discriminant (Rectype);
4930 while Present (Comp) loop
4931 Set_Component_Clause (Comp, Empty);
4932 Next_Component_Or_Discriminant (Comp);
4936 -- All done if no component clauses
4938 CC := First (Component_Clauses (N));
4944 -- A representation like this applies to the base type
4946 Set_Has_Record_Rep_Clause (Base_Type (Rectype));
4947 Set_Has_Non_Standard_Rep (Base_Type (Rectype));
4948 Set_Has_Specified_Layout (Base_Type (Rectype));
4950 -- Process the component clauses
4952 while Present (CC) loop
4956 if Nkind (CC) = N_Pragma then
4959 -- The only pragma of interest is Complete_Representation
4961 if Pragma_Name (CC) = Name_Complete_Representation then
4965 -- Processing for real component clause
4968 Posit := Static_Integer (Position (CC));
4969 Fbit := Static_Integer (First_Bit (CC));
4970 Lbit := Static_Integer (Last_Bit (CC));
4973 and then Fbit /= No_Uint
4974 and then Lbit /= No_Uint
4978 ("position cannot be negative", Position (CC));
4982 ("first bit cannot be negative", First_Bit (CC));
4984 -- The Last_Bit specified in a component clause must not be
4985 -- less than the First_Bit minus one (RM-13.5.1(10)).
4987 elsif Lbit < Fbit - 1 then
4989 ("last bit cannot be less than first bit minus one",
4992 -- Values look OK, so find the corresponding record component
4993 -- Even though the syntax allows an attribute reference for
4994 -- implementation-defined components, GNAT does not allow the
4995 -- tag to get an explicit position.
4997 elsif Nkind (Component_Name (CC)) = N_Attribute_Reference then
4998 if Attribute_Name (Component_Name (CC)) = Name_Tag then
4999 Error_Msg_N ("position of tag cannot be specified", CC);
5001 Error_Msg_N ("illegal component name", CC);
5005 Comp := First_Entity (Rectype);
5006 while Present (Comp) loop
5007 exit when Chars (Comp) = Chars (Component_Name (CC));
5013 -- Maybe component of base type that is absent from
5014 -- statically constrained first subtype.
5016 Comp := First_Entity (Base_Type (Rectype));
5017 while Present (Comp) loop
5018 exit when Chars (Comp) = Chars (Component_Name (CC));
5025 ("component clause is for non-existent field", CC);
5027 -- Ada 2012 (AI05-0026): Any name that denotes a
5028 -- discriminant of an object of an unchecked union type
5029 -- shall not occur within a record_representation_clause.
5031 -- The general restriction of using record rep clauses on
5032 -- Unchecked_Union types has now been lifted. Since it is
5033 -- possible to introduce a record rep clause which mentions
5034 -- the discriminant of an Unchecked_Union in non-Ada 2012
5035 -- code, this check is applied to all versions of the
5038 elsif Ekind (Comp) = E_Discriminant
5039 and then Is_Unchecked_Union (Rectype)
5042 ("cannot reference discriminant of unchecked union",
5043 Component_Name (CC));
5045 elsif Is_Record_Extension and then Is_Inherited (Comp) then
5047 ("component clause not allowed for inherited "
5048 & "component&", CC, Comp);
5050 elsif Present (Component_Clause (Comp)) then
5052 -- Diagnose duplicate rep clause, or check consistency
5053 -- if this is an inherited component. In a double fault,
5054 -- there may be a duplicate inconsistent clause for an
5055 -- inherited component.
5057 if Scope (Original_Record_Component (Comp)) = Rectype
5058 or else Parent (Component_Clause (Comp)) = N
5060 Error_Msg_Sloc := Sloc (Component_Clause (Comp));
5061 Error_Msg_N ("component clause previously given#", CC);
5065 Rep1 : constant Node_Id := Component_Clause (Comp);
5067 if Intval (Position (Rep1)) /=
5068 Intval (Position (CC))
5069 or else Intval (First_Bit (Rep1)) /=
5070 Intval (First_Bit (CC))
5071 or else Intval (Last_Bit (Rep1)) /=
5072 Intval (Last_Bit (CC))
5075 ("component clause inconsistent "
5076 & "with representation of ancestor", CC);
5078 elsif Warn_On_Redundant_Constructs then
5080 ("?r?redundant confirming component clause "
5081 & "for component!", CC);
5086 -- Normal case where this is the first component clause we
5087 -- have seen for this entity, so set it up properly.
5090 -- Make reference for field in record rep clause and set
5091 -- appropriate entity field in the field identifier.
5094 (Comp, Component_Name (CC), Set_Ref => False);
5095 Set_Entity (Component_Name (CC), Comp);
5097 -- Update Fbit and Lbit to the actual bit number
5099 Fbit := Fbit + UI_From_Int (SSU) * Posit;
5100 Lbit := Lbit + UI_From_Int (SSU) * Posit;
5102 if Has_Size_Clause (Rectype)
5103 and then RM_Size (Rectype) <= Lbit
5106 ("bit number out of range of specified size",
5109 Set_Component_Clause (Comp, CC);
5110 Set_Component_Bit_Offset (Comp, Fbit);
5111 Set_Esize (Comp, 1 + (Lbit - Fbit));
5112 Set_Normalized_First_Bit (Comp, Fbit mod SSU);
5113 Set_Normalized_Position (Comp, Fbit / SSU);
5115 if Warn_On_Overridden_Size
5116 and then Has_Size_Clause (Etype (Comp))
5117 and then RM_Size (Etype (Comp)) /= Esize (Comp)
5120 ("?S?component size overrides size clause for&",
5121 Component_Name (CC), Etype (Comp));
5124 -- This information is also set in the corresponding
5125 -- component of the base type, found by accessing the
5126 -- Original_Record_Component link if it is present.
5128 Ocomp := Original_Record_Component (Comp);
5135 (Component_Name (CC),
5141 (Comp, First_Node (CC), "component clause", Biased);
5143 if Present (Ocomp) then
5144 Set_Component_Clause (Ocomp, CC);
5145 Set_Component_Bit_Offset (Ocomp, Fbit);
5146 Set_Normalized_First_Bit (Ocomp, Fbit mod SSU);
5147 Set_Normalized_Position (Ocomp, Fbit / SSU);
5148 Set_Esize (Ocomp, 1 + (Lbit - Fbit));
5150 Set_Normalized_Position_Max
5151 (Ocomp, Normalized_Position (Ocomp));
5153 -- Note: we don't use Set_Biased here, because we
5154 -- already gave a warning above if needed, and we
5155 -- would get a duplicate for the same name here.
5157 Set_Has_Biased_Representation
5158 (Ocomp, Has_Biased_Representation (Comp));
5161 if Esize (Comp) < 0 then
5162 Error_Msg_N ("component size is negative", CC);
5173 -- Check missing components if Complete_Representation pragma appeared
5175 if Present (CR_Pragma) then
5176 Comp := First_Component_Or_Discriminant (Rectype);
5177 while Present (Comp) loop
5178 if No (Component_Clause (Comp)) then
5180 ("missing component clause for &", CR_Pragma, Comp);
5183 Next_Component_Or_Discriminant (Comp);
5186 -- Give missing components warning if required
5188 elsif Warn_On_Unrepped_Components then
5190 Num_Repped_Components : Nat := 0;
5191 Num_Unrepped_Components : Nat := 0;
5194 -- First count number of repped and unrepped components
5196 Comp := First_Component_Or_Discriminant (Rectype);
5197 while Present (Comp) loop
5198 if Present (Component_Clause (Comp)) then
5199 Num_Repped_Components := Num_Repped_Components + 1;
5201 Num_Unrepped_Components := Num_Unrepped_Components + 1;
5204 Next_Component_Or_Discriminant (Comp);
5207 -- We are only interested in the case where there is at least one
5208 -- unrepped component, and at least half the components have rep
5209 -- clauses. We figure that if less than half have them, then the
5210 -- partial rep clause is really intentional. If the component
5211 -- type has no underlying type set at this point (as for a generic
5212 -- formal type), we don't know enough to give a warning on the
5215 if Num_Unrepped_Components > 0
5216 and then Num_Unrepped_Components < Num_Repped_Components
5218 Comp := First_Component_Or_Discriminant (Rectype);
5219 while Present (Comp) loop
5220 if No (Component_Clause (Comp))
5221 and then Comes_From_Source (Comp)
5222 and then Present (Underlying_Type (Etype (Comp)))
5223 and then (Is_Scalar_Type (Underlying_Type (Etype (Comp)))
5224 or else Size_Known_At_Compile_Time
5225 (Underlying_Type (Etype (Comp))))
5226 and then not Has_Warnings_Off (Rectype)
5228 Error_Msg_Sloc := Sloc (Comp);
5230 ("?C?no component clause given for & declared #",
5234 Next_Component_Or_Discriminant (Comp);
5239 end Analyze_Record_Representation_Clause;
5241 -------------------------------------------
5242 -- Build_Invariant_Procedure_Declaration --
5243 -------------------------------------------
5245 function Build_Invariant_Procedure_Declaration
5246 (Typ : Entity_Id) return Node_Id
5248 Loc : constant Source_Ptr := Sloc (Typ);
5249 Object_Entity : constant Entity_Id :=
5250 Make_Defining_Identifier (Loc, New_Internal_Name ('I'));
5255 Set_Etype (Object_Entity, Typ);
5257 -- Check for duplicate definiations.
5259 if Has_Invariants (Typ) and then Present (Invariant_Procedure (Typ)) then
5264 Make_Defining_Identifier (Loc,
5265 Chars => New_External_Name (Chars (Typ), "Invariant"));
5266 Set_Has_Invariants (Typ);
5267 Set_Ekind (SId, E_Procedure);
5268 Set_Is_Invariant_Procedure (SId);
5269 Set_Invariant_Procedure (Typ, SId);
5272 Make_Procedure_Specification (Loc,
5273 Defining_Unit_Name => SId,
5274 Parameter_Specifications => New_List (
5275 Make_Parameter_Specification (Loc,
5276 Defining_Identifier => Object_Entity,
5277 Parameter_Type => New_Occurrence_Of (Typ, Loc))));
5279 return Make_Subprogram_Declaration (Loc, Specification => Spec);
5280 end Build_Invariant_Procedure_Declaration;
5282 -------------------------------
5283 -- Build_Invariant_Procedure --
5284 -------------------------------
5286 -- The procedure that is constructed here has the form
5288 -- procedure typInvariant (Ixxx : typ) is
5290 -- pragma Check (Invariant, exp, "failed invariant from xxx");
5291 -- pragma Check (Invariant, exp, "failed invariant from xxx");
5293 -- pragma Check (Invariant, exp, "failed inherited invariant from xxx");
5295 -- end typInvariant;
5297 procedure Build_Invariant_Procedure (Typ : Entity_Id; N : Node_Id) is
5298 Loc : constant Source_Ptr := Sloc (Typ);
5305 Visible_Decls : constant List_Id := Visible_Declarations (N);
5306 Private_Decls : constant List_Id := Private_Declarations (N);
5308 procedure Add_Invariants (T : Entity_Id; Inherit : Boolean);
5309 -- Appends statements to Stmts for any invariants in the rep item chain
5310 -- of the given type. If Inherit is False, then we only process entries
5311 -- on the chain for the type Typ. If Inherit is True, then we ignore any
5312 -- Invariant aspects, but we process all Invariant'Class aspects, adding
5313 -- "inherited" to the exception message and generating an informational
5314 -- message about the inheritance of an invariant.
5316 Object_Name : Name_Id;
5317 -- Name for argument of invariant procedure
5319 Object_Entity : Node_Id;
5320 -- The entity of the formal for the procedure
5322 --------------------
5323 -- Add_Invariants --
5324 --------------------
5326 procedure Add_Invariants (T : Entity_Id; Inherit : Boolean) is
5336 procedure Replace_Type_Reference (N : Node_Id);
5337 -- Replace a single occurrence N of the subtype name with a reference
5338 -- to the formal of the predicate function. N can be an identifier
5339 -- referencing the subtype, or a selected component, representing an
5340 -- appropriately qualified occurrence of the subtype name.
5342 procedure Replace_Type_References is
5343 new Replace_Type_References_Generic (Replace_Type_Reference);
5344 -- Traverse an expression replacing all occurrences of the subtype
5345 -- name with appropriate references to the object that is the formal
5346 -- parameter of the predicate function. Note that we must ensure
5347 -- that the type and entity information is properly set in the
5348 -- replacement node, since we will do a Preanalyze call of this
5349 -- expression without proper visibility of the procedure argument.
5351 ----------------------------
5352 -- Replace_Type_Reference --
5353 ----------------------------
5355 -- Note: See comments in Add_Predicates.Replace_Type_Reference
5356 -- regarding handling of Sloc and Comes_From_Source.
5358 procedure Replace_Type_Reference (N : Node_Id) is
5360 -- Invariant'Class, replace with T'Class (obj)
5362 if Class_Present (Ritem) then
5364 Make_Type_Conversion (Sloc (N),
5366 Make_Attribute_Reference (Sloc (N),
5367 Prefix => New_Occurrence_Of (T, Sloc (N)),
5368 Attribute_Name => Name_Class),
5369 Expression => Make_Identifier (Sloc (N), Object_Name)));
5371 Set_Entity (Expression (N), Object_Entity);
5372 Set_Etype (Expression (N), Typ);
5374 -- Invariant, replace with obj
5377 Rewrite (N, Make_Identifier (Sloc (N), Object_Name));
5378 Set_Entity (N, Object_Entity);
5382 Set_Comes_From_Source (N, True);
5383 end Replace_Type_Reference;
5385 -- Start of processing for Add_Invariants
5388 Ritem := First_Rep_Item (T);
5389 while Present (Ritem) loop
5390 if Nkind (Ritem) = N_Pragma
5391 and then Pragma_Name (Ritem) = Name_Invariant
5393 Arg1 := First (Pragma_Argument_Associations (Ritem));
5394 Arg2 := Next (Arg1);
5395 Arg3 := Next (Arg2);
5397 Arg1 := Get_Pragma_Arg (Arg1);
5398 Arg2 := Get_Pragma_Arg (Arg2);
5400 -- For Inherit case, ignore Invariant, process only Class case
5403 if not Class_Present (Ritem) then
5407 -- For Inherit false, process only item for right type
5410 if Entity (Arg1) /= Typ then
5416 Stmts := Empty_List;
5419 Exp := New_Copy_Tree (Arg2);
5421 -- Preserve sloc of original pragma Invariant
5423 Loc := Sloc (Ritem);
5425 -- We need to replace any occurrences of the name of the type
5426 -- with references to the object, converted to type'Class in
5427 -- the case of Invariant'Class aspects.
5429 Replace_Type_References (Exp, Chars (T));
5431 -- If this invariant comes from an aspect, find the aspect
5432 -- specification, and replace the saved expression because
5433 -- we need the subtype references replaced for the calls to
5434 -- Preanalyze_Spec_Expressin in Check_Aspect_At_Freeze_Point
5435 -- and Check_Aspect_At_End_Of_Declarations.
5437 if From_Aspect_Specification (Ritem) then
5442 -- Loop to find corresponding aspect, note that this
5443 -- must be present given the pragma is marked delayed.
5445 Aitem := Next_Rep_Item (Ritem);
5446 while Present (Aitem) loop
5447 if Nkind (Aitem) = N_Aspect_Specification
5448 and then Aspect_Rep_Item (Aitem) = Ritem
5451 (Identifier (Aitem), New_Copy_Tree (Exp));
5455 Aitem := Next_Rep_Item (Aitem);
5460 -- Now we need to preanalyze the expression to properly capture
5461 -- the visibility in the visible part. The expression will not
5462 -- be analyzed for real until the body is analyzed, but that is
5463 -- at the end of the private part and has the wrong visibility.
5465 Set_Parent (Exp, N);
5466 Preanalyze_Assert_Expression (Exp, Standard_Boolean);
5468 -- Build first two arguments for Check pragma
5471 Make_Pragma_Argument_Association (Loc,
5472 Expression => Make_Identifier (Loc, Name_Invariant)),
5473 Make_Pragma_Argument_Association (Loc,
5474 Expression => Exp));
5476 -- Add message if present in Invariant pragma
5478 if Present (Arg3) then
5479 Str := Strval (Get_Pragma_Arg (Arg3));
5481 -- If inherited case, and message starts "failed invariant",
5482 -- change it to be "failed inherited invariant".
5485 String_To_Name_Buffer (Str);
5487 if Name_Buffer (1 .. 16) = "failed invariant" then
5488 Insert_Str_In_Name_Buffer ("inherited ", 8);
5489 Str := String_From_Name_Buffer;
5494 Make_Pragma_Argument_Association (Loc,
5495 Expression => Make_String_Literal (Loc, Str)));
5498 -- Add Check pragma to list of statements
5502 Pragma_Identifier =>
5503 Make_Identifier (Loc, Name_Check),
5504 Pragma_Argument_Associations => Assoc));
5506 -- If Inherited case and option enabled, output info msg. Note
5507 -- that we know this is a case of Invariant'Class.
5509 if Inherit and Opt.List_Inherited_Aspects then
5510 Error_Msg_Sloc := Sloc (Ritem);
5512 ("?L?info: & inherits `Invariant''Class` aspect from #",
5518 Next_Rep_Item (Ritem);
5522 -- Start of processing for Build_Invariant_Procedure
5530 -- If the aspect specification exists for some view of the type, the
5531 -- declaration for the procedure has been created.
5533 if Has_Invariants (Typ) then
5534 SId := Invariant_Procedure (Typ);
5537 if Present (SId) then
5538 PDecl := Unit_Declaration_Node (SId);
5541 PDecl := Build_Invariant_Procedure_Declaration (Typ);
5544 -- Recover formal of procedure, for use in the calls to invariant
5545 -- functions (including inherited ones).
5549 (First (Parameter_Specifications (Specification (PDecl))));
5550 Object_Name := Chars (Object_Entity);
5552 -- Add invariants for the current type
5554 Add_Invariants (Typ, Inherit => False);
5556 -- Add invariants for parent types
5559 Current_Typ : Entity_Id;
5560 Parent_Typ : Entity_Id;
5565 Parent_Typ := Etype (Current_Typ);
5567 if Is_Private_Type (Parent_Typ)
5568 and then Present (Full_View (Base_Type (Parent_Typ)))
5570 Parent_Typ := Full_View (Base_Type (Parent_Typ));
5573 exit when Parent_Typ = Current_Typ;
5575 Current_Typ := Parent_Typ;
5576 Add_Invariants (Current_Typ, Inherit => True);
5580 -- Build the procedure if we generated at least one Check pragma
5582 if Stmts /= No_List then
5583 Spec := Copy_Separate_Tree (Specification (PDecl));
5586 Make_Subprogram_Body (Loc,
5587 Specification => Spec,
5588 Declarations => Empty_List,
5589 Handled_Statement_Sequence =>
5590 Make_Handled_Sequence_Of_Statements (Loc,
5591 Statements => Stmts));
5593 -- Insert procedure declaration and spec at the appropriate points.
5594 -- If declaration is already analyzed, it was processed by the
5595 -- generated pragma.
5597 if Present (Private_Decls) then
5599 -- The spec goes at the end of visible declarations, but they have
5600 -- already been analyzed, so we need to explicitly do the analyze.
5602 if not Analyzed (PDecl) then
5603 Append_To (Visible_Decls, PDecl);
5607 -- The body goes at the end of the private declarations, which we
5608 -- have not analyzed yet, so we do not need to perform an explicit
5609 -- analyze call. We skip this if there are no private declarations
5610 -- (this is an error that will be caught elsewhere);
5612 Append_To (Private_Decls, PBody);
5614 -- If the invariant appears on the full view of a type, the
5615 -- analysis of the private part is complete, and we must
5616 -- analyze the new body explicitly.
5618 if In_Private_Part (Current_Scope) then
5622 -- If there are no private declarations this may be an error that
5623 -- will be diagnosed elsewhere. However, if this is a non-private
5624 -- type that inherits invariants, it needs no completion and there
5625 -- may be no private part. In this case insert invariant procedure
5626 -- at end of current declarative list, and analyze at once, given
5627 -- that the type is about to be frozen.
5629 elsif not Is_Private_Type (Typ) then
5630 Append_To (Visible_Decls, PDecl);
5631 Append_To (Visible_Decls, PBody);
5636 end Build_Invariant_Procedure;
5638 -------------------------------
5639 -- Build_Predicate_Functions --
5640 -------------------------------
5642 -- The procedures that are constructed here has the form:
5644 -- function typPredicate (Ixxx : typ) return Boolean is
5647 -- exp1 and then exp2 and then ...
5648 -- and then typ1Predicate (typ1 (Ixxx))
5649 -- and then typ2Predicate (typ2 (Ixxx))
5651 -- end typPredicate;
5653 -- Here exp1, and exp2 are expressions from Predicate pragmas. Note that
5654 -- this is the point at which these expressions get analyzed, providing the
5655 -- required delay, and typ1, typ2, are entities from which predicates are
5656 -- inherited. Note that we do NOT generate Check pragmas, that's because we
5657 -- use this function even if checks are off, e.g. for membership tests.
5659 -- If the expression has at least one Raise_Expression, then we also build
5660 -- the typPredicateM version of the function, in which any occurence of a
5661 -- Raise_Expressioon is converted to "return False".
5663 procedure Build_Predicate_Functions (Typ : Entity_Id; N : Node_Id) is
5664 Loc : constant Source_Ptr := Sloc (Typ);
5667 -- This is the expression for the result of the function. It is
5668 -- is build by connecting the component predicates with AND THEN.
5671 -- This is the corresponding return expression for the Predicate_M
5672 -- function. It differs in that raise expressions are marked for
5673 -- special expansion (see Process_REs).
5675 Object_Name : constant Name_Id := New_Internal_Name ('I');
5676 -- Name for argument of Predicate procedure. Note that we use the same
5677 -- name for both predicate procedure. That way the reference within the
5678 -- predicate expression is the same in both functions.
5680 Object_Entity : constant Entity_Id :=
5681 Make_Defining_Identifier (Loc, Chars => Object_Name);
5682 -- Entity for argument of Predicate procedure
5684 Object_Entity_M : constant Entity_Id :=
5685 Make_Defining_Identifier (Loc, Chars => Object_Name);
5686 -- Entity for argument of Predicate_M procedure
5688 Raise_Expression_Present : Boolean := False;
5689 -- Set True if Expr has at least one Raise_Expression
5691 procedure Add_Call (T : Entity_Id);
5692 -- Includes a call to the predicate function for type T in Expr if T
5693 -- has predicates and Predicate_Function (T) is non-empty.
5695 procedure Add_Predicates;
5696 -- Appends expressions for any Predicate pragmas in the rep item chain
5697 -- Typ to Expr. Note that we look only at items for this exact entity.
5698 -- Inheritance of predicates for the parent type is done by calling the
5699 -- Predicate_Function of the parent type, using Add_Call above.
5701 function Test_RE (N : Node_Id) return Traverse_Result;
5702 -- Used in Test_REs, tests one node for being a raise expression, and if
5703 -- so sets Raise_Expression_Present True.
5705 procedure Test_REs is new Traverse_Proc (Test_RE);
5706 -- Tests to see if Expr contains any raise expressions
5708 function Process_RE (N : Node_Id) return Traverse_Result;
5709 -- Used in Process REs, tests if node N is a raise expression, and if
5710 -- so, marks it to be converted to return False.
5712 procedure Process_REs is new Traverse_Proc (Process_RE);
5713 -- Marks any raise expressions in Expr_M to return False
5715 Dynamic_Predicate_Present : Boolean := False;
5716 -- Set True if a dynamic predicate is present, results in the entire
5717 -- predicate being considered dynamic even if it looks static
5719 Static_Predicate_Present : Node_Id := Empty;
5720 -- Set to N_Pragma node for a static predicate if one is encountered.
5726 procedure Add_Call (T : Entity_Id) is
5730 if Present (T) and then Present (Predicate_Function (T)) then
5731 Set_Has_Predicates (Typ);
5733 -- Build the call to the predicate function of T
5737 (T, Convert_To (T, Make_Identifier (Loc, Object_Name)));
5739 -- Add call to evolving expression, using AND THEN if needed
5746 Left_Opnd => Relocate_Node (Expr),
5750 -- Output info message on inheritance if required. Note we do not
5751 -- give this information for generic actual types, since it is
5752 -- unwelcome noise in that case in instantiations. We also
5753 -- generally suppress the message in instantiations, and also
5754 -- if it involves internal names.
5756 if Opt.List_Inherited_Aspects
5757 and then not Is_Generic_Actual_Type (Typ)
5758 and then Instantiation_Depth (Sloc (Typ)) = 0
5759 and then not Is_Internal_Name (Chars (T))
5760 and then not Is_Internal_Name (Chars (Typ))
5762 Error_Msg_Sloc := Sloc (Predicate_Function (T));
5763 Error_Msg_Node_2 := T;
5764 Error_Msg_N ("info: & inherits predicate from & #?L?", Typ);
5769 --------------------
5770 -- Add_Predicates --
5771 --------------------
5773 procedure Add_Predicates is
5778 procedure Replace_Type_Reference (N : Node_Id);
5779 -- Replace a single occurrence N of the subtype name with a reference
5780 -- to the formal of the predicate function. N can be an identifier
5781 -- referencing the subtype, or a selected component, representing an
5782 -- appropriately qualified occurrence of the subtype name.
5784 procedure Replace_Type_References is
5785 new Replace_Type_References_Generic (Replace_Type_Reference);
5786 -- Traverse an expression changing every occurrence of an identifier
5787 -- whose name matches the name of the subtype with a reference to
5788 -- the formal parameter of the predicate function.
5790 ----------------------------
5791 -- Replace_Type_Reference --
5792 ----------------------------
5794 procedure Replace_Type_Reference (N : Node_Id) is
5796 Rewrite (N, Make_Identifier (Sloc (N), Object_Name));
5797 -- Use the Sloc of the usage name, not the defining name
5800 Set_Entity (N, Object_Entity);
5802 -- We want to treat the node as if it comes from source, so that
5803 -- ASIS will not ignore it
5805 Set_Comes_From_Source (N, True);
5806 end Replace_Type_Reference;
5808 -- Start of processing for Add_Predicates
5811 Ritem := First_Rep_Item (Typ);
5812 while Present (Ritem) loop
5813 if Nkind (Ritem) = N_Pragma
5814 and then Pragma_Name (Ritem) = Name_Predicate
5816 if Present (Corresponding_Aspect (Ritem)) then
5817 case Chars (Identifier (Corresponding_Aspect (Ritem))) is
5818 when Name_Dynamic_Predicate =>
5819 Dynamic_Predicate_Present := True;
5820 when Name_Static_Predicate =>
5821 Static_Predicate_Present := Ritem;
5827 -- Acquire arguments
5829 Arg1 := First (Pragma_Argument_Associations (Ritem));
5830 Arg2 := Next (Arg1);
5832 Arg1 := Get_Pragma_Arg (Arg1);
5833 Arg2 := Get_Pragma_Arg (Arg2);
5835 -- See if this predicate pragma is for the current type or for
5836 -- its full view. A predicate on a private completion is placed
5837 -- on the partial view beause this is the visible entity that
5840 if Entity (Arg1) = Typ
5841 or else Full_View (Entity (Arg1)) = Typ
5843 -- We have a match, this entry is for our subtype
5845 -- We need to replace any occurrences of the name of the
5846 -- type with references to the object.
5848 Replace_Type_References (Arg2, Chars (Typ));
5850 -- If this predicate comes from an aspect, find the aspect
5851 -- specification, and replace the saved expression because
5852 -- we need the subtype references replaced for the calls to
5853 -- Preanalyze_Spec_Expressin in Check_Aspect_At_Freeze_Point
5854 -- and Check_Aspect_At_End_Of_Declarations.
5856 if From_Aspect_Specification (Ritem) then
5861 -- Loop to find corresponding aspect, note that this
5862 -- must be present given the pragma is marked delayed.
5864 Aitem := Next_Rep_Item (Ritem);
5866 if Nkind (Aitem) = N_Aspect_Specification
5867 and then Aspect_Rep_Item (Aitem) = Ritem
5870 (Identifier (Aitem), New_Copy_Tree (Arg2));
5874 Aitem := Next_Rep_Item (Aitem);
5879 -- Now we can add the expression
5882 Expr := Relocate_Node (Arg2);
5884 -- There already was a predicate, so add to it
5889 Left_Opnd => Relocate_Node (Expr),
5890 Right_Opnd => Relocate_Node (Arg2));
5895 Next_Rep_Item (Ritem);
5903 function Process_RE (N : Node_Id) return Traverse_Result is
5905 if Nkind (N) = N_Raise_Expression then
5906 Set_Convert_To_Return_False (N);
5917 function Test_RE (N : Node_Id) return Traverse_Result is
5919 if Nkind (N) = N_Raise_Expression then
5920 Raise_Expression_Present := True;
5927 -- Start of processing for Build_Predicate_Functions
5930 -- Return if already built or if type does not have predicates
5932 if not Has_Predicates (Typ)
5933 or else Present (Predicate_Function (Typ))
5938 -- Prepare to construct predicate expression
5942 -- Add Predicates for the current type
5946 -- Add predicates for ancestor if present
5949 Atyp : constant Entity_Id := Nearest_Ancestor (Typ);
5951 if Present (Atyp) then
5956 -- Case where predicates are present
5958 if Present (Expr) then
5960 -- Test for raise expression present
5964 -- If raise expression is present, capture a copy of Expr for use
5965 -- in building the predicateM function version later on. For this
5966 -- copy we replace references to Object_Entity by Object_Entity_M.
5968 if Raise_Expression_Present then
5970 Map : constant Elist_Id := New_Elmt_List;
5972 Append_Elmt (Object_Entity, Map);
5973 Append_Elmt (Object_Entity_M, Map);
5974 Expr_M := New_Copy_Tree (Expr, Map => Map);
5978 -- Build the main predicate function
5981 SId : constant Entity_Id :=
5982 Make_Defining_Identifier (Loc,
5983 Chars => New_External_Name (Chars (Typ), "Predicate"));
5984 -- The entity for the the function spec
5986 SIdB : constant Entity_Id :=
5987 Make_Defining_Identifier (Loc,
5988 Chars => New_External_Name (Chars (Typ), "Predicate"));
5989 -- The entity for the function body
5996 -- Build function declaration
5998 Set_Ekind (SId, E_Function);
5999 Set_Is_Predicate_Function (SId);
6000 Set_Predicate_Function (Typ, SId);
6002 -- The predicate function is shared between views of a type
6004 if Is_Private_Type (Typ) and then Present (Full_View (Typ)) then
6005 Set_Predicate_Function (Full_View (Typ), SId);
6009 Make_Function_Specification (Loc,
6010 Defining_Unit_Name => SId,
6011 Parameter_Specifications => New_List (
6012 Make_Parameter_Specification (Loc,
6013 Defining_Identifier => Object_Entity,
6014 Parameter_Type => New_Occurrence_Of (Typ, Loc))),
6015 Result_Definition =>
6016 New_Occurrence_Of (Standard_Boolean, Loc));
6019 Make_Subprogram_Declaration (Loc,
6020 Specification => Spec);
6022 -- Build function body
6025 Make_Function_Specification (Loc,
6026 Defining_Unit_Name => SIdB,
6027 Parameter_Specifications => New_List (
6028 Make_Parameter_Specification (Loc,
6029 Defining_Identifier =>
6030 Make_Defining_Identifier (Loc, Object_Name),
6032 New_Occurrence_Of (Typ, Loc))),
6033 Result_Definition =>
6034 New_Occurrence_Of (Standard_Boolean, Loc));
6037 Make_Subprogram_Body (Loc,
6038 Specification => Spec,
6039 Declarations => Empty_List,
6040 Handled_Statement_Sequence =>
6041 Make_Handled_Sequence_Of_Statements (Loc,
6042 Statements => New_List (
6043 Make_Simple_Return_Statement (Loc,
6044 Expression => Expr))));
6046 -- Insert declaration before freeze node and body after
6048 Insert_Before_And_Analyze (N, FDecl);
6049 Insert_After_And_Analyze (N, FBody);
6052 -- Test for raise expressions present and if so build M version
6054 if Raise_Expression_Present then
6056 SId : constant Entity_Id :=
6057 Make_Defining_Identifier (Loc,
6058 Chars => New_External_Name (Chars (Typ), "PredicateM"));
6059 -- The entity for the the function spec
6061 SIdB : constant Entity_Id :=
6062 Make_Defining_Identifier (Loc,
6063 Chars => New_External_Name (Chars (Typ), "PredicateM"));
6064 -- The entity for the function body
6072 -- Mark any raise expressions for special expansion
6074 Process_REs (Expr_M);
6076 -- Build function declaration
6078 Set_Ekind (SId, E_Function);
6079 Set_Is_Predicate_Function_M (SId);
6080 Set_Predicate_Function_M (Typ, SId);
6082 -- The predicate function is shared between views of a type
6084 if Is_Private_Type (Typ) and then Present (Full_View (Typ)) then
6085 Set_Predicate_Function_M (Full_View (Typ), SId);
6089 Make_Function_Specification (Loc,
6090 Defining_Unit_Name => SId,
6091 Parameter_Specifications => New_List (
6092 Make_Parameter_Specification (Loc,
6093 Defining_Identifier => Object_Entity_M,
6094 Parameter_Type => New_Occurrence_Of (Typ, Loc))),
6095 Result_Definition =>
6096 New_Occurrence_Of (Standard_Boolean, Loc));
6099 Make_Subprogram_Declaration (Loc,
6100 Specification => Spec);
6102 -- Build function body
6105 Make_Function_Specification (Loc,
6106 Defining_Unit_Name => SIdB,
6107 Parameter_Specifications => New_List (
6108 Make_Parameter_Specification (Loc,
6109 Defining_Identifier =>
6110 Make_Defining_Identifier (Loc, Object_Name),
6112 New_Occurrence_Of (Typ, Loc))),
6113 Result_Definition =>
6114 New_Occurrence_Of (Standard_Boolean, Loc));
6116 -- Build the body, we declare the boolean expression before
6117 -- doing the return, because we are not really confident of
6118 -- what happens if a return appears within a return!
6121 Make_Defining_Identifier (Loc,
6122 Chars => New_Internal_Name ('B'));
6125 Make_Subprogram_Body (Loc,
6126 Specification => Spec,
6128 Declarations => New_List (
6129 Make_Object_Declaration (Loc,
6130 Defining_Identifier => BTemp,
6131 Constant_Present => True,
6132 Object_Definition =>
6133 New_Reference_To (Standard_Boolean, Loc),
6134 Expression => Expr_M)),
6136 Handled_Statement_Sequence =>
6137 Make_Handled_Sequence_Of_Statements (Loc,
6138 Statements => New_List (
6139 Make_Simple_Return_Statement (Loc,
6140 Expression => New_Reference_To (BTemp, Loc)))));
6142 -- Insert declaration before freeze node and body after
6144 Insert_Before_And_Analyze (N, FDecl);
6145 Insert_After_And_Analyze (N, FBody);
6149 -- Deal with static predicate case
6151 if Ekind_In (Typ, E_Enumeration_Subtype,
6152 E_Modular_Integer_Subtype,
6153 E_Signed_Integer_Subtype)
6154 and then Is_Static_Subtype (Typ)
6155 and then not Dynamic_Predicate_Present
6157 Build_Static_Predicate (Typ, Expr, Object_Name);
6159 if Present (Static_Predicate_Present)
6160 and No (Static_Predicate (Typ))
6163 ("expression does not have required form for "
6164 & "static predicate",
6165 Next (First (Pragma_Argument_Associations
6166 (Static_Predicate_Present))));
6170 end Build_Predicate_Functions;
6172 ----------------------------
6173 -- Build_Static_Predicate --
6174 ----------------------------
6176 procedure Build_Static_Predicate
6181 Loc : constant Source_Ptr := Sloc (Expr);
6183 Non_Static : exception;
6184 -- Raised if something non-static is found
6186 Btyp : constant Entity_Id := Base_Type (Typ);
6188 BLo : constant Uint := Expr_Value (Type_Low_Bound (Btyp));
6189 BHi : constant Uint := Expr_Value (Type_High_Bound (Btyp));
6190 -- Low bound and high bound value of base type of Typ
6192 TLo : constant Uint := Expr_Value (Type_Low_Bound (Typ));
6193 THi : constant Uint := Expr_Value (Type_High_Bound (Typ));
6194 -- Low bound and high bound values of static subtype Typ
6199 -- One entry in a Rlist value, a single REnt (range entry) value denotes
6200 -- one range from Lo to Hi. To represent a single value range Lo = Hi =
6203 type RList is array (Nat range <>) of REnt;
6204 -- A list of ranges. The ranges are sorted in increasing order, and are
6205 -- disjoint (there is a gap of at least one value between each range in
6206 -- the table). A value is in the set of ranges in Rlist if it lies
6207 -- within one of these ranges.
6209 False_Range : constant RList :=
6210 RList'(1 .. 0 => REnt'(No_Uint, No_Uint));
6211 -- An empty set of ranges represents a range list that can never be
6212 -- satisfied, since there are no ranges in which the value could lie,
6213 -- so it does not lie in any of them. False_Range is a canonical value
6214 -- for this empty set, but general processing should test for an Rlist
6215 -- with length zero (see Is_False predicate), since other null ranges
6216 -- may appear which must be treated as False.
6218 True_Range : constant RList := RList'(1 => REnt'(BLo, BHi));
6219 -- Range representing True, value must be in the base range
6221 function "and" (Left : RList; Right : RList) return RList;
6222 -- And's together two range lists, returning a range list. This is a set
6223 -- intersection operation.
6225 function "or" (Left : RList; Right : RList) return RList;
6226 -- Or's together two range lists, returning a range list. This is a set
6229 function "not" (Right : RList) return RList;
6230 -- Returns complement of a given range list, i.e. a range list
6231 -- representing all the values in TLo .. THi that are not in the input
6234 function Build_Val (V : Uint) return Node_Id;
6235 -- Return an analyzed N_Identifier node referencing this value, suitable
6236 -- for use as an entry in the Static_Predicate list. This node is typed
6237 -- with the base type.
6239 function Build_Range (Lo : Uint; Hi : Uint) return Node_Id;
6240 -- Return an analyzed N_Range node referencing this range, suitable for
6241 -- use as an entry in the Static_Predicate list. This node is typed with
6244 function Get_RList (Exp : Node_Id) return RList;
6245 -- This is a recursive routine that converts the given expression into a
6246 -- list of ranges, suitable for use in building the static predicate.
6248 function Is_False (R : RList) return Boolean;
6249 pragma Inline (Is_False);
6250 -- Returns True if the given range list is empty, and thus represents a
6251 -- False list of ranges that can never be satisfied.
6253 function Is_True (R : RList) return Boolean;
6254 -- Returns True if R trivially represents the True predicate by having a
6255 -- single range from BLo to BHi.
6257 function Is_Type_Ref (N : Node_Id) return Boolean;
6258 pragma Inline (Is_Type_Ref);
6259 -- Returns if True if N is a reference to the type for the predicate in
6260 -- the expression (i.e. if it is an identifier whose Chars field matches
6261 -- the Nam given in the call).
6263 function Lo_Val (N : Node_Id) return Uint;
6264 -- Given static expression or static range from a Static_Predicate list,
6265 -- gets expression value or low bound of range.
6267 function Hi_Val (N : Node_Id) return Uint;
6268 -- Given static expression or static range from a Static_Predicate list,
6269 -- gets expression value of high bound of range.
6271 function Membership_Entry (N : Node_Id) return RList;
6272 -- Given a single membership entry (range, value, or subtype), returns
6273 -- the corresponding range list. Raises Static_Error if not static.
6275 function Membership_Entries (N : Node_Id) return RList;
6276 -- Given an element on an alternatives list of a membership operation,
6277 -- returns the range list corresponding to this entry and all following
6278 -- entries (i.e. returns the "or" of this list of values).
6280 function Stat_Pred (Typ : Entity_Id) return RList;
6281 -- Given a type, if it has a static predicate, then return the predicate
6282 -- as a range list, otherwise raise Non_Static.
6288 function "and" (Left : RList; Right : RList) return RList is
6290 -- First range of result
6292 SLeft : Nat := Left'First;
6293 -- Start of rest of left entries
6295 SRight : Nat := Right'First;
6296 -- Start of rest of right entries
6299 -- If either range is True, return the other
6301 if Is_True (Left) then
6303 elsif Is_True (Right) then
6307 -- If either range is False, return False
6309 if Is_False (Left) or else Is_False (Right) then
6313 -- Loop to remove entries at start that are disjoint, and thus just
6314 -- get discarded from the result entirely.
6317 -- If no operands left in either operand, result is false
6319 if SLeft > Left'Last or else SRight > Right'Last then
6322 -- Discard first left operand entry if disjoint with right
6324 elsif Left (SLeft).Hi < Right (SRight).Lo then
6327 -- Discard first right operand entry if disjoint with left
6329 elsif Right (SRight).Hi < Left (SLeft).Lo then
6330 SRight := SRight + 1;
6332 -- Otherwise we have an overlapping entry
6339 -- Now we have two non-null operands, and first entries overlap. The
6340 -- first entry in the result will be the overlapping part of these
6343 FEnt := REnt'(Lo => UI_Max (Left (SLeft).Lo, Right (SRight).Lo),
6344 Hi => UI_Min (Left (SLeft).Hi, Right (SRight).Hi));
6346 -- Now we can remove the entry that ended at a lower value, since its
6347 -- contribution is entirely contained in Fent.
6349 if Left (SLeft).Hi <= Right (SRight).Hi then
6352 SRight := SRight + 1;
6355 -- Compute result by concatenating this first entry with the "and" of
6356 -- the remaining parts of the left and right operands. Note that if
6357 -- either of these is empty, "and" will yield empty, so that we will
6358 -- end up with just Fent, which is what we want in that case.
6361 FEnt & (Left (SLeft .. Left'Last) and Right (SRight .. Right'Last));
6368 function "not" (Right : RList) return RList is
6370 -- Return True if False range
6372 if Is_False (Right) then
6376 -- Return False if True range
6378 if Is_True (Right) then
6382 -- Here if not trivial case
6385 Result : RList (1 .. Right'Length + 1);
6386 -- May need one more entry for gap at beginning and end
6389 -- Number of entries stored in Result
6394 if Right (Right'First).Lo > TLo then
6396 Result (Count) := REnt'(TLo, Right (Right'First).Lo - 1);
6399 -- Gaps between ranges
6401 for J in Right'First .. Right'Last - 1 loop
6404 REnt'(Right (J).Hi + 1, Right (J + 1).Lo - 1);
6409 if Right (Right'Last).Hi < THi then
6411 Result (Count) := REnt'(Right (Right'Last).Hi + 1, THi);
6414 return Result (1 .. Count);
6422 function "or" (Left : RList; Right : RList) return RList is
6424 -- First range of result
6426 SLeft : Nat := Left'First;
6427 -- Start of rest of left entries
6429 SRight : Nat := Right'First;
6430 -- Start of rest of right entries
6433 -- If either range is True, return True
6435 if Is_True (Left) or else Is_True (Right) then
6439 -- If either range is False (empty), return the other
6441 if Is_False (Left) then
6443 elsif Is_False (Right) then
6447 -- Initialize result first entry from left or right operand depending
6448 -- on which starts with the lower range.
6450 if Left (SLeft).Lo < Right (SRight).Lo then
6451 FEnt := Left (SLeft);
6454 FEnt := Right (SRight);
6455 SRight := SRight + 1;
6458 -- This loop eats ranges from left and right operands that are
6459 -- contiguous with the first range we are gathering.
6462 -- Eat first entry in left operand if contiguous or overlapped by
6463 -- gathered first operand of result.
6465 if SLeft <= Left'Last
6466 and then Left (SLeft).Lo <= FEnt.Hi + 1
6468 FEnt.Hi := UI_Max (FEnt.Hi, Left (SLeft).Hi);
6471 -- Eat first entry in right operand if contiguous or overlapped by
6472 -- gathered right operand of result.
6474 elsif SRight <= Right'Last
6475 and then Right (SRight).Lo <= FEnt.Hi + 1
6477 FEnt.Hi := UI_Max (FEnt.Hi, Right (SRight).Hi);
6478 SRight := SRight + 1;
6480 -- All done if no more entries to eat
6487 -- Obtain result as the first entry we just computed, concatenated
6488 -- to the "or" of the remaining results (if one operand is empty,
6489 -- this will just concatenate with the other
6492 FEnt & (Left (SLeft .. Left'Last) or Right (SRight .. Right'Last));
6499 function Build_Range (Lo : Uint; Hi : Uint) return Node_Id is
6505 Low_Bound => Build_Val (Lo),
6506 High_Bound => Build_Val (Hi));
6507 Set_Etype (Result, Btyp);
6508 Set_Analyzed (Result);
6517 function Build_Val (V : Uint) return Node_Id is
6521 if Is_Enumeration_Type (Typ) then
6522 Result := Get_Enum_Lit_From_Pos (Typ, V, Loc);
6524 Result := Make_Integer_Literal (Loc, V);
6527 Set_Etype (Result, Btyp);
6528 Set_Is_Static_Expression (Result);
6529 Set_Analyzed (Result);
6537 function Get_RList (Exp : Node_Id) return RList is
6542 -- Static expression can only be true or false
6544 if Is_OK_Static_Expression (Exp) then
6548 if Expr_Value (Exp) = 0 then
6555 -- Otherwise test node type
6563 when N_Op_And | N_And_Then =>
6564 return Get_RList (Left_Opnd (Exp))
6566 Get_RList (Right_Opnd (Exp));
6570 when N_Op_Or | N_Or_Else =>
6571 return Get_RList (Left_Opnd (Exp))
6573 Get_RList (Right_Opnd (Exp));
6578 return not Get_RList (Right_Opnd (Exp));
6580 -- Comparisons of type with static value
6582 when N_Op_Compare =>
6584 -- Type is left operand
6586 if Is_Type_Ref (Left_Opnd (Exp))
6587 and then Is_OK_Static_Expression (Right_Opnd (Exp))
6589 Val := Expr_Value (Right_Opnd (Exp));
6591 -- Typ is right operand
6593 elsif Is_Type_Ref (Right_Opnd (Exp))
6594 and then Is_OK_Static_Expression (Left_Opnd (Exp))
6596 Val := Expr_Value (Left_Opnd (Exp));
6598 -- Invert sense of comparison
6601 when N_Op_Gt => Op := N_Op_Lt;
6602 when N_Op_Lt => Op := N_Op_Gt;
6603 when N_Op_Ge => Op := N_Op_Le;
6604 when N_Op_Le => Op := N_Op_Ge;
6605 when others => null;
6608 -- Other cases are non-static
6614 -- Construct range according to comparison operation
6618 return RList'(1 => REnt'(Val, Val));
6621 return RList'(1 => REnt'(Val, BHi));
6624 return RList'(1 => REnt'(Val + 1, BHi));
6627 return RList'(1 => REnt'(BLo, Val));
6630 return RList'(1 => REnt'(BLo, Val - 1));
6633 return RList'(REnt'(BLo, Val - 1),
6634 REnt'(Val + 1, BHi));
6637 raise Program_Error;
6643 if not Is_Type_Ref (Left_Opnd (Exp)) then
6647 if Present (Right_Opnd (Exp)) then
6648 return Membership_Entry (Right_Opnd (Exp));
6650 return Membership_Entries (First (Alternatives (Exp)));
6653 -- Negative membership (NOT IN)
6656 if not Is_Type_Ref (Left_Opnd (Exp)) then
6660 if Present (Right_Opnd (Exp)) then
6661 return not Membership_Entry (Right_Opnd (Exp));
6663 return not Membership_Entries (First (Alternatives (Exp)));
6666 -- Function call, may be call to static predicate
6668 when N_Function_Call =>
6669 if Is_Entity_Name (Name (Exp)) then
6671 Ent : constant Entity_Id := Entity (Name (Exp));
6673 if Is_Predicate_Function (Ent)
6675 Is_Predicate_Function_M (Ent)
6677 return Stat_Pred (Etype (First_Formal (Ent)));
6682 -- Other function call cases are non-static
6686 -- Qualified expression, dig out the expression
6688 when N_Qualified_Expression =>
6689 return Get_RList (Expression (Exp));
6694 return (Get_RList (Left_Opnd (Exp))
6695 and not Get_RList (Right_Opnd (Exp)))
6696 or (Get_RList (Right_Opnd (Exp))
6697 and not Get_RList (Left_Opnd (Exp)));
6699 -- Any other node type is non-static
6710 function Hi_Val (N : Node_Id) return Uint is
6712 if Is_Static_Expression (N) then
6713 return Expr_Value (N);
6715 pragma Assert (Nkind (N) = N_Range);
6716 return Expr_Value (High_Bound (N));
6724 function Is_False (R : RList) return Boolean is
6726 return R'Length = 0;
6733 function Is_True (R : RList) return Boolean is
6736 and then R (R'First).Lo = BLo
6737 and then R (R'First).Hi = BHi;
6744 function Is_Type_Ref (N : Node_Id) return Boolean is
6746 return Nkind (N) = N_Identifier and then Chars (N) = Nam;
6753 function Lo_Val (N : Node_Id) return Uint is
6755 if Is_Static_Expression (N) then
6756 return Expr_Value (N);
6758 pragma Assert (Nkind (N) = N_Range);
6759 return Expr_Value (Low_Bound (N));
6763 ------------------------
6764 -- Membership_Entries --
6765 ------------------------
6767 function Membership_Entries (N : Node_Id) return RList is
6769 if No (Next (N)) then
6770 return Membership_Entry (N);
6772 return Membership_Entry (N) or Membership_Entries (Next (N));
6774 end Membership_Entries;
6776 ----------------------
6777 -- Membership_Entry --
6778 ----------------------
6780 function Membership_Entry (N : Node_Id) return RList is
6788 if Nkind (N) = N_Range then
6789 if not Is_Static_Expression (Low_Bound (N))
6791 not Is_Static_Expression (High_Bound (N))
6795 SLo := Expr_Value (Low_Bound (N));
6796 SHi := Expr_Value (High_Bound (N));
6797 return RList'(1 => REnt'(SLo, SHi));
6800 -- Static expression case
6802 elsif Is_Static_Expression (N) then
6803 Val := Expr_Value (N);
6804 return RList'(1 => REnt'(Val, Val));
6806 -- Identifier (other than static expression) case
6808 else pragma Assert (Nkind (N) = N_Identifier);
6812 if Is_Type (Entity (N)) then
6814 -- If type has predicates, process them
6816 if Has_Predicates (Entity (N)) then
6817 return Stat_Pred (Entity (N));
6819 -- For static subtype without predicates, get range
6821 elsif Is_Static_Subtype (Entity (N)) then
6822 SLo := Expr_Value (Type_Low_Bound (Entity (N)));
6823 SHi := Expr_Value (Type_High_Bound (Entity (N)));
6824 return RList'(1 => REnt'(SLo, SHi));
6826 -- Any other type makes us non-static
6832 -- Any other kind of identifier in predicate (e.g. a non-static
6833 -- expression value) means this is not a static predicate.
6839 end Membership_Entry;
6845 function Stat_Pred (Typ : Entity_Id) return RList is
6847 -- Not static if type does not have static predicates
6849 if not Has_Predicates (Typ) or else No (Static_Predicate (Typ)) then
6853 -- Otherwise we convert the predicate list to a range list
6856 Result : RList (1 .. List_Length (Static_Predicate (Typ)));
6860 P := First (Static_Predicate (Typ));
6861 for J in Result'Range loop
6862 Result (J) := REnt'(Lo_Val (P), Hi_Val (P));
6870 -- Start of processing for Build_Static_Predicate
6873 -- Now analyze the expression to see if it is a static predicate
6876 Ranges : constant RList := Get_RList (Expr);
6877 -- Range list from expression if it is static
6882 -- Convert range list into a form for the static predicate. In the
6883 -- Ranges array, we just have raw ranges, these must be converted
6884 -- to properly typed and analyzed static expressions or range nodes.
6886 -- Note: here we limit ranges to the ranges of the subtype, so that
6887 -- a predicate is always false for values outside the subtype. That
6888 -- seems fine, such values are invalid anyway, and considering them
6889 -- to fail the predicate seems allowed and friendly, and furthermore
6890 -- simplifies processing for case statements and loops.
6894 for J in Ranges'Range loop
6896 Lo : Uint := Ranges (J).Lo;
6897 Hi : Uint := Ranges (J).Hi;
6900 -- Ignore completely out of range entry
6902 if Hi < TLo or else Lo > THi then
6905 -- Otherwise process entry
6908 -- Adjust out of range value to subtype range
6918 -- Convert range into required form
6920 Append_To (Plist, Build_Range (Lo, Hi));
6925 -- Processing was successful and all entries were static, so now we
6926 -- can store the result as the predicate list.
6928 Set_Static_Predicate (Typ, Plist);
6930 -- The processing for static predicates put the expression into
6931 -- canonical form as a series of ranges. It also eliminated
6932 -- duplicates and collapsed and combined ranges. We might as well
6933 -- replace the alternatives list of the right operand of the
6934 -- membership test with the static predicate list, which will
6935 -- usually be more efficient.
6938 New_Alts : constant List_Id := New_List;
6943 Old_Node := First (Plist);
6944 while Present (Old_Node) loop
6945 New_Node := New_Copy (Old_Node);
6947 if Nkind (New_Node) = N_Range then
6948 Set_Low_Bound (New_Node, New_Copy (Low_Bound (Old_Node)));
6949 Set_High_Bound (New_Node, New_Copy (High_Bound (Old_Node)));
6952 Append_To (New_Alts, New_Node);
6956 -- If empty list, replace by False
6958 if Is_Empty_List (New_Alts) then
6959 Rewrite (Expr, New_Occurrence_Of (Standard_False, Loc));
6961 -- Else replace by set membership test
6966 Left_Opnd => Make_Identifier (Loc, Nam),
6967 Right_Opnd => Empty,
6968 Alternatives => New_Alts));
6970 -- Resolve new expression in function context
6972 Install_Formals (Predicate_Function (Typ));
6973 Push_Scope (Predicate_Function (Typ));
6974 Analyze_And_Resolve (Expr, Standard_Boolean);
6980 -- If non-static, return doing nothing
6985 end Build_Static_Predicate;
6987 -----------------------------------------
6988 -- Check_Aspect_At_End_Of_Declarations --
6989 -----------------------------------------
6991 procedure Check_Aspect_At_End_Of_Declarations (ASN : Node_Id) is
6992 Ent : constant Entity_Id := Entity (ASN);
6993 Ident : constant Node_Id := Identifier (ASN);
6994 A_Id : constant Aspect_Id := Get_Aspect_Id (Chars (Ident));
6996 End_Decl_Expr : constant Node_Id := Entity (Ident);
6997 -- Expression to be analyzed at end of declarations
6999 Freeze_Expr : constant Node_Id := Expression (ASN);
7000 -- Expression from call to Check_Aspect_At_Freeze_Point
7002 T : constant Entity_Id := Etype (Freeze_Expr);
7003 -- Type required for preanalyze call
7006 -- Set False if error
7008 -- On entry to this procedure, Entity (Ident) contains a copy of the
7009 -- original expression from the aspect, saved for this purpose, and
7010 -- but Expression (Ident) is a preanalyzed copy of the expression,
7011 -- preanalyzed just after the freeze point.
7013 procedure Check_Overloaded_Name;
7014 -- For aspects whose expression is simply a name, this routine checks if
7015 -- the name is overloaded or not. If so, it verifies there is an
7016 -- interpretation that matches the entity obtained at the freeze point,
7017 -- otherwise the compiler complains.
7019 ---------------------------
7020 -- Check_Overloaded_Name --
7021 ---------------------------
7023 procedure Check_Overloaded_Name is
7025 if not Is_Overloaded (End_Decl_Expr) then
7026 Err := Entity (End_Decl_Expr) /= Entity (Freeze_Expr);
7032 Index : Interp_Index;
7036 Get_First_Interp (End_Decl_Expr, Index, It);
7037 while Present (It.Typ) loop
7038 if It.Nam = Entity (Freeze_Expr) then
7043 Get_Next_Interp (Index, It);
7047 end Check_Overloaded_Name;
7049 -- Start of processing for Check_Aspect_At_End_Of_Declarations
7052 -- Case of aspects Dimension, Dimension_System and Synchronization
7054 if A_Id = Aspect_Synchronization then
7057 -- Case of stream attributes, just have to compare entities. However,
7058 -- the expression is just a name (possibly overloaded), and there may
7059 -- be stream operations declared for unrelated types, so we just need
7060 -- to verify that one of these interpretations is the one available at
7061 -- at the freeze point.
7063 elsif A_Id = Aspect_Input or else
7064 A_Id = Aspect_Output or else
7065 A_Id = Aspect_Read or else
7068 Analyze (End_Decl_Expr);
7069 Check_Overloaded_Name;
7071 elsif A_Id = Aspect_Variable_Indexing or else
7072 A_Id = Aspect_Constant_Indexing or else
7073 A_Id = Aspect_Default_Iterator or else
7074 A_Id = Aspect_Iterator_Element
7076 -- Make type unfrozen before analysis, to prevent spurious errors
7077 -- about late attributes.
7079 Set_Is_Frozen (Ent, False);
7080 Analyze (End_Decl_Expr);
7081 Set_Is_Frozen (Ent, True);
7083 -- If the end of declarations comes before any other freeze
7084 -- point, the Freeze_Expr is not analyzed: no check needed.
7086 if Analyzed (Freeze_Expr) and then not In_Instance then
7087 Check_Overloaded_Name;
7095 -- In a generic context the aspect expressions have not been
7096 -- preanalyzed, so do it now. There are no conformance checks
7097 -- to perform in this case.
7100 Check_Aspect_At_Freeze_Point (ASN);
7103 -- The default values attributes may be defined in the private part,
7104 -- and the analysis of the expression may take place when only the
7105 -- partial view is visible. The expression must be scalar, so use
7106 -- the full view to resolve.
7108 elsif (A_Id = Aspect_Default_Value
7110 A_Id = Aspect_Default_Component_Value)
7111 and then Is_Private_Type (T)
7113 Preanalyze_Spec_Expression (End_Decl_Expr, Full_View (T));
7115 Preanalyze_Spec_Expression (End_Decl_Expr, T);
7118 Err := not Fully_Conformant_Expressions (End_Decl_Expr, Freeze_Expr);
7121 -- Output error message if error
7125 ("visibility of aspect for& changes after freeze point",
7128 ("info: & is frozen here, aspects evaluated at this point??",
7129 Freeze_Node (Ent), Ent);
7131 end Check_Aspect_At_End_Of_Declarations;
7133 ----------------------------------
7134 -- Check_Aspect_At_Freeze_Point --
7135 ----------------------------------
7137 procedure Check_Aspect_At_Freeze_Point (ASN : Node_Id) is
7138 Ident : constant Node_Id := Identifier (ASN);
7139 -- Identifier (use Entity field to save expression)
7141 A_Id : constant Aspect_Id := Get_Aspect_Id (Chars (Ident));
7143 T : Entity_Id := Empty;
7144 -- Type required for preanalyze call
7147 -- On entry to this procedure, Entity (Ident) contains a copy of the
7148 -- original expression from the aspect, saved for this purpose.
7150 -- On exit from this procedure Entity (Ident) is unchanged, still
7151 -- containing that copy, but Expression (Ident) is a preanalyzed copy
7152 -- of the expression, preanalyzed just after the freeze point.
7154 -- Make a copy of the expression to be preanalyzed
7156 Set_Expression (ASN, New_Copy_Tree (Entity (Ident)));
7158 -- Find type for preanalyze call
7162 -- No_Aspect should be impossible
7165 raise Program_Error;
7167 -- Aspects taking an optional boolean argument
7169 when Boolean_Aspects |
7170 Library_Unit_Aspects =>
7171 T := Standard_Boolean;
7173 -- Aspects corresponding to attribute definition clauses
7175 when Aspect_Address =>
7176 T := RTE (RE_Address);
7178 when Aspect_Attach_Handler =>
7179 T := RTE (RE_Interrupt_ID);
7181 when Aspect_Bit_Order | Aspect_Scalar_Storage_Order =>
7182 T := RTE (RE_Bit_Order);
7184 when Aspect_Convention =>
7188 T := RTE (RE_CPU_Range);
7190 -- Default_Component_Value is resolved with the component type
7192 when Aspect_Default_Component_Value =>
7193 T := Component_Type (Entity (ASN));
7195 -- Default_Value is resolved with the type entity in question
7197 when Aspect_Default_Value =>
7200 -- Depends is a delayed aspect because it mentiones names first
7201 -- introduced by aspect Global which is already delayed. There is
7202 -- no action to be taken with respect to the aspect itself as the
7203 -- analysis is done by the corresponding pragma.
7205 when Aspect_Depends =>
7208 when Aspect_Dispatching_Domain =>
7209 T := RTE (RE_Dispatching_Domain);
7211 when Aspect_External_Tag =>
7212 T := Standard_String;
7214 when Aspect_External_Name =>
7215 T := Standard_String;
7217 -- Global is a delayed aspect because it may reference names that
7218 -- have not been declared yet. There is no action to be taken with
7219 -- respect to the aspect itself as the reference checking is done
7220 -- on the corresponding pragma.
7222 when Aspect_Global =>
7225 when Aspect_Link_Name =>
7226 T := Standard_String;
7228 when Aspect_Priority | Aspect_Interrupt_Priority =>
7229 T := Standard_Integer;
7231 when Aspect_Relative_Deadline =>
7232 T := RTE (RE_Time_Span);
7234 when Aspect_Small =>
7235 T := Universal_Real;
7237 -- For a simple storage pool, we have to retrieve the type of the
7238 -- pool object associated with the aspect's corresponding attribute
7239 -- definition clause.
7241 when Aspect_Simple_Storage_Pool =>
7242 T := Etype (Expression (Aspect_Rep_Item (ASN)));
7244 when Aspect_Storage_Pool =>
7245 T := Class_Wide_Type (RTE (RE_Root_Storage_Pool));
7247 when Aspect_Alignment |
7248 Aspect_Component_Size |
7249 Aspect_Machine_Radix |
7250 Aspect_Object_Size |
7252 Aspect_Storage_Size |
7253 Aspect_Stream_Size |
7254 Aspect_Value_Size =>
7257 when Aspect_Synchronization =>
7260 -- Special case, the expression of these aspects is just an entity
7261 -- that does not need any resolution, so just analyze.
7270 Analyze (Expression (ASN));
7273 -- Same for Iterator aspects, where the expression is a function
7274 -- name. Legality rules are checked separately.
7276 when Aspect_Constant_Indexing |
7277 Aspect_Default_Iterator |
7278 Aspect_Iterator_Element |
7279 Aspect_Variable_Indexing =>
7280 Analyze (Expression (ASN));
7283 -- Invariant/Predicate take boolean expressions
7285 when Aspect_Dynamic_Predicate |
7288 Aspect_Static_Predicate |
7289 Aspect_Type_Invariant =>
7290 T := Standard_Boolean;
7292 -- Here is the list of aspects that don't require delay analysis
7294 when Aspect_Abstract_State |
7295 Aspect_Contract_Case |
7296 Aspect_Contract_Cases |
7298 Aspect_Dimension_System |
7299 Aspect_Implicit_Dereference |
7301 Aspect_Postcondition |
7303 Aspect_Precondition |
7305 raise Program_Error;
7309 -- Do the preanalyze call
7311 Preanalyze_Spec_Expression (Expression (ASN), T);
7312 end Check_Aspect_At_Freeze_Point;
7314 -----------------------------------
7315 -- Check_Constant_Address_Clause --
7316 -----------------------------------
7318 procedure Check_Constant_Address_Clause
7322 procedure Check_At_Constant_Address (Nod : Node_Id);
7323 -- Checks that the given node N represents a name whose 'Address is
7324 -- constant (in the same sense as OK_Constant_Address_Clause, i.e. the
7325 -- address value is the same at the point of declaration of U_Ent and at
7326 -- the time of elaboration of the address clause.
7328 procedure Check_Expr_Constants (Nod : Node_Id);
7329 -- Checks that Nod meets the requirements for a constant address clause
7330 -- in the sense of the enclosing procedure.
7332 procedure Check_List_Constants (Lst : List_Id);
7333 -- Check that all elements of list Lst meet the requirements for a
7334 -- constant address clause in the sense of the enclosing procedure.
7336 -------------------------------
7337 -- Check_At_Constant_Address --
7338 -------------------------------
7340 procedure Check_At_Constant_Address (Nod : Node_Id) is
7342 if Is_Entity_Name (Nod) then
7343 if Present (Address_Clause (Entity ((Nod)))) then
7345 ("invalid address clause for initialized object &!",
7348 ("address for& cannot" &
7349 " depend on another address clause! (RM 13.1(22))!",
7352 elsif In_Same_Source_Unit (Entity (Nod), U_Ent)
7353 and then Sloc (U_Ent) < Sloc (Entity (Nod))
7356 ("invalid address clause for initialized object &!",
7358 Error_Msg_Node_2 := U_Ent;
7360 ("\& must be defined before & (RM 13.1(22))!",
7364 elsif Nkind (Nod) = N_Selected_Component then
7366 T : constant Entity_Id := Etype (Prefix (Nod));
7369 if (Is_Record_Type (T)
7370 and then Has_Discriminants (T))
7373 and then Is_Record_Type (Designated_Type (T))
7374 and then Has_Discriminants (Designated_Type (T)))
7377 ("invalid address clause for initialized object &!",
7380 ("\address cannot depend on component" &
7381 " of discriminated record (RM 13.1(22))!",
7384 Check_At_Constant_Address (Prefix (Nod));
7388 elsif Nkind (Nod) = N_Indexed_Component then
7389 Check_At_Constant_Address (Prefix (Nod));
7390 Check_List_Constants (Expressions (Nod));
7393 Check_Expr_Constants (Nod);
7395 end Check_At_Constant_Address;
7397 --------------------------
7398 -- Check_Expr_Constants --
7399 --------------------------
7401 procedure Check_Expr_Constants (Nod : Node_Id) is
7402 Loc_U_Ent : constant Source_Ptr := Sloc (U_Ent);
7403 Ent : Entity_Id := Empty;
7406 if Nkind (Nod) in N_Has_Etype
7407 and then Etype (Nod) = Any_Type
7413 when N_Empty | N_Error =>
7416 when N_Identifier | N_Expanded_Name =>
7417 Ent := Entity (Nod);
7419 -- We need to look at the original node if it is different
7420 -- from the node, since we may have rewritten things and
7421 -- substituted an identifier representing the rewrite.
7423 if Original_Node (Nod) /= Nod then
7424 Check_Expr_Constants (Original_Node (Nod));
7426 -- If the node is an object declaration without initial
7427 -- value, some code has been expanded, and the expression
7428 -- is not constant, even if the constituents might be
7429 -- acceptable, as in A'Address + offset.
7431 if Ekind (Ent) = E_Variable
7433 Nkind (Declaration_Node (Ent)) = N_Object_Declaration
7435 No (Expression (Declaration_Node (Ent)))
7438 ("invalid address clause for initialized object &!",
7441 -- If entity is constant, it may be the result of expanding
7442 -- a check. We must verify that its declaration appears
7443 -- before the object in question, else we also reject the
7446 elsif Ekind (Ent) = E_Constant
7447 and then In_Same_Source_Unit (Ent, U_Ent)
7448 and then Sloc (Ent) > Loc_U_Ent
7451 ("invalid address clause for initialized object &!",
7458 -- Otherwise look at the identifier and see if it is OK
7460 if Ekind_In (Ent, E_Named_Integer, E_Named_Real)
7461 or else Is_Type (Ent)
7466 Ekind (Ent) = E_Constant
7468 Ekind (Ent) = E_In_Parameter
7470 -- This is the case where we must have Ent defined before
7471 -- U_Ent. Clearly if they are in different units this
7472 -- requirement is met since the unit containing Ent is
7473 -- already processed.
7475 if not In_Same_Source_Unit (Ent, U_Ent) then
7478 -- Otherwise location of Ent must be before the location
7479 -- of U_Ent, that's what prior defined means.
7481 elsif Sloc (Ent) < Loc_U_Ent then
7486 ("invalid address clause for initialized object &!",
7488 Error_Msg_Node_2 := U_Ent;
7490 ("\& must be defined before & (RM 13.1(22))!",
7494 elsif Nkind (Original_Node (Nod)) = N_Function_Call then
7495 Check_Expr_Constants (Original_Node (Nod));
7499 ("invalid address clause for initialized object &!",
7502 if Comes_From_Source (Ent) then
7504 ("\reference to variable& not allowed"
7505 & " (RM 13.1(22))!", Nod, Ent);
7508 ("non-static expression not allowed"
7509 & " (RM 13.1(22))!", Nod);
7513 when N_Integer_Literal =>
7515 -- If this is a rewritten unchecked conversion, in a system
7516 -- where Address is an integer type, always use the base type
7517 -- for a literal value. This is user-friendly and prevents
7518 -- order-of-elaboration issues with instances of unchecked
7521 if Nkind (Original_Node (Nod)) = N_Function_Call then
7522 Set_Etype (Nod, Base_Type (Etype (Nod)));
7525 when N_Real_Literal |
7527 N_Character_Literal =>
7531 Check_Expr_Constants (Low_Bound (Nod));
7532 Check_Expr_Constants (High_Bound (Nod));
7534 when N_Explicit_Dereference =>
7535 Check_Expr_Constants (Prefix (Nod));
7537 when N_Indexed_Component =>
7538 Check_Expr_Constants (Prefix (Nod));
7539 Check_List_Constants (Expressions (Nod));
7542 Check_Expr_Constants (Prefix (Nod));
7543 Check_Expr_Constants (Discrete_Range (Nod));
7545 when N_Selected_Component =>
7546 Check_Expr_Constants (Prefix (Nod));
7548 when N_Attribute_Reference =>
7549 if Attribute_Name (Nod) = Name_Address
7551 Attribute_Name (Nod) = Name_Access
7553 Attribute_Name (Nod) = Name_Unchecked_Access
7555 Attribute_Name (Nod) = Name_Unrestricted_Access
7557 Check_At_Constant_Address (Prefix (Nod));
7560 Check_Expr_Constants (Prefix (Nod));
7561 Check_List_Constants (Expressions (Nod));
7565 Check_List_Constants (Component_Associations (Nod));
7566 Check_List_Constants (Expressions (Nod));
7568 when N_Component_Association =>
7569 Check_Expr_Constants (Expression (Nod));
7571 when N_Extension_Aggregate =>
7572 Check_Expr_Constants (Ancestor_Part (Nod));
7573 Check_List_Constants (Component_Associations (Nod));
7574 Check_List_Constants (Expressions (Nod));
7579 when N_Binary_Op | N_Short_Circuit | N_Membership_Test =>
7580 Check_Expr_Constants (Left_Opnd (Nod));
7581 Check_Expr_Constants (Right_Opnd (Nod));
7584 Check_Expr_Constants (Right_Opnd (Nod));
7586 when N_Type_Conversion |
7587 N_Qualified_Expression |
7589 N_Unchecked_Type_Conversion =>
7590 Check_Expr_Constants (Expression (Nod));
7592 when N_Function_Call =>
7593 if not Is_Pure (Entity (Name (Nod))) then
7595 ("invalid address clause for initialized object &!",
7599 ("\function & is not pure (RM 13.1(22))!",
7600 Nod, Entity (Name (Nod)));
7603 Check_List_Constants (Parameter_Associations (Nod));
7606 when N_Parameter_Association =>
7607 Check_Expr_Constants (Explicit_Actual_Parameter (Nod));
7611 ("invalid address clause for initialized object &!",
7614 ("\must be constant defined before& (RM 13.1(22))!",
7617 end Check_Expr_Constants;
7619 --------------------------
7620 -- Check_List_Constants --
7621 --------------------------
7623 procedure Check_List_Constants (Lst : List_Id) is
7627 if Present (Lst) then
7628 Nod1 := First (Lst);
7629 while Present (Nod1) loop
7630 Check_Expr_Constants (Nod1);
7634 end Check_List_Constants;
7636 -- Start of processing for Check_Constant_Address_Clause
7639 -- If rep_clauses are to be ignored, no need for legality checks. In
7640 -- particular, no need to pester user about rep clauses that violate
7641 -- the rule on constant addresses, given that these clauses will be
7642 -- removed by Freeze before they reach the back end.
7644 if not Ignore_Rep_Clauses then
7645 Check_Expr_Constants (Expr);
7647 end Check_Constant_Address_Clause;
7649 ----------------------------------------
7650 -- Check_Record_Representation_Clause --
7651 ----------------------------------------
7653 procedure Check_Record_Representation_Clause (N : Node_Id) is
7654 Loc : constant Source_Ptr := Sloc (N);
7655 Ident : constant Node_Id := Identifier (N);
7656 Rectype : Entity_Id;
7661 Hbit : Uint := Uint_0;
7665 Max_Bit_So_Far : Uint;
7666 -- Records the maximum bit position so far. If all field positions
7667 -- are monotonically increasing, then we can skip the circuit for
7668 -- checking for overlap, since no overlap is possible.
7670 Tagged_Parent : Entity_Id := Empty;
7671 -- This is set in the case of a derived tagged type for which we have
7672 -- Is_Fully_Repped_Tagged_Type True (indicating that all components are
7673 -- positioned by record representation clauses). In this case we must
7674 -- check for overlap between components of this tagged type, and the
7675 -- components of its parent. Tagged_Parent will point to this parent
7676 -- type. For all other cases Tagged_Parent is left set to Empty.
7678 Parent_Last_Bit : Uint;
7679 -- Relevant only if Tagged_Parent is set, Parent_Last_Bit indicates the
7680 -- last bit position for any field in the parent type. We only need to
7681 -- check overlap for fields starting below this point.
7683 Overlap_Check_Required : Boolean;
7684 -- Used to keep track of whether or not an overlap check is required
7686 Overlap_Detected : Boolean := False;
7687 -- Set True if an overlap is detected
7689 Ccount : Natural := 0;
7690 -- Number of component clauses in record rep clause
7692 procedure Check_Component_Overlap (C1_Ent, C2_Ent : Entity_Id);
7693 -- Given two entities for record components or discriminants, checks
7694 -- if they have overlapping component clauses and issues errors if so.
7696 procedure Find_Component;
7697 -- Finds component entity corresponding to current component clause (in
7698 -- CC), and sets Comp to the entity, and Fbit/Lbit to the zero origin
7699 -- start/stop bits for the field. If there is no matching component or
7700 -- if the matching component does not have a component clause, then
7701 -- that's an error and Comp is set to Empty, but no error message is
7702 -- issued, since the message was already given. Comp is also set to
7703 -- Empty if the current "component clause" is in fact a pragma.
7705 -----------------------------
7706 -- Check_Component_Overlap --
7707 -----------------------------
7709 procedure Check_Component_Overlap (C1_Ent, C2_Ent : Entity_Id) is
7710 CC1 : constant Node_Id := Component_Clause (C1_Ent);
7711 CC2 : constant Node_Id := Component_Clause (C2_Ent);
7714 if Present (CC1) and then Present (CC2) then
7716 -- Exclude odd case where we have two tag components in the same
7717 -- record, both at location zero. This seems a bit strange, but
7718 -- it seems to happen in some circumstances, perhaps on an error.
7720 if Chars (C1_Ent) = Name_uTag
7722 Chars (C2_Ent) = Name_uTag
7727 -- Here we check if the two fields overlap
7730 S1 : constant Uint := Component_Bit_Offset (C1_Ent);
7731 S2 : constant Uint := Component_Bit_Offset (C2_Ent);
7732 E1 : constant Uint := S1 + Esize (C1_Ent);
7733 E2 : constant Uint := S2 + Esize (C2_Ent);
7736 if E2 <= S1 or else E1 <= S2 then
7739 Error_Msg_Node_2 := Component_Name (CC2);
7740 Error_Msg_Sloc := Sloc (Error_Msg_Node_2);
7741 Error_Msg_Node_1 := Component_Name (CC1);
7743 ("component& overlaps & #", Component_Name (CC1));
7744 Overlap_Detected := True;
7748 end Check_Component_Overlap;
7750 --------------------
7751 -- Find_Component --
7752 --------------------
7754 procedure Find_Component is
7756 procedure Search_Component (R : Entity_Id);
7757 -- Search components of R for a match. If found, Comp is set
7759 ----------------------
7760 -- Search_Component --
7761 ----------------------
7763 procedure Search_Component (R : Entity_Id) is
7765 Comp := First_Component_Or_Discriminant (R);
7766 while Present (Comp) loop
7768 -- Ignore error of attribute name for component name (we
7769 -- already gave an error message for this, so no need to
7772 if Nkind (Component_Name (CC)) = N_Attribute_Reference then
7775 exit when Chars (Comp) = Chars (Component_Name (CC));
7778 Next_Component_Or_Discriminant (Comp);
7780 end Search_Component;
7782 -- Start of processing for Find_Component
7785 -- Return with Comp set to Empty if we have a pragma
7787 if Nkind (CC) = N_Pragma then
7792 -- Search current record for matching component
7794 Search_Component (Rectype);
7796 -- If not found, maybe component of base type discriminant that is
7797 -- absent from statically constrained first subtype.
7800 Search_Component (Base_Type (Rectype));
7803 -- If no component, or the component does not reference the component
7804 -- clause in question, then there was some previous error for which
7805 -- we already gave a message, so just return with Comp Empty.
7807 if No (Comp) or else Component_Clause (Comp) /= CC then
7808 Check_Error_Detected;
7811 -- Normal case where we have a component clause
7814 Fbit := Component_Bit_Offset (Comp);
7815 Lbit := Fbit + Esize (Comp) - 1;
7819 -- Start of processing for Check_Record_Representation_Clause
7823 Rectype := Entity (Ident);
7825 if Rectype = Any_Type then
7828 Rectype := Underlying_Type (Rectype);
7831 -- See if we have a fully repped derived tagged type
7834 PS : constant Entity_Id := Parent_Subtype (Rectype);
7837 if Present (PS) and then Is_Fully_Repped_Tagged_Type (PS) then
7838 Tagged_Parent := PS;
7840 -- Find maximum bit of any component of the parent type
7842 Parent_Last_Bit := UI_From_Int (System_Address_Size - 1);
7843 Pcomp := First_Entity (Tagged_Parent);
7844 while Present (Pcomp) loop
7845 if Ekind_In (Pcomp, E_Discriminant, E_Component) then
7846 if Component_Bit_Offset (Pcomp) /= No_Uint
7847 and then Known_Static_Esize (Pcomp)
7852 Component_Bit_Offset (Pcomp) + Esize (Pcomp) - 1);
7855 Next_Entity (Pcomp);
7861 -- All done if no component clauses
7863 CC := First (Component_Clauses (N));
7869 -- If a tag is present, then create a component clause that places it
7870 -- at the start of the record (otherwise gigi may place it after other
7871 -- fields that have rep clauses).
7873 Fent := First_Entity (Rectype);
7875 if Nkind (Fent) = N_Defining_Identifier
7876 and then Chars (Fent) = Name_uTag
7878 Set_Component_Bit_Offset (Fent, Uint_0);
7879 Set_Normalized_Position (Fent, Uint_0);
7880 Set_Normalized_First_Bit (Fent, Uint_0);
7881 Set_Normalized_Position_Max (Fent, Uint_0);
7882 Init_Esize (Fent, System_Address_Size);
7884 Set_Component_Clause (Fent,
7885 Make_Component_Clause (Loc,
7886 Component_Name => Make_Identifier (Loc, Name_uTag),
7888 Position => Make_Integer_Literal (Loc, Uint_0),
7889 First_Bit => Make_Integer_Literal (Loc, Uint_0),
7891 Make_Integer_Literal (Loc,
7892 UI_From_Int (System_Address_Size))));
7894 Ccount := Ccount + 1;
7897 Max_Bit_So_Far := Uint_Minus_1;
7898 Overlap_Check_Required := False;
7900 -- Process the component clauses
7902 while Present (CC) loop
7905 if Present (Comp) then
7906 Ccount := Ccount + 1;
7908 -- We need a full overlap check if record positions non-monotonic
7910 if Fbit <= Max_Bit_So_Far then
7911 Overlap_Check_Required := True;
7914 Max_Bit_So_Far := Lbit;
7916 -- Check bit position out of range of specified size
7918 if Has_Size_Clause (Rectype)
7919 and then RM_Size (Rectype) <= Lbit
7922 ("bit number out of range of specified size",
7925 -- Check for overlap with tag component
7928 if Is_Tagged_Type (Rectype)
7929 and then Fbit < System_Address_Size
7932 ("component overlaps tag field of&",
7933 Component_Name (CC), Rectype);
7934 Overlap_Detected := True;
7942 -- Check parent overlap if component might overlap parent field
7944 if Present (Tagged_Parent) and then Fbit <= Parent_Last_Bit then
7945 Pcomp := First_Component_Or_Discriminant (Tagged_Parent);
7946 while Present (Pcomp) loop
7947 if not Is_Tag (Pcomp)
7948 and then Chars (Pcomp) /= Name_uParent
7950 Check_Component_Overlap (Comp, Pcomp);
7953 Next_Component_Or_Discriminant (Pcomp);
7961 -- Now that we have processed all the component clauses, check for
7962 -- overlap. We have to leave this till last, since the components can
7963 -- appear in any arbitrary order in the representation clause.
7965 -- We do not need this check if all specified ranges were monotonic,
7966 -- as recorded by Overlap_Check_Required being False at this stage.
7968 -- This first section checks if there are any overlapping entries at
7969 -- all. It does this by sorting all entries and then seeing if there are
7970 -- any overlaps. If there are none, then that is decisive, but if there
7971 -- are overlaps, they may still be OK (they may result from fields in
7972 -- different variants).
7974 if Overlap_Check_Required then
7975 Overlap_Check1 : declare
7977 OC_Fbit : array (0 .. Ccount) of Uint;
7978 -- First-bit values for component clauses, the value is the offset
7979 -- of the first bit of the field from start of record. The zero
7980 -- entry is for use in sorting.
7982 OC_Lbit : array (0 .. Ccount) of Uint;
7983 -- Last-bit values for component clauses, the value is the offset
7984 -- of the last bit of the field from start of record. The zero
7985 -- entry is for use in sorting.
7987 OC_Count : Natural := 0;
7988 -- Count of entries in OC_Fbit and OC_Lbit
7990 function OC_Lt (Op1, Op2 : Natural) return Boolean;
7991 -- Compare routine for Sort
7993 procedure OC_Move (From : Natural; To : Natural);
7994 -- Move routine for Sort
7996 package Sorting is new GNAT.Heap_Sort_G (OC_Move, OC_Lt);
8002 function OC_Lt (Op1, Op2 : Natural) return Boolean is
8004 return OC_Fbit (Op1) < OC_Fbit (Op2);
8011 procedure OC_Move (From : Natural; To : Natural) is
8013 OC_Fbit (To) := OC_Fbit (From);
8014 OC_Lbit (To) := OC_Lbit (From);
8017 -- Start of processing for Overlap_Check
8020 CC := First (Component_Clauses (N));
8021 while Present (CC) loop
8023 -- Exclude component clause already marked in error
8025 if not Error_Posted (CC) then
8028 if Present (Comp) then
8029 OC_Count := OC_Count + 1;
8030 OC_Fbit (OC_Count) := Fbit;
8031 OC_Lbit (OC_Count) := Lbit;
8038 Sorting.Sort (OC_Count);
8040 Overlap_Check_Required := False;
8041 for J in 1 .. OC_Count - 1 loop
8042 if OC_Lbit (J) >= OC_Fbit (J + 1) then
8043 Overlap_Check_Required := True;
8050 -- If Overlap_Check_Required is still True, then we have to do the full
8051 -- scale overlap check, since we have at least two fields that do
8052 -- overlap, and we need to know if that is OK since they are in
8053 -- different variant, or whether we have a definite problem.
8055 if Overlap_Check_Required then
8056 Overlap_Check2 : declare
8057 C1_Ent, C2_Ent : Entity_Id;
8058 -- Entities of components being checked for overlap
8061 -- Component_List node whose Component_Items are being checked
8064 -- Component declaration for component being checked
8067 C1_Ent := First_Entity (Base_Type (Rectype));
8069 -- Loop through all components in record. For each component check
8070 -- for overlap with any of the preceding elements on the component
8071 -- list containing the component and also, if the component is in
8072 -- a variant, check against components outside the case structure.
8073 -- This latter test is repeated recursively up the variant tree.
8075 Main_Component_Loop : while Present (C1_Ent) loop
8076 if not Ekind_In (C1_Ent, E_Component, E_Discriminant) then
8077 goto Continue_Main_Component_Loop;
8080 -- Skip overlap check if entity has no declaration node. This
8081 -- happens with discriminants in constrained derived types.
8082 -- Possibly we are missing some checks as a result, but that
8083 -- does not seem terribly serious.
8085 if No (Declaration_Node (C1_Ent)) then
8086 goto Continue_Main_Component_Loop;
8089 Clist := Parent (List_Containing (Declaration_Node (C1_Ent)));
8091 -- Loop through component lists that need checking. Check the
8092 -- current component list and all lists in variants above us.
8094 Component_List_Loop : loop
8096 -- If derived type definition, go to full declaration
8097 -- If at outer level, check discriminants if there are any.
8099 if Nkind (Clist) = N_Derived_Type_Definition then
8100 Clist := Parent (Clist);
8103 -- Outer level of record definition, check discriminants
8105 if Nkind_In (Clist, N_Full_Type_Declaration,
8106 N_Private_Type_Declaration)
8108 if Has_Discriminants (Defining_Identifier (Clist)) then
8110 First_Discriminant (Defining_Identifier (Clist));
8111 while Present (C2_Ent) loop
8112 exit when C1_Ent = C2_Ent;
8113 Check_Component_Overlap (C1_Ent, C2_Ent);
8114 Next_Discriminant (C2_Ent);
8118 -- Record extension case
8120 elsif Nkind (Clist) = N_Derived_Type_Definition then
8123 -- Otherwise check one component list
8126 Citem := First (Component_Items (Clist));
8127 while Present (Citem) loop
8128 if Nkind (Citem) = N_Component_Declaration then
8129 C2_Ent := Defining_Identifier (Citem);
8130 exit when C1_Ent = C2_Ent;
8131 Check_Component_Overlap (C1_Ent, C2_Ent);
8138 -- Check for variants above us (the parent of the Clist can
8139 -- be a variant, in which case its parent is a variant part,
8140 -- and the parent of the variant part is a component list
8141 -- whose components must all be checked against the current
8142 -- component for overlap).
8144 if Nkind (Parent (Clist)) = N_Variant then
8145 Clist := Parent (Parent (Parent (Clist)));
8147 -- Check for possible discriminant part in record, this
8148 -- is treated essentially as another level in the
8149 -- recursion. For this case the parent of the component
8150 -- list is the record definition, and its parent is the
8151 -- full type declaration containing the discriminant
8154 elsif Nkind (Parent (Clist)) = N_Record_Definition then
8155 Clist := Parent (Parent ((Clist)));
8157 -- If neither of these two cases, we are at the top of
8161 exit Component_List_Loop;
8163 end loop Component_List_Loop;
8165 <<Continue_Main_Component_Loop>>
8166 Next_Entity (C1_Ent);
8168 end loop Main_Component_Loop;
8172 -- The following circuit deals with warning on record holes (gaps). We
8173 -- skip this check if overlap was detected, since it makes sense for the
8174 -- programmer to fix this illegality before worrying about warnings.
8176 if not Overlap_Detected and Warn_On_Record_Holes then
8177 Record_Hole_Check : declare
8178 Decl : constant Node_Id := Declaration_Node (Base_Type (Rectype));
8179 -- Full declaration of record type
8181 procedure Check_Component_List
8185 -- Check component list CL for holes. The starting bit should be
8186 -- Sbit. which is zero for the main record component list and set
8187 -- appropriately for recursive calls for variants. DS is set to
8188 -- a list of discriminant specifications to be included in the
8189 -- consideration of components. It is No_List if none to consider.
8191 --------------------------
8192 -- Check_Component_List --
8193 --------------------------
8195 procedure Check_Component_List
8203 Compl := Integer (List_Length (Component_Items (CL)));
8205 if DS /= No_List then
8206 Compl := Compl + Integer (List_Length (DS));
8210 Comps : array (Natural range 0 .. Compl) of Entity_Id;
8211 -- Gather components (zero entry is for sort routine)
8213 Ncomps : Natural := 0;
8214 -- Number of entries stored in Comps (starting at Comps (1))
8217 -- One component item or discriminant specification
8220 -- Starting bit for next component
8228 function Lt (Op1, Op2 : Natural) return Boolean;
8229 -- Compare routine for Sort
8231 procedure Move (From : Natural; To : Natural);
8232 -- Move routine for Sort
8234 package Sorting is new GNAT.Heap_Sort_G (Move, Lt);
8240 function Lt (Op1, Op2 : Natural) return Boolean is
8242 return Component_Bit_Offset (Comps (Op1))
8244 Component_Bit_Offset (Comps (Op2));
8251 procedure Move (From : Natural; To : Natural) is
8253 Comps (To) := Comps (From);
8257 -- Gather discriminants into Comp
8259 if DS /= No_List then
8260 Citem := First (DS);
8261 while Present (Citem) loop
8262 if Nkind (Citem) = N_Discriminant_Specification then
8264 Ent : constant Entity_Id :=
8265 Defining_Identifier (Citem);
8267 if Ekind (Ent) = E_Discriminant then
8268 Ncomps := Ncomps + 1;
8269 Comps (Ncomps) := Ent;
8278 -- Gather component entities into Comp
8280 Citem := First (Component_Items (CL));
8281 while Present (Citem) loop
8282 if Nkind (Citem) = N_Component_Declaration then
8283 Ncomps := Ncomps + 1;
8284 Comps (Ncomps) := Defining_Identifier (Citem);
8290 -- Now sort the component entities based on the first bit.
8291 -- Note we already know there are no overlapping components.
8293 Sorting.Sort (Ncomps);
8295 -- Loop through entries checking for holes
8298 for J in 1 .. Ncomps loop
8300 Error_Msg_Uint_1 := Component_Bit_Offset (CEnt) - Nbit;
8302 if Error_Msg_Uint_1 > 0 then
8304 ("?H?^-bit gap before component&",
8305 Component_Name (Component_Clause (CEnt)), CEnt);
8308 Nbit := Component_Bit_Offset (CEnt) + Esize (CEnt);
8311 -- Process variant parts recursively if present
8313 if Present (Variant_Part (CL)) then
8314 Variant := First (Variants (Variant_Part (CL)));
8315 while Present (Variant) loop
8316 Check_Component_List
8317 (Component_List (Variant), Nbit, No_List);
8322 end Check_Component_List;
8324 -- Start of processing for Record_Hole_Check
8331 if Is_Tagged_Type (Rectype) then
8332 Sbit := UI_From_Int (System_Address_Size);
8337 if Nkind (Decl) = N_Full_Type_Declaration
8338 and then Nkind (Type_Definition (Decl)) = N_Record_Definition
8340 Check_Component_List
8341 (Component_List (Type_Definition (Decl)),
8343 Discriminant_Specifications (Decl));
8346 end Record_Hole_Check;
8349 -- For records that have component clauses for all components, and whose
8350 -- size is less than or equal to 32, we need to know the size in the
8351 -- front end to activate possible packed array processing where the
8352 -- component type is a record.
8354 -- At this stage Hbit + 1 represents the first unused bit from all the
8355 -- component clauses processed, so if the component clauses are
8356 -- complete, then this is the length of the record.
8358 -- For records longer than System.Storage_Unit, and for those where not
8359 -- all components have component clauses, the back end determines the
8360 -- length (it may for example be appropriate to round up the size
8361 -- to some convenient boundary, based on alignment considerations, etc).
8363 if Unknown_RM_Size (Rectype) and then Hbit + 1 <= 32 then
8365 -- Nothing to do if at least one component has no component clause
8367 Comp := First_Component_Or_Discriminant (Rectype);
8368 while Present (Comp) loop
8369 exit when No (Component_Clause (Comp));
8370 Next_Component_Or_Discriminant (Comp);
8373 -- If we fall out of loop, all components have component clauses
8374 -- and so we can set the size to the maximum value.
8377 Set_RM_Size (Rectype, Hbit + 1);
8380 end Check_Record_Representation_Clause;
8386 procedure Check_Size
8390 Biased : out Boolean)
8392 UT : constant Entity_Id := Underlying_Type (T);
8398 -- Reject patently improper size values.
8400 if Is_Elementary_Type (T)
8401 and then Siz > UI_From_Int (Int'Last)
8403 Error_Msg_N ("Size value too large for elementary type", N);
8405 if Nkind (Original_Node (N)) = N_Op_Expon then
8407 ("\maybe '* was meant, rather than '*'*", Original_Node (N));
8411 -- Dismiss generic types
8413 if Is_Generic_Type (T)
8415 Is_Generic_Type (UT)
8417 Is_Generic_Type (Root_Type (UT))
8421 -- Guard against previous errors
8423 elsif No (UT) or else UT = Any_Type then
8424 Check_Error_Detected;
8427 -- Check case of bit packed array
8429 elsif Is_Array_Type (UT)
8430 and then Known_Static_Component_Size (UT)
8431 and then Is_Bit_Packed_Array (UT)
8439 Asiz := Component_Size (UT);
8440 Indx := First_Index (UT);
8442 Ityp := Etype (Indx);
8444 -- If non-static bound, then we are not in the business of
8445 -- trying to check the length, and indeed an error will be
8446 -- issued elsewhere, since sizes of non-static array types
8447 -- cannot be set implicitly or explicitly.
8449 if not Is_Static_Subtype (Ityp) then
8453 -- Otherwise accumulate next dimension
8455 Asiz := Asiz * (Expr_Value (Type_High_Bound (Ityp)) -
8456 Expr_Value (Type_Low_Bound (Ityp)) +
8460 exit when No (Indx);
8467 Error_Msg_Uint_1 := Asiz;
8469 ("size for& too small, minimum allowed is ^", N, T);
8470 Set_Esize (T, Asiz);
8471 Set_RM_Size (T, Asiz);
8475 -- All other composite types are ignored
8477 elsif Is_Composite_Type (UT) then
8480 -- For fixed-point types, don't check minimum if type is not frozen,
8481 -- since we don't know all the characteristics of the type that can
8482 -- affect the size (e.g. a specified small) till freeze time.
8484 elsif Is_Fixed_Point_Type (UT)
8485 and then not Is_Frozen (UT)
8489 -- Cases for which a minimum check is required
8492 -- Ignore if specified size is correct for the type
8494 if Known_Esize (UT) and then Siz = Esize (UT) then
8498 -- Otherwise get minimum size
8500 M := UI_From_Int (Minimum_Size (UT));
8504 -- Size is less than minimum size, but one possibility remains
8505 -- that we can manage with the new size if we bias the type.
8507 M := UI_From_Int (Minimum_Size (UT, Biased => True));
8510 Error_Msg_Uint_1 := M;
8512 ("size for& too small, minimum allowed is ^", N, T);
8522 -------------------------
8523 -- Get_Alignment_Value --
8524 -------------------------
8526 function Get_Alignment_Value (Expr : Node_Id) return Uint is
8527 Align : constant Uint := Static_Integer (Expr);
8530 if Align = No_Uint then
8533 elsif Align <= 0 then
8534 Error_Msg_N ("alignment value must be positive", Expr);
8538 for J in Int range 0 .. 64 loop
8540 M : constant Uint := Uint_2 ** J;
8543 exit when M = Align;
8547 ("alignment value must be power of 2", Expr);
8555 end Get_Alignment_Value;
8557 -------------------------------------
8558 -- Inherit_Aspects_At_Freeze_Point --
8559 -------------------------------------
8561 procedure Inherit_Aspects_At_Freeze_Point (Typ : Entity_Id) is
8562 function Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
8563 (Rep_Item : Node_Id) return Boolean;
8564 -- This routine checks if Rep_Item is either a pragma or an aspect
8565 -- specification node whose correponding pragma (if any) is present in
8566 -- the Rep Item chain of the entity it has been specified to.
8568 --------------------------------------------------
8569 -- Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item --
8570 --------------------------------------------------
8572 function Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
8573 (Rep_Item : Node_Id) return Boolean
8576 return Nkind (Rep_Item) = N_Pragma
8577 or else Present_In_Rep_Item
8578 (Entity (Rep_Item), Aspect_Rep_Item (Rep_Item));
8579 end Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item;
8581 -- Start of processing for Inherit_Aspects_At_Freeze_Point
8584 -- A representation item is either subtype-specific (Size and Alignment
8585 -- clauses) or type-related (all others). Subtype-specific aspects may
8586 -- differ for different subtypes of the same type (RM 13.1.8).
8588 -- A derived type inherits each type-related representation aspect of
8589 -- its parent type that was directly specified before the declaration of
8590 -- the derived type (RM 13.1.15).
8592 -- A derived subtype inherits each subtype-specific representation
8593 -- aspect of its parent subtype that was directly specified before the
8594 -- declaration of the derived type (RM 13.1.15).
8596 -- The general processing involves inheriting a representation aspect
8597 -- from a parent type whenever the first rep item (aspect specification,
8598 -- attribute definition clause, pragma) corresponding to the given
8599 -- representation aspect in the rep item chain of Typ, if any, isn't
8600 -- directly specified to Typ but to one of its parents.
8602 -- ??? Note that, for now, just a limited number of representation
8603 -- aspects have been inherited here so far. Many of them are
8604 -- still inherited in Sem_Ch3. This will be fixed soon. Here is
8605 -- a non- exhaustive list of aspects that likely also need to
8606 -- be moved to this routine: Alignment, Component_Alignment,
8607 -- Component_Size, Machine_Radix, Object_Size, Pack, Predicates,
8608 -- Preelaborable_Initialization, RM_Size and Small.
8610 if Nkind (Parent (Typ)) = N_Private_Extension_Declaration then
8616 if not Has_Rep_Item (Typ, Name_Ada_05, Name_Ada_2005, False)
8617 and then Has_Rep_Item (Typ, Name_Ada_05, Name_Ada_2005)
8618 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
8619 (Get_Rep_Item (Typ, Name_Ada_05, Name_Ada_2005))
8621 Set_Is_Ada_2005_Only (Typ);
8626 if not Has_Rep_Item (Typ, Name_Ada_12, Name_Ada_2012, False)
8627 and then Has_Rep_Item (Typ, Name_Ada_12, Name_Ada_2012)
8628 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
8629 (Get_Rep_Item (Typ, Name_Ada_12, Name_Ada_2012))
8631 Set_Is_Ada_2012_Only (Typ);
8636 if not Has_Rep_Item (Typ, Name_Atomic, Name_Shared, False)
8637 and then Has_Rep_Pragma (Typ, Name_Atomic, Name_Shared)
8638 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
8639 (Get_Rep_Item (Typ, Name_Atomic, Name_Shared))
8641 Set_Is_Atomic (Typ);
8642 Set_Treat_As_Volatile (Typ);
8643 Set_Is_Volatile (Typ);
8646 -- Default_Component_Value
8648 if Is_Array_Type (Typ)
8649 and then Has_Rep_Item (Typ, Name_Default_Component_Value, False)
8650 and then Has_Rep_Item (Typ, Name_Default_Component_Value)
8652 Set_Default_Aspect_Component_Value (Typ,
8653 Default_Aspect_Component_Value
8654 (Entity (Get_Rep_Item (Typ, Name_Default_Component_Value))));
8659 if Is_Scalar_Type (Typ)
8660 and then Has_Rep_Item (Typ, Name_Default_Value, False)
8661 and then Has_Rep_Item (Typ, Name_Default_Value)
8663 Set_Default_Aspect_Value (Typ,
8664 Default_Aspect_Value
8665 (Entity (Get_Rep_Item (Typ, Name_Default_Value))));
8670 if not Has_Rep_Item (Typ, Name_Discard_Names, False)
8671 and then Has_Rep_Item (Typ, Name_Discard_Names)
8672 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
8673 (Get_Rep_Item (Typ, Name_Discard_Names))
8675 Set_Discard_Names (Typ);
8680 if not Has_Rep_Item (Typ, Name_Invariant, False)
8681 and then Has_Rep_Item (Typ, Name_Invariant)
8682 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
8683 (Get_Rep_Item (Typ, Name_Invariant))
8685 Set_Has_Invariants (Typ);
8687 if Class_Present (Get_Rep_Item (Typ, Name_Invariant)) then
8688 Set_Has_Inheritable_Invariants (Typ);
8694 if not Has_Rep_Item (Typ, Name_Volatile, False)
8695 and then Has_Rep_Item (Typ, Name_Volatile)
8696 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
8697 (Get_Rep_Item (Typ, Name_Volatile))
8699 Set_Treat_As_Volatile (Typ);
8700 Set_Is_Volatile (Typ);
8703 -- Inheritance for derived types only
8705 if Is_Derived_Type (Typ) then
8707 Bas_Typ : constant Entity_Id := Base_Type (Typ);
8708 Imp_Bas_Typ : constant Entity_Id := Implementation_Base_Type (Typ);
8711 -- Atomic_Components
8713 if not Has_Rep_Item (Typ, Name_Atomic_Components, False)
8714 and then Has_Rep_Item (Typ, Name_Atomic_Components)
8715 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
8716 (Get_Rep_Item (Typ, Name_Atomic_Components))
8718 Set_Has_Atomic_Components (Imp_Bas_Typ);
8721 -- Volatile_Components
8723 if not Has_Rep_Item (Typ, Name_Volatile_Components, False)
8724 and then Has_Rep_Item (Typ, Name_Volatile_Components)
8725 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
8726 (Get_Rep_Item (Typ, Name_Volatile_Components))
8728 Set_Has_Volatile_Components (Imp_Bas_Typ);
8731 -- Finalize_Storage_Only.
8733 if not Has_Rep_Pragma (Typ, Name_Finalize_Storage_Only, False)
8734 and then Has_Rep_Pragma (Typ, Name_Finalize_Storage_Only)
8736 Set_Finalize_Storage_Only (Bas_Typ);
8739 -- Universal_Aliasing
8741 if not Has_Rep_Item (Typ, Name_Universal_Aliasing, False)
8742 and then Has_Rep_Item (Typ, Name_Universal_Aliasing)
8743 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
8744 (Get_Rep_Item (Typ, Name_Universal_Aliasing))
8746 Set_Universal_Aliasing (Imp_Bas_Typ);
8749 -- Record type specific aspects
8751 if Is_Record_Type (Typ) then
8755 if not Has_Rep_Item (Typ, Name_Bit_Order, False)
8756 and then Has_Rep_Item (Typ, Name_Bit_Order)
8758 Set_Reverse_Bit_Order (Bas_Typ,
8759 Reverse_Bit_Order (Entity (Name
8760 (Get_Rep_Item (Typ, Name_Bit_Order)))));
8763 -- Scalar_Storage_Order
8765 if not Has_Rep_Item (Typ, Name_Scalar_Storage_Order, False)
8766 and then Has_Rep_Item (Typ, Name_Scalar_Storage_Order)
8768 Set_Reverse_Storage_Order (Bas_Typ,
8769 Reverse_Storage_Order (Entity (Name
8770 (Get_Rep_Item (Typ, Name_Scalar_Storage_Order)))));
8775 end Inherit_Aspects_At_Freeze_Point;
8781 procedure Initialize is
8783 Address_Clause_Checks.Init;
8784 Independence_Checks.Init;
8785 Unchecked_Conversions.Init;
8788 -------------------------
8789 -- Is_Operational_Item --
8790 -------------------------
8792 function Is_Operational_Item (N : Node_Id) return Boolean is
8794 if Nkind (N) /= N_Attribute_Definition_Clause then
8799 Id : constant Attribute_Id := Get_Attribute_Id (Chars (N));
8801 return Id = Attribute_Input
8802 or else Id = Attribute_Output
8803 or else Id = Attribute_Read
8804 or else Id = Attribute_Write
8805 or else Id = Attribute_External_Tag;
8808 end Is_Operational_Item;
8814 function Minimum_Size
8816 Biased : Boolean := False) return Nat
8818 Lo : Uint := No_Uint;
8819 Hi : Uint := No_Uint;
8820 LoR : Ureal := No_Ureal;
8821 HiR : Ureal := No_Ureal;
8822 LoSet : Boolean := False;
8823 HiSet : Boolean := False;
8827 R_Typ : constant Entity_Id := Root_Type (T);
8830 -- If bad type, return 0
8832 if T = Any_Type then
8835 -- For generic types, just return zero. There cannot be any legitimate
8836 -- need to know such a size, but this routine may be called with a
8837 -- generic type as part of normal processing.
8839 elsif Is_Generic_Type (R_Typ)
8840 or else R_Typ = Any_Type
8844 -- Access types. Normally an access type cannot have a size smaller
8845 -- than the size of System.Address. The exception is on VMS, where
8846 -- we have short and long addresses, and it is possible for an access
8847 -- type to have a short address size (and thus be less than the size
8848 -- of System.Address itself). We simply skip the check for VMS, and
8849 -- leave it to the back end to do the check.
8851 elsif Is_Access_Type (T) then
8852 if OpenVMS_On_Target then
8855 return System_Address_Size;
8858 -- Floating-point types
8860 elsif Is_Floating_Point_Type (T) then
8861 return UI_To_Int (Esize (R_Typ));
8865 elsif Is_Discrete_Type (T) then
8867 -- The following loop is looking for the nearest compile time known
8868 -- bounds following the ancestor subtype chain. The idea is to find
8869 -- the most restrictive known bounds information.
8873 if Ancest = Any_Type or else Etype (Ancest) = Any_Type then
8878 if Compile_Time_Known_Value (Type_Low_Bound (Ancest)) then
8879 Lo := Expr_Rep_Value (Type_Low_Bound (Ancest));
8886 if Compile_Time_Known_Value (Type_High_Bound (Ancest)) then
8887 Hi := Expr_Rep_Value (Type_High_Bound (Ancest));
8893 Ancest := Ancestor_Subtype (Ancest);
8896 Ancest := Base_Type (T);
8898 if Is_Generic_Type (Ancest) then
8904 -- Fixed-point types. We can't simply use Expr_Value to get the
8905 -- Corresponding_Integer_Value values of the bounds, since these do not
8906 -- get set till the type is frozen, and this routine can be called
8907 -- before the type is frozen. Similarly the test for bounds being static
8908 -- needs to include the case where we have unanalyzed real literals for
8911 elsif Is_Fixed_Point_Type (T) then
8913 -- The following loop is looking for the nearest compile time known
8914 -- bounds following the ancestor subtype chain. The idea is to find
8915 -- the most restrictive known bounds information.
8919 if Ancest = Any_Type or else Etype (Ancest) = Any_Type then
8923 -- Note: In the following two tests for LoSet and HiSet, it may
8924 -- seem redundant to test for N_Real_Literal here since normally
8925 -- one would assume that the test for the value being known at
8926 -- compile time includes this case. However, there is a glitch.
8927 -- If the real literal comes from folding a non-static expression,
8928 -- then we don't consider any non- static expression to be known
8929 -- at compile time if we are in configurable run time mode (needed
8930 -- in some cases to give a clearer definition of what is and what
8931 -- is not accepted). So the test is indeed needed. Without it, we
8932 -- would set neither Lo_Set nor Hi_Set and get an infinite loop.
8935 if Nkind (Type_Low_Bound (Ancest)) = N_Real_Literal
8936 or else Compile_Time_Known_Value (Type_Low_Bound (Ancest))
8938 LoR := Expr_Value_R (Type_Low_Bound (Ancest));
8945 if Nkind (Type_High_Bound (Ancest)) = N_Real_Literal
8946 or else Compile_Time_Known_Value (Type_High_Bound (Ancest))
8948 HiR := Expr_Value_R (Type_High_Bound (Ancest));
8954 Ancest := Ancestor_Subtype (Ancest);
8957 Ancest := Base_Type (T);
8959 if Is_Generic_Type (Ancest) then
8965 Lo := UR_To_Uint (LoR / Small_Value (T));
8966 Hi := UR_To_Uint (HiR / Small_Value (T));
8968 -- No other types allowed
8971 raise Program_Error;
8974 -- Fall through with Hi and Lo set. Deal with biased case
8977 and then not Is_Fixed_Point_Type (T)
8978 and then not (Is_Enumeration_Type (T)
8979 and then Has_Non_Standard_Rep (T)))
8980 or else Has_Biased_Representation (T)
8986 -- Signed case. Note that we consider types like range 1 .. -1 to be
8987 -- signed for the purpose of computing the size, since the bounds have
8988 -- to be accommodated in the base type.
8990 if Lo < 0 or else Hi < 0 then
8994 -- S = size, B = 2 ** (size - 1) (can accommodate -B .. +(B - 1))
8995 -- Note that we accommodate the case where the bounds cross. This
8996 -- can happen either because of the way the bounds are declared
8997 -- or because of the algorithm in Freeze_Fixed_Point_Type.
9011 -- If both bounds are positive, make sure that both are represen-
9012 -- table in the case where the bounds are crossed. This can happen
9013 -- either because of the way the bounds are declared, or because of
9014 -- the algorithm in Freeze_Fixed_Point_Type.
9020 -- S = size, (can accommodate 0 .. (2**size - 1))
9023 while Hi >= Uint_2 ** S loop
9031 ---------------------------
9032 -- New_Stream_Subprogram --
9033 ---------------------------
9035 procedure New_Stream_Subprogram
9039 Nam : TSS_Name_Type)
9041 Loc : constant Source_Ptr := Sloc (N);
9042 Sname : constant Name_Id := Make_TSS_Name (Base_Type (Ent), Nam);
9043 Subp_Id : Entity_Id;
9044 Subp_Decl : Node_Id;
9048 Defer_Declaration : constant Boolean :=
9049 Is_Tagged_Type (Ent) or else Is_Private_Type (Ent);
9050 -- For a tagged type, there is a declaration for each stream attribute
9051 -- at the freeze point, and we must generate only a completion of this
9052 -- declaration. We do the same for private types, because the full view
9053 -- might be tagged. Otherwise we generate a declaration at the point of
9054 -- the attribute definition clause.
9056 function Build_Spec return Node_Id;
9057 -- Used for declaration and renaming declaration, so that this is
9058 -- treated as a renaming_as_body.
9064 function Build_Spec return Node_Id is
9065 Out_P : constant Boolean := (Nam = TSS_Stream_Read);
9068 T_Ref : constant Node_Id := New_Reference_To (Etyp, Loc);
9071 Subp_Id := Make_Defining_Identifier (Loc, Sname);
9073 -- S : access Root_Stream_Type'Class
9075 Formals := New_List (
9076 Make_Parameter_Specification (Loc,
9077 Defining_Identifier =>
9078 Make_Defining_Identifier (Loc, Name_S),
9080 Make_Access_Definition (Loc,
9083 Designated_Type (Etype (F)), Loc))));
9085 if Nam = TSS_Stream_Input then
9087 Make_Function_Specification (Loc,
9088 Defining_Unit_Name => Subp_Id,
9089 Parameter_Specifications => Formals,
9090 Result_Definition => T_Ref);
9095 Make_Parameter_Specification (Loc,
9096 Defining_Identifier => Make_Defining_Identifier (Loc, Name_V),
9097 Out_Present => Out_P,
9098 Parameter_Type => T_Ref));
9101 Make_Procedure_Specification (Loc,
9102 Defining_Unit_Name => Subp_Id,
9103 Parameter_Specifications => Formals);
9109 -- Start of processing for New_Stream_Subprogram
9112 F := First_Formal (Subp);
9114 if Ekind (Subp) = E_Procedure then
9115 Etyp := Etype (Next_Formal (F));
9117 Etyp := Etype (Subp);
9120 -- Prepare subprogram declaration and insert it as an action on the
9121 -- clause node. The visibility for this entity is used to test for
9122 -- visibility of the attribute definition clause (in the sense of
9123 -- 8.3(23) as amended by AI-195).
9125 if not Defer_Declaration then
9127 Make_Subprogram_Declaration (Loc,
9128 Specification => Build_Spec);
9130 -- For a tagged type, there is always a visible declaration for each
9131 -- stream TSS (it is a predefined primitive operation), and the
9132 -- completion of this declaration occurs at the freeze point, which is
9133 -- not always visible at places where the attribute definition clause is
9134 -- visible. So, we create a dummy entity here for the purpose of
9135 -- tracking the visibility of the attribute definition clause itself.
9139 Make_Defining_Identifier (Loc, New_External_Name (Sname, 'V'));
9141 Make_Object_Declaration (Loc,
9142 Defining_Identifier => Subp_Id,
9143 Object_Definition => New_Occurrence_Of (Standard_Boolean, Loc));
9146 Insert_Action (N, Subp_Decl);
9147 Set_Entity (N, Subp_Id);
9150 Make_Subprogram_Renaming_Declaration (Loc,
9151 Specification => Build_Spec,
9152 Name => New_Reference_To (Subp, Loc));
9154 if Defer_Declaration then
9155 Set_TSS (Base_Type (Ent), Subp_Id);
9157 Insert_Action (N, Subp_Decl);
9158 Copy_TSS (Subp_Id, Base_Type (Ent));
9160 end New_Stream_Subprogram;
9162 ------------------------
9163 -- Rep_Item_Too_Early --
9164 ------------------------
9166 function Rep_Item_Too_Early (T : Entity_Id; N : Node_Id) return Boolean is
9168 -- Cannot apply non-operational rep items to generic types
9170 if Is_Operational_Item (N) then
9174 and then Is_Generic_Type (Root_Type (T))
9176 Error_Msg_N ("representation item not allowed for generic type", N);
9180 -- Otherwise check for incomplete type
9182 if Is_Incomplete_Or_Private_Type (T)
9183 and then No (Underlying_Type (T))
9185 (Nkind (N) /= N_Pragma
9186 or else Get_Pragma_Id (N) /= Pragma_Import)
9189 ("representation item must be after full type declaration", N);
9192 -- If the type has incomplete components, a representation clause is
9193 -- illegal but stream attributes and Convention pragmas are correct.
9195 elsif Has_Private_Component (T) then
9196 if Nkind (N) = N_Pragma then
9201 ("representation item must appear after type is fully defined",
9208 end Rep_Item_Too_Early;
9210 -----------------------
9211 -- Rep_Item_Too_Late --
9212 -----------------------
9214 function Rep_Item_Too_Late
9217 FOnly : Boolean := False) return Boolean
9220 Parent_Type : Entity_Id;
9223 -- Output the too late message. Note that this is not considered a
9224 -- serious error, since the effect is simply that we ignore the
9225 -- representation clause in this case.
9231 procedure Too_Late is
9233 -- Other compilers seem more relaxed about rep items appearing too
9234 -- late. Since analysis tools typically don't care about rep items
9235 -- anyway, no reason to be too strict about this.
9237 if not Relaxed_RM_Semantics then
9238 Error_Msg_N ("|representation item appears too late!", N);
9242 -- Start of processing for Rep_Item_Too_Late
9245 -- First make sure entity is not frozen (RM 13.1(9))
9249 -- Exclude imported types, which may be frozen if they appear in a
9250 -- representation clause for a local type.
9252 and then not From_With_Type (T)
9254 -- Exclude generated entities (not coming from source). The common
9255 -- case is when we generate a renaming which prematurely freezes the
9256 -- renamed internal entity, but we still want to be able to set copies
9257 -- of attribute values such as Size/Alignment.
9259 and then Comes_From_Source (T)
9262 S := First_Subtype (T);
9264 if Present (Freeze_Node (S)) then
9266 ("??no more representation items for }", Freeze_Node (S), S);
9271 -- Check for case of non-tagged derived type whose parent either has
9272 -- primitive operations, or is a by reference type (RM 13.1(10)).
9276 and then Is_Derived_Type (T)
9277 and then not Is_Tagged_Type (T)
9279 Parent_Type := Etype (Base_Type (T));
9281 if Has_Primitive_Operations (Parent_Type) then
9284 ("primitive operations already defined for&!", N, Parent_Type);
9287 elsif Is_By_Reference_Type (Parent_Type) then
9290 ("parent type & is a by reference type!", N, Parent_Type);
9295 -- No error, link item into head of chain of rep items for the entity,
9296 -- but avoid chaining if we have an overloadable entity, and the pragma
9297 -- is one that can apply to multiple overloaded entities.
9299 if Is_Overloadable (T) and then Nkind (N) = N_Pragma then
9301 Pname : constant Name_Id := Pragma_Name (N);
9303 if Pname = Name_Convention or else
9304 Pname = Name_Import or else
9305 Pname = Name_Export or else
9306 Pname = Name_External or else
9307 Pname = Name_Interface
9314 Record_Rep_Item (T, N);
9316 end Rep_Item_Too_Late;
9318 -------------------------------------
9319 -- Replace_Type_References_Generic --
9320 -------------------------------------
9322 procedure Replace_Type_References_Generic (N : Node_Id; TName : Name_Id) is
9324 function Replace_Node (N : Node_Id) return Traverse_Result;
9325 -- Processes a single node in the traversal procedure below, checking
9326 -- if node N should be replaced, and if so, doing the replacement.
9328 procedure Replace_Type_Refs is new Traverse_Proc (Replace_Node);
9329 -- This instantiation provides the body of Replace_Type_References
9335 function Replace_Node (N : Node_Id) return Traverse_Result is
9340 -- Case of identifier
9342 if Nkind (N) = N_Identifier then
9344 -- If not the type name, all done with this node
9346 if Chars (N) /= TName then
9349 -- Otherwise do the replacement and we are done with this node
9352 Replace_Type_Reference (N);
9356 -- Case of selected component (which is what a qualification
9357 -- looks like in the unanalyzed tree, which is what we have.
9359 elsif Nkind (N) = N_Selected_Component then
9361 -- If selector name is not our type, keeping going (we might
9362 -- still have an occurrence of the type in the prefix).
9364 if Nkind (Selector_Name (N)) /= N_Identifier
9365 or else Chars (Selector_Name (N)) /= TName
9369 -- Selector name is our type, check qualification
9372 -- Loop through scopes and prefixes, doing comparison
9377 -- Continue if no more scopes or scope with no name
9379 if No (S) or else Nkind (S) not in N_Has_Chars then
9383 -- Do replace if prefix is an identifier matching the
9384 -- scope that we are currently looking at.
9386 if Nkind (P) = N_Identifier
9387 and then Chars (P) = Chars (S)
9389 Replace_Type_Reference (N);
9393 -- Go check scope above us if prefix is itself of the
9394 -- form of a selected component, whose selector matches
9395 -- the scope we are currently looking at.
9397 if Nkind (P) = N_Selected_Component
9398 and then Nkind (Selector_Name (P)) = N_Identifier
9399 and then Chars (Selector_Name (P)) = Chars (S)
9404 -- For anything else, we don't have a match, so keep on
9405 -- going, there are still some weird cases where we may
9406 -- still have a replacement within the prefix.
9414 -- Continue for any other node kind
9422 Replace_Type_Refs (N);
9423 end Replace_Type_References_Generic;
9425 -------------------------
9426 -- Same_Representation --
9427 -------------------------
9429 function Same_Representation (Typ1, Typ2 : Entity_Id) return Boolean is
9430 T1 : constant Entity_Id := Underlying_Type (Typ1);
9431 T2 : constant Entity_Id := Underlying_Type (Typ2);
9434 -- A quick check, if base types are the same, then we definitely have
9435 -- the same representation, because the subtype specific representation
9436 -- attributes (Size and Alignment) do not affect representation from
9437 -- the point of view of this test.
9439 if Base_Type (T1) = Base_Type (T2) then
9442 elsif Is_Private_Type (Base_Type (T2))
9443 and then Base_Type (T1) = Full_View (Base_Type (T2))
9448 -- Tagged types never have differing representations
9450 if Is_Tagged_Type (T1) then
9454 -- Representations are definitely different if conventions differ
9456 if Convention (T1) /= Convention (T2) then
9460 -- Representations are different if component alignments or scalar
9461 -- storage orders differ.
9463 if (Is_Record_Type (T1) or else Is_Array_Type (T1))
9465 (Is_Record_Type (T2) or else Is_Array_Type (T2))
9467 (Component_Alignment (T1) /= Component_Alignment (T2)
9469 Reverse_Storage_Order (T1) /= Reverse_Storage_Order (T2))
9474 -- For arrays, the only real issue is component size. If we know the
9475 -- component size for both arrays, and it is the same, then that's
9476 -- good enough to know we don't have a change of representation.
9478 if Is_Array_Type (T1) then
9479 if Known_Component_Size (T1)
9480 and then Known_Component_Size (T2)
9481 and then Component_Size (T1) = Component_Size (T2)
9483 if VM_Target = No_VM then
9486 -- In VM targets the representation of arrays with aliased
9487 -- components differs from arrays with non-aliased components
9490 return Has_Aliased_Components (Base_Type (T1))
9492 Has_Aliased_Components (Base_Type (T2));
9497 -- Types definitely have same representation if neither has non-standard
9498 -- representation since default representations are always consistent.
9499 -- If only one has non-standard representation, and the other does not,
9500 -- then we consider that they do not have the same representation. They
9501 -- might, but there is no way of telling early enough.
9503 if Has_Non_Standard_Rep (T1) then
9504 if not Has_Non_Standard_Rep (T2) then
9508 return not Has_Non_Standard_Rep (T2);
9511 -- Here the two types both have non-standard representation, and we need
9512 -- to determine if they have the same non-standard representation.
9514 -- For arrays, we simply need to test if the component sizes are the
9515 -- same. Pragma Pack is reflected in modified component sizes, so this
9516 -- check also deals with pragma Pack.
9518 if Is_Array_Type (T1) then
9519 return Component_Size (T1) = Component_Size (T2);
9521 -- Tagged types always have the same representation, because it is not
9522 -- possible to specify different representations for common fields.
9524 elsif Is_Tagged_Type (T1) then
9527 -- Case of record types
9529 elsif Is_Record_Type (T1) then
9531 -- Packed status must conform
9533 if Is_Packed (T1) /= Is_Packed (T2) then
9536 -- Otherwise we must check components. Typ2 maybe a constrained
9537 -- subtype with fewer components, so we compare the components
9538 -- of the base types.
9541 Record_Case : declare
9542 CD1, CD2 : Entity_Id;
9544 function Same_Rep return Boolean;
9545 -- CD1 and CD2 are either components or discriminants. This
9546 -- function tests whether they have the same representation.
9552 function Same_Rep return Boolean is
9554 if No (Component_Clause (CD1)) then
9555 return No (Component_Clause (CD2));
9557 -- Note: at this point, component clauses have been
9558 -- normalized to the default bit order, so that the
9559 -- comparison of Component_Bit_Offsets is meaningful.
9562 Present (Component_Clause (CD2))
9564 Component_Bit_Offset (CD1) = Component_Bit_Offset (CD2)
9566 Esize (CD1) = Esize (CD2);
9570 -- Start of processing for Record_Case
9573 if Has_Discriminants (T1) then
9575 -- The number of discriminants may be different if the
9576 -- derived type has fewer (constrained by values). The
9577 -- invisible discriminants retain the representation of
9578 -- the original, so the discrepancy does not per se
9579 -- indicate a different representation.
9581 CD1 := First_Discriminant (T1);
9582 CD2 := First_Discriminant (T2);
9583 while Present (CD1) and then Present (CD2) loop
9584 if not Same_Rep then
9587 Next_Discriminant (CD1);
9588 Next_Discriminant (CD2);
9593 CD1 := First_Component (Underlying_Type (Base_Type (T1)));
9594 CD2 := First_Component (Underlying_Type (Base_Type (T2)));
9595 while Present (CD1) loop
9596 if not Same_Rep then
9599 Next_Component (CD1);
9600 Next_Component (CD2);
9608 -- For enumeration types, we must check each literal to see if the
9609 -- representation is the same. Note that we do not permit enumeration
9610 -- representation clauses for Character and Wide_Character, so these
9611 -- cases were already dealt with.
9613 elsif Is_Enumeration_Type (T1) then
9614 Enumeration_Case : declare
9618 L1 := First_Literal (T1);
9619 L2 := First_Literal (T2);
9620 while Present (L1) loop
9621 if Enumeration_Rep (L1) /= Enumeration_Rep (L2) then
9630 end Enumeration_Case;
9632 -- Any other types have the same representation for these purposes
9637 end Same_Representation;
9643 procedure Set_Biased
9647 Biased : Boolean := True)
9651 Set_Has_Biased_Representation (E);
9653 if Warn_On_Biased_Representation then
9655 ("?B?" & Msg & " forces biased representation for&", N, E);
9660 --------------------
9661 -- Set_Enum_Esize --
9662 --------------------
9664 procedure Set_Enum_Esize (T : Entity_Id) is
9672 -- Find the minimum standard size (8,16,32,64) that fits
9674 Lo := Enumeration_Rep (Entity (Type_Low_Bound (T)));
9675 Hi := Enumeration_Rep (Entity (Type_High_Bound (T)));
9678 if Lo >= -Uint_2**07 and then Hi < Uint_2**07 then
9679 Sz := Standard_Character_Size; -- May be > 8 on some targets
9681 elsif Lo >= -Uint_2**15 and then Hi < Uint_2**15 then
9684 elsif Lo >= -Uint_2**31 and then Hi < Uint_2**31 then
9687 else pragma Assert (Lo >= -Uint_2**63 and then Hi < Uint_2**63);
9692 if Hi < Uint_2**08 then
9693 Sz := Standard_Character_Size; -- May be > 8 on some targets
9695 elsif Hi < Uint_2**16 then
9698 elsif Hi < Uint_2**32 then
9701 else pragma Assert (Hi < Uint_2**63);
9706 -- That minimum is the proper size unless we have a foreign convention
9707 -- and the size required is 32 or less, in which case we bump the size
9708 -- up to 32. This is required for C and C++ and seems reasonable for
9709 -- all other foreign conventions.
9711 if Has_Foreign_Convention (T)
9712 and then Esize (T) < Standard_Integer_Size
9714 Init_Esize (T, Standard_Integer_Size);
9720 ------------------------------
9721 -- Validate_Address_Clauses --
9722 ------------------------------
9724 procedure Validate_Address_Clauses is
9726 for J in Address_Clause_Checks.First .. Address_Clause_Checks.Last loop
9728 ACCR : Address_Clause_Check_Record
9729 renames Address_Clause_Checks.Table (J);
9740 -- Skip processing of this entry if warning already posted
9742 if not Address_Warning_Posted (ACCR.N) then
9743 Expr := Original_Node (Expression (ACCR.N));
9747 X_Alignment := Alignment (ACCR.X);
9748 Y_Alignment := Alignment (ACCR.Y);
9750 -- Similarly obtain sizes
9752 X_Size := Esize (ACCR.X);
9753 Y_Size := Esize (ACCR.Y);
9755 -- Check for large object overlaying smaller one
9758 and then X_Size > Uint_0
9759 and then X_Size > Y_Size
9762 ("?& overlays smaller object", ACCR.N, ACCR.X);
9764 ("\??program execution may be erroneous", ACCR.N);
9765 Error_Msg_Uint_1 := X_Size;
9767 ("\??size of & is ^", ACCR.N, ACCR.X);
9768 Error_Msg_Uint_1 := Y_Size;
9770 ("\??size of & is ^", ACCR.N, ACCR.Y);
9772 -- Check for inadequate alignment, both of the base object
9773 -- and of the offset, if any.
9775 -- Note: we do not check the alignment if we gave a size
9776 -- warning, since it would likely be redundant.
9778 elsif Y_Alignment /= Uint_0
9779 and then (Y_Alignment < X_Alignment
9782 Nkind (Expr) = N_Attribute_Reference
9784 Attribute_Name (Expr) = Name_Address
9786 Has_Compatible_Alignment
9787 (ACCR.X, Prefix (Expr))
9788 /= Known_Compatible))
9791 ("??specified address for& may be inconsistent "
9792 & "with alignment", ACCR.N, ACCR.X);
9794 ("\??program execution may be erroneous (RM 13.3(27))",
9796 Error_Msg_Uint_1 := X_Alignment;
9798 ("\??alignment of & is ^", ACCR.N, ACCR.X);
9799 Error_Msg_Uint_1 := Y_Alignment;
9801 ("\??alignment of & is ^", ACCR.N, ACCR.Y);
9802 if Y_Alignment >= X_Alignment then
9804 ("\??but offset is not multiple of alignment", ACCR.N);
9810 end Validate_Address_Clauses;
9812 ---------------------------
9813 -- Validate_Independence --
9814 ---------------------------
9816 procedure Validate_Independence is
9817 SU : constant Uint := UI_From_Int (System_Storage_Unit);
9825 procedure Check_Array_Type (Atyp : Entity_Id);
9826 -- Checks if the array type Atyp has independent components, and
9827 -- if not, outputs an appropriate set of error messages.
9829 procedure No_Independence;
9830 -- Output message that independence cannot be guaranteed
9832 function OK_Component (C : Entity_Id) return Boolean;
9833 -- Checks one component to see if it is independently accessible, and
9834 -- if so yields True, otherwise yields False if independent access
9835 -- cannot be guaranteed. This is a conservative routine, it only
9836 -- returns True if it knows for sure, it returns False if it knows
9837 -- there is a problem, or it cannot be sure there is no problem.
9839 procedure Reason_Bad_Component (C : Entity_Id);
9840 -- Outputs continuation message if a reason can be determined for
9841 -- the component C being bad.
9843 ----------------------
9844 -- Check_Array_Type --
9845 ----------------------
9847 procedure Check_Array_Type (Atyp : Entity_Id) is
9848 Ctyp : constant Entity_Id := Component_Type (Atyp);
9851 -- OK if no alignment clause, no pack, and no component size
9853 if not Has_Component_Size_Clause (Atyp)
9854 and then not Has_Alignment_Clause (Atyp)
9855 and then not Is_Packed (Atyp)
9860 -- Check actual component size
9862 if not Known_Component_Size (Atyp)
9863 or else not (Addressable (Component_Size (Atyp))
9864 and then Component_Size (Atyp) < 64)
9865 or else Component_Size (Atyp) mod Esize (Ctyp) /= 0
9869 -- Bad component size, check reason
9871 if Has_Component_Size_Clause (Atyp) then
9872 P := Get_Attribute_Definition_Clause
9873 (Atyp, Attribute_Component_Size);
9876 Error_Msg_Sloc := Sloc (P);
9877 Error_Msg_N ("\because of Component_Size clause#", N);
9882 if Is_Packed (Atyp) then
9883 P := Get_Rep_Pragma (Atyp, Name_Pack);
9886 Error_Msg_Sloc := Sloc (P);
9887 Error_Msg_N ("\because of pragma Pack#", N);
9892 -- No reason found, just return
9897 -- Array type is OK independence-wise
9900 end Check_Array_Type;
9902 ---------------------
9903 -- No_Independence --
9904 ---------------------
9906 procedure No_Independence is
9908 if Pragma_Name (N) = Name_Independent then
9910 ("independence cannot be guaranteed for&", N, E);
9913 ("independent components cannot be guaranteed for&", N, E);
9915 end No_Independence;
9921 function OK_Component (C : Entity_Id) return Boolean is
9922 Rec : constant Entity_Id := Scope (C);
9923 Ctyp : constant Entity_Id := Etype (C);
9926 -- OK if no component clause, no Pack, and no alignment clause
9928 if No (Component_Clause (C))
9929 and then not Is_Packed (Rec)
9930 and then not Has_Alignment_Clause (Rec)
9935 -- Here we look at the actual component layout. A component is
9936 -- addressable if its size is a multiple of the Esize of the
9937 -- component type, and its starting position in the record has
9938 -- appropriate alignment, and the record itself has appropriate
9939 -- alignment to guarantee the component alignment.
9941 -- Make sure sizes are static, always assume the worst for any
9942 -- cases where we cannot check static values.
9944 if not (Known_Static_Esize (C)
9946 Known_Static_Esize (Ctyp))
9951 -- Size of component must be addressable or greater than 64 bits
9952 -- and a multiple of bytes.
9954 if not Addressable (Esize (C)) and then Esize (C) < Uint_64 then
9958 -- Check size is proper multiple
9960 if Esize (C) mod Esize (Ctyp) /= 0 then
9964 -- Check alignment of component is OK
9966 if not Known_Component_Bit_Offset (C)
9967 or else Component_Bit_Offset (C) < Uint_0
9968 or else Component_Bit_Offset (C) mod Esize (Ctyp) /= 0
9973 -- Check alignment of record type is OK
9975 if not Known_Alignment (Rec)
9976 or else (Alignment (Rec) * SU) mod Esize (Ctyp) /= 0
9981 -- All tests passed, component is addressable
9986 --------------------------
9987 -- Reason_Bad_Component --
9988 --------------------------
9990 procedure Reason_Bad_Component (C : Entity_Id) is
9991 Rec : constant Entity_Id := Scope (C);
9992 Ctyp : constant Entity_Id := Etype (C);
9995 -- If component clause present assume that's the problem
9997 if Present (Component_Clause (C)) then
9998 Error_Msg_Sloc := Sloc (Component_Clause (C));
9999 Error_Msg_N ("\because of Component_Clause#", N);
10003 -- If pragma Pack clause present, assume that's the problem
10005 if Is_Packed (Rec) then
10006 P := Get_Rep_Pragma (Rec, Name_Pack);
10008 if Present (P) then
10009 Error_Msg_Sloc := Sloc (P);
10010 Error_Msg_N ("\because of pragma Pack#", N);
10015 -- See if record has bad alignment clause
10017 if Has_Alignment_Clause (Rec)
10018 and then Known_Alignment (Rec)
10019 and then (Alignment (Rec) * SU) mod Esize (Ctyp) /= 0
10021 P := Get_Attribute_Definition_Clause (Rec, Attribute_Alignment);
10023 if Present (P) then
10024 Error_Msg_Sloc := Sloc (P);
10025 Error_Msg_N ("\because of Alignment clause#", N);
10029 -- Couldn't find a reason, so return without a message
10032 end Reason_Bad_Component;
10034 -- Start of processing for Validate_Independence
10037 for J in Independence_Checks.First .. Independence_Checks.Last loop
10038 N := Independence_Checks.Table (J).N;
10039 E := Independence_Checks.Table (J).E;
10040 IC := Pragma_Name (N) = Name_Independent_Components;
10042 -- Deal with component case
10044 if Ekind (E) = E_Discriminant or else Ekind (E) = E_Component then
10045 if not OK_Component (E) then
10047 Reason_Bad_Component (E);
10052 -- Deal with record with Independent_Components
10054 if IC and then Is_Record_Type (E) then
10055 Comp := First_Component_Or_Discriminant (E);
10056 while Present (Comp) loop
10057 if not OK_Component (Comp) then
10059 Reason_Bad_Component (Comp);
10063 Next_Component_Or_Discriminant (Comp);
10067 -- Deal with address clause case
10069 if Is_Object (E) then
10070 Addr := Address_Clause (E);
10072 if Present (Addr) then
10074 Error_Msg_Sloc := Sloc (Addr);
10075 Error_Msg_N ("\because of Address clause#", N);
10080 -- Deal with independent components for array type
10082 if IC and then Is_Array_Type (E) then
10083 Check_Array_Type (E);
10086 -- Deal with independent components for array object
10088 if IC and then Is_Object (E) and then Is_Array_Type (Etype (E)) then
10089 Check_Array_Type (Etype (E));
10094 end Validate_Independence;
10096 -----------------------------------
10097 -- Validate_Unchecked_Conversion --
10098 -----------------------------------
10100 procedure Validate_Unchecked_Conversion
10102 Act_Unit : Entity_Id)
10104 Source : Entity_Id;
10105 Target : Entity_Id;
10109 -- Obtain source and target types. Note that we call Ancestor_Subtype
10110 -- here because the processing for generic instantiation always makes
10111 -- subtypes, and we want the original frozen actual types.
10113 -- If we are dealing with private types, then do the check on their
10114 -- fully declared counterparts if the full declarations have been
10115 -- encountered (they don't have to be visible, but they must exist!)
10117 Source := Ancestor_Subtype (Etype (First_Formal (Act_Unit)));
10119 if Is_Private_Type (Source)
10120 and then Present (Underlying_Type (Source))
10122 Source := Underlying_Type (Source);
10125 Target := Ancestor_Subtype (Etype (Act_Unit));
10127 -- If either type is generic, the instantiation happens within a generic
10128 -- unit, and there is nothing to check. The proper check will happen
10129 -- when the enclosing generic is instantiated.
10131 if Is_Generic_Type (Source) or else Is_Generic_Type (Target) then
10135 if Is_Private_Type (Target)
10136 and then Present (Underlying_Type (Target))
10138 Target := Underlying_Type (Target);
10141 -- Source may be unconstrained array, but not target
10143 if Is_Array_Type (Target) and then not Is_Constrained (Target) then
10145 ("unchecked conversion to unconstrained array not allowed", N);
10149 -- Warn if conversion between two different convention pointers
10151 if Is_Access_Type (Target)
10152 and then Is_Access_Type (Source)
10153 and then Convention (Target) /= Convention (Source)
10154 and then Warn_On_Unchecked_Conversion
10156 -- Give warnings for subprogram pointers only on most targets. The
10157 -- exception is VMS, where data pointers can have different lengths
10158 -- depending on the pointer convention.
10160 if Is_Access_Subprogram_Type (Target)
10161 or else Is_Access_Subprogram_Type (Source)
10162 or else OpenVMS_On_Target
10165 ("?z?conversion between pointers with different conventions!",
10170 -- Warn if one of the operands is Ada.Calendar.Time. Do not emit a
10171 -- warning when compiling GNAT-related sources.
10173 if Warn_On_Unchecked_Conversion
10174 and then not In_Predefined_Unit (N)
10175 and then RTU_Loaded (Ada_Calendar)
10177 (Chars (Source) = Name_Time
10179 Chars (Target) = Name_Time)
10181 -- If Ada.Calendar is loaded and the name of one of the operands is
10182 -- Time, there is a good chance that this is Ada.Calendar.Time.
10185 Calendar_Time : constant Entity_Id :=
10186 Full_View (RTE (RO_CA_Time));
10188 pragma Assert (Present (Calendar_Time));
10190 if Source = Calendar_Time or else Target = Calendar_Time then
10192 ("?z?representation of 'Time values may change between " &
10193 "'G'N'A'T versions", N);
10198 -- Make entry in unchecked conversion table for later processing by
10199 -- Validate_Unchecked_Conversions, which will check sizes and alignments
10200 -- (using values set by the back-end where possible). This is only done
10201 -- if the appropriate warning is active.
10203 if Warn_On_Unchecked_Conversion then
10204 Unchecked_Conversions.Append
10205 (New_Val => UC_Entry'(Eloc => Sloc (N),
10207 Target => Target));
10209 -- If both sizes are known statically now, then back end annotation
10210 -- is not required to do a proper check but if either size is not
10211 -- known statically, then we need the annotation.
10213 if Known_Static_RM_Size (Source)
10215 Known_Static_RM_Size (Target)
10219 Back_Annotate_Rep_Info := True;
10223 -- If unchecked conversion to access type, and access type is declared
10224 -- in the same unit as the unchecked conversion, then set the flag
10225 -- No_Strict_Aliasing (no strict aliasing is implicit here)
10227 if Is_Access_Type (Target) and then
10228 In_Same_Source_Unit (Target, N)
10230 Set_No_Strict_Aliasing (Implementation_Base_Type (Target));
10233 -- Generate N_Validate_Unchecked_Conversion node for back end in case
10234 -- the back end needs to perform special validation checks.
10236 -- Shouldn't this be in Exp_Ch13, since the check only gets done if we
10237 -- have full expansion and the back end is called ???
10240 Make_Validate_Unchecked_Conversion (Sloc (N));
10241 Set_Source_Type (Vnode, Source);
10242 Set_Target_Type (Vnode, Target);
10244 -- If the unchecked conversion node is in a list, just insert before it.
10245 -- If not we have some strange case, not worth bothering about.
10247 if Is_List_Member (N) then
10248 Insert_After (N, Vnode);
10250 end Validate_Unchecked_Conversion;
10252 ------------------------------------
10253 -- Validate_Unchecked_Conversions --
10254 ------------------------------------
10256 procedure Validate_Unchecked_Conversions is
10258 for N in Unchecked_Conversions.First .. Unchecked_Conversions.Last loop
10260 T : UC_Entry renames Unchecked_Conversions.Table (N);
10262 Eloc : constant Source_Ptr := T.Eloc;
10263 Source : constant Entity_Id := T.Source;
10264 Target : constant Entity_Id := T.Target;
10270 -- This validation check, which warns if we have unequal sizes for
10271 -- unchecked conversion, and thus potentially implementation
10272 -- dependent semantics, is one of the few occasions on which we
10273 -- use the official RM size instead of Esize. See description in
10274 -- Einfo "Handling of Type'Size Values" for details.
10276 if Serious_Errors_Detected = 0
10277 and then Known_Static_RM_Size (Source)
10278 and then Known_Static_RM_Size (Target)
10280 -- Don't do the check if warnings off for either type, note the
10281 -- deliberate use of OR here instead of OR ELSE to get the flag
10282 -- Warnings_Off_Used set for both types if appropriate.
10284 and then not (Has_Warnings_Off (Source)
10286 Has_Warnings_Off (Target))
10288 Source_Siz := RM_Size (Source);
10289 Target_Siz := RM_Size (Target);
10291 if Source_Siz /= Target_Siz then
10293 ("?z?types for unchecked conversion have different sizes!",
10296 if All_Errors_Mode then
10297 Error_Msg_Name_1 := Chars (Source);
10298 Error_Msg_Uint_1 := Source_Siz;
10299 Error_Msg_Name_2 := Chars (Target);
10300 Error_Msg_Uint_2 := Target_Siz;
10301 Error_Msg ("\size of % is ^, size of % is ^?z?", Eloc);
10303 Error_Msg_Uint_1 := UI_Abs (Source_Siz - Target_Siz);
10305 if Is_Discrete_Type (Source)
10307 Is_Discrete_Type (Target)
10309 if Source_Siz > Target_Siz then
10311 ("\?z?^ high order bits of source will "
10312 & "be ignored!", Eloc);
10314 elsif Is_Unsigned_Type (Source) then
10316 ("\?z?source will be extended with ^ high order "
10317 & "zero bits?!", Eloc);
10321 ("\?z?source will be extended with ^ high order "
10322 & "sign bits!", Eloc);
10325 elsif Source_Siz < Target_Siz then
10326 if Is_Discrete_Type (Target) then
10327 if Bytes_Big_Endian then
10329 ("\?z?target value will include ^ undefined "
10330 & "low order bits!", Eloc);
10333 ("\?z?target value will include ^ undefined "
10334 & "high order bits!", Eloc);
10339 ("\?z?^ trailing bits of target value will be "
10340 & "undefined!", Eloc);
10343 else pragma Assert (Source_Siz > Target_Siz);
10345 ("\?z?^ trailing bits of source will be ignored!",
10352 -- If both types are access types, we need to check the alignment.
10353 -- If the alignment of both is specified, we can do it here.
10355 if Serious_Errors_Detected = 0
10356 and then Ekind (Source) in Access_Kind
10357 and then Ekind (Target) in Access_Kind
10358 and then Target_Strict_Alignment
10359 and then Present (Designated_Type (Source))
10360 and then Present (Designated_Type (Target))
10363 D_Source : constant Entity_Id := Designated_Type (Source);
10364 D_Target : constant Entity_Id := Designated_Type (Target);
10367 if Known_Alignment (D_Source)
10369 Known_Alignment (D_Target)
10372 Source_Align : constant Uint := Alignment (D_Source);
10373 Target_Align : constant Uint := Alignment (D_Target);
10376 if Source_Align < Target_Align
10377 and then not Is_Tagged_Type (D_Source)
10379 -- Suppress warning if warnings suppressed on either
10380 -- type or either designated type. Note the use of
10381 -- OR here instead of OR ELSE. That is intentional,
10382 -- we would like to set flag Warnings_Off_Used in
10383 -- all types for which warnings are suppressed.
10385 and then not (Has_Warnings_Off (D_Source)
10387 Has_Warnings_Off (D_Target)
10389 Has_Warnings_Off (Source)
10391 Has_Warnings_Off (Target))
10393 Error_Msg_Uint_1 := Target_Align;
10394 Error_Msg_Uint_2 := Source_Align;
10395 Error_Msg_Node_1 := D_Target;
10396 Error_Msg_Node_2 := D_Source;
10398 ("?z?alignment of & (^) is stricter than "
10399 & "alignment of & (^)!", Eloc);
10401 ("\?z?resulting access value may have invalid "
10402 & "alignment!", Eloc);
10410 end Validate_Unchecked_Conversions;