1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2011, 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 -- Copy expression for later processing by the procedures
894 -- Check_Aspect_At_[Freeze_Point | End_Of_Declarations]
896 Set_Entity (Id, New_Copy_Tree (Expr));
898 -- Processing based on specific aspect
902 -- No_Aspect should be impossible
907 -- Aspects taking an optional boolean argument. For all of
908 -- these we just create a matching pragma and insert it, if
909 -- the expression is missing or set to True. If the expression
910 -- is False, we can ignore the aspect with the exception that
911 -- in the case of a derived type, we must check for an illegal
912 -- attempt to cancel an inherited aspect.
914 when Boolean_Aspects =>
915 Set_Is_Boolean_Aspect (Aspect);
918 and then Is_False (Static_Boolean (Expr))
920 Check_False_Aspect_For_Derived_Type;
924 -- If True, build corresponding pragma node
928 Pragma_Argument_Associations => New_List (Ent),
930 Make_Identifier (Sloc (Id), Chars (Id)));
932 -- Never need to delay for boolean aspects
934 pragma Assert (not Delay_Required);
936 -- Library unit aspects. These are boolean aspects, but we
937 -- have to do special things with the insertion, since the
938 -- pragma belongs inside the declarations of a package.
940 when Library_Unit_Aspects =>
942 and then Is_False (Static_Boolean (Expr))
947 -- Build corresponding pragma node
951 Pragma_Argument_Associations => New_List (Ent),
953 Make_Identifier (Sloc (Id), Chars (Id)));
955 -- This requires special handling in the case of a package
956 -- declaration, the pragma needs to be inserted in the list
957 -- of declarations for the associated package. There is no
958 -- issue of visibility delay for these aspects.
960 if Nkind (N) = N_Package_Declaration then
961 if Nkind (Parent (N)) /= N_Compilation_Unit then
963 ("incorrect context for library unit aspect&", Id);
966 (Aitem, Visible_Declarations (Specification (N)));
972 -- If not package declaration, no delay is required
974 pragma Assert (not Delay_Required);
976 -- Aspects related to container iterators. These aspects denote
977 -- subprograms, and thus must be delayed.
979 when Aspect_Constant_Indexing |
980 Aspect_Variable_Indexing =>
982 if not Is_Type (E) or else not Is_Tagged_Type (E) then
983 Error_Msg_N ("indexing applies to a tagged type", N);
987 Make_Attribute_Definition_Clause (Loc,
990 Expression => Relocate_Node (Expr));
992 Delay_Required := True;
993 Set_Is_Delayed_Aspect (Aspect);
995 when Aspect_Default_Iterator |
996 Aspect_Iterator_Element =>
999 Make_Attribute_Definition_Clause (Loc,
1001 Chars => Chars (Id),
1002 Expression => Relocate_Node (Expr));
1004 Delay_Required := True;
1005 Set_Is_Delayed_Aspect (Aspect);
1007 when Aspect_Implicit_Dereference =>
1009 or else not Has_Discriminants (E)
1012 ("Aspect must apply to a type with discriminants", N);
1020 Disc := First_Discriminant (E);
1021 while Present (Disc) loop
1022 if Chars (Expr) = Chars (Disc)
1023 and then Ekind (Etype (Disc)) =
1024 E_Anonymous_Access_Type
1026 Set_Has_Implicit_Dereference (E);
1027 Set_Has_Implicit_Dereference (Disc);
1031 Next_Discriminant (Disc);
1034 -- Error if no proper access discriminant.
1037 ("not an access discriminant of&", Expr, E);
1043 -- Aspects corresponding to attribute definition clauses
1045 when Aspect_Address |
1048 Aspect_Component_Size |
1049 Aspect_External_Tag |
1051 Aspect_Machine_Radix |
1052 Aspect_Object_Size |
1057 Aspect_Storage_Pool |
1058 Aspect_Storage_Size |
1059 Aspect_Stream_Size |
1063 -- Construct the attribute definition clause
1066 Make_Attribute_Definition_Clause (Loc,
1068 Chars => Chars (Id),
1069 Expression => Relocate_Node (Expr));
1071 -- A delay is required except in the common case where
1072 -- the expression is a literal, in which case it is fine
1073 -- to take care of it right away.
1075 if Nkind_In (Expr, N_Integer_Literal, N_String_Literal) then
1076 pragma Assert (not Delay_Required);
1079 Delay_Required := True;
1080 Set_Is_Delayed_Aspect (Aspect);
1083 -- Aspects corresponding to pragmas with two arguments, where
1084 -- the first argument is a local name referring to the entity,
1085 -- and the second argument is the aspect definition expression
1086 -- which is an expression that does not get analyzed.
1088 when Aspect_Suppress |
1089 Aspect_Unsuppress =>
1091 -- Construct the pragma
1095 Pragma_Argument_Associations => New_List (
1096 New_Occurrence_Of (E, Loc),
1097 Relocate_Node (Expr)),
1098 Pragma_Identifier =>
1099 Make_Identifier (Sloc (Id), Chars (Id)));
1101 -- We don't have to play the delay game here, since the only
1102 -- values are check names which don't get analyzed anyway.
1104 pragma Assert (not Delay_Required);
1106 -- Aspects corresponding to pragmas with two arguments, where
1107 -- the second argument is a local name referring to the entity,
1108 -- and the first argument is the aspect definition expression.
1110 when Aspect_Warnings =>
1112 -- Construct the pragma
1116 Pragma_Argument_Associations => New_List (
1117 Relocate_Node (Expr),
1118 New_Occurrence_Of (E, Loc)),
1119 Pragma_Identifier =>
1120 Make_Identifier (Sloc (Id), Chars (Id)),
1121 Class_Present => Class_Present (Aspect));
1123 -- We don't have to play the delay game here, since the only
1124 -- values are ON/OFF which don't get analyzed anyway.
1126 pragma Assert (not Delay_Required);
1128 -- Default_Value and Default_Component_Value aspects. These
1129 -- are specially handled because they have no corresponding
1130 -- pragmas or attributes.
1132 when Aspect_Default_Value | Aspect_Default_Component_Value =>
1133 Error_Msg_Name_1 := Chars (Id);
1135 if not Is_Type (E) then
1136 Error_Msg_N ("aspect% can only apply to a type", Id);
1139 elsif not Is_First_Subtype (E) then
1140 Error_Msg_N ("aspect% cannot apply to subtype", Id);
1143 elsif A_Id = Aspect_Default_Value
1144 and then not Is_Scalar_Type (E)
1147 ("aspect% can only be applied to scalar type", Id);
1150 elsif A_Id = Aspect_Default_Component_Value then
1151 if not Is_Array_Type (E) then
1153 ("aspect% can only be applied to array type", Id);
1155 elsif not Is_Scalar_Type (Component_Type (E)) then
1157 ("aspect% requires scalar components", Id);
1163 Delay_Required := True;
1164 Set_Is_Delayed_Aspect (Aspect);
1165 Set_Has_Default_Aspect (Base_Type (Entity (Ent)));
1167 when Aspect_Attach_Handler =>
1170 Pragma_Identifier =>
1171 Make_Identifier (Sloc (Id), Name_Attach_Handler),
1172 Pragma_Argument_Associations =>
1173 New_List (Ent, Relocate_Node (Expr)));
1175 Set_From_Aspect_Specification (Aitem, True);
1176 Set_Corresponding_Aspect (Aitem, Aspect);
1178 pragma Assert (not Delay_Required);
1180 when Aspect_Priority |
1181 Aspect_Interrupt_Priority |
1182 Aspect_Dispatching_Domain |
1188 if A_Id = Aspect_Priority then
1189 Pname := Name_Priority;
1191 elsif A_Id = Aspect_Interrupt_Priority then
1192 Pname := Name_Interrupt_Priority;
1194 elsif A_Id = Aspect_CPU then
1198 Pname := Name_Dispatching_Domain;
1203 Pragma_Identifier =>
1204 Make_Identifier (Sloc (Id), Pname),
1205 Pragma_Argument_Associations =>
1207 (Make_Pragma_Argument_Association
1209 Expression => Relocate_Node (Expr))));
1211 Set_From_Aspect_Specification (Aitem, True);
1212 Set_Corresponding_Aspect (Aitem, Aspect);
1214 pragma Assert (not Delay_Required);
1217 -- Aspects Pre/Post generate Precondition/Postcondition pragmas
1218 -- with a first argument that is the expression, and a second
1219 -- argument that is an informative message if the test fails.
1220 -- This is inserted right after the declaration, to get the
1221 -- required pragma placement. The processing for the pragmas
1222 -- takes care of the required delay.
1224 when Pre_Post_Aspects => declare
1228 if A_Id = Aspect_Pre or else A_Id = Aspect_Precondition then
1229 Pname := Name_Precondition;
1231 Pname := Name_Postcondition;
1234 -- If the expressions is of the form A and then B, then
1235 -- we generate separate Pre/Post aspects for the separate
1236 -- clauses. Since we allow multiple pragmas, there is no
1237 -- problem in allowing multiple Pre/Post aspects internally.
1238 -- These should be treated in reverse order (B first and
1239 -- A second) since they are later inserted just after N in
1240 -- the order they are treated. This way, the pragma for A
1241 -- ends up preceding the pragma for B, which may have an
1242 -- importance for the error raised (either constraint error
1243 -- or precondition error).
1245 -- We do not do this for Pre'Class, since we have to put
1246 -- these conditions together in a complex OR expression
1248 -- We do not do this in ASIS mode, as ASIS relies on the
1249 -- original node representing the complete expression, when
1250 -- retrieving it through the source aspect table.
1253 and then (Pname = Name_Postcondition
1254 or else not Class_Present (Aspect))
1256 while Nkind (Expr) = N_And_Then loop
1257 Insert_After (Aspect,
1258 Make_Aspect_Specification (Sloc (Left_Opnd (Expr)),
1259 Identifier => Identifier (Aspect),
1260 Expression => Relocate_Node (Left_Opnd (Expr)),
1261 Class_Present => Class_Present (Aspect),
1262 Split_PPC => True));
1263 Rewrite (Expr, Relocate_Node (Right_Opnd (Expr)));
1264 Eloc := Sloc (Expr);
1268 -- Build the precondition/postcondition pragma
1272 Pragma_Identifier =>
1273 Make_Identifier (Sloc (Id), Pname),
1274 Class_Present => Class_Present (Aspect),
1275 Split_PPC => Split_PPC (Aspect),
1276 Pragma_Argument_Associations => New_List (
1277 Make_Pragma_Argument_Association (Eloc,
1278 Chars => Name_Check,
1279 Expression => Relocate_Node (Expr))));
1281 -- Add message unless exception messages are suppressed
1283 if not Opt.Exception_Locations_Suppressed then
1284 Append_To (Pragma_Argument_Associations (Aitem),
1285 Make_Pragma_Argument_Association (Eloc,
1286 Chars => Name_Message,
1288 Make_String_Literal (Eloc,
1290 & Get_Name_String (Pname)
1292 & Build_Location_String (Eloc))));
1295 Set_From_Aspect_Specification (Aitem, True);
1296 Set_Corresponding_Aspect (Aitem, Aspect);
1297 Set_Is_Delayed_Aspect (Aspect);
1299 -- For Pre/Post cases, insert immediately after the entity
1300 -- declaration, since that is the required pragma placement.
1301 -- Note that for these aspects, we do not have to worry
1302 -- about delay issues, since the pragmas themselves deal
1303 -- with delay of visibility for the expression analysis.
1305 -- If the entity is a library-level subprogram, the pre/
1306 -- postconditions must be treated as late pragmas.
1308 if Nkind (Parent (N)) = N_Compilation_Unit then
1309 Add_Global_Declaration (Aitem);
1311 Insert_After (N, Aitem);
1317 -- Invariant aspects generate a corresponding pragma with a
1318 -- first argument that is the entity, a second argument that is
1319 -- the expression and a third argument that is an appropriate
1320 -- message. This is inserted right after the declaration, to
1321 -- get the required pragma placement. The pragma processing
1322 -- takes care of the required delay.
1324 when Aspect_Invariant |
1325 Aspect_Type_Invariant =>
1327 -- Analysis of the pragma will verify placement legality:
1328 -- an invariant must apply to a private type, or appear in
1329 -- the private part of a spec and apply to a completion.
1331 -- Construct the pragma
1335 Pragma_Argument_Associations =>
1336 New_List (Ent, Relocate_Node (Expr)),
1337 Class_Present => Class_Present (Aspect),
1338 Pragma_Identifier =>
1339 Make_Identifier (Sloc (Id), Name_Invariant));
1341 -- Add message unless exception messages are suppressed
1343 if not Opt.Exception_Locations_Suppressed then
1344 Append_To (Pragma_Argument_Associations (Aitem),
1345 Make_Pragma_Argument_Association (Eloc,
1346 Chars => Name_Message,
1348 Make_String_Literal (Eloc,
1349 Strval => "failed invariant from "
1350 & Build_Location_String (Eloc))));
1353 Set_From_Aspect_Specification (Aitem, True);
1354 Set_Corresponding_Aspect (Aitem, Aspect);
1355 Set_Is_Delayed_Aspect (Aspect);
1357 -- For Invariant case, insert immediately after the entity
1358 -- declaration. We do not have to worry about delay issues
1359 -- since the pragma processing takes care of this.
1361 Insert_After (N, Aitem);
1364 -- Predicate aspects generate a corresponding pragma with a
1365 -- first argument that is the entity, and the second argument
1366 -- is the expression.
1368 when Aspect_Dynamic_Predicate |
1370 Aspect_Static_Predicate =>
1372 -- Construct the pragma (always a pragma Predicate, with
1373 -- flags recording whether it is static/dynamic).
1377 Pragma_Argument_Associations =>
1378 New_List (Ent, Relocate_Node (Expr)),
1379 Class_Present => Class_Present (Aspect),
1380 Pragma_Identifier =>
1381 Make_Identifier (Sloc (Id), Name_Predicate));
1383 Set_From_Aspect_Specification (Aitem, True);
1384 Set_Corresponding_Aspect (Aitem, Aspect);
1386 -- Make sure we have a freeze node (it might otherwise be
1387 -- missing in cases like subtype X is Y, and we would not
1388 -- have a place to build the predicate function).
1390 Set_Has_Predicates (E);
1392 if Is_Private_Type (E)
1393 and then Present (Full_View (E))
1395 Set_Has_Predicates (Full_View (E));
1396 Set_Has_Delayed_Aspects (Full_View (E));
1399 Ensure_Freeze_Node (E);
1400 Set_Is_Delayed_Aspect (Aspect);
1401 Delay_Required := True;
1403 when Aspect_Test_Case => declare
1405 Comp_Expr : Node_Id;
1406 Comp_Assn : Node_Id;
1412 if Nkind (Parent (N)) = N_Compilation_Unit then
1414 ("incorrect placement of aspect `Test_Case`", E);
1418 if Nkind (Expr) /= N_Aggregate then
1420 ("wrong syntax for aspect `Test_Case` for &", Id, E);
1424 -- Make pragma expressions refer to the original aspect
1425 -- expressions through the Original_Node link. This is used
1426 -- in semantic analysis for ASIS mode, so that the original
1427 -- expression also gets analyzed.
1429 Comp_Expr := First (Expressions (Expr));
1430 while Present (Comp_Expr) loop
1431 New_Expr := Relocate_Node (Comp_Expr);
1432 Set_Original_Node (New_Expr, Comp_Expr);
1434 (Make_Pragma_Argument_Association (Sloc (Comp_Expr),
1435 Expression => New_Expr),
1440 Comp_Assn := First (Component_Associations (Expr));
1441 while Present (Comp_Assn) loop
1442 if List_Length (Choices (Comp_Assn)) /= 1
1444 Nkind (First (Choices (Comp_Assn))) /= N_Identifier
1447 ("wrong syntax for aspect `Test_Case` for &", Id, E);
1451 New_Expr := Relocate_Node (Expression (Comp_Assn));
1452 Set_Original_Node (New_Expr, Expression (Comp_Assn));
1453 Append (Make_Pragma_Argument_Association (
1454 Sloc => Sloc (Comp_Assn),
1455 Chars => Chars (First (Choices (Comp_Assn))),
1456 Expression => New_Expr),
1461 -- Build the test-case pragma
1465 Pragma_Identifier =>
1466 Make_Identifier (Sloc (Id), Name_Test_Case),
1467 Pragma_Argument_Associations =>
1470 Set_From_Aspect_Specification (Aitem, True);
1471 Set_Corresponding_Aspect (Aitem, Aspect);
1472 Set_Is_Delayed_Aspect (Aspect);
1474 -- Insert immediately after the entity declaration
1476 Insert_After (N, Aitem);
1481 when Aspect_Dimension =>
1482 Analyze_Aspect_Dimension (N, Id, Expr);
1485 when Aspect_Dimension_System =>
1486 Analyze_Aspect_Dimension_System (N, Id, Expr);
1491 -- If a delay is required, we delay the freeze (not much point in
1492 -- delaying the aspect if we don't delay the freeze!). The pragma
1493 -- or attribute clause if there is one is then attached to the
1494 -- aspect specification which is placed in the rep item list.
1496 if Delay_Required then
1497 if Present (Aitem) then
1498 Set_From_Aspect_Specification (Aitem, True);
1500 if Nkind (Aitem) = N_Pragma then
1501 Set_Corresponding_Aspect (Aitem, Aspect);
1504 Set_Is_Delayed_Aspect (Aitem);
1505 Set_Aspect_Rep_Item (Aspect, Aitem);
1508 Ensure_Freeze_Node (E);
1509 Set_Has_Delayed_Aspects (E);
1510 Record_Rep_Item (E, Aspect);
1512 -- If no delay required, insert the pragma/clause in the tree
1515 Set_From_Aspect_Specification (Aitem, True);
1517 if Nkind (Aitem) = N_Pragma then
1518 Set_Corresponding_Aspect (Aitem, Aspect);
1521 -- If this is a compilation unit, we will put the pragma in
1522 -- the Pragmas_After list of the N_Compilation_Unit_Aux node.
1524 if Nkind (Parent (Ins_Node)) = N_Compilation_Unit then
1526 Aux : constant Node_Id :=
1527 Aux_Decls_Node (Parent (Ins_Node));
1530 pragma Assert (Nkind (Aux) = N_Compilation_Unit_Aux);
1532 if No (Pragmas_After (Aux)) then
1533 Set_Pragmas_After (Aux, Empty_List);
1536 -- For Pre_Post put at start of list, otherwise at end
1538 if A_Id in Pre_Post_Aspects then
1539 Prepend (Aitem, Pragmas_After (Aux));
1541 Append (Aitem, Pragmas_After (Aux));
1545 -- Here if not compilation unit case
1550 -- For Pre/Post cases, insert immediately after the
1551 -- entity declaration, since that is the required pragma
1554 when Pre_Post_Aspects =>
1555 Insert_After (N, Aitem);
1557 -- For Priority aspects, insert into the task or
1558 -- protected definition, which we need to create if it's
1559 -- not there. The same applies to CPU and
1560 -- Dispatching_Domain but only to tasks.
1562 when Aspect_Priority |
1563 Aspect_Interrupt_Priority |
1564 Aspect_Dispatching_Domain |
1567 T : Node_Id; -- the type declaration
1568 L : List_Id; -- list of decls of task/protected
1571 if Nkind (N) = N_Object_Declaration then
1572 T := Parent (Etype (Defining_Identifier (N)));
1577 if Nkind (T) = N_Protected_Type_Declaration
1578 and then A_Id /= Aspect_Dispatching_Domain
1579 and then A_Id /= Aspect_CPU
1582 (Present (Protected_Definition (T)));
1584 L := Visible_Declarations
1585 (Protected_Definition (T));
1587 elsif Nkind (T) = N_Task_Type_Declaration then
1588 if No (Task_Definition (T)) then
1591 Make_Task_Definition
1593 Visible_Declarations => New_List,
1594 End_Label => Empty));
1597 L := Visible_Declarations (Task_Definition (T));
1600 raise Program_Error;
1603 Prepend (Aitem, To => L);
1605 -- Analyze rewritten pragma. Otherwise, its
1606 -- analysis is done too late, after the task or
1607 -- protected object has been created.
1612 -- For all other cases, insert in sequence
1615 Insert_After (Ins_Node, Aitem);
1624 end loop Aspect_Loop;
1625 end Analyze_Aspect_Specifications;
1627 -----------------------
1628 -- Analyze_At_Clause --
1629 -----------------------
1631 -- An at clause is replaced by the corresponding Address attribute
1632 -- definition clause that is the preferred approach in Ada 95.
1634 procedure Analyze_At_Clause (N : Node_Id) is
1635 CS : constant Boolean := Comes_From_Source (N);
1638 -- This is an obsolescent feature
1640 Check_Restriction (No_Obsolescent_Features, N);
1642 if Warn_On_Obsolescent_Feature then
1644 ("at clause is an obsolescent feature (RM J.7(2))?", N);
1646 ("\use address attribute definition clause instead?", N);
1649 -- Rewrite as address clause
1652 Make_Attribute_Definition_Clause (Sloc (N),
1653 Name => Identifier (N),
1654 Chars => Name_Address,
1655 Expression => Expression (N)));
1657 -- We preserve Comes_From_Source, since logically the clause still
1658 -- comes from the source program even though it is changed in form.
1660 Set_Comes_From_Source (N, CS);
1662 -- Analyze rewritten clause
1664 Analyze_Attribute_Definition_Clause (N);
1665 end Analyze_At_Clause;
1667 -----------------------------------------
1668 -- Analyze_Attribute_Definition_Clause --
1669 -----------------------------------------
1671 procedure Analyze_Attribute_Definition_Clause (N : Node_Id) is
1672 Loc : constant Source_Ptr := Sloc (N);
1673 Nam : constant Node_Id := Name (N);
1674 Attr : constant Name_Id := Chars (N);
1675 Expr : constant Node_Id := Expression (N);
1676 Id : constant Attribute_Id := Get_Attribute_Id (Attr);
1679 -- The entity of Nam after it is analyzed. In the case of an incomplete
1680 -- type, this is the underlying type.
1683 -- The underlying entity to which the attribute applies. Generally this
1684 -- is the Underlying_Type of Ent, except in the case where the clause
1685 -- applies to full view of incomplete type or private type in which case
1686 -- U_Ent is just a copy of Ent.
1688 FOnly : Boolean := False;
1689 -- Reset to True for subtype specific attribute (Alignment, Size)
1690 -- and for stream attributes, i.e. those cases where in the call
1691 -- to Rep_Item_Too_Late, FOnly is set True so that only the freezing
1692 -- rules are checked. Note that the case of stream attributes is not
1693 -- clear from the RM, but see AI95-00137. Also, the RM seems to
1694 -- disallow Storage_Size for derived task types, but that is also
1695 -- clearly unintentional.
1697 procedure Analyze_Stream_TSS_Definition (TSS_Nam : TSS_Name_Type);
1698 -- Common processing for 'Read, 'Write, 'Input and 'Output attribute
1699 -- definition clauses.
1701 function Duplicate_Clause return Boolean;
1702 -- This routine checks if the aspect for U_Ent being given by attribute
1703 -- definition clause N is for an aspect that has already been specified,
1704 -- and if so gives an error message. If there is a duplicate, True is
1705 -- returned, otherwise if there is no error, False is returned.
1707 procedure Check_Indexing_Functions;
1708 -- Check that the function in Constant_Indexing or Variable_Indexing
1709 -- attribute has the proper type structure. If the name is overloaded,
1710 -- check that all interpretations are legal.
1712 procedure Check_Iterator_Functions;
1713 -- Check that there is a single function in Default_Iterator attribute
1714 -- has the proper type structure.
1716 function Check_Primitive_Function (Subp : Entity_Id) return Boolean;
1717 -- Common legality check for the previous two
1719 -----------------------------------
1720 -- Analyze_Stream_TSS_Definition --
1721 -----------------------------------
1723 procedure Analyze_Stream_TSS_Definition (TSS_Nam : TSS_Name_Type) is
1724 Subp : Entity_Id := Empty;
1729 Is_Read : constant Boolean := (TSS_Nam = TSS_Stream_Read);
1730 -- True for Read attribute, false for other attributes
1732 function Has_Good_Profile (Subp : Entity_Id) return Boolean;
1733 -- Return true if the entity is a subprogram with an appropriate
1734 -- profile for the attribute being defined.
1736 ----------------------
1737 -- Has_Good_Profile --
1738 ----------------------
1740 function Has_Good_Profile (Subp : Entity_Id) return Boolean is
1742 Is_Function : constant Boolean := (TSS_Nam = TSS_Stream_Input);
1743 Expected_Ekind : constant array (Boolean) of Entity_Kind :=
1744 (False => E_Procedure, True => E_Function);
1748 if Ekind (Subp) /= Expected_Ekind (Is_Function) then
1752 F := First_Formal (Subp);
1755 or else Ekind (Etype (F)) /= E_Anonymous_Access_Type
1756 or else Designated_Type (Etype (F)) /=
1757 Class_Wide_Type (RTE (RE_Root_Stream_Type))
1762 if not Is_Function then
1766 Expected_Mode : constant array (Boolean) of Entity_Kind :=
1767 (False => E_In_Parameter,
1768 True => E_Out_Parameter);
1770 if Parameter_Mode (F) /= Expected_Mode (Is_Read) then
1778 Typ := Etype (Subp);
1781 return Base_Type (Typ) = Base_Type (Ent)
1782 and then No (Next_Formal (F));
1783 end Has_Good_Profile;
1785 -- Start of processing for Analyze_Stream_TSS_Definition
1790 if not Is_Type (U_Ent) then
1791 Error_Msg_N ("local name must be a subtype", Nam);
1795 Pnam := TSS (Base_Type (U_Ent), TSS_Nam);
1797 -- If Pnam is present, it can be either inherited from an ancestor
1798 -- type (in which case it is legal to redefine it for this type), or
1799 -- be a previous definition of the attribute for the same type (in
1800 -- which case it is illegal).
1802 -- In the first case, it will have been analyzed already, and we
1803 -- can check that its profile does not match the expected profile
1804 -- for a stream attribute of U_Ent. In the second case, either Pnam
1805 -- has been analyzed (and has the expected profile), or it has not
1806 -- been analyzed yet (case of a type that has not been frozen yet
1807 -- and for which the stream attribute has been set using Set_TSS).
1810 and then (No (First_Entity (Pnam)) or else Has_Good_Profile (Pnam))
1812 Error_Msg_Sloc := Sloc (Pnam);
1813 Error_Msg_Name_1 := Attr;
1814 Error_Msg_N ("% attribute already defined #", Nam);
1820 if Is_Entity_Name (Expr) then
1821 if not Is_Overloaded (Expr) then
1822 if Has_Good_Profile (Entity (Expr)) then
1823 Subp := Entity (Expr);
1827 Get_First_Interp (Expr, I, It);
1828 while Present (It.Nam) loop
1829 if Has_Good_Profile (It.Nam) then
1834 Get_Next_Interp (I, It);
1839 if Present (Subp) then
1840 if Is_Abstract_Subprogram (Subp) then
1841 Error_Msg_N ("stream subprogram must not be abstract", Expr);
1845 Set_Entity (Expr, Subp);
1846 Set_Etype (Expr, Etype (Subp));
1848 New_Stream_Subprogram (N, U_Ent, Subp, TSS_Nam);
1851 Error_Msg_Name_1 := Attr;
1852 Error_Msg_N ("incorrect expression for% attribute", Expr);
1854 end Analyze_Stream_TSS_Definition;
1856 ------------------------------
1857 -- Check_Indexing_Functions --
1858 ------------------------------
1860 procedure Check_Indexing_Functions is
1862 procedure Check_One_Function (Subp : Entity_Id);
1863 -- Check one possible interpretation
1865 ------------------------
1866 -- Check_One_Function --
1867 ------------------------
1869 procedure Check_One_Function (Subp : Entity_Id) is
1870 Default_Element : constant Node_Id :=
1872 (Etype (First_Formal (Subp)),
1873 Aspect_Iterator_Element);
1876 if not Check_Primitive_Function (Subp) then
1878 ("aspect Indexing requires a function that applies to type&",
1882 -- An indexing function must return either the default element of
1883 -- the container, or a reference type.
1885 if Present (Default_Element) then
1886 Analyze (Default_Element);
1887 if Is_Entity_Name (Default_Element)
1888 and then Covers (Entity (Default_Element), Etype (Subp))
1894 -- Otherwise the return type must be a reference type.
1896 if not Has_Implicit_Dereference (Etype (Subp)) then
1898 ("function for indexing must return a reference type", Subp);
1900 end Check_One_Function;
1902 -- Start of processing for Check_Indexing_Functions
1911 if not Is_Overloaded (Expr) then
1912 Check_One_Function (Entity (Expr));
1920 Get_First_Interp (Expr, I, It);
1921 while Present (It.Nam) loop
1923 -- Note that analysis will have added the interpretation
1924 -- that corresponds to the dereference. We only check the
1925 -- subprogram itself.
1927 if Is_Overloadable (It.Nam) then
1928 Check_One_Function (It.Nam);
1931 Get_Next_Interp (I, It);
1935 end Check_Indexing_Functions;
1937 ------------------------------
1938 -- Check_Iterator_Functions --
1939 ------------------------------
1941 procedure Check_Iterator_Functions is
1942 Default : Entity_Id;
1944 function Valid_Default_Iterator (Subp : Entity_Id) return Boolean;
1945 -- Check one possible interpretation for validity
1947 ----------------------------
1948 -- Valid_Default_Iterator --
1949 ----------------------------
1951 function Valid_Default_Iterator (Subp : Entity_Id) return Boolean is
1955 if not Check_Primitive_Function (Subp) then
1958 Formal := First_Formal (Subp);
1961 -- False if any subsequent formal has no default expression
1963 Formal := Next_Formal (Formal);
1964 while Present (Formal) loop
1965 if No (Expression (Parent (Formal))) then
1969 Next_Formal (Formal);
1972 -- True if all subsequent formals have default expressions
1975 end Valid_Default_Iterator;
1977 -- Start of processing for Check_Iterator_Functions
1982 if not Is_Entity_Name (Expr) then
1983 Error_Msg_N ("aspect Iterator must be a function name", Expr);
1986 if not Is_Overloaded (Expr) then
1987 if not Check_Primitive_Function (Entity (Expr)) then
1989 ("aspect Indexing requires a function that applies to type&",
1990 Entity (Expr), Ent);
1993 if not Valid_Default_Iterator (Entity (Expr)) then
1994 Error_Msg_N ("improper function for default iterator", Expr);
2004 Get_First_Interp (Expr, I, It);
2005 while Present (It.Nam) loop
2006 if not Check_Primitive_Function (It.Nam)
2007 or else not Valid_Default_Iterator (It.Nam)
2011 elsif Present (Default) then
2012 Error_Msg_N ("default iterator must be unique", Expr);
2018 Get_Next_Interp (I, It);
2022 if Present (Default) then
2023 Set_Entity (Expr, Default);
2024 Set_Is_Overloaded (Expr, False);
2027 end Check_Iterator_Functions;
2029 -------------------------------
2030 -- Check_Primitive_Function --
2031 -------------------------------
2033 function Check_Primitive_Function (Subp : Entity_Id) return Boolean is
2037 if Ekind (Subp) /= E_Function then
2041 if No (First_Formal (Subp)) then
2044 Ctrl := Etype (First_Formal (Subp));
2048 or else Ctrl = Class_Wide_Type (Ent)
2050 (Ekind (Ctrl) = E_Anonymous_Access_Type
2052 (Designated_Type (Ctrl) = Ent
2053 or else Designated_Type (Ctrl) = Class_Wide_Type (Ent)))
2062 end Check_Primitive_Function;
2064 ----------------------
2065 -- Duplicate_Clause --
2066 ----------------------
2068 function Duplicate_Clause return Boolean is
2072 -- Nothing to do if this attribute definition clause comes from
2073 -- an aspect specification, since we could not be duplicating an
2074 -- explicit clause, and we dealt with the case of duplicated aspects
2075 -- in Analyze_Aspect_Specifications.
2077 if From_Aspect_Specification (N) then
2081 -- Otherwise current clause may duplicate previous clause or a
2082 -- previously given aspect specification for the same aspect.
2084 A := Get_Rep_Item_For_Entity (U_Ent, Chars (N));
2087 if Entity (A) = U_Ent then
2088 Error_Msg_Name_1 := Chars (N);
2089 Error_Msg_Sloc := Sloc (A);
2090 Error_Msg_NE ("aspect% for & previously given#", N, U_Ent);
2096 end Duplicate_Clause;
2098 -- Start of processing for Analyze_Attribute_Definition_Clause
2101 -- The following code is a defense against recursion. Not clear that
2102 -- this can happen legitimately, but perhaps some error situations
2103 -- can cause it, and we did see this recursion during testing.
2105 if Analyzed (N) then
2108 Set_Analyzed (N, True);
2111 -- Process Ignore_Rep_Clauses option (we also ignore rep clauses in
2112 -- CodePeer mode or Alfa mode, since they are not relevant in these
2115 if Ignore_Rep_Clauses or CodePeer_Mode or Alfa_Mode then
2118 -- The following should be ignored. They do not affect legality
2119 -- and may be target dependent. The basic idea of -gnatI is to
2120 -- ignore any rep clauses that may be target dependent but do not
2121 -- affect legality (except possibly to be rejected because they
2122 -- are incompatible with the compilation target).
2124 when Attribute_Alignment |
2125 Attribute_Bit_Order |
2126 Attribute_Component_Size |
2127 Attribute_Machine_Radix |
2128 Attribute_Object_Size |
2130 Attribute_Stream_Size |
2131 Attribute_Value_Size =>
2132 Rewrite (N, Make_Null_Statement (Sloc (N)));
2135 -- We do not want too ignore 'Small in CodePeer_Mode or Alfa_Mode,
2136 -- since it has an impact on the exact computations performed.
2138 -- Perhaps 'Small should also not be ignored by
2139 -- Ignore_Rep_Clauses ???
2141 when Attribute_Small =>
2142 if Ignore_Rep_Clauses then
2143 Rewrite (N, Make_Null_Statement (Sloc (N)));
2147 -- The following should not be ignored, because in the first place
2148 -- they are reasonably portable, and should not cause problems in
2149 -- compiling code from another target, and also they do affect
2150 -- legality, e.g. failing to provide a stream attribute for a
2151 -- type may make a program illegal.
2153 when Attribute_External_Tag |
2157 Attribute_Storage_Pool |
2158 Attribute_Storage_Size |
2162 -- Other cases are errors ("attribute& cannot be set with
2163 -- definition clause"), which will be caught below.
2171 Ent := Entity (Nam);
2173 if Rep_Item_Too_Early (Ent, N) then
2177 -- Rep clause applies to full view of incomplete type or private type if
2178 -- we have one (if not, this is a premature use of the type). However,
2179 -- certain semantic checks need to be done on the specified entity (i.e.
2180 -- the private view), so we save it in Ent.
2182 if Is_Private_Type (Ent)
2183 and then Is_Derived_Type (Ent)
2184 and then not Is_Tagged_Type (Ent)
2185 and then No (Full_View (Ent))
2187 -- If this is a private type whose completion is a derivation from
2188 -- another private type, there is no full view, and the attribute
2189 -- belongs to the type itself, not its underlying parent.
2193 elsif Ekind (Ent) = E_Incomplete_Type then
2195 -- The attribute applies to the full view, set the entity of the
2196 -- attribute definition accordingly.
2198 Ent := Underlying_Type (Ent);
2200 Set_Entity (Nam, Ent);
2203 U_Ent := Underlying_Type (Ent);
2206 -- Avoid cascaded error
2208 if Etype (Nam) = Any_Type then
2211 -- Must be declared in current scope
2213 elsif Scope (Ent) /= Current_Scope then
2214 Error_Msg_N ("entity must be declared in this scope", Nam);
2217 -- Must not be a source renaming (we do have some cases where the
2218 -- expander generates a renaming, and those cases are OK, in such
2219 -- cases any attribute applies to the renamed object as well).
2221 elsif Is_Object (Ent)
2222 and then Present (Renamed_Object (Ent))
2224 -- Case of renamed object from source, this is an error
2226 if Comes_From_Source (Renamed_Object (Ent)) then
2227 Get_Name_String (Chars (N));
2228 Error_Msg_Strlen := Name_Len;
2229 Error_Msg_String (1 .. Name_Len) := Name_Buffer (1 .. Name_Len);
2231 ("~ clause not allowed for a renaming declaration "
2232 & "(RM 13.1(6))", Nam);
2235 -- For the case of a compiler generated renaming, the attribute
2236 -- definition clause applies to the renamed object created by the
2237 -- expander. The easiest general way to handle this is to create a
2238 -- copy of the attribute definition clause for this object.
2242 Make_Attribute_Definition_Clause (Loc,
2244 New_Occurrence_Of (Entity (Renamed_Object (Ent)), Loc),
2246 Expression => Duplicate_Subexpr (Expression (N))));
2249 -- If no underlying entity, use entity itself, applies to some
2250 -- previously detected error cases ???
2252 elsif No (U_Ent) then
2255 -- Cannot specify for a subtype (exception Object/Value_Size)
2257 elsif Is_Type (U_Ent)
2258 and then not Is_First_Subtype (U_Ent)
2259 and then Id /= Attribute_Object_Size
2260 and then Id /= Attribute_Value_Size
2261 and then not From_At_Mod (N)
2263 Error_Msg_N ("cannot specify attribute for subtype", Nam);
2267 Set_Entity (N, U_Ent);
2269 -- Switch on particular attribute
2277 -- Address attribute definition clause
2279 when Attribute_Address => Address : begin
2281 -- A little error check, catch for X'Address use X'Address;
2283 if Nkind (Nam) = N_Identifier
2284 and then Nkind (Expr) = N_Attribute_Reference
2285 and then Attribute_Name (Expr) = Name_Address
2286 and then Nkind (Prefix (Expr)) = N_Identifier
2287 and then Chars (Nam) = Chars (Prefix (Expr))
2290 ("address for & is self-referencing", Prefix (Expr), Ent);
2294 -- Not that special case, carry on with analysis of expression
2296 Analyze_And_Resolve (Expr, RTE (RE_Address));
2298 -- Even when ignoring rep clauses we need to indicate that the
2299 -- entity has an address clause and thus it is legal to declare
2302 if Ignore_Rep_Clauses then
2303 if Ekind_In (U_Ent, E_Variable, E_Constant) then
2304 Record_Rep_Item (U_Ent, N);
2310 if Duplicate_Clause then
2313 -- Case of address clause for subprogram
2315 elsif Is_Subprogram (U_Ent) then
2316 if Has_Homonym (U_Ent) then
2318 ("address clause cannot be given " &
2319 "for overloaded subprogram",
2324 -- For subprograms, all address clauses are permitted, and we
2325 -- mark the subprogram as having a deferred freeze so that Gigi
2326 -- will not elaborate it too soon.
2328 -- Above needs more comments, what is too soon about???
2330 Set_Has_Delayed_Freeze (U_Ent);
2332 -- Case of address clause for entry
2334 elsif Ekind (U_Ent) = E_Entry then
2335 if Nkind (Parent (N)) = N_Task_Body then
2337 ("entry address must be specified in task spec", Nam);
2341 -- For entries, we require a constant address
2343 Check_Constant_Address_Clause (Expr, U_Ent);
2345 -- Special checks for task types
2347 if Is_Task_Type (Scope (U_Ent))
2348 and then Comes_From_Source (Scope (U_Ent))
2351 ("?entry address declared for entry in task type", N);
2353 ("\?only one task can be declared of this type", N);
2356 -- Entry address clauses are obsolescent
2358 Check_Restriction (No_Obsolescent_Features, N);
2360 if Warn_On_Obsolescent_Feature then
2362 ("attaching interrupt to task entry is an " &
2363 "obsolescent feature (RM J.7.1)?", N);
2365 ("\use interrupt procedure instead?", N);
2368 -- Case of an address clause for a controlled object which we
2369 -- consider to be erroneous.
2371 elsif Is_Controlled (Etype (U_Ent))
2372 or else Has_Controlled_Component (Etype (U_Ent))
2375 ("?controlled object& must not be overlaid", Nam, U_Ent);
2377 ("\?Program_Error will be raised at run time", Nam);
2378 Insert_Action (Declaration_Node (U_Ent),
2379 Make_Raise_Program_Error (Loc,
2380 Reason => PE_Overlaid_Controlled_Object));
2383 -- Case of address clause for a (non-controlled) object
2386 Ekind (U_Ent) = E_Variable
2388 Ekind (U_Ent) = E_Constant
2391 Expr : constant Node_Id := Expression (N);
2396 -- Exported variables cannot have an address clause, because
2397 -- this cancels the effect of the pragma Export.
2399 if Is_Exported (U_Ent) then
2401 ("cannot export object with address clause", Nam);
2405 Find_Overlaid_Entity (N, O_Ent, Off);
2407 -- Overlaying controlled objects is erroneous
2410 and then (Has_Controlled_Component (Etype (O_Ent))
2411 or else Is_Controlled (Etype (O_Ent)))
2414 ("?cannot overlay with controlled object", Expr);
2416 ("\?Program_Error will be raised at run time", Expr);
2417 Insert_Action (Declaration_Node (U_Ent),
2418 Make_Raise_Program_Error (Loc,
2419 Reason => PE_Overlaid_Controlled_Object));
2422 elsif Present (O_Ent)
2423 and then Ekind (U_Ent) = E_Constant
2424 and then not Is_Constant_Object (O_Ent)
2426 Error_Msg_N ("constant overlays a variable?", Expr);
2428 -- Imported variables can have an address clause, but then
2429 -- the import is pretty meaningless except to suppress
2430 -- initializations, so we do not need such variables to
2431 -- be statically allocated (and in fact it causes trouble
2432 -- if the address clause is a local value).
2434 elsif Is_Imported (U_Ent) then
2435 Set_Is_Statically_Allocated (U_Ent, False);
2438 -- We mark a possible modification of a variable with an
2439 -- address clause, since it is likely aliasing is occurring.
2441 Note_Possible_Modification (Nam, Sure => False);
2443 -- Here we are checking for explicit overlap of one variable
2444 -- by another, and if we find this then mark the overlapped
2445 -- variable as also being volatile to prevent unwanted
2446 -- optimizations. This is a significant pessimization so
2447 -- avoid it when there is an offset, i.e. when the object
2448 -- is composite; they cannot be optimized easily anyway.
2451 and then Is_Object (O_Ent)
2454 Set_Treat_As_Volatile (O_Ent);
2457 -- Legality checks on the address clause for initialized
2458 -- objects is deferred until the freeze point, because
2459 -- a subsequent pragma might indicate that the object is
2460 -- imported and thus not initialized.
2462 Set_Has_Delayed_Freeze (U_Ent);
2464 -- If an initialization call has been generated for this
2465 -- object, it needs to be deferred to after the freeze node
2466 -- we have just now added, otherwise GIGI will see a
2467 -- reference to the variable (as actual to the IP call)
2468 -- before its definition.
2471 Init_Call : constant Node_Id := Find_Init_Call (U_Ent, N);
2473 if Present (Init_Call) then
2475 Append_Freeze_Action (U_Ent, Init_Call);
2479 if Is_Exported (U_Ent) then
2481 ("& cannot be exported if an address clause is given",
2484 ("\define and export a variable " &
2485 "that holds its address instead",
2489 -- Entity has delayed freeze, so we will generate an
2490 -- alignment check at the freeze point unless suppressed.
2492 if not Range_Checks_Suppressed (U_Ent)
2493 and then not Alignment_Checks_Suppressed (U_Ent)
2495 Set_Check_Address_Alignment (N);
2498 -- Kill the size check code, since we are not allocating
2499 -- the variable, it is somewhere else.
2501 Kill_Size_Check_Code (U_Ent);
2503 -- If the address clause is of the form:
2505 -- for Y'Address use X'Address
2509 -- Const : constant Address := X'Address;
2511 -- for Y'Address use Const;
2513 -- then we make an entry in the table for checking the size
2514 -- and alignment of the overlaying variable. We defer this
2515 -- check till after code generation to take full advantage
2516 -- of the annotation done by the back end. This entry is
2517 -- only made if the address clause comes from source.
2519 -- If the entity has a generic type, the check will be
2520 -- performed in the instance if the actual type justifies
2521 -- it, and we do not insert the clause in the table to
2522 -- prevent spurious warnings.
2524 if Address_Clause_Overlay_Warnings
2525 and then Comes_From_Source (N)
2526 and then Present (O_Ent)
2527 and then Is_Object (O_Ent)
2529 if not Is_Generic_Type (Etype (U_Ent)) then
2530 Address_Clause_Checks.Append ((N, U_Ent, O_Ent, Off));
2533 -- If variable overlays a constant view, and we are
2534 -- warning on overlays, then mark the variable as
2535 -- overlaying a constant (we will give warnings later
2536 -- if this variable is assigned).
2538 if Is_Constant_Object (O_Ent)
2539 and then Ekind (U_Ent) = E_Variable
2541 Set_Overlays_Constant (U_Ent);
2546 -- Not a valid entity for an address clause
2549 Error_Msg_N ("address cannot be given for &", Nam);
2557 -- Alignment attribute definition clause
2559 when Attribute_Alignment => Alignment : declare
2560 Align : constant Uint := Get_Alignment_Value (Expr);
2561 Max_Align : constant Uint := UI_From_Int (Maximum_Alignment);
2566 if not Is_Type (U_Ent)
2567 and then Ekind (U_Ent) /= E_Variable
2568 and then Ekind (U_Ent) /= E_Constant
2570 Error_Msg_N ("alignment cannot be given for &", Nam);
2572 elsif Duplicate_Clause then
2575 elsif Align /= No_Uint then
2576 Set_Has_Alignment_Clause (U_Ent);
2578 -- Tagged type case, check for attempt to set alignment to a
2579 -- value greater than Max_Align, and reset if so.
2581 if Is_Tagged_Type (U_Ent) and then Align > Max_Align then
2583 ("?alignment for & set to Maximum_Aligment", Nam);
2584 Set_Alignment (U_Ent, Max_Align);
2589 Set_Alignment (U_Ent, Align);
2592 -- For an array type, U_Ent is the first subtype. In that case,
2593 -- also set the alignment of the anonymous base type so that
2594 -- other subtypes (such as the itypes for aggregates of the
2595 -- type) also receive the expected alignment.
2597 if Is_Array_Type (U_Ent) then
2598 Set_Alignment (Base_Type (U_Ent), Align);
2607 -- Bit_Order attribute definition clause
2609 when Attribute_Bit_Order => Bit_Order : declare
2611 if not Is_Record_Type (U_Ent) then
2613 ("Bit_Order can only be defined for record type", Nam);
2615 elsif Duplicate_Clause then
2619 Analyze_And_Resolve (Expr, RTE (RE_Bit_Order));
2621 if Etype (Expr) = Any_Type then
2624 elsif not Is_Static_Expression (Expr) then
2625 Flag_Non_Static_Expr
2626 ("Bit_Order requires static expression!", Expr);
2629 if (Expr_Value (Expr) = 0) /= Bytes_Big_Endian then
2630 Set_Reverse_Bit_Order (U_Ent, True);
2636 --------------------
2637 -- Component_Size --
2638 --------------------
2640 -- Component_Size attribute definition clause
2642 when Attribute_Component_Size => Component_Size_Case : declare
2643 Csize : constant Uint := Static_Integer (Expr);
2647 New_Ctyp : Entity_Id;
2651 if not Is_Array_Type (U_Ent) then
2652 Error_Msg_N ("component size requires array type", Nam);
2656 Btype := Base_Type (U_Ent);
2657 Ctyp := Component_Type (Btype);
2659 if Duplicate_Clause then
2662 elsif Rep_Item_Too_Early (Btype, N) then
2665 elsif Csize /= No_Uint then
2666 Check_Size (Expr, Ctyp, Csize, Biased);
2668 -- For the biased case, build a declaration for a subtype that
2669 -- will be used to represent the biased subtype that reflects
2670 -- the biased representation of components. We need the subtype
2671 -- to get proper conversions on referencing elements of the
2672 -- array. Note: component size clauses are ignored in VM mode.
2674 if VM_Target = No_VM then
2677 Make_Defining_Identifier (Loc,
2679 New_External_Name (Chars (U_Ent), 'C', 0, 'T'));
2682 Make_Subtype_Declaration (Loc,
2683 Defining_Identifier => New_Ctyp,
2684 Subtype_Indication =>
2685 New_Occurrence_Of (Component_Type (Btype), Loc));
2687 Set_Parent (Decl, N);
2688 Analyze (Decl, Suppress => All_Checks);
2690 Set_Has_Delayed_Freeze (New_Ctyp, False);
2691 Set_Esize (New_Ctyp, Csize);
2692 Set_RM_Size (New_Ctyp, Csize);
2693 Init_Alignment (New_Ctyp);
2694 Set_Is_Itype (New_Ctyp, True);
2695 Set_Associated_Node_For_Itype (New_Ctyp, U_Ent);
2697 Set_Component_Type (Btype, New_Ctyp);
2698 Set_Biased (New_Ctyp, N, "component size clause");
2701 Set_Component_Size (Btype, Csize);
2703 -- For VM case, we ignore component size clauses
2706 -- Give a warning unless we are in GNAT mode, in which case
2707 -- the warning is suppressed since it is not useful.
2709 if not GNAT_Mode then
2711 ("?component size ignored in this configuration", N);
2715 -- Deal with warning on overridden size
2717 if Warn_On_Overridden_Size
2718 and then Has_Size_Clause (Ctyp)
2719 and then RM_Size (Ctyp) /= Csize
2722 ("?component size overrides size clause for&",
2726 Set_Has_Component_Size_Clause (Btype, True);
2727 Set_Has_Non_Standard_Rep (Btype, True);
2729 end Component_Size_Case;
2731 -----------------------
2732 -- Constant_Indexing --
2733 -----------------------
2735 when Attribute_Constant_Indexing =>
2736 Check_Indexing_Functions;
2738 ----------------------
2739 -- Default_Iterator --
2740 ----------------------
2742 when Attribute_Default_Iterator => Default_Iterator : declare
2746 if not Is_Tagged_Type (U_Ent) then
2748 ("aspect Default_Iterator applies to tagged type", Nam);
2751 Check_Iterator_Functions;
2755 if not Is_Entity_Name (Expr)
2756 or else Ekind (Entity (Expr)) /= E_Function
2758 Error_Msg_N ("aspect Iterator must be a function", Expr);
2760 Func := Entity (Expr);
2763 if No (First_Formal (Func))
2764 or else Etype (First_Formal (Func)) /= U_Ent
2767 ("Default Iterator must be a primitive of&", Func, U_Ent);
2769 end Default_Iterator;
2775 when Attribute_External_Tag => External_Tag :
2777 if not Is_Tagged_Type (U_Ent) then
2778 Error_Msg_N ("should be a tagged type", Nam);
2781 if Duplicate_Clause then
2785 Analyze_And_Resolve (Expr, Standard_String);
2787 if not Is_Static_Expression (Expr) then
2788 Flag_Non_Static_Expr
2789 ("static string required for tag name!", Nam);
2792 if VM_Target = No_VM then
2793 Set_Has_External_Tag_Rep_Clause (U_Ent);
2795 Error_Msg_Name_1 := Attr;
2797 ("% attribute unsupported in this configuration", Nam);
2800 if not Is_Library_Level_Entity (U_Ent) then
2802 ("?non-unique external tag supplied for &", N, U_Ent);
2804 ("?\same external tag applies to all subprogram calls", N);
2806 ("?\corresponding internal tag cannot be obtained", N);
2811 --------------------------
2812 -- Implicit_Dereference --
2813 --------------------------
2815 when Attribute_Implicit_Dereference =>
2817 -- Legality checks already performed at the point of
2818 -- the type declaration, aspect is not delayed.
2826 when Attribute_Input =>
2827 Analyze_Stream_TSS_Definition (TSS_Stream_Input);
2828 Set_Has_Specified_Stream_Input (Ent);
2830 ----------------------
2831 -- Iterator_Element --
2832 ----------------------
2834 when Attribute_Iterator_Element =>
2837 if not Is_Entity_Name (Expr)
2838 or else not Is_Type (Entity (Expr))
2840 Error_Msg_N ("aspect Iterator_Element must be a type", Expr);
2847 -- Machine radix attribute definition clause
2849 when Attribute_Machine_Radix => Machine_Radix : declare
2850 Radix : constant Uint := Static_Integer (Expr);
2853 if not Is_Decimal_Fixed_Point_Type (U_Ent) then
2854 Error_Msg_N ("decimal fixed-point type expected for &", Nam);
2856 elsif Duplicate_Clause then
2859 elsif Radix /= No_Uint then
2860 Set_Has_Machine_Radix_Clause (U_Ent);
2861 Set_Has_Non_Standard_Rep (Base_Type (U_Ent));
2865 elsif Radix = 10 then
2866 Set_Machine_Radix_10 (U_Ent);
2868 Error_Msg_N ("machine radix value must be 2 or 10", Expr);
2877 -- Object_Size attribute definition clause
2879 when Attribute_Object_Size => Object_Size : declare
2880 Size : constant Uint := Static_Integer (Expr);
2883 pragma Warnings (Off, Biased);
2886 if not Is_Type (U_Ent) then
2887 Error_Msg_N ("Object_Size cannot be given for &", Nam);
2889 elsif Duplicate_Clause then
2893 Check_Size (Expr, U_Ent, Size, Biased);
2901 UI_Mod (Size, 64) /= 0
2904 ("Object_Size must be 8, 16, 32, or multiple of 64",
2908 Set_Esize (U_Ent, Size);
2909 Set_Has_Object_Size_Clause (U_Ent);
2910 Alignment_Check_For_Size_Change (U_Ent, Size);
2918 when Attribute_Output =>
2919 Analyze_Stream_TSS_Definition (TSS_Stream_Output);
2920 Set_Has_Specified_Stream_Output (Ent);
2926 when Attribute_Read =>
2927 Analyze_Stream_TSS_Definition (TSS_Stream_Read);
2928 Set_Has_Specified_Stream_Read (Ent);
2934 -- Size attribute definition clause
2936 when Attribute_Size => Size : declare
2937 Size : constant Uint := Static_Integer (Expr);
2944 if Duplicate_Clause then
2947 elsif not Is_Type (U_Ent)
2948 and then Ekind (U_Ent) /= E_Variable
2949 and then Ekind (U_Ent) /= E_Constant
2951 Error_Msg_N ("size cannot be given for &", Nam);
2953 elsif Is_Array_Type (U_Ent)
2954 and then not Is_Constrained (U_Ent)
2957 ("size cannot be given for unconstrained array", Nam);
2959 elsif Size /= No_Uint then
2960 if VM_Target /= No_VM and then not GNAT_Mode then
2962 -- Size clause is not handled properly on VM targets.
2963 -- Display a warning unless we are in GNAT mode, in which
2964 -- case this is useless.
2967 ("?size clauses are ignored in this configuration", N);
2970 if Is_Type (U_Ent) then
2973 Etyp := Etype (U_Ent);
2976 -- Check size, note that Gigi is in charge of checking that the
2977 -- size of an array or record type is OK. Also we do not check
2978 -- the size in the ordinary fixed-point case, since it is too
2979 -- early to do so (there may be subsequent small clause that
2980 -- affects the size). We can check the size if a small clause
2981 -- has already been given.
2983 if not Is_Ordinary_Fixed_Point_Type (U_Ent)
2984 or else Has_Small_Clause (U_Ent)
2986 Check_Size (Expr, Etyp, Size, Biased);
2987 Set_Biased (U_Ent, N, "size clause", Biased);
2990 -- For types set RM_Size and Esize if possible
2992 if Is_Type (U_Ent) then
2993 Set_RM_Size (U_Ent, Size);
2995 -- For elementary types, increase Object_Size to power of 2,
2996 -- but not less than a storage unit in any case (normally
2997 -- this means it will be byte addressable).
2999 -- For all other types, nothing else to do, we leave Esize
3000 -- (object size) unset, the back end will set it from the
3001 -- size and alignment in an appropriate manner.
3003 -- In both cases, we check whether the alignment must be
3004 -- reset in the wake of the size change.
3006 if Is_Elementary_Type (U_Ent) then
3007 if Size <= System_Storage_Unit then
3008 Init_Esize (U_Ent, System_Storage_Unit);
3009 elsif Size <= 16 then
3010 Init_Esize (U_Ent, 16);
3011 elsif Size <= 32 then
3012 Init_Esize (U_Ent, 32);
3014 Set_Esize (U_Ent, (Size + 63) / 64 * 64);
3017 Alignment_Check_For_Size_Change (U_Ent, Esize (U_Ent));
3019 Alignment_Check_For_Size_Change (U_Ent, Size);
3022 -- For objects, set Esize only
3025 if Is_Elementary_Type (Etyp) then
3026 if Size /= System_Storage_Unit
3028 Size /= System_Storage_Unit * 2
3030 Size /= System_Storage_Unit * 4
3032 Size /= System_Storage_Unit * 8
3034 Error_Msg_Uint_1 := UI_From_Int (System_Storage_Unit);
3035 Error_Msg_Uint_2 := Error_Msg_Uint_1 * 8;
3037 ("size for primitive object must be a power of 2"
3038 & " in the range ^-^", N);
3042 Set_Esize (U_Ent, Size);
3045 Set_Has_Size_Clause (U_Ent);
3053 -- Small attribute definition clause
3055 when Attribute_Small => Small : declare
3056 Implicit_Base : constant Entity_Id := Base_Type (U_Ent);
3060 Analyze_And_Resolve (Expr, Any_Real);
3062 if Etype (Expr) = Any_Type then
3065 elsif not Is_Static_Expression (Expr) then
3066 Flag_Non_Static_Expr
3067 ("small requires static expression!", Expr);
3071 Small := Expr_Value_R (Expr);
3073 if Small <= Ureal_0 then
3074 Error_Msg_N ("small value must be greater than zero", Expr);
3080 if not Is_Ordinary_Fixed_Point_Type (U_Ent) then
3082 ("small requires an ordinary fixed point type", Nam);
3084 elsif Has_Small_Clause (U_Ent) then
3085 Error_Msg_N ("small already given for &", Nam);
3087 elsif Small > Delta_Value (U_Ent) then
3089 ("small value must not be greater then delta value", Nam);
3092 Set_Small_Value (U_Ent, Small);
3093 Set_Small_Value (Implicit_Base, Small);
3094 Set_Has_Small_Clause (U_Ent);
3095 Set_Has_Small_Clause (Implicit_Base);
3096 Set_Has_Non_Standard_Rep (Implicit_Base);
3104 -- Storage_Pool attribute definition clause
3106 when Attribute_Storage_Pool => Storage_Pool : declare
3111 if Ekind (U_Ent) = E_Access_Subprogram_Type then
3113 ("storage pool cannot be given for access-to-subprogram type",
3118 Ekind_In (U_Ent, E_Access_Type, E_General_Access_Type)
3121 ("storage pool can only be given for access types", Nam);
3124 elsif Is_Derived_Type (U_Ent) then
3126 ("storage pool cannot be given for a derived access type",
3129 elsif Duplicate_Clause then
3132 elsif Present (Associated_Storage_Pool (U_Ent)) then
3133 Error_Msg_N ("storage pool already given for &", Nam);
3138 (Expr, Class_Wide_Type (RTE (RE_Root_Storage_Pool)));
3140 if not Denotes_Variable (Expr) then
3141 Error_Msg_N ("storage pool must be a variable", Expr);
3145 if Nkind (Expr) = N_Type_Conversion then
3146 T := Etype (Expression (Expr));
3151 -- The Stack_Bounded_Pool is used internally for implementing
3152 -- access types with a Storage_Size. Since it only work properly
3153 -- when used on one specific type, we need to check that it is not
3154 -- hijacked improperly:
3156 -- type T is access Integer;
3157 -- for T'Storage_Size use n;
3158 -- type Q is access Float;
3159 -- for Q'Storage_Size use T'Storage_Size; -- incorrect
3161 if RTE_Available (RE_Stack_Bounded_Pool)
3162 and then Base_Type (T) = RTE (RE_Stack_Bounded_Pool)
3164 Error_Msg_N ("non-shareable internal Pool", Expr);
3168 -- If the argument is a name that is not an entity name, then
3169 -- we construct a renaming operation to define an entity of
3170 -- type storage pool.
3172 if not Is_Entity_Name (Expr)
3173 and then Is_Object_Reference (Expr)
3175 Pool := Make_Temporary (Loc, 'P', Expr);
3178 Rnode : constant Node_Id :=
3179 Make_Object_Renaming_Declaration (Loc,
3180 Defining_Identifier => Pool,
3182 New_Occurrence_Of (Etype (Expr), Loc),
3186 Insert_Before (N, Rnode);
3188 Set_Associated_Storage_Pool (U_Ent, Pool);
3191 elsif Is_Entity_Name (Expr) then
3192 Pool := Entity (Expr);
3194 -- If pool is a renamed object, get original one. This can
3195 -- happen with an explicit renaming, and within instances.
3197 while Present (Renamed_Object (Pool))
3198 and then Is_Entity_Name (Renamed_Object (Pool))
3200 Pool := Entity (Renamed_Object (Pool));
3203 if Present (Renamed_Object (Pool))
3204 and then Nkind (Renamed_Object (Pool)) = N_Type_Conversion
3205 and then Is_Entity_Name (Expression (Renamed_Object (Pool)))
3207 Pool := Entity (Expression (Renamed_Object (Pool)));
3210 Set_Associated_Storage_Pool (U_Ent, Pool);
3212 elsif Nkind (Expr) = N_Type_Conversion
3213 and then Is_Entity_Name (Expression (Expr))
3214 and then Nkind (Original_Node (Expr)) = N_Attribute_Reference
3216 Pool := Entity (Expression (Expr));
3217 Set_Associated_Storage_Pool (U_Ent, Pool);
3220 Error_Msg_N ("incorrect reference to a Storage Pool", Expr);
3229 -- Storage_Size attribute definition clause
3231 when Attribute_Storage_Size => Storage_Size : declare
3232 Btype : constant Entity_Id := Base_Type (U_Ent);
3236 if Is_Task_Type (U_Ent) then
3237 Check_Restriction (No_Obsolescent_Features, N);
3239 if Warn_On_Obsolescent_Feature then
3241 ("storage size clause for task is an " &
3242 "obsolescent feature (RM J.9)?", N);
3243 Error_Msg_N ("\use Storage_Size pragma instead?", N);
3249 if not Is_Access_Type (U_Ent)
3250 and then Ekind (U_Ent) /= E_Task_Type
3252 Error_Msg_N ("storage size cannot be given for &", Nam);
3254 elsif Is_Access_Type (U_Ent) and Is_Derived_Type (U_Ent) then
3256 ("storage size cannot be given for a derived access type",
3259 elsif Duplicate_Clause then
3263 Analyze_And_Resolve (Expr, Any_Integer);
3265 if Is_Access_Type (U_Ent) then
3266 if Present (Associated_Storage_Pool (U_Ent)) then
3267 Error_Msg_N ("storage pool already given for &", Nam);
3271 if Is_OK_Static_Expression (Expr)
3272 and then Expr_Value (Expr) = 0
3274 Set_No_Pool_Assigned (Btype);
3277 else -- Is_Task_Type (U_Ent)
3278 Sprag := Get_Rep_Pragma (Btype, Name_Storage_Size);
3280 if Present (Sprag) then
3281 Error_Msg_Sloc := Sloc (Sprag);
3283 ("Storage_Size already specified#", Nam);
3288 Set_Has_Storage_Size_Clause (Btype);
3296 when Attribute_Stream_Size => Stream_Size : declare
3297 Size : constant Uint := Static_Integer (Expr);
3300 if Ada_Version <= Ada_95 then
3301 Check_Restriction (No_Implementation_Attributes, N);
3304 if Duplicate_Clause then
3307 elsif Is_Elementary_Type (U_Ent) then
3308 if Size /= System_Storage_Unit
3310 Size /= System_Storage_Unit * 2
3312 Size /= System_Storage_Unit * 4
3314 Size /= System_Storage_Unit * 8
3316 Error_Msg_Uint_1 := UI_From_Int (System_Storage_Unit);
3318 ("stream size for elementary type must be a"
3319 & " power of 2 and at least ^", N);
3321 elsif RM_Size (U_Ent) > Size then
3322 Error_Msg_Uint_1 := RM_Size (U_Ent);
3324 ("stream size for elementary type must be a"
3325 & " power of 2 and at least ^", N);
3328 Set_Has_Stream_Size_Clause (U_Ent);
3331 Error_Msg_N ("Stream_Size cannot be given for &", Nam);
3339 -- Value_Size attribute definition clause
3341 when Attribute_Value_Size => Value_Size : declare
3342 Size : constant Uint := Static_Integer (Expr);
3346 if not Is_Type (U_Ent) then
3347 Error_Msg_N ("Value_Size cannot be given for &", Nam);
3349 elsif Duplicate_Clause then
3352 elsif Is_Array_Type (U_Ent)
3353 and then not Is_Constrained (U_Ent)
3356 ("Value_Size cannot be given for unconstrained array", Nam);
3359 if Is_Elementary_Type (U_Ent) then
3360 Check_Size (Expr, U_Ent, Size, Biased);
3361 Set_Biased (U_Ent, N, "value size clause", Biased);
3364 Set_RM_Size (U_Ent, Size);
3368 -----------------------
3369 -- Variable_Indexing --
3370 -----------------------
3372 when Attribute_Variable_Indexing =>
3373 Check_Indexing_Functions;
3379 when Attribute_Write =>
3380 Analyze_Stream_TSS_Definition (TSS_Stream_Write);
3381 Set_Has_Specified_Stream_Write (Ent);
3383 -- All other attributes cannot be set
3387 ("attribute& cannot be set with definition clause", N);
3390 -- The test for the type being frozen must be performed after any
3391 -- expression the clause has been analyzed since the expression itself
3392 -- might cause freezing that makes the clause illegal.
3394 if Rep_Item_Too_Late (U_Ent, N, FOnly) then
3397 end Analyze_Attribute_Definition_Clause;
3399 ----------------------------
3400 -- Analyze_Code_Statement --
3401 ----------------------------
3403 procedure Analyze_Code_Statement (N : Node_Id) is
3404 HSS : constant Node_Id := Parent (N);
3405 SBody : constant Node_Id := Parent (HSS);
3406 Subp : constant Entity_Id := Current_Scope;
3413 -- Analyze and check we get right type, note that this implements the
3414 -- requirement (RM 13.8(1)) that Machine_Code be with'ed, since that
3415 -- is the only way that Asm_Insn could possibly be visible.
3417 Analyze_And_Resolve (Expression (N));
3419 if Etype (Expression (N)) = Any_Type then
3421 elsif Etype (Expression (N)) /= RTE (RE_Asm_Insn) then
3422 Error_Msg_N ("incorrect type for code statement", N);
3426 Check_Code_Statement (N);
3428 -- Make sure we appear in the handled statement sequence of a
3429 -- subprogram (RM 13.8(3)).
3431 if Nkind (HSS) /= N_Handled_Sequence_Of_Statements
3432 or else Nkind (SBody) /= N_Subprogram_Body
3435 ("code statement can only appear in body of subprogram", N);
3439 -- Do remaining checks (RM 13.8(3)) if not already done
3441 if not Is_Machine_Code_Subprogram (Subp) then
3442 Set_Is_Machine_Code_Subprogram (Subp);
3444 -- No exception handlers allowed
3446 if Present (Exception_Handlers (HSS)) then
3448 ("exception handlers not permitted in machine code subprogram",
3449 First (Exception_Handlers (HSS)));
3452 -- No declarations other than use clauses and pragmas (we allow
3453 -- certain internally generated declarations as well).
3455 Decl := First (Declarations (SBody));
3456 while Present (Decl) loop
3457 DeclO := Original_Node (Decl);
3458 if Comes_From_Source (DeclO)
3459 and not Nkind_In (DeclO, N_Pragma,
3460 N_Use_Package_Clause,
3462 N_Implicit_Label_Declaration)
3465 ("this declaration not allowed in machine code subprogram",
3472 -- No statements other than code statements, pragmas, and labels.
3473 -- Again we allow certain internally generated statements.
3475 -- In Ada 2012, qualified expressions are names, and the code
3476 -- statement is initially parsed as a procedure call.
3478 Stmt := First (Statements (HSS));
3479 while Present (Stmt) loop
3480 StmtO := Original_Node (Stmt);
3482 -- A procedure call transformed into a code statement is OK.
3484 if Ada_Version >= Ada_2012
3485 and then Nkind (StmtO) = N_Procedure_Call_Statement
3486 and then Nkind (Name (StmtO)) = N_Qualified_Expression
3490 elsif Comes_From_Source (StmtO)
3491 and then not Nkind_In (StmtO, N_Pragma,
3496 ("this statement is not allowed in machine code subprogram",
3503 end Analyze_Code_Statement;
3505 -----------------------------------------------
3506 -- Analyze_Enumeration_Representation_Clause --
3507 -----------------------------------------------
3509 procedure Analyze_Enumeration_Representation_Clause (N : Node_Id) is
3510 Ident : constant Node_Id := Identifier (N);
3511 Aggr : constant Node_Id := Array_Aggregate (N);
3512 Enumtype : Entity_Id;
3519 Err : Boolean := False;
3520 -- Set True to avoid cascade errors and crashes on incorrect source code
3522 Lo : constant Uint := Expr_Value (Type_Low_Bound (Universal_Integer));
3523 Hi : constant Uint := Expr_Value (Type_High_Bound (Universal_Integer));
3524 -- Allowed range of universal integer (= allowed range of enum lit vals)
3528 -- Minimum and maximum values of entries
3531 -- Pointer to node for literal providing max value
3534 if Ignore_Rep_Clauses then
3538 -- First some basic error checks
3541 Enumtype := Entity (Ident);
3543 if Enumtype = Any_Type
3544 or else Rep_Item_Too_Early (Enumtype, N)
3548 Enumtype := Underlying_Type (Enumtype);
3551 if not Is_Enumeration_Type (Enumtype) then
3553 ("enumeration type required, found}",
3554 Ident, First_Subtype (Enumtype));
3558 -- Ignore rep clause on generic actual type. This will already have
3559 -- been flagged on the template as an error, and this is the safest
3560 -- way to ensure we don't get a junk cascaded message in the instance.
3562 if Is_Generic_Actual_Type (Enumtype) then
3565 -- Type must be in current scope
3567 elsif Scope (Enumtype) /= Current_Scope then
3568 Error_Msg_N ("type must be declared in this scope", Ident);
3571 -- Type must be a first subtype
3573 elsif not Is_First_Subtype (Enumtype) then
3574 Error_Msg_N ("cannot give enumeration rep clause for subtype", N);
3577 -- Ignore duplicate rep clause
3579 elsif Has_Enumeration_Rep_Clause (Enumtype) then
3580 Error_Msg_N ("duplicate enumeration rep clause ignored", N);
3583 -- Don't allow rep clause for standard [wide_[wide_]]character
3585 elsif Is_Standard_Character_Type (Enumtype) then
3586 Error_Msg_N ("enumeration rep clause not allowed for this type", N);
3589 -- Check that the expression is a proper aggregate (no parentheses)
3591 elsif Paren_Count (Aggr) /= 0 then
3593 ("extra parentheses surrounding aggregate not allowed",
3597 -- All tests passed, so set rep clause in place
3600 Set_Has_Enumeration_Rep_Clause (Enumtype);
3601 Set_Has_Enumeration_Rep_Clause (Base_Type (Enumtype));
3604 -- Now we process the aggregate. Note that we don't use the normal
3605 -- aggregate code for this purpose, because we don't want any of the
3606 -- normal expansion activities, and a number of special semantic
3607 -- rules apply (including the component type being any integer type)
3609 Elit := First_Literal (Enumtype);
3611 -- First the positional entries if any
3613 if Present (Expressions (Aggr)) then
3614 Expr := First (Expressions (Aggr));
3615 while Present (Expr) loop
3617 Error_Msg_N ("too many entries in aggregate", Expr);
3621 Val := Static_Integer (Expr);
3623 -- Err signals that we found some incorrect entries processing
3624 -- the list. The final checks for completeness and ordering are
3625 -- skipped in this case.
3627 if Val = No_Uint then
3629 elsif Val < Lo or else Hi < Val then
3630 Error_Msg_N ("value outside permitted range", Expr);
3634 Set_Enumeration_Rep (Elit, Val);
3635 Set_Enumeration_Rep_Expr (Elit, Expr);
3641 -- Now process the named entries if present
3643 if Present (Component_Associations (Aggr)) then
3644 Assoc := First (Component_Associations (Aggr));
3645 while Present (Assoc) loop
3646 Choice := First (Choices (Assoc));
3648 if Present (Next (Choice)) then
3650 ("multiple choice not allowed here", Next (Choice));
3654 if Nkind (Choice) = N_Others_Choice then
3655 Error_Msg_N ("others choice not allowed here", Choice);
3658 elsif Nkind (Choice) = N_Range then
3660 -- ??? should allow zero/one element range here
3662 Error_Msg_N ("range not allowed here", Choice);
3666 Analyze_And_Resolve (Choice, Enumtype);
3668 if Error_Posted (Choice) then
3673 if Is_Entity_Name (Choice)
3674 and then Is_Type (Entity (Choice))
3676 Error_Msg_N ("subtype name not allowed here", Choice);
3679 -- ??? should allow static subtype with zero/one entry
3681 elsif Etype (Choice) = Base_Type (Enumtype) then
3682 if not Is_Static_Expression (Choice) then
3683 Flag_Non_Static_Expr
3684 ("non-static expression used for choice!", Choice);
3688 Elit := Expr_Value_E (Choice);
3690 if Present (Enumeration_Rep_Expr (Elit)) then
3692 Sloc (Enumeration_Rep_Expr (Elit));
3694 ("representation for& previously given#",
3699 Set_Enumeration_Rep_Expr (Elit, Expression (Assoc));
3701 Expr := Expression (Assoc);
3702 Val := Static_Integer (Expr);
3704 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);
3722 -- Aggregate is fully processed. Now we check that a full set of
3723 -- representations was given, and that they are in range and in order.
3724 -- These checks are only done if no other errors occurred.
3730 Elit := First_Literal (Enumtype);
3731 while Present (Elit) loop
3732 if No (Enumeration_Rep_Expr (Elit)) then
3733 Error_Msg_NE ("missing representation for&!", N, Elit);
3736 Val := Enumeration_Rep (Elit);
3738 if Min = No_Uint then
3742 if Val /= No_Uint then
3743 if Max /= No_Uint and then Val <= Max then
3745 ("enumeration value for& not ordered!",
3746 Enumeration_Rep_Expr (Elit), Elit);
3749 Max_Node := Enumeration_Rep_Expr (Elit);
3753 -- If there is at least one literal whose representation is not
3754 -- equal to the Pos value, then note that this enumeration type
3755 -- has a non-standard representation.
3757 if Val /= Enumeration_Pos (Elit) then
3758 Set_Has_Non_Standard_Rep (Base_Type (Enumtype));
3765 -- Now set proper size information
3768 Minsize : Uint := UI_From_Int (Minimum_Size (Enumtype));
3771 if Has_Size_Clause (Enumtype) then
3773 -- All OK, if size is OK now
3775 if RM_Size (Enumtype) >= Minsize then
3779 -- Try if we can get by with biasing
3782 UI_From_Int (Minimum_Size (Enumtype, Biased => True));
3784 -- Error message if even biasing does not work
3786 if RM_Size (Enumtype) < Minsize then
3787 Error_Msg_Uint_1 := RM_Size (Enumtype);
3788 Error_Msg_Uint_2 := Max;
3790 ("previously given size (^) is too small "
3791 & "for this value (^)", Max_Node);
3793 -- If biasing worked, indicate that we now have biased rep
3797 (Enumtype, Size_Clause (Enumtype), "size clause");
3802 Set_RM_Size (Enumtype, Minsize);
3803 Set_Enum_Esize (Enumtype);
3806 Set_RM_Size (Base_Type (Enumtype), RM_Size (Enumtype));
3807 Set_Esize (Base_Type (Enumtype), Esize (Enumtype));
3808 Set_Alignment (Base_Type (Enumtype), Alignment (Enumtype));
3812 -- We repeat the too late test in case it froze itself!
3814 if Rep_Item_Too_Late (Enumtype, N) then
3817 end Analyze_Enumeration_Representation_Clause;
3819 ----------------------------
3820 -- Analyze_Free_Statement --
3821 ----------------------------
3823 procedure Analyze_Free_Statement (N : Node_Id) is
3825 Analyze (Expression (N));
3826 end Analyze_Free_Statement;
3828 ---------------------------
3829 -- Analyze_Freeze_Entity --
3830 ---------------------------
3832 procedure Analyze_Freeze_Entity (N : Node_Id) is
3833 E : constant Entity_Id := Entity (N);
3836 -- Remember that we are processing a freezing entity. Required to
3837 -- ensure correct decoration of internal entities associated with
3838 -- interfaces (see New_Overloaded_Entity).
3840 Inside_Freezing_Actions := Inside_Freezing_Actions + 1;
3842 -- For tagged types covering interfaces add internal entities that link
3843 -- the primitives of the interfaces with the primitives that cover them.
3844 -- Note: These entities were originally generated only when generating
3845 -- code because their main purpose was to provide support to initialize
3846 -- the secondary dispatch tables. They are now generated also when
3847 -- compiling with no code generation to provide ASIS the relationship
3848 -- between interface primitives and tagged type primitives. They are
3849 -- also used to locate primitives covering interfaces when processing
3850 -- generics (see Derive_Subprograms).
3852 if Ada_Version >= Ada_2005
3853 and then Ekind (E) = E_Record_Type
3854 and then Is_Tagged_Type (E)
3855 and then not Is_Interface (E)
3856 and then Has_Interfaces (E)
3858 -- This would be a good common place to call the routine that checks
3859 -- overriding of interface primitives (and thus factorize calls to
3860 -- Check_Abstract_Overriding located at different contexts in the
3861 -- compiler). However, this is not possible because it causes
3862 -- spurious errors in case of late overriding.
3864 Add_Internal_Interface_Entities (E);
3869 if Ekind (E) = E_Record_Type
3870 and then Is_CPP_Class (E)
3871 and then Is_Tagged_Type (E)
3872 and then Tagged_Type_Expansion
3873 and then Expander_Active
3875 if CPP_Num_Prims (E) = 0 then
3877 -- If the CPP type has user defined components then it must import
3878 -- primitives from C++. This is required because if the C++ class
3879 -- has no primitives then the C++ compiler does not added the _tag
3880 -- component to the type.
3882 pragma Assert (Chars (First_Entity (E)) = Name_uTag);
3884 if First_Entity (E) /= Last_Entity (E) then
3886 ("?'C'P'P type must import at least one primitive from C++",
3891 -- Check that all its primitives are abstract or imported from C++.
3892 -- Check also availability of the C++ constructor.
3895 Has_Constructors : constant Boolean := Has_CPP_Constructors (E);
3897 Error_Reported : Boolean := False;
3901 Elmt := First_Elmt (Primitive_Operations (E));
3902 while Present (Elmt) loop
3903 Prim := Node (Elmt);
3905 if Comes_From_Source (Prim) then
3906 if Is_Abstract_Subprogram (Prim) then
3909 elsif not Is_Imported (Prim)
3910 or else Convention (Prim) /= Convention_CPP
3913 ("?primitives of 'C'P'P types must be imported from C++"
3914 & " or abstract", Prim);
3916 elsif not Has_Constructors
3917 and then not Error_Reported
3919 Error_Msg_Name_1 := Chars (E);
3921 ("?'C'P'P constructor required for type %", Prim);
3922 Error_Reported := True;
3931 Inside_Freezing_Actions := Inside_Freezing_Actions - 1;
3933 -- If we have a type with predicates, build predicate function
3935 if Is_Type (E) and then Has_Predicates (E) then
3936 Build_Predicate_Function (E, N);
3939 -- If type has delayed aspects, this is where we do the preanalysis at
3940 -- the freeze point, as part of the consistent visibility check. Note
3941 -- that this must be done after calling Build_Predicate_Function or
3942 -- Build_Invariant_Procedure since these subprograms fix occurrences of
3943 -- the subtype name in the saved expression so that they will not cause
3944 -- trouble in the preanalysis.
3946 if Has_Delayed_Aspects (E) then
3951 -- Look for aspect specification entries for this entity
3953 Ritem := First_Rep_Item (E);
3954 while Present (Ritem) loop
3955 if Nkind (Ritem) = N_Aspect_Specification
3956 and then Entity (Ritem) = E
3957 and then Is_Delayed_Aspect (Ritem)
3958 and then Scope (E) = Current_Scope
3960 Check_Aspect_At_Freeze_Point (Ritem);
3963 Next_Rep_Item (Ritem);
3967 end Analyze_Freeze_Entity;
3969 ------------------------------------------
3970 -- Analyze_Record_Representation_Clause --
3971 ------------------------------------------
3973 -- Note: we check as much as we can here, but we can't do any checks
3974 -- based on the position values (e.g. overlap checks) until freeze time
3975 -- because especially in Ada 2005 (machine scalar mode), the processing
3976 -- for non-standard bit order can substantially change the positions.
3977 -- See procedure Check_Record_Representation_Clause (called from Freeze)
3978 -- for the remainder of this processing.
3980 procedure Analyze_Record_Representation_Clause (N : Node_Id) is
3981 Ident : constant Node_Id := Identifier (N);
3986 Hbit : Uint := Uint_0;
3990 Rectype : Entity_Id;
3992 CR_Pragma : Node_Id := Empty;
3993 -- Points to N_Pragma node if Complete_Representation pragma present
3996 if Ignore_Rep_Clauses then
4001 Rectype := Entity (Ident);
4003 if Rectype = Any_Type
4004 or else Rep_Item_Too_Early (Rectype, N)
4008 Rectype := Underlying_Type (Rectype);
4011 -- First some basic error checks
4013 if not Is_Record_Type (Rectype) then
4015 ("record type required, found}", Ident, First_Subtype (Rectype));
4018 elsif Scope (Rectype) /= Current_Scope then
4019 Error_Msg_N ("type must be declared in this scope", N);
4022 elsif not Is_First_Subtype (Rectype) then
4023 Error_Msg_N ("cannot give record rep clause for subtype", N);
4026 elsif Has_Record_Rep_Clause (Rectype) then
4027 Error_Msg_N ("duplicate record rep clause ignored", N);
4030 elsif Rep_Item_Too_Late (Rectype, N) then
4034 if Present (Mod_Clause (N)) then
4036 Loc : constant Source_Ptr := Sloc (N);
4037 M : constant Node_Id := Mod_Clause (N);
4038 P : constant List_Id := Pragmas_Before (M);
4042 pragma Warnings (Off, Mod_Val);
4045 Check_Restriction (No_Obsolescent_Features, Mod_Clause (N));
4047 if Warn_On_Obsolescent_Feature then
4049 ("mod clause is an obsolescent feature (RM J.8)?", N);
4051 ("\use alignment attribute definition clause instead?", N);
4058 -- In ASIS_Mode mode, expansion is disabled, but we must convert
4059 -- the Mod clause into an alignment clause anyway, so that the
4060 -- back-end can compute and back-annotate properly the size and
4061 -- alignment of types that may include this record.
4063 -- This seems dubious, this destroys the source tree in a manner
4064 -- not detectable by ASIS ???
4066 if Operating_Mode = Check_Semantics and then ASIS_Mode then
4068 Make_Attribute_Definition_Clause (Loc,
4069 Name => New_Reference_To (Base_Type (Rectype), Loc),
4070 Chars => Name_Alignment,
4071 Expression => Relocate_Node (Expression (M)));
4073 Set_From_At_Mod (AtM_Nod);
4074 Insert_After (N, AtM_Nod);
4075 Mod_Val := Get_Alignment_Value (Expression (AtM_Nod));
4076 Set_Mod_Clause (N, Empty);
4079 -- Get the alignment value to perform error checking
4081 Mod_Val := Get_Alignment_Value (Expression (M));
4086 -- For untagged types, clear any existing component clauses for the
4087 -- type. If the type is derived, this is what allows us to override
4088 -- a rep clause for the parent. For type extensions, the representation
4089 -- of the inherited components is inherited, so we want to keep previous
4090 -- component clauses for completeness.
4092 if not Is_Tagged_Type (Rectype) then
4093 Comp := First_Component_Or_Discriminant (Rectype);
4094 while Present (Comp) loop
4095 Set_Component_Clause (Comp, Empty);
4096 Next_Component_Or_Discriminant (Comp);
4100 -- All done if no component clauses
4102 CC := First (Component_Clauses (N));
4108 -- A representation like this applies to the base type
4110 Set_Has_Record_Rep_Clause (Base_Type (Rectype));
4111 Set_Has_Non_Standard_Rep (Base_Type (Rectype));
4112 Set_Has_Specified_Layout (Base_Type (Rectype));
4114 -- Process the component clauses
4116 while Present (CC) loop
4120 if Nkind (CC) = N_Pragma then
4123 -- The only pragma of interest is Complete_Representation
4125 if Pragma_Name (CC) = Name_Complete_Representation then
4129 -- Processing for real component clause
4132 Posit := Static_Integer (Position (CC));
4133 Fbit := Static_Integer (First_Bit (CC));
4134 Lbit := Static_Integer (Last_Bit (CC));
4137 and then Fbit /= No_Uint
4138 and then Lbit /= No_Uint
4142 ("position cannot be negative", Position (CC));
4146 ("first bit cannot be negative", First_Bit (CC));
4148 -- The Last_Bit specified in a component clause must not be
4149 -- less than the First_Bit minus one (RM-13.5.1(10)).
4151 elsif Lbit < Fbit - 1 then
4153 ("last bit cannot be less than first bit minus one",
4156 -- Values look OK, so find the corresponding record component
4157 -- Even though the syntax allows an attribute reference for
4158 -- implementation-defined components, GNAT does not allow the
4159 -- tag to get an explicit position.
4161 elsif Nkind (Component_Name (CC)) = N_Attribute_Reference then
4162 if Attribute_Name (Component_Name (CC)) = Name_Tag then
4163 Error_Msg_N ("position of tag cannot be specified", CC);
4165 Error_Msg_N ("illegal component name", CC);
4169 Comp := First_Entity (Rectype);
4170 while Present (Comp) loop
4171 exit when Chars (Comp) = Chars (Component_Name (CC));
4177 -- Maybe component of base type that is absent from
4178 -- statically constrained first subtype.
4180 Comp := First_Entity (Base_Type (Rectype));
4181 while Present (Comp) loop
4182 exit when Chars (Comp) = Chars (Component_Name (CC));
4189 ("component clause is for non-existent field", CC);
4191 -- Ada 2012 (AI05-0026): Any name that denotes a
4192 -- discriminant of an object of an unchecked union type
4193 -- shall not occur within a record_representation_clause.
4195 -- The general restriction of using record rep clauses on
4196 -- Unchecked_Union types has now been lifted. Since it is
4197 -- possible to introduce a record rep clause which mentions
4198 -- the discriminant of an Unchecked_Union in non-Ada 2012
4199 -- code, this check is applied to all versions of the
4202 elsif Ekind (Comp) = E_Discriminant
4203 and then Is_Unchecked_Union (Rectype)
4206 ("cannot reference discriminant of Unchecked_Union",
4207 Component_Name (CC));
4209 elsif Present (Component_Clause (Comp)) then
4211 -- Diagnose duplicate rep clause, or check consistency
4212 -- if this is an inherited component. In a double fault,
4213 -- there may be a duplicate inconsistent clause for an
4214 -- inherited component.
4216 if Scope (Original_Record_Component (Comp)) = Rectype
4217 or else Parent (Component_Clause (Comp)) = N
4219 Error_Msg_Sloc := Sloc (Component_Clause (Comp));
4220 Error_Msg_N ("component clause previously given#", CC);
4224 Rep1 : constant Node_Id := Component_Clause (Comp);
4226 if Intval (Position (Rep1)) /=
4227 Intval (Position (CC))
4228 or else Intval (First_Bit (Rep1)) /=
4229 Intval (First_Bit (CC))
4230 or else Intval (Last_Bit (Rep1)) /=
4231 Intval (Last_Bit (CC))
4233 Error_Msg_N ("component clause inconsistent "
4234 & "with representation of ancestor", CC);
4235 elsif Warn_On_Redundant_Constructs then
4236 Error_Msg_N ("?redundant component clause "
4237 & "for inherited component!", CC);
4242 -- Normal case where this is the first component clause we
4243 -- have seen for this entity, so set it up properly.
4246 -- Make reference for field in record rep clause and set
4247 -- appropriate entity field in the field identifier.
4250 (Comp, Component_Name (CC), Set_Ref => False);
4251 Set_Entity (Component_Name (CC), Comp);
4253 -- Update Fbit and Lbit to the actual bit number
4255 Fbit := Fbit + UI_From_Int (SSU) * Posit;
4256 Lbit := Lbit + UI_From_Int (SSU) * Posit;
4258 if Has_Size_Clause (Rectype)
4259 and then RM_Size (Rectype) <= Lbit
4262 ("bit number out of range of specified size",
4265 Set_Component_Clause (Comp, CC);
4266 Set_Component_Bit_Offset (Comp, Fbit);
4267 Set_Esize (Comp, 1 + (Lbit - Fbit));
4268 Set_Normalized_First_Bit (Comp, Fbit mod SSU);
4269 Set_Normalized_Position (Comp, Fbit / SSU);
4271 if Warn_On_Overridden_Size
4272 and then Has_Size_Clause (Etype (Comp))
4273 and then RM_Size (Etype (Comp)) /= Esize (Comp)
4276 ("?component size overrides size clause for&",
4277 Component_Name (CC), Etype (Comp));
4280 -- This information is also set in the corresponding
4281 -- component of the base type, found by accessing the
4282 -- Original_Record_Component link if it is present.
4284 Ocomp := Original_Record_Component (Comp);
4291 (Component_Name (CC),
4297 (Comp, First_Node (CC), "component clause", Biased);
4299 if Present (Ocomp) then
4300 Set_Component_Clause (Ocomp, CC);
4301 Set_Component_Bit_Offset (Ocomp, Fbit);
4302 Set_Normalized_First_Bit (Ocomp, Fbit mod SSU);
4303 Set_Normalized_Position (Ocomp, Fbit / SSU);
4304 Set_Esize (Ocomp, 1 + (Lbit - Fbit));
4306 Set_Normalized_Position_Max
4307 (Ocomp, Normalized_Position (Ocomp));
4309 -- Note: we don't use Set_Biased here, because we
4310 -- already gave a warning above if needed, and we
4311 -- would get a duplicate for the same name here.
4313 Set_Has_Biased_Representation
4314 (Ocomp, Has_Biased_Representation (Comp));
4317 if Esize (Comp) < 0 then
4318 Error_Msg_N ("component size is negative", CC);
4329 -- Check missing components if Complete_Representation pragma appeared
4331 if Present (CR_Pragma) then
4332 Comp := First_Component_Or_Discriminant (Rectype);
4333 while Present (Comp) loop
4334 if No (Component_Clause (Comp)) then
4336 ("missing component clause for &", CR_Pragma, Comp);
4339 Next_Component_Or_Discriminant (Comp);
4342 -- If no Complete_Representation pragma, warn if missing components
4344 elsif Warn_On_Unrepped_Components then
4346 Num_Repped_Components : Nat := 0;
4347 Num_Unrepped_Components : Nat := 0;
4350 -- First count number of repped and unrepped components
4352 Comp := First_Component_Or_Discriminant (Rectype);
4353 while Present (Comp) loop
4354 if Present (Component_Clause (Comp)) then
4355 Num_Repped_Components := Num_Repped_Components + 1;
4357 Num_Unrepped_Components := Num_Unrepped_Components + 1;
4360 Next_Component_Or_Discriminant (Comp);
4363 -- We are only interested in the case where there is at least one
4364 -- unrepped component, and at least half the components have rep
4365 -- clauses. We figure that if less than half have them, then the
4366 -- partial rep clause is really intentional. If the component
4367 -- type has no underlying type set at this point (as for a generic
4368 -- formal type), we don't know enough to give a warning on the
4371 if Num_Unrepped_Components > 0
4372 and then Num_Unrepped_Components < Num_Repped_Components
4374 Comp := First_Component_Or_Discriminant (Rectype);
4375 while Present (Comp) loop
4376 if No (Component_Clause (Comp))
4377 and then Comes_From_Source (Comp)
4378 and then Present (Underlying_Type (Etype (Comp)))
4379 and then (Is_Scalar_Type (Underlying_Type (Etype (Comp)))
4380 or else Size_Known_At_Compile_Time
4381 (Underlying_Type (Etype (Comp))))
4382 and then not Has_Warnings_Off (Rectype)
4384 Error_Msg_Sloc := Sloc (Comp);
4386 ("?no component clause given for & declared #",
4390 Next_Component_Or_Discriminant (Comp);
4395 end Analyze_Record_Representation_Clause;
4397 -------------------------------
4398 -- Build_Invariant_Procedure --
4399 -------------------------------
4401 -- The procedure that is constructed here has the form
4403 -- procedure typInvariant (Ixxx : typ) is
4405 -- pragma Check (Invariant, exp, "failed invariant from xxx");
4406 -- pragma Check (Invariant, exp, "failed invariant from xxx");
4408 -- pragma Check (Invariant, exp, "failed inherited invariant from xxx");
4410 -- end typInvariant;
4412 procedure Build_Invariant_Procedure (Typ : Entity_Id; N : Node_Id) is
4413 Loc : constant Source_Ptr := Sloc (Typ);
4420 Visible_Decls : constant List_Id := Visible_Declarations (N);
4421 Private_Decls : constant List_Id := Private_Declarations (N);
4423 procedure Add_Invariants (T : Entity_Id; Inherit : Boolean);
4424 -- Appends statements to Stmts for any invariants in the rep item chain
4425 -- of the given type. If Inherit is False, then we only process entries
4426 -- on the chain for the type Typ. If Inherit is True, then we ignore any
4427 -- Invariant aspects, but we process all Invariant'Class aspects, adding
4428 -- "inherited" to the exception message and generating an informational
4429 -- message about the inheritance of an invariant.
4431 Object_Name : constant Name_Id := New_Internal_Name ('I');
4432 -- Name for argument of invariant procedure
4434 Object_Entity : constant Node_Id :=
4435 Make_Defining_Identifier (Loc, Object_Name);
4436 -- The procedure declaration entity for the argument
4438 --------------------
4439 -- Add_Invariants --
4440 --------------------
4442 procedure Add_Invariants (T : Entity_Id; Inherit : Boolean) is
4452 procedure Replace_Type_Reference (N : Node_Id);
4453 -- Replace a single occurrence N of the subtype name with a reference
4454 -- to the formal of the predicate function. N can be an identifier
4455 -- referencing the subtype, or a selected component, representing an
4456 -- appropriately qualified occurrence of the subtype name.
4458 procedure Replace_Type_References is
4459 new Replace_Type_References_Generic (Replace_Type_Reference);
4460 -- Traverse an expression replacing all occurrences of the subtype
4461 -- name with appropriate references to the object that is the formal
4462 -- parameter of the predicate function. Note that we must ensure
4463 -- that the type and entity information is properly set in the
4464 -- replacement node, since we will do a Preanalyze call of this
4465 -- expression without proper visibility of the procedure argument.
4467 ----------------------------
4468 -- Replace_Type_Reference --
4469 ----------------------------
4471 procedure Replace_Type_Reference (N : Node_Id) is
4473 -- Invariant'Class, replace with T'Class (obj)
4475 if Class_Present (Ritem) then
4477 Make_Type_Conversion (Loc,
4479 Make_Attribute_Reference (Loc,
4480 Prefix => New_Occurrence_Of (T, Loc),
4481 Attribute_Name => Name_Class),
4482 Expression => Make_Identifier (Loc, Object_Name)));
4484 Set_Entity (Expression (N), Object_Entity);
4485 Set_Etype (Expression (N), Typ);
4487 -- Invariant, replace with obj
4490 Rewrite (N, Make_Identifier (Loc, Object_Name));
4491 Set_Entity (N, Object_Entity);
4494 end Replace_Type_Reference;
4496 -- Start of processing for Add_Invariants
4499 Ritem := First_Rep_Item (T);
4500 while Present (Ritem) loop
4501 if Nkind (Ritem) = N_Pragma
4502 and then Pragma_Name (Ritem) = Name_Invariant
4504 Arg1 := First (Pragma_Argument_Associations (Ritem));
4505 Arg2 := Next (Arg1);
4506 Arg3 := Next (Arg2);
4508 Arg1 := Get_Pragma_Arg (Arg1);
4509 Arg2 := Get_Pragma_Arg (Arg2);
4511 -- For Inherit case, ignore Invariant, process only Class case
4514 if not Class_Present (Ritem) then
4518 -- For Inherit false, process only item for right type
4521 if Entity (Arg1) /= Typ then
4527 Stmts := Empty_List;
4530 Exp := New_Copy_Tree (Arg2);
4533 -- We need to replace any occurrences of the name of the type
4534 -- with references to the object, converted to type'Class in
4535 -- the case of Invariant'Class aspects.
4537 Replace_Type_References (Exp, Chars (T));
4539 -- If this invariant comes from an aspect, find the aspect
4540 -- specification, and replace the saved expression because
4541 -- we need the subtype references replaced for the calls to
4542 -- Preanalyze_Spec_Expressin in Check_Aspect_At_Freeze_Point
4543 -- and Check_Aspect_At_End_Of_Declarations.
4545 if From_Aspect_Specification (Ritem) then
4550 -- Loop to find corresponding aspect, note that this
4551 -- must be present given the pragma is marked delayed.
4553 Aitem := Next_Rep_Item (Ritem);
4554 while Present (Aitem) loop
4555 if Nkind (Aitem) = N_Aspect_Specification
4556 and then Aspect_Rep_Item (Aitem) = Ritem
4559 (Identifier (Aitem), New_Copy_Tree (Exp));
4563 Aitem := Next_Rep_Item (Aitem);
4568 -- Now we need to preanalyze the expression to properly capture
4569 -- the visibility in the visible part. The expression will not
4570 -- be analyzed for real until the body is analyzed, but that is
4571 -- at the end of the private part and has the wrong visibility.
4573 Set_Parent (Exp, N);
4574 Preanalyze_Spec_Expression (Exp, Standard_Boolean);
4576 -- Build first two arguments for Check pragma
4579 Make_Pragma_Argument_Association (Loc,
4580 Expression => Make_Identifier (Loc, Name_Invariant)),
4581 Make_Pragma_Argument_Association (Loc, Expression => Exp));
4583 -- Add message if present in Invariant pragma
4585 if Present (Arg3) then
4586 Str := Strval (Get_Pragma_Arg (Arg3));
4588 -- If inherited case, and message starts "failed invariant",
4589 -- change it to be "failed inherited invariant".
4592 String_To_Name_Buffer (Str);
4594 if Name_Buffer (1 .. 16) = "failed invariant" then
4595 Insert_Str_In_Name_Buffer ("inherited ", 8);
4596 Str := String_From_Name_Buffer;
4601 Make_Pragma_Argument_Association (Loc,
4602 Expression => Make_String_Literal (Loc, Str)));
4605 -- Add Check pragma to list of statements
4609 Pragma_Identifier =>
4610 Make_Identifier (Loc, Name_Check),
4611 Pragma_Argument_Associations => Assoc));
4613 -- If Inherited case and option enabled, output info msg. Note
4614 -- that we know this is a case of Invariant'Class.
4616 if Inherit and Opt.List_Inherited_Aspects then
4617 Error_Msg_Sloc := Sloc (Ritem);
4619 ("?info: & inherits `Invariant''Class` aspect from #",
4625 Next_Rep_Item (Ritem);
4629 -- Start of processing for Build_Invariant_Procedure
4635 Set_Etype (Object_Entity, Typ);
4637 -- Add invariants for the current type
4639 Add_Invariants (Typ, Inherit => False);
4641 -- Add invariants for parent types
4644 Current_Typ : Entity_Id;
4645 Parent_Typ : Entity_Id;
4650 Parent_Typ := Etype (Current_Typ);
4652 if Is_Private_Type (Parent_Typ)
4653 and then Present (Full_View (Base_Type (Parent_Typ)))
4655 Parent_Typ := Full_View (Base_Type (Parent_Typ));
4658 exit when Parent_Typ = Current_Typ;
4660 Current_Typ := Parent_Typ;
4661 Add_Invariants (Current_Typ, Inherit => True);
4665 -- Build the procedure if we generated at least one Check pragma
4667 if Stmts /= No_List then
4669 -- Build procedure declaration
4672 Make_Defining_Identifier (Loc,
4673 Chars => New_External_Name (Chars (Typ), "Invariant"));
4674 Set_Has_Invariants (SId);
4675 Set_Invariant_Procedure (Typ, SId);
4678 Make_Procedure_Specification (Loc,
4679 Defining_Unit_Name => SId,
4680 Parameter_Specifications => New_List (
4681 Make_Parameter_Specification (Loc,
4682 Defining_Identifier => Object_Entity,
4683 Parameter_Type => New_Occurrence_Of (Typ, Loc))));
4685 PDecl := Make_Subprogram_Declaration (Loc, Specification => Spec);
4687 -- Build procedure body
4690 Make_Defining_Identifier (Loc,
4691 Chars => New_External_Name (Chars (Typ), "Invariant"));
4694 Make_Procedure_Specification (Loc,
4695 Defining_Unit_Name => SId,
4696 Parameter_Specifications => New_List (
4697 Make_Parameter_Specification (Loc,
4698 Defining_Identifier =>
4699 Make_Defining_Identifier (Loc, Object_Name),
4700 Parameter_Type => New_Occurrence_Of (Typ, Loc))));
4703 Make_Subprogram_Body (Loc,
4704 Specification => Spec,
4705 Declarations => Empty_List,
4706 Handled_Statement_Sequence =>
4707 Make_Handled_Sequence_Of_Statements (Loc,
4708 Statements => Stmts));
4710 -- Insert procedure declaration and spec at the appropriate points.
4711 -- Skip this if there are no private declarations (that's an error
4712 -- that will be diagnosed elsewhere, and there is no point in having
4713 -- an invariant procedure set if the full declaration is missing).
4715 if Present (Private_Decls) then
4717 -- The spec goes at the end of visible declarations, but they have
4718 -- already been analyzed, so we need to explicitly do the analyze.
4720 Append_To (Visible_Decls, PDecl);
4723 -- The body goes at the end of the private declarations, which we
4724 -- have not analyzed yet, so we do not need to perform an explicit
4725 -- analyze call. We skip this if there are no private declarations
4726 -- (this is an error that will be caught elsewhere);
4728 Append_To (Private_Decls, PBody);
4731 end Build_Invariant_Procedure;
4733 ------------------------------
4734 -- Build_Predicate_Function --
4735 ------------------------------
4737 -- The procedure that is constructed here has the form
4739 -- function typPredicate (Ixxx : typ) return Boolean is
4742 -- exp1 and then exp2 and then ...
4743 -- and then typ1Predicate (typ1 (Ixxx))
4744 -- and then typ2Predicate (typ2 (Ixxx))
4746 -- end typPredicate;
4748 -- Here exp1, and exp2 are expressions from Predicate pragmas. Note that
4749 -- this is the point at which these expressions get analyzed, providing the
4750 -- required delay, and typ1, typ2, are entities from which predicates are
4751 -- inherited. Note that we do NOT generate Check pragmas, that's because we
4752 -- use this function even if checks are off, e.g. for membership tests.
4754 procedure Build_Predicate_Function (Typ : Entity_Id; N : Node_Id) is
4755 Loc : constant Source_Ptr := Sloc (Typ);
4762 -- This is the expression for the return statement in the function. It
4763 -- is build by connecting the component predicates with AND THEN.
4765 procedure Add_Call (T : Entity_Id);
4766 -- Includes a call to the predicate function for type T in Expr if T
4767 -- has predicates and Predicate_Function (T) is non-empty.
4769 procedure Add_Predicates;
4770 -- Appends expressions for any Predicate pragmas in the rep item chain
4771 -- Typ to Expr. Note that we look only at items for this exact entity.
4772 -- Inheritance of predicates for the parent type is done by calling the
4773 -- Predicate_Function of the parent type, using Add_Call above.
4775 Object_Name : constant Name_Id := New_Internal_Name ('I');
4776 -- Name for argument of Predicate procedure
4778 Object_Entity : constant Entity_Id :=
4779 Make_Defining_Identifier (Loc, Object_Name);
4780 -- The entity for the spec entity for the argument
4782 Dynamic_Predicate_Present : Boolean := False;
4783 -- Set True if a dynamic predicate is present, results in the entire
4784 -- predicate being considered dynamic even if it looks static
4786 Static_Predicate_Present : Node_Id := Empty;
4787 -- Set to N_Pragma node for a static predicate if one is encountered.
4793 procedure Add_Call (T : Entity_Id) is
4797 if Present (T) and then Present (Predicate_Function (T)) then
4798 Set_Has_Predicates (Typ);
4800 -- Build the call to the predicate function of T
4804 (T, Convert_To (T, Make_Identifier (Loc, Object_Name)));
4806 -- Add call to evolving expression, using AND THEN if needed
4813 Left_Opnd => Relocate_Node (Expr),
4817 -- Output info message on inheritance if required. Note we do not
4818 -- give this information for generic actual types, since it is
4819 -- unwelcome noise in that case in instantiations. We also
4820 -- generally suppress the message in instantiations, and also
4821 -- if it involves internal names.
4823 if Opt.List_Inherited_Aspects
4824 and then not Is_Generic_Actual_Type (Typ)
4825 and then Instantiation_Depth (Sloc (Typ)) = 0
4826 and then not Is_Internal_Name (Chars (T))
4827 and then not Is_Internal_Name (Chars (Typ))
4829 Error_Msg_Sloc := Sloc (Predicate_Function (T));
4830 Error_Msg_Node_2 := T;
4831 Error_Msg_N ("?info: & inherits predicate from & #", Typ);
4836 --------------------
4837 -- Add_Predicates --
4838 --------------------
4840 procedure Add_Predicates is
4845 procedure Replace_Type_Reference (N : Node_Id);
4846 -- Replace a single occurrence N of the subtype name with a reference
4847 -- to the formal of the predicate function. N can be an identifier
4848 -- referencing the subtype, or a selected component, representing an
4849 -- appropriately qualified occurrence of the subtype name.
4851 procedure Replace_Type_References is
4852 new Replace_Type_References_Generic (Replace_Type_Reference);
4853 -- Traverse an expression changing every occurrence of an identifier
4854 -- whose name matches the name of the subtype with a reference to
4855 -- the formal parameter of the predicate function.
4857 ----------------------------
4858 -- Replace_Type_Reference --
4859 ----------------------------
4861 procedure Replace_Type_Reference (N : Node_Id) is
4863 Rewrite (N, Make_Identifier (Loc, Object_Name));
4864 Set_Entity (N, Object_Entity);
4866 end Replace_Type_Reference;
4868 -- Start of processing for Add_Predicates
4871 Ritem := First_Rep_Item (Typ);
4872 while Present (Ritem) loop
4873 if Nkind (Ritem) = N_Pragma
4874 and then Pragma_Name (Ritem) = Name_Predicate
4876 if Present (Corresponding_Aspect (Ritem)) then
4877 case Chars (Identifier (Corresponding_Aspect (Ritem))) is
4878 when Name_Dynamic_Predicate =>
4879 Dynamic_Predicate_Present := True;
4880 when Name_Static_Predicate =>
4881 Static_Predicate_Present := Ritem;
4887 -- Acquire arguments
4889 Arg1 := First (Pragma_Argument_Associations (Ritem));
4890 Arg2 := Next (Arg1);
4892 Arg1 := Get_Pragma_Arg (Arg1);
4893 Arg2 := Get_Pragma_Arg (Arg2);
4895 -- See if this predicate pragma is for the current type or for
4896 -- its full view. A predicate on a private completion is placed
4897 -- on the partial view beause this is the visible entity that
4900 if Entity (Arg1) = Typ
4901 or else Full_View (Entity (Arg1)) = Typ
4904 -- We have a match, this entry is for our subtype
4906 -- We need to replace any occurrences of the name of the
4907 -- type with references to the object.
4909 Replace_Type_References (Arg2, Chars (Typ));
4911 -- If this predicate comes from an aspect, find the aspect
4912 -- specification, and replace the saved expression because
4913 -- we need the subtype references replaced for the calls to
4914 -- Preanalyze_Spec_Expressin in Check_Aspect_At_Freeze_Point
4915 -- and Check_Aspect_At_End_Of_Declarations.
4917 if From_Aspect_Specification (Ritem) then
4922 -- Loop to find corresponding aspect, note that this
4923 -- must be present given the pragma is marked delayed.
4925 Aitem := Next_Rep_Item (Ritem);
4927 if Nkind (Aitem) = N_Aspect_Specification
4928 and then Aspect_Rep_Item (Aitem) = Ritem
4931 (Identifier (Aitem), New_Copy_Tree (Arg2));
4935 Aitem := Next_Rep_Item (Aitem);
4940 -- Now we can add the expression
4943 Expr := Relocate_Node (Arg2);
4945 -- There already was a predicate, so add to it
4950 Left_Opnd => Relocate_Node (Expr),
4951 Right_Opnd => Relocate_Node (Arg2));
4956 Next_Rep_Item (Ritem);
4960 -- Start of processing for Build_Predicate_Function
4963 -- Initialize for construction of statement list
4967 -- Return if already built or if type does not have predicates
4969 if not Has_Predicates (Typ)
4970 or else Present (Predicate_Function (Typ))
4975 -- Add Predicates for the current type
4979 -- Add predicates for ancestor if present
4982 Atyp : constant Entity_Id := Nearest_Ancestor (Typ);
4984 if Present (Atyp) then
4989 -- If we have predicates, build the function
4991 if Present (Expr) then
4993 -- Build function declaration
4995 pragma Assert (Has_Predicates (Typ));
4997 Make_Defining_Identifier (Loc,
4998 Chars => New_External_Name (Chars (Typ), "Predicate"));
4999 Set_Has_Predicates (SId);
5000 Set_Predicate_Function (Typ, SId);
5003 Make_Function_Specification (Loc,
5004 Defining_Unit_Name => SId,
5005 Parameter_Specifications => New_List (
5006 Make_Parameter_Specification (Loc,
5007 Defining_Identifier => Object_Entity,
5008 Parameter_Type => New_Occurrence_Of (Typ, Loc))),
5009 Result_Definition =>
5010 New_Occurrence_Of (Standard_Boolean, Loc));
5012 FDecl := Make_Subprogram_Declaration (Loc, Specification => Spec);
5014 -- Build function body
5017 Make_Defining_Identifier (Loc,
5018 Chars => New_External_Name (Chars (Typ), "Predicate"));
5021 Make_Function_Specification (Loc,
5022 Defining_Unit_Name => SId,
5023 Parameter_Specifications => New_List (
5024 Make_Parameter_Specification (Loc,
5025 Defining_Identifier =>
5026 Make_Defining_Identifier (Loc, Object_Name),
5028 New_Occurrence_Of (Typ, Loc))),
5029 Result_Definition =>
5030 New_Occurrence_Of (Standard_Boolean, Loc));
5033 Make_Subprogram_Body (Loc,
5034 Specification => Spec,
5035 Declarations => Empty_List,
5036 Handled_Statement_Sequence =>
5037 Make_Handled_Sequence_Of_Statements (Loc,
5038 Statements => New_List (
5039 Make_Simple_Return_Statement (Loc,
5040 Expression => Expr))));
5042 -- Insert declaration before freeze node and body after
5044 Insert_Before_And_Analyze (N, FDecl);
5045 Insert_After_And_Analyze (N, FBody);
5047 -- Deal with static predicate case
5049 if Ekind_In (Typ, E_Enumeration_Subtype,
5050 E_Modular_Integer_Subtype,
5051 E_Signed_Integer_Subtype)
5052 and then Is_Static_Subtype (Typ)
5053 and then not Dynamic_Predicate_Present
5055 Build_Static_Predicate (Typ, Expr, Object_Name);
5057 if Present (Static_Predicate_Present)
5058 and No (Static_Predicate (Typ))
5061 ("expression does not have required form for "
5062 & "static predicate",
5063 Next (First (Pragma_Argument_Associations
5064 (Static_Predicate_Present))));
5068 end Build_Predicate_Function;
5070 ----------------------------
5071 -- Build_Static_Predicate --
5072 ----------------------------
5074 procedure Build_Static_Predicate
5079 Loc : constant Source_Ptr := Sloc (Expr);
5081 Non_Static : exception;
5082 -- Raised if something non-static is found
5084 Btyp : constant Entity_Id := Base_Type (Typ);
5086 BLo : constant Uint := Expr_Value (Type_Low_Bound (Btyp));
5087 BHi : constant Uint := Expr_Value (Type_High_Bound (Btyp));
5088 -- Low bound and high bound value of base type of Typ
5090 TLo : constant Uint := Expr_Value (Type_Low_Bound (Typ));
5091 THi : constant Uint := Expr_Value (Type_High_Bound (Typ));
5092 -- Low bound and high bound values of static subtype Typ
5097 -- One entry in a Rlist value, a single REnt (range entry) value
5098 -- denotes one range from Lo to Hi. To represent a single value
5099 -- range Lo = Hi = value.
5101 type RList is array (Nat range <>) of REnt;
5102 -- A list of ranges. The ranges are sorted in increasing order,
5103 -- and are disjoint (there is a gap of at least one value between
5104 -- each range in the table). A value is in the set of ranges in
5105 -- Rlist if it lies within one of these ranges
5107 False_Range : constant RList :=
5108 RList'(1 .. 0 => REnt'(No_Uint, No_Uint));
5109 -- An empty set of ranges represents a range list that can never be
5110 -- satisfied, since there are no ranges in which the value could lie,
5111 -- so it does not lie in any of them. False_Range is a canonical value
5112 -- for this empty set, but general processing should test for an Rlist
5113 -- with length zero (see Is_False predicate), since other null ranges
5114 -- may appear which must be treated as False.
5116 True_Range : constant RList := RList'(1 => REnt'(BLo, BHi));
5117 -- Range representing True, value must be in the base range
5119 function "and" (Left, Right : RList) return RList;
5120 -- And's together two range lists, returning a range list. This is
5121 -- a set intersection operation.
5123 function "or" (Left, Right : RList) return RList;
5124 -- Or's together two range lists, returning a range list. This is a
5125 -- set union operation.
5127 function "not" (Right : RList) return RList;
5128 -- Returns complement of a given range list, i.e. a range list
5129 -- representing all the values in TLo .. THi that are not in the
5130 -- input operand Right.
5132 function Build_Val (V : Uint) return Node_Id;
5133 -- Return an analyzed N_Identifier node referencing this value, suitable
5134 -- for use as an entry in the Static_Predicate list. This node is typed
5135 -- with the base type.
5137 function Build_Range (Lo, Hi : Uint) return Node_Id;
5138 -- Return an analyzed N_Range node referencing this range, suitable
5139 -- for use as an entry in the Static_Predicate list. This node is typed
5140 -- with the base type.
5142 function Get_RList (Exp : Node_Id) return RList;
5143 -- This is a recursive routine that converts the given expression into
5144 -- a list of ranges, suitable for use in building the static predicate.
5146 function Is_False (R : RList) return Boolean;
5147 pragma Inline (Is_False);
5148 -- Returns True if the given range list is empty, and thus represents
5149 -- a False list of ranges that can never be satisfied.
5151 function Is_True (R : RList) return Boolean;
5152 -- Returns True if R trivially represents the True predicate by having
5153 -- a single range from BLo to BHi.
5155 function Is_Type_Ref (N : Node_Id) return Boolean;
5156 pragma Inline (Is_Type_Ref);
5157 -- Returns if True if N is a reference to the type for the predicate in
5158 -- the expression (i.e. if it is an identifier whose Chars field matches
5159 -- the Nam given in the call).
5161 function Lo_Val (N : Node_Id) return Uint;
5162 -- Given static expression or static range from a Static_Predicate list,
5163 -- gets expression value or low bound of range.
5165 function Hi_Val (N : Node_Id) return Uint;
5166 -- Given static expression or static range from a Static_Predicate list,
5167 -- gets expression value of high bound of range.
5169 function Membership_Entry (N : Node_Id) return RList;
5170 -- Given a single membership entry (range, value, or subtype), returns
5171 -- the corresponding range list. Raises Static_Error if not static.
5173 function Membership_Entries (N : Node_Id) return RList;
5174 -- Given an element on an alternatives list of a membership operation,
5175 -- returns the range list corresponding to this entry and all following
5176 -- entries (i.e. returns the "or" of this list of values).
5178 function Stat_Pred (Typ : Entity_Id) return RList;
5179 -- Given a type, if it has a static predicate, then return the predicate
5180 -- as a range list, otherwise raise Non_Static.
5186 function "and" (Left, Right : RList) return RList is
5188 -- First range of result
5190 SLeft : Nat := Left'First;
5191 -- Start of rest of left entries
5193 SRight : Nat := Right'First;
5194 -- Start of rest of right entries
5197 -- If either range is True, return the other
5199 if Is_True (Left) then
5201 elsif Is_True (Right) then
5205 -- If either range is False, return False
5207 if Is_False (Left) or else Is_False (Right) then
5211 -- Loop to remove entries at start that are disjoint, and thus
5212 -- just get discarded from the result entirely.
5215 -- If no operands left in either operand, result is false
5217 if SLeft > Left'Last or else SRight > Right'Last then
5220 -- Discard first left operand entry if disjoint with right
5222 elsif Left (SLeft).Hi < Right (SRight).Lo then
5225 -- Discard first right operand entry if disjoint with left
5227 elsif Right (SRight).Hi < Left (SLeft).Lo then
5228 SRight := SRight + 1;
5230 -- Otherwise we have an overlapping entry
5237 -- Now we have two non-null operands, and first entries overlap.
5238 -- The first entry in the result will be the overlapping part of
5239 -- these two entries.
5241 FEnt := REnt'(Lo => UI_Max (Left (SLeft).Lo, Right (SRight).Lo),
5242 Hi => UI_Min (Left (SLeft).Hi, Right (SRight).Hi));
5244 -- Now we can remove the entry that ended at a lower value, since
5245 -- its contribution is entirely contained in Fent.
5247 if Left (SLeft).Hi <= Right (SRight).Hi then
5250 SRight := SRight + 1;
5253 -- Compute result by concatenating this first entry with the "and"
5254 -- of the remaining parts of the left and right operands. Note that
5255 -- if either of these is empty, "and" will yield empty, so that we
5256 -- will end up with just Fent, which is what we want in that case.
5259 FEnt & (Left (SLeft .. Left'Last) and Right (SRight .. Right'Last));
5266 function "not" (Right : RList) return RList is
5268 -- Return True if False range
5270 if Is_False (Right) then
5274 -- Return False if True range
5276 if Is_True (Right) then
5280 -- Here if not trivial case
5283 Result : RList (1 .. Right'Length + 1);
5284 -- May need one more entry for gap at beginning and end
5287 -- Number of entries stored in Result
5292 if Right (Right'First).Lo > TLo then
5294 Result (Count) := REnt'(TLo, Right (Right'First).Lo - 1);
5297 -- Gaps between ranges
5299 for J in Right'First .. Right'Last - 1 loop
5302 REnt'(Right (J).Hi + 1, Right (J + 1).Lo - 1);
5307 if Right (Right'Last).Hi < THi then
5309 Result (Count) := REnt'(Right (Right'Last).Hi + 1, THi);
5312 return Result (1 .. Count);
5320 function "or" (Left, Right : RList) return RList is
5322 -- First range of result
5324 SLeft : Nat := Left'First;
5325 -- Start of rest of left entries
5327 SRight : Nat := Right'First;
5328 -- Start of rest of right entries
5331 -- If either range is True, return True
5333 if Is_True (Left) or else Is_True (Right) then
5337 -- If either range is False (empty), return the other
5339 if Is_False (Left) then
5341 elsif Is_False (Right) then
5345 -- Initialize result first entry from left or right operand
5346 -- depending on which starts with the lower range.
5348 if Left (SLeft).Lo < Right (SRight).Lo then
5349 FEnt := Left (SLeft);
5352 FEnt := Right (SRight);
5353 SRight := SRight + 1;
5356 -- This loop eats ranges from left and right operands that
5357 -- are contiguous with the first range we are gathering.
5360 -- Eat first entry in left operand if contiguous or
5361 -- overlapped by gathered first operand of result.
5363 if SLeft <= Left'Last
5364 and then Left (SLeft).Lo <= FEnt.Hi + 1
5366 FEnt.Hi := UI_Max (FEnt.Hi, Left (SLeft).Hi);
5369 -- Eat first entry in right operand if contiguous or
5370 -- overlapped by gathered right operand of result.
5372 elsif SRight <= Right'Last
5373 and then Right (SRight).Lo <= FEnt.Hi + 1
5375 FEnt.Hi := UI_Max (FEnt.Hi, Right (SRight).Hi);
5376 SRight := SRight + 1;
5378 -- All done if no more entries to eat!
5385 -- Obtain result as the first entry we just computed, concatenated
5386 -- to the "or" of the remaining results (if one operand is empty,
5387 -- this will just concatenate with the other
5390 FEnt & (Left (SLeft .. Left'Last) or Right (SRight .. Right'Last));
5397 function Build_Range (Lo, Hi : Uint) return Node_Id is
5401 return Build_Val (Hi);
5405 Low_Bound => Build_Val (Lo),
5406 High_Bound => Build_Val (Hi));
5407 Set_Etype (Result, Btyp);
5408 Set_Analyzed (Result);
5417 function Build_Val (V : Uint) return Node_Id is
5421 if Is_Enumeration_Type (Typ) then
5422 Result := Get_Enum_Lit_From_Pos (Typ, V, Loc);
5424 Result := Make_Integer_Literal (Loc, V);
5427 Set_Etype (Result, Btyp);
5428 Set_Is_Static_Expression (Result);
5429 Set_Analyzed (Result);
5437 function Get_RList (Exp : Node_Id) return RList is
5442 -- Static expression can only be true or false
5444 if Is_OK_Static_Expression (Exp) then
5448 if Expr_Value (Exp) = 0 then
5455 -- Otherwise test node type
5463 when N_Op_And | N_And_Then =>
5464 return Get_RList (Left_Opnd (Exp))
5466 Get_RList (Right_Opnd (Exp));
5470 when N_Op_Or | N_Or_Else =>
5471 return Get_RList (Left_Opnd (Exp))
5473 Get_RList (Right_Opnd (Exp));
5478 return not Get_RList (Right_Opnd (Exp));
5480 -- Comparisons of type with static value
5482 when N_Op_Compare =>
5483 -- Type is left operand
5485 if Is_Type_Ref (Left_Opnd (Exp))
5486 and then Is_OK_Static_Expression (Right_Opnd (Exp))
5488 Val := Expr_Value (Right_Opnd (Exp));
5490 -- Typ is right operand
5492 elsif Is_Type_Ref (Right_Opnd (Exp))
5493 and then Is_OK_Static_Expression (Left_Opnd (Exp))
5495 Val := Expr_Value (Left_Opnd (Exp));
5497 -- Invert sense of comparison
5500 when N_Op_Gt => Op := N_Op_Lt;
5501 when N_Op_Lt => Op := N_Op_Gt;
5502 when N_Op_Ge => Op := N_Op_Le;
5503 when N_Op_Le => Op := N_Op_Ge;
5504 when others => null;
5507 -- Other cases are non-static
5513 -- Construct range according to comparison operation
5517 return RList'(1 => REnt'(Val, Val));
5520 return RList'(1 => REnt'(Val, BHi));
5523 return RList'(1 => REnt'(Val + 1, BHi));
5526 return RList'(1 => REnt'(BLo, Val));
5529 return RList'(1 => REnt'(BLo, Val - 1));
5532 return RList'(REnt'(BLo, Val - 1),
5533 REnt'(Val + 1, BHi));
5536 raise Program_Error;
5542 if not Is_Type_Ref (Left_Opnd (Exp)) then
5546 if Present (Right_Opnd (Exp)) then
5547 return Membership_Entry (Right_Opnd (Exp));
5549 return Membership_Entries (First (Alternatives (Exp)));
5552 -- Negative membership (NOT IN)
5555 if not Is_Type_Ref (Left_Opnd (Exp)) then
5559 if Present (Right_Opnd (Exp)) then
5560 return not Membership_Entry (Right_Opnd (Exp));
5562 return not Membership_Entries (First (Alternatives (Exp)));
5565 -- Function call, may be call to static predicate
5567 when N_Function_Call =>
5568 if Is_Entity_Name (Name (Exp)) then
5570 Ent : constant Entity_Id := Entity (Name (Exp));
5572 if Has_Predicates (Ent) then
5573 return Stat_Pred (Etype (First_Formal (Ent)));
5578 -- Other function call cases are non-static
5582 -- Qualified expression, dig out the expression
5584 when N_Qualified_Expression =>
5585 return Get_RList (Expression (Exp));
5590 return (Get_RList (Left_Opnd (Exp))
5591 and not Get_RList (Right_Opnd (Exp)))
5592 or (Get_RList (Right_Opnd (Exp))
5593 and not Get_RList (Left_Opnd (Exp)));
5595 -- Any other node type is non-static
5606 function Hi_Val (N : Node_Id) return Uint is
5608 if Is_Static_Expression (N) then
5609 return Expr_Value (N);
5611 pragma Assert (Nkind (N) = N_Range);
5612 return Expr_Value (High_Bound (N));
5620 function Is_False (R : RList) return Boolean is
5622 return R'Length = 0;
5629 function Is_True (R : RList) return Boolean is
5632 and then R (R'First).Lo = BLo
5633 and then R (R'First).Hi = BHi;
5640 function Is_Type_Ref (N : Node_Id) return Boolean is
5642 return Nkind (N) = N_Identifier and then Chars (N) = Nam;
5649 function Lo_Val (N : Node_Id) return Uint is
5651 if Is_Static_Expression (N) then
5652 return Expr_Value (N);
5654 pragma Assert (Nkind (N) = N_Range);
5655 return Expr_Value (Low_Bound (N));
5659 ------------------------
5660 -- Membership_Entries --
5661 ------------------------
5663 function Membership_Entries (N : Node_Id) return RList is
5665 if No (Next (N)) then
5666 return Membership_Entry (N);
5668 return Membership_Entry (N) or Membership_Entries (Next (N));
5670 end Membership_Entries;
5672 ----------------------
5673 -- Membership_Entry --
5674 ----------------------
5676 function Membership_Entry (N : Node_Id) return RList is
5684 if Nkind (N) = N_Range then
5685 if not Is_Static_Expression (Low_Bound (N))
5687 not Is_Static_Expression (High_Bound (N))
5691 SLo := Expr_Value (Low_Bound (N));
5692 SHi := Expr_Value (High_Bound (N));
5693 return RList'(1 => REnt'(SLo, SHi));
5696 -- Static expression case
5698 elsif Is_Static_Expression (N) then
5699 Val := Expr_Value (N);
5700 return RList'(1 => REnt'(Val, Val));
5702 -- Identifier (other than static expression) case
5704 else pragma Assert (Nkind (N) = N_Identifier);
5708 if Is_Type (Entity (N)) then
5710 -- If type has predicates, process them
5712 if Has_Predicates (Entity (N)) then
5713 return Stat_Pred (Entity (N));
5715 -- For static subtype without predicates, get range
5717 elsif Is_Static_Subtype (Entity (N)) then
5718 SLo := Expr_Value (Type_Low_Bound (Entity (N)));
5719 SHi := Expr_Value (Type_High_Bound (Entity (N)));
5720 return RList'(1 => REnt'(SLo, SHi));
5722 -- Any other type makes us non-static
5728 -- Any other kind of identifier in predicate (e.g. a non-static
5729 -- expression value) means this is not a static predicate.
5735 end Membership_Entry;
5741 function Stat_Pred (Typ : Entity_Id) return RList is
5743 -- Not static if type does not have static predicates
5745 if not Has_Predicates (Typ)
5746 or else No (Static_Predicate (Typ))
5751 -- Otherwise we convert the predicate list to a range list
5754 Result : RList (1 .. List_Length (Static_Predicate (Typ)));
5758 P := First (Static_Predicate (Typ));
5759 for J in Result'Range loop
5760 Result (J) := REnt'(Lo_Val (P), Hi_Val (P));
5768 -- Start of processing for Build_Static_Predicate
5771 -- Now analyze the expression to see if it is a static predicate
5774 Ranges : constant RList := Get_RList (Expr);
5775 -- Range list from expression if it is static
5780 -- Convert range list into a form for the static predicate. In the
5781 -- Ranges array, we just have raw ranges, these must be converted
5782 -- to properly typed and analyzed static expressions or range nodes.
5784 -- Note: here we limit ranges to the ranges of the subtype, so that
5785 -- a predicate is always false for values outside the subtype. That
5786 -- seems fine, such values are invalid anyway, and considering them
5787 -- to fail the predicate seems allowed and friendly, and furthermore
5788 -- simplifies processing for case statements and loops.
5792 for J in Ranges'Range loop
5794 Lo : Uint := Ranges (J).Lo;
5795 Hi : Uint := Ranges (J).Hi;
5798 -- Ignore completely out of range entry
5800 if Hi < TLo or else Lo > THi then
5803 -- Otherwise process entry
5806 -- Adjust out of range value to subtype range
5816 -- Convert range into required form
5819 Append_To (Plist, Build_Val (Lo));
5821 Append_To (Plist, Build_Range (Lo, Hi));
5827 -- Processing was successful and all entries were static, so now we
5828 -- can store the result as the predicate list.
5830 Set_Static_Predicate (Typ, Plist);
5832 -- The processing for static predicates put the expression into
5833 -- canonical form as a series of ranges. It also eliminated
5834 -- duplicates and collapsed and combined ranges. We might as well
5835 -- replace the alternatives list of the right operand of the
5836 -- membership test with the static predicate list, which will
5837 -- usually be more efficient.
5840 New_Alts : constant List_Id := New_List;
5845 Old_Node := First (Plist);
5846 while Present (Old_Node) loop
5847 New_Node := New_Copy (Old_Node);
5849 if Nkind (New_Node) = N_Range then
5850 Set_Low_Bound (New_Node, New_Copy (Low_Bound (Old_Node)));
5851 Set_High_Bound (New_Node, New_Copy (High_Bound (Old_Node)));
5854 Append_To (New_Alts, New_Node);
5858 -- If empty list, replace by False
5860 if Is_Empty_List (New_Alts) then
5861 Rewrite (Expr, New_Occurrence_Of (Standard_False, Loc));
5863 -- Else replace by set membership test
5868 Left_Opnd => Make_Identifier (Loc, Nam),
5869 Right_Opnd => Empty,
5870 Alternatives => New_Alts));
5872 -- Resolve new expression in function context
5874 Install_Formals (Predicate_Function (Typ));
5875 Push_Scope (Predicate_Function (Typ));
5876 Analyze_And_Resolve (Expr, Standard_Boolean);
5882 -- If non-static, return doing nothing
5887 end Build_Static_Predicate;
5889 -----------------------------------------
5890 -- Check_Aspect_At_End_Of_Declarations --
5891 -----------------------------------------
5893 procedure Check_Aspect_At_End_Of_Declarations (ASN : Node_Id) is
5894 Ent : constant Entity_Id := Entity (ASN);
5895 Ident : constant Node_Id := Identifier (ASN);
5897 Freeze_Expr : constant Node_Id := Expression (ASN);
5898 -- Expression from call to Check_Aspect_At_Freeze_Point
5900 End_Decl_Expr : constant Node_Id := Entity (Ident);
5901 -- Expression to be analyzed at end of declarations
5903 T : constant Entity_Id := Etype (Freeze_Expr);
5904 -- Type required for preanalyze call
5906 A_Id : constant Aspect_Id := Get_Aspect_Id (Chars (Ident));
5909 -- Set False if error
5911 -- On entry to this procedure, Entity (Ident) contains a copy of the
5912 -- original expression from the aspect, saved for this purpose, and
5913 -- but Expression (Ident) is a preanalyzed copy of the expression,
5914 -- preanalyzed just after the freeze point.
5917 -- Case of stream attributes, just have to compare entities
5919 if A_Id = Aspect_Input or else
5920 A_Id = Aspect_Output or else
5921 A_Id = Aspect_Read or else
5924 Analyze (End_Decl_Expr);
5925 Err := Entity (End_Decl_Expr) /= Entity (Freeze_Expr);
5927 elsif A_Id = Aspect_Variable_Indexing or else
5928 A_Id = Aspect_Constant_Indexing or else
5929 A_Id = Aspect_Default_Iterator or else
5930 A_Id = Aspect_Iterator_Element
5932 -- Make type unfrozen before analysis, to prevent spurious errors
5933 -- about late attributes.
5935 Set_Is_Frozen (Ent, False);
5936 Analyze (End_Decl_Expr);
5937 Analyze (Aspect_Rep_Item (ASN));
5938 Set_Is_Frozen (Ent, True);
5940 -- If the end of declarations comes before any other freeze
5941 -- point, the Freeze_Expr is not analyzed: no check needed.
5944 Analyzed (Freeze_Expr)
5945 and then not In_Instance
5946 and then Entity (End_Decl_Expr) /= Entity (Freeze_Expr);
5951 -- In a generic context the aspect expressions have not been
5952 -- preanalyzed, so do it now. There are no conformance checks
5953 -- to perform in this case.
5956 Check_Aspect_At_Freeze_Point (ASN);
5959 Preanalyze_Spec_Expression (End_Decl_Expr, T);
5962 Err := not Fully_Conformant_Expressions (End_Decl_Expr, Freeze_Expr);
5965 -- Output error message if error
5969 ("visibility of aspect for& changes after freeze point",
5972 ("?info: & is frozen here, aspects evaluated at this point",
5973 Freeze_Node (Ent), Ent);
5975 end Check_Aspect_At_End_Of_Declarations;
5977 ----------------------------------
5978 -- Check_Aspect_At_Freeze_Point --
5979 ----------------------------------
5981 procedure Check_Aspect_At_Freeze_Point (ASN : Node_Id) is
5982 Ident : constant Node_Id := Identifier (ASN);
5983 -- Identifier (use Entity field to save expression)
5986 -- Type required for preanalyze call
5988 A_Id : constant Aspect_Id := Get_Aspect_Id (Chars (Ident));
5991 -- On entry to this procedure, Entity (Ident) contains a copy of the
5992 -- original expression from the aspect, saved for this purpose.
5994 -- On exit from this procedure Entity (Ident) is unchanged, still
5995 -- containing that copy, but Expression (Ident) is a preanalyzed copy
5996 -- of the expression, preanalyzed just after the freeze point.
5998 -- Make a copy of the expression to be preanalyed
6000 Set_Expression (ASN, New_Copy_Tree (Entity (Ident)));
6002 -- Find type for preanalyze call
6006 -- No_Aspect should be impossible
6009 raise Program_Error;
6011 -- Library unit aspects should be impossible (never delayed)
6013 when Library_Unit_Aspects =>
6014 raise Program_Error;
6016 -- Aspects taking an optional boolean argument. Should be impossible
6017 -- since these are never delayed.
6019 when Boolean_Aspects =>
6020 raise Program_Error;
6022 -- Test_Case aspect applies to entries and subprograms, hence should
6023 -- never be delayed.
6025 when Aspect_Test_Case =>
6026 raise Program_Error;
6028 when Aspect_Attach_Handler =>
6029 T := RTE (RE_Interrupt_ID);
6031 -- Default_Value is resolved with the type entity in question
6033 when Aspect_Default_Value =>
6036 -- Default_Component_Value is resolved with the component type
6038 when Aspect_Default_Component_Value =>
6039 T := Component_Type (Entity (ASN));
6041 -- Aspects corresponding to attribute definition clauses
6043 when Aspect_Address =>
6044 T := RTE (RE_Address);
6046 when Aspect_Bit_Order =>
6047 T := RTE (RE_Bit_Order);
6050 T := RTE (RE_CPU_Range);
6052 when Aspect_Dispatching_Domain =>
6053 T := RTE (RE_Dispatching_Domain);
6055 when Aspect_External_Tag =>
6056 T := Standard_String;
6058 when Aspect_Priority | Aspect_Interrupt_Priority =>
6059 T := Standard_Integer;
6061 when Aspect_Small =>
6062 T := Universal_Real;
6064 when Aspect_Storage_Pool =>
6065 T := Class_Wide_Type (RTE (RE_Root_Storage_Pool));
6067 when Aspect_Alignment |
6068 Aspect_Component_Size |
6069 Aspect_Machine_Radix |
6070 Aspect_Object_Size |
6072 Aspect_Storage_Size |
6073 Aspect_Stream_Size |
6074 Aspect_Value_Size =>
6077 -- Stream attribute. Special case, the expression is just an entity
6078 -- that does not need any resolution, so just analyze.
6084 Analyze (Expression (ASN));
6087 -- Same for Iterator aspects, where the expression is a function
6088 -- name. Legality rules are checked separately.
6090 when Aspect_Constant_Indexing |
6091 Aspect_Default_Iterator |
6092 Aspect_Iterator_Element |
6093 Aspect_Implicit_Dereference |
6094 Aspect_Variable_Indexing =>
6095 Analyze (Expression (ASN));
6098 -- Suppress/Unsuppress/Warnings should never be delayed
6100 when Aspect_Suppress |
6103 raise Program_Error;
6105 -- Pre/Post/Invariant/Predicate take boolean expressions
6107 when Aspect_Dynamic_Predicate |
6110 Aspect_Precondition |
6112 Aspect_Postcondition |
6114 Aspect_Static_Predicate |
6115 Aspect_Type_Invariant =>
6116 T := Standard_Boolean;
6118 when Aspect_Dimension |
6119 Aspect_Dimension_System =>
6120 raise Program_Error;
6124 -- Do the preanalyze call
6126 Preanalyze_Spec_Expression (Expression (ASN), T);
6127 end Check_Aspect_At_Freeze_Point;
6129 -----------------------------------
6130 -- Check_Constant_Address_Clause --
6131 -----------------------------------
6133 procedure Check_Constant_Address_Clause
6137 procedure Check_At_Constant_Address (Nod : Node_Id);
6138 -- Checks that the given node N represents a name whose 'Address is
6139 -- constant (in the same sense as OK_Constant_Address_Clause, i.e. the
6140 -- address value is the same at the point of declaration of U_Ent and at
6141 -- the time of elaboration of the address clause.
6143 procedure Check_Expr_Constants (Nod : Node_Id);
6144 -- Checks that Nod meets the requirements for a constant address clause
6145 -- in the sense of the enclosing procedure.
6147 procedure Check_List_Constants (Lst : List_Id);
6148 -- Check that all elements of list Lst meet the requirements for a
6149 -- constant address clause in the sense of the enclosing procedure.
6151 -------------------------------
6152 -- Check_At_Constant_Address --
6153 -------------------------------
6155 procedure Check_At_Constant_Address (Nod : Node_Id) is
6157 if Is_Entity_Name (Nod) then
6158 if Present (Address_Clause (Entity ((Nod)))) then
6160 ("invalid address clause for initialized object &!",
6163 ("address for& cannot" &
6164 " depend on another address clause! (RM 13.1(22))!",
6167 elsif In_Same_Source_Unit (Entity (Nod), U_Ent)
6168 and then Sloc (U_Ent) < Sloc (Entity (Nod))
6171 ("invalid address clause for initialized object &!",
6173 Error_Msg_Node_2 := U_Ent;
6175 ("\& must be defined before & (RM 13.1(22))!",
6179 elsif Nkind (Nod) = N_Selected_Component then
6181 T : constant Entity_Id := Etype (Prefix (Nod));
6184 if (Is_Record_Type (T)
6185 and then Has_Discriminants (T))
6188 and then Is_Record_Type (Designated_Type (T))
6189 and then Has_Discriminants (Designated_Type (T)))
6192 ("invalid address clause for initialized object &!",
6195 ("\address cannot depend on component" &
6196 " of discriminated record (RM 13.1(22))!",
6199 Check_At_Constant_Address (Prefix (Nod));
6203 elsif Nkind (Nod) = N_Indexed_Component then
6204 Check_At_Constant_Address (Prefix (Nod));
6205 Check_List_Constants (Expressions (Nod));
6208 Check_Expr_Constants (Nod);
6210 end Check_At_Constant_Address;
6212 --------------------------
6213 -- Check_Expr_Constants --
6214 --------------------------
6216 procedure Check_Expr_Constants (Nod : Node_Id) is
6217 Loc_U_Ent : constant Source_Ptr := Sloc (U_Ent);
6218 Ent : Entity_Id := Empty;
6221 if Nkind (Nod) in N_Has_Etype
6222 and then Etype (Nod) = Any_Type
6228 when N_Empty | N_Error =>
6231 when N_Identifier | N_Expanded_Name =>
6232 Ent := Entity (Nod);
6234 -- We need to look at the original node if it is different
6235 -- from the node, since we may have rewritten things and
6236 -- substituted an identifier representing the rewrite.
6238 if Original_Node (Nod) /= Nod then
6239 Check_Expr_Constants (Original_Node (Nod));
6241 -- If the node is an object declaration without initial
6242 -- value, some code has been expanded, and the expression
6243 -- is not constant, even if the constituents might be
6244 -- acceptable, as in A'Address + offset.
6246 if Ekind (Ent) = E_Variable
6248 Nkind (Declaration_Node (Ent)) = N_Object_Declaration
6250 No (Expression (Declaration_Node (Ent)))
6253 ("invalid address clause for initialized object &!",
6256 -- If entity is constant, it may be the result of expanding
6257 -- a check. We must verify that its declaration appears
6258 -- before the object in question, else we also reject the
6261 elsif Ekind (Ent) = E_Constant
6262 and then In_Same_Source_Unit (Ent, U_Ent)
6263 and then Sloc (Ent) > Loc_U_Ent
6266 ("invalid address clause for initialized object &!",
6273 -- Otherwise look at the identifier and see if it is OK
6275 if Ekind_In (Ent, E_Named_Integer, E_Named_Real)
6276 or else Is_Type (Ent)
6281 Ekind (Ent) = E_Constant
6283 Ekind (Ent) = E_In_Parameter
6285 -- This is the case where we must have Ent defined before
6286 -- U_Ent. Clearly if they are in different units this
6287 -- requirement is met since the unit containing Ent is
6288 -- already processed.
6290 if not In_Same_Source_Unit (Ent, U_Ent) then
6293 -- Otherwise location of Ent must be before the location
6294 -- of U_Ent, that's what prior defined means.
6296 elsif Sloc (Ent) < Loc_U_Ent then
6301 ("invalid address clause for initialized object &!",
6303 Error_Msg_Node_2 := U_Ent;
6305 ("\& must be defined before & (RM 13.1(22))!",
6309 elsif Nkind (Original_Node (Nod)) = N_Function_Call then
6310 Check_Expr_Constants (Original_Node (Nod));
6314 ("invalid address clause for initialized object &!",
6317 if Comes_From_Source (Ent) then
6319 ("\reference to variable& not allowed"
6320 & " (RM 13.1(22))!", Nod, Ent);
6323 ("non-static expression not allowed"
6324 & " (RM 13.1(22))!", Nod);
6328 when N_Integer_Literal =>
6330 -- If this is a rewritten unchecked conversion, in a system
6331 -- where Address is an integer type, always use the base type
6332 -- for a literal value. This is user-friendly and prevents
6333 -- order-of-elaboration issues with instances of unchecked
6336 if Nkind (Original_Node (Nod)) = N_Function_Call then
6337 Set_Etype (Nod, Base_Type (Etype (Nod)));
6340 when N_Real_Literal |
6342 N_Character_Literal =>
6346 Check_Expr_Constants (Low_Bound (Nod));
6347 Check_Expr_Constants (High_Bound (Nod));
6349 when N_Explicit_Dereference =>
6350 Check_Expr_Constants (Prefix (Nod));
6352 when N_Indexed_Component =>
6353 Check_Expr_Constants (Prefix (Nod));
6354 Check_List_Constants (Expressions (Nod));
6357 Check_Expr_Constants (Prefix (Nod));
6358 Check_Expr_Constants (Discrete_Range (Nod));
6360 when N_Selected_Component =>
6361 Check_Expr_Constants (Prefix (Nod));
6363 when N_Attribute_Reference =>
6364 if Attribute_Name (Nod) = Name_Address
6366 Attribute_Name (Nod) = Name_Access
6368 Attribute_Name (Nod) = Name_Unchecked_Access
6370 Attribute_Name (Nod) = Name_Unrestricted_Access
6372 Check_At_Constant_Address (Prefix (Nod));
6375 Check_Expr_Constants (Prefix (Nod));
6376 Check_List_Constants (Expressions (Nod));
6380 Check_List_Constants (Component_Associations (Nod));
6381 Check_List_Constants (Expressions (Nod));
6383 when N_Component_Association =>
6384 Check_Expr_Constants (Expression (Nod));
6386 when N_Extension_Aggregate =>
6387 Check_Expr_Constants (Ancestor_Part (Nod));
6388 Check_List_Constants (Component_Associations (Nod));
6389 Check_List_Constants (Expressions (Nod));
6394 when N_Binary_Op | N_Short_Circuit | N_Membership_Test =>
6395 Check_Expr_Constants (Left_Opnd (Nod));
6396 Check_Expr_Constants (Right_Opnd (Nod));
6399 Check_Expr_Constants (Right_Opnd (Nod));
6401 when N_Type_Conversion |
6402 N_Qualified_Expression |
6404 Check_Expr_Constants (Expression (Nod));
6406 when N_Unchecked_Type_Conversion =>
6407 Check_Expr_Constants (Expression (Nod));
6409 -- If this is a rewritten unchecked conversion, subtypes in
6410 -- this node are those created within the instance. To avoid
6411 -- order of elaboration issues, replace them with their base
6412 -- types. Note that address clauses can cause order of
6413 -- elaboration problems because they are elaborated by the
6414 -- back-end at the point of definition, and may mention
6415 -- entities declared in between (as long as everything is
6416 -- static). It is user-friendly to allow unchecked conversions
6419 if Nkind (Original_Node (Nod)) = N_Function_Call then
6420 Set_Etype (Expression (Nod),
6421 Base_Type (Etype (Expression (Nod))));
6422 Set_Etype (Nod, Base_Type (Etype (Nod)));
6425 when N_Function_Call =>
6426 if not Is_Pure (Entity (Name (Nod))) then
6428 ("invalid address clause for initialized object &!",
6432 ("\function & is not pure (RM 13.1(22))!",
6433 Nod, Entity (Name (Nod)));
6436 Check_List_Constants (Parameter_Associations (Nod));
6439 when N_Parameter_Association =>
6440 Check_Expr_Constants (Explicit_Actual_Parameter (Nod));
6444 ("invalid address clause for initialized object &!",
6447 ("\must be constant defined before& (RM 13.1(22))!",
6450 end Check_Expr_Constants;
6452 --------------------------
6453 -- Check_List_Constants --
6454 --------------------------
6456 procedure Check_List_Constants (Lst : List_Id) is
6460 if Present (Lst) then
6461 Nod1 := First (Lst);
6462 while Present (Nod1) loop
6463 Check_Expr_Constants (Nod1);
6467 end Check_List_Constants;
6469 -- Start of processing for Check_Constant_Address_Clause
6472 -- If rep_clauses are to be ignored, no need for legality checks. In
6473 -- particular, no need to pester user about rep clauses that violate
6474 -- the rule on constant addresses, given that these clauses will be
6475 -- removed by Freeze before they reach the back end.
6477 if not Ignore_Rep_Clauses then
6478 Check_Expr_Constants (Expr);
6480 end Check_Constant_Address_Clause;
6482 ----------------------------------------
6483 -- Check_Record_Representation_Clause --
6484 ----------------------------------------
6486 procedure Check_Record_Representation_Clause (N : Node_Id) is
6487 Loc : constant Source_Ptr := Sloc (N);
6488 Ident : constant Node_Id := Identifier (N);
6489 Rectype : Entity_Id;
6494 Hbit : Uint := Uint_0;
6498 Max_Bit_So_Far : Uint;
6499 -- Records the maximum bit position so far. If all field positions
6500 -- are monotonically increasing, then we can skip the circuit for
6501 -- checking for overlap, since no overlap is possible.
6503 Tagged_Parent : Entity_Id := Empty;
6504 -- This is set in the case of a derived tagged type for which we have
6505 -- Is_Fully_Repped_Tagged_Type True (indicating that all components are
6506 -- positioned by record representation clauses). In this case we must
6507 -- check for overlap between components of this tagged type, and the
6508 -- components of its parent. Tagged_Parent will point to this parent
6509 -- type. For all other cases Tagged_Parent is left set to Empty.
6511 Parent_Last_Bit : Uint;
6512 -- Relevant only if Tagged_Parent is set, Parent_Last_Bit indicates the
6513 -- last bit position for any field in the parent type. We only need to
6514 -- check overlap for fields starting below this point.
6516 Overlap_Check_Required : Boolean;
6517 -- Used to keep track of whether or not an overlap check is required
6519 Overlap_Detected : Boolean := False;
6520 -- Set True if an overlap is detected
6522 Ccount : Natural := 0;
6523 -- Number of component clauses in record rep clause
6525 procedure Check_Component_Overlap (C1_Ent, C2_Ent : Entity_Id);
6526 -- Given two entities for record components or discriminants, checks
6527 -- if they have overlapping component clauses and issues errors if so.
6529 procedure Find_Component;
6530 -- Finds component entity corresponding to current component clause (in
6531 -- CC), and sets Comp to the entity, and Fbit/Lbit to the zero origin
6532 -- start/stop bits for the field. If there is no matching component or
6533 -- if the matching component does not have a component clause, then
6534 -- that's an error and Comp is set to Empty, but no error message is
6535 -- issued, since the message was already given. Comp is also set to
6536 -- Empty if the current "component clause" is in fact a pragma.
6538 -----------------------------
6539 -- Check_Component_Overlap --
6540 -----------------------------
6542 procedure Check_Component_Overlap (C1_Ent, C2_Ent : Entity_Id) is
6543 CC1 : constant Node_Id := Component_Clause (C1_Ent);
6544 CC2 : constant Node_Id := Component_Clause (C2_Ent);
6547 if Present (CC1) and then Present (CC2) then
6549 -- Exclude odd case where we have two tag fields in the same
6550 -- record, both at location zero. This seems a bit strange, but
6551 -- it seems to happen in some circumstances, perhaps on an error.
6553 if Chars (C1_Ent) = Name_uTag
6555 Chars (C2_Ent) = Name_uTag
6560 -- Here we check if the two fields overlap
6563 S1 : constant Uint := Component_Bit_Offset (C1_Ent);
6564 S2 : constant Uint := Component_Bit_Offset (C2_Ent);
6565 E1 : constant Uint := S1 + Esize (C1_Ent);
6566 E2 : constant Uint := S2 + Esize (C2_Ent);
6569 if E2 <= S1 or else E1 <= S2 then
6572 Error_Msg_Node_2 := Component_Name (CC2);
6573 Error_Msg_Sloc := Sloc (Error_Msg_Node_2);
6574 Error_Msg_Node_1 := Component_Name (CC1);
6576 ("component& overlaps & #", Component_Name (CC1));
6577 Overlap_Detected := True;
6581 end Check_Component_Overlap;
6583 --------------------
6584 -- Find_Component --
6585 --------------------
6587 procedure Find_Component is
6589 procedure Search_Component (R : Entity_Id);
6590 -- Search components of R for a match. If found, Comp is set.
6592 ----------------------
6593 -- Search_Component --
6594 ----------------------
6596 procedure Search_Component (R : Entity_Id) is
6598 Comp := First_Component_Or_Discriminant (R);
6599 while Present (Comp) loop
6601 -- Ignore error of attribute name for component name (we
6602 -- already gave an error message for this, so no need to
6605 if Nkind (Component_Name (CC)) = N_Attribute_Reference then
6608 exit when Chars (Comp) = Chars (Component_Name (CC));
6611 Next_Component_Or_Discriminant (Comp);
6613 end Search_Component;
6615 -- Start of processing for Find_Component
6618 -- Return with Comp set to Empty if we have a pragma
6620 if Nkind (CC) = N_Pragma then
6625 -- Search current record for matching component
6627 Search_Component (Rectype);
6629 -- If not found, maybe component of base type that is absent from
6630 -- statically constrained first subtype.
6633 Search_Component (Base_Type (Rectype));
6636 -- If no component, or the component does not reference the component
6637 -- clause in question, then there was some previous error for which
6638 -- we already gave a message, so just return with Comp Empty.
6641 or else Component_Clause (Comp) /= CC
6645 -- Normal case where we have a component clause
6648 Fbit := Component_Bit_Offset (Comp);
6649 Lbit := Fbit + Esize (Comp) - 1;
6653 -- Start of processing for Check_Record_Representation_Clause
6657 Rectype := Entity (Ident);
6659 if Rectype = Any_Type then
6662 Rectype := Underlying_Type (Rectype);
6665 -- See if we have a fully repped derived tagged type
6668 PS : constant Entity_Id := Parent_Subtype (Rectype);
6671 if Present (PS) and then Is_Fully_Repped_Tagged_Type (PS) then
6672 Tagged_Parent := PS;
6674 -- Find maximum bit of any component of the parent type
6676 Parent_Last_Bit := UI_From_Int (System_Address_Size - 1);
6677 Pcomp := First_Entity (Tagged_Parent);
6678 while Present (Pcomp) loop
6679 if Ekind_In (Pcomp, E_Discriminant, E_Component) then
6680 if Component_Bit_Offset (Pcomp) /= No_Uint
6681 and then Known_Static_Esize (Pcomp)
6686 Component_Bit_Offset (Pcomp) + Esize (Pcomp) - 1);
6689 Next_Entity (Pcomp);
6695 -- All done if no component clauses
6697 CC := First (Component_Clauses (N));
6703 -- If a tag is present, then create a component clause that places it
6704 -- at the start of the record (otherwise gigi may place it after other
6705 -- fields that have rep clauses).
6707 Fent := First_Entity (Rectype);
6709 if Nkind (Fent) = N_Defining_Identifier
6710 and then Chars (Fent) = Name_uTag
6712 Set_Component_Bit_Offset (Fent, Uint_0);
6713 Set_Normalized_Position (Fent, Uint_0);
6714 Set_Normalized_First_Bit (Fent, Uint_0);
6715 Set_Normalized_Position_Max (Fent, Uint_0);
6716 Init_Esize (Fent, System_Address_Size);
6718 Set_Component_Clause (Fent,
6719 Make_Component_Clause (Loc,
6720 Component_Name => Make_Identifier (Loc, Name_uTag),
6722 Position => Make_Integer_Literal (Loc, Uint_0),
6723 First_Bit => Make_Integer_Literal (Loc, Uint_0),
6725 Make_Integer_Literal (Loc,
6726 UI_From_Int (System_Address_Size))));
6728 Ccount := Ccount + 1;
6731 Max_Bit_So_Far := Uint_Minus_1;
6732 Overlap_Check_Required := False;
6734 -- Process the component clauses
6736 while Present (CC) loop
6739 if Present (Comp) then
6740 Ccount := Ccount + 1;
6742 -- We need a full overlap check if record positions non-monotonic
6744 if Fbit <= Max_Bit_So_Far then
6745 Overlap_Check_Required := True;
6748 Max_Bit_So_Far := Lbit;
6750 -- Check bit position out of range of specified size
6752 if Has_Size_Clause (Rectype)
6753 and then RM_Size (Rectype) <= Lbit
6756 ("bit number out of range of specified size",
6759 -- Check for overlap with tag field
6762 if Is_Tagged_Type (Rectype)
6763 and then Fbit < System_Address_Size
6766 ("component overlaps tag field of&",
6767 Component_Name (CC), Rectype);
6768 Overlap_Detected := True;
6776 -- Check parent overlap if component might overlap parent field
6778 if Present (Tagged_Parent)
6779 and then Fbit <= Parent_Last_Bit
6781 Pcomp := First_Component_Or_Discriminant (Tagged_Parent);
6782 while Present (Pcomp) loop
6783 if not Is_Tag (Pcomp)
6784 and then Chars (Pcomp) /= Name_uParent
6786 Check_Component_Overlap (Comp, Pcomp);
6789 Next_Component_Or_Discriminant (Pcomp);
6797 -- Now that we have processed all the component clauses, check for
6798 -- overlap. We have to leave this till last, since the components can
6799 -- appear in any arbitrary order in the representation clause.
6801 -- We do not need this check if all specified ranges were monotonic,
6802 -- as recorded by Overlap_Check_Required being False at this stage.
6804 -- This first section checks if there are any overlapping entries at
6805 -- all. It does this by sorting all entries and then seeing if there are
6806 -- any overlaps. If there are none, then that is decisive, but if there
6807 -- are overlaps, they may still be OK (they may result from fields in
6808 -- different variants).
6810 if Overlap_Check_Required then
6811 Overlap_Check1 : declare
6813 OC_Fbit : array (0 .. Ccount) of Uint;
6814 -- First-bit values for component clauses, the value is the offset
6815 -- of the first bit of the field from start of record. The zero
6816 -- entry is for use in sorting.
6818 OC_Lbit : array (0 .. Ccount) of Uint;
6819 -- Last-bit values for component clauses, the value is the offset
6820 -- of the last bit of the field from start of record. The zero
6821 -- entry is for use in sorting.
6823 OC_Count : Natural := 0;
6824 -- Count of entries in OC_Fbit and OC_Lbit
6826 function OC_Lt (Op1, Op2 : Natural) return Boolean;
6827 -- Compare routine for Sort
6829 procedure OC_Move (From : Natural; To : Natural);
6830 -- Move routine for Sort
6832 package Sorting is new GNAT.Heap_Sort_G (OC_Move, OC_Lt);
6838 function OC_Lt (Op1, Op2 : Natural) return Boolean is
6840 return OC_Fbit (Op1) < OC_Fbit (Op2);
6847 procedure OC_Move (From : Natural; To : Natural) is
6849 OC_Fbit (To) := OC_Fbit (From);
6850 OC_Lbit (To) := OC_Lbit (From);
6853 -- Start of processing for Overlap_Check
6856 CC := First (Component_Clauses (N));
6857 while Present (CC) loop
6859 -- Exclude component clause already marked in error
6861 if not Error_Posted (CC) then
6864 if Present (Comp) then
6865 OC_Count := OC_Count + 1;
6866 OC_Fbit (OC_Count) := Fbit;
6867 OC_Lbit (OC_Count) := Lbit;
6874 Sorting.Sort (OC_Count);
6876 Overlap_Check_Required := False;
6877 for J in 1 .. OC_Count - 1 loop
6878 if OC_Lbit (J) >= OC_Fbit (J + 1) then
6879 Overlap_Check_Required := True;
6886 -- If Overlap_Check_Required is still True, then we have to do the full
6887 -- scale overlap check, since we have at least two fields that do
6888 -- overlap, and we need to know if that is OK since they are in
6889 -- different variant, or whether we have a definite problem.
6891 if Overlap_Check_Required then
6892 Overlap_Check2 : declare
6893 C1_Ent, C2_Ent : Entity_Id;
6894 -- Entities of components being checked for overlap
6897 -- Component_List node whose Component_Items are being checked
6900 -- Component declaration for component being checked
6903 C1_Ent := First_Entity (Base_Type (Rectype));
6905 -- Loop through all components in record. For each component check
6906 -- for overlap with any of the preceding elements on the component
6907 -- list containing the component and also, if the component is in
6908 -- a variant, check against components outside the case structure.
6909 -- This latter test is repeated recursively up the variant tree.
6911 Main_Component_Loop : while Present (C1_Ent) loop
6912 if not Ekind_In (C1_Ent, E_Component, E_Discriminant) then
6913 goto Continue_Main_Component_Loop;
6916 -- Skip overlap check if entity has no declaration node. This
6917 -- happens with discriminants in constrained derived types.
6918 -- Possibly we are missing some checks as a result, but that
6919 -- does not seem terribly serious.
6921 if No (Declaration_Node (C1_Ent)) then
6922 goto Continue_Main_Component_Loop;
6925 Clist := Parent (List_Containing (Declaration_Node (C1_Ent)));
6927 -- Loop through component lists that need checking. Check the
6928 -- current component list and all lists in variants above us.
6930 Component_List_Loop : loop
6932 -- If derived type definition, go to full declaration
6933 -- If at outer level, check discriminants if there are any.
6935 if Nkind (Clist) = N_Derived_Type_Definition then
6936 Clist := Parent (Clist);
6939 -- Outer level of record definition, check discriminants
6941 if Nkind_In (Clist, N_Full_Type_Declaration,
6942 N_Private_Type_Declaration)
6944 if Has_Discriminants (Defining_Identifier (Clist)) then
6946 First_Discriminant (Defining_Identifier (Clist));
6947 while Present (C2_Ent) loop
6948 exit when C1_Ent = C2_Ent;
6949 Check_Component_Overlap (C1_Ent, C2_Ent);
6950 Next_Discriminant (C2_Ent);
6954 -- Record extension case
6956 elsif Nkind (Clist) = N_Derived_Type_Definition then
6959 -- Otherwise check one component list
6962 Citem := First (Component_Items (Clist));
6963 while Present (Citem) loop
6964 if Nkind (Citem) = N_Component_Declaration then
6965 C2_Ent := Defining_Identifier (Citem);
6966 exit when C1_Ent = C2_Ent;
6967 Check_Component_Overlap (C1_Ent, C2_Ent);
6974 -- Check for variants above us (the parent of the Clist can
6975 -- be a variant, in which case its parent is a variant part,
6976 -- and the parent of the variant part is a component list
6977 -- whose components must all be checked against the current
6978 -- component for overlap).
6980 if Nkind (Parent (Clist)) = N_Variant then
6981 Clist := Parent (Parent (Parent (Clist)));
6983 -- Check for possible discriminant part in record, this
6984 -- is treated essentially as another level in the
6985 -- recursion. For this case the parent of the component
6986 -- list is the record definition, and its parent is the
6987 -- full type declaration containing the discriminant
6990 elsif Nkind (Parent (Clist)) = N_Record_Definition then
6991 Clist := Parent (Parent ((Clist)));
6993 -- If neither of these two cases, we are at the top of
6997 exit Component_List_Loop;
6999 end loop Component_List_Loop;
7001 <<Continue_Main_Component_Loop>>
7002 Next_Entity (C1_Ent);
7004 end loop Main_Component_Loop;
7008 -- The following circuit deals with warning on record holes (gaps). We
7009 -- skip this check if overlap was detected, since it makes sense for the
7010 -- programmer to fix this illegality before worrying about warnings.
7012 if not Overlap_Detected and Warn_On_Record_Holes then
7013 Record_Hole_Check : declare
7014 Decl : constant Node_Id := Declaration_Node (Base_Type (Rectype));
7015 -- Full declaration of record type
7017 procedure Check_Component_List
7021 -- Check component list CL for holes. The starting bit should be
7022 -- Sbit. which is zero for the main record component list and set
7023 -- appropriately for recursive calls for variants. DS is set to
7024 -- a list of discriminant specifications to be included in the
7025 -- consideration of components. It is No_List if none to consider.
7027 --------------------------
7028 -- Check_Component_List --
7029 --------------------------
7031 procedure Check_Component_List
7039 Compl := Integer (List_Length (Component_Items (CL)));
7041 if DS /= No_List then
7042 Compl := Compl + Integer (List_Length (DS));
7046 Comps : array (Natural range 0 .. Compl) of Entity_Id;
7047 -- Gather components (zero entry is for sort routine)
7049 Ncomps : Natural := 0;
7050 -- Number of entries stored in Comps (starting at Comps (1))
7053 -- One component item or discriminant specification
7056 -- Starting bit for next component
7064 function Lt (Op1, Op2 : Natural) return Boolean;
7065 -- Compare routine for Sort
7067 procedure Move (From : Natural; To : Natural);
7068 -- Move routine for Sort
7070 package Sorting is new GNAT.Heap_Sort_G (Move, Lt);
7076 function Lt (Op1, Op2 : Natural) return Boolean is
7078 return Component_Bit_Offset (Comps (Op1))
7080 Component_Bit_Offset (Comps (Op2));
7087 procedure Move (From : Natural; To : Natural) is
7089 Comps (To) := Comps (From);
7093 -- Gather discriminants into Comp
7095 if DS /= No_List then
7096 Citem := First (DS);
7097 while Present (Citem) loop
7098 if Nkind (Citem) = N_Discriminant_Specification then
7100 Ent : constant Entity_Id :=
7101 Defining_Identifier (Citem);
7103 if Ekind (Ent) = E_Discriminant then
7104 Ncomps := Ncomps + 1;
7105 Comps (Ncomps) := Ent;
7114 -- Gather component entities into Comp
7116 Citem := First (Component_Items (CL));
7117 while Present (Citem) loop
7118 if Nkind (Citem) = N_Component_Declaration then
7119 Ncomps := Ncomps + 1;
7120 Comps (Ncomps) := Defining_Identifier (Citem);
7126 -- Now sort the component entities based on the first bit.
7127 -- Note we already know there are no overlapping components.
7129 Sorting.Sort (Ncomps);
7131 -- Loop through entries checking for holes
7134 for J in 1 .. Ncomps loop
7136 Error_Msg_Uint_1 := Component_Bit_Offset (CEnt) - Nbit;
7138 if Error_Msg_Uint_1 > 0 then
7140 ("?^-bit gap before component&",
7141 Component_Name (Component_Clause (CEnt)), CEnt);
7144 Nbit := Component_Bit_Offset (CEnt) + Esize (CEnt);
7147 -- Process variant parts recursively if present
7149 if Present (Variant_Part (CL)) then
7150 Variant := First (Variants (Variant_Part (CL)));
7151 while Present (Variant) loop
7152 Check_Component_List
7153 (Component_List (Variant), Nbit, No_List);
7158 end Check_Component_List;
7160 -- Start of processing for Record_Hole_Check
7167 if Is_Tagged_Type (Rectype) then
7168 Sbit := UI_From_Int (System_Address_Size);
7173 if Nkind (Decl) = N_Full_Type_Declaration
7174 and then Nkind (Type_Definition (Decl)) = N_Record_Definition
7176 Check_Component_List
7177 (Component_List (Type_Definition (Decl)),
7179 Discriminant_Specifications (Decl));
7182 end Record_Hole_Check;
7185 -- For records that have component clauses for all components, and whose
7186 -- size is less than or equal to 32, we need to know the size in the
7187 -- front end to activate possible packed array processing where the
7188 -- component type is a record.
7190 -- At this stage Hbit + 1 represents the first unused bit from all the
7191 -- component clauses processed, so if the component clauses are
7192 -- complete, then this is the length of the record.
7194 -- For records longer than System.Storage_Unit, and for those where not
7195 -- all components have component clauses, the back end determines the
7196 -- length (it may for example be appropriate to round up the size
7197 -- to some convenient boundary, based on alignment considerations, etc).
7199 if Unknown_RM_Size (Rectype) and then Hbit + 1 <= 32 then
7201 -- Nothing to do if at least one component has no component clause
7203 Comp := First_Component_Or_Discriminant (Rectype);
7204 while Present (Comp) loop
7205 exit when No (Component_Clause (Comp));
7206 Next_Component_Or_Discriminant (Comp);
7209 -- If we fall out of loop, all components have component clauses
7210 -- and so we can set the size to the maximum value.
7213 Set_RM_Size (Rectype, Hbit + 1);
7216 end Check_Record_Representation_Clause;
7222 procedure Check_Size
7226 Biased : out Boolean)
7228 UT : constant Entity_Id := Underlying_Type (T);
7234 -- Dismiss cases for generic types or types with previous errors
7237 or else UT = Any_Type
7238 or else Is_Generic_Type (UT)
7239 or else Is_Generic_Type (Root_Type (UT))
7243 -- Check case of bit packed array
7245 elsif Is_Array_Type (UT)
7246 and then Known_Static_Component_Size (UT)
7247 and then Is_Bit_Packed_Array (UT)
7255 Asiz := Component_Size (UT);
7256 Indx := First_Index (UT);
7258 Ityp := Etype (Indx);
7260 -- If non-static bound, then we are not in the business of
7261 -- trying to check the length, and indeed an error will be
7262 -- issued elsewhere, since sizes of non-static array types
7263 -- cannot be set implicitly or explicitly.
7265 if not Is_Static_Subtype (Ityp) then
7269 -- Otherwise accumulate next dimension
7271 Asiz := Asiz * (Expr_Value (Type_High_Bound (Ityp)) -
7272 Expr_Value (Type_Low_Bound (Ityp)) +
7276 exit when No (Indx);
7282 Error_Msg_Uint_1 := Asiz;
7284 ("size for& too small, minimum allowed is ^", N, T);
7285 Set_Esize (T, Asiz);
7286 Set_RM_Size (T, Asiz);
7290 -- All other composite types are ignored
7292 elsif Is_Composite_Type (UT) then
7295 -- For fixed-point types, don't check minimum if type is not frozen,
7296 -- since we don't know all the characteristics of the type that can
7297 -- affect the size (e.g. a specified small) till freeze time.
7299 elsif Is_Fixed_Point_Type (UT)
7300 and then not Is_Frozen (UT)
7304 -- Cases for which a minimum check is required
7307 -- Ignore if specified size is correct for the type
7309 if Known_Esize (UT) and then Siz = Esize (UT) then
7313 -- Otherwise get minimum size
7315 M := UI_From_Int (Minimum_Size (UT));
7319 -- Size is less than minimum size, but one possibility remains
7320 -- that we can manage with the new size if we bias the type.
7322 M := UI_From_Int (Minimum_Size (UT, Biased => True));
7325 Error_Msg_Uint_1 := M;
7327 ("size for& too small, minimum allowed is ^", N, T);
7337 -------------------------
7338 -- Get_Alignment_Value --
7339 -------------------------
7341 function Get_Alignment_Value (Expr : Node_Id) return Uint is
7342 Align : constant Uint := Static_Integer (Expr);
7345 if Align = No_Uint then
7348 elsif Align <= 0 then
7349 Error_Msg_N ("alignment value must be positive", Expr);
7353 for J in Int range 0 .. 64 loop
7355 M : constant Uint := Uint_2 ** J;
7358 exit when M = Align;
7362 ("alignment value must be power of 2", Expr);
7370 end Get_Alignment_Value;
7376 procedure Initialize is
7378 Address_Clause_Checks.Init;
7379 Independence_Checks.Init;
7380 Unchecked_Conversions.Init;
7383 -------------------------
7384 -- Is_Operational_Item --
7385 -------------------------
7387 function Is_Operational_Item (N : Node_Id) return Boolean is
7389 if Nkind (N) /= N_Attribute_Definition_Clause then
7393 Id : constant Attribute_Id := Get_Attribute_Id (Chars (N));
7395 return Id = Attribute_Input
7396 or else Id = Attribute_Output
7397 or else Id = Attribute_Read
7398 or else Id = Attribute_Write
7399 or else Id = Attribute_External_Tag;
7402 end Is_Operational_Item;
7408 function Minimum_Size
7410 Biased : Boolean := False) return Nat
7412 Lo : Uint := No_Uint;
7413 Hi : Uint := No_Uint;
7414 LoR : Ureal := No_Ureal;
7415 HiR : Ureal := No_Ureal;
7416 LoSet : Boolean := False;
7417 HiSet : Boolean := False;
7421 R_Typ : constant Entity_Id := Root_Type (T);
7424 -- If bad type, return 0
7426 if T = Any_Type then
7429 -- For generic types, just return zero. There cannot be any legitimate
7430 -- need to know such a size, but this routine may be called with a
7431 -- generic type as part of normal processing.
7433 elsif Is_Generic_Type (R_Typ)
7434 or else R_Typ = Any_Type
7438 -- Access types. Normally an access type cannot have a size smaller
7439 -- than the size of System.Address. The exception is on VMS, where
7440 -- we have short and long addresses, and it is possible for an access
7441 -- type to have a short address size (and thus be less than the size
7442 -- of System.Address itself). We simply skip the check for VMS, and
7443 -- leave it to the back end to do the check.
7445 elsif Is_Access_Type (T) then
7446 if OpenVMS_On_Target then
7449 return System_Address_Size;
7452 -- Floating-point types
7454 elsif Is_Floating_Point_Type (T) then
7455 return UI_To_Int (Esize (R_Typ));
7459 elsif Is_Discrete_Type (T) then
7461 -- The following loop is looking for the nearest compile time known
7462 -- bounds following the ancestor subtype chain. The idea is to find
7463 -- the most restrictive known bounds information.
7467 if Ancest = Any_Type or else Etype (Ancest) = Any_Type then
7472 if Compile_Time_Known_Value (Type_Low_Bound (Ancest)) then
7473 Lo := Expr_Rep_Value (Type_Low_Bound (Ancest));
7480 if Compile_Time_Known_Value (Type_High_Bound (Ancest)) then
7481 Hi := Expr_Rep_Value (Type_High_Bound (Ancest));
7487 Ancest := Ancestor_Subtype (Ancest);
7490 Ancest := Base_Type (T);
7492 if Is_Generic_Type (Ancest) then
7498 -- Fixed-point types. We can't simply use Expr_Value to get the
7499 -- Corresponding_Integer_Value values of the bounds, since these do not
7500 -- get set till the type is frozen, and this routine can be called
7501 -- before the type is frozen. Similarly the test for bounds being static
7502 -- needs to include the case where we have unanalyzed real literals for
7505 elsif Is_Fixed_Point_Type (T) then
7507 -- The following loop is looking for the nearest compile time known
7508 -- bounds following the ancestor subtype chain. The idea is to find
7509 -- the most restrictive known bounds information.
7513 if Ancest = Any_Type or else Etype (Ancest) = Any_Type then
7517 -- Note: In the following two tests for LoSet and HiSet, it may
7518 -- seem redundant to test for N_Real_Literal here since normally
7519 -- one would assume that the test for the value being known at
7520 -- compile time includes this case. However, there is a glitch.
7521 -- If the real literal comes from folding a non-static expression,
7522 -- then we don't consider any non- static expression to be known
7523 -- at compile time if we are in configurable run time mode (needed
7524 -- in some cases to give a clearer definition of what is and what
7525 -- is not accepted). So the test is indeed needed. Without it, we
7526 -- would set neither Lo_Set nor Hi_Set and get an infinite loop.
7529 if Nkind (Type_Low_Bound (Ancest)) = N_Real_Literal
7530 or else Compile_Time_Known_Value (Type_Low_Bound (Ancest))
7532 LoR := Expr_Value_R (Type_Low_Bound (Ancest));
7539 if Nkind (Type_High_Bound (Ancest)) = N_Real_Literal
7540 or else Compile_Time_Known_Value (Type_High_Bound (Ancest))
7542 HiR := Expr_Value_R (Type_High_Bound (Ancest));
7548 Ancest := Ancestor_Subtype (Ancest);
7551 Ancest := Base_Type (T);
7553 if Is_Generic_Type (Ancest) then
7559 Lo := UR_To_Uint (LoR / Small_Value (T));
7560 Hi := UR_To_Uint (HiR / Small_Value (T));
7562 -- No other types allowed
7565 raise Program_Error;
7568 -- Fall through with Hi and Lo set. Deal with biased case
7571 and then not Is_Fixed_Point_Type (T)
7572 and then not (Is_Enumeration_Type (T)
7573 and then Has_Non_Standard_Rep (T)))
7574 or else Has_Biased_Representation (T)
7580 -- Signed case. Note that we consider types like range 1 .. -1 to be
7581 -- signed for the purpose of computing the size, since the bounds have
7582 -- to be accommodated in the base type.
7584 if Lo < 0 or else Hi < 0 then
7588 -- S = size, B = 2 ** (size - 1) (can accommodate -B .. +(B - 1))
7589 -- Note that we accommodate the case where the bounds cross. This
7590 -- can happen either because of the way the bounds are declared
7591 -- or because of the algorithm in Freeze_Fixed_Point_Type.
7605 -- If both bounds are positive, make sure that both are represen-
7606 -- table in the case where the bounds are crossed. This can happen
7607 -- either because of the way the bounds are declared, or because of
7608 -- the algorithm in Freeze_Fixed_Point_Type.
7614 -- S = size, (can accommodate 0 .. (2**size - 1))
7617 while Hi >= Uint_2 ** S loop
7625 ---------------------------
7626 -- New_Stream_Subprogram --
7627 ---------------------------
7629 procedure New_Stream_Subprogram
7633 Nam : TSS_Name_Type)
7635 Loc : constant Source_Ptr := Sloc (N);
7636 Sname : constant Name_Id := Make_TSS_Name (Base_Type (Ent), Nam);
7637 Subp_Id : Entity_Id;
7638 Subp_Decl : Node_Id;
7642 Defer_Declaration : constant Boolean :=
7643 Is_Tagged_Type (Ent) or else Is_Private_Type (Ent);
7644 -- For a tagged type, there is a declaration for each stream attribute
7645 -- at the freeze point, and we must generate only a completion of this
7646 -- declaration. We do the same for private types, because the full view
7647 -- might be tagged. Otherwise we generate a declaration at the point of
7648 -- the attribute definition clause.
7650 function Build_Spec return Node_Id;
7651 -- Used for declaration and renaming declaration, so that this is
7652 -- treated as a renaming_as_body.
7658 function Build_Spec return Node_Id is
7659 Out_P : constant Boolean := (Nam = TSS_Stream_Read);
7662 T_Ref : constant Node_Id := New_Reference_To (Etyp, Loc);
7665 Subp_Id := Make_Defining_Identifier (Loc, Sname);
7667 -- S : access Root_Stream_Type'Class
7669 Formals := New_List (
7670 Make_Parameter_Specification (Loc,
7671 Defining_Identifier =>
7672 Make_Defining_Identifier (Loc, Name_S),
7674 Make_Access_Definition (Loc,
7677 Designated_Type (Etype (F)), Loc))));
7679 if Nam = TSS_Stream_Input then
7680 Spec := Make_Function_Specification (Loc,
7681 Defining_Unit_Name => Subp_Id,
7682 Parameter_Specifications => Formals,
7683 Result_Definition => T_Ref);
7688 Make_Parameter_Specification (Loc,
7689 Defining_Identifier => Make_Defining_Identifier (Loc, Name_V),
7690 Out_Present => Out_P,
7691 Parameter_Type => T_Ref));
7694 Make_Procedure_Specification (Loc,
7695 Defining_Unit_Name => Subp_Id,
7696 Parameter_Specifications => Formals);
7702 -- Start of processing for New_Stream_Subprogram
7705 F := First_Formal (Subp);
7707 if Ekind (Subp) = E_Procedure then
7708 Etyp := Etype (Next_Formal (F));
7710 Etyp := Etype (Subp);
7713 -- Prepare subprogram declaration and insert it as an action on the
7714 -- clause node. The visibility for this entity is used to test for
7715 -- visibility of the attribute definition clause (in the sense of
7716 -- 8.3(23) as amended by AI-195).
7718 if not Defer_Declaration then
7720 Make_Subprogram_Declaration (Loc,
7721 Specification => Build_Spec);
7723 -- For a tagged type, there is always a visible declaration for each
7724 -- stream TSS (it is a predefined primitive operation), and the
7725 -- completion of this declaration occurs at the freeze point, which is
7726 -- not always visible at places where the attribute definition clause is
7727 -- visible. So, we create a dummy entity here for the purpose of
7728 -- tracking the visibility of the attribute definition clause itself.
7732 Make_Defining_Identifier (Loc, New_External_Name (Sname, 'V'));
7734 Make_Object_Declaration (Loc,
7735 Defining_Identifier => Subp_Id,
7736 Object_Definition => New_Occurrence_Of (Standard_Boolean, Loc));
7739 Insert_Action (N, Subp_Decl);
7740 Set_Entity (N, Subp_Id);
7743 Make_Subprogram_Renaming_Declaration (Loc,
7744 Specification => Build_Spec,
7745 Name => New_Reference_To (Subp, Loc));
7747 if Defer_Declaration then
7748 Set_TSS (Base_Type (Ent), Subp_Id);
7750 Insert_Action (N, Subp_Decl);
7751 Copy_TSS (Subp_Id, Base_Type (Ent));
7753 end New_Stream_Subprogram;
7755 ------------------------
7756 -- Rep_Item_Too_Early --
7757 ------------------------
7759 function Rep_Item_Too_Early (T : Entity_Id; N : Node_Id) return Boolean is
7761 -- Cannot apply non-operational rep items to generic types
7763 if Is_Operational_Item (N) then
7767 and then Is_Generic_Type (Root_Type (T))
7769 Error_Msg_N ("representation item not allowed for generic type", N);
7773 -- Otherwise check for incomplete type
7775 if Is_Incomplete_Or_Private_Type (T)
7776 and then No (Underlying_Type (T))
7778 (Nkind (N) /= N_Pragma
7779 or else Get_Pragma_Id (N) /= Pragma_Import)
7782 ("representation item must be after full type declaration", N);
7785 -- If the type has incomplete components, a representation clause is
7786 -- illegal but stream attributes and Convention pragmas are correct.
7788 elsif Has_Private_Component (T) then
7789 if Nkind (N) = N_Pragma then
7793 ("representation item must appear after type is fully defined",
7800 end Rep_Item_Too_Early;
7802 -----------------------
7803 -- Rep_Item_Too_Late --
7804 -----------------------
7806 function Rep_Item_Too_Late
7809 FOnly : Boolean := False) return Boolean
7812 Parent_Type : Entity_Id;
7815 -- Output the too late message. Note that this is not considered a
7816 -- serious error, since the effect is simply that we ignore the
7817 -- representation clause in this case.
7823 procedure Too_Late is
7825 Error_Msg_N ("|representation item appears too late!", N);
7828 -- Start of processing for Rep_Item_Too_Late
7831 -- First make sure entity is not frozen (RM 13.1(9))
7835 -- Exclude imported types, which may be frozen if they appear in a
7836 -- representation clause for a local type.
7838 and then not From_With_Type (T)
7840 -- Exclude generated entitiesa (not coming from source). The common
7841 -- case is when we generate a renaming which prematurely freezes the
7842 -- renamed internal entity, but we still want to be able to set copies
7843 -- of attribute values such as Size/Alignment.
7845 and then Comes_From_Source (T)
7848 S := First_Subtype (T);
7850 if Present (Freeze_Node (S)) then
7852 ("?no more representation items for }", Freeze_Node (S), S);
7857 -- Check for case of non-tagged derived type whose parent either has
7858 -- primitive operations, or is a by reference type (RM 13.1(10)).
7862 and then Is_Derived_Type (T)
7863 and then not Is_Tagged_Type (T)
7865 Parent_Type := Etype (Base_Type (T));
7867 if Has_Primitive_Operations (Parent_Type) then
7870 ("primitive operations already defined for&!", N, Parent_Type);
7873 elsif Is_By_Reference_Type (Parent_Type) then
7876 ("parent type & is a by reference type!", N, Parent_Type);
7881 -- No error, link item into head of chain of rep items for the entity,
7882 -- but avoid chaining if we have an overloadable entity, and the pragma
7883 -- is one that can apply to multiple overloaded entities.
7885 if Is_Overloadable (T)
7886 and then Nkind (N) = N_Pragma
7889 Pname : constant Name_Id := Pragma_Name (N);
7891 if Pname = Name_Convention or else
7892 Pname = Name_Import or else
7893 Pname = Name_Export or else
7894 Pname = Name_External or else
7895 Pname = Name_Interface
7902 Record_Rep_Item (T, N);
7904 end Rep_Item_Too_Late;
7906 -------------------------------------
7907 -- Replace_Type_References_Generic --
7908 -------------------------------------
7910 procedure Replace_Type_References_Generic (N : Node_Id; TName : Name_Id) is
7912 function Replace_Node (N : Node_Id) return Traverse_Result;
7913 -- Processes a single node in the traversal procedure below, checking
7914 -- if node N should be replaced, and if so, doing the replacement.
7916 procedure Replace_Type_Refs is new Traverse_Proc (Replace_Node);
7917 -- This instantiation provides the body of Replace_Type_References
7923 function Replace_Node (N : Node_Id) return Traverse_Result is
7928 -- Case of identifier
7930 if Nkind (N) = N_Identifier then
7932 -- If not the type name, all done with this node
7934 if Chars (N) /= TName then
7937 -- Otherwise do the replacement and we are done with this node
7940 Replace_Type_Reference (N);
7944 -- Case of selected component (which is what a qualification
7945 -- looks like in the unanalyzed tree, which is what we have.
7947 elsif Nkind (N) = N_Selected_Component then
7949 -- If selector name is not our type, keeping going (we might
7950 -- still have an occurrence of the type in the prefix).
7952 if Nkind (Selector_Name (N)) /= N_Identifier
7953 or else Chars (Selector_Name (N)) /= TName
7957 -- Selector name is our type, check qualification
7960 -- Loop through scopes and prefixes, doing comparison
7965 -- Continue if no more scopes or scope with no name
7967 if No (S) or else Nkind (S) not in N_Has_Chars then
7971 -- Do replace if prefix is an identifier matching the
7972 -- scope that we are currently looking at.
7974 if Nkind (P) = N_Identifier
7975 and then Chars (P) = Chars (S)
7977 Replace_Type_Reference (N);
7981 -- Go check scope above us if prefix is itself of the
7982 -- form of a selected component, whose selector matches
7983 -- the scope we are currently looking at.
7985 if Nkind (P) = N_Selected_Component
7986 and then Nkind (Selector_Name (P)) = N_Identifier
7987 and then Chars (Selector_Name (P)) = Chars (S)
7992 -- For anything else, we don't have a match, so keep on
7993 -- going, there are still some weird cases where we may
7994 -- still have a replacement within the prefix.
8002 -- Continue for any other node kind
8010 Replace_Type_Refs (N);
8011 end Replace_Type_References_Generic;
8013 -------------------------
8014 -- Same_Representation --
8015 -------------------------
8017 function Same_Representation (Typ1, Typ2 : Entity_Id) return Boolean is
8018 T1 : constant Entity_Id := Underlying_Type (Typ1);
8019 T2 : constant Entity_Id := Underlying_Type (Typ2);
8022 -- A quick check, if base types are the same, then we definitely have
8023 -- the same representation, because the subtype specific representation
8024 -- attributes (Size and Alignment) do not affect representation from
8025 -- the point of view of this test.
8027 if Base_Type (T1) = Base_Type (T2) then
8030 elsif Is_Private_Type (Base_Type (T2))
8031 and then Base_Type (T1) = Full_View (Base_Type (T2))
8036 -- Tagged types never have differing representations
8038 if Is_Tagged_Type (T1) then
8042 -- Representations are definitely different if conventions differ
8044 if Convention (T1) /= Convention (T2) then
8048 -- Representations are different if component alignments differ
8050 if (Is_Record_Type (T1) or else Is_Array_Type (T1))
8052 (Is_Record_Type (T2) or else Is_Array_Type (T2))
8053 and then Component_Alignment (T1) /= Component_Alignment (T2)
8058 -- For arrays, the only real issue is component size. If we know the
8059 -- component size for both arrays, and it is the same, then that's
8060 -- good enough to know we don't have a change of representation.
8062 if Is_Array_Type (T1) then
8063 if Known_Component_Size (T1)
8064 and then Known_Component_Size (T2)
8065 and then Component_Size (T1) = Component_Size (T2)
8067 if VM_Target = No_VM then
8070 -- In VM targets the representation of arrays with aliased
8071 -- components differs from arrays with non-aliased components
8074 return Has_Aliased_Components (Base_Type (T1))
8076 Has_Aliased_Components (Base_Type (T2));
8081 -- Types definitely have same representation if neither has non-standard
8082 -- representation since default representations are always consistent.
8083 -- If only one has non-standard representation, and the other does not,
8084 -- then we consider that they do not have the same representation. They
8085 -- might, but there is no way of telling early enough.
8087 if Has_Non_Standard_Rep (T1) then
8088 if not Has_Non_Standard_Rep (T2) then
8092 return not Has_Non_Standard_Rep (T2);
8095 -- Here the two types both have non-standard representation, and we need
8096 -- to determine if they have the same non-standard representation.
8098 -- For arrays, we simply need to test if the component sizes are the
8099 -- same. Pragma Pack is reflected in modified component sizes, so this
8100 -- check also deals with pragma Pack.
8102 if Is_Array_Type (T1) then
8103 return Component_Size (T1) = Component_Size (T2);
8105 -- Tagged types always have the same representation, because it is not
8106 -- possible to specify different representations for common fields.
8108 elsif Is_Tagged_Type (T1) then
8111 -- Case of record types
8113 elsif Is_Record_Type (T1) then
8115 -- Packed status must conform
8117 if Is_Packed (T1) /= Is_Packed (T2) then
8120 -- Otherwise we must check components. Typ2 maybe a constrained
8121 -- subtype with fewer components, so we compare the components
8122 -- of the base types.
8125 Record_Case : declare
8126 CD1, CD2 : Entity_Id;
8128 function Same_Rep return Boolean;
8129 -- CD1 and CD2 are either components or discriminants. This
8130 -- function tests whether the two have the same representation
8136 function Same_Rep return Boolean is
8138 if No (Component_Clause (CD1)) then
8139 return No (Component_Clause (CD2));
8143 Present (Component_Clause (CD2))
8145 Component_Bit_Offset (CD1) = Component_Bit_Offset (CD2)
8147 Esize (CD1) = Esize (CD2);
8151 -- Start of processing for Record_Case
8154 if Has_Discriminants (T1) then
8155 CD1 := First_Discriminant (T1);
8156 CD2 := First_Discriminant (T2);
8158 -- The number of discriminants may be different if the
8159 -- derived type has fewer (constrained by values). The
8160 -- invisible discriminants retain the representation of
8161 -- the original, so the discrepancy does not per se
8162 -- indicate a different representation.
8165 and then Present (CD2)
8167 if not Same_Rep then
8170 Next_Discriminant (CD1);
8171 Next_Discriminant (CD2);
8176 CD1 := First_Component (Underlying_Type (Base_Type (T1)));
8177 CD2 := First_Component (Underlying_Type (Base_Type (T2)));
8179 while Present (CD1) loop
8180 if not Same_Rep then
8183 Next_Component (CD1);
8184 Next_Component (CD2);
8192 -- For enumeration types, we must check each literal to see if the
8193 -- representation is the same. Note that we do not permit enumeration
8194 -- representation clauses for Character and Wide_Character, so these
8195 -- cases were already dealt with.
8197 elsif Is_Enumeration_Type (T1) then
8198 Enumeration_Case : declare
8202 L1 := First_Literal (T1);
8203 L2 := First_Literal (T2);
8205 while Present (L1) loop
8206 if Enumeration_Rep (L1) /= Enumeration_Rep (L2) then
8216 end Enumeration_Case;
8218 -- Any other types have the same representation for these purposes
8223 end Same_Representation;
8229 procedure Set_Biased
8233 Biased : Boolean := True)
8237 Set_Has_Biased_Representation (E);
8239 if Warn_On_Biased_Representation then
8241 ("?" & Msg & " forces biased representation for&", N, E);
8246 --------------------
8247 -- Set_Enum_Esize --
8248 --------------------
8250 procedure Set_Enum_Esize (T : Entity_Id) is
8258 -- Find the minimum standard size (8,16,32,64) that fits
8260 Lo := Enumeration_Rep (Entity (Type_Low_Bound (T)));
8261 Hi := Enumeration_Rep (Entity (Type_High_Bound (T)));
8264 if Lo >= -Uint_2**07 and then Hi < Uint_2**07 then
8265 Sz := Standard_Character_Size; -- May be > 8 on some targets
8267 elsif Lo >= -Uint_2**15 and then Hi < Uint_2**15 then
8270 elsif Lo >= -Uint_2**31 and then Hi < Uint_2**31 then
8273 else pragma Assert (Lo >= -Uint_2**63 and then Hi < Uint_2**63);
8278 if Hi < Uint_2**08 then
8279 Sz := Standard_Character_Size; -- May be > 8 on some targets
8281 elsif Hi < Uint_2**16 then
8284 elsif Hi < Uint_2**32 then
8287 else pragma Assert (Hi < Uint_2**63);
8292 -- That minimum is the proper size unless we have a foreign convention
8293 -- and the size required is 32 or less, in which case we bump the size
8294 -- up to 32. This is required for C and C++ and seems reasonable for
8295 -- all other foreign conventions.
8297 if Has_Foreign_Convention (T)
8298 and then Esize (T) < Standard_Integer_Size
8300 Init_Esize (T, Standard_Integer_Size);
8306 ------------------------------
8307 -- Validate_Address_Clauses --
8308 ------------------------------
8310 procedure Validate_Address_Clauses is
8312 for J in Address_Clause_Checks.First .. Address_Clause_Checks.Last loop
8314 ACCR : Address_Clause_Check_Record
8315 renames Address_Clause_Checks.Table (J);
8326 -- Skip processing of this entry if warning already posted
8328 if not Address_Warning_Posted (ACCR.N) then
8330 Expr := Original_Node (Expression (ACCR.N));
8334 X_Alignment := Alignment (ACCR.X);
8335 Y_Alignment := Alignment (ACCR.Y);
8337 -- Similarly obtain sizes
8339 X_Size := Esize (ACCR.X);
8340 Y_Size := Esize (ACCR.Y);
8342 -- Check for large object overlaying smaller one
8345 and then X_Size > Uint_0
8346 and then X_Size > Y_Size
8349 ("?& overlays smaller object", ACCR.N, ACCR.X);
8351 ("\?program execution may be erroneous", ACCR.N);
8352 Error_Msg_Uint_1 := X_Size;
8354 ("\?size of & is ^", ACCR.N, ACCR.X);
8355 Error_Msg_Uint_1 := Y_Size;
8357 ("\?size of & is ^", ACCR.N, ACCR.Y);
8359 -- Check for inadequate alignment, both of the base object
8360 -- and of the offset, if any.
8362 -- Note: we do not check the alignment if we gave a size
8363 -- warning, since it would likely be redundant.
8365 elsif Y_Alignment /= Uint_0
8366 and then (Y_Alignment < X_Alignment
8369 Nkind (Expr) = N_Attribute_Reference
8371 Attribute_Name (Expr) = Name_Address
8373 Has_Compatible_Alignment
8374 (ACCR.X, Prefix (Expr))
8375 /= Known_Compatible))
8378 ("?specified address for& may be inconsistent "
8382 ("\?program execution may be erroneous (RM 13.3(27))",
8384 Error_Msg_Uint_1 := X_Alignment;
8386 ("\?alignment of & is ^",
8388 Error_Msg_Uint_1 := Y_Alignment;
8390 ("\?alignment of & is ^",
8392 if Y_Alignment >= X_Alignment then
8394 ("\?but offset is not multiple of alignment",
8401 end Validate_Address_Clauses;
8403 ---------------------------
8404 -- Validate_Independence --
8405 ---------------------------
8407 procedure Validate_Independence is
8408 SU : constant Uint := UI_From_Int (System_Storage_Unit);
8416 procedure Check_Array_Type (Atyp : Entity_Id);
8417 -- Checks if the array type Atyp has independent components, and
8418 -- if not, outputs an appropriate set of error messages.
8420 procedure No_Independence;
8421 -- Output message that independence cannot be guaranteed
8423 function OK_Component (C : Entity_Id) return Boolean;
8424 -- Checks one component to see if it is independently accessible, and
8425 -- if so yields True, otherwise yields False if independent access
8426 -- cannot be guaranteed. This is a conservative routine, it only
8427 -- returns True if it knows for sure, it returns False if it knows
8428 -- there is a problem, or it cannot be sure there is no problem.
8430 procedure Reason_Bad_Component (C : Entity_Id);
8431 -- Outputs continuation message if a reason can be determined for
8432 -- the component C being bad.
8434 ----------------------
8435 -- Check_Array_Type --
8436 ----------------------
8438 procedure Check_Array_Type (Atyp : Entity_Id) is
8439 Ctyp : constant Entity_Id := Component_Type (Atyp);
8442 -- OK if no alignment clause, no pack, and no component size
8444 if not Has_Component_Size_Clause (Atyp)
8445 and then not Has_Alignment_Clause (Atyp)
8446 and then not Is_Packed (Atyp)
8451 -- Check actual component size
8453 if not Known_Component_Size (Atyp)
8454 or else not (Addressable (Component_Size (Atyp))
8455 and then Component_Size (Atyp) < 64)
8456 or else Component_Size (Atyp) mod Esize (Ctyp) /= 0
8460 -- Bad component size, check reason
8462 if Has_Component_Size_Clause (Atyp) then
8464 Get_Attribute_Definition_Clause
8465 (Atyp, Attribute_Component_Size);
8468 Error_Msg_Sloc := Sloc (P);
8469 Error_Msg_N ("\because of Component_Size clause#", N);
8474 if Is_Packed (Atyp) then
8475 P := Get_Rep_Pragma (Atyp, Name_Pack);
8478 Error_Msg_Sloc := Sloc (P);
8479 Error_Msg_N ("\because of pragma Pack#", N);
8484 -- No reason found, just return
8489 -- Array type is OK independence-wise
8492 end Check_Array_Type;
8494 ---------------------
8495 -- No_Independence --
8496 ---------------------
8498 procedure No_Independence is
8500 if Pragma_Name (N) = Name_Independent then
8502 ("independence cannot be guaranteed for&", N, E);
8505 ("independent components cannot be guaranteed for&", N, E);
8507 end No_Independence;
8513 function OK_Component (C : Entity_Id) return Boolean is
8514 Rec : constant Entity_Id := Scope (C);
8515 Ctyp : constant Entity_Id := Etype (C);
8518 -- OK if no component clause, no Pack, and no alignment clause
8520 if No (Component_Clause (C))
8521 and then not Is_Packed (Rec)
8522 and then not Has_Alignment_Clause (Rec)
8527 -- Here we look at the actual component layout. A component is
8528 -- addressable if its size is a multiple of the Esize of the
8529 -- component type, and its starting position in the record has
8530 -- appropriate alignment, and the record itself has appropriate
8531 -- alignment to guarantee the component alignment.
8533 -- Make sure sizes are static, always assume the worst for any
8534 -- cases where we cannot check static values.
8536 if not (Known_Static_Esize (C)
8537 and then Known_Static_Esize (Ctyp))
8542 -- Size of component must be addressable or greater than 64 bits
8543 -- and a multiple of bytes.
8545 if not Addressable (Esize (C))
8546 and then Esize (C) < Uint_64
8551 -- Check size is proper multiple
8553 if Esize (C) mod Esize (Ctyp) /= 0 then
8557 -- Check alignment of component is OK
8559 if not Known_Component_Bit_Offset (C)
8560 or else Component_Bit_Offset (C) < Uint_0
8561 or else Component_Bit_Offset (C) mod Esize (Ctyp) /= 0
8566 -- Check alignment of record type is OK
8568 if not Known_Alignment (Rec)
8569 or else (Alignment (Rec) * SU) mod Esize (Ctyp) /= 0
8574 -- All tests passed, component is addressable
8579 --------------------------
8580 -- Reason_Bad_Component --
8581 --------------------------
8583 procedure Reason_Bad_Component (C : Entity_Id) is
8584 Rec : constant Entity_Id := Scope (C);
8585 Ctyp : constant Entity_Id := Etype (C);
8588 -- If component clause present assume that's the problem
8590 if Present (Component_Clause (C)) then
8591 Error_Msg_Sloc := Sloc (Component_Clause (C));
8592 Error_Msg_N ("\because of Component_Clause#", N);
8596 -- If pragma Pack clause present, assume that's the problem
8598 if Is_Packed (Rec) then
8599 P := Get_Rep_Pragma (Rec, Name_Pack);
8602 Error_Msg_Sloc := Sloc (P);
8603 Error_Msg_N ("\because of pragma Pack#", N);
8608 -- See if record has bad alignment clause
8610 if Has_Alignment_Clause (Rec)
8611 and then Known_Alignment (Rec)
8612 and then (Alignment (Rec) * SU) mod Esize (Ctyp) /= 0
8614 P := Get_Attribute_Definition_Clause (Rec, Attribute_Alignment);
8617 Error_Msg_Sloc := Sloc (P);
8618 Error_Msg_N ("\because of Alignment clause#", N);
8622 -- Couldn't find a reason, so return without a message
8625 end Reason_Bad_Component;
8627 -- Start of processing for Validate_Independence
8630 for J in Independence_Checks.First .. Independence_Checks.Last loop
8631 N := Independence_Checks.Table (J).N;
8632 E := Independence_Checks.Table (J).E;
8633 IC := Pragma_Name (N) = Name_Independent_Components;
8635 -- Deal with component case
8637 if Ekind (E) = E_Discriminant or else Ekind (E) = E_Component then
8638 if not OK_Component (E) then
8640 Reason_Bad_Component (E);
8645 -- Deal with record with Independent_Components
8647 if IC and then Is_Record_Type (E) then
8648 Comp := First_Component_Or_Discriminant (E);
8649 while Present (Comp) loop
8650 if not OK_Component (Comp) then
8652 Reason_Bad_Component (Comp);
8656 Next_Component_Or_Discriminant (Comp);
8660 -- Deal with address clause case
8662 if Is_Object (E) then
8663 Addr := Address_Clause (E);
8665 if Present (Addr) then
8667 Error_Msg_Sloc := Sloc (Addr);
8668 Error_Msg_N ("\because of Address clause#", N);
8673 -- Deal with independent components for array type
8675 if IC and then Is_Array_Type (E) then
8676 Check_Array_Type (E);
8679 -- Deal with independent components for array object
8681 if IC and then Is_Object (E) and then Is_Array_Type (Etype (E)) then
8682 Check_Array_Type (Etype (E));
8687 end Validate_Independence;
8689 -----------------------------------
8690 -- Validate_Unchecked_Conversion --
8691 -----------------------------------
8693 procedure Validate_Unchecked_Conversion
8695 Act_Unit : Entity_Id)
8702 -- Obtain source and target types. Note that we call Ancestor_Subtype
8703 -- here because the processing for generic instantiation always makes
8704 -- subtypes, and we want the original frozen actual types.
8706 -- If we are dealing with private types, then do the check on their
8707 -- fully declared counterparts if the full declarations have been
8708 -- encountered (they don't have to be visible, but they must exist!)
8710 Source := Ancestor_Subtype (Etype (First_Formal (Act_Unit)));
8712 if Is_Private_Type (Source)
8713 and then Present (Underlying_Type (Source))
8715 Source := Underlying_Type (Source);
8718 Target := Ancestor_Subtype (Etype (Act_Unit));
8720 -- If either type is generic, the instantiation happens within a generic
8721 -- unit, and there is nothing to check. The proper check will happen
8722 -- when the enclosing generic is instantiated.
8724 if Is_Generic_Type (Source) or else Is_Generic_Type (Target) then
8728 if Is_Private_Type (Target)
8729 and then Present (Underlying_Type (Target))
8731 Target := Underlying_Type (Target);
8734 -- Source may be unconstrained array, but not target
8736 if Is_Array_Type (Target)
8737 and then not Is_Constrained (Target)
8740 ("unchecked conversion to unconstrained array not allowed", N);
8744 -- Warn if conversion between two different convention pointers
8746 if Is_Access_Type (Target)
8747 and then Is_Access_Type (Source)
8748 and then Convention (Target) /= Convention (Source)
8749 and then Warn_On_Unchecked_Conversion
8751 -- Give warnings for subprogram pointers only on most targets. The
8752 -- exception is VMS, where data pointers can have different lengths
8753 -- depending on the pointer convention.
8755 if Is_Access_Subprogram_Type (Target)
8756 or else Is_Access_Subprogram_Type (Source)
8757 or else OpenVMS_On_Target
8760 ("?conversion between pointers with different conventions!", N);
8764 -- Warn if one of the operands is Ada.Calendar.Time. Do not emit a
8765 -- warning when compiling GNAT-related sources.
8767 if Warn_On_Unchecked_Conversion
8768 and then not In_Predefined_Unit (N)
8769 and then RTU_Loaded (Ada_Calendar)
8771 (Chars (Source) = Name_Time
8773 Chars (Target) = Name_Time)
8775 -- If Ada.Calendar is loaded and the name of one of the operands is
8776 -- Time, there is a good chance that this is Ada.Calendar.Time.
8779 Calendar_Time : constant Entity_Id :=
8780 Full_View (RTE (RO_CA_Time));
8782 pragma Assert (Present (Calendar_Time));
8784 if Source = Calendar_Time
8785 or else Target = Calendar_Time
8788 ("?representation of 'Time values may change between " &
8789 "'G'N'A'T versions", N);
8794 -- Make entry in unchecked conversion table for later processing by
8795 -- Validate_Unchecked_Conversions, which will check sizes and alignments
8796 -- (using values set by the back-end where possible). This is only done
8797 -- if the appropriate warning is active.
8799 if Warn_On_Unchecked_Conversion then
8800 Unchecked_Conversions.Append
8801 (New_Val => UC_Entry'
8806 -- If both sizes are known statically now, then back end annotation
8807 -- is not required to do a proper check but if either size is not
8808 -- known statically, then we need the annotation.
8810 if Known_Static_RM_Size (Source)
8811 and then Known_Static_RM_Size (Target)
8815 Back_Annotate_Rep_Info := True;
8819 -- If unchecked conversion to access type, and access type is declared
8820 -- in the same unit as the unchecked conversion, then set the flag
8821 -- No_Strict_Aliasing (no strict aliasing is implicit here)
8823 if Is_Access_Type (Target) and then
8824 In_Same_Source_Unit (Target, N)
8826 Set_No_Strict_Aliasing (Implementation_Base_Type (Target));
8829 -- Generate N_Validate_Unchecked_Conversion node for back end in case
8830 -- the back end needs to perform special validation checks.
8832 -- Shouldn't this be in Exp_Ch13, since the check only gets done if we
8833 -- have full expansion and the back end is called ???
8836 Make_Validate_Unchecked_Conversion (Sloc (N));
8837 Set_Source_Type (Vnode, Source);
8838 Set_Target_Type (Vnode, Target);
8840 -- If the unchecked conversion node is in a list, just insert before it.
8841 -- If not we have some strange case, not worth bothering about.
8843 if Is_List_Member (N) then
8844 Insert_After (N, Vnode);
8846 end Validate_Unchecked_Conversion;
8848 ------------------------------------
8849 -- Validate_Unchecked_Conversions --
8850 ------------------------------------
8852 procedure Validate_Unchecked_Conversions is
8854 for N in Unchecked_Conversions.First .. Unchecked_Conversions.Last loop
8856 T : UC_Entry renames Unchecked_Conversions.Table (N);
8858 Eloc : constant Source_Ptr := T.Eloc;
8859 Source : constant Entity_Id := T.Source;
8860 Target : constant Entity_Id := T.Target;
8866 -- This validation check, which warns if we have unequal sizes for
8867 -- unchecked conversion, and thus potentially implementation
8868 -- dependent semantics, is one of the few occasions on which we
8869 -- use the official RM size instead of Esize. See description in
8870 -- Einfo "Handling of Type'Size Values" for details.
8872 if Serious_Errors_Detected = 0
8873 and then Known_Static_RM_Size (Source)
8874 and then Known_Static_RM_Size (Target)
8876 -- Don't do the check if warnings off for either type, note the
8877 -- deliberate use of OR here instead of OR ELSE to get the flag
8878 -- Warnings_Off_Used set for both types if appropriate.
8880 and then not (Has_Warnings_Off (Source)
8882 Has_Warnings_Off (Target))
8884 Source_Siz := RM_Size (Source);
8885 Target_Siz := RM_Size (Target);
8887 if Source_Siz /= Target_Siz then
8889 ("?types for unchecked conversion have different sizes!",
8892 if All_Errors_Mode then
8893 Error_Msg_Name_1 := Chars (Source);
8894 Error_Msg_Uint_1 := Source_Siz;
8895 Error_Msg_Name_2 := Chars (Target);
8896 Error_Msg_Uint_2 := Target_Siz;
8897 Error_Msg ("\size of % is ^, size of % is ^?", Eloc);
8899 Error_Msg_Uint_1 := UI_Abs (Source_Siz - Target_Siz);
8901 if Is_Discrete_Type (Source)
8902 and then Is_Discrete_Type (Target)
8904 if Source_Siz > Target_Siz then
8906 ("\?^ high order bits of source will be ignored!",
8909 elsif Is_Unsigned_Type (Source) then
8911 ("\?source will be extended with ^ high order " &
8912 "zero bits?!", Eloc);
8916 ("\?source will be extended with ^ high order " &
8921 elsif Source_Siz < Target_Siz then
8922 if Is_Discrete_Type (Target) then
8923 if Bytes_Big_Endian then
8925 ("\?target value will include ^ undefined " &
8930 ("\?target value will include ^ undefined " &
8937 ("\?^ trailing bits of target value will be " &
8938 "undefined!", Eloc);
8941 else pragma Assert (Source_Siz > Target_Siz);
8943 ("\?^ trailing bits of source will be ignored!",
8950 -- If both types are access types, we need to check the alignment.
8951 -- If the alignment of both is specified, we can do it here.
8953 if Serious_Errors_Detected = 0
8954 and then Ekind (Source) in Access_Kind
8955 and then Ekind (Target) in Access_Kind
8956 and then Target_Strict_Alignment
8957 and then Present (Designated_Type (Source))
8958 and then Present (Designated_Type (Target))
8961 D_Source : constant Entity_Id := Designated_Type (Source);
8962 D_Target : constant Entity_Id := Designated_Type (Target);
8965 if Known_Alignment (D_Source)
8966 and then Known_Alignment (D_Target)
8969 Source_Align : constant Uint := Alignment (D_Source);
8970 Target_Align : constant Uint := Alignment (D_Target);
8973 if Source_Align < Target_Align
8974 and then not Is_Tagged_Type (D_Source)
8976 -- Suppress warning if warnings suppressed on either
8977 -- type or either designated type. Note the use of
8978 -- OR here instead of OR ELSE. That is intentional,
8979 -- we would like to set flag Warnings_Off_Used in
8980 -- all types for which warnings are suppressed.
8982 and then not (Has_Warnings_Off (D_Source)
8984 Has_Warnings_Off (D_Target)
8986 Has_Warnings_Off (Source)
8988 Has_Warnings_Off (Target))
8990 Error_Msg_Uint_1 := Target_Align;
8991 Error_Msg_Uint_2 := Source_Align;
8992 Error_Msg_Node_1 := D_Target;
8993 Error_Msg_Node_2 := D_Source;
8995 ("?alignment of & (^) is stricter than " &
8996 "alignment of & (^)!", Eloc);
8998 ("\?resulting access value may have invalid " &
8999 "alignment!", Eloc);
9007 end Validate_Unchecked_Conversions;