1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2012, Free Software Foundation, Inc. --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING3. If not, go to --
19 -- http://www.gnu.org/licenses for a complete copy of the license. --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
24 ------------------------------------------------------------------------------
26 with Aspects; use Aspects;
27 with Atree; use Atree;
28 with Checks; use Checks;
29 with Einfo; use Einfo;
30 with Elists; use Elists;
31 with Errout; use Errout;
32 with Exp_Disp; use Exp_Disp;
33 with Exp_Tss; use Exp_Tss;
34 with Exp_Util; use Exp_Util;
36 with Lib.Xref; use Lib.Xref;
37 with Namet; use Namet;
38 with Nlists; use Nlists;
39 with Nmake; use Nmake;
41 with Restrict; use Restrict;
42 with Rident; use Rident;
43 with Rtsfind; use Rtsfind;
45 with Sem_Aux; use Sem_Aux;
46 with Sem_Ch3; use Sem_Ch3;
47 with Sem_Ch6; use Sem_Ch6;
48 with Sem_Ch8; use Sem_Ch8;
49 with Sem_Dim; use Sem_Dim;
50 with Sem_Eval; use Sem_Eval;
51 with Sem_Res; use Sem_Res;
52 with Sem_Type; use Sem_Type;
53 with Sem_Util; use Sem_Util;
54 with Sem_Warn; use Sem_Warn;
55 with Sinput; use Sinput;
56 with Snames; use Snames;
57 with Stand; use Stand;
58 with Sinfo; use Sinfo;
59 with Stringt; use Stringt;
60 with Targparm; use Targparm;
61 with Ttypes; use Ttypes;
62 with Tbuild; use Tbuild;
63 with Urealp; use Urealp;
64 with Warnsw; use Warnsw;
66 with GNAT.Heap_Sort_G;
68 package body Sem_Ch13 is
70 SSU : constant Pos := System_Storage_Unit;
71 -- Convenient short hand for commonly used constant
73 -----------------------
74 -- Local Subprograms --
75 -----------------------
77 procedure Alignment_Check_For_Size_Change (Typ : Entity_Id; Size : Uint);
78 -- This routine is called after setting one of the sizes of type entity
79 -- Typ to Size. The purpose is to deal with the situation of a derived
80 -- type whose inherited alignment is no longer appropriate for the new
81 -- size value. In this case, we reset the Alignment to unknown.
83 procedure Build_Predicate_Function (Typ : Entity_Id; N : Node_Id);
84 -- If Typ has predicates (indicated by Has_Predicates being set for Typ,
85 -- then either there are pragma Invariant entries on the rep chain for the
86 -- type (note that Predicate aspects are converted to pragma Predicate), or
87 -- there are inherited aspects from a parent type, or ancestor subtypes.
88 -- This procedure builds the spec and body for the Predicate function that
89 -- tests these predicates. N is the freeze node for the type. The spec of
90 -- the function is inserted before the freeze node, and the body of the
91 -- function is inserted after the freeze node.
93 procedure Build_Static_Predicate
97 -- Given a predicated type Typ, where Typ is a discrete static subtype,
98 -- whose predicate expression is Expr, tests if Expr is a static predicate,
99 -- and if so, builds the predicate range list. Nam is the name of the one
100 -- argument to the predicate function. Occurrences of the type name in the
101 -- predicate expression have been replaced by identifier references to this
102 -- name, which is unique, so any identifier with Chars matching Nam must be
103 -- a reference to the type. If the predicate is non-static, this procedure
104 -- returns doing nothing. If the predicate is static, then the predicate
105 -- list is stored in Static_Predicate (Typ), and the Expr is rewritten as
106 -- a canonicalized membership operation.
108 function Get_Alignment_Value (Expr : Node_Id) return Uint;
109 -- Given the expression for an alignment value, returns the corresponding
110 -- Uint value. If the value is inappropriate, then error messages are
111 -- posted as required, and a value of No_Uint is returned.
113 function Is_Operational_Item (N : Node_Id) return Boolean;
114 -- A specification for a stream attribute is allowed before the full type
115 -- is declared, as explained in AI-00137 and the corrigendum. Attributes
116 -- that do not specify a representation characteristic are operational
119 procedure New_Stream_Subprogram
123 Nam : TSS_Name_Type);
124 -- Create a subprogram renaming of a given stream attribute to the
125 -- designated subprogram and then in the tagged case, provide this as a
126 -- primitive operation, or in the non-tagged case make an appropriate TSS
127 -- entry. This is more properly an expansion activity than just semantics,
128 -- but the presence of user-defined stream functions for limited types is a
129 -- legality check, which is why this takes place here rather than in
130 -- exp_ch13, where it was previously. Nam indicates the name of the TSS
131 -- function to be generated.
133 -- To avoid elaboration anomalies with freeze nodes, for untagged types
134 -- we generate both a subprogram declaration and a subprogram renaming
135 -- declaration, so that the attribute specification is handled as a
136 -- renaming_as_body. For tagged types, the specification is one of the
140 with procedure Replace_Type_Reference (N : Node_Id);
141 procedure Replace_Type_References_Generic (N : Node_Id; TName : Name_Id);
142 -- This is used to scan an expression for a predicate or invariant aspect
143 -- replacing occurrences of the name TName (the name of the subtype to
144 -- which the aspect applies) with appropriate references to the parameter
145 -- of the predicate function or invariant procedure. The procedure passed
146 -- as a generic parameter does the actual replacement of node N, which is
147 -- either a simple direct reference to TName, or a selected component that
148 -- represents an appropriately qualified occurrence of TName.
154 Biased : Boolean := True);
155 -- If Biased is True, sets Has_Biased_Representation flag for E, and
156 -- outputs a warning message at node N if Warn_On_Biased_Representation is
157 -- is True. This warning inserts the string Msg to describe the construct
160 ----------------------------------------------
161 -- Table for Validate_Unchecked_Conversions --
162 ----------------------------------------------
164 -- The following table collects unchecked conversions for validation.
165 -- Entries are made by Validate_Unchecked_Conversion and then the call
166 -- to Validate_Unchecked_Conversions does the actual error checking and
167 -- posting of warnings. The reason for this delayed processing is to take
168 -- advantage of back-annotations of size and alignment values performed by
171 -- Note: the reason we store a Source_Ptr value instead of a Node_Id is
172 -- that by the time Validate_Unchecked_Conversions is called, Sprint will
173 -- already have modified all Sloc values if the -gnatD option is set.
175 type UC_Entry is record
176 Eloc : Source_Ptr; -- node used for posting warnings
177 Source : Entity_Id; -- source type for unchecked conversion
178 Target : Entity_Id; -- target type for unchecked conversion
181 package Unchecked_Conversions is new Table.Table (
182 Table_Component_Type => UC_Entry,
183 Table_Index_Type => Int,
184 Table_Low_Bound => 1,
186 Table_Increment => 200,
187 Table_Name => "Unchecked_Conversions");
189 ----------------------------------------
190 -- Table for Validate_Address_Clauses --
191 ----------------------------------------
193 -- If an address clause has the form
195 -- for X'Address use Expr
197 -- where Expr is of the form Y'Address or recursively is a reference to a
198 -- constant of either of these forms, and X and Y are entities of objects,
199 -- then if Y has a smaller alignment than X, that merits a warning about
200 -- possible bad alignment. The following table collects address clauses of
201 -- this kind. We put these in a table so that they can be checked after the
202 -- back end has completed annotation of the alignments of objects, since we
203 -- can catch more cases that way.
205 type Address_Clause_Check_Record is record
207 -- The address clause
210 -- The entity of the object overlaying Y
213 -- The entity of the object being overlaid
216 -- Whether the address is offset within Y
219 package Address_Clause_Checks is new Table.Table (
220 Table_Component_Type => Address_Clause_Check_Record,
221 Table_Index_Type => Int,
222 Table_Low_Bound => 1,
224 Table_Increment => 200,
225 Table_Name => "Address_Clause_Checks");
227 -----------------------------------------
228 -- Adjust_Record_For_Reverse_Bit_Order --
229 -----------------------------------------
231 procedure Adjust_Record_For_Reverse_Bit_Order (R : Entity_Id) is
236 -- Processing depends on version of Ada
238 -- For Ada 95, we just renumber bits within a storage unit. We do the
239 -- same for Ada 83 mode, since we recognize the Bit_Order attribute in
240 -- Ada 83, and are free to add this extension.
242 if Ada_Version < Ada_2005 then
243 Comp := First_Component_Or_Discriminant (R);
244 while Present (Comp) loop
245 CC := Component_Clause (Comp);
247 -- If component clause is present, then deal with the non-default
248 -- bit order case for Ada 95 mode.
250 -- We only do this processing for the base type, and in fact that
251 -- is important, since otherwise if there are record subtypes, we
252 -- could reverse the bits once for each subtype, which is wrong.
255 and then Ekind (R) = E_Record_Type
258 CFB : constant Uint := Component_Bit_Offset (Comp);
259 CSZ : constant Uint := Esize (Comp);
260 CLC : constant Node_Id := Component_Clause (Comp);
261 Pos : constant Node_Id := Position (CLC);
262 FB : constant Node_Id := First_Bit (CLC);
264 Storage_Unit_Offset : constant Uint :=
265 CFB / System_Storage_Unit;
267 Start_Bit : constant Uint :=
268 CFB mod System_Storage_Unit;
271 -- Cases where field goes over storage unit boundary
273 if Start_Bit + CSZ > System_Storage_Unit then
275 -- Allow multi-byte field but generate warning
277 if Start_Bit mod System_Storage_Unit = 0
278 and then CSZ mod System_Storage_Unit = 0
281 ("multi-byte field specified with non-standard"
282 & " Bit_Order?", CLC);
284 if Bytes_Big_Endian then
286 ("bytes are not reversed "
287 & "(component is big-endian)?", CLC);
290 ("bytes are not reversed "
291 & "(component is little-endian)?", CLC);
294 -- Do not allow non-contiguous field
298 ("attempt to specify non-contiguous field "
299 & "not permitted", CLC);
301 ("\caused by non-standard Bit_Order "
304 ("\consider possibility of using "
305 & "Ada 2005 mode here", CLC);
308 -- Case where field fits in one storage unit
311 -- Give warning if suspicious component clause
313 if Intval (FB) >= System_Storage_Unit
314 and then Warn_On_Reverse_Bit_Order
317 ("?Bit_Order clause does not affect " &
318 "byte ordering", Pos);
320 Intval (Pos) + Intval (FB) /
323 ("?position normalized to ^ before bit " &
324 "order interpreted", Pos);
327 -- Here is where we fix up the Component_Bit_Offset value
328 -- to account for the reverse bit order. Some examples of
329 -- what needs to be done are:
331 -- First_Bit .. Last_Bit Component_Bit_Offset
343 -- The rule is that the first bit is is obtained by
344 -- subtracting the old ending bit from storage_unit - 1.
346 Set_Component_Bit_Offset
348 (Storage_Unit_Offset * System_Storage_Unit) +
349 (System_Storage_Unit - 1) -
350 (Start_Bit + CSZ - 1));
352 Set_Normalized_First_Bit
354 Component_Bit_Offset (Comp) mod
355 System_Storage_Unit);
360 Next_Component_Or_Discriminant (Comp);
363 -- For Ada 2005, we do machine scalar processing, as fully described In
364 -- AI-133. This involves gathering all components which start at the
365 -- same byte offset and processing them together. Same approach is still
366 -- valid in later versions including Ada 2012.
370 Max_Machine_Scalar_Size : constant Uint :=
372 (Standard_Long_Long_Integer_Size);
373 -- We use this as the maximum machine scalar size
376 SSU : constant Uint := UI_From_Int (System_Storage_Unit);
379 -- This first loop through components does two things. First it
380 -- deals with the case of components with component clauses whose
381 -- length is greater than the maximum machine scalar size (either
382 -- accepting them or rejecting as needed). Second, it counts the
383 -- number of components with component clauses whose length does
384 -- not exceed this maximum for later processing.
387 Comp := First_Component_Or_Discriminant (R);
388 while Present (Comp) loop
389 CC := Component_Clause (Comp);
393 Fbit : constant Uint :=
394 Static_Integer (First_Bit (CC));
395 Lbit : constant Uint :=
396 Static_Integer (Last_Bit (CC));
399 -- Case of component with last bit >= max machine scalar
401 if Lbit >= Max_Machine_Scalar_Size then
403 -- This is allowed only if first bit is zero, and
404 -- last bit + 1 is a multiple of storage unit size.
406 if Fbit = 0 and then (Lbit + 1) mod SSU = 0 then
408 -- This is the case to give a warning if enabled
410 if Warn_On_Reverse_Bit_Order then
412 ("multi-byte field specified with "
413 & " non-standard Bit_Order?", CC);
415 if Bytes_Big_Endian then
417 ("\bytes are not reversed "
418 & "(component is big-endian)?", CC);
421 ("\bytes are not reversed "
422 & "(component is little-endian)?", CC);
426 -- Give error message for RM 13.4.1(10) violation
430 ("machine scalar rules not followed for&",
431 First_Bit (CC), Comp);
433 Error_Msg_Uint_1 := Lbit;
434 Error_Msg_Uint_2 := Max_Machine_Scalar_Size;
436 ("\last bit (^) exceeds maximum machine "
440 if (Lbit + 1) mod SSU /= 0 then
441 Error_Msg_Uint_1 := SSU;
443 ("\and is not a multiple of Storage_Unit (^) "
448 Error_Msg_Uint_1 := Fbit;
450 ("\and first bit (^) is non-zero "
456 -- OK case of machine scalar related component clause,
457 -- For now, just count them.
460 Num_CC := Num_CC + 1;
465 Next_Component_Or_Discriminant (Comp);
468 -- We need to sort the component clauses on the basis of the
469 -- Position values in the clause, so we can group clauses with
470 -- the same Position. together to determine the relevant machine
474 Comps : array (0 .. Num_CC) of Entity_Id;
475 -- Array to collect component and discriminant entities. The
476 -- data starts at index 1, the 0'th entry is for the sort
479 function CP_Lt (Op1, Op2 : Natural) return Boolean;
480 -- Compare routine for Sort
482 procedure CP_Move (From : Natural; To : Natural);
483 -- Move routine for Sort
485 package Sorting is new GNAT.Heap_Sort_G (CP_Move, CP_Lt);
489 -- Start and stop positions in the component list of the set of
490 -- components with the same starting position (that constitute
491 -- components in a single machine scalar).
494 -- Maximum last bit value of any component in this set
497 -- Corresponding machine scalar size
503 function CP_Lt (Op1, Op2 : Natural) return Boolean is
505 return Position (Component_Clause (Comps (Op1))) <
506 Position (Component_Clause (Comps (Op2)));
513 procedure CP_Move (From : Natural; To : Natural) is
515 Comps (To) := Comps (From);
518 -- Start of processing for Sort_CC
521 -- Collect the machine scalar relevant component clauses
524 Comp := First_Component_Or_Discriminant (R);
525 while Present (Comp) loop
527 CC : constant Node_Id := Component_Clause (Comp);
530 -- Collect only component clauses whose last bit is less
531 -- than machine scalar size. Any component clause whose
532 -- last bit exceeds this value does not take part in
533 -- machine scalar layout considerations. The test for
534 -- Error_Posted makes sure we exclude component clauses
535 -- for which we already posted an error.
538 and then not Error_Posted (Last_Bit (CC))
539 and then Static_Integer (Last_Bit (CC)) <
540 Max_Machine_Scalar_Size
542 Num_CC := Num_CC + 1;
543 Comps (Num_CC) := Comp;
547 Next_Component_Or_Discriminant (Comp);
550 -- Sort by ascending position number
552 Sorting.Sort (Num_CC);
554 -- We now have all the components whose size does not exceed
555 -- the max machine scalar value, sorted by starting position.
556 -- In this loop we gather groups of clauses starting at the
557 -- same position, to process them in accordance with AI-133.
560 while Stop < Num_CC loop
565 (Last_Bit (Component_Clause (Comps (Start))));
566 while Stop < Num_CC loop
568 (Position (Component_Clause (Comps (Stop + 1)))) =
570 (Position (Component_Clause (Comps (Stop))))
578 (Component_Clause (Comps (Stop)))));
584 -- Now we have a group of component clauses from Start to
585 -- Stop whose positions are identical, and MaxL is the
586 -- maximum last bit value of any of these components.
588 -- We need to determine the corresponding machine scalar
589 -- size. This loop assumes that machine scalar sizes are
590 -- even, and that each possible machine scalar has twice
591 -- as many bits as the next smaller one.
593 MSS := Max_Machine_Scalar_Size;
595 and then (MSS / 2) >= SSU
596 and then (MSS / 2) > MaxL
601 -- Here is where we fix up the Component_Bit_Offset value
602 -- to account for the reverse bit order. Some examples of
603 -- what needs to be done for the case of a machine scalar
606 -- First_Bit .. Last_Bit Component_Bit_Offset
618 -- The rule is that the first bit is obtained by subtracting
619 -- the old ending bit from machine scalar size - 1.
621 for C in Start .. Stop loop
623 Comp : constant Entity_Id := Comps (C);
624 CC : constant Node_Id :=
625 Component_Clause (Comp);
626 LB : constant Uint :=
627 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 :=
631 Static_Integer (Position (CC));
634 if Warn_On_Reverse_Bit_Order then
635 Error_Msg_Uint_1 := MSS;
637 ("info: reverse bit order in machine " &
638 "scalar of length^?", First_Bit (CC));
639 Error_Msg_Uint_1 := NFB;
640 Error_Msg_Uint_2 := NLB;
642 if Bytes_Big_Endian then
644 ("?\info: big-endian range for "
645 & "component & is ^ .. ^",
646 First_Bit (CC), Comp);
649 ("?\info: little-endian range "
650 & "for component & is ^ .. ^",
651 First_Bit (CC), Comp);
655 Set_Component_Bit_Offset (Comp, Pos * SSU + NFB);
656 Set_Normalized_First_Bit (Comp, NFB mod SSU);
663 end Adjust_Record_For_Reverse_Bit_Order;
665 -------------------------------------
666 -- Alignment_Check_For_Size_Change --
667 -------------------------------------
669 procedure Alignment_Check_For_Size_Change (Typ : Entity_Id; Size : Uint) is
671 -- If the alignment is known, and not set by a rep clause, and is
672 -- inconsistent with the size being set, then reset it to unknown,
673 -- we assume in this case that the size overrides the inherited
674 -- alignment, and that the alignment must be recomputed.
676 if Known_Alignment (Typ)
677 and then not Has_Alignment_Clause (Typ)
678 and then Size mod (Alignment (Typ) * SSU) /= 0
680 Init_Alignment (Typ);
682 end Alignment_Check_For_Size_Change;
684 -----------------------------------
685 -- Analyze_Aspect_Specifications --
686 -----------------------------------
688 procedure Analyze_Aspect_Specifications (N : Node_Id; E : Entity_Id) is
693 L : constant List_Id := Aspect_Specifications (N);
695 Ins_Node : Node_Id := N;
696 -- Insert pragmas (except Pre/Post/Invariant/Predicate) after this node
698 -- The general processing involves building an attribute definition
699 -- clause or a pragma node that corresponds to the aspect. Then one
700 -- of two things happens:
702 -- If we are required to delay the evaluation of this aspect to the
703 -- freeze point, we attach the corresponding pragma/attribute definition
704 -- clause to the aspect specification node, which is then placed in the
705 -- Rep Item chain. In this case we mark the entity by setting the flag
706 -- Has_Delayed_Aspects and we evaluate the rep item at the freeze point.
708 -- If no delay is required, we just insert the pragma or attribute
709 -- after the declaration, and it will get processed by the normal
710 -- circuit. The From_Aspect_Specification flag is set on the pragma
711 -- or attribute definition node in either case to activate special
712 -- processing (e.g. not traversing the list of homonyms for inline).
714 Delay_Required : Boolean := False;
715 -- Set True if delay is required
718 pragma Assert (Present (L));
720 -- Loop through aspects
723 Aspect_Loop : while Present (Aspect) loop
725 Loc : constant Source_Ptr := Sloc (Aspect);
726 Id : constant Node_Id := Identifier (Aspect);
727 Expr : constant Node_Id := Expression (Aspect);
728 Nam : constant Name_Id := Chars (Id);
729 A_Id : constant Aspect_Id := Get_Aspect_Id (Nam);
732 Eloc : Source_Ptr := No_Location;
733 -- Source location of expression, modified when we split PPC's. It
734 -- is set below when Expr is present.
736 procedure Check_False_Aspect_For_Derived_Type;
737 -- This procedure checks for the case of a false aspect for a
738 -- derived type, which improperly tries to cancel an aspect
739 -- inherited from the parent;
741 -----------------------------------------
742 -- Check_False_Aspect_For_Derived_Type --
743 -----------------------------------------
745 procedure Check_False_Aspect_For_Derived_Type is
747 -- We are only checking derived types
749 if not Is_Derived_Type (E) then
754 when Aspect_Atomic | Aspect_Shared =>
755 if not Is_Atomic (E) then
759 when Aspect_Atomic_Components =>
760 if not Has_Atomic_Components (E) then
764 when Aspect_Discard_Names =>
765 if not Discard_Names (E) then
770 if not Is_Packed (E) then
774 when Aspect_Unchecked_Union =>
775 if not Is_Unchecked_Union (E) then
779 when Aspect_Volatile =>
780 if not Is_Volatile (E) then
784 when Aspect_Volatile_Components =>
785 if not Has_Volatile_Components (E) then
793 -- Fall through means we are canceling an inherited aspect
795 Error_Msg_Name_1 := Nam;
797 ("derived type& inherits aspect%, cannot cancel", Expr, E);
798 end Check_False_Aspect_For_Derived_Type;
800 -- Start of processing for Aspect_Loop
803 -- Skip aspect if already analyzed (not clear if this is needed)
805 if Analyzed (Aspect) then
809 -- Set the source location of expression, used in the case of
810 -- a failed precondition/postcondition or invariant. Note that
811 -- the source location of the expression is not usually the best
812 -- choice here. For example, it gets located on the last AND
813 -- keyword in a chain of boolean expressiond AND'ed together.
814 -- It is best to put the message on the first character of the
815 -- assertion, which is the effect of the First_Node call here.
817 if Present (Expr) then
818 Eloc := Sloc (First_Node (Expr));
821 -- Check restriction No_Implementation_Aspect_Specifications
823 if Impl_Defined_Aspects (A_Id) then
825 (No_Implementation_Aspect_Specifications, Aspect);
828 -- Check restriction No_Specification_Of_Aspect
830 Check_Restriction_No_Specification_Of_Aspect (Aspect);
832 -- Analyze this aspect
834 Set_Analyzed (Aspect);
835 Set_Entity (Aspect, E);
836 Ent := New_Occurrence_Of (E, Sloc (Id));
838 -- Check for duplicate aspect. Note that the Comes_From_Source
839 -- test allows duplicate Pre/Post's that we generate internally
840 -- to escape being flagged here.
842 if No_Duplicates_Allowed (A_Id) then
844 while Anod /= Aspect loop
846 (A_Id, Get_Aspect_Id (Chars (Identifier (Anod))))
847 and then Comes_From_Source (Aspect)
849 Error_Msg_Name_1 := Nam;
850 Error_Msg_Sloc := Sloc (Anod);
852 -- Case of same aspect specified twice
854 if Class_Present (Anod) = Class_Present (Aspect) then
855 if not Class_Present (Anod) then
857 ("aspect% for & previously given#",
861 ("aspect `%''Class` for & previously given#",
865 -- Case of Pre and Pre'Class both specified
867 elsif Nam = Name_Pre then
868 if Class_Present (Aspect) then
870 ("aspect `Pre''Class` for & is not allowed here",
873 ("\since aspect `Pre` previously given#",
878 ("aspect `Pre` for & is not allowed here",
881 ("\since aspect `Pre''Class` previously given#",
886 -- Allowed case of X and X'Class both specified
893 -- Check some general restrictions on language defined aspects
895 if not Impl_Defined_Aspects (A_Id) then
896 Error_Msg_Name_1 := Nam;
898 -- Not allowed for renaming declarations
900 if Nkind (N) in N_Renaming_Declaration then
902 ("aspect % not allowed for renaming declaration",
906 -- Not allowed for formal type declarations
908 if Nkind (N) = N_Formal_Type_Declaration then
910 ("aspect % not allowed for formal type declaration",
915 -- Copy expression for later processing by the procedures
916 -- Check_Aspect_At_[Freeze_Point | End_Of_Declarations]
918 Set_Entity (Id, New_Copy_Tree (Expr));
920 -- Processing based on specific aspect
924 -- No_Aspect should be impossible
929 -- Aspects taking an optional boolean argument. For all of
930 -- these we just create a matching pragma and insert it, if
931 -- the expression is missing or set to True. If the expression
932 -- is False, we can ignore the aspect with the exception that
933 -- in the case of a derived type, we must check for an illegal
934 -- attempt to cancel an inherited aspect.
936 when Boolean_Aspects =>
937 Set_Is_Boolean_Aspect (Aspect);
940 and then Is_False (Static_Boolean (Expr))
942 Check_False_Aspect_For_Derived_Type;
946 -- If True, build corresponding pragma node
950 Pragma_Argument_Associations => New_List (Ent),
952 Make_Identifier (Sloc (Id), Chars (Id)));
954 -- Never need to delay for boolean aspects
956 pragma Assert (not Delay_Required);
958 -- Library unit aspects. These are boolean aspects, but we
959 -- have to do special things with the insertion, since the
960 -- pragma belongs inside the declarations of a package.
962 when Library_Unit_Aspects =>
964 and then Is_False (Static_Boolean (Expr))
969 -- Build corresponding pragma node
973 Pragma_Argument_Associations => New_List (Ent),
975 Make_Identifier (Sloc (Id), Chars (Id)));
977 -- This requires special handling in the case of a package
978 -- declaration, the pragma needs to be inserted in the list
979 -- of declarations for the associated package. There is no
980 -- issue of visibility delay for these aspects.
982 if Nkind (N) = N_Package_Declaration then
983 if Nkind (Parent (N)) /= N_Compilation_Unit then
985 ("incorrect context for library unit aspect&", Id);
988 (Aitem, Visible_Declarations (Specification (N)));
994 -- If not package declaration, no delay is required
996 pragma Assert (not Delay_Required);
998 -- Aspects related to container iterators. These aspects denote
999 -- subprograms, and thus must be delayed.
1001 when Aspect_Constant_Indexing |
1002 Aspect_Variable_Indexing =>
1004 if not Is_Type (E) or else not Is_Tagged_Type (E) then
1005 Error_Msg_N ("indexing applies to a tagged type", N);
1009 Make_Attribute_Definition_Clause (Loc,
1011 Chars => Chars (Id),
1012 Expression => Relocate_Node (Expr));
1014 Delay_Required := True;
1015 Set_Is_Delayed_Aspect (Aspect);
1017 when Aspect_Default_Iterator |
1018 Aspect_Iterator_Element =>
1021 Make_Attribute_Definition_Clause (Loc,
1023 Chars => Chars (Id),
1024 Expression => Relocate_Node (Expr));
1026 Delay_Required := True;
1027 Set_Is_Delayed_Aspect (Aspect);
1029 when Aspect_Implicit_Dereference =>
1031 or else not Has_Discriminants (E)
1034 ("Aspect must apply to a type with discriminants", N);
1042 Disc := First_Discriminant (E);
1043 while Present (Disc) loop
1044 if Chars (Expr) = Chars (Disc)
1045 and then Ekind (Etype (Disc)) =
1046 E_Anonymous_Access_Type
1048 Set_Has_Implicit_Dereference (E);
1049 Set_Has_Implicit_Dereference (Disc);
1053 Next_Discriminant (Disc);
1056 -- Error if no proper access discriminant.
1059 ("not an access discriminant of&", Expr, E);
1065 -- Aspects corresponding to attribute definition clauses
1067 when Aspect_Address |
1070 Aspect_Component_Size |
1071 Aspect_External_Tag |
1073 Aspect_Machine_Radix |
1074 Aspect_Object_Size |
1079 Aspect_Simple_Storage_Pool |
1080 Aspect_Storage_Pool |
1081 Aspect_Storage_Size |
1082 Aspect_Stream_Size |
1086 -- Construct the attribute definition clause
1089 Make_Attribute_Definition_Clause (Loc,
1091 Chars => Chars (Id),
1092 Expression => Relocate_Node (Expr));
1094 -- A delay is required except in the common case where
1095 -- the expression is a literal, in which case it is fine
1096 -- to take care of it right away.
1098 if Nkind_In (Expr, N_Integer_Literal, N_String_Literal) then
1099 pragma Assert (not Delay_Required);
1102 Delay_Required := True;
1103 Set_Is_Delayed_Aspect (Aspect);
1106 -- Aspects corresponding to pragmas with two arguments, where
1107 -- the first argument is a local name referring to the entity,
1108 -- and the second argument is the aspect definition expression
1109 -- which is an expression that does not get analyzed.
1111 when Aspect_Suppress |
1112 Aspect_Unsuppress =>
1114 -- Construct the pragma
1118 Pragma_Argument_Associations => New_List (
1119 New_Occurrence_Of (E, Loc),
1120 Relocate_Node (Expr)),
1121 Pragma_Identifier =>
1122 Make_Identifier (Sloc (Id), Chars (Id)));
1124 -- We don't have to play the delay game here, since the only
1125 -- values are check names which don't get analyzed anyway.
1127 pragma Assert (not Delay_Required);
1129 when Aspect_Synchronization =>
1131 -- The aspect corresponds to pragma Implemented.
1132 -- Construct the pragma
1136 Pragma_Argument_Associations => New_List (
1137 New_Occurrence_Of (E, Loc),
1138 Relocate_Node (Expr)),
1139 Pragma_Identifier =>
1140 Make_Identifier (Sloc (Id), Name_Implemented));
1142 pragma Assert (not Delay_Required);
1144 -- Aspects corresponding to pragmas with two arguments, where
1145 -- the second argument is a local name referring to the entity,
1146 -- and the first argument is the aspect definition expression.
1148 when Aspect_Warnings =>
1150 -- Construct the pragma
1154 Pragma_Argument_Associations => New_List (
1155 Relocate_Node (Expr),
1156 New_Occurrence_Of (E, Loc)),
1157 Pragma_Identifier =>
1158 Make_Identifier (Sloc (Id), Chars (Id)),
1159 Class_Present => Class_Present (Aspect));
1161 -- We don't have to play the delay game here, since the only
1162 -- values are ON/OFF which don't get analyzed anyway.
1164 pragma Assert (not Delay_Required);
1166 -- Default_Value and Default_Component_Value aspects. These
1167 -- are specially handled because they have no corresponding
1168 -- pragmas or attributes.
1170 when Aspect_Default_Value | Aspect_Default_Component_Value =>
1171 Error_Msg_Name_1 := Chars (Id);
1173 if not Is_Type (E) then
1174 Error_Msg_N ("aspect% can only apply to a type", Id);
1177 elsif not Is_First_Subtype (E) then
1178 Error_Msg_N ("aspect% cannot apply to subtype", Id);
1181 elsif A_Id = Aspect_Default_Value
1182 and then not Is_Scalar_Type (E)
1185 ("aspect% can only be applied to scalar type", Id);
1188 elsif A_Id = Aspect_Default_Component_Value then
1189 if not Is_Array_Type (E) then
1191 ("aspect% can only be applied to array type", Id);
1193 elsif not Is_Scalar_Type (Component_Type (E)) then
1195 ("aspect% requires scalar components", Id);
1201 Delay_Required := True;
1202 Set_Is_Delayed_Aspect (Aspect);
1203 Set_Has_Default_Aspect (Base_Type (Entity (Ent)));
1205 if Is_Scalar_Type (E) then
1206 Set_Default_Aspect_Value (Entity (Ent), Expr);
1208 Set_Default_Aspect_Component_Value (Entity (Ent), Expr);
1211 when Aspect_Attach_Handler =>
1214 Pragma_Identifier =>
1215 Make_Identifier (Sloc (Id), Name_Attach_Handler),
1216 Pragma_Argument_Associations =>
1217 New_List (Ent, Relocate_Node (Expr)));
1219 Set_From_Aspect_Specification (Aitem, True);
1220 Set_Corresponding_Aspect (Aitem, Aspect);
1222 pragma Assert (not Delay_Required);
1224 when Aspect_Priority |
1225 Aspect_Interrupt_Priority |
1226 Aspect_Dispatching_Domain |
1232 if A_Id = Aspect_Priority then
1233 Pname := Name_Priority;
1235 elsif A_Id = Aspect_Interrupt_Priority then
1236 Pname := Name_Interrupt_Priority;
1238 elsif A_Id = Aspect_CPU then
1242 Pname := Name_Dispatching_Domain;
1247 Pragma_Identifier =>
1248 Make_Identifier (Sloc (Id), Pname),
1249 Pragma_Argument_Associations =>
1251 (Make_Pragma_Argument_Association
1253 Expression => Relocate_Node (Expr))));
1255 Set_From_Aspect_Specification (Aitem, True);
1256 Set_Corresponding_Aspect (Aitem, Aspect);
1258 pragma Assert (not Delay_Required);
1261 -- Aspects Pre/Post generate Precondition/Postcondition pragmas
1262 -- with a first argument that is the expression, and a second
1263 -- argument that is an informative message if the test fails.
1264 -- This is inserted right after the declaration, to get the
1265 -- required pragma placement. The processing for the pragmas
1266 -- takes care of the required delay.
1268 when Pre_Post_Aspects => declare
1272 if A_Id = Aspect_Pre or else A_Id = Aspect_Precondition then
1273 Pname := Name_Precondition;
1275 Pname := Name_Postcondition;
1278 -- If the expressions is of the form A and then B, then
1279 -- we generate separate Pre/Post aspects for the separate
1280 -- clauses. Since we allow multiple pragmas, there is no
1281 -- problem in allowing multiple Pre/Post aspects internally.
1282 -- These should be treated in reverse order (B first and
1283 -- A second) since they are later inserted just after N in
1284 -- the order they are treated. This way, the pragma for A
1285 -- ends up preceding the pragma for B, which may have an
1286 -- importance for the error raised (either constraint error
1287 -- or precondition error).
1289 -- We do not do this for Pre'Class, since we have to put
1290 -- these conditions together in a complex OR expression
1292 -- We do not do this in ASIS mode, as ASIS relies on the
1293 -- original node representing the complete expression, when
1294 -- retrieving it through the source aspect table.
1297 and then (Pname = Name_Postcondition
1298 or else not Class_Present (Aspect))
1300 while Nkind (Expr) = N_And_Then loop
1301 Insert_After (Aspect,
1302 Make_Aspect_Specification (Sloc (Left_Opnd (Expr)),
1303 Identifier => Identifier (Aspect),
1304 Expression => Relocate_Node (Left_Opnd (Expr)),
1305 Class_Present => Class_Present (Aspect),
1306 Split_PPC => True));
1307 Rewrite (Expr, Relocate_Node (Right_Opnd (Expr)));
1308 Eloc := Sloc (Expr);
1312 -- Build the precondition/postcondition pragma
1316 Pragma_Identifier =>
1317 Make_Identifier (Sloc (Id), Pname),
1318 Class_Present => Class_Present (Aspect),
1319 Split_PPC => Split_PPC (Aspect),
1320 Pragma_Argument_Associations => New_List (
1321 Make_Pragma_Argument_Association (Eloc,
1322 Chars => Name_Check,
1323 Expression => Relocate_Node (Expr))));
1325 -- Add message unless exception messages are suppressed
1327 if not Opt.Exception_Locations_Suppressed then
1328 Append_To (Pragma_Argument_Associations (Aitem),
1329 Make_Pragma_Argument_Association (Eloc,
1330 Chars => Name_Message,
1332 Make_String_Literal (Eloc,
1334 & Get_Name_String (Pname)
1336 & Build_Location_String (Eloc))));
1339 Set_From_Aspect_Specification (Aitem, True);
1340 Set_Corresponding_Aspect (Aitem, Aspect);
1341 Set_Is_Delayed_Aspect (Aspect);
1343 -- For Pre/Post cases, insert immediately after the entity
1344 -- declaration, since that is the required pragma placement.
1345 -- Note that for these aspects, we do not have to worry
1346 -- about delay issues, since the pragmas themselves deal
1347 -- with delay of visibility for the expression analysis.
1349 -- If the entity is a library-level subprogram, the pre/
1350 -- postconditions must be treated as late pragmas.
1352 if Nkind (Parent (N)) = N_Compilation_Unit then
1353 Add_Global_Declaration (Aitem);
1355 Insert_After (N, Aitem);
1361 -- Invariant aspects generate a corresponding pragma with a
1362 -- first argument that is the entity, a second argument that is
1363 -- the expression and a third argument that is an appropriate
1364 -- message. This is inserted right after the declaration, to
1365 -- get the required pragma placement. The pragma processing
1366 -- takes care of the required delay.
1368 when Aspect_Invariant |
1369 Aspect_Type_Invariant =>
1371 -- Analysis of the pragma will verify placement legality:
1372 -- an invariant must apply to a private type, or appear in
1373 -- the private part of a spec and apply to a completion.
1375 -- Construct the pragma
1379 Pragma_Argument_Associations =>
1380 New_List (Ent, Relocate_Node (Expr)),
1381 Class_Present => Class_Present (Aspect),
1382 Pragma_Identifier =>
1383 Make_Identifier (Sloc (Id), Name_Invariant));
1385 -- Add message unless exception messages are suppressed
1387 if not Opt.Exception_Locations_Suppressed then
1388 Append_To (Pragma_Argument_Associations (Aitem),
1389 Make_Pragma_Argument_Association (Eloc,
1390 Chars => Name_Message,
1392 Make_String_Literal (Eloc,
1393 Strval => "failed invariant from "
1394 & Build_Location_String (Eloc))));
1397 Set_From_Aspect_Specification (Aitem, True);
1398 Set_Corresponding_Aspect (Aitem, Aspect);
1399 Set_Is_Delayed_Aspect (Aspect);
1401 -- For Invariant case, insert immediately after the entity
1402 -- declaration. We do not have to worry about delay issues
1403 -- since the pragma processing takes care of this.
1405 Insert_After (N, Aitem);
1408 -- Predicate aspects generate a corresponding pragma with a
1409 -- first argument that is the entity, and the second argument
1410 -- is the expression.
1412 when Aspect_Dynamic_Predicate |
1414 Aspect_Static_Predicate =>
1416 -- Construct the pragma (always a pragma Predicate, with
1417 -- flags recording whether it is static/dynamic).
1421 Pragma_Argument_Associations =>
1422 New_List (Ent, Relocate_Node (Expr)),
1423 Class_Present => Class_Present (Aspect),
1424 Pragma_Identifier =>
1425 Make_Identifier (Sloc (Id), Name_Predicate));
1427 Set_From_Aspect_Specification (Aitem, True);
1428 Set_Corresponding_Aspect (Aitem, Aspect);
1430 -- Make sure we have a freeze node (it might otherwise be
1431 -- missing in cases like subtype X is Y, and we would not
1432 -- have a place to build the predicate function).
1434 -- If the type is private, indicate that its completion
1435 -- has a freeze node, because that is the one that will be
1436 -- visible at freeze time.
1438 Set_Has_Predicates (E);
1440 if Is_Private_Type (E)
1441 and then Present (Full_View (E))
1443 Set_Has_Predicates (Full_View (E));
1444 Set_Has_Delayed_Aspects (Full_View (E));
1445 Ensure_Freeze_Node (Full_View (E));
1448 Ensure_Freeze_Node (E);
1449 Set_Is_Delayed_Aspect (Aspect);
1450 Delay_Required := True;
1452 when Aspect_Test_Case => declare
1454 Comp_Expr : Node_Id;
1455 Comp_Assn : Node_Id;
1461 if Nkind (Parent (N)) = N_Compilation_Unit then
1463 ("incorrect placement of aspect `Test_Case`", E);
1467 if Nkind (Expr) /= N_Aggregate then
1469 ("wrong syntax for aspect `Test_Case` for &", Id, E);
1473 -- Make pragma expressions refer to the original aspect
1474 -- expressions through the Original_Node link. This is used
1475 -- in semantic analysis for ASIS mode, so that the original
1476 -- expression also gets analyzed.
1478 Comp_Expr := First (Expressions (Expr));
1479 while Present (Comp_Expr) loop
1480 New_Expr := Relocate_Node (Comp_Expr);
1481 Set_Original_Node (New_Expr, Comp_Expr);
1483 (Make_Pragma_Argument_Association (Sloc (Comp_Expr),
1484 Expression => New_Expr),
1489 Comp_Assn := First (Component_Associations (Expr));
1490 while Present (Comp_Assn) loop
1491 if List_Length (Choices (Comp_Assn)) /= 1
1493 Nkind (First (Choices (Comp_Assn))) /= N_Identifier
1496 ("wrong syntax for aspect `Test_Case` for &", Id, E);
1500 New_Expr := Relocate_Node (Expression (Comp_Assn));
1501 Set_Original_Node (New_Expr, Expression (Comp_Assn));
1502 Append (Make_Pragma_Argument_Association (
1503 Sloc => Sloc (Comp_Assn),
1504 Chars => Chars (First (Choices (Comp_Assn))),
1505 Expression => New_Expr),
1510 -- Build the test-case pragma
1514 Pragma_Identifier =>
1515 Make_Identifier (Sloc (Id), Name_Test_Case),
1516 Pragma_Argument_Associations =>
1519 Set_From_Aspect_Specification (Aitem, True);
1520 Set_Corresponding_Aspect (Aitem, Aspect);
1521 Set_Is_Delayed_Aspect (Aspect);
1523 -- Insert immediately after the entity declaration
1525 Insert_After (N, Aitem);
1530 when Aspect_Dimension =>
1531 Analyze_Aspect_Dimension (N, Id, Expr);
1534 when Aspect_Dimension_System =>
1535 Analyze_Aspect_Dimension_System (N, Id, Expr);
1540 -- If a delay is required, we delay the freeze (not much point in
1541 -- delaying the aspect if we don't delay the freeze!). The pragma
1542 -- or attribute clause if there is one is then attached to the
1543 -- aspect specification which is placed in the rep item list.
1545 if Delay_Required then
1546 if Present (Aitem) then
1547 Set_From_Aspect_Specification (Aitem, True);
1549 if Nkind (Aitem) = N_Pragma then
1550 Set_Corresponding_Aspect (Aitem, Aspect);
1553 Set_Is_Delayed_Aspect (Aitem);
1554 Set_Aspect_Rep_Item (Aspect, Aitem);
1557 Ensure_Freeze_Node (E);
1558 Set_Has_Delayed_Aspects (E);
1559 Record_Rep_Item (E, Aspect);
1561 -- If no delay required, insert the pragma/clause in the tree
1564 Set_From_Aspect_Specification (Aitem, True);
1566 if Nkind (Aitem) = N_Pragma then
1567 Set_Corresponding_Aspect (Aitem, Aspect);
1570 -- If this is a compilation unit, we will put the pragma in
1571 -- the Pragmas_After list of the N_Compilation_Unit_Aux node.
1573 if Nkind (Parent (Ins_Node)) = N_Compilation_Unit then
1575 Aux : constant Node_Id :=
1576 Aux_Decls_Node (Parent (Ins_Node));
1579 pragma Assert (Nkind (Aux) = N_Compilation_Unit_Aux);
1581 if No (Pragmas_After (Aux)) then
1582 Set_Pragmas_After (Aux, Empty_List);
1585 -- For Pre_Post put at start of list, otherwise at end
1587 if A_Id in Pre_Post_Aspects then
1588 Prepend (Aitem, Pragmas_After (Aux));
1590 Append (Aitem, Pragmas_After (Aux));
1594 -- Here if not compilation unit case
1599 -- For Pre/Post cases, insert immediately after the
1600 -- entity declaration, since that is the required pragma
1603 when Pre_Post_Aspects =>
1604 Insert_After (N, Aitem);
1606 -- For Priority aspects, insert into the task or
1607 -- protected definition, which we need to create if it's
1608 -- not there. The same applies to CPU and
1609 -- Dispatching_Domain but only to tasks.
1611 when Aspect_Priority |
1612 Aspect_Interrupt_Priority |
1613 Aspect_Dispatching_Domain |
1616 T : Node_Id; -- the type declaration
1617 L : List_Id; -- list of decls of task/protected
1620 if Nkind (N) = N_Object_Declaration then
1621 T := Parent (Etype (Defining_Identifier (N)));
1626 if Nkind (T) = N_Protected_Type_Declaration
1627 and then A_Id /= Aspect_Dispatching_Domain
1628 and then A_Id /= Aspect_CPU
1631 (Present (Protected_Definition (T)));
1633 L := Visible_Declarations
1634 (Protected_Definition (T));
1636 elsif Nkind (T) = N_Task_Type_Declaration then
1637 if No (Task_Definition (T)) then
1640 Make_Task_Definition
1642 Visible_Declarations => New_List,
1643 End_Label => Empty));
1646 L := Visible_Declarations (Task_Definition (T));
1649 raise Program_Error;
1652 Prepend (Aitem, To => L);
1654 -- Analyze rewritten pragma. Otherwise, its
1655 -- analysis is done too late, after the task or
1656 -- protected object has been created.
1661 -- For all other cases, insert in sequence
1664 Insert_After (Ins_Node, Aitem);
1673 end loop Aspect_Loop;
1674 end Analyze_Aspect_Specifications;
1676 -----------------------
1677 -- Analyze_At_Clause --
1678 -----------------------
1680 -- An at clause is replaced by the corresponding Address attribute
1681 -- definition clause that is the preferred approach in Ada 95.
1683 procedure Analyze_At_Clause (N : Node_Id) is
1684 CS : constant Boolean := Comes_From_Source (N);
1687 -- This is an obsolescent feature
1689 Check_Restriction (No_Obsolescent_Features, N);
1691 if Warn_On_Obsolescent_Feature then
1693 ("at clause is an obsolescent feature (RM J.7(2))?", N);
1695 ("\use address attribute definition clause instead?", N);
1698 -- Rewrite as address clause
1701 Make_Attribute_Definition_Clause (Sloc (N),
1702 Name => Identifier (N),
1703 Chars => Name_Address,
1704 Expression => Expression (N)));
1706 -- We preserve Comes_From_Source, since logically the clause still
1707 -- comes from the source program even though it is changed in form.
1709 Set_Comes_From_Source (N, CS);
1711 -- Analyze rewritten clause
1713 Analyze_Attribute_Definition_Clause (N);
1714 end Analyze_At_Clause;
1716 -----------------------------------------
1717 -- Analyze_Attribute_Definition_Clause --
1718 -----------------------------------------
1720 procedure Analyze_Attribute_Definition_Clause (N : Node_Id) is
1721 Loc : constant Source_Ptr := Sloc (N);
1722 Nam : constant Node_Id := Name (N);
1723 Attr : constant Name_Id := Chars (N);
1724 Expr : constant Node_Id := Expression (N);
1725 Id : constant Attribute_Id := Get_Attribute_Id (Attr);
1728 -- The entity of Nam after it is analyzed. In the case of an incomplete
1729 -- type, this is the underlying type.
1732 -- The underlying entity to which the attribute applies. Generally this
1733 -- is the Underlying_Type of Ent, except in the case where the clause
1734 -- applies to full view of incomplete type or private type in which case
1735 -- U_Ent is just a copy of Ent.
1737 FOnly : Boolean := False;
1738 -- Reset to True for subtype specific attribute (Alignment, Size)
1739 -- and for stream attributes, i.e. those cases where in the call
1740 -- to Rep_Item_Too_Late, FOnly is set True so that only the freezing
1741 -- rules are checked. Note that the case of stream attributes is not
1742 -- clear from the RM, but see AI95-00137. Also, the RM seems to
1743 -- disallow Storage_Size for derived task types, but that is also
1744 -- clearly unintentional.
1746 procedure Analyze_Stream_TSS_Definition (TSS_Nam : TSS_Name_Type);
1747 -- Common processing for 'Read, 'Write, 'Input and 'Output attribute
1748 -- definition clauses.
1750 function Duplicate_Clause return Boolean;
1751 -- This routine checks if the aspect for U_Ent being given by attribute
1752 -- definition clause N is for an aspect that has already been specified,
1753 -- and if so gives an error message. If there is a duplicate, True is
1754 -- returned, otherwise if there is no error, False is returned.
1756 procedure Check_Indexing_Functions;
1757 -- Check that the function in Constant_Indexing or Variable_Indexing
1758 -- attribute has the proper type structure. If the name is overloaded,
1759 -- check that all interpretations are legal.
1761 procedure Check_Iterator_Functions;
1762 -- Check that there is a single function in Default_Iterator attribute
1763 -- has the proper type structure.
1765 function Check_Primitive_Function (Subp : Entity_Id) return Boolean;
1766 -- Common legality check for the previous two
1768 -----------------------------------
1769 -- Analyze_Stream_TSS_Definition --
1770 -----------------------------------
1772 procedure Analyze_Stream_TSS_Definition (TSS_Nam : TSS_Name_Type) is
1773 Subp : Entity_Id := Empty;
1778 Is_Read : constant Boolean := (TSS_Nam = TSS_Stream_Read);
1779 -- True for Read attribute, false for other attributes
1781 function Has_Good_Profile (Subp : Entity_Id) return Boolean;
1782 -- Return true if the entity is a subprogram with an appropriate
1783 -- profile for the attribute being defined.
1785 ----------------------
1786 -- Has_Good_Profile --
1787 ----------------------
1789 function Has_Good_Profile (Subp : Entity_Id) return Boolean is
1791 Is_Function : constant Boolean := (TSS_Nam = TSS_Stream_Input);
1792 Expected_Ekind : constant array (Boolean) of Entity_Kind :=
1793 (False => E_Procedure, True => E_Function);
1797 if Ekind (Subp) /= Expected_Ekind (Is_Function) then
1801 F := First_Formal (Subp);
1804 or else Ekind (Etype (F)) /= E_Anonymous_Access_Type
1805 or else Designated_Type (Etype (F)) /=
1806 Class_Wide_Type (RTE (RE_Root_Stream_Type))
1811 if not Is_Function then
1815 Expected_Mode : constant array (Boolean) of Entity_Kind :=
1816 (False => E_In_Parameter,
1817 True => E_Out_Parameter);
1819 if Parameter_Mode (F) /= Expected_Mode (Is_Read) then
1827 Typ := Etype (Subp);
1830 return Base_Type (Typ) = Base_Type (Ent)
1831 and then No (Next_Formal (F));
1832 end Has_Good_Profile;
1834 -- Start of processing for Analyze_Stream_TSS_Definition
1839 if not Is_Type (U_Ent) then
1840 Error_Msg_N ("local name must be a subtype", Nam);
1844 Pnam := TSS (Base_Type (U_Ent), TSS_Nam);
1846 -- If Pnam is present, it can be either inherited from an ancestor
1847 -- type (in which case it is legal to redefine it for this type), or
1848 -- be a previous definition of the attribute for the same type (in
1849 -- which case it is illegal).
1851 -- In the first case, it will have been analyzed already, and we
1852 -- can check that its profile does not match the expected profile
1853 -- for a stream attribute of U_Ent. In the second case, either Pnam
1854 -- has been analyzed (and has the expected profile), or it has not
1855 -- been analyzed yet (case of a type that has not been frozen yet
1856 -- and for which the stream attribute has been set using Set_TSS).
1859 and then (No (First_Entity (Pnam)) or else Has_Good_Profile (Pnam))
1861 Error_Msg_Sloc := Sloc (Pnam);
1862 Error_Msg_Name_1 := Attr;
1863 Error_Msg_N ("% attribute already defined #", Nam);
1869 if Is_Entity_Name (Expr) then
1870 if not Is_Overloaded (Expr) then
1871 if Has_Good_Profile (Entity (Expr)) then
1872 Subp := Entity (Expr);
1876 Get_First_Interp (Expr, I, It);
1877 while Present (It.Nam) loop
1878 if Has_Good_Profile (It.Nam) then
1883 Get_Next_Interp (I, It);
1888 if Present (Subp) then
1889 if Is_Abstract_Subprogram (Subp) then
1890 Error_Msg_N ("stream subprogram must not be abstract", Expr);
1894 Set_Entity (Expr, Subp);
1895 Set_Etype (Expr, Etype (Subp));
1897 New_Stream_Subprogram (N, U_Ent, Subp, TSS_Nam);
1900 Error_Msg_Name_1 := Attr;
1901 Error_Msg_N ("incorrect expression for% attribute", Expr);
1903 end Analyze_Stream_TSS_Definition;
1905 ------------------------------
1906 -- Check_Indexing_Functions --
1907 ------------------------------
1909 procedure Check_Indexing_Functions is
1911 procedure Check_One_Function (Subp : Entity_Id);
1912 -- Check one possible interpretation
1914 ------------------------
1915 -- Check_One_Function --
1916 ------------------------
1918 procedure Check_One_Function (Subp : Entity_Id) is
1919 Default_Element : constant Node_Id :=
1921 (Etype (First_Formal (Subp)),
1922 Aspect_Iterator_Element);
1925 if not Check_Primitive_Function (Subp) then
1927 ("aspect Indexing requires a function that applies to type&",
1931 -- An indexing function must return either the default element of
1932 -- the container, or a reference type.
1934 if Present (Default_Element) then
1935 Analyze (Default_Element);
1936 if Is_Entity_Name (Default_Element)
1937 and then Covers (Entity (Default_Element), Etype (Subp))
1943 -- Otherwise the return type must be a reference type.
1945 if not Has_Implicit_Dereference (Etype (Subp)) then
1947 ("function for indexing must return a reference type", Subp);
1949 end Check_One_Function;
1951 -- Start of processing for Check_Indexing_Functions
1960 if not Is_Overloaded (Expr) then
1961 Check_One_Function (Entity (Expr));
1969 Get_First_Interp (Expr, I, It);
1970 while Present (It.Nam) loop
1972 -- Note that analysis will have added the interpretation
1973 -- that corresponds to the dereference. We only check the
1974 -- subprogram itself.
1976 if Is_Overloadable (It.Nam) then
1977 Check_One_Function (It.Nam);
1980 Get_Next_Interp (I, It);
1984 end Check_Indexing_Functions;
1986 ------------------------------
1987 -- Check_Iterator_Functions --
1988 ------------------------------
1990 procedure Check_Iterator_Functions is
1991 Default : Entity_Id;
1993 function Valid_Default_Iterator (Subp : Entity_Id) return Boolean;
1994 -- Check one possible interpretation for validity
1996 ----------------------------
1997 -- Valid_Default_Iterator --
1998 ----------------------------
2000 function Valid_Default_Iterator (Subp : Entity_Id) return Boolean is
2004 if not Check_Primitive_Function (Subp) then
2007 Formal := First_Formal (Subp);
2010 -- False if any subsequent formal has no default expression
2012 Formal := Next_Formal (Formal);
2013 while Present (Formal) loop
2014 if No (Expression (Parent (Formal))) then
2018 Next_Formal (Formal);
2021 -- True if all subsequent formals have default expressions
2024 end Valid_Default_Iterator;
2026 -- Start of processing for Check_Iterator_Functions
2031 if not Is_Entity_Name (Expr) then
2032 Error_Msg_N ("aspect Iterator must be a function name", Expr);
2035 if not Is_Overloaded (Expr) then
2036 if not Check_Primitive_Function (Entity (Expr)) then
2038 ("aspect Indexing requires a function that applies to type&",
2039 Entity (Expr), Ent);
2042 if not Valid_Default_Iterator (Entity (Expr)) then
2043 Error_Msg_N ("improper function for default iterator", Expr);
2053 Get_First_Interp (Expr, I, It);
2054 while Present (It.Nam) loop
2055 if not Check_Primitive_Function (It.Nam)
2056 or else not Valid_Default_Iterator (It.Nam)
2060 elsif Present (Default) then
2061 Error_Msg_N ("default iterator must be unique", Expr);
2067 Get_Next_Interp (I, It);
2071 if Present (Default) then
2072 Set_Entity (Expr, Default);
2073 Set_Is_Overloaded (Expr, False);
2076 end Check_Iterator_Functions;
2078 -------------------------------
2079 -- Check_Primitive_Function --
2080 -------------------------------
2082 function Check_Primitive_Function (Subp : Entity_Id) return Boolean is
2086 if Ekind (Subp) /= E_Function then
2090 if No (First_Formal (Subp)) then
2093 Ctrl := Etype (First_Formal (Subp));
2097 or else Ctrl = Class_Wide_Type (Ent)
2099 (Ekind (Ctrl) = E_Anonymous_Access_Type
2101 (Designated_Type (Ctrl) = Ent
2102 or else Designated_Type (Ctrl) = Class_Wide_Type (Ent)))
2111 end Check_Primitive_Function;
2113 ----------------------
2114 -- Duplicate_Clause --
2115 ----------------------
2117 function Duplicate_Clause return Boolean is
2121 -- Nothing to do if this attribute definition clause comes from
2122 -- an aspect specification, since we could not be duplicating an
2123 -- explicit clause, and we dealt with the case of duplicated aspects
2124 -- in Analyze_Aspect_Specifications.
2126 if From_Aspect_Specification (N) then
2130 -- Otherwise current clause may duplicate previous clause or a
2131 -- previously given aspect specification for the same aspect.
2133 A := Get_Rep_Item_For_Entity (U_Ent, Chars (N));
2136 if Entity (A) = U_Ent then
2137 Error_Msg_Name_1 := Chars (N);
2138 Error_Msg_Sloc := Sloc (A);
2139 Error_Msg_NE ("aspect% for & previously given#", N, U_Ent);
2145 end Duplicate_Clause;
2147 -- Start of processing for Analyze_Attribute_Definition_Clause
2150 -- The following code is a defense against recursion. Not clear that
2151 -- this can happen legitimately, but perhaps some error situations
2152 -- can cause it, and we did see this recursion during testing.
2154 if Analyzed (N) then
2157 Set_Analyzed (N, True);
2160 -- Ignore some selected attributes in CodePeer mode since they are not
2161 -- relevant in this context.
2163 if CodePeer_Mode then
2166 -- Ignore Component_Size in CodePeer mode, to avoid changing the
2167 -- internal representation of types by implicitly packing them.
2169 when Attribute_Component_Size =>
2170 Rewrite (N, Make_Null_Statement (Sloc (N)));
2178 -- Process Ignore_Rep_Clauses option
2180 if Ignore_Rep_Clauses then
2183 -- The following should be ignored. They do not affect legality
2184 -- and may be target dependent. The basic idea of -gnatI is to
2185 -- ignore any rep clauses that may be target dependent but do not
2186 -- affect legality (except possibly to be rejected because they
2187 -- are incompatible with the compilation target).
2189 when Attribute_Alignment |
2190 Attribute_Bit_Order |
2191 Attribute_Component_Size |
2192 Attribute_Machine_Radix |
2193 Attribute_Object_Size |
2195 Attribute_Stream_Size |
2196 Attribute_Value_Size =>
2197 Rewrite (N, Make_Null_Statement (Sloc (N)));
2200 -- Perhaps 'Small should not be ignored by Ignore_Rep_Clauses ???
2202 when Attribute_Small =>
2203 if Ignore_Rep_Clauses then
2204 Rewrite (N, Make_Null_Statement (Sloc (N)));
2208 -- The following should not be ignored, because in the first place
2209 -- they are reasonably portable, and should not cause problems in
2210 -- compiling code from another target, and also they do affect
2211 -- legality, e.g. failing to provide a stream attribute for a
2212 -- type may make a program illegal.
2214 when Attribute_External_Tag |
2218 Attribute_Simple_Storage_Pool |
2219 Attribute_Storage_Pool |
2220 Attribute_Storage_Size |
2224 -- Other cases are errors ("attribute& cannot be set with
2225 -- definition clause"), which will be caught below.
2233 Ent := Entity (Nam);
2235 if Rep_Item_Too_Early (Ent, N) then
2239 -- Rep clause applies to full view of incomplete type or private type if
2240 -- we have one (if not, this is a premature use of the type). However,
2241 -- certain semantic checks need to be done on the specified entity (i.e.
2242 -- the private view), so we save it in Ent.
2244 if Is_Private_Type (Ent)
2245 and then Is_Derived_Type (Ent)
2246 and then not Is_Tagged_Type (Ent)
2247 and then No (Full_View (Ent))
2249 -- If this is a private type whose completion is a derivation from
2250 -- another private type, there is no full view, and the attribute
2251 -- belongs to the type itself, not its underlying parent.
2255 elsif Ekind (Ent) = E_Incomplete_Type then
2257 -- The attribute applies to the full view, set the entity of the
2258 -- attribute definition accordingly.
2260 Ent := Underlying_Type (Ent);
2262 Set_Entity (Nam, Ent);
2265 U_Ent := Underlying_Type (Ent);
2268 -- Avoid cascaded error
2270 if Etype (Nam) = Any_Type then
2273 -- Must be declared in current scope
2275 elsif Scope (Ent) /= Current_Scope then
2276 Error_Msg_N ("entity must be declared in this scope", Nam);
2279 -- Must not be a source renaming (we do have some cases where the
2280 -- expander generates a renaming, and those cases are OK, in such
2281 -- cases any attribute applies to the renamed object as well).
2283 elsif Is_Object (Ent)
2284 and then Present (Renamed_Object (Ent))
2286 -- Case of renamed object from source, this is an error
2288 if Comes_From_Source (Renamed_Object (Ent)) then
2289 Get_Name_String (Chars (N));
2290 Error_Msg_Strlen := Name_Len;
2291 Error_Msg_String (1 .. Name_Len) := Name_Buffer (1 .. Name_Len);
2293 ("~ clause not allowed for a renaming declaration "
2294 & "(RM 13.1(6))", Nam);
2297 -- For the case of a compiler generated renaming, the attribute
2298 -- definition clause applies to the renamed object created by the
2299 -- expander. The easiest general way to handle this is to create a
2300 -- copy of the attribute definition clause for this object.
2304 Make_Attribute_Definition_Clause (Loc,
2306 New_Occurrence_Of (Entity (Renamed_Object (Ent)), Loc),
2308 Expression => Duplicate_Subexpr (Expression (N))));
2311 -- If no underlying entity, use entity itself, applies to some
2312 -- previously detected error cases ???
2314 elsif No (U_Ent) then
2317 -- Cannot specify for a subtype (exception Object/Value_Size)
2319 elsif Is_Type (U_Ent)
2320 and then not Is_First_Subtype (U_Ent)
2321 and then Id /= Attribute_Object_Size
2322 and then Id /= Attribute_Value_Size
2323 and then not From_At_Mod (N)
2325 Error_Msg_N ("cannot specify attribute for subtype", Nam);
2329 Set_Entity (N, U_Ent);
2331 -- Switch on particular attribute
2339 -- Address attribute definition clause
2341 when Attribute_Address => Address : begin
2343 -- A little error check, catch for X'Address use X'Address;
2345 if Nkind (Nam) = N_Identifier
2346 and then Nkind (Expr) = N_Attribute_Reference
2347 and then Attribute_Name (Expr) = Name_Address
2348 and then Nkind (Prefix (Expr)) = N_Identifier
2349 and then Chars (Nam) = Chars (Prefix (Expr))
2352 ("address for & is self-referencing", Prefix (Expr), Ent);
2356 -- Not that special case, carry on with analysis of expression
2358 Analyze_And_Resolve (Expr, RTE (RE_Address));
2360 -- Even when ignoring rep clauses we need to indicate that the
2361 -- entity has an address clause and thus it is legal to declare
2364 if Ignore_Rep_Clauses then
2365 if Ekind_In (U_Ent, E_Variable, E_Constant) then
2366 Record_Rep_Item (U_Ent, N);
2372 if Duplicate_Clause then
2375 -- Case of address clause for subprogram
2377 elsif Is_Subprogram (U_Ent) then
2378 if Has_Homonym (U_Ent) then
2380 ("address clause cannot be given " &
2381 "for overloaded subprogram",
2386 -- For subprograms, all address clauses are permitted, and we
2387 -- mark the subprogram as having a deferred freeze so that Gigi
2388 -- will not elaborate it too soon.
2390 -- Above needs more comments, what is too soon about???
2392 Set_Has_Delayed_Freeze (U_Ent);
2394 -- Case of address clause for entry
2396 elsif Ekind (U_Ent) = E_Entry then
2397 if Nkind (Parent (N)) = N_Task_Body then
2399 ("entry address must be specified in task spec", Nam);
2403 -- For entries, we require a constant address
2405 Check_Constant_Address_Clause (Expr, U_Ent);
2407 -- Special checks for task types
2409 if Is_Task_Type (Scope (U_Ent))
2410 and then Comes_From_Source (Scope (U_Ent))
2413 ("?entry address declared for entry in task type", N);
2415 ("\?only one task can be declared of this type", N);
2418 -- Entry address clauses are obsolescent
2420 Check_Restriction (No_Obsolescent_Features, N);
2422 if Warn_On_Obsolescent_Feature then
2424 ("attaching interrupt to task entry is an " &
2425 "obsolescent feature (RM J.7.1)?", N);
2427 ("\use interrupt procedure instead?", N);
2430 -- Case of an address clause for a controlled object which we
2431 -- consider to be erroneous.
2433 elsif Is_Controlled (Etype (U_Ent))
2434 or else Has_Controlled_Component (Etype (U_Ent))
2437 ("?controlled object& must not be overlaid", Nam, U_Ent);
2439 ("\?Program_Error will be raised at run time", Nam);
2440 Insert_Action (Declaration_Node (U_Ent),
2441 Make_Raise_Program_Error (Loc,
2442 Reason => PE_Overlaid_Controlled_Object));
2445 -- Case of address clause for a (non-controlled) object
2448 Ekind (U_Ent) = E_Variable
2450 Ekind (U_Ent) = E_Constant
2453 Expr : constant Node_Id := Expression (N);
2458 -- Exported variables cannot have an address clause, because
2459 -- this cancels the effect of the pragma Export.
2461 if Is_Exported (U_Ent) then
2463 ("cannot export object with address clause", Nam);
2467 Find_Overlaid_Entity (N, O_Ent, Off);
2469 -- Overlaying controlled objects is erroneous
2472 and then (Has_Controlled_Component (Etype (O_Ent))
2473 or else Is_Controlled (Etype (O_Ent)))
2476 ("?cannot overlay with controlled object", Expr);
2478 ("\?Program_Error will be raised at run time", Expr);
2479 Insert_Action (Declaration_Node (U_Ent),
2480 Make_Raise_Program_Error (Loc,
2481 Reason => PE_Overlaid_Controlled_Object));
2484 elsif Present (O_Ent)
2485 and then Ekind (U_Ent) = E_Constant
2486 and then not Is_Constant_Object (O_Ent)
2488 Error_Msg_N ("constant overlays a variable?", Expr);
2490 -- Imported variables can have an address clause, but then
2491 -- the import is pretty meaningless except to suppress
2492 -- initializations, so we do not need such variables to
2493 -- be statically allocated (and in fact it causes trouble
2494 -- if the address clause is a local value).
2496 elsif Is_Imported (U_Ent) then
2497 Set_Is_Statically_Allocated (U_Ent, False);
2500 -- We mark a possible modification of a variable with an
2501 -- address clause, since it is likely aliasing is occurring.
2503 Note_Possible_Modification (Nam, Sure => False);
2505 -- Here we are checking for explicit overlap of one variable
2506 -- by another, and if we find this then mark the overlapped
2507 -- variable as also being volatile to prevent unwanted
2508 -- optimizations. This is a significant pessimization so
2509 -- avoid it when there is an offset, i.e. when the object
2510 -- is composite; they cannot be optimized easily anyway.
2513 and then Is_Object (O_Ent)
2516 Set_Treat_As_Volatile (O_Ent);
2519 -- Legality checks on the address clause for initialized
2520 -- objects is deferred until the freeze point, because
2521 -- a subsequent pragma might indicate that the object is
2522 -- imported and thus not initialized.
2524 Set_Has_Delayed_Freeze (U_Ent);
2526 -- If an initialization call has been generated for this
2527 -- object, it needs to be deferred to after the freeze node
2528 -- we have just now added, otherwise GIGI will see a
2529 -- reference to the variable (as actual to the IP call)
2530 -- before its definition.
2533 Init_Call : constant Node_Id := Find_Init_Call (U_Ent, N);
2535 if Present (Init_Call) then
2537 Append_Freeze_Action (U_Ent, Init_Call);
2541 if Is_Exported (U_Ent) then
2543 ("& cannot be exported if an address clause is given",
2546 ("\define and export a variable " &
2547 "that holds its address instead",
2551 -- Entity has delayed freeze, so we will generate an
2552 -- alignment check at the freeze point unless suppressed.
2554 if not Range_Checks_Suppressed (U_Ent)
2555 and then not Alignment_Checks_Suppressed (U_Ent)
2557 Set_Check_Address_Alignment (N);
2560 -- Kill the size check code, since we are not allocating
2561 -- the variable, it is somewhere else.
2563 Kill_Size_Check_Code (U_Ent);
2565 -- If the address clause is of the form:
2567 -- for Y'Address use X'Address
2571 -- Const : constant Address := X'Address;
2573 -- for Y'Address use Const;
2575 -- then we make an entry in the table for checking the size
2576 -- and alignment of the overlaying variable. We defer this
2577 -- check till after code generation to take full advantage
2578 -- of the annotation done by the back end. This entry is
2579 -- only made if the address clause comes from source.
2581 -- If the entity has a generic type, the check will be
2582 -- performed in the instance if the actual type justifies
2583 -- it, and we do not insert the clause in the table to
2584 -- prevent spurious warnings.
2586 if Address_Clause_Overlay_Warnings
2587 and then Comes_From_Source (N)
2588 and then Present (O_Ent)
2589 and then Is_Object (O_Ent)
2591 if not Is_Generic_Type (Etype (U_Ent)) then
2592 Address_Clause_Checks.Append ((N, U_Ent, O_Ent, Off));
2595 -- If variable overlays a constant view, and we are
2596 -- warning on overlays, then mark the variable as
2597 -- overlaying a constant (we will give warnings later
2598 -- if this variable is assigned).
2600 if Is_Constant_Object (O_Ent)
2601 and then Ekind (U_Ent) = E_Variable
2603 Set_Overlays_Constant (U_Ent);
2608 -- Not a valid entity for an address clause
2611 Error_Msg_N ("address cannot be given for &", Nam);
2619 -- Alignment attribute definition clause
2621 when Attribute_Alignment => Alignment : declare
2622 Align : constant Uint := Get_Alignment_Value (Expr);
2623 Max_Align : constant Uint := UI_From_Int (Maximum_Alignment);
2628 if not Is_Type (U_Ent)
2629 and then Ekind (U_Ent) /= E_Variable
2630 and then Ekind (U_Ent) /= E_Constant
2632 Error_Msg_N ("alignment cannot be given for &", Nam);
2634 elsif Duplicate_Clause then
2637 elsif Align /= No_Uint then
2638 Set_Has_Alignment_Clause (U_Ent);
2640 -- Tagged type case, check for attempt to set alignment to a
2641 -- value greater than Max_Align, and reset if so.
2643 if Is_Tagged_Type (U_Ent) and then Align > Max_Align then
2645 ("?alignment for & set to Maximum_Aligment", Nam);
2646 Set_Alignment (U_Ent, Max_Align);
2651 Set_Alignment (U_Ent, Align);
2654 -- For an array type, U_Ent is the first subtype. In that case,
2655 -- also set the alignment of the anonymous base type so that
2656 -- other subtypes (such as the itypes for aggregates of the
2657 -- type) also receive the expected alignment.
2659 if Is_Array_Type (U_Ent) then
2660 Set_Alignment (Base_Type (U_Ent), Align);
2669 -- Bit_Order attribute definition clause
2671 when Attribute_Bit_Order => Bit_Order : declare
2673 if not Is_Record_Type (U_Ent) then
2675 ("Bit_Order can only be defined for record type", Nam);
2677 elsif Duplicate_Clause then
2681 Analyze_And_Resolve (Expr, RTE (RE_Bit_Order));
2683 if Etype (Expr) = Any_Type then
2686 elsif not Is_Static_Expression (Expr) then
2687 Flag_Non_Static_Expr
2688 ("Bit_Order requires static expression!", Expr);
2691 if (Expr_Value (Expr) = 0) /= Bytes_Big_Endian then
2692 Set_Reverse_Bit_Order (U_Ent, True);
2698 --------------------
2699 -- Component_Size --
2700 --------------------
2702 -- Component_Size attribute definition clause
2704 when Attribute_Component_Size => Component_Size_Case : declare
2705 Csize : constant Uint := Static_Integer (Expr);
2709 New_Ctyp : Entity_Id;
2713 if not Is_Array_Type (U_Ent) then
2714 Error_Msg_N ("component size requires array type", Nam);
2718 Btype := Base_Type (U_Ent);
2719 Ctyp := Component_Type (Btype);
2721 if Duplicate_Clause then
2724 elsif Rep_Item_Too_Early (Btype, N) then
2727 elsif Csize /= No_Uint then
2728 Check_Size (Expr, Ctyp, Csize, Biased);
2730 -- For the biased case, build a declaration for a subtype that
2731 -- will be used to represent the biased subtype that reflects
2732 -- the biased representation of components. We need the subtype
2733 -- to get proper conversions on referencing elements of the
2734 -- array. Note: component size clauses are ignored in VM mode.
2736 if VM_Target = No_VM then
2739 Make_Defining_Identifier (Loc,
2741 New_External_Name (Chars (U_Ent), 'C', 0, 'T'));
2744 Make_Subtype_Declaration (Loc,
2745 Defining_Identifier => New_Ctyp,
2746 Subtype_Indication =>
2747 New_Occurrence_Of (Component_Type (Btype), Loc));
2749 Set_Parent (Decl, N);
2750 Analyze (Decl, Suppress => All_Checks);
2752 Set_Has_Delayed_Freeze (New_Ctyp, False);
2753 Set_Esize (New_Ctyp, Csize);
2754 Set_RM_Size (New_Ctyp, Csize);
2755 Init_Alignment (New_Ctyp);
2756 Set_Is_Itype (New_Ctyp, True);
2757 Set_Associated_Node_For_Itype (New_Ctyp, U_Ent);
2759 Set_Component_Type (Btype, New_Ctyp);
2760 Set_Biased (New_Ctyp, N, "component size clause");
2763 Set_Component_Size (Btype, Csize);
2765 -- For VM case, we ignore component size clauses
2768 -- Give a warning unless we are in GNAT mode, in which case
2769 -- the warning is suppressed since it is not useful.
2771 if not GNAT_Mode then
2773 ("?component size ignored in this configuration", N);
2777 -- Deal with warning on overridden size
2779 if Warn_On_Overridden_Size
2780 and then Has_Size_Clause (Ctyp)
2781 and then RM_Size (Ctyp) /= Csize
2784 ("?component size overrides size clause for&",
2788 Set_Has_Component_Size_Clause (Btype, True);
2789 Set_Has_Non_Standard_Rep (Btype, True);
2791 end Component_Size_Case;
2793 -----------------------
2794 -- Constant_Indexing --
2795 -----------------------
2797 when Attribute_Constant_Indexing =>
2798 Check_Indexing_Functions;
2800 ----------------------
2801 -- Default_Iterator --
2802 ----------------------
2804 when Attribute_Default_Iterator => Default_Iterator : declare
2808 if not Is_Tagged_Type (U_Ent) then
2810 ("aspect Default_Iterator applies to tagged type", Nam);
2813 Check_Iterator_Functions;
2817 if not Is_Entity_Name (Expr)
2818 or else Ekind (Entity (Expr)) /= E_Function
2820 Error_Msg_N ("aspect Iterator must be a function", Expr);
2822 Func := Entity (Expr);
2825 if No (First_Formal (Func))
2826 or else Etype (First_Formal (Func)) /= U_Ent
2829 ("Default Iterator must be a primitive of&", Func, U_Ent);
2831 end Default_Iterator;
2837 when Attribute_External_Tag => External_Tag :
2839 if not Is_Tagged_Type (U_Ent) then
2840 Error_Msg_N ("should be a tagged type", Nam);
2843 if Duplicate_Clause then
2847 Analyze_And_Resolve (Expr, Standard_String);
2849 if not Is_Static_Expression (Expr) then
2850 Flag_Non_Static_Expr
2851 ("static string required for tag name!", Nam);
2854 if VM_Target = No_VM then
2855 Set_Has_External_Tag_Rep_Clause (U_Ent);
2857 Error_Msg_Name_1 := Attr;
2859 ("% attribute unsupported in this configuration", Nam);
2862 if not Is_Library_Level_Entity (U_Ent) then
2864 ("?non-unique external tag supplied for &", N, U_Ent);
2866 ("?\same external tag applies to all subprogram calls", N);
2868 ("?\corresponding internal tag cannot be obtained", N);
2873 --------------------------
2874 -- Implicit_Dereference --
2875 --------------------------
2877 when Attribute_Implicit_Dereference =>
2879 -- Legality checks already performed at the point of
2880 -- the type declaration, aspect is not delayed.
2888 when Attribute_Input =>
2889 Analyze_Stream_TSS_Definition (TSS_Stream_Input);
2890 Set_Has_Specified_Stream_Input (Ent);
2892 ----------------------
2893 -- Iterator_Element --
2894 ----------------------
2896 when Attribute_Iterator_Element =>
2899 if not Is_Entity_Name (Expr)
2900 or else not Is_Type (Entity (Expr))
2902 Error_Msg_N ("aspect Iterator_Element must be a type", Expr);
2909 -- Machine radix attribute definition clause
2911 when Attribute_Machine_Radix => Machine_Radix : declare
2912 Radix : constant Uint := Static_Integer (Expr);
2915 if not Is_Decimal_Fixed_Point_Type (U_Ent) then
2916 Error_Msg_N ("decimal fixed-point type expected for &", Nam);
2918 elsif Duplicate_Clause then
2921 elsif Radix /= No_Uint then
2922 Set_Has_Machine_Radix_Clause (U_Ent);
2923 Set_Has_Non_Standard_Rep (Base_Type (U_Ent));
2927 elsif Radix = 10 then
2928 Set_Machine_Radix_10 (U_Ent);
2930 Error_Msg_N ("machine radix value must be 2 or 10", Expr);
2939 -- Object_Size attribute definition clause
2941 when Attribute_Object_Size => Object_Size : declare
2942 Size : constant Uint := Static_Integer (Expr);
2945 pragma Warnings (Off, Biased);
2948 if not Is_Type (U_Ent) then
2949 Error_Msg_N ("Object_Size cannot be given for &", Nam);
2951 elsif Duplicate_Clause then
2955 Check_Size (Expr, U_Ent, Size, Biased);
2963 UI_Mod (Size, 64) /= 0
2966 ("Object_Size must be 8, 16, 32, or multiple of 64",
2970 Set_Esize (U_Ent, Size);
2971 Set_Has_Object_Size_Clause (U_Ent);
2972 Alignment_Check_For_Size_Change (U_Ent, Size);
2980 when Attribute_Output =>
2981 Analyze_Stream_TSS_Definition (TSS_Stream_Output);
2982 Set_Has_Specified_Stream_Output (Ent);
2988 when Attribute_Read =>
2989 Analyze_Stream_TSS_Definition (TSS_Stream_Read);
2990 Set_Has_Specified_Stream_Read (Ent);
2996 -- Size attribute definition clause
2998 when Attribute_Size => Size : declare
2999 Size : constant Uint := Static_Integer (Expr);
3006 if Duplicate_Clause then
3009 elsif not Is_Type (U_Ent)
3010 and then Ekind (U_Ent) /= E_Variable
3011 and then Ekind (U_Ent) /= E_Constant
3013 Error_Msg_N ("size cannot be given for &", Nam);
3015 elsif Is_Array_Type (U_Ent)
3016 and then not Is_Constrained (U_Ent)
3019 ("size cannot be given for unconstrained array", Nam);
3021 elsif Size /= No_Uint then
3022 if VM_Target /= No_VM and then not GNAT_Mode then
3024 -- Size clause is not handled properly on VM targets.
3025 -- Display a warning unless we are in GNAT mode, in which
3026 -- case this is useless.
3029 ("?size clauses are ignored in this configuration", N);
3032 if Is_Type (U_Ent) then
3035 Etyp := Etype (U_Ent);
3038 -- Check size, note that Gigi is in charge of checking that the
3039 -- size of an array or record type is OK. Also we do not check
3040 -- the size in the ordinary fixed-point case, since it is too
3041 -- early to do so (there may be subsequent small clause that
3042 -- affects the size). We can check the size if a small clause
3043 -- has already been given.
3045 if not Is_Ordinary_Fixed_Point_Type (U_Ent)
3046 or else Has_Small_Clause (U_Ent)
3048 Check_Size (Expr, Etyp, Size, Biased);
3049 Set_Biased (U_Ent, N, "size clause", Biased);
3052 -- For types set RM_Size and Esize if possible
3054 if Is_Type (U_Ent) then
3055 Set_RM_Size (U_Ent, Size);
3057 -- For elementary types, increase Object_Size to power of 2,
3058 -- but not less than a storage unit in any case (normally
3059 -- this means it will be byte addressable).
3061 -- For all other types, nothing else to do, we leave Esize
3062 -- (object size) unset, the back end will set it from the
3063 -- size and alignment in an appropriate manner.
3065 -- In both cases, we check whether the alignment must be
3066 -- reset in the wake of the size change.
3068 if Is_Elementary_Type (U_Ent) then
3069 if Size <= System_Storage_Unit then
3070 Init_Esize (U_Ent, System_Storage_Unit);
3071 elsif Size <= 16 then
3072 Init_Esize (U_Ent, 16);
3073 elsif Size <= 32 then
3074 Init_Esize (U_Ent, 32);
3076 Set_Esize (U_Ent, (Size + 63) / 64 * 64);
3079 Alignment_Check_For_Size_Change (U_Ent, Esize (U_Ent));
3081 Alignment_Check_For_Size_Change (U_Ent, Size);
3084 -- For objects, set Esize only
3087 if Is_Elementary_Type (Etyp) then
3088 if Size /= System_Storage_Unit
3090 Size /= System_Storage_Unit * 2
3092 Size /= System_Storage_Unit * 4
3094 Size /= System_Storage_Unit * 8
3096 Error_Msg_Uint_1 := UI_From_Int (System_Storage_Unit);
3097 Error_Msg_Uint_2 := Error_Msg_Uint_1 * 8;
3099 ("size for primitive object must be a power of 2"
3100 & " in the range ^-^", N);
3104 Set_Esize (U_Ent, Size);
3107 Set_Has_Size_Clause (U_Ent);
3115 -- Small attribute definition clause
3117 when Attribute_Small => Small : declare
3118 Implicit_Base : constant Entity_Id := Base_Type (U_Ent);
3122 Analyze_And_Resolve (Expr, Any_Real);
3124 if Etype (Expr) = Any_Type then
3127 elsif not Is_Static_Expression (Expr) then
3128 Flag_Non_Static_Expr
3129 ("small requires static expression!", Expr);
3133 Small := Expr_Value_R (Expr);
3135 if Small <= Ureal_0 then
3136 Error_Msg_N ("small value must be greater than zero", Expr);
3142 if not Is_Ordinary_Fixed_Point_Type (U_Ent) then
3144 ("small requires an ordinary fixed point type", Nam);
3146 elsif Has_Small_Clause (U_Ent) then
3147 Error_Msg_N ("small already given for &", Nam);
3149 elsif Small > Delta_Value (U_Ent) then
3151 ("small value must not be greater then delta value", Nam);
3154 Set_Small_Value (U_Ent, Small);
3155 Set_Small_Value (Implicit_Base, Small);
3156 Set_Has_Small_Clause (U_Ent);
3157 Set_Has_Small_Clause (Implicit_Base);
3158 Set_Has_Non_Standard_Rep (Implicit_Base);
3166 -- Storage_Pool attribute definition clause
3168 when Attribute_Storage_Pool | Attribute_Simple_Storage_Pool => declare
3173 if Ekind (U_Ent) = E_Access_Subprogram_Type then
3175 ("storage pool cannot be given for access-to-subprogram type",
3180 Ekind_In (U_Ent, E_Access_Type, E_General_Access_Type)
3183 ("storage pool can only be given for access types", Nam);
3186 elsif Is_Derived_Type (U_Ent) then
3188 ("storage pool cannot be given for a derived access type",
3191 elsif Duplicate_Clause then
3194 elsif Present (Associated_Storage_Pool (U_Ent)) then
3195 Error_Msg_N ("storage pool already given for &", Nam);
3199 if Id = Attribute_Storage_Pool then
3201 (Expr, Class_Wide_Type (RTE (RE_Root_Storage_Pool)));
3203 -- In the Simple_Storage_Pool case, we allow a variable of any
3204 -- simple storage pool type, so we Resolve without imposing an
3208 Analyze_And_Resolve (Expr);
3210 if not Present (Get_Rep_Pragma
3211 (Etype (Expr), Name_Simple_Storage_Pool_Type))
3214 ("expression must be of a simple storage pool type", Expr);
3218 if not Denotes_Variable (Expr) then
3219 Error_Msg_N ("storage pool must be a variable", Expr);
3223 if Nkind (Expr) = N_Type_Conversion then
3224 T := Etype (Expression (Expr));
3229 -- The Stack_Bounded_Pool is used internally for implementing
3230 -- access types with a Storage_Size. Since it only work properly
3231 -- when used on one specific type, we need to check that it is not
3232 -- hijacked improperly:
3234 -- type T is access Integer;
3235 -- for T'Storage_Size use n;
3236 -- type Q is access Float;
3237 -- for Q'Storage_Size use T'Storage_Size; -- incorrect
3239 if RTE_Available (RE_Stack_Bounded_Pool)
3240 and then Base_Type (T) = RTE (RE_Stack_Bounded_Pool)
3242 Error_Msg_N ("non-shareable internal Pool", Expr);
3246 -- If the argument is a name that is not an entity name, then
3247 -- we construct a renaming operation to define an entity of
3248 -- type storage pool.
3250 if not Is_Entity_Name (Expr)
3251 and then Is_Object_Reference (Expr)
3253 Pool := Make_Temporary (Loc, 'P', Expr);
3256 Rnode : constant Node_Id :=
3257 Make_Object_Renaming_Declaration (Loc,
3258 Defining_Identifier => Pool,
3260 New_Occurrence_Of (Etype (Expr), Loc),
3264 Insert_Before (N, Rnode);
3266 Set_Associated_Storage_Pool (U_Ent, Pool);
3269 elsif Is_Entity_Name (Expr) then
3270 Pool := Entity (Expr);
3272 -- If pool is a renamed object, get original one. This can
3273 -- happen with an explicit renaming, and within instances.
3275 while Present (Renamed_Object (Pool))
3276 and then Is_Entity_Name (Renamed_Object (Pool))
3278 Pool := Entity (Renamed_Object (Pool));
3281 if Present (Renamed_Object (Pool))
3282 and then Nkind (Renamed_Object (Pool)) = N_Type_Conversion
3283 and then Is_Entity_Name (Expression (Renamed_Object (Pool)))
3285 Pool := Entity (Expression (Renamed_Object (Pool)));
3288 Set_Associated_Storage_Pool (U_Ent, Pool);
3290 elsif Nkind (Expr) = N_Type_Conversion
3291 and then Is_Entity_Name (Expression (Expr))
3292 and then Nkind (Original_Node (Expr)) = N_Attribute_Reference
3294 Pool := Entity (Expression (Expr));
3295 Set_Associated_Storage_Pool (U_Ent, Pool);
3298 Error_Msg_N ("incorrect reference to a Storage Pool", Expr);
3307 -- Storage_Size attribute definition clause
3309 when Attribute_Storage_Size => Storage_Size : declare
3310 Btype : constant Entity_Id := Base_Type (U_Ent);
3314 if Is_Task_Type (U_Ent) then
3315 Check_Restriction (No_Obsolescent_Features, N);
3317 if Warn_On_Obsolescent_Feature then
3319 ("storage size clause for task is an " &
3320 "obsolescent feature (RM J.9)?", N);
3321 Error_Msg_N ("\use Storage_Size pragma instead?", N);
3327 if not Is_Access_Type (U_Ent)
3328 and then Ekind (U_Ent) /= E_Task_Type
3330 Error_Msg_N ("storage size cannot be given for &", Nam);
3332 elsif Is_Access_Type (U_Ent) and Is_Derived_Type (U_Ent) then
3334 ("storage size cannot be given for a derived access type",
3337 elsif Duplicate_Clause then
3341 Analyze_And_Resolve (Expr, Any_Integer);
3343 if Is_Access_Type (U_Ent) then
3344 if Present (Associated_Storage_Pool (U_Ent)) then
3345 Error_Msg_N ("storage pool already given for &", Nam);
3349 if Is_OK_Static_Expression (Expr)
3350 and then Expr_Value (Expr) = 0
3352 Set_No_Pool_Assigned (Btype);
3355 else -- Is_Task_Type (U_Ent)
3356 Sprag := Get_Rep_Pragma (Btype, Name_Storage_Size);
3358 if Present (Sprag) then
3359 Error_Msg_Sloc := Sloc (Sprag);
3361 ("Storage_Size already specified#", Nam);
3366 Set_Has_Storage_Size_Clause (Btype);
3374 when Attribute_Stream_Size => Stream_Size : declare
3375 Size : constant Uint := Static_Integer (Expr);
3378 if Ada_Version <= Ada_95 then
3379 Check_Restriction (No_Implementation_Attributes, N);
3382 if Duplicate_Clause then
3385 elsif Is_Elementary_Type (U_Ent) then
3386 if Size /= System_Storage_Unit
3388 Size /= System_Storage_Unit * 2
3390 Size /= System_Storage_Unit * 4
3392 Size /= System_Storage_Unit * 8
3394 Error_Msg_Uint_1 := UI_From_Int (System_Storage_Unit);
3396 ("stream size for elementary type must be a"
3397 & " power of 2 and at least ^", N);
3399 elsif RM_Size (U_Ent) > Size then
3400 Error_Msg_Uint_1 := RM_Size (U_Ent);
3402 ("stream size for elementary type must be a"
3403 & " power of 2 and at least ^", N);
3406 Set_Has_Stream_Size_Clause (U_Ent);
3409 Error_Msg_N ("Stream_Size cannot be given for &", Nam);
3417 -- Value_Size attribute definition clause
3419 when Attribute_Value_Size => Value_Size : declare
3420 Size : constant Uint := Static_Integer (Expr);
3424 if not Is_Type (U_Ent) then
3425 Error_Msg_N ("Value_Size cannot be given for &", Nam);
3427 elsif Duplicate_Clause then
3430 elsif Is_Array_Type (U_Ent)
3431 and then not Is_Constrained (U_Ent)
3434 ("Value_Size cannot be given for unconstrained array", Nam);
3437 if Is_Elementary_Type (U_Ent) then
3438 Check_Size (Expr, U_Ent, Size, Biased);
3439 Set_Biased (U_Ent, N, "value size clause", Biased);
3442 Set_RM_Size (U_Ent, Size);
3446 -----------------------
3447 -- Variable_Indexing --
3448 -----------------------
3450 when Attribute_Variable_Indexing =>
3451 Check_Indexing_Functions;
3457 when Attribute_Write =>
3458 Analyze_Stream_TSS_Definition (TSS_Stream_Write);
3459 Set_Has_Specified_Stream_Write (Ent);
3461 -- All other attributes cannot be set
3465 ("attribute& cannot be set with definition clause", N);
3468 -- The test for the type being frozen must be performed after any
3469 -- expression the clause has been analyzed since the expression itself
3470 -- might cause freezing that makes the clause illegal.
3472 if Rep_Item_Too_Late (U_Ent, N, FOnly) then
3475 end Analyze_Attribute_Definition_Clause;
3477 ----------------------------
3478 -- Analyze_Code_Statement --
3479 ----------------------------
3481 procedure Analyze_Code_Statement (N : Node_Id) is
3482 HSS : constant Node_Id := Parent (N);
3483 SBody : constant Node_Id := Parent (HSS);
3484 Subp : constant Entity_Id := Current_Scope;
3491 -- Analyze and check we get right type, note that this implements the
3492 -- requirement (RM 13.8(1)) that Machine_Code be with'ed, since that
3493 -- is the only way that Asm_Insn could possibly be visible.
3495 Analyze_And_Resolve (Expression (N));
3497 if Etype (Expression (N)) = Any_Type then
3499 elsif Etype (Expression (N)) /= RTE (RE_Asm_Insn) then
3500 Error_Msg_N ("incorrect type for code statement", N);
3504 Check_Code_Statement (N);
3506 -- Make sure we appear in the handled statement sequence of a
3507 -- subprogram (RM 13.8(3)).
3509 if Nkind (HSS) /= N_Handled_Sequence_Of_Statements
3510 or else Nkind (SBody) /= N_Subprogram_Body
3513 ("code statement can only appear in body of subprogram", N);
3517 -- Do remaining checks (RM 13.8(3)) if not already done
3519 if not Is_Machine_Code_Subprogram (Subp) then
3520 Set_Is_Machine_Code_Subprogram (Subp);
3522 -- No exception handlers allowed
3524 if Present (Exception_Handlers (HSS)) then
3526 ("exception handlers not permitted in machine code subprogram",
3527 First (Exception_Handlers (HSS)));
3530 -- No declarations other than use clauses and pragmas (we allow
3531 -- certain internally generated declarations as well).
3533 Decl := First (Declarations (SBody));
3534 while Present (Decl) loop
3535 DeclO := Original_Node (Decl);
3536 if Comes_From_Source (DeclO)
3537 and not Nkind_In (DeclO, N_Pragma,
3538 N_Use_Package_Clause,
3540 N_Implicit_Label_Declaration)
3543 ("this declaration not allowed in machine code subprogram",
3550 -- No statements other than code statements, pragmas, and labels.
3551 -- Again we allow certain internally generated statements.
3553 -- In Ada 2012, qualified expressions are names, and the code
3554 -- statement is initially parsed as a procedure call.
3556 Stmt := First (Statements (HSS));
3557 while Present (Stmt) loop
3558 StmtO := Original_Node (Stmt);
3560 -- A procedure call transformed into a code statement is OK.
3562 if Ada_Version >= Ada_2012
3563 and then Nkind (StmtO) = N_Procedure_Call_Statement
3564 and then Nkind (Name (StmtO)) = N_Qualified_Expression
3568 elsif Comes_From_Source (StmtO)
3569 and then not Nkind_In (StmtO, N_Pragma,
3574 ("this statement is not allowed in machine code subprogram",
3581 end Analyze_Code_Statement;
3583 -----------------------------------------------
3584 -- Analyze_Enumeration_Representation_Clause --
3585 -----------------------------------------------
3587 procedure Analyze_Enumeration_Representation_Clause (N : Node_Id) is
3588 Ident : constant Node_Id := Identifier (N);
3589 Aggr : constant Node_Id := Array_Aggregate (N);
3590 Enumtype : Entity_Id;
3597 Err : Boolean := False;
3598 -- Set True to avoid cascade errors and crashes on incorrect source code
3600 Lo : constant Uint := Expr_Value (Type_Low_Bound (Universal_Integer));
3601 Hi : constant Uint := Expr_Value (Type_High_Bound (Universal_Integer));
3602 -- Allowed range of universal integer (= allowed range of enum lit vals)
3606 -- Minimum and maximum values of entries
3609 -- Pointer to node for literal providing max value
3612 if Ignore_Rep_Clauses then
3616 -- First some basic error checks
3619 Enumtype := Entity (Ident);
3621 if Enumtype = Any_Type
3622 or else Rep_Item_Too_Early (Enumtype, N)
3626 Enumtype := Underlying_Type (Enumtype);
3629 if not Is_Enumeration_Type (Enumtype) then
3631 ("enumeration type required, found}",
3632 Ident, First_Subtype (Enumtype));
3636 -- Ignore rep clause on generic actual type. This will already have
3637 -- been flagged on the template as an error, and this is the safest
3638 -- way to ensure we don't get a junk cascaded message in the instance.
3640 if Is_Generic_Actual_Type (Enumtype) then
3643 -- Type must be in current scope
3645 elsif Scope (Enumtype) /= Current_Scope then
3646 Error_Msg_N ("type must be declared in this scope", Ident);
3649 -- Type must be a first subtype
3651 elsif not Is_First_Subtype (Enumtype) then
3652 Error_Msg_N ("cannot give enumeration rep clause for subtype", N);
3655 -- Ignore duplicate rep clause
3657 elsif Has_Enumeration_Rep_Clause (Enumtype) then
3658 Error_Msg_N ("duplicate enumeration rep clause ignored", N);
3661 -- Don't allow rep clause for standard [wide_[wide_]]character
3663 elsif Is_Standard_Character_Type (Enumtype) then
3664 Error_Msg_N ("enumeration rep clause not allowed for this type", N);
3667 -- Check that the expression is a proper aggregate (no parentheses)
3669 elsif Paren_Count (Aggr) /= 0 then
3671 ("extra parentheses surrounding aggregate not allowed",
3675 -- All tests passed, so set rep clause in place
3678 Set_Has_Enumeration_Rep_Clause (Enumtype);
3679 Set_Has_Enumeration_Rep_Clause (Base_Type (Enumtype));
3682 -- Now we process the aggregate. Note that we don't use the normal
3683 -- aggregate code for this purpose, because we don't want any of the
3684 -- normal expansion activities, and a number of special semantic
3685 -- rules apply (including the component type being any integer type)
3687 Elit := First_Literal (Enumtype);
3689 -- First the positional entries if any
3691 if Present (Expressions (Aggr)) then
3692 Expr := First (Expressions (Aggr));
3693 while Present (Expr) loop
3695 Error_Msg_N ("too many entries in aggregate", Expr);
3699 Val := Static_Integer (Expr);
3701 -- Err signals that we found some incorrect entries processing
3702 -- the list. The final checks for completeness and ordering are
3703 -- skipped in this case.
3705 if Val = No_Uint then
3707 elsif Val < Lo or else Hi < Val then
3708 Error_Msg_N ("value outside permitted range", Expr);
3712 Set_Enumeration_Rep (Elit, Val);
3713 Set_Enumeration_Rep_Expr (Elit, Expr);
3719 -- Now process the named entries if present
3721 if Present (Component_Associations (Aggr)) then
3722 Assoc := First (Component_Associations (Aggr));
3723 while Present (Assoc) loop
3724 Choice := First (Choices (Assoc));
3726 if Present (Next (Choice)) then
3728 ("multiple choice not allowed here", Next (Choice));
3732 if Nkind (Choice) = N_Others_Choice then
3733 Error_Msg_N ("others choice not allowed here", Choice);
3736 elsif Nkind (Choice) = N_Range then
3738 -- ??? should allow zero/one element range here
3740 Error_Msg_N ("range not allowed here", Choice);
3744 Analyze_And_Resolve (Choice, Enumtype);
3746 if Error_Posted (Choice) then
3751 if Is_Entity_Name (Choice)
3752 and then Is_Type (Entity (Choice))
3754 Error_Msg_N ("subtype name not allowed here", Choice);
3757 -- ??? should allow static subtype with zero/one entry
3759 elsif Etype (Choice) = Base_Type (Enumtype) then
3760 if not Is_Static_Expression (Choice) then
3761 Flag_Non_Static_Expr
3762 ("non-static expression used for choice!", Choice);
3766 Elit := Expr_Value_E (Choice);
3768 if Present (Enumeration_Rep_Expr (Elit)) then
3770 Sloc (Enumeration_Rep_Expr (Elit));
3772 ("representation for& previously given#",
3777 Set_Enumeration_Rep_Expr (Elit, Expression (Assoc));
3779 Expr := Expression (Assoc);
3780 Val := Static_Integer (Expr);
3782 if Val = No_Uint then
3785 elsif Val < Lo or else Hi < Val then
3786 Error_Msg_N ("value outside permitted range", Expr);
3790 Set_Enumeration_Rep (Elit, Val);
3800 -- Aggregate is fully processed. Now we check that a full set of
3801 -- representations was given, and that they are in range and in order.
3802 -- These checks are only done if no other errors occurred.
3808 Elit := First_Literal (Enumtype);
3809 while Present (Elit) loop
3810 if No (Enumeration_Rep_Expr (Elit)) then
3811 Error_Msg_NE ("missing representation for&!", N, Elit);
3814 Val := Enumeration_Rep (Elit);
3816 if Min = No_Uint then
3820 if Val /= No_Uint then
3821 if Max /= No_Uint and then Val <= Max then
3823 ("enumeration value for& not ordered!",
3824 Enumeration_Rep_Expr (Elit), Elit);
3827 Max_Node := Enumeration_Rep_Expr (Elit);
3831 -- If there is at least one literal whose representation is not
3832 -- equal to the Pos value, then note that this enumeration type
3833 -- has a non-standard representation.
3835 if Val /= Enumeration_Pos (Elit) then
3836 Set_Has_Non_Standard_Rep (Base_Type (Enumtype));
3843 -- Now set proper size information
3846 Minsize : Uint := UI_From_Int (Minimum_Size (Enumtype));
3849 if Has_Size_Clause (Enumtype) then
3851 -- All OK, if size is OK now
3853 if RM_Size (Enumtype) >= Minsize then
3857 -- Try if we can get by with biasing
3860 UI_From_Int (Minimum_Size (Enumtype, Biased => True));
3862 -- Error message if even biasing does not work
3864 if RM_Size (Enumtype) < Minsize then
3865 Error_Msg_Uint_1 := RM_Size (Enumtype);
3866 Error_Msg_Uint_2 := Max;
3868 ("previously given size (^) is too small "
3869 & "for this value (^)", Max_Node);
3871 -- If biasing worked, indicate that we now have biased rep
3875 (Enumtype, Size_Clause (Enumtype), "size clause");
3880 Set_RM_Size (Enumtype, Minsize);
3881 Set_Enum_Esize (Enumtype);
3884 Set_RM_Size (Base_Type (Enumtype), RM_Size (Enumtype));
3885 Set_Esize (Base_Type (Enumtype), Esize (Enumtype));
3886 Set_Alignment (Base_Type (Enumtype), Alignment (Enumtype));
3890 -- We repeat the too late test in case it froze itself!
3892 if Rep_Item_Too_Late (Enumtype, N) then
3895 end Analyze_Enumeration_Representation_Clause;
3897 ----------------------------
3898 -- Analyze_Free_Statement --
3899 ----------------------------
3901 procedure Analyze_Free_Statement (N : Node_Id) is
3903 Analyze (Expression (N));
3904 end Analyze_Free_Statement;
3906 ---------------------------
3907 -- Analyze_Freeze_Entity --
3908 ---------------------------
3910 procedure Analyze_Freeze_Entity (N : Node_Id) is
3911 E : constant Entity_Id := Entity (N);
3914 -- Remember that we are processing a freezing entity. Required to
3915 -- ensure correct decoration of internal entities associated with
3916 -- interfaces (see New_Overloaded_Entity).
3918 Inside_Freezing_Actions := Inside_Freezing_Actions + 1;
3920 -- For tagged types covering interfaces add internal entities that link
3921 -- the primitives of the interfaces with the primitives that cover them.
3922 -- Note: These entities were originally generated only when generating
3923 -- code because their main purpose was to provide support to initialize
3924 -- the secondary dispatch tables. They are now generated also when
3925 -- compiling with no code generation to provide ASIS the relationship
3926 -- between interface primitives and tagged type primitives. They are
3927 -- also used to locate primitives covering interfaces when processing
3928 -- generics (see Derive_Subprograms).
3930 if Ada_Version >= Ada_2005
3931 and then Ekind (E) = E_Record_Type
3932 and then Is_Tagged_Type (E)
3933 and then not Is_Interface (E)
3934 and then Has_Interfaces (E)
3936 -- This would be a good common place to call the routine that checks
3937 -- overriding of interface primitives (and thus factorize calls to
3938 -- Check_Abstract_Overriding located at different contexts in the
3939 -- compiler). However, this is not possible because it causes
3940 -- spurious errors in case of late overriding.
3942 Add_Internal_Interface_Entities (E);
3947 if Ekind (E) = E_Record_Type
3948 and then Is_CPP_Class (E)
3949 and then Is_Tagged_Type (E)
3950 and then Tagged_Type_Expansion
3951 and then Expander_Active
3953 if CPP_Num_Prims (E) = 0 then
3955 -- If the CPP type has user defined components then it must import
3956 -- primitives from C++. This is required because if the C++ class
3957 -- has no primitives then the C++ compiler does not added the _tag
3958 -- component to the type.
3960 pragma Assert (Chars (First_Entity (E)) = Name_uTag);
3962 if First_Entity (E) /= Last_Entity (E) then
3964 ("?'C'P'P type must import at least one primitive from C++",
3969 -- Check that all its primitives are abstract or imported from C++.
3970 -- Check also availability of the C++ constructor.
3973 Has_Constructors : constant Boolean := Has_CPP_Constructors (E);
3975 Error_Reported : Boolean := False;
3979 Elmt := First_Elmt (Primitive_Operations (E));
3980 while Present (Elmt) loop
3981 Prim := Node (Elmt);
3983 if Comes_From_Source (Prim) then
3984 if Is_Abstract_Subprogram (Prim) then
3987 elsif not Is_Imported (Prim)
3988 or else Convention (Prim) /= Convention_CPP
3991 ("?primitives of 'C'P'P types must be imported from C++"
3992 & " or abstract", Prim);
3994 elsif not Has_Constructors
3995 and then not Error_Reported
3997 Error_Msg_Name_1 := Chars (E);
3999 ("?'C'P'P constructor required for type %", Prim);
4000 Error_Reported := True;
4009 Inside_Freezing_Actions := Inside_Freezing_Actions - 1;
4011 -- If we have a type with predicates, build predicate function
4013 if Is_Type (E) and then Has_Predicates (E) then
4014 Build_Predicate_Function (E, N);
4017 -- If type has delayed aspects, this is where we do the preanalysis at
4018 -- the freeze point, as part of the consistent visibility check. Note
4019 -- that this must be done after calling Build_Predicate_Function or
4020 -- Build_Invariant_Procedure since these subprograms fix occurrences of
4021 -- the subtype name in the saved expression so that they will not cause
4022 -- trouble in the preanalysis.
4024 if Has_Delayed_Aspects (E) then
4029 -- Look for aspect specification entries for this entity
4031 Ritem := First_Rep_Item (E);
4032 while Present (Ritem) loop
4033 if Nkind (Ritem) = N_Aspect_Specification
4034 and then Entity (Ritem) = E
4035 and then Is_Delayed_Aspect (Ritem)
4036 and then Scope (E) = Current_Scope
4038 Check_Aspect_At_Freeze_Point (Ritem);
4041 Next_Rep_Item (Ritem);
4045 end Analyze_Freeze_Entity;
4047 ------------------------------------------
4048 -- Analyze_Record_Representation_Clause --
4049 ------------------------------------------
4051 -- Note: we check as much as we can here, but we can't do any checks
4052 -- based on the position values (e.g. overlap checks) until freeze time
4053 -- because especially in Ada 2005 (machine scalar mode), the processing
4054 -- for non-standard bit order can substantially change the positions.
4055 -- See procedure Check_Record_Representation_Clause (called from Freeze)
4056 -- for the remainder of this processing.
4058 procedure Analyze_Record_Representation_Clause (N : Node_Id) is
4059 Ident : constant Node_Id := Identifier (N);
4064 Hbit : Uint := Uint_0;
4068 Rectype : Entity_Id;
4070 CR_Pragma : Node_Id := Empty;
4071 -- Points to N_Pragma node if Complete_Representation pragma present
4074 if Ignore_Rep_Clauses then
4079 Rectype := Entity (Ident);
4081 if Rectype = Any_Type
4082 or else Rep_Item_Too_Early (Rectype, N)
4086 Rectype := Underlying_Type (Rectype);
4089 -- First some basic error checks
4091 if not Is_Record_Type (Rectype) then
4093 ("record type required, found}", Ident, First_Subtype (Rectype));
4096 elsif Scope (Rectype) /= Current_Scope then
4097 Error_Msg_N ("type must be declared in this scope", N);
4100 elsif not Is_First_Subtype (Rectype) then
4101 Error_Msg_N ("cannot give record rep clause for subtype", N);
4104 elsif Has_Record_Rep_Clause (Rectype) then
4105 Error_Msg_N ("duplicate record rep clause ignored", N);
4108 elsif Rep_Item_Too_Late (Rectype, N) then
4112 if Present (Mod_Clause (N)) then
4114 Loc : constant Source_Ptr := Sloc (N);
4115 M : constant Node_Id := Mod_Clause (N);
4116 P : constant List_Id := Pragmas_Before (M);
4120 pragma Warnings (Off, Mod_Val);
4123 Check_Restriction (No_Obsolescent_Features, Mod_Clause (N));
4125 if Warn_On_Obsolescent_Feature then
4127 ("mod clause is an obsolescent feature (RM J.8)?", N);
4129 ("\use alignment attribute definition clause instead?", N);
4136 -- In ASIS_Mode mode, expansion is disabled, but we must convert
4137 -- the Mod clause into an alignment clause anyway, so that the
4138 -- back-end can compute and back-annotate properly the size and
4139 -- alignment of types that may include this record.
4141 -- This seems dubious, this destroys the source tree in a manner
4142 -- not detectable by ASIS ???
4144 if Operating_Mode = Check_Semantics and then ASIS_Mode then
4146 Make_Attribute_Definition_Clause (Loc,
4147 Name => New_Reference_To (Base_Type (Rectype), Loc),
4148 Chars => Name_Alignment,
4149 Expression => Relocate_Node (Expression (M)));
4151 Set_From_At_Mod (AtM_Nod);
4152 Insert_After (N, AtM_Nod);
4153 Mod_Val := Get_Alignment_Value (Expression (AtM_Nod));
4154 Set_Mod_Clause (N, Empty);
4157 -- Get the alignment value to perform error checking
4159 Mod_Val := Get_Alignment_Value (Expression (M));
4164 -- For untagged types, clear any existing component clauses for the
4165 -- type. If the type is derived, this is what allows us to override
4166 -- a rep clause for the parent. For type extensions, the representation
4167 -- of the inherited components is inherited, so we want to keep previous
4168 -- component clauses for completeness.
4170 if not Is_Tagged_Type (Rectype) then
4171 Comp := First_Component_Or_Discriminant (Rectype);
4172 while Present (Comp) loop
4173 Set_Component_Clause (Comp, Empty);
4174 Next_Component_Or_Discriminant (Comp);
4178 -- All done if no component clauses
4180 CC := First (Component_Clauses (N));
4186 -- A representation like this applies to the base type
4188 Set_Has_Record_Rep_Clause (Base_Type (Rectype));
4189 Set_Has_Non_Standard_Rep (Base_Type (Rectype));
4190 Set_Has_Specified_Layout (Base_Type (Rectype));
4192 -- Process the component clauses
4194 while Present (CC) loop
4198 if Nkind (CC) = N_Pragma then
4201 -- The only pragma of interest is Complete_Representation
4203 if Pragma_Name (CC) = Name_Complete_Representation then
4207 -- Processing for real component clause
4210 Posit := Static_Integer (Position (CC));
4211 Fbit := Static_Integer (First_Bit (CC));
4212 Lbit := Static_Integer (Last_Bit (CC));
4215 and then Fbit /= No_Uint
4216 and then Lbit /= No_Uint
4220 ("position cannot be negative", Position (CC));
4224 ("first bit cannot be negative", First_Bit (CC));
4226 -- The Last_Bit specified in a component clause must not be
4227 -- less than the First_Bit minus one (RM-13.5.1(10)).
4229 elsif Lbit < Fbit - 1 then
4231 ("last bit cannot be less than first bit minus one",
4234 -- Values look OK, so find the corresponding record component
4235 -- Even though the syntax allows an attribute reference for
4236 -- implementation-defined components, GNAT does not allow the
4237 -- tag to get an explicit position.
4239 elsif Nkind (Component_Name (CC)) = N_Attribute_Reference then
4240 if Attribute_Name (Component_Name (CC)) = Name_Tag then
4241 Error_Msg_N ("position of tag cannot be specified", CC);
4243 Error_Msg_N ("illegal component name", CC);
4247 Comp := First_Entity (Rectype);
4248 while Present (Comp) loop
4249 exit when Chars (Comp) = Chars (Component_Name (CC));
4255 -- Maybe component of base type that is absent from
4256 -- statically constrained first subtype.
4258 Comp := First_Entity (Base_Type (Rectype));
4259 while Present (Comp) loop
4260 exit when Chars (Comp) = Chars (Component_Name (CC));
4267 ("component clause is for non-existent field", CC);
4269 -- Ada 2012 (AI05-0026): Any name that denotes a
4270 -- discriminant of an object of an unchecked union type
4271 -- shall not occur within a record_representation_clause.
4273 -- The general restriction of using record rep clauses on
4274 -- Unchecked_Union types has now been lifted. Since it is
4275 -- possible to introduce a record rep clause which mentions
4276 -- the discriminant of an Unchecked_Union in non-Ada 2012
4277 -- code, this check is applied to all versions of the
4280 elsif Ekind (Comp) = E_Discriminant
4281 and then Is_Unchecked_Union (Rectype)
4284 ("cannot reference discriminant of Unchecked_Union",
4285 Component_Name (CC));
4287 elsif Present (Component_Clause (Comp)) then
4289 -- Diagnose duplicate rep clause, or check consistency
4290 -- if this is an inherited component. In a double fault,
4291 -- there may be a duplicate inconsistent clause for an
4292 -- inherited component.
4294 if Scope (Original_Record_Component (Comp)) = Rectype
4295 or else Parent (Component_Clause (Comp)) = N
4297 Error_Msg_Sloc := Sloc (Component_Clause (Comp));
4298 Error_Msg_N ("component clause previously given#", CC);
4302 Rep1 : constant Node_Id := Component_Clause (Comp);
4304 if Intval (Position (Rep1)) /=
4305 Intval (Position (CC))
4306 or else Intval (First_Bit (Rep1)) /=
4307 Intval (First_Bit (CC))
4308 or else Intval (Last_Bit (Rep1)) /=
4309 Intval (Last_Bit (CC))
4311 Error_Msg_N ("component clause inconsistent "
4312 & "with representation of ancestor", CC);
4313 elsif Warn_On_Redundant_Constructs then
4314 Error_Msg_N ("?redundant component clause "
4315 & "for inherited component!", CC);
4320 -- Normal case where this is the first component clause we
4321 -- have seen for this entity, so set it up properly.
4324 -- Make reference for field in record rep clause and set
4325 -- appropriate entity field in the field identifier.
4328 (Comp, Component_Name (CC), Set_Ref => False);
4329 Set_Entity (Component_Name (CC), Comp);
4331 -- Update Fbit and Lbit to the actual bit number
4333 Fbit := Fbit + UI_From_Int (SSU) * Posit;
4334 Lbit := Lbit + UI_From_Int (SSU) * Posit;
4336 if Has_Size_Clause (Rectype)
4337 and then RM_Size (Rectype) <= Lbit
4340 ("bit number out of range of specified size",
4343 Set_Component_Clause (Comp, CC);
4344 Set_Component_Bit_Offset (Comp, Fbit);
4345 Set_Esize (Comp, 1 + (Lbit - Fbit));
4346 Set_Normalized_First_Bit (Comp, Fbit mod SSU);
4347 Set_Normalized_Position (Comp, Fbit / SSU);
4349 if Warn_On_Overridden_Size
4350 and then Has_Size_Clause (Etype (Comp))
4351 and then RM_Size (Etype (Comp)) /= Esize (Comp)
4354 ("?component size overrides size clause for&",
4355 Component_Name (CC), Etype (Comp));
4358 -- This information is also set in the corresponding
4359 -- component of the base type, found by accessing the
4360 -- Original_Record_Component link if it is present.
4362 Ocomp := Original_Record_Component (Comp);
4369 (Component_Name (CC),
4375 (Comp, First_Node (CC), "component clause", Biased);
4377 if Present (Ocomp) then
4378 Set_Component_Clause (Ocomp, CC);
4379 Set_Component_Bit_Offset (Ocomp, Fbit);
4380 Set_Normalized_First_Bit (Ocomp, Fbit mod SSU);
4381 Set_Normalized_Position (Ocomp, Fbit / SSU);
4382 Set_Esize (Ocomp, 1 + (Lbit - Fbit));
4384 Set_Normalized_Position_Max
4385 (Ocomp, Normalized_Position (Ocomp));
4387 -- Note: we don't use Set_Biased here, because we
4388 -- already gave a warning above if needed, and we
4389 -- would get a duplicate for the same name here.
4391 Set_Has_Biased_Representation
4392 (Ocomp, Has_Biased_Representation (Comp));
4395 if Esize (Comp) < 0 then
4396 Error_Msg_N ("component size is negative", CC);
4407 -- Check missing components if Complete_Representation pragma appeared
4409 if Present (CR_Pragma) then
4410 Comp := First_Component_Or_Discriminant (Rectype);
4411 while Present (Comp) loop
4412 if No (Component_Clause (Comp)) then
4414 ("missing component clause for &", CR_Pragma, Comp);
4417 Next_Component_Or_Discriminant (Comp);
4420 -- If no Complete_Representation pragma, warn if missing components
4422 elsif Warn_On_Unrepped_Components then
4424 Num_Repped_Components : Nat := 0;
4425 Num_Unrepped_Components : Nat := 0;
4428 -- First count number of repped and unrepped components
4430 Comp := First_Component_Or_Discriminant (Rectype);
4431 while Present (Comp) loop
4432 if Present (Component_Clause (Comp)) then
4433 Num_Repped_Components := Num_Repped_Components + 1;
4435 Num_Unrepped_Components := Num_Unrepped_Components + 1;
4438 Next_Component_Or_Discriminant (Comp);
4441 -- We are only interested in the case where there is at least one
4442 -- unrepped component, and at least half the components have rep
4443 -- clauses. We figure that if less than half have them, then the
4444 -- partial rep clause is really intentional. If the component
4445 -- type has no underlying type set at this point (as for a generic
4446 -- formal type), we don't know enough to give a warning on the
4449 if Num_Unrepped_Components > 0
4450 and then Num_Unrepped_Components < Num_Repped_Components
4452 Comp := First_Component_Or_Discriminant (Rectype);
4453 while Present (Comp) loop
4454 if No (Component_Clause (Comp))
4455 and then Comes_From_Source (Comp)
4456 and then Present (Underlying_Type (Etype (Comp)))
4457 and then (Is_Scalar_Type (Underlying_Type (Etype (Comp)))
4458 or else Size_Known_At_Compile_Time
4459 (Underlying_Type (Etype (Comp))))
4460 and then not Has_Warnings_Off (Rectype)
4462 Error_Msg_Sloc := Sloc (Comp);
4464 ("?no component clause given for & declared #",
4468 Next_Component_Or_Discriminant (Comp);
4473 end Analyze_Record_Representation_Clause;
4475 -------------------------------
4476 -- Build_Invariant_Procedure --
4477 -------------------------------
4479 -- The procedure that is constructed here has the form
4481 -- procedure typInvariant (Ixxx : typ) is
4483 -- pragma Check (Invariant, exp, "failed invariant from xxx");
4484 -- pragma Check (Invariant, exp, "failed invariant from xxx");
4486 -- pragma Check (Invariant, exp, "failed inherited invariant from xxx");
4488 -- end typInvariant;
4490 procedure Build_Invariant_Procedure (Typ : Entity_Id; N : Node_Id) is
4491 Loc : constant Source_Ptr := Sloc (Typ);
4498 Visible_Decls : constant List_Id := Visible_Declarations (N);
4499 Private_Decls : constant List_Id := Private_Declarations (N);
4501 procedure Add_Invariants (T : Entity_Id; Inherit : Boolean);
4502 -- Appends statements to Stmts for any invariants in the rep item chain
4503 -- of the given type. If Inherit is False, then we only process entries
4504 -- on the chain for the type Typ. If Inherit is True, then we ignore any
4505 -- Invariant aspects, but we process all Invariant'Class aspects, adding
4506 -- "inherited" to the exception message and generating an informational
4507 -- message about the inheritance of an invariant.
4509 Object_Name : constant Name_Id := New_Internal_Name ('I');
4510 -- Name for argument of invariant procedure
4512 Object_Entity : constant Node_Id :=
4513 Make_Defining_Identifier (Loc, Object_Name);
4514 -- The procedure declaration entity for the argument
4516 --------------------
4517 -- Add_Invariants --
4518 --------------------
4520 procedure Add_Invariants (T : Entity_Id; Inherit : Boolean) is
4530 procedure Replace_Type_Reference (N : Node_Id);
4531 -- Replace a single occurrence N of the subtype name with a reference
4532 -- to the formal of the predicate function. N can be an identifier
4533 -- referencing the subtype, or a selected component, representing an
4534 -- appropriately qualified occurrence of the subtype name.
4536 procedure Replace_Type_References is
4537 new Replace_Type_References_Generic (Replace_Type_Reference);
4538 -- Traverse an expression replacing all occurrences of the subtype
4539 -- name with appropriate references to the object that is the formal
4540 -- parameter of the predicate function. Note that we must ensure
4541 -- that the type and entity information is properly set in the
4542 -- replacement node, since we will do a Preanalyze call of this
4543 -- expression without proper visibility of the procedure argument.
4545 ----------------------------
4546 -- Replace_Type_Reference --
4547 ----------------------------
4549 procedure Replace_Type_Reference (N : Node_Id) is
4551 -- Invariant'Class, replace with T'Class (obj)
4553 if Class_Present (Ritem) then
4555 Make_Type_Conversion (Loc,
4557 Make_Attribute_Reference (Loc,
4558 Prefix => New_Occurrence_Of (T, Loc),
4559 Attribute_Name => Name_Class),
4560 Expression => Make_Identifier (Loc, Object_Name)));
4562 Set_Entity (Expression (N), Object_Entity);
4563 Set_Etype (Expression (N), Typ);
4565 -- Invariant, replace with obj
4568 Rewrite (N, Make_Identifier (Loc, Object_Name));
4569 Set_Entity (N, Object_Entity);
4572 end Replace_Type_Reference;
4574 -- Start of processing for Add_Invariants
4577 Ritem := First_Rep_Item (T);
4578 while Present (Ritem) loop
4579 if Nkind (Ritem) = N_Pragma
4580 and then Pragma_Name (Ritem) = Name_Invariant
4582 Arg1 := First (Pragma_Argument_Associations (Ritem));
4583 Arg2 := Next (Arg1);
4584 Arg3 := Next (Arg2);
4586 Arg1 := Get_Pragma_Arg (Arg1);
4587 Arg2 := Get_Pragma_Arg (Arg2);
4589 -- For Inherit case, ignore Invariant, process only Class case
4592 if not Class_Present (Ritem) then
4596 -- For Inherit false, process only item for right type
4599 if Entity (Arg1) /= Typ then
4605 Stmts := Empty_List;
4608 Exp := New_Copy_Tree (Arg2);
4611 -- We need to replace any occurrences of the name of the type
4612 -- with references to the object, converted to type'Class in
4613 -- the case of Invariant'Class aspects.
4615 Replace_Type_References (Exp, Chars (T));
4617 -- If this invariant comes from an aspect, find the aspect
4618 -- specification, and replace the saved expression because
4619 -- we need the subtype references replaced for the calls to
4620 -- Preanalyze_Spec_Expressin in Check_Aspect_At_Freeze_Point
4621 -- and Check_Aspect_At_End_Of_Declarations.
4623 if From_Aspect_Specification (Ritem) then
4628 -- Loop to find corresponding aspect, note that this
4629 -- must be present given the pragma is marked delayed.
4631 Aitem := Next_Rep_Item (Ritem);
4632 while Present (Aitem) loop
4633 if Nkind (Aitem) = N_Aspect_Specification
4634 and then Aspect_Rep_Item (Aitem) = Ritem
4637 (Identifier (Aitem), New_Copy_Tree (Exp));
4641 Aitem := Next_Rep_Item (Aitem);
4646 -- Now we need to preanalyze the expression to properly capture
4647 -- the visibility in the visible part. The expression will not
4648 -- be analyzed for real until the body is analyzed, but that is
4649 -- at the end of the private part and has the wrong visibility.
4651 Set_Parent (Exp, N);
4652 Preanalyze_Spec_Expression (Exp, Standard_Boolean);
4654 -- Build first two arguments for Check pragma
4657 Make_Pragma_Argument_Association (Loc,
4658 Expression => Make_Identifier (Loc, Name_Invariant)),
4659 Make_Pragma_Argument_Association (Loc, Expression => Exp));
4661 -- Add message if present in Invariant pragma
4663 if Present (Arg3) then
4664 Str := Strval (Get_Pragma_Arg (Arg3));
4666 -- If inherited case, and message starts "failed invariant",
4667 -- change it to be "failed inherited invariant".
4670 String_To_Name_Buffer (Str);
4672 if Name_Buffer (1 .. 16) = "failed invariant" then
4673 Insert_Str_In_Name_Buffer ("inherited ", 8);
4674 Str := String_From_Name_Buffer;
4679 Make_Pragma_Argument_Association (Loc,
4680 Expression => Make_String_Literal (Loc, Str)));
4683 -- Add Check pragma to list of statements
4687 Pragma_Identifier =>
4688 Make_Identifier (Loc, Name_Check),
4689 Pragma_Argument_Associations => Assoc));
4691 -- If Inherited case and option enabled, output info msg. Note
4692 -- that we know this is a case of Invariant'Class.
4694 if Inherit and Opt.List_Inherited_Aspects then
4695 Error_Msg_Sloc := Sloc (Ritem);
4697 ("?info: & inherits `Invariant''Class` aspect from #",
4703 Next_Rep_Item (Ritem);
4707 -- Start of processing for Build_Invariant_Procedure
4713 Set_Etype (Object_Entity, Typ);
4715 -- Add invariants for the current type
4717 Add_Invariants (Typ, Inherit => False);
4719 -- Add invariants for parent types
4722 Current_Typ : Entity_Id;
4723 Parent_Typ : Entity_Id;
4728 Parent_Typ := Etype (Current_Typ);
4730 if Is_Private_Type (Parent_Typ)
4731 and then Present (Full_View (Base_Type (Parent_Typ)))
4733 Parent_Typ := Full_View (Base_Type (Parent_Typ));
4736 exit when Parent_Typ = Current_Typ;
4738 Current_Typ := Parent_Typ;
4739 Add_Invariants (Current_Typ, Inherit => True);
4743 -- Build the procedure if we generated at least one Check pragma
4745 if Stmts /= No_List then
4747 -- Build procedure declaration
4750 Make_Defining_Identifier (Loc,
4751 Chars => New_External_Name (Chars (Typ), "Invariant"));
4752 Set_Has_Invariants (SId);
4753 Set_Invariant_Procedure (Typ, SId);
4756 Make_Procedure_Specification (Loc,
4757 Defining_Unit_Name => SId,
4758 Parameter_Specifications => New_List (
4759 Make_Parameter_Specification (Loc,
4760 Defining_Identifier => Object_Entity,
4761 Parameter_Type => New_Occurrence_Of (Typ, Loc))));
4763 PDecl := Make_Subprogram_Declaration (Loc, Specification => Spec);
4765 -- Build procedure body
4768 Make_Defining_Identifier (Loc,
4769 Chars => New_External_Name (Chars (Typ), "Invariant"));
4772 Make_Procedure_Specification (Loc,
4773 Defining_Unit_Name => SId,
4774 Parameter_Specifications => New_List (
4775 Make_Parameter_Specification (Loc,
4776 Defining_Identifier =>
4777 Make_Defining_Identifier (Loc, Object_Name),
4778 Parameter_Type => New_Occurrence_Of (Typ, Loc))));
4781 Make_Subprogram_Body (Loc,
4782 Specification => Spec,
4783 Declarations => Empty_List,
4784 Handled_Statement_Sequence =>
4785 Make_Handled_Sequence_Of_Statements (Loc,
4786 Statements => Stmts));
4788 -- Insert procedure declaration and spec at the appropriate points.
4789 -- Skip this if there are no private declarations (that's an error
4790 -- that will be diagnosed elsewhere, and there is no point in having
4791 -- an invariant procedure set if the full declaration is missing).
4793 if Present (Private_Decls) then
4795 -- The spec goes at the end of visible declarations, but they have
4796 -- already been analyzed, so we need to explicitly do the analyze.
4798 Append_To (Visible_Decls, PDecl);
4801 -- The body goes at the end of the private declarations, which we
4802 -- have not analyzed yet, so we do not need to perform an explicit
4803 -- analyze call. We skip this if there are no private declarations
4804 -- (this is an error that will be caught elsewhere);
4806 Append_To (Private_Decls, PBody);
4808 -- If the invariant appears on the full view of a type, the
4809 -- analysis of the private part is complete, and we must
4810 -- analyze the new body explicitly.
4812 if In_Private_Part (Current_Scope) then
4817 end Build_Invariant_Procedure;
4819 ------------------------------
4820 -- Build_Predicate_Function --
4821 ------------------------------
4823 -- The procedure that is constructed here has the form
4825 -- function typPredicate (Ixxx : typ) return Boolean is
4828 -- exp1 and then exp2 and then ...
4829 -- and then typ1Predicate (typ1 (Ixxx))
4830 -- and then typ2Predicate (typ2 (Ixxx))
4832 -- end typPredicate;
4834 -- Here exp1, and exp2 are expressions from Predicate pragmas. Note that
4835 -- this is the point at which these expressions get analyzed, providing the
4836 -- required delay, and typ1, typ2, are entities from which predicates are
4837 -- inherited. Note that we do NOT generate Check pragmas, that's because we
4838 -- use this function even if checks are off, e.g. for membership tests.
4840 procedure Build_Predicate_Function (Typ : Entity_Id; N : Node_Id) is
4841 Loc : constant Source_Ptr := Sloc (Typ);
4848 -- This is the expression for the return statement in the function. It
4849 -- is build by connecting the component predicates with AND THEN.
4851 procedure Add_Call (T : Entity_Id);
4852 -- Includes a call to the predicate function for type T in Expr if T
4853 -- has predicates and Predicate_Function (T) is non-empty.
4855 procedure Add_Predicates;
4856 -- Appends expressions for any Predicate pragmas in the rep item chain
4857 -- Typ to Expr. Note that we look only at items for this exact entity.
4858 -- Inheritance of predicates for the parent type is done by calling the
4859 -- Predicate_Function of the parent type, using Add_Call above.
4861 Object_Name : constant Name_Id := New_Internal_Name ('I');
4862 -- Name for argument of Predicate procedure
4864 Object_Entity : constant Entity_Id :=
4865 Make_Defining_Identifier (Loc, Object_Name);
4866 -- The entity for the spec entity for the argument
4868 Dynamic_Predicate_Present : Boolean := False;
4869 -- Set True if a dynamic predicate is present, results in the entire
4870 -- predicate being considered dynamic even if it looks static
4872 Static_Predicate_Present : Node_Id := Empty;
4873 -- Set to N_Pragma node for a static predicate if one is encountered.
4879 procedure Add_Call (T : Entity_Id) is
4883 if Present (T) and then Present (Predicate_Function (T)) then
4884 Set_Has_Predicates (Typ);
4886 -- Build the call to the predicate function of T
4890 (T, Convert_To (T, Make_Identifier (Loc, Object_Name)));
4892 -- Add call to evolving expression, using AND THEN if needed
4899 Left_Opnd => Relocate_Node (Expr),
4903 -- Output info message on inheritance if required. Note we do not
4904 -- give this information for generic actual types, since it is
4905 -- unwelcome noise in that case in instantiations. We also
4906 -- generally suppress the message in instantiations, and also
4907 -- if it involves internal names.
4909 if Opt.List_Inherited_Aspects
4910 and then not Is_Generic_Actual_Type (Typ)
4911 and then Instantiation_Depth (Sloc (Typ)) = 0
4912 and then not Is_Internal_Name (Chars (T))
4913 and then not Is_Internal_Name (Chars (Typ))
4915 Error_Msg_Sloc := Sloc (Predicate_Function (T));
4916 Error_Msg_Node_2 := T;
4917 Error_Msg_N ("?info: & inherits predicate from & #", Typ);
4922 --------------------
4923 -- Add_Predicates --
4924 --------------------
4926 procedure Add_Predicates is
4931 procedure Replace_Type_Reference (N : Node_Id);
4932 -- Replace a single occurrence N of the subtype name with a reference
4933 -- to the formal of the predicate function. N can be an identifier
4934 -- referencing the subtype, or a selected component, representing an
4935 -- appropriately qualified occurrence of the subtype name.
4937 procedure Replace_Type_References is
4938 new Replace_Type_References_Generic (Replace_Type_Reference);
4939 -- Traverse an expression changing every occurrence of an identifier
4940 -- whose name matches the name of the subtype with a reference to
4941 -- the formal parameter of the predicate function.
4943 ----------------------------
4944 -- Replace_Type_Reference --
4945 ----------------------------
4947 procedure Replace_Type_Reference (N : Node_Id) is
4949 Rewrite (N, Make_Identifier (Loc, Object_Name));
4950 Set_Entity (N, Object_Entity);
4952 end Replace_Type_Reference;
4954 -- Start of processing for Add_Predicates
4957 Ritem := First_Rep_Item (Typ);
4958 while Present (Ritem) loop
4959 if Nkind (Ritem) = N_Pragma
4960 and then Pragma_Name (Ritem) = Name_Predicate
4962 if Present (Corresponding_Aspect (Ritem)) then
4963 case Chars (Identifier (Corresponding_Aspect (Ritem))) is
4964 when Name_Dynamic_Predicate =>
4965 Dynamic_Predicate_Present := True;
4966 when Name_Static_Predicate =>
4967 Static_Predicate_Present := Ritem;
4973 -- Acquire arguments
4975 Arg1 := First (Pragma_Argument_Associations (Ritem));
4976 Arg2 := Next (Arg1);
4978 Arg1 := Get_Pragma_Arg (Arg1);
4979 Arg2 := Get_Pragma_Arg (Arg2);
4981 -- See if this predicate pragma is for the current type or for
4982 -- its full view. A predicate on a private completion is placed
4983 -- on the partial view beause this is the visible entity that
4986 if Entity (Arg1) = Typ
4987 or else Full_View (Entity (Arg1)) = Typ
4990 -- We have a match, this entry is for our subtype
4992 -- We need to replace any occurrences of the name of the
4993 -- type with references to the object.
4995 Replace_Type_References (Arg2, Chars (Typ));
4997 -- If this predicate comes from an aspect, find the aspect
4998 -- specification, and replace the saved expression because
4999 -- we need the subtype references replaced for the calls to
5000 -- Preanalyze_Spec_Expressin in Check_Aspect_At_Freeze_Point
5001 -- and Check_Aspect_At_End_Of_Declarations.
5003 if From_Aspect_Specification (Ritem) then
5008 -- Loop to find corresponding aspect, note that this
5009 -- must be present given the pragma is marked delayed.
5011 Aitem := Next_Rep_Item (Ritem);
5013 if Nkind (Aitem) = N_Aspect_Specification
5014 and then Aspect_Rep_Item (Aitem) = Ritem
5017 (Identifier (Aitem), New_Copy_Tree (Arg2));
5021 Aitem := Next_Rep_Item (Aitem);
5026 -- Now we can add the expression
5029 Expr := Relocate_Node (Arg2);
5031 -- There already was a predicate, so add to it
5036 Left_Opnd => Relocate_Node (Expr),
5037 Right_Opnd => Relocate_Node (Arg2));
5042 Next_Rep_Item (Ritem);
5046 -- Start of processing for Build_Predicate_Function
5049 -- Initialize for construction of statement list
5053 -- Return if already built or if type does not have predicates
5055 if not Has_Predicates (Typ)
5056 or else Present (Predicate_Function (Typ))
5061 -- Add Predicates for the current type
5065 -- Add predicates for ancestor if present
5068 Atyp : constant Entity_Id := Nearest_Ancestor (Typ);
5070 if Present (Atyp) then
5075 -- If we have predicates, build the function
5077 if Present (Expr) then
5079 -- Build function declaration
5081 pragma Assert (Has_Predicates (Typ));
5083 Make_Defining_Identifier (Loc,
5084 Chars => New_External_Name (Chars (Typ), "Predicate"));
5085 Set_Has_Predicates (SId);
5086 Set_Predicate_Function (Typ, SId);
5088 -- The predicate function is shared between views of a type.
5090 if Is_Private_Type (Typ) and then Present (Full_View (Typ)) then
5091 Set_Predicate_Function (Full_View (Typ), SId);
5095 Make_Function_Specification (Loc,
5096 Defining_Unit_Name => SId,
5097 Parameter_Specifications => New_List (
5098 Make_Parameter_Specification (Loc,
5099 Defining_Identifier => Object_Entity,
5100 Parameter_Type => New_Occurrence_Of (Typ, Loc))),
5101 Result_Definition =>
5102 New_Occurrence_Of (Standard_Boolean, Loc));
5104 FDecl := Make_Subprogram_Declaration (Loc, Specification => Spec);
5106 -- Build function body
5109 Make_Defining_Identifier (Loc,
5110 Chars => New_External_Name (Chars (Typ), "Predicate"));
5113 Make_Function_Specification (Loc,
5114 Defining_Unit_Name => SId,
5115 Parameter_Specifications => New_List (
5116 Make_Parameter_Specification (Loc,
5117 Defining_Identifier =>
5118 Make_Defining_Identifier (Loc, Object_Name),
5120 New_Occurrence_Of (Typ, Loc))),
5121 Result_Definition =>
5122 New_Occurrence_Of (Standard_Boolean, Loc));
5125 Make_Subprogram_Body (Loc,
5126 Specification => Spec,
5127 Declarations => Empty_List,
5128 Handled_Statement_Sequence =>
5129 Make_Handled_Sequence_Of_Statements (Loc,
5130 Statements => New_List (
5131 Make_Simple_Return_Statement (Loc,
5132 Expression => Expr))));
5134 -- Insert declaration before freeze node and body after
5136 Insert_Before_And_Analyze (N, FDecl);
5137 Insert_After_And_Analyze (N, FBody);
5139 -- Deal with static predicate case
5141 if Ekind_In (Typ, E_Enumeration_Subtype,
5142 E_Modular_Integer_Subtype,
5143 E_Signed_Integer_Subtype)
5144 and then Is_Static_Subtype (Typ)
5145 and then not Dynamic_Predicate_Present
5147 Build_Static_Predicate (Typ, Expr, Object_Name);
5149 if Present (Static_Predicate_Present)
5150 and No (Static_Predicate (Typ))
5153 ("expression does not have required form for "
5154 & "static predicate",
5155 Next (First (Pragma_Argument_Associations
5156 (Static_Predicate_Present))));
5160 end Build_Predicate_Function;
5162 ----------------------------
5163 -- Build_Static_Predicate --
5164 ----------------------------
5166 procedure Build_Static_Predicate
5171 Loc : constant Source_Ptr := Sloc (Expr);
5173 Non_Static : exception;
5174 -- Raised if something non-static is found
5176 Btyp : constant Entity_Id := Base_Type (Typ);
5178 BLo : constant Uint := Expr_Value (Type_Low_Bound (Btyp));
5179 BHi : constant Uint := Expr_Value (Type_High_Bound (Btyp));
5180 -- Low bound and high bound value of base type of Typ
5182 TLo : constant Uint := Expr_Value (Type_Low_Bound (Typ));
5183 THi : constant Uint := Expr_Value (Type_High_Bound (Typ));
5184 -- Low bound and high bound values of static subtype Typ
5189 -- One entry in a Rlist value, a single REnt (range entry) value
5190 -- denotes one range from Lo to Hi. To represent a single value
5191 -- range Lo = Hi = value.
5193 type RList is array (Nat range <>) of REnt;
5194 -- A list of ranges. The ranges are sorted in increasing order,
5195 -- and are disjoint (there is a gap of at least one value between
5196 -- each range in the table). A value is in the set of ranges in
5197 -- Rlist if it lies within one of these ranges
5199 False_Range : constant RList :=
5200 RList'(1 .. 0 => REnt'(No_Uint, No_Uint));
5201 -- An empty set of ranges represents a range list that can never be
5202 -- satisfied, since there are no ranges in which the value could lie,
5203 -- so it does not lie in any of them. False_Range is a canonical value
5204 -- for this empty set, but general processing should test for an Rlist
5205 -- with length zero (see Is_False predicate), since other null ranges
5206 -- may appear which must be treated as False.
5208 True_Range : constant RList := RList'(1 => REnt'(BLo, BHi));
5209 -- Range representing True, value must be in the base range
5211 function "and" (Left, Right : RList) return RList;
5212 -- And's together two range lists, returning a range list. This is
5213 -- a set intersection operation.
5215 function "or" (Left, Right : RList) return RList;
5216 -- Or's together two range lists, returning a range list. This is a
5217 -- set union operation.
5219 function "not" (Right : RList) return RList;
5220 -- Returns complement of a given range list, i.e. a range list
5221 -- representing all the values in TLo .. THi that are not in the
5222 -- input operand Right.
5224 function Build_Val (V : Uint) return Node_Id;
5225 -- Return an analyzed N_Identifier node referencing this value, suitable
5226 -- for use as an entry in the Static_Predicate list. This node is typed
5227 -- with the base type.
5229 function Build_Range (Lo, Hi : Uint) return Node_Id;
5230 -- Return an analyzed N_Range node referencing this range, suitable
5231 -- for use as an entry in the Static_Predicate list. This node is typed
5232 -- with the base type.
5234 function Get_RList (Exp : Node_Id) return RList;
5235 -- This is a recursive routine that converts the given expression into
5236 -- a list of ranges, suitable for use in building the static predicate.
5238 function Is_False (R : RList) return Boolean;
5239 pragma Inline (Is_False);
5240 -- Returns True if the given range list is empty, and thus represents
5241 -- a False list of ranges that can never be satisfied.
5243 function Is_True (R : RList) return Boolean;
5244 -- Returns True if R trivially represents the True predicate by having
5245 -- a single range from BLo to BHi.
5247 function Is_Type_Ref (N : Node_Id) return Boolean;
5248 pragma Inline (Is_Type_Ref);
5249 -- Returns if True if N is a reference to the type for the predicate in
5250 -- the expression (i.e. if it is an identifier whose Chars field matches
5251 -- the Nam given in the call).
5253 function Lo_Val (N : Node_Id) return Uint;
5254 -- Given static expression or static range from a Static_Predicate list,
5255 -- gets expression value or low bound of range.
5257 function Hi_Val (N : Node_Id) return Uint;
5258 -- Given static expression or static range from a Static_Predicate list,
5259 -- gets expression value of high bound of range.
5261 function Membership_Entry (N : Node_Id) return RList;
5262 -- Given a single membership entry (range, value, or subtype), returns
5263 -- the corresponding range list. Raises Static_Error if not static.
5265 function Membership_Entries (N : Node_Id) return RList;
5266 -- Given an element on an alternatives list of a membership operation,
5267 -- returns the range list corresponding to this entry and all following
5268 -- entries (i.e. returns the "or" of this list of values).
5270 function Stat_Pred (Typ : Entity_Id) return RList;
5271 -- Given a type, if it has a static predicate, then return the predicate
5272 -- as a range list, otherwise raise Non_Static.
5278 function "and" (Left, Right : RList) return RList is
5280 -- First range of result
5282 SLeft : Nat := Left'First;
5283 -- Start of rest of left entries
5285 SRight : Nat := Right'First;
5286 -- Start of rest of right entries
5289 -- If either range is True, return the other
5291 if Is_True (Left) then
5293 elsif Is_True (Right) then
5297 -- If either range is False, return False
5299 if Is_False (Left) or else Is_False (Right) then
5303 -- Loop to remove entries at start that are disjoint, and thus
5304 -- just get discarded from the result entirely.
5307 -- If no operands left in either operand, result is false
5309 if SLeft > Left'Last or else SRight > Right'Last then
5312 -- Discard first left operand entry if disjoint with right
5314 elsif Left (SLeft).Hi < Right (SRight).Lo then
5317 -- Discard first right operand entry if disjoint with left
5319 elsif Right (SRight).Hi < Left (SLeft).Lo then
5320 SRight := SRight + 1;
5322 -- Otherwise we have an overlapping entry
5329 -- Now we have two non-null operands, and first entries overlap.
5330 -- The first entry in the result will be the overlapping part of
5331 -- these two entries.
5333 FEnt := REnt'(Lo => UI_Max (Left (SLeft).Lo, Right (SRight).Lo),
5334 Hi => UI_Min (Left (SLeft).Hi, Right (SRight).Hi));
5336 -- Now we can remove the entry that ended at a lower value, since
5337 -- its contribution is entirely contained in Fent.
5339 if Left (SLeft).Hi <= Right (SRight).Hi then
5342 SRight := SRight + 1;
5345 -- Compute result by concatenating this first entry with the "and"
5346 -- of the remaining parts of the left and right operands. Note that
5347 -- if either of these is empty, "and" will yield empty, so that we
5348 -- will end up with just Fent, which is what we want in that case.
5351 FEnt & (Left (SLeft .. Left'Last) and Right (SRight .. Right'Last));
5358 function "not" (Right : RList) return RList is
5360 -- Return True if False range
5362 if Is_False (Right) then
5366 -- Return False if True range
5368 if Is_True (Right) then
5372 -- Here if not trivial case
5375 Result : RList (1 .. Right'Length + 1);
5376 -- May need one more entry for gap at beginning and end
5379 -- Number of entries stored in Result
5384 if Right (Right'First).Lo > TLo then
5386 Result (Count) := REnt'(TLo, Right (Right'First).Lo - 1);
5389 -- Gaps between ranges
5391 for J in Right'First .. Right'Last - 1 loop
5394 REnt'(Right (J).Hi + 1, Right (J + 1).Lo - 1);
5399 if Right (Right'Last).Hi < THi then
5401 Result (Count) := REnt'(Right (Right'Last).Hi + 1, THi);
5404 return Result (1 .. Count);
5412 function "or" (Left, Right : RList) return RList is
5414 -- First range of result
5416 SLeft : Nat := Left'First;
5417 -- Start of rest of left entries
5419 SRight : Nat := Right'First;
5420 -- Start of rest of right entries
5423 -- If either range is True, return True
5425 if Is_True (Left) or else Is_True (Right) then
5429 -- If either range is False (empty), return the other
5431 if Is_False (Left) then
5433 elsif Is_False (Right) then
5437 -- Initialize result first entry from left or right operand
5438 -- depending on which starts with the lower range.
5440 if Left (SLeft).Lo < Right (SRight).Lo then
5441 FEnt := Left (SLeft);
5444 FEnt := Right (SRight);
5445 SRight := SRight + 1;
5448 -- This loop eats ranges from left and right operands that
5449 -- are contiguous with the first range we are gathering.
5452 -- Eat first entry in left operand if contiguous or
5453 -- overlapped by gathered first operand of result.
5455 if SLeft <= Left'Last
5456 and then Left (SLeft).Lo <= FEnt.Hi + 1
5458 FEnt.Hi := UI_Max (FEnt.Hi, Left (SLeft).Hi);
5461 -- Eat first entry in right operand if contiguous or
5462 -- overlapped by gathered right operand of result.
5464 elsif SRight <= Right'Last
5465 and then Right (SRight).Lo <= FEnt.Hi + 1
5467 FEnt.Hi := UI_Max (FEnt.Hi, Right (SRight).Hi);
5468 SRight := SRight + 1;
5470 -- All done if no more entries to eat!
5477 -- Obtain result as the first entry we just computed, concatenated
5478 -- to the "or" of the remaining results (if one operand is empty,
5479 -- this will just concatenate with the other
5482 FEnt & (Left (SLeft .. Left'Last) or Right (SRight .. Right'Last));
5489 function Build_Range (Lo, Hi : Uint) return Node_Id is
5493 return Build_Val (Hi);
5497 Low_Bound => Build_Val (Lo),
5498 High_Bound => Build_Val (Hi));
5499 Set_Etype (Result, Btyp);
5500 Set_Analyzed (Result);
5509 function Build_Val (V : Uint) return Node_Id is
5513 if Is_Enumeration_Type (Typ) then
5514 Result := Get_Enum_Lit_From_Pos (Typ, V, Loc);
5516 Result := Make_Integer_Literal (Loc, V);
5519 Set_Etype (Result, Btyp);
5520 Set_Is_Static_Expression (Result);
5521 Set_Analyzed (Result);
5529 function Get_RList (Exp : Node_Id) return RList is
5534 -- Static expression can only be true or false
5536 if Is_OK_Static_Expression (Exp) then
5540 if Expr_Value (Exp) = 0 then
5547 -- Otherwise test node type
5555 when N_Op_And | N_And_Then =>
5556 return Get_RList (Left_Opnd (Exp))
5558 Get_RList (Right_Opnd (Exp));
5562 when N_Op_Or | N_Or_Else =>
5563 return Get_RList (Left_Opnd (Exp))
5565 Get_RList (Right_Opnd (Exp));
5570 return not Get_RList (Right_Opnd (Exp));
5572 -- Comparisons of type with static value
5574 when N_Op_Compare =>
5575 -- Type is left operand
5577 if Is_Type_Ref (Left_Opnd (Exp))
5578 and then Is_OK_Static_Expression (Right_Opnd (Exp))
5580 Val := Expr_Value (Right_Opnd (Exp));
5582 -- Typ is right operand
5584 elsif Is_Type_Ref (Right_Opnd (Exp))
5585 and then Is_OK_Static_Expression (Left_Opnd (Exp))
5587 Val := Expr_Value (Left_Opnd (Exp));
5589 -- Invert sense of comparison
5592 when N_Op_Gt => Op := N_Op_Lt;
5593 when N_Op_Lt => Op := N_Op_Gt;
5594 when N_Op_Ge => Op := N_Op_Le;
5595 when N_Op_Le => Op := N_Op_Ge;
5596 when others => null;
5599 -- Other cases are non-static
5605 -- Construct range according to comparison operation
5609 return RList'(1 => REnt'(Val, Val));
5612 return RList'(1 => REnt'(Val, BHi));
5615 return RList'(1 => REnt'(Val + 1, BHi));
5618 return RList'(1 => REnt'(BLo, Val));
5621 return RList'(1 => REnt'(BLo, Val - 1));
5624 return RList'(REnt'(BLo, Val - 1),
5625 REnt'(Val + 1, BHi));
5628 raise Program_Error;
5634 if not Is_Type_Ref (Left_Opnd (Exp)) then
5638 if Present (Right_Opnd (Exp)) then
5639 return Membership_Entry (Right_Opnd (Exp));
5641 return Membership_Entries (First (Alternatives (Exp)));
5644 -- Negative membership (NOT IN)
5647 if not Is_Type_Ref (Left_Opnd (Exp)) then
5651 if Present (Right_Opnd (Exp)) then
5652 return not Membership_Entry (Right_Opnd (Exp));
5654 return not Membership_Entries (First (Alternatives (Exp)));
5657 -- Function call, may be call to static predicate
5659 when N_Function_Call =>
5660 if Is_Entity_Name (Name (Exp)) then
5662 Ent : constant Entity_Id := Entity (Name (Exp));
5664 if Has_Predicates (Ent) then
5665 return Stat_Pred (Etype (First_Formal (Ent)));
5670 -- Other function call cases are non-static
5674 -- Qualified expression, dig out the expression
5676 when N_Qualified_Expression =>
5677 return Get_RList (Expression (Exp));
5682 return (Get_RList (Left_Opnd (Exp))
5683 and not Get_RList (Right_Opnd (Exp)))
5684 or (Get_RList (Right_Opnd (Exp))
5685 and not Get_RList (Left_Opnd (Exp)));
5687 -- Any other node type is non-static
5698 function Hi_Val (N : Node_Id) return Uint is
5700 if Is_Static_Expression (N) then
5701 return Expr_Value (N);
5703 pragma Assert (Nkind (N) = N_Range);
5704 return Expr_Value (High_Bound (N));
5712 function Is_False (R : RList) return Boolean is
5714 return R'Length = 0;
5721 function Is_True (R : RList) return Boolean is
5724 and then R (R'First).Lo = BLo
5725 and then R (R'First).Hi = BHi;
5732 function Is_Type_Ref (N : Node_Id) return Boolean is
5734 return Nkind (N) = N_Identifier and then Chars (N) = Nam;
5741 function Lo_Val (N : Node_Id) return Uint is
5743 if Is_Static_Expression (N) then
5744 return Expr_Value (N);
5746 pragma Assert (Nkind (N) = N_Range);
5747 return Expr_Value (Low_Bound (N));
5751 ------------------------
5752 -- Membership_Entries --
5753 ------------------------
5755 function Membership_Entries (N : Node_Id) return RList is
5757 if No (Next (N)) then
5758 return Membership_Entry (N);
5760 return Membership_Entry (N) or Membership_Entries (Next (N));
5762 end Membership_Entries;
5764 ----------------------
5765 -- Membership_Entry --
5766 ----------------------
5768 function Membership_Entry (N : Node_Id) return RList is
5776 if Nkind (N) = N_Range then
5777 if not Is_Static_Expression (Low_Bound (N))
5779 not Is_Static_Expression (High_Bound (N))
5783 SLo := Expr_Value (Low_Bound (N));
5784 SHi := Expr_Value (High_Bound (N));
5785 return RList'(1 => REnt'(SLo, SHi));
5788 -- Static expression case
5790 elsif Is_Static_Expression (N) then
5791 Val := Expr_Value (N);
5792 return RList'(1 => REnt'(Val, Val));
5794 -- Identifier (other than static expression) case
5796 else pragma Assert (Nkind (N) = N_Identifier);
5800 if Is_Type (Entity (N)) then
5802 -- If type has predicates, process them
5804 if Has_Predicates (Entity (N)) then
5805 return Stat_Pred (Entity (N));
5807 -- For static subtype without predicates, get range
5809 elsif Is_Static_Subtype (Entity (N)) then
5810 SLo := Expr_Value (Type_Low_Bound (Entity (N)));
5811 SHi := Expr_Value (Type_High_Bound (Entity (N)));
5812 return RList'(1 => REnt'(SLo, SHi));
5814 -- Any other type makes us non-static
5820 -- Any other kind of identifier in predicate (e.g. a non-static
5821 -- expression value) means this is not a static predicate.
5827 end Membership_Entry;
5833 function Stat_Pred (Typ : Entity_Id) return RList is
5835 -- Not static if type does not have static predicates
5837 if not Has_Predicates (Typ)
5838 or else No (Static_Predicate (Typ))
5843 -- Otherwise we convert the predicate list to a range list
5846 Result : RList (1 .. List_Length (Static_Predicate (Typ)));
5850 P := First (Static_Predicate (Typ));
5851 for J in Result'Range loop
5852 Result (J) := REnt'(Lo_Val (P), Hi_Val (P));
5860 -- Start of processing for Build_Static_Predicate
5863 -- Now analyze the expression to see if it is a static predicate
5866 Ranges : constant RList := Get_RList (Expr);
5867 -- Range list from expression if it is static
5872 -- Convert range list into a form for the static predicate. In the
5873 -- Ranges array, we just have raw ranges, these must be converted
5874 -- to properly typed and analyzed static expressions or range nodes.
5876 -- Note: here we limit ranges to the ranges of the subtype, so that
5877 -- a predicate is always false for values outside the subtype. That
5878 -- seems fine, such values are invalid anyway, and considering them
5879 -- to fail the predicate seems allowed and friendly, and furthermore
5880 -- simplifies processing for case statements and loops.
5884 for J in Ranges'Range loop
5886 Lo : Uint := Ranges (J).Lo;
5887 Hi : Uint := Ranges (J).Hi;
5890 -- Ignore completely out of range entry
5892 if Hi < TLo or else Lo > THi then
5895 -- Otherwise process entry
5898 -- Adjust out of range value to subtype range
5908 -- Convert range into required form
5911 Append_To (Plist, Build_Val (Lo));
5913 Append_To (Plist, Build_Range (Lo, Hi));
5919 -- Processing was successful and all entries were static, so now we
5920 -- can store the result as the predicate list.
5922 Set_Static_Predicate (Typ, Plist);
5924 -- The processing for static predicates put the expression into
5925 -- canonical form as a series of ranges. It also eliminated
5926 -- duplicates and collapsed and combined ranges. We might as well
5927 -- replace the alternatives list of the right operand of the
5928 -- membership test with the static predicate list, which will
5929 -- usually be more efficient.
5932 New_Alts : constant List_Id := New_List;
5937 Old_Node := First (Plist);
5938 while Present (Old_Node) loop
5939 New_Node := New_Copy (Old_Node);
5941 if Nkind (New_Node) = N_Range then
5942 Set_Low_Bound (New_Node, New_Copy (Low_Bound (Old_Node)));
5943 Set_High_Bound (New_Node, New_Copy (High_Bound (Old_Node)));
5946 Append_To (New_Alts, New_Node);
5950 -- If empty list, replace by False
5952 if Is_Empty_List (New_Alts) then
5953 Rewrite (Expr, New_Occurrence_Of (Standard_False, Loc));
5955 -- Else replace by set membership test
5960 Left_Opnd => Make_Identifier (Loc, Nam),
5961 Right_Opnd => Empty,
5962 Alternatives => New_Alts));
5964 -- Resolve new expression in function context
5966 Install_Formals (Predicate_Function (Typ));
5967 Push_Scope (Predicate_Function (Typ));
5968 Analyze_And_Resolve (Expr, Standard_Boolean);
5974 -- If non-static, return doing nothing
5979 end Build_Static_Predicate;
5981 -----------------------------------------
5982 -- Check_Aspect_At_End_Of_Declarations --
5983 -----------------------------------------
5985 procedure Check_Aspect_At_End_Of_Declarations (ASN : Node_Id) is
5986 Ent : constant Entity_Id := Entity (ASN);
5987 Ident : constant Node_Id := Identifier (ASN);
5989 Freeze_Expr : constant Node_Id := Expression (ASN);
5990 -- Expression from call to Check_Aspect_At_Freeze_Point
5992 End_Decl_Expr : constant Node_Id := Entity (Ident);
5993 -- Expression to be analyzed at end of declarations
5995 T : constant Entity_Id := Etype (Freeze_Expr);
5996 -- Type required for preanalyze call
5998 A_Id : constant Aspect_Id := Get_Aspect_Id (Chars (Ident));
6001 -- Set False if error
6003 -- On entry to this procedure, Entity (Ident) contains a copy of the
6004 -- original expression from the aspect, saved for this purpose, and
6005 -- but Expression (Ident) is a preanalyzed copy of the expression,
6006 -- preanalyzed just after the freeze point.
6009 -- Case of stream attributes, just have to compare entities
6011 if A_Id = Aspect_Input or else
6012 A_Id = Aspect_Output or else
6013 A_Id = Aspect_Read or else
6016 Analyze (End_Decl_Expr);
6017 Err := Entity (End_Decl_Expr) /= Entity (Freeze_Expr);
6019 elsif A_Id = Aspect_Variable_Indexing or else
6020 A_Id = Aspect_Constant_Indexing or else
6021 A_Id = Aspect_Default_Iterator or else
6022 A_Id = Aspect_Iterator_Element
6024 -- Make type unfrozen before analysis, to prevent spurious errors
6025 -- about late attributes.
6027 Set_Is_Frozen (Ent, False);
6028 Analyze (End_Decl_Expr);
6029 Analyze (Aspect_Rep_Item (ASN));
6030 Set_Is_Frozen (Ent, True);
6032 -- If the end of declarations comes before any other freeze
6033 -- point, the Freeze_Expr is not analyzed: no check needed.
6036 Analyzed (Freeze_Expr)
6037 and then not In_Instance
6038 and then Entity (End_Decl_Expr) /= Entity (Freeze_Expr);
6043 -- In a generic context the aspect expressions have not been
6044 -- preanalyzed, so do it now. There are no conformance checks
6045 -- to perform in this case.
6048 Check_Aspect_At_Freeze_Point (ASN);
6051 -- The default values attributes may be defined in the private part,
6052 -- and the analysis of the expression may take place when only the
6053 -- partial view is visible. The expression must be scalar, so use
6054 -- the full view to resolve.
6056 elsif (A_Id = Aspect_Default_Value
6058 A_Id = Aspect_Default_Component_Value)
6059 and then Is_Private_Type (T)
6061 Preanalyze_Spec_Expression (End_Decl_Expr, Full_View (T));
6063 Preanalyze_Spec_Expression (End_Decl_Expr, T);
6066 Err := not Fully_Conformant_Expressions (End_Decl_Expr, Freeze_Expr);
6069 -- Output error message if error
6073 ("visibility of aspect for& changes after freeze point",
6076 ("?info: & is frozen here, aspects evaluated at this point",
6077 Freeze_Node (Ent), Ent);
6079 end Check_Aspect_At_End_Of_Declarations;
6081 ----------------------------------
6082 -- Check_Aspect_At_Freeze_Point --
6083 ----------------------------------
6085 procedure Check_Aspect_At_Freeze_Point (ASN : Node_Id) is
6086 Ident : constant Node_Id := Identifier (ASN);
6087 -- Identifier (use Entity field to save expression)
6090 -- Type required for preanalyze call
6092 A_Id : constant Aspect_Id := Get_Aspect_Id (Chars (Ident));
6095 -- On entry to this procedure, Entity (Ident) contains a copy of the
6096 -- original expression from the aspect, saved for this purpose.
6098 -- On exit from this procedure Entity (Ident) is unchanged, still
6099 -- containing that copy, but Expression (Ident) is a preanalyzed copy
6100 -- of the expression, preanalyzed just after the freeze point.
6102 -- Make a copy of the expression to be preanalyed
6104 Set_Expression (ASN, New_Copy_Tree (Entity (Ident)));
6106 -- Find type for preanalyze call
6110 -- No_Aspect should be impossible
6113 raise Program_Error;
6115 -- Library unit aspects should be impossible (never delayed)
6117 when Library_Unit_Aspects =>
6118 raise Program_Error;
6120 -- Aspects taking an optional boolean argument. Should be impossible
6121 -- since these are never delayed.
6123 when Boolean_Aspects =>
6124 raise Program_Error;
6126 -- Test_Case aspect applies to entries and subprograms, hence should
6127 -- never be delayed.
6129 when Aspect_Test_Case =>
6130 raise Program_Error;
6132 when Aspect_Attach_Handler =>
6133 T := RTE (RE_Interrupt_ID);
6135 -- Default_Value is resolved with the type entity in question
6137 when Aspect_Default_Value =>
6140 -- Default_Component_Value is resolved with the component type
6142 when Aspect_Default_Component_Value =>
6143 T := Component_Type (Entity (ASN));
6145 -- Aspects corresponding to attribute definition clauses
6147 when Aspect_Address =>
6148 T := RTE (RE_Address);
6150 when Aspect_Bit_Order =>
6151 T := RTE (RE_Bit_Order);
6154 T := RTE (RE_CPU_Range);
6156 when Aspect_Dispatching_Domain =>
6157 T := RTE (RE_Dispatching_Domain);
6159 when Aspect_External_Tag =>
6160 T := Standard_String;
6162 when Aspect_Priority | Aspect_Interrupt_Priority =>
6163 T := Standard_Integer;
6165 when Aspect_Small =>
6166 T := Universal_Real;
6168 -- For a simple storage pool, we have to retrieve the type of the
6169 -- pool object associated with the aspect's corresponding attribute
6170 -- definition clause.
6172 when Aspect_Simple_Storage_Pool =>
6173 T := Etype (Expression (Aspect_Rep_Item (ASN)));
6175 when Aspect_Storage_Pool =>
6176 T := Class_Wide_Type (RTE (RE_Root_Storage_Pool));
6178 when Aspect_Alignment |
6179 Aspect_Component_Size |
6180 Aspect_Machine_Radix |
6181 Aspect_Object_Size |
6183 Aspect_Storage_Size |
6184 Aspect_Stream_Size |
6185 Aspect_Value_Size =>
6188 -- Stream attribute. Special case, the expression is just an entity
6189 -- that does not need any resolution, so just analyze.
6195 Analyze (Expression (ASN));
6198 -- Same for Iterator aspects, where the expression is a function
6199 -- name. Legality rules are checked separately.
6201 when Aspect_Constant_Indexing |
6202 Aspect_Default_Iterator |
6203 Aspect_Iterator_Element |
6204 Aspect_Implicit_Dereference |
6205 Aspect_Variable_Indexing =>
6206 Analyze (Expression (ASN));
6209 -- Suppress/Unsuppress/Synchronization/Warnings should not be delayed
6211 when Aspect_Suppress |
6213 Aspect_Synchronization |
6215 raise Program_Error;
6217 -- Pre/Post/Invariant/Predicate take boolean expressions
6219 when Aspect_Dynamic_Predicate |
6222 Aspect_Precondition |
6224 Aspect_Postcondition |
6226 Aspect_Static_Predicate |
6227 Aspect_Type_Invariant =>
6228 T := Standard_Boolean;
6230 when Aspect_Dimension |
6231 Aspect_Dimension_System =>
6232 raise Program_Error;
6236 -- Do the preanalyze call
6238 Preanalyze_Spec_Expression (Expression (ASN), T);
6239 end Check_Aspect_At_Freeze_Point;
6241 -----------------------------------
6242 -- Check_Constant_Address_Clause --
6243 -----------------------------------
6245 procedure Check_Constant_Address_Clause
6249 procedure Check_At_Constant_Address (Nod : Node_Id);
6250 -- Checks that the given node N represents a name whose 'Address is
6251 -- constant (in the same sense as OK_Constant_Address_Clause, i.e. the
6252 -- address value is the same at the point of declaration of U_Ent and at
6253 -- the time of elaboration of the address clause.
6255 procedure Check_Expr_Constants (Nod : Node_Id);
6256 -- Checks that Nod meets the requirements for a constant address clause
6257 -- in the sense of the enclosing procedure.
6259 procedure Check_List_Constants (Lst : List_Id);
6260 -- Check that all elements of list Lst meet the requirements for a
6261 -- constant address clause in the sense of the enclosing procedure.
6263 -------------------------------
6264 -- Check_At_Constant_Address --
6265 -------------------------------
6267 procedure Check_At_Constant_Address (Nod : Node_Id) is
6269 if Is_Entity_Name (Nod) then
6270 if Present (Address_Clause (Entity ((Nod)))) then
6272 ("invalid address clause for initialized object &!",
6275 ("address for& cannot" &
6276 " depend on another address clause! (RM 13.1(22))!",
6279 elsif In_Same_Source_Unit (Entity (Nod), U_Ent)
6280 and then Sloc (U_Ent) < Sloc (Entity (Nod))
6283 ("invalid address clause for initialized object &!",
6285 Error_Msg_Node_2 := U_Ent;
6287 ("\& must be defined before & (RM 13.1(22))!",
6291 elsif Nkind (Nod) = N_Selected_Component then
6293 T : constant Entity_Id := Etype (Prefix (Nod));
6296 if (Is_Record_Type (T)
6297 and then Has_Discriminants (T))
6300 and then Is_Record_Type (Designated_Type (T))
6301 and then Has_Discriminants (Designated_Type (T)))
6304 ("invalid address clause for initialized object &!",
6307 ("\address cannot depend on component" &
6308 " of discriminated record (RM 13.1(22))!",
6311 Check_At_Constant_Address (Prefix (Nod));
6315 elsif Nkind (Nod) = N_Indexed_Component then
6316 Check_At_Constant_Address (Prefix (Nod));
6317 Check_List_Constants (Expressions (Nod));
6320 Check_Expr_Constants (Nod);
6322 end Check_At_Constant_Address;
6324 --------------------------
6325 -- Check_Expr_Constants --
6326 --------------------------
6328 procedure Check_Expr_Constants (Nod : Node_Id) is
6329 Loc_U_Ent : constant Source_Ptr := Sloc (U_Ent);
6330 Ent : Entity_Id := Empty;
6333 if Nkind (Nod) in N_Has_Etype
6334 and then Etype (Nod) = Any_Type
6340 when N_Empty | N_Error =>
6343 when N_Identifier | N_Expanded_Name =>
6344 Ent := Entity (Nod);
6346 -- We need to look at the original node if it is different
6347 -- from the node, since we may have rewritten things and
6348 -- substituted an identifier representing the rewrite.
6350 if Original_Node (Nod) /= Nod then
6351 Check_Expr_Constants (Original_Node (Nod));
6353 -- If the node is an object declaration without initial
6354 -- value, some code has been expanded, and the expression
6355 -- is not constant, even if the constituents might be
6356 -- acceptable, as in A'Address + offset.
6358 if Ekind (Ent) = E_Variable
6360 Nkind (Declaration_Node (Ent)) = N_Object_Declaration
6362 No (Expression (Declaration_Node (Ent)))
6365 ("invalid address clause for initialized object &!",
6368 -- If entity is constant, it may be the result of expanding
6369 -- a check. We must verify that its declaration appears
6370 -- before the object in question, else we also reject the
6373 elsif Ekind (Ent) = E_Constant
6374 and then In_Same_Source_Unit (Ent, U_Ent)
6375 and then Sloc (Ent) > Loc_U_Ent
6378 ("invalid address clause for initialized object &!",
6385 -- Otherwise look at the identifier and see if it is OK
6387 if Ekind_In (Ent, E_Named_Integer, E_Named_Real)
6388 or else Is_Type (Ent)
6393 Ekind (Ent) = E_Constant
6395 Ekind (Ent) = E_In_Parameter
6397 -- This is the case where we must have Ent defined before
6398 -- U_Ent. Clearly if they are in different units this
6399 -- requirement is met since the unit containing Ent is
6400 -- already processed.
6402 if not In_Same_Source_Unit (Ent, U_Ent) then
6405 -- Otherwise location of Ent must be before the location
6406 -- of U_Ent, that's what prior defined means.
6408 elsif Sloc (Ent) < Loc_U_Ent then
6413 ("invalid address clause for initialized object &!",
6415 Error_Msg_Node_2 := U_Ent;
6417 ("\& must be defined before & (RM 13.1(22))!",
6421 elsif Nkind (Original_Node (Nod)) = N_Function_Call then
6422 Check_Expr_Constants (Original_Node (Nod));
6426 ("invalid address clause for initialized object &!",
6429 if Comes_From_Source (Ent) then
6431 ("\reference to variable& not allowed"
6432 & " (RM 13.1(22))!", Nod, Ent);
6435 ("non-static expression not allowed"
6436 & " (RM 13.1(22))!", Nod);
6440 when N_Integer_Literal =>
6442 -- If this is a rewritten unchecked conversion, in a system
6443 -- where Address is an integer type, always use the base type
6444 -- for a literal value. This is user-friendly and prevents
6445 -- order-of-elaboration issues with instances of unchecked
6448 if Nkind (Original_Node (Nod)) = N_Function_Call then
6449 Set_Etype (Nod, Base_Type (Etype (Nod)));
6452 when N_Real_Literal |
6454 N_Character_Literal =>
6458 Check_Expr_Constants (Low_Bound (Nod));
6459 Check_Expr_Constants (High_Bound (Nod));
6461 when N_Explicit_Dereference =>
6462 Check_Expr_Constants (Prefix (Nod));
6464 when N_Indexed_Component =>
6465 Check_Expr_Constants (Prefix (Nod));
6466 Check_List_Constants (Expressions (Nod));
6469 Check_Expr_Constants (Prefix (Nod));
6470 Check_Expr_Constants (Discrete_Range (Nod));
6472 when N_Selected_Component =>
6473 Check_Expr_Constants (Prefix (Nod));
6475 when N_Attribute_Reference =>
6476 if Attribute_Name (Nod) = Name_Address
6478 Attribute_Name (Nod) = Name_Access
6480 Attribute_Name (Nod) = Name_Unchecked_Access
6482 Attribute_Name (Nod) = Name_Unrestricted_Access
6484 Check_At_Constant_Address (Prefix (Nod));
6487 Check_Expr_Constants (Prefix (Nod));
6488 Check_List_Constants (Expressions (Nod));
6492 Check_List_Constants (Component_Associations (Nod));
6493 Check_List_Constants (Expressions (Nod));
6495 when N_Component_Association =>
6496 Check_Expr_Constants (Expression (Nod));
6498 when N_Extension_Aggregate =>
6499 Check_Expr_Constants (Ancestor_Part (Nod));
6500 Check_List_Constants (Component_Associations (Nod));
6501 Check_List_Constants (Expressions (Nod));
6506 when N_Binary_Op | N_Short_Circuit | N_Membership_Test =>
6507 Check_Expr_Constants (Left_Opnd (Nod));
6508 Check_Expr_Constants (Right_Opnd (Nod));
6511 Check_Expr_Constants (Right_Opnd (Nod));
6513 when N_Type_Conversion |
6514 N_Qualified_Expression |
6516 Check_Expr_Constants (Expression (Nod));
6518 when N_Unchecked_Type_Conversion =>
6519 Check_Expr_Constants (Expression (Nod));
6521 -- If this is a rewritten unchecked conversion, subtypes in
6522 -- this node are those created within the instance. To avoid
6523 -- order of elaboration issues, replace them with their base
6524 -- types. Note that address clauses can cause order of
6525 -- elaboration problems because they are elaborated by the
6526 -- back-end at the point of definition, and may mention
6527 -- entities declared in between (as long as everything is
6528 -- static). It is user-friendly to allow unchecked conversions
6531 if Nkind (Original_Node (Nod)) = N_Function_Call then
6532 Set_Etype (Expression (Nod),
6533 Base_Type (Etype (Expression (Nod))));
6534 Set_Etype (Nod, Base_Type (Etype (Nod)));
6537 when N_Function_Call =>
6538 if not Is_Pure (Entity (Name (Nod))) then
6540 ("invalid address clause for initialized object &!",
6544 ("\function & is not pure (RM 13.1(22))!",
6545 Nod, Entity (Name (Nod)));
6548 Check_List_Constants (Parameter_Associations (Nod));
6551 when N_Parameter_Association =>
6552 Check_Expr_Constants (Explicit_Actual_Parameter (Nod));
6556 ("invalid address clause for initialized object &!",
6559 ("\must be constant defined before& (RM 13.1(22))!",
6562 end Check_Expr_Constants;
6564 --------------------------
6565 -- Check_List_Constants --
6566 --------------------------
6568 procedure Check_List_Constants (Lst : List_Id) is
6572 if Present (Lst) then
6573 Nod1 := First (Lst);
6574 while Present (Nod1) loop
6575 Check_Expr_Constants (Nod1);
6579 end Check_List_Constants;
6581 -- Start of processing for Check_Constant_Address_Clause
6584 -- If rep_clauses are to be ignored, no need for legality checks. In
6585 -- particular, no need to pester user about rep clauses that violate
6586 -- the rule on constant addresses, given that these clauses will be
6587 -- removed by Freeze before they reach the back end.
6589 if not Ignore_Rep_Clauses then
6590 Check_Expr_Constants (Expr);
6592 end Check_Constant_Address_Clause;
6594 ----------------------------------------
6595 -- Check_Record_Representation_Clause --
6596 ----------------------------------------
6598 procedure Check_Record_Representation_Clause (N : Node_Id) is
6599 Loc : constant Source_Ptr := Sloc (N);
6600 Ident : constant Node_Id := Identifier (N);
6601 Rectype : Entity_Id;
6606 Hbit : Uint := Uint_0;
6610 Max_Bit_So_Far : Uint;
6611 -- Records the maximum bit position so far. If all field positions
6612 -- are monotonically increasing, then we can skip the circuit for
6613 -- checking for overlap, since no overlap is possible.
6615 Tagged_Parent : Entity_Id := Empty;
6616 -- This is set in the case of a derived tagged type for which we have
6617 -- Is_Fully_Repped_Tagged_Type True (indicating that all components are
6618 -- positioned by record representation clauses). In this case we must
6619 -- check for overlap between components of this tagged type, and the
6620 -- components of its parent. Tagged_Parent will point to this parent
6621 -- type. For all other cases Tagged_Parent is left set to Empty.
6623 Parent_Last_Bit : Uint;
6624 -- Relevant only if Tagged_Parent is set, Parent_Last_Bit indicates the
6625 -- last bit position for any field in the parent type. We only need to
6626 -- check overlap for fields starting below this point.
6628 Overlap_Check_Required : Boolean;
6629 -- Used to keep track of whether or not an overlap check is required
6631 Overlap_Detected : Boolean := False;
6632 -- Set True if an overlap is detected
6634 Ccount : Natural := 0;
6635 -- Number of component clauses in record rep clause
6637 procedure Check_Component_Overlap (C1_Ent, C2_Ent : Entity_Id);
6638 -- Given two entities for record components or discriminants, checks
6639 -- if they have overlapping component clauses and issues errors if so.
6641 procedure Find_Component;
6642 -- Finds component entity corresponding to current component clause (in
6643 -- CC), and sets Comp to the entity, and Fbit/Lbit to the zero origin
6644 -- start/stop bits for the field. If there is no matching component or
6645 -- if the matching component does not have a component clause, then
6646 -- that's an error and Comp is set to Empty, but no error message is
6647 -- issued, since the message was already given. Comp is also set to
6648 -- Empty if the current "component clause" is in fact a pragma.
6650 -----------------------------
6651 -- Check_Component_Overlap --
6652 -----------------------------
6654 procedure Check_Component_Overlap (C1_Ent, C2_Ent : Entity_Id) is
6655 CC1 : constant Node_Id := Component_Clause (C1_Ent);
6656 CC2 : constant Node_Id := Component_Clause (C2_Ent);
6659 if Present (CC1) and then Present (CC2) then
6661 -- Exclude odd case where we have two tag fields in the same
6662 -- record, both at location zero. This seems a bit strange, but
6663 -- it seems to happen in some circumstances, perhaps on an error.
6665 if Chars (C1_Ent) = Name_uTag
6667 Chars (C2_Ent) = Name_uTag
6672 -- Here we check if the two fields overlap
6675 S1 : constant Uint := Component_Bit_Offset (C1_Ent);
6676 S2 : constant Uint := Component_Bit_Offset (C2_Ent);
6677 E1 : constant Uint := S1 + Esize (C1_Ent);
6678 E2 : constant Uint := S2 + Esize (C2_Ent);
6681 if E2 <= S1 or else E1 <= S2 then
6684 Error_Msg_Node_2 := Component_Name (CC2);
6685 Error_Msg_Sloc := Sloc (Error_Msg_Node_2);
6686 Error_Msg_Node_1 := Component_Name (CC1);
6688 ("component& overlaps & #", Component_Name (CC1));
6689 Overlap_Detected := True;
6693 end Check_Component_Overlap;
6695 --------------------
6696 -- Find_Component --
6697 --------------------
6699 procedure Find_Component is
6701 procedure Search_Component (R : Entity_Id);
6702 -- Search components of R for a match. If found, Comp is set.
6704 ----------------------
6705 -- Search_Component --
6706 ----------------------
6708 procedure Search_Component (R : Entity_Id) is
6710 Comp := First_Component_Or_Discriminant (R);
6711 while Present (Comp) loop
6713 -- Ignore error of attribute name for component name (we
6714 -- already gave an error message for this, so no need to
6717 if Nkind (Component_Name (CC)) = N_Attribute_Reference then
6720 exit when Chars (Comp) = Chars (Component_Name (CC));
6723 Next_Component_Or_Discriminant (Comp);
6725 end Search_Component;
6727 -- Start of processing for Find_Component
6730 -- Return with Comp set to Empty if we have a pragma
6732 if Nkind (CC) = N_Pragma then
6737 -- Search current record for matching component
6739 Search_Component (Rectype);
6741 -- If not found, maybe component of base type that is absent from
6742 -- statically constrained first subtype.
6745 Search_Component (Base_Type (Rectype));
6748 -- If no component, or the component does not reference the component
6749 -- clause in question, then there was some previous error for which
6750 -- we already gave a message, so just return with Comp Empty.
6753 or else Component_Clause (Comp) /= CC
6757 -- Normal case where we have a component clause
6760 Fbit := Component_Bit_Offset (Comp);
6761 Lbit := Fbit + Esize (Comp) - 1;
6765 -- Start of processing for Check_Record_Representation_Clause
6769 Rectype := Entity (Ident);
6771 if Rectype = Any_Type then
6774 Rectype := Underlying_Type (Rectype);
6777 -- See if we have a fully repped derived tagged type
6780 PS : constant Entity_Id := Parent_Subtype (Rectype);
6783 if Present (PS) and then Is_Fully_Repped_Tagged_Type (PS) then
6784 Tagged_Parent := PS;
6786 -- Find maximum bit of any component of the parent type
6788 Parent_Last_Bit := UI_From_Int (System_Address_Size - 1);
6789 Pcomp := First_Entity (Tagged_Parent);
6790 while Present (Pcomp) loop
6791 if Ekind_In (Pcomp, E_Discriminant, E_Component) then
6792 if Component_Bit_Offset (Pcomp) /= No_Uint
6793 and then Known_Static_Esize (Pcomp)
6798 Component_Bit_Offset (Pcomp) + Esize (Pcomp) - 1);
6801 Next_Entity (Pcomp);
6807 -- All done if no component clauses
6809 CC := First (Component_Clauses (N));
6815 -- If a tag is present, then create a component clause that places it
6816 -- at the start of the record (otherwise gigi may place it after other
6817 -- fields that have rep clauses).
6819 Fent := First_Entity (Rectype);
6821 if Nkind (Fent) = N_Defining_Identifier
6822 and then Chars (Fent) = Name_uTag
6824 Set_Component_Bit_Offset (Fent, Uint_0);
6825 Set_Normalized_Position (Fent, Uint_0);
6826 Set_Normalized_First_Bit (Fent, Uint_0);
6827 Set_Normalized_Position_Max (Fent, Uint_0);
6828 Init_Esize (Fent, System_Address_Size);
6830 Set_Component_Clause (Fent,
6831 Make_Component_Clause (Loc,
6832 Component_Name => Make_Identifier (Loc, Name_uTag),
6834 Position => Make_Integer_Literal (Loc, Uint_0),
6835 First_Bit => Make_Integer_Literal (Loc, Uint_0),
6837 Make_Integer_Literal (Loc,
6838 UI_From_Int (System_Address_Size))));
6840 Ccount := Ccount + 1;
6843 Max_Bit_So_Far := Uint_Minus_1;
6844 Overlap_Check_Required := False;
6846 -- Process the component clauses
6848 while Present (CC) loop
6851 if Present (Comp) then
6852 Ccount := Ccount + 1;
6854 -- We need a full overlap check if record positions non-monotonic
6856 if Fbit <= Max_Bit_So_Far then
6857 Overlap_Check_Required := True;
6860 Max_Bit_So_Far := Lbit;
6862 -- Check bit position out of range of specified size
6864 if Has_Size_Clause (Rectype)
6865 and then RM_Size (Rectype) <= Lbit
6868 ("bit number out of range of specified size",
6871 -- Check for overlap with tag field
6874 if Is_Tagged_Type (Rectype)
6875 and then Fbit < System_Address_Size
6878 ("component overlaps tag field of&",
6879 Component_Name (CC), Rectype);
6880 Overlap_Detected := True;
6888 -- Check parent overlap if component might overlap parent field
6890 if Present (Tagged_Parent)
6891 and then Fbit <= Parent_Last_Bit
6893 Pcomp := First_Component_Or_Discriminant (Tagged_Parent);
6894 while Present (Pcomp) loop
6895 if not Is_Tag (Pcomp)
6896 and then Chars (Pcomp) /= Name_uParent
6898 Check_Component_Overlap (Comp, Pcomp);
6901 Next_Component_Or_Discriminant (Pcomp);
6909 -- Now that we have processed all the component clauses, check for
6910 -- overlap. We have to leave this till last, since the components can
6911 -- appear in any arbitrary order in the representation clause.
6913 -- We do not need this check if all specified ranges were monotonic,
6914 -- as recorded by Overlap_Check_Required being False at this stage.
6916 -- This first section checks if there are any overlapping entries at
6917 -- all. It does this by sorting all entries and then seeing if there are
6918 -- any overlaps. If there are none, then that is decisive, but if there
6919 -- are overlaps, they may still be OK (they may result from fields in
6920 -- different variants).
6922 if Overlap_Check_Required then
6923 Overlap_Check1 : declare
6925 OC_Fbit : array (0 .. Ccount) of Uint;
6926 -- First-bit values for component clauses, the value is the offset
6927 -- of the first bit of the field from start of record. The zero
6928 -- entry is for use in sorting.
6930 OC_Lbit : array (0 .. Ccount) of Uint;
6931 -- Last-bit values for component clauses, the value is the offset
6932 -- of the last bit of the field from start of record. The zero
6933 -- entry is for use in sorting.
6935 OC_Count : Natural := 0;
6936 -- Count of entries in OC_Fbit and OC_Lbit
6938 function OC_Lt (Op1, Op2 : Natural) return Boolean;
6939 -- Compare routine for Sort
6941 procedure OC_Move (From : Natural; To : Natural);
6942 -- Move routine for Sort
6944 package Sorting is new GNAT.Heap_Sort_G (OC_Move, OC_Lt);
6950 function OC_Lt (Op1, Op2 : Natural) return Boolean is
6952 return OC_Fbit (Op1) < OC_Fbit (Op2);
6959 procedure OC_Move (From : Natural; To : Natural) is
6961 OC_Fbit (To) := OC_Fbit (From);
6962 OC_Lbit (To) := OC_Lbit (From);
6965 -- Start of processing for Overlap_Check
6968 CC := First (Component_Clauses (N));
6969 while Present (CC) loop
6971 -- Exclude component clause already marked in error
6973 if not Error_Posted (CC) then
6976 if Present (Comp) then
6977 OC_Count := OC_Count + 1;
6978 OC_Fbit (OC_Count) := Fbit;
6979 OC_Lbit (OC_Count) := Lbit;
6986 Sorting.Sort (OC_Count);
6988 Overlap_Check_Required := False;
6989 for J in 1 .. OC_Count - 1 loop
6990 if OC_Lbit (J) >= OC_Fbit (J + 1) then
6991 Overlap_Check_Required := True;
6998 -- If Overlap_Check_Required is still True, then we have to do the full
6999 -- scale overlap check, since we have at least two fields that do
7000 -- overlap, and we need to know if that is OK since they are in
7001 -- different variant, or whether we have a definite problem.
7003 if Overlap_Check_Required then
7004 Overlap_Check2 : declare
7005 C1_Ent, C2_Ent : Entity_Id;
7006 -- Entities of components being checked for overlap
7009 -- Component_List node whose Component_Items are being checked
7012 -- Component declaration for component being checked
7015 C1_Ent := First_Entity (Base_Type (Rectype));
7017 -- Loop through all components in record. For each component check
7018 -- for overlap with any of the preceding elements on the component
7019 -- list containing the component and also, if the component is in
7020 -- a variant, check against components outside the case structure.
7021 -- This latter test is repeated recursively up the variant tree.
7023 Main_Component_Loop : while Present (C1_Ent) loop
7024 if not Ekind_In (C1_Ent, E_Component, E_Discriminant) then
7025 goto Continue_Main_Component_Loop;
7028 -- Skip overlap check if entity has no declaration node. This
7029 -- happens with discriminants in constrained derived types.
7030 -- Possibly we are missing some checks as a result, but that
7031 -- does not seem terribly serious.
7033 if No (Declaration_Node (C1_Ent)) then
7034 goto Continue_Main_Component_Loop;
7037 Clist := Parent (List_Containing (Declaration_Node (C1_Ent)));
7039 -- Loop through component lists that need checking. Check the
7040 -- current component list and all lists in variants above us.
7042 Component_List_Loop : loop
7044 -- If derived type definition, go to full declaration
7045 -- If at outer level, check discriminants if there are any.
7047 if Nkind (Clist) = N_Derived_Type_Definition then
7048 Clist := Parent (Clist);
7051 -- Outer level of record definition, check discriminants
7053 if Nkind_In (Clist, N_Full_Type_Declaration,
7054 N_Private_Type_Declaration)
7056 if Has_Discriminants (Defining_Identifier (Clist)) then
7058 First_Discriminant (Defining_Identifier (Clist));
7059 while Present (C2_Ent) loop
7060 exit when C1_Ent = C2_Ent;
7061 Check_Component_Overlap (C1_Ent, C2_Ent);
7062 Next_Discriminant (C2_Ent);
7066 -- Record extension case
7068 elsif Nkind (Clist) = N_Derived_Type_Definition then
7071 -- Otherwise check one component list
7074 Citem := First (Component_Items (Clist));
7075 while Present (Citem) loop
7076 if Nkind (Citem) = N_Component_Declaration then
7077 C2_Ent := Defining_Identifier (Citem);
7078 exit when C1_Ent = C2_Ent;
7079 Check_Component_Overlap (C1_Ent, C2_Ent);
7086 -- Check for variants above us (the parent of the Clist can
7087 -- be a variant, in which case its parent is a variant part,
7088 -- and the parent of the variant part is a component list
7089 -- whose components must all be checked against the current
7090 -- component for overlap).
7092 if Nkind (Parent (Clist)) = N_Variant then
7093 Clist := Parent (Parent (Parent (Clist)));
7095 -- Check for possible discriminant part in record, this
7096 -- is treated essentially as another level in the
7097 -- recursion. For this case the parent of the component
7098 -- list is the record definition, and its parent is the
7099 -- full type declaration containing the discriminant
7102 elsif Nkind (Parent (Clist)) = N_Record_Definition then
7103 Clist := Parent (Parent ((Clist)));
7105 -- If neither of these two cases, we are at the top of
7109 exit Component_List_Loop;
7111 end loop Component_List_Loop;
7113 <<Continue_Main_Component_Loop>>
7114 Next_Entity (C1_Ent);
7116 end loop Main_Component_Loop;
7120 -- The following circuit deals with warning on record holes (gaps). We
7121 -- skip this check if overlap was detected, since it makes sense for the
7122 -- programmer to fix this illegality before worrying about warnings.
7124 if not Overlap_Detected and Warn_On_Record_Holes then
7125 Record_Hole_Check : declare
7126 Decl : constant Node_Id := Declaration_Node (Base_Type (Rectype));
7127 -- Full declaration of record type
7129 procedure Check_Component_List
7133 -- Check component list CL for holes. The starting bit should be
7134 -- Sbit. which is zero for the main record component list and set
7135 -- appropriately for recursive calls for variants. DS is set to
7136 -- a list of discriminant specifications to be included in the
7137 -- consideration of components. It is No_List if none to consider.
7139 --------------------------
7140 -- Check_Component_List --
7141 --------------------------
7143 procedure Check_Component_List
7151 Compl := Integer (List_Length (Component_Items (CL)));
7153 if DS /= No_List then
7154 Compl := Compl + Integer (List_Length (DS));
7158 Comps : array (Natural range 0 .. Compl) of Entity_Id;
7159 -- Gather components (zero entry is for sort routine)
7161 Ncomps : Natural := 0;
7162 -- Number of entries stored in Comps (starting at Comps (1))
7165 -- One component item or discriminant specification
7168 -- Starting bit for next component
7176 function Lt (Op1, Op2 : Natural) return Boolean;
7177 -- Compare routine for Sort
7179 procedure Move (From : Natural; To : Natural);
7180 -- Move routine for Sort
7182 package Sorting is new GNAT.Heap_Sort_G (Move, Lt);
7188 function Lt (Op1, Op2 : Natural) return Boolean is
7190 return Component_Bit_Offset (Comps (Op1))
7192 Component_Bit_Offset (Comps (Op2));
7199 procedure Move (From : Natural; To : Natural) is
7201 Comps (To) := Comps (From);
7205 -- Gather discriminants into Comp
7207 if DS /= No_List then
7208 Citem := First (DS);
7209 while Present (Citem) loop
7210 if Nkind (Citem) = N_Discriminant_Specification then
7212 Ent : constant Entity_Id :=
7213 Defining_Identifier (Citem);
7215 if Ekind (Ent) = E_Discriminant then
7216 Ncomps := Ncomps + 1;
7217 Comps (Ncomps) := Ent;
7226 -- Gather component entities into Comp
7228 Citem := First (Component_Items (CL));
7229 while Present (Citem) loop
7230 if Nkind (Citem) = N_Component_Declaration then
7231 Ncomps := Ncomps + 1;
7232 Comps (Ncomps) := Defining_Identifier (Citem);
7238 -- Now sort the component entities based on the first bit.
7239 -- Note we already know there are no overlapping components.
7241 Sorting.Sort (Ncomps);
7243 -- Loop through entries checking for holes
7246 for J in 1 .. Ncomps loop
7248 Error_Msg_Uint_1 := Component_Bit_Offset (CEnt) - Nbit;
7250 if Error_Msg_Uint_1 > 0 then
7252 ("?^-bit gap before component&",
7253 Component_Name (Component_Clause (CEnt)), CEnt);
7256 Nbit := Component_Bit_Offset (CEnt) + Esize (CEnt);
7259 -- Process variant parts recursively if present
7261 if Present (Variant_Part (CL)) then
7262 Variant := First (Variants (Variant_Part (CL)));
7263 while Present (Variant) loop
7264 Check_Component_List
7265 (Component_List (Variant), Nbit, No_List);
7270 end Check_Component_List;
7272 -- Start of processing for Record_Hole_Check
7279 if Is_Tagged_Type (Rectype) then
7280 Sbit := UI_From_Int (System_Address_Size);
7285 if Nkind (Decl) = N_Full_Type_Declaration
7286 and then Nkind (Type_Definition (Decl)) = N_Record_Definition
7288 Check_Component_List
7289 (Component_List (Type_Definition (Decl)),
7291 Discriminant_Specifications (Decl));
7294 end Record_Hole_Check;
7297 -- For records that have component clauses for all components, and whose
7298 -- size is less than or equal to 32, we need to know the size in the
7299 -- front end to activate possible packed array processing where the
7300 -- component type is a record.
7302 -- At this stage Hbit + 1 represents the first unused bit from all the
7303 -- component clauses processed, so if the component clauses are
7304 -- complete, then this is the length of the record.
7306 -- For records longer than System.Storage_Unit, and for those where not
7307 -- all components have component clauses, the back end determines the
7308 -- length (it may for example be appropriate to round up the size
7309 -- to some convenient boundary, based on alignment considerations, etc).
7311 if Unknown_RM_Size (Rectype) and then Hbit + 1 <= 32 then
7313 -- Nothing to do if at least one component has no component clause
7315 Comp := First_Component_Or_Discriminant (Rectype);
7316 while Present (Comp) loop
7317 exit when No (Component_Clause (Comp));
7318 Next_Component_Or_Discriminant (Comp);
7321 -- If we fall out of loop, all components have component clauses
7322 -- and so we can set the size to the maximum value.
7325 Set_RM_Size (Rectype, Hbit + 1);
7328 end Check_Record_Representation_Clause;
7334 procedure Check_Size
7338 Biased : out Boolean)
7340 UT : constant Entity_Id := Underlying_Type (T);
7346 -- Dismiss cases for generic types or types with previous errors
7349 or else UT = Any_Type
7350 or else Is_Generic_Type (UT)
7351 or else Is_Generic_Type (Root_Type (UT))
7355 -- Check case of bit packed array
7357 elsif Is_Array_Type (UT)
7358 and then Known_Static_Component_Size (UT)
7359 and then Is_Bit_Packed_Array (UT)
7367 Asiz := Component_Size (UT);
7368 Indx := First_Index (UT);
7370 Ityp := Etype (Indx);
7372 -- If non-static bound, then we are not in the business of
7373 -- trying to check the length, and indeed an error will be
7374 -- issued elsewhere, since sizes of non-static array types
7375 -- cannot be set implicitly or explicitly.
7377 if not Is_Static_Subtype (Ityp) then
7381 -- Otherwise accumulate next dimension
7383 Asiz := Asiz * (Expr_Value (Type_High_Bound (Ityp)) -
7384 Expr_Value (Type_Low_Bound (Ityp)) +
7388 exit when No (Indx);
7394 Error_Msg_Uint_1 := Asiz;
7396 ("size for& too small, minimum allowed is ^", N, T);
7397 Set_Esize (T, Asiz);
7398 Set_RM_Size (T, Asiz);
7402 -- All other composite types are ignored
7404 elsif Is_Composite_Type (UT) then
7407 -- For fixed-point types, don't check minimum if type is not frozen,
7408 -- since we don't know all the characteristics of the type that can
7409 -- affect the size (e.g. a specified small) till freeze time.
7411 elsif Is_Fixed_Point_Type (UT)
7412 and then not Is_Frozen (UT)
7416 -- Cases for which a minimum check is required
7419 -- Ignore if specified size is correct for the type
7421 if Known_Esize (UT) and then Siz = Esize (UT) then
7425 -- Otherwise get minimum size
7427 M := UI_From_Int (Minimum_Size (UT));
7431 -- Size is less than minimum size, but one possibility remains
7432 -- that we can manage with the new size if we bias the type.
7434 M := UI_From_Int (Minimum_Size (UT, Biased => True));
7437 Error_Msg_Uint_1 := M;
7439 ("size for& too small, minimum allowed is ^", N, T);
7449 -------------------------
7450 -- Get_Alignment_Value --
7451 -------------------------
7453 function Get_Alignment_Value (Expr : Node_Id) return Uint is
7454 Align : constant Uint := Static_Integer (Expr);
7457 if Align = No_Uint then
7460 elsif Align <= 0 then
7461 Error_Msg_N ("alignment value must be positive", Expr);
7465 for J in Int range 0 .. 64 loop
7467 M : constant Uint := Uint_2 ** J;
7470 exit when M = Align;
7474 ("alignment value must be power of 2", Expr);
7482 end Get_Alignment_Value;
7488 procedure Initialize is
7490 Address_Clause_Checks.Init;
7491 Independence_Checks.Init;
7492 Unchecked_Conversions.Init;
7495 -------------------------
7496 -- Is_Operational_Item --
7497 -------------------------
7499 function Is_Operational_Item (N : Node_Id) return Boolean is
7501 if Nkind (N) /= N_Attribute_Definition_Clause then
7505 Id : constant Attribute_Id := Get_Attribute_Id (Chars (N));
7507 return Id = Attribute_Input
7508 or else Id = Attribute_Output
7509 or else Id = Attribute_Read
7510 or else Id = Attribute_Write
7511 or else Id = Attribute_External_Tag;
7514 end Is_Operational_Item;
7520 function Minimum_Size
7522 Biased : Boolean := False) return Nat
7524 Lo : Uint := No_Uint;
7525 Hi : Uint := No_Uint;
7526 LoR : Ureal := No_Ureal;
7527 HiR : Ureal := No_Ureal;
7528 LoSet : Boolean := False;
7529 HiSet : Boolean := False;
7533 R_Typ : constant Entity_Id := Root_Type (T);
7536 -- If bad type, return 0
7538 if T = Any_Type then
7541 -- For generic types, just return zero. There cannot be any legitimate
7542 -- need to know such a size, but this routine may be called with a
7543 -- generic type as part of normal processing.
7545 elsif Is_Generic_Type (R_Typ)
7546 or else R_Typ = Any_Type
7550 -- Access types. Normally an access type cannot have a size smaller
7551 -- than the size of System.Address. The exception is on VMS, where
7552 -- we have short and long addresses, and it is possible for an access
7553 -- type to have a short address size (and thus be less than the size
7554 -- of System.Address itself). We simply skip the check for VMS, and
7555 -- leave it to the back end to do the check.
7557 elsif Is_Access_Type (T) then
7558 if OpenVMS_On_Target then
7561 return System_Address_Size;
7564 -- Floating-point types
7566 elsif Is_Floating_Point_Type (T) then
7567 return UI_To_Int (Esize (R_Typ));
7571 elsif Is_Discrete_Type (T) then
7573 -- The following loop is looking for the nearest compile time known
7574 -- bounds following the ancestor subtype chain. The idea is to find
7575 -- the most restrictive known bounds information.
7579 if Ancest = Any_Type or else Etype (Ancest) = Any_Type then
7584 if Compile_Time_Known_Value (Type_Low_Bound (Ancest)) then
7585 Lo := Expr_Rep_Value (Type_Low_Bound (Ancest));
7592 if Compile_Time_Known_Value (Type_High_Bound (Ancest)) then
7593 Hi := Expr_Rep_Value (Type_High_Bound (Ancest));
7599 Ancest := Ancestor_Subtype (Ancest);
7602 Ancest := Base_Type (T);
7604 if Is_Generic_Type (Ancest) then
7610 -- Fixed-point types. We can't simply use Expr_Value to get the
7611 -- Corresponding_Integer_Value values of the bounds, since these do not
7612 -- get set till the type is frozen, and this routine can be called
7613 -- before the type is frozen. Similarly the test for bounds being static
7614 -- needs to include the case where we have unanalyzed real literals for
7617 elsif Is_Fixed_Point_Type (T) then
7619 -- The following loop is looking for the nearest compile time known
7620 -- bounds following the ancestor subtype chain. The idea is to find
7621 -- the most restrictive known bounds information.
7625 if Ancest = Any_Type or else Etype (Ancest) = Any_Type then
7629 -- Note: In the following two tests for LoSet and HiSet, it may
7630 -- seem redundant to test for N_Real_Literal here since normally
7631 -- one would assume that the test for the value being known at
7632 -- compile time includes this case. However, there is a glitch.
7633 -- If the real literal comes from folding a non-static expression,
7634 -- then we don't consider any non- static expression to be known
7635 -- at compile time if we are in configurable run time mode (needed
7636 -- in some cases to give a clearer definition of what is and what
7637 -- is not accepted). So the test is indeed needed. Without it, we
7638 -- would set neither Lo_Set nor Hi_Set and get an infinite loop.
7641 if Nkind (Type_Low_Bound (Ancest)) = N_Real_Literal
7642 or else Compile_Time_Known_Value (Type_Low_Bound (Ancest))
7644 LoR := Expr_Value_R (Type_Low_Bound (Ancest));
7651 if Nkind (Type_High_Bound (Ancest)) = N_Real_Literal
7652 or else Compile_Time_Known_Value (Type_High_Bound (Ancest))
7654 HiR := Expr_Value_R (Type_High_Bound (Ancest));
7660 Ancest := Ancestor_Subtype (Ancest);
7663 Ancest := Base_Type (T);
7665 if Is_Generic_Type (Ancest) then
7671 Lo := UR_To_Uint (LoR / Small_Value (T));
7672 Hi := UR_To_Uint (HiR / Small_Value (T));
7674 -- No other types allowed
7677 raise Program_Error;
7680 -- Fall through with Hi and Lo set. Deal with biased case
7683 and then not Is_Fixed_Point_Type (T)
7684 and then not (Is_Enumeration_Type (T)
7685 and then Has_Non_Standard_Rep (T)))
7686 or else Has_Biased_Representation (T)
7692 -- Signed case. Note that we consider types like range 1 .. -1 to be
7693 -- signed for the purpose of computing the size, since the bounds have
7694 -- to be accommodated in the base type.
7696 if Lo < 0 or else Hi < 0 then
7700 -- S = size, B = 2 ** (size - 1) (can accommodate -B .. +(B - 1))
7701 -- Note that we accommodate the case where the bounds cross. This
7702 -- can happen either because of the way the bounds are declared
7703 -- or because of the algorithm in Freeze_Fixed_Point_Type.
7717 -- If both bounds are positive, make sure that both are represen-
7718 -- table in the case where the bounds are crossed. This can happen
7719 -- either because of the way the bounds are declared, or because of
7720 -- the algorithm in Freeze_Fixed_Point_Type.
7726 -- S = size, (can accommodate 0 .. (2**size - 1))
7729 while Hi >= Uint_2 ** S loop
7737 ---------------------------
7738 -- New_Stream_Subprogram --
7739 ---------------------------
7741 procedure New_Stream_Subprogram
7745 Nam : TSS_Name_Type)
7747 Loc : constant Source_Ptr := Sloc (N);
7748 Sname : constant Name_Id := Make_TSS_Name (Base_Type (Ent), Nam);
7749 Subp_Id : Entity_Id;
7750 Subp_Decl : Node_Id;
7754 Defer_Declaration : constant Boolean :=
7755 Is_Tagged_Type (Ent) or else Is_Private_Type (Ent);
7756 -- For a tagged type, there is a declaration for each stream attribute
7757 -- at the freeze point, and we must generate only a completion of this
7758 -- declaration. We do the same for private types, because the full view
7759 -- might be tagged. Otherwise we generate a declaration at the point of
7760 -- the attribute definition clause.
7762 function Build_Spec return Node_Id;
7763 -- Used for declaration and renaming declaration, so that this is
7764 -- treated as a renaming_as_body.
7770 function Build_Spec return Node_Id is
7771 Out_P : constant Boolean := (Nam = TSS_Stream_Read);
7774 T_Ref : constant Node_Id := New_Reference_To (Etyp, Loc);
7777 Subp_Id := Make_Defining_Identifier (Loc, Sname);
7779 -- S : access Root_Stream_Type'Class
7781 Formals := New_List (
7782 Make_Parameter_Specification (Loc,
7783 Defining_Identifier =>
7784 Make_Defining_Identifier (Loc, Name_S),
7786 Make_Access_Definition (Loc,
7789 Designated_Type (Etype (F)), Loc))));
7791 if Nam = TSS_Stream_Input then
7792 Spec := Make_Function_Specification (Loc,
7793 Defining_Unit_Name => Subp_Id,
7794 Parameter_Specifications => Formals,
7795 Result_Definition => T_Ref);
7800 Make_Parameter_Specification (Loc,
7801 Defining_Identifier => Make_Defining_Identifier (Loc, Name_V),
7802 Out_Present => Out_P,
7803 Parameter_Type => T_Ref));
7806 Make_Procedure_Specification (Loc,
7807 Defining_Unit_Name => Subp_Id,
7808 Parameter_Specifications => Formals);
7814 -- Start of processing for New_Stream_Subprogram
7817 F := First_Formal (Subp);
7819 if Ekind (Subp) = E_Procedure then
7820 Etyp := Etype (Next_Formal (F));
7822 Etyp := Etype (Subp);
7825 -- Prepare subprogram declaration and insert it as an action on the
7826 -- clause node. The visibility for this entity is used to test for
7827 -- visibility of the attribute definition clause (in the sense of
7828 -- 8.3(23) as amended by AI-195).
7830 if not Defer_Declaration then
7832 Make_Subprogram_Declaration (Loc,
7833 Specification => Build_Spec);
7835 -- For a tagged type, there is always a visible declaration for each
7836 -- stream TSS (it is a predefined primitive operation), and the
7837 -- completion of this declaration occurs at the freeze point, which is
7838 -- not always visible at places where the attribute definition clause is
7839 -- visible. So, we create a dummy entity here for the purpose of
7840 -- tracking the visibility of the attribute definition clause itself.
7844 Make_Defining_Identifier (Loc, New_External_Name (Sname, 'V'));
7846 Make_Object_Declaration (Loc,
7847 Defining_Identifier => Subp_Id,
7848 Object_Definition => New_Occurrence_Of (Standard_Boolean, Loc));
7851 Insert_Action (N, Subp_Decl);
7852 Set_Entity (N, Subp_Id);
7855 Make_Subprogram_Renaming_Declaration (Loc,
7856 Specification => Build_Spec,
7857 Name => New_Reference_To (Subp, Loc));
7859 if Defer_Declaration then
7860 Set_TSS (Base_Type (Ent), Subp_Id);
7862 Insert_Action (N, Subp_Decl);
7863 Copy_TSS (Subp_Id, Base_Type (Ent));
7865 end New_Stream_Subprogram;
7867 ------------------------
7868 -- Rep_Item_Too_Early --
7869 ------------------------
7871 function Rep_Item_Too_Early (T : Entity_Id; N : Node_Id) return Boolean is
7873 -- Cannot apply non-operational rep items to generic types
7875 if Is_Operational_Item (N) then
7879 and then Is_Generic_Type (Root_Type (T))
7881 Error_Msg_N ("representation item not allowed for generic type", N);
7885 -- Otherwise check for incomplete type
7887 if Is_Incomplete_Or_Private_Type (T)
7888 and then No (Underlying_Type (T))
7890 (Nkind (N) /= N_Pragma
7891 or else Get_Pragma_Id (N) /= Pragma_Import)
7894 ("representation item must be after full type declaration", N);
7897 -- If the type has incomplete components, a representation clause is
7898 -- illegal but stream attributes and Convention pragmas are correct.
7900 elsif Has_Private_Component (T) then
7901 if Nkind (N) = N_Pragma then
7905 ("representation item must appear after type is fully defined",
7912 end Rep_Item_Too_Early;
7914 -----------------------
7915 -- Rep_Item_Too_Late --
7916 -----------------------
7918 function Rep_Item_Too_Late
7921 FOnly : Boolean := False) return Boolean
7924 Parent_Type : Entity_Id;
7927 -- Output the too late message. Note that this is not considered a
7928 -- serious error, since the effect is simply that we ignore the
7929 -- representation clause in this case.
7935 procedure Too_Late is
7937 Error_Msg_N ("|representation item appears too late!", N);
7940 -- Start of processing for Rep_Item_Too_Late
7943 -- First make sure entity is not frozen (RM 13.1(9))
7947 -- Exclude imported types, which may be frozen if they appear in a
7948 -- representation clause for a local type.
7950 and then not From_With_Type (T)
7952 -- Exclude generated entitiesa (not coming from source). The common
7953 -- case is when we generate a renaming which prematurely freezes the
7954 -- renamed internal entity, but we still want to be able to set copies
7955 -- of attribute values such as Size/Alignment.
7957 and then Comes_From_Source (T)
7960 S := First_Subtype (T);
7962 if Present (Freeze_Node (S)) then
7964 ("?no more representation items for }", Freeze_Node (S), S);
7969 -- Check for case of non-tagged derived type whose parent either has
7970 -- primitive operations, or is a by reference type (RM 13.1(10)).
7974 and then Is_Derived_Type (T)
7975 and then not Is_Tagged_Type (T)
7977 Parent_Type := Etype (Base_Type (T));
7979 if Has_Primitive_Operations (Parent_Type) then
7982 ("primitive operations already defined for&!", N, Parent_Type);
7985 elsif Is_By_Reference_Type (Parent_Type) then
7988 ("parent type & is a by reference type!", N, Parent_Type);
7993 -- No error, link item into head of chain of rep items for the entity,
7994 -- but avoid chaining if we have an overloadable entity, and the pragma
7995 -- is one that can apply to multiple overloaded entities.
7997 if Is_Overloadable (T)
7998 and then Nkind (N) = N_Pragma
8001 Pname : constant Name_Id := Pragma_Name (N);
8003 if Pname = Name_Convention or else
8004 Pname = Name_Import or else
8005 Pname = Name_Export or else
8006 Pname = Name_External or else
8007 Pname = Name_Interface
8014 Record_Rep_Item (T, N);
8016 end Rep_Item_Too_Late;
8018 -------------------------------------
8019 -- Replace_Type_References_Generic --
8020 -------------------------------------
8022 procedure Replace_Type_References_Generic (N : Node_Id; TName : Name_Id) is
8024 function Replace_Node (N : Node_Id) return Traverse_Result;
8025 -- Processes a single node in the traversal procedure below, checking
8026 -- if node N should be replaced, and if so, doing the replacement.
8028 procedure Replace_Type_Refs is new Traverse_Proc (Replace_Node);
8029 -- This instantiation provides the body of Replace_Type_References
8035 function Replace_Node (N : Node_Id) return Traverse_Result is
8040 -- Case of identifier
8042 if Nkind (N) = N_Identifier then
8044 -- If not the type name, all done with this node
8046 if Chars (N) /= TName then
8049 -- Otherwise do the replacement and we are done with this node
8052 Replace_Type_Reference (N);
8056 -- Case of selected component (which is what a qualification
8057 -- looks like in the unanalyzed tree, which is what we have.
8059 elsif Nkind (N) = N_Selected_Component then
8061 -- If selector name is not our type, keeping going (we might
8062 -- still have an occurrence of the type in the prefix).
8064 if Nkind (Selector_Name (N)) /= N_Identifier
8065 or else Chars (Selector_Name (N)) /= TName
8069 -- Selector name is our type, check qualification
8072 -- Loop through scopes and prefixes, doing comparison
8077 -- Continue if no more scopes or scope with no name
8079 if No (S) or else Nkind (S) not in N_Has_Chars then
8083 -- Do replace if prefix is an identifier matching the
8084 -- scope that we are currently looking at.
8086 if Nkind (P) = N_Identifier
8087 and then Chars (P) = Chars (S)
8089 Replace_Type_Reference (N);
8093 -- Go check scope above us if prefix is itself of the
8094 -- form of a selected component, whose selector matches
8095 -- the scope we are currently looking at.
8097 if Nkind (P) = N_Selected_Component
8098 and then Nkind (Selector_Name (P)) = N_Identifier
8099 and then Chars (Selector_Name (P)) = Chars (S)
8104 -- For anything else, we don't have a match, so keep on
8105 -- going, there are still some weird cases where we may
8106 -- still have a replacement within the prefix.
8114 -- Continue for any other node kind
8122 Replace_Type_Refs (N);
8123 end Replace_Type_References_Generic;
8125 -------------------------
8126 -- Same_Representation --
8127 -------------------------
8129 function Same_Representation (Typ1, Typ2 : Entity_Id) return Boolean is
8130 T1 : constant Entity_Id := Underlying_Type (Typ1);
8131 T2 : constant Entity_Id := Underlying_Type (Typ2);
8134 -- A quick check, if base types are the same, then we definitely have
8135 -- the same representation, because the subtype specific representation
8136 -- attributes (Size and Alignment) do not affect representation from
8137 -- the point of view of this test.
8139 if Base_Type (T1) = Base_Type (T2) then
8142 elsif Is_Private_Type (Base_Type (T2))
8143 and then Base_Type (T1) = Full_View (Base_Type (T2))
8148 -- Tagged types never have differing representations
8150 if Is_Tagged_Type (T1) then
8154 -- Representations are definitely different if conventions differ
8156 if Convention (T1) /= Convention (T2) then
8160 -- Representations are different if component alignments differ
8162 if (Is_Record_Type (T1) or else Is_Array_Type (T1))
8164 (Is_Record_Type (T2) or else Is_Array_Type (T2))
8165 and then Component_Alignment (T1) /= Component_Alignment (T2)
8170 -- For arrays, the only real issue is component size. If we know the
8171 -- component size for both arrays, and it is the same, then that's
8172 -- good enough to know we don't have a change of representation.
8174 if Is_Array_Type (T1) then
8175 if Known_Component_Size (T1)
8176 and then Known_Component_Size (T2)
8177 and then Component_Size (T1) = Component_Size (T2)
8179 if VM_Target = No_VM then
8182 -- In VM targets the representation of arrays with aliased
8183 -- components differs from arrays with non-aliased components
8186 return Has_Aliased_Components (Base_Type (T1))
8188 Has_Aliased_Components (Base_Type (T2));
8193 -- Types definitely have same representation if neither has non-standard
8194 -- representation since default representations are always consistent.
8195 -- If only one has non-standard representation, and the other does not,
8196 -- then we consider that they do not have the same representation. They
8197 -- might, but there is no way of telling early enough.
8199 if Has_Non_Standard_Rep (T1) then
8200 if not Has_Non_Standard_Rep (T2) then
8204 return not Has_Non_Standard_Rep (T2);
8207 -- Here the two types both have non-standard representation, and we need
8208 -- to determine if they have the same non-standard representation.
8210 -- For arrays, we simply need to test if the component sizes are the
8211 -- same. Pragma Pack is reflected in modified component sizes, so this
8212 -- check also deals with pragma Pack.
8214 if Is_Array_Type (T1) then
8215 return Component_Size (T1) = Component_Size (T2);
8217 -- Tagged types always have the same representation, because it is not
8218 -- possible to specify different representations for common fields.
8220 elsif Is_Tagged_Type (T1) then
8223 -- Case of record types
8225 elsif Is_Record_Type (T1) then
8227 -- Packed status must conform
8229 if Is_Packed (T1) /= Is_Packed (T2) then
8232 -- Otherwise we must check components. Typ2 maybe a constrained
8233 -- subtype with fewer components, so we compare the components
8234 -- of the base types.
8237 Record_Case : declare
8238 CD1, CD2 : Entity_Id;
8240 function Same_Rep return Boolean;
8241 -- CD1 and CD2 are either components or discriminants. This
8242 -- function tests whether the two have the same representation
8248 function Same_Rep return Boolean is
8250 if No (Component_Clause (CD1)) then
8251 return No (Component_Clause (CD2));
8255 Present (Component_Clause (CD2))
8257 Component_Bit_Offset (CD1) = Component_Bit_Offset (CD2)
8259 Esize (CD1) = Esize (CD2);
8263 -- Start of processing for Record_Case
8266 if Has_Discriminants (T1) then
8267 CD1 := First_Discriminant (T1);
8268 CD2 := First_Discriminant (T2);
8270 -- The number of discriminants may be different if the
8271 -- derived type has fewer (constrained by values). The
8272 -- invisible discriminants retain the representation of
8273 -- the original, so the discrepancy does not per se
8274 -- indicate a different representation.
8277 and then Present (CD2)
8279 if not Same_Rep then
8282 Next_Discriminant (CD1);
8283 Next_Discriminant (CD2);
8288 CD1 := First_Component (Underlying_Type (Base_Type (T1)));
8289 CD2 := First_Component (Underlying_Type (Base_Type (T2)));
8291 while Present (CD1) loop
8292 if not Same_Rep then
8295 Next_Component (CD1);
8296 Next_Component (CD2);
8304 -- For enumeration types, we must check each literal to see if the
8305 -- representation is the same. Note that we do not permit enumeration
8306 -- representation clauses for Character and Wide_Character, so these
8307 -- cases were already dealt with.
8309 elsif Is_Enumeration_Type (T1) then
8310 Enumeration_Case : declare
8314 L1 := First_Literal (T1);
8315 L2 := First_Literal (T2);
8317 while Present (L1) loop
8318 if Enumeration_Rep (L1) /= Enumeration_Rep (L2) then
8328 end Enumeration_Case;
8330 -- Any other types have the same representation for these purposes
8335 end Same_Representation;
8341 procedure Set_Biased
8345 Biased : Boolean := True)
8349 Set_Has_Biased_Representation (E);
8351 if Warn_On_Biased_Representation then
8353 ("?" & Msg & " forces biased representation for&", N, E);
8358 --------------------
8359 -- Set_Enum_Esize --
8360 --------------------
8362 procedure Set_Enum_Esize (T : Entity_Id) is
8370 -- Find the minimum standard size (8,16,32,64) that fits
8372 Lo := Enumeration_Rep (Entity (Type_Low_Bound (T)));
8373 Hi := Enumeration_Rep (Entity (Type_High_Bound (T)));
8376 if Lo >= -Uint_2**07 and then Hi < Uint_2**07 then
8377 Sz := Standard_Character_Size; -- May be > 8 on some targets
8379 elsif Lo >= -Uint_2**15 and then Hi < Uint_2**15 then
8382 elsif Lo >= -Uint_2**31 and then Hi < Uint_2**31 then
8385 else pragma Assert (Lo >= -Uint_2**63 and then Hi < Uint_2**63);
8390 if Hi < Uint_2**08 then
8391 Sz := Standard_Character_Size; -- May be > 8 on some targets
8393 elsif Hi < Uint_2**16 then
8396 elsif Hi < Uint_2**32 then
8399 else pragma Assert (Hi < Uint_2**63);
8404 -- That minimum is the proper size unless we have a foreign convention
8405 -- and the size required is 32 or less, in which case we bump the size
8406 -- up to 32. This is required for C and C++ and seems reasonable for
8407 -- all other foreign conventions.
8409 if Has_Foreign_Convention (T)
8410 and then Esize (T) < Standard_Integer_Size
8412 Init_Esize (T, Standard_Integer_Size);
8418 ------------------------------
8419 -- Validate_Address_Clauses --
8420 ------------------------------
8422 procedure Validate_Address_Clauses is
8424 for J in Address_Clause_Checks.First .. Address_Clause_Checks.Last loop
8426 ACCR : Address_Clause_Check_Record
8427 renames Address_Clause_Checks.Table (J);
8438 -- Skip processing of this entry if warning already posted
8440 if not Address_Warning_Posted (ACCR.N) then
8442 Expr := Original_Node (Expression (ACCR.N));
8446 X_Alignment := Alignment (ACCR.X);
8447 Y_Alignment := Alignment (ACCR.Y);
8449 -- Similarly obtain sizes
8451 X_Size := Esize (ACCR.X);
8452 Y_Size := Esize (ACCR.Y);
8454 -- Check for large object overlaying smaller one
8457 and then X_Size > Uint_0
8458 and then X_Size > Y_Size
8461 ("?& overlays smaller object", ACCR.N, ACCR.X);
8463 ("\?program execution may be erroneous", ACCR.N);
8464 Error_Msg_Uint_1 := X_Size;
8466 ("\?size of & is ^", ACCR.N, ACCR.X);
8467 Error_Msg_Uint_1 := Y_Size;
8469 ("\?size of & is ^", ACCR.N, ACCR.Y);
8471 -- Check for inadequate alignment, both of the base object
8472 -- and of the offset, if any.
8474 -- Note: we do not check the alignment if we gave a size
8475 -- warning, since it would likely be redundant.
8477 elsif Y_Alignment /= Uint_0
8478 and then (Y_Alignment < X_Alignment
8481 Nkind (Expr) = N_Attribute_Reference
8483 Attribute_Name (Expr) = Name_Address
8485 Has_Compatible_Alignment
8486 (ACCR.X, Prefix (Expr))
8487 /= Known_Compatible))
8490 ("?specified address for& may be inconsistent "
8494 ("\?program execution may be erroneous (RM 13.3(27))",
8496 Error_Msg_Uint_1 := X_Alignment;
8498 ("\?alignment of & is ^",
8500 Error_Msg_Uint_1 := Y_Alignment;
8502 ("\?alignment of & is ^",
8504 if Y_Alignment >= X_Alignment then
8506 ("\?but offset is not multiple of alignment",
8513 end Validate_Address_Clauses;
8515 ---------------------------
8516 -- Validate_Independence --
8517 ---------------------------
8519 procedure Validate_Independence is
8520 SU : constant Uint := UI_From_Int (System_Storage_Unit);
8528 procedure Check_Array_Type (Atyp : Entity_Id);
8529 -- Checks if the array type Atyp has independent components, and
8530 -- if not, outputs an appropriate set of error messages.
8532 procedure No_Independence;
8533 -- Output message that independence cannot be guaranteed
8535 function OK_Component (C : Entity_Id) return Boolean;
8536 -- Checks one component to see if it is independently accessible, and
8537 -- if so yields True, otherwise yields False if independent access
8538 -- cannot be guaranteed. This is a conservative routine, it only
8539 -- returns True if it knows for sure, it returns False if it knows
8540 -- there is a problem, or it cannot be sure there is no problem.
8542 procedure Reason_Bad_Component (C : Entity_Id);
8543 -- Outputs continuation message if a reason can be determined for
8544 -- the component C being bad.
8546 ----------------------
8547 -- Check_Array_Type --
8548 ----------------------
8550 procedure Check_Array_Type (Atyp : Entity_Id) is
8551 Ctyp : constant Entity_Id := Component_Type (Atyp);
8554 -- OK if no alignment clause, no pack, and no component size
8556 if not Has_Component_Size_Clause (Atyp)
8557 and then not Has_Alignment_Clause (Atyp)
8558 and then not Is_Packed (Atyp)
8563 -- Check actual component size
8565 if not Known_Component_Size (Atyp)
8566 or else not (Addressable (Component_Size (Atyp))
8567 and then Component_Size (Atyp) < 64)
8568 or else Component_Size (Atyp) mod Esize (Ctyp) /= 0
8572 -- Bad component size, check reason
8574 if Has_Component_Size_Clause (Atyp) then
8576 Get_Attribute_Definition_Clause
8577 (Atyp, Attribute_Component_Size);
8580 Error_Msg_Sloc := Sloc (P);
8581 Error_Msg_N ("\because of Component_Size clause#", N);
8586 if Is_Packed (Atyp) then
8587 P := Get_Rep_Pragma (Atyp, Name_Pack);
8590 Error_Msg_Sloc := Sloc (P);
8591 Error_Msg_N ("\because of pragma Pack#", N);
8596 -- No reason found, just return
8601 -- Array type is OK independence-wise
8604 end Check_Array_Type;
8606 ---------------------
8607 -- No_Independence --
8608 ---------------------
8610 procedure No_Independence is
8612 if Pragma_Name (N) = Name_Independent then
8614 ("independence cannot be guaranteed for&", N, E);
8617 ("independent components cannot be guaranteed for&", N, E);
8619 end No_Independence;
8625 function OK_Component (C : Entity_Id) return Boolean is
8626 Rec : constant Entity_Id := Scope (C);
8627 Ctyp : constant Entity_Id := Etype (C);
8630 -- OK if no component clause, no Pack, and no alignment clause
8632 if No (Component_Clause (C))
8633 and then not Is_Packed (Rec)
8634 and then not Has_Alignment_Clause (Rec)
8639 -- Here we look at the actual component layout. A component is
8640 -- addressable if its size is a multiple of the Esize of the
8641 -- component type, and its starting position in the record has
8642 -- appropriate alignment, and the record itself has appropriate
8643 -- alignment to guarantee the component alignment.
8645 -- Make sure sizes are static, always assume the worst for any
8646 -- cases where we cannot check static values.
8648 if not (Known_Static_Esize (C)
8649 and then Known_Static_Esize (Ctyp))
8654 -- Size of component must be addressable or greater than 64 bits
8655 -- and a multiple of bytes.
8657 if not Addressable (Esize (C))
8658 and then Esize (C) < Uint_64
8663 -- Check size is proper multiple
8665 if Esize (C) mod Esize (Ctyp) /= 0 then
8669 -- Check alignment of component is OK
8671 if not Known_Component_Bit_Offset (C)
8672 or else Component_Bit_Offset (C) < Uint_0
8673 or else Component_Bit_Offset (C) mod Esize (Ctyp) /= 0
8678 -- Check alignment of record type is OK
8680 if not Known_Alignment (Rec)
8681 or else (Alignment (Rec) * SU) mod Esize (Ctyp) /= 0
8686 -- All tests passed, component is addressable
8691 --------------------------
8692 -- Reason_Bad_Component --
8693 --------------------------
8695 procedure Reason_Bad_Component (C : Entity_Id) is
8696 Rec : constant Entity_Id := Scope (C);
8697 Ctyp : constant Entity_Id := Etype (C);
8700 -- If component clause present assume that's the problem
8702 if Present (Component_Clause (C)) then
8703 Error_Msg_Sloc := Sloc (Component_Clause (C));
8704 Error_Msg_N ("\because of Component_Clause#", N);
8708 -- If pragma Pack clause present, assume that's the problem
8710 if Is_Packed (Rec) then
8711 P := Get_Rep_Pragma (Rec, Name_Pack);
8714 Error_Msg_Sloc := Sloc (P);
8715 Error_Msg_N ("\because of pragma Pack#", N);
8720 -- See if record has bad alignment clause
8722 if Has_Alignment_Clause (Rec)
8723 and then Known_Alignment (Rec)
8724 and then (Alignment (Rec) * SU) mod Esize (Ctyp) /= 0
8726 P := Get_Attribute_Definition_Clause (Rec, Attribute_Alignment);
8729 Error_Msg_Sloc := Sloc (P);
8730 Error_Msg_N ("\because of Alignment clause#", N);
8734 -- Couldn't find a reason, so return without a message
8737 end Reason_Bad_Component;
8739 -- Start of processing for Validate_Independence
8742 for J in Independence_Checks.First .. Independence_Checks.Last loop
8743 N := Independence_Checks.Table (J).N;
8744 E := Independence_Checks.Table (J).E;
8745 IC := Pragma_Name (N) = Name_Independent_Components;
8747 -- Deal with component case
8749 if Ekind (E) = E_Discriminant or else Ekind (E) = E_Component then
8750 if not OK_Component (E) then
8752 Reason_Bad_Component (E);
8757 -- Deal with record with Independent_Components
8759 if IC and then Is_Record_Type (E) then
8760 Comp := First_Component_Or_Discriminant (E);
8761 while Present (Comp) loop
8762 if not OK_Component (Comp) then
8764 Reason_Bad_Component (Comp);
8768 Next_Component_Or_Discriminant (Comp);
8772 -- Deal with address clause case
8774 if Is_Object (E) then
8775 Addr := Address_Clause (E);
8777 if Present (Addr) then
8779 Error_Msg_Sloc := Sloc (Addr);
8780 Error_Msg_N ("\because of Address clause#", N);
8785 -- Deal with independent components for array type
8787 if IC and then Is_Array_Type (E) then
8788 Check_Array_Type (E);
8791 -- Deal with independent components for array object
8793 if IC and then Is_Object (E) and then Is_Array_Type (Etype (E)) then
8794 Check_Array_Type (Etype (E));
8799 end Validate_Independence;
8801 -----------------------------------
8802 -- Validate_Unchecked_Conversion --
8803 -----------------------------------
8805 procedure Validate_Unchecked_Conversion
8807 Act_Unit : Entity_Id)
8814 -- Obtain source and target types. Note that we call Ancestor_Subtype
8815 -- here because the processing for generic instantiation always makes
8816 -- subtypes, and we want the original frozen actual types.
8818 -- If we are dealing with private types, then do the check on their
8819 -- fully declared counterparts if the full declarations have been
8820 -- encountered (they don't have to be visible, but they must exist!)
8822 Source := Ancestor_Subtype (Etype (First_Formal (Act_Unit)));
8824 if Is_Private_Type (Source)
8825 and then Present (Underlying_Type (Source))
8827 Source := Underlying_Type (Source);
8830 Target := Ancestor_Subtype (Etype (Act_Unit));
8832 -- If either type is generic, the instantiation happens within a generic
8833 -- unit, and there is nothing to check. The proper check will happen
8834 -- when the enclosing generic is instantiated.
8836 if Is_Generic_Type (Source) or else Is_Generic_Type (Target) then
8840 if Is_Private_Type (Target)
8841 and then Present (Underlying_Type (Target))
8843 Target := Underlying_Type (Target);
8846 -- Source may be unconstrained array, but not target
8848 if Is_Array_Type (Target)
8849 and then not Is_Constrained (Target)
8852 ("unchecked conversion to unconstrained array not allowed", N);
8856 -- Warn if conversion between two different convention pointers
8858 if Is_Access_Type (Target)
8859 and then Is_Access_Type (Source)
8860 and then Convention (Target) /= Convention (Source)
8861 and then Warn_On_Unchecked_Conversion
8863 -- Give warnings for subprogram pointers only on most targets. The
8864 -- exception is VMS, where data pointers can have different lengths
8865 -- depending on the pointer convention.
8867 if Is_Access_Subprogram_Type (Target)
8868 or else Is_Access_Subprogram_Type (Source)
8869 or else OpenVMS_On_Target
8872 ("?conversion between pointers with different conventions!", N);
8876 -- Warn if one of the operands is Ada.Calendar.Time. Do not emit a
8877 -- warning when compiling GNAT-related sources.
8879 if Warn_On_Unchecked_Conversion
8880 and then not In_Predefined_Unit (N)
8881 and then RTU_Loaded (Ada_Calendar)
8883 (Chars (Source) = Name_Time
8885 Chars (Target) = Name_Time)
8887 -- If Ada.Calendar is loaded and the name of one of the operands is
8888 -- Time, there is a good chance that this is Ada.Calendar.Time.
8891 Calendar_Time : constant Entity_Id :=
8892 Full_View (RTE (RO_CA_Time));
8894 pragma Assert (Present (Calendar_Time));
8896 if Source = Calendar_Time
8897 or else Target = Calendar_Time
8900 ("?representation of 'Time values may change between " &
8901 "'G'N'A'T versions", N);
8906 -- Make entry in unchecked conversion table for later processing by
8907 -- Validate_Unchecked_Conversions, which will check sizes and alignments
8908 -- (using values set by the back-end where possible). This is only done
8909 -- if the appropriate warning is active.
8911 if Warn_On_Unchecked_Conversion then
8912 Unchecked_Conversions.Append
8913 (New_Val => UC_Entry'
8918 -- If both sizes are known statically now, then back end annotation
8919 -- is not required to do a proper check but if either size is not
8920 -- known statically, then we need the annotation.
8922 if Known_Static_RM_Size (Source)
8923 and then Known_Static_RM_Size (Target)
8927 Back_Annotate_Rep_Info := True;
8931 -- If unchecked conversion to access type, and access type is declared
8932 -- in the same unit as the unchecked conversion, then set the flag
8933 -- No_Strict_Aliasing (no strict aliasing is implicit here)
8935 if Is_Access_Type (Target) and then
8936 In_Same_Source_Unit (Target, N)
8938 Set_No_Strict_Aliasing (Implementation_Base_Type (Target));
8941 -- Generate N_Validate_Unchecked_Conversion node for back end in case
8942 -- the back end needs to perform special validation checks.
8944 -- Shouldn't this be in Exp_Ch13, since the check only gets done if we
8945 -- have full expansion and the back end is called ???
8948 Make_Validate_Unchecked_Conversion (Sloc (N));
8949 Set_Source_Type (Vnode, Source);
8950 Set_Target_Type (Vnode, Target);
8952 -- If the unchecked conversion node is in a list, just insert before it.
8953 -- If not we have some strange case, not worth bothering about.
8955 if Is_List_Member (N) then
8956 Insert_After (N, Vnode);
8958 end Validate_Unchecked_Conversion;
8960 ------------------------------------
8961 -- Validate_Unchecked_Conversions --
8962 ------------------------------------
8964 procedure Validate_Unchecked_Conversions is
8966 for N in Unchecked_Conversions.First .. Unchecked_Conversions.Last loop
8968 T : UC_Entry renames Unchecked_Conversions.Table (N);
8970 Eloc : constant Source_Ptr := T.Eloc;
8971 Source : constant Entity_Id := T.Source;
8972 Target : constant Entity_Id := T.Target;
8978 -- This validation check, which warns if we have unequal sizes for
8979 -- unchecked conversion, and thus potentially implementation
8980 -- dependent semantics, is one of the few occasions on which we
8981 -- use the official RM size instead of Esize. See description in
8982 -- Einfo "Handling of Type'Size Values" for details.
8984 if Serious_Errors_Detected = 0
8985 and then Known_Static_RM_Size (Source)
8986 and then Known_Static_RM_Size (Target)
8988 -- Don't do the check if warnings off for either type, note the
8989 -- deliberate use of OR here instead of OR ELSE to get the flag
8990 -- Warnings_Off_Used set for both types if appropriate.
8992 and then not (Has_Warnings_Off (Source)
8994 Has_Warnings_Off (Target))
8996 Source_Siz := RM_Size (Source);
8997 Target_Siz := RM_Size (Target);
8999 if Source_Siz /= Target_Siz then
9001 ("?types for unchecked conversion have different sizes!",
9004 if All_Errors_Mode then
9005 Error_Msg_Name_1 := Chars (Source);
9006 Error_Msg_Uint_1 := Source_Siz;
9007 Error_Msg_Name_2 := Chars (Target);
9008 Error_Msg_Uint_2 := Target_Siz;
9009 Error_Msg ("\size of % is ^, size of % is ^?", Eloc);
9011 Error_Msg_Uint_1 := UI_Abs (Source_Siz - Target_Siz);
9013 if Is_Discrete_Type (Source)
9014 and then Is_Discrete_Type (Target)
9016 if Source_Siz > Target_Siz then
9018 ("\?^ high order bits of source will be ignored!",
9021 elsif Is_Unsigned_Type (Source) then
9023 ("\?source will be extended with ^ high order " &
9024 "zero bits?!", Eloc);
9028 ("\?source will be extended with ^ high order " &
9033 elsif Source_Siz < Target_Siz then
9034 if Is_Discrete_Type (Target) then
9035 if Bytes_Big_Endian then
9037 ("\?target value will include ^ undefined " &
9042 ("\?target value will include ^ undefined " &
9049 ("\?^ trailing bits of target value will be " &
9050 "undefined!", Eloc);
9053 else pragma Assert (Source_Siz > Target_Siz);
9055 ("\?^ trailing bits of source will be ignored!",
9062 -- If both types are access types, we need to check the alignment.
9063 -- If the alignment of both is specified, we can do it here.
9065 if Serious_Errors_Detected = 0
9066 and then Ekind (Source) in Access_Kind
9067 and then Ekind (Target) in Access_Kind
9068 and then Target_Strict_Alignment
9069 and then Present (Designated_Type (Source))
9070 and then Present (Designated_Type (Target))
9073 D_Source : constant Entity_Id := Designated_Type (Source);
9074 D_Target : constant Entity_Id := Designated_Type (Target);
9077 if Known_Alignment (D_Source)
9078 and then Known_Alignment (D_Target)
9081 Source_Align : constant Uint := Alignment (D_Source);
9082 Target_Align : constant Uint := Alignment (D_Target);
9085 if Source_Align < Target_Align
9086 and then not Is_Tagged_Type (D_Source)
9088 -- Suppress warning if warnings suppressed on either
9089 -- type or either designated type. Note the use of
9090 -- OR here instead of OR ELSE. That is intentional,
9091 -- we would like to set flag Warnings_Off_Used in
9092 -- all types for which warnings are suppressed.
9094 and then not (Has_Warnings_Off (D_Source)
9096 Has_Warnings_Off (D_Target)
9098 Has_Warnings_Off (Source)
9100 Has_Warnings_Off (Target))
9102 Error_Msg_Uint_1 := Target_Align;
9103 Error_Msg_Uint_2 := Source_Align;
9104 Error_Msg_Node_1 := D_Target;
9105 Error_Msg_Node_2 := D_Source;
9107 ("?alignment of & (^) is stricter than " &
9108 "alignment of & (^)!", Eloc);
9110 ("\?resulting access value may have invalid " &
9111 "alignment!", Eloc);
9119 end Validate_Unchecked_Conversions;