1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2010, 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_Eval; use Sem_Eval;
50 with Sem_Res; use Sem_Res;
51 with Sem_Type; use Sem_Type;
52 with Sem_Util; use Sem_Util;
53 with Sem_Warn; use Sem_Warn;
54 with Sinput; use Sinput;
55 with Snames; use Snames;
56 with Stand; use Stand;
57 with Sinfo; use Sinfo;
58 with Stringt; use Stringt;
59 with Targparm; use Targparm;
60 with Ttypes; use Ttypes;
61 with Tbuild; use Tbuild;
62 with Urealp; use Urealp;
64 with GNAT.Heap_Sort_G;
66 package body Sem_Ch13 is
68 SSU : constant Pos := System_Storage_Unit;
69 -- Convenient short hand for commonly used constant
71 -----------------------
72 -- Local Subprograms --
73 -----------------------
75 procedure Alignment_Check_For_Esize_Change (Typ : Entity_Id);
76 -- This routine is called after setting the Esize of type entity Typ.
77 -- The purpose is to deal with the situation where an alignment has been
78 -- inherited from a derived type that is no longer appropriate for the
79 -- new Esize value. In this case, we reset the Alignment to unknown.
81 procedure Build_Predicate_Function (Typ : Entity_Id; N : Node_Id);
82 -- If Typ has predicates (indicated by Has_Predicates being set for Typ,
83 -- then either there are pragma Invariant entries on the rep chain for the
84 -- type (note that Predicate aspects are converted to pragma Predicate), or
85 -- there are inherited aspects from a parent type, or ancestor subtypes.
86 -- This procedure builds the spec and body for the Predicate function that
87 -- tests these predicates. N is the freeze node for the type. The spec of
88 -- the function is inserted before the freeze node, and the body of the
89 -- function is inserted after the freeze node.
91 procedure Build_Static_Predicate
95 -- Given a predicated type Typ, where Typ is a discrete static subtype,
96 -- whose predicate expression is Expr, tests if Expr is a static predicate,
97 -- and if so, builds the predicate range list. Nam is the name of the one
98 -- argument to the predicate function. Occurrences of the type name in the
99 -- predicate expression have been replaced by identifier references to this
100 -- name, which is unique, so any identifier with Chars matching Nam must be
101 -- a reference to the type. If the predicate is non-static, this procedure
102 -- returns doing nothing. If the predicate is static, then the predicate
103 -- list is stored in Static_Predicate (Typ), and the Expr is rewritten as
104 -- a canonicalized membership operation.
106 function Get_Alignment_Value (Expr : Node_Id) return Uint;
107 -- Given the expression for an alignment value, returns the corresponding
108 -- Uint value. If the value is inappropriate, then error messages are
109 -- posted as required, and a value of No_Uint is returned.
111 function Is_Operational_Item (N : Node_Id) return Boolean;
112 -- A specification for a stream attribute is allowed before the full type
113 -- is declared, as explained in AI-00137 and the corrigendum. Attributes
114 -- that do not specify a representation characteristic are operational
117 procedure New_Stream_Subprogram
121 Nam : TSS_Name_Type);
122 -- Create a subprogram renaming of a given stream attribute to the
123 -- designated subprogram and then in the tagged case, provide this as a
124 -- primitive operation, or in the non-tagged case make an appropriate TSS
125 -- entry. This is more properly an expansion activity than just semantics,
126 -- but the presence of user-defined stream functions for limited types is a
127 -- legality check, which is why this takes place here rather than in
128 -- exp_ch13, where it was previously. Nam indicates the name of the TSS
129 -- function to be generated.
131 -- To avoid elaboration anomalies with freeze nodes, for untagged types
132 -- we generate both a subprogram declaration and a subprogram renaming
133 -- declaration, so that the attribute specification is handled as a
134 -- renaming_as_body. For tagged types, the specification is one of the
138 with procedure Replace_Type_Reference (N : Node_Id);
139 procedure Replace_Type_References_Generic (N : Node_Id; TName : Name_Id);
140 -- This is used to scan an expression for a predicate or invariant aspect
141 -- replacing occurrences of the name TName (the name of the subtype to
142 -- which the aspect applies) with appropriate references to the parameter
143 -- of the predicate function or invariant procedure. The procedure passed
144 -- as a generic parameter does the actual replacement of node N, which is
145 -- either a simple direct reference to TName, or a selected component that
146 -- represents an appropriately qualified occurrence of TName.
152 Biased : Boolean := True);
153 -- If Biased is True, sets Has_Biased_Representation flag for E, and
154 -- outputs a warning message at node N if Warn_On_Biased_Representation is
155 -- is True. This warning inserts the string Msg to describe the construct
158 ----------------------------------------------
159 -- Table for Validate_Unchecked_Conversions --
160 ----------------------------------------------
162 -- The following table collects unchecked conversions for validation.
163 -- Entries are made by Validate_Unchecked_Conversion and then the
164 -- call to Validate_Unchecked_Conversions does the actual error
165 -- checking and posting of warnings. The reason for this delayed
166 -- processing is to take advantage of back-annotations of size and
167 -- alignment values performed by the back end.
169 -- Note: the reason we store a Source_Ptr value instead of a Node_Id
170 -- is that by the time Validate_Unchecked_Conversions is called, Sprint
171 -- will already have modified all Sloc values if the -gnatD option is set.
173 type UC_Entry is record
174 Eloc : Source_Ptr; -- node used for posting warnings
175 Source : Entity_Id; -- source type for unchecked conversion
176 Target : Entity_Id; -- target type for unchecked conversion
179 package Unchecked_Conversions is new Table.Table (
180 Table_Component_Type => UC_Entry,
181 Table_Index_Type => Int,
182 Table_Low_Bound => 1,
184 Table_Increment => 200,
185 Table_Name => "Unchecked_Conversions");
187 ----------------------------------------
188 -- Table for Validate_Address_Clauses --
189 ----------------------------------------
191 -- If an address clause has the form
193 -- for X'Address use Expr
195 -- where Expr is of the form Y'Address or recursively is a reference
196 -- to a constant of either of these forms, and X and Y are entities of
197 -- objects, then if Y has a smaller alignment than X, that merits a
198 -- warning about possible bad alignment. The following table collects
199 -- address clauses of this kind. We put these in a table so that they
200 -- can be checked after the back end has completed annotation of the
201 -- alignments of objects, since we can catch more cases that way.
203 type Address_Clause_Check_Record is record
205 -- The address clause
208 -- The entity of the object overlaying Y
211 -- The entity of the object being overlaid
214 -- Whether the address is offset within Y
217 package Address_Clause_Checks is new Table.Table (
218 Table_Component_Type => Address_Clause_Check_Record,
219 Table_Index_Type => Int,
220 Table_Low_Bound => 1,
222 Table_Increment => 200,
223 Table_Name => "Address_Clause_Checks");
225 -----------------------------------------
226 -- Adjust_Record_For_Reverse_Bit_Order --
227 -----------------------------------------
229 procedure Adjust_Record_For_Reverse_Bit_Order (R : Entity_Id) is
234 -- Processing depends on version of Ada
236 -- For Ada 95, we just renumber bits within a storage unit. We do the
237 -- same for Ada 83 mode, since we recognize pragma Bit_Order in Ada 83,
238 -- and are free to add this extension.
240 if Ada_Version < Ada_2005 then
241 Comp := First_Component_Or_Discriminant (R);
242 while Present (Comp) loop
243 CC := Component_Clause (Comp);
245 -- If component clause is present, then deal with the non-default
246 -- bit order case for Ada 95 mode.
248 -- We only do this processing for the base type, and in fact that
249 -- is important, since otherwise if there are record subtypes, we
250 -- could reverse the bits once for each subtype, which is wrong.
253 and then Ekind (R) = E_Record_Type
256 CFB : constant Uint := Component_Bit_Offset (Comp);
257 CSZ : constant Uint := Esize (Comp);
258 CLC : constant Node_Id := Component_Clause (Comp);
259 Pos : constant Node_Id := Position (CLC);
260 FB : constant Node_Id := First_Bit (CLC);
262 Storage_Unit_Offset : constant Uint :=
263 CFB / System_Storage_Unit;
265 Start_Bit : constant Uint :=
266 CFB mod System_Storage_Unit;
269 -- Cases where field goes over storage unit boundary
271 if Start_Bit + CSZ > System_Storage_Unit then
273 -- Allow multi-byte field but generate warning
275 if Start_Bit mod System_Storage_Unit = 0
276 and then CSZ mod System_Storage_Unit = 0
279 ("multi-byte field specified with non-standard"
280 & " Bit_Order?", CLC);
282 if Bytes_Big_Endian then
284 ("bytes are not reversed "
285 & "(component is big-endian)?", CLC);
288 ("bytes are not reversed "
289 & "(component is little-endian)?", CLC);
292 -- Do not allow non-contiguous field
296 ("attempt to specify non-contiguous field "
297 & "not permitted", CLC);
299 ("\caused by non-standard Bit_Order "
302 ("\consider possibility of using "
303 & "Ada 2005 mode here", CLC);
306 -- Case where field fits in one storage unit
309 -- Give warning if suspicious component clause
311 if Intval (FB) >= System_Storage_Unit
312 and then Warn_On_Reverse_Bit_Order
315 ("?Bit_Order clause does not affect " &
316 "byte ordering", Pos);
318 Intval (Pos) + Intval (FB) /
321 ("?position normalized to ^ before bit " &
322 "order interpreted", Pos);
325 -- Here is where we fix up the Component_Bit_Offset value
326 -- to account for the reverse bit order. Some examples of
327 -- what needs to be done are:
329 -- First_Bit .. Last_Bit Component_Bit_Offset
341 -- The rule is that the first bit is is obtained by
342 -- subtracting the old ending bit from storage_unit - 1.
344 Set_Component_Bit_Offset
346 (Storage_Unit_Offset * System_Storage_Unit) +
347 (System_Storage_Unit - 1) -
348 (Start_Bit + CSZ - 1));
350 Set_Normalized_First_Bit
352 Component_Bit_Offset (Comp) mod
353 System_Storage_Unit);
358 Next_Component_Or_Discriminant (Comp);
361 -- For Ada 2005, we do machine scalar processing, as fully described In
362 -- AI-133. This involves gathering all components which start at the
363 -- same byte offset and processing them together. Same approach is still
364 -- valid in later versions including Ada 2012.
368 Max_Machine_Scalar_Size : constant Uint :=
370 (Standard_Long_Long_Integer_Size);
371 -- We use this as the maximum machine scalar size
374 SSU : constant Uint := UI_From_Int (System_Storage_Unit);
377 -- This first loop through components does two things. First it
378 -- deals with the case of components with component clauses whose
379 -- length is greater than the maximum machine scalar size (either
380 -- accepting them or rejecting as needed). Second, it counts the
381 -- number of components with component clauses whose length does
382 -- not exceed this maximum for later processing.
385 Comp := First_Component_Or_Discriminant (R);
386 while Present (Comp) loop
387 CC := Component_Clause (Comp);
391 Fbit : constant Uint :=
392 Static_Integer (First_Bit (CC));
393 Lbit : constant Uint :=
394 Static_Integer (Last_Bit (CC));
397 -- Case of component with last bit >= max machine scalar
399 if Lbit >= Max_Machine_Scalar_Size then
401 -- This is allowed only if first bit is zero, and
402 -- last bit + 1 is a multiple of storage unit size.
404 if Fbit = 0 and then (Lbit + 1) mod SSU = 0 then
406 -- This is the case to give a warning if enabled
408 if Warn_On_Reverse_Bit_Order then
410 ("multi-byte field specified with "
411 & " non-standard Bit_Order?", CC);
413 if Bytes_Big_Endian then
415 ("\bytes are not reversed "
416 & "(component is big-endian)?", CC);
419 ("\bytes are not reversed "
420 & "(component is little-endian)?", CC);
424 -- Give error message for RM 13.4.1(10) violation
428 ("machine scalar rules not followed for&",
429 First_Bit (CC), Comp);
431 Error_Msg_Uint_1 := Lbit;
432 Error_Msg_Uint_2 := Max_Machine_Scalar_Size;
434 ("\last bit (^) exceeds maximum machine "
438 if (Lbit + 1) mod SSU /= 0 then
439 Error_Msg_Uint_1 := SSU;
441 ("\and is not a multiple of Storage_Unit (^) "
442 & "('R'M 13.4.1(10))",
446 Error_Msg_Uint_1 := Fbit;
448 ("\and first bit (^) is non-zero "
449 & "('R'M 13.4.1(10))",
454 -- OK case of machine scalar related component clause,
455 -- For now, just count them.
458 Num_CC := Num_CC + 1;
463 Next_Component_Or_Discriminant (Comp);
466 -- We need to sort the component clauses on the basis of the
467 -- Position values in the clause, so we can group clauses with
468 -- the same Position. together to determine the relevant machine
472 Comps : array (0 .. Num_CC) of Entity_Id;
473 -- Array to collect component and discriminant entities. The
474 -- data starts at index 1, the 0'th entry is for the sort
477 function CP_Lt (Op1, Op2 : Natural) return Boolean;
478 -- Compare routine for Sort
480 procedure CP_Move (From : Natural; To : Natural);
481 -- Move routine for Sort
483 package Sorting is new GNAT.Heap_Sort_G (CP_Move, CP_Lt);
487 -- Start and stop positions in the component list of the set of
488 -- components with the same starting position (that constitute
489 -- components in a single machine scalar).
492 -- Maximum last bit value of any component in this set
495 -- Corresponding machine scalar size
501 function CP_Lt (Op1, Op2 : Natural) return Boolean is
503 return Position (Component_Clause (Comps (Op1))) <
504 Position (Component_Clause (Comps (Op2)));
511 procedure CP_Move (From : Natural; To : Natural) is
513 Comps (To) := Comps (From);
516 -- Start of processing for Sort_CC
519 -- Collect the machine scalar relevant component clauses
522 Comp := First_Component_Or_Discriminant (R);
523 while Present (Comp) loop
525 CC : constant Node_Id := Component_Clause (Comp);
528 -- Collect only component clauses whose last bit is less
529 -- than machine scalar size. Any component clause whose
530 -- last bit exceeds this value does not take part in
531 -- machine scalar layout considerations. The test for
532 -- Error_Posted makes sure we exclude component clauses
533 -- for which we already posted an error.
536 and then not Error_Posted (Last_Bit (CC))
537 and then Static_Integer (Last_Bit (CC)) <
538 Max_Machine_Scalar_Size
540 Num_CC := Num_CC + 1;
541 Comps (Num_CC) := Comp;
545 Next_Component_Or_Discriminant (Comp);
548 -- Sort by ascending position number
550 Sorting.Sort (Num_CC);
552 -- We now have all the components whose size does not exceed
553 -- the max machine scalar value, sorted by starting position.
554 -- In this loop we gather groups of clauses starting at the
555 -- same position, to process them in accordance with AI-133.
558 while Stop < Num_CC loop
563 (Last_Bit (Component_Clause (Comps (Start))));
564 while Stop < Num_CC loop
566 (Position (Component_Clause (Comps (Stop + 1)))) =
568 (Position (Component_Clause (Comps (Stop))))
576 (Component_Clause (Comps (Stop)))));
582 -- Now we have a group of component clauses from Start to
583 -- Stop whose positions are identical, and MaxL is the
584 -- maximum last bit value of any of these components.
586 -- We need to determine the corresponding machine scalar
587 -- size. This loop assumes that machine scalar sizes are
588 -- even, and that each possible machine scalar has twice
589 -- as many bits as the next smaller one.
591 MSS := Max_Machine_Scalar_Size;
593 and then (MSS / 2) >= SSU
594 and then (MSS / 2) > MaxL
599 -- Here is where we fix up the Component_Bit_Offset value
600 -- to account for the reverse bit order. Some examples of
601 -- what needs to be done for the case of a machine scalar
604 -- First_Bit .. Last_Bit Component_Bit_Offset
616 -- The rule is that the first bit is obtained by subtracting
617 -- the old ending bit from machine scalar size - 1.
619 for C in Start .. Stop loop
621 Comp : constant Entity_Id := Comps (C);
622 CC : constant Node_Id :=
623 Component_Clause (Comp);
624 LB : constant Uint :=
625 Static_Integer (Last_Bit (CC));
626 NFB : constant Uint := MSS - Uint_1 - LB;
627 NLB : constant Uint := NFB + Esize (Comp) - 1;
628 Pos : constant Uint :=
629 Static_Integer (Position (CC));
632 if Warn_On_Reverse_Bit_Order then
633 Error_Msg_Uint_1 := MSS;
635 ("info: reverse bit order in machine " &
636 "scalar of length^?", First_Bit (CC));
637 Error_Msg_Uint_1 := NFB;
638 Error_Msg_Uint_2 := NLB;
640 if Bytes_Big_Endian then
642 ("?\info: big-endian range for "
643 & "component & is ^ .. ^",
644 First_Bit (CC), Comp);
647 ("?\info: little-endian range "
648 & "for component & is ^ .. ^",
649 First_Bit (CC), Comp);
653 Set_Component_Bit_Offset (Comp, Pos * SSU + NFB);
654 Set_Normalized_First_Bit (Comp, NFB mod SSU);
661 end Adjust_Record_For_Reverse_Bit_Order;
663 --------------------------------------
664 -- Alignment_Check_For_Esize_Change --
665 --------------------------------------
667 procedure Alignment_Check_For_Esize_Change (Typ : Entity_Id) is
669 -- If the alignment is known, and not set by a rep clause, and is
670 -- inconsistent with the size being set, then reset it to unknown,
671 -- we assume in this case that the size overrides the inherited
672 -- alignment, and that the alignment must be recomputed.
674 if Known_Alignment (Typ)
675 and then not Has_Alignment_Clause (Typ)
676 and then Esize (Typ) mod (Alignment (Typ) * SSU) /= 0
678 Init_Alignment (Typ);
680 end Alignment_Check_For_Esize_Change;
682 -----------------------------------
683 -- Analyze_Aspect_Specifications --
684 -----------------------------------
686 procedure Analyze_Aspect_Specifications (N : Node_Id; E : Entity_Id) is
691 L : constant List_Id := Aspect_Specifications (N);
693 Ins_Node : Node_Id := N;
694 -- Insert pragmas (except Pre/Post/Invariant/Predicate) after this node
696 -- The general processing involves building an attribute definition
697 -- clause or a pragma node that corresponds to the access type. Then
698 -- one of two things happens:
700 -- If we are required to delay the evaluation of this aspect to the
701 -- freeze point, we attach the corresponding pragma/attribute definition
702 -- clause to the aspect specification node, which is then placed in the
703 -- Rep Item chain. In this case we mark the entity by setting the flag
704 -- Has_Delayed_Aspects and we evaluate the rep item at the freeze point.
706 -- If no delay is required, we just insert the pragma or attribute
707 -- after the declaration, and it will get processed by the normal
708 -- circuit. The From_Aspect_Specification flag is set on the pragma
709 -- or attribute definition node in either case to activate special
710 -- processing (e.g. not traversing the list of homonyms for inline).
712 Delay_Required : Boolean;
713 -- Set True if delay is required
716 pragma Assert (Present (L));
718 -- Loop through aspects
721 Aspect_Loop : while Present (Aspect) loop
723 Loc : constant Source_Ptr := Sloc (Aspect);
724 Id : constant Node_Id := Identifier (Aspect);
725 Expr : constant Node_Id := Expression (Aspect);
726 Nam : constant Name_Id := Chars (Id);
727 A_Id : constant Aspect_Id := Get_Aspect_Id (Nam);
730 Eloc : Source_Ptr := Sloc (Expr);
731 -- Source location of expression, modified when we split PPC's
733 procedure Check_False_Aspect_For_Derived_Type;
734 -- This procedure checks for the case of a false aspect for a
735 -- derived type, which improperly tries to cancel an aspect
736 -- inherited from the parent;
738 -----------------------------------------
739 -- Check_False_Aspect_For_Derived_Type --
740 -----------------------------------------
742 procedure Check_False_Aspect_For_Derived_Type is
744 -- We are only checking derived types
746 if not Is_Derived_Type (E) then
751 when Aspect_Atomic | Aspect_Shared =>
752 if not Is_Atomic (E) then
756 when Aspect_Atomic_Components =>
757 if not Has_Atomic_Components (E) then
761 when Aspect_Discard_Names =>
762 if not Discard_Names (E) then
767 if not Is_Packed (E) then
771 when Aspect_Unchecked_Union =>
772 if not Is_Unchecked_Union (E) then
776 when Aspect_Volatile =>
777 if not Is_Volatile (E) then
781 when Aspect_Volatile_Components =>
782 if not Has_Volatile_Components (E) then
790 -- Fall through means we are canceling an inherited aspect
792 Error_Msg_Name_1 := Nam;
794 ("derived type& inherits aspect%, cannot cancel", Expr, E);
795 end Check_False_Aspect_For_Derived_Type;
797 -- Start of processing for Aspect_Loop
800 -- Skip aspect if already analyzed (not clear if this is needed)
802 if Analyzed (Aspect) then
806 Set_Analyzed (Aspect);
807 Set_Entity (Aspect, E);
808 Ent := New_Occurrence_Of (E, Sloc (Id));
810 -- Check for duplicate aspect. Note that the Comes_From_Source
811 -- test allows duplicate Pre/Post's that we generate internally
812 -- to escape being flagged here.
815 while Anod /= Aspect loop
816 if Same_Aspect (A_Id, Get_Aspect_Id (Chars (Identifier (Anod))))
817 and then Comes_From_Source (Aspect)
819 Error_Msg_Name_1 := Nam;
820 Error_Msg_Sloc := Sloc (Anod);
822 -- Case of same aspect specified twice
824 if Class_Present (Anod) = Class_Present (Aspect) then
825 if not Class_Present (Anod) then
827 ("aspect% for & previously given#",
831 ("aspect `%''Class` for & previously given#",
835 -- Case of Pre and Pre'Class both specified
837 elsif Nam = Name_Pre then
838 if Class_Present (Aspect) then
840 ("aspect `Pre''Class` for & is not allowed here",
843 ("\since aspect `Pre` previously given#",
848 ("aspect `Pre` for & is not allowed here",
851 ("\since aspect `Pre''Class` previously given#",
862 -- Copy expression for later processing by the procedures
863 -- Check_Aspect_At_[Freeze_Point | End_Of_Declarations]
865 Set_Entity (Id, New_Copy_Tree (Expr));
867 -- Processing based on specific aspect
871 -- No_Aspect should be impossible
876 -- Aspects taking an optional boolean argument. For all of
877 -- these we just create a matching pragma and insert it, if
878 -- the expression is missing or set to True. If the expression
879 -- is False, we can ignore the aspect with the exception that
880 -- in the case of a derived type, we must check for an illegal
881 -- attempt to cancel an inherited aspect.
883 when Boolean_Aspects =>
884 Set_Is_Boolean_Aspect (Aspect);
887 and then Is_False (Static_Boolean (Expr))
889 Check_False_Aspect_For_Derived_Type;
893 -- If True, build corresponding pragma node
897 Pragma_Argument_Associations => New_List (Ent),
899 Make_Identifier (Sloc (Id), Chars (Id)));
901 -- Never need to delay for boolean aspects
903 Delay_Required := False;
905 -- Library unit aspects. These are boolean aspects, but we
906 -- have to do special things with the insertion, since the
907 -- pragma belongs inside the declarations of a package.
909 when Library_Unit_Aspects =>
911 and then Is_False (Static_Boolean (Expr))
916 -- Build corresponding pragma node
920 Pragma_Argument_Associations => New_List (Ent),
922 Make_Identifier (Sloc (Id), Chars (Id)));
924 -- This requires special handling in the case of a package
925 -- declaration, the pragma needs to be inserted in the list
926 -- of declarations for the associated package. There is no
927 -- issue of visibility delay for these aspects.
929 if Nkind (N) = N_Package_Declaration then
930 if Nkind (Parent (N)) /= N_Compilation_Unit then
932 ("incorrect context for library unit aspect&", Id);
935 (Aitem, Visible_Declarations (Specification (N)));
941 -- If not package declaration, no delay is required
943 Delay_Required := False;
945 -- Aspects corresponding to attribute definition clauses
947 when Aspect_Address |
950 Aspect_Component_Size |
951 Aspect_External_Tag |
953 Aspect_Machine_Radix |
958 Aspect_Storage_Pool |
959 Aspect_Storage_Size |
964 -- Construct the attribute definition clause
967 Make_Attribute_Definition_Clause (Loc,
970 Expression => Relocate_Node (Expr));
972 -- A delay is required except in the common case where
973 -- the expression is a literal, in which case it is fine
974 -- to take care of it right away.
976 if Nkind_In (Expr, N_Integer_Literal, N_String_Literal) then
977 Delay_Required := False;
979 Delay_Required := True;
980 Set_Is_Delayed_Aspect (Aspect);
983 -- Aspects corresponding to pragmas with two arguments, where
984 -- the first argument is a local name referring to the entity,
985 -- and the second argument is the aspect definition expression.
987 when Aspect_Suppress |
990 -- Construct the pragma
994 Pragma_Argument_Associations => New_List (
995 New_Occurrence_Of (E, Eloc),
996 Relocate_Node (Expr)),
998 Make_Identifier (Sloc (Id), Chars (Id)));
1000 -- We don't have to play the delay game here, since the only
1001 -- values are check names which don't get analyzed anyway.
1003 Delay_Required := False;
1005 -- Aspects corresponding to pragmas with two arguments, where
1006 -- the second argument is a local name referring to the entity,
1007 -- and the first argument is the aspect definition expression.
1009 when Aspect_Warnings =>
1011 -- Construct the pragma
1015 Pragma_Argument_Associations => New_List (
1016 Relocate_Node (Expr),
1017 New_Occurrence_Of (E, Eloc)),
1018 Pragma_Identifier =>
1019 Make_Identifier (Sloc (Id), Chars (Id)),
1020 Class_Present => Class_Present (Aspect));
1022 -- We don't have to play the delay game here, since the only
1023 -- values are ON/OFF which don't get analyzed anyway.
1025 Delay_Required := False;
1027 -- Aspects Pre/Post generate Precondition/Postcondition pragmas
1028 -- with a first argument that is the expression, and a second
1029 -- argument that is an informative message if the test fails.
1030 -- This is inserted right after the declaration, to get the
1031 -- required pragma placement. The processing for the pragmas
1032 -- takes care of the required delay.
1034 when Pre_Post_Aspects => declare
1038 if A_Id = Aspect_Pre or else A_Id = Aspect_Precondition then
1039 Pname := Name_Precondition;
1041 Pname := Name_Postcondition;
1044 -- If the expressions is of the form A and then B, then
1045 -- we generate separate Pre/Post aspects for the separate
1046 -- clauses. Since we allow multiple pragmas, there is no
1047 -- problem in allowing multiple Pre/Post aspects internally.
1049 -- We do not do this for Pre'Class, since we have to put
1050 -- these conditions together in a complex OR expression
1052 if Pname = Name_Postcondition
1053 or else not Class_Present (Aspect)
1055 while Nkind (Expr) = N_And_Then loop
1056 Insert_After (Aspect,
1057 Make_Aspect_Specification (Sloc (Right_Opnd (Expr)),
1058 Identifier => Identifier (Aspect),
1059 Expression => Relocate_Node (Right_Opnd (Expr)),
1060 Class_Present => Class_Present (Aspect),
1061 Split_PPC => True));
1062 Rewrite (Expr, Relocate_Node (Left_Opnd (Expr)));
1063 Eloc := Sloc (Expr);
1067 -- Build the precondition/postcondition pragma
1071 Pragma_Identifier =>
1072 Make_Identifier (Sloc (Id), Pname),
1073 Class_Present => Class_Present (Aspect),
1074 Split_PPC => Split_PPC (Aspect),
1075 Pragma_Argument_Associations => New_List (
1076 Make_Pragma_Argument_Association (Eloc,
1077 Chars => Name_Check,
1078 Expression => Relocate_Node (Expr))));
1080 -- Add message unless exception messages are suppressed
1082 if not Opt.Exception_Locations_Suppressed then
1083 Append_To (Pragma_Argument_Associations (Aitem),
1084 Make_Pragma_Argument_Association (Eloc,
1085 Chars => Name_Message,
1087 Make_String_Literal (Eloc,
1089 & Get_Name_String (Pname)
1091 & Build_Location_String (Eloc))));
1094 Set_From_Aspect_Specification (Aitem, True);
1095 Set_Is_Delayed_Aspect (Aspect);
1097 -- For Pre/Post cases, insert immediately after the entity
1098 -- declaration, since that is the required pragma placement.
1099 -- Note that for these aspects, we do not have to worry
1100 -- about delay issues, since the pragmas themselves deal
1101 -- with delay of visibility for the expression analysis.
1103 -- If the entity is a library-level subprogram, the pre/
1104 -- postconditions must be treated as late pragmas.
1106 if Nkind (Parent (N)) = N_Compilation_Unit then
1107 Add_Global_Declaration (Aitem);
1109 Insert_After (N, Aitem);
1115 -- Invariant aspects generate a corresponding pragma with a
1116 -- first argument that is the entity, a second argument that is
1117 -- the expression and a third argument that is an appropriate
1118 -- message. This is inserted right after the declaration, to
1119 -- get the required pragma placement. The pragma processing
1120 -- takes care of the required delay.
1122 when Aspect_Invariant |
1123 Aspect_Type_Invariant =>
1125 -- Construct the pragma
1129 Pragma_Argument_Associations =>
1130 New_List (Ent, Relocate_Node (Expr)),
1131 Class_Present => Class_Present (Aspect),
1132 Pragma_Identifier =>
1133 Make_Identifier (Sloc (Id), Name_Invariant));
1135 -- Add message unless exception messages are suppressed
1137 if not Opt.Exception_Locations_Suppressed then
1138 Append_To (Pragma_Argument_Associations (Aitem),
1139 Make_Pragma_Argument_Association (Eloc,
1140 Chars => Name_Message,
1142 Make_String_Literal (Eloc,
1143 Strval => "failed invariant from "
1144 & Build_Location_String (Eloc))));
1147 Set_From_Aspect_Specification (Aitem, True);
1148 Set_Is_Delayed_Aspect (Aspect);
1150 -- For Invariant case, insert immediately after the entity
1151 -- declaration. We do not have to worry about delay issues
1152 -- since the pragma processing takes care of this.
1154 Insert_After (N, Aitem);
1157 -- Predicate aspects generate a corresponding pragma with a
1158 -- first argument that is the entity, and the second argument
1159 -- is the expression.
1161 when Aspect_Dynamic_Predicate |
1163 Aspect_Static_Predicate =>
1165 -- Construct the pragma (always a pragma Predicate, with
1166 -- flags recording whether
1170 Pragma_Argument_Associations =>
1171 New_List (Ent, Relocate_Node (Expr)),
1172 Class_Present => Class_Present (Aspect),
1173 Pragma_Identifier =>
1174 Make_Identifier (Sloc (Id), Name_Predicate));
1176 Set_From_Aspect_Specification (Aitem, True);
1178 -- Set special flags for dynamic/static cases
1180 if A_Id = Aspect_Dynamic_Predicate then
1181 Set_From_Dynamic_Predicate (Aitem);
1182 elsif A_Id = Aspect_Static_Predicate then
1183 Set_From_Static_Predicate (Aitem);
1186 -- Make sure we have a freeze node (it might otherwise be
1187 -- missing in cases like subtype X is Y, and we would not
1188 -- have a place to build the predicate function).
1190 Set_Has_Predicates (E);
1191 Ensure_Freeze_Node (E);
1192 Set_Is_Delayed_Aspect (Aspect);
1193 Delay_Required := True;
1196 Set_From_Aspect_Specification (Aitem, True);
1198 -- If a delay is required, we delay the freeze (not much point in
1199 -- delaying the aspect if we don't delay the freeze!). The pragma
1200 -- or clause is then attached to the aspect specification which
1201 -- is placed in the rep item list.
1203 if Delay_Required then
1204 Ensure_Freeze_Node (E);
1205 Set_Is_Delayed_Aspect (Aitem);
1206 Set_Has_Delayed_Aspects (E);
1207 Set_Aspect_Rep_Item (Aspect, Aitem);
1208 Record_Rep_Item (E, Aspect);
1210 -- If no delay required, insert the pragma/clause in the tree
1213 -- If this is a compilation unit, we will put the pragma in
1214 -- the Pragmas_After list of the N_Compilation_Unit_Aux node.
1216 if Nkind (Parent (Ins_Node)) = N_Compilation_Unit then
1218 Aux : constant Node_Id :=
1219 Aux_Decls_Node (Parent (Ins_Node));
1222 pragma Assert (Nkind (Aux) = N_Compilation_Unit_Aux);
1224 if No (Pragmas_After (Aux)) then
1225 Set_Pragmas_After (Aux, Empty_List);
1228 -- For Pre_Post put at start of list, otherwise at end
1230 if A_Id in Pre_Post_Aspects then
1231 Prepend (Aitem, Pragmas_After (Aux));
1233 Append (Aitem, Pragmas_After (Aux));
1237 -- Here if not compilation unit case
1240 -- For Pre/Post cases, insert immediately after the entity
1241 -- declaration, since that is the required pragma placement.
1243 if A_Id in Pre_Post_Aspects then
1244 Insert_After (N, Aitem);
1246 -- For all other cases, insert in sequence
1249 Insert_After (Ins_Node, Aitem);
1258 end loop Aspect_Loop;
1259 end Analyze_Aspect_Specifications;
1261 -----------------------
1262 -- Analyze_At_Clause --
1263 -----------------------
1265 -- An at clause is replaced by the corresponding Address attribute
1266 -- definition clause that is the preferred approach in Ada 95.
1268 procedure Analyze_At_Clause (N : Node_Id) is
1269 CS : constant Boolean := Comes_From_Source (N);
1272 -- This is an obsolescent feature
1274 Check_Restriction (No_Obsolescent_Features, N);
1276 if Warn_On_Obsolescent_Feature then
1278 ("at clause is an obsolescent feature (RM J.7(2))?", N);
1280 ("\use address attribute definition clause instead?", N);
1283 -- Rewrite as address clause
1286 Make_Attribute_Definition_Clause (Sloc (N),
1287 Name => Identifier (N),
1288 Chars => Name_Address,
1289 Expression => Expression (N)));
1291 -- We preserve Comes_From_Source, since logically the clause still
1292 -- comes from the source program even though it is changed in form.
1294 Set_Comes_From_Source (N, CS);
1296 -- Analyze rewritten clause
1298 Analyze_Attribute_Definition_Clause (N);
1299 end Analyze_At_Clause;
1301 -----------------------------------------
1302 -- Analyze_Attribute_Definition_Clause --
1303 -----------------------------------------
1305 procedure Analyze_Attribute_Definition_Clause (N : Node_Id) is
1306 Loc : constant Source_Ptr := Sloc (N);
1307 Nam : constant Node_Id := Name (N);
1308 Attr : constant Name_Id := Chars (N);
1309 Expr : constant Node_Id := Expression (N);
1310 Id : constant Attribute_Id := Get_Attribute_Id (Attr);
1314 FOnly : Boolean := False;
1315 -- Reset to True for subtype specific attribute (Alignment, Size)
1316 -- and for stream attributes, i.e. those cases where in the call
1317 -- to Rep_Item_Too_Late, FOnly is set True so that only the freezing
1318 -- rules are checked. Note that the case of stream attributes is not
1319 -- clear from the RM, but see AI95-00137. Also, the RM seems to
1320 -- disallow Storage_Size for derived task types, but that is also
1321 -- clearly unintentional.
1323 procedure Analyze_Stream_TSS_Definition (TSS_Nam : TSS_Name_Type);
1324 -- Common processing for 'Read, 'Write, 'Input and 'Output attribute
1325 -- definition clauses.
1327 function Duplicate_Clause return Boolean;
1328 -- This routine checks if the aspect for U_Ent being given by attribute
1329 -- definition clause N is for an aspect that has already been specified,
1330 -- and if so gives an error message. If there is a duplicate, True is
1331 -- returned, otherwise if there is no error, False is returned.
1333 -----------------------------------
1334 -- Analyze_Stream_TSS_Definition --
1335 -----------------------------------
1337 procedure Analyze_Stream_TSS_Definition (TSS_Nam : TSS_Name_Type) is
1338 Subp : Entity_Id := Empty;
1343 Is_Read : constant Boolean := (TSS_Nam = TSS_Stream_Read);
1345 function Has_Good_Profile (Subp : Entity_Id) return Boolean;
1346 -- Return true if the entity is a subprogram with an appropriate
1347 -- profile for the attribute being defined.
1349 ----------------------
1350 -- Has_Good_Profile --
1351 ----------------------
1353 function Has_Good_Profile (Subp : Entity_Id) return Boolean is
1355 Is_Function : constant Boolean := (TSS_Nam = TSS_Stream_Input);
1356 Expected_Ekind : constant array (Boolean) of Entity_Kind :=
1357 (False => E_Procedure, True => E_Function);
1361 if Ekind (Subp) /= Expected_Ekind (Is_Function) then
1365 F := First_Formal (Subp);
1368 or else Ekind (Etype (F)) /= E_Anonymous_Access_Type
1369 or else Designated_Type (Etype (F)) /=
1370 Class_Wide_Type (RTE (RE_Root_Stream_Type))
1375 if not Is_Function then
1379 Expected_Mode : constant array (Boolean) of Entity_Kind :=
1380 (False => E_In_Parameter,
1381 True => E_Out_Parameter);
1383 if Parameter_Mode (F) /= Expected_Mode (Is_Read) then
1391 Typ := Etype (Subp);
1394 return Base_Type (Typ) = Base_Type (Ent)
1395 and then No (Next_Formal (F));
1396 end Has_Good_Profile;
1398 -- Start of processing for Analyze_Stream_TSS_Definition
1403 if not Is_Type (U_Ent) then
1404 Error_Msg_N ("local name must be a subtype", Nam);
1408 Pnam := TSS (Base_Type (U_Ent), TSS_Nam);
1410 -- If Pnam is present, it can be either inherited from an ancestor
1411 -- type (in which case it is legal to redefine it for this type), or
1412 -- be a previous definition of the attribute for the same type (in
1413 -- which case it is illegal).
1415 -- In the first case, it will have been analyzed already, and we
1416 -- can check that its profile does not match the expected profile
1417 -- for a stream attribute of U_Ent. In the second case, either Pnam
1418 -- has been analyzed (and has the expected profile), or it has not
1419 -- been analyzed yet (case of a type that has not been frozen yet
1420 -- and for which the stream attribute has been set using Set_TSS).
1423 and then (No (First_Entity (Pnam)) or else Has_Good_Profile (Pnam))
1425 Error_Msg_Sloc := Sloc (Pnam);
1426 Error_Msg_Name_1 := Attr;
1427 Error_Msg_N ("% attribute already defined #", Nam);
1433 if Is_Entity_Name (Expr) then
1434 if not Is_Overloaded (Expr) then
1435 if Has_Good_Profile (Entity (Expr)) then
1436 Subp := Entity (Expr);
1440 Get_First_Interp (Expr, I, It);
1441 while Present (It.Nam) loop
1442 if Has_Good_Profile (It.Nam) then
1447 Get_Next_Interp (I, It);
1452 if Present (Subp) then
1453 if Is_Abstract_Subprogram (Subp) then
1454 Error_Msg_N ("stream subprogram must not be abstract", Expr);
1458 Set_Entity (Expr, Subp);
1459 Set_Etype (Expr, Etype (Subp));
1461 New_Stream_Subprogram (N, U_Ent, Subp, TSS_Nam);
1464 Error_Msg_Name_1 := Attr;
1465 Error_Msg_N ("incorrect expression for% attribute", Expr);
1467 end Analyze_Stream_TSS_Definition;
1469 ----------------------
1470 -- Duplicate_Clause --
1471 ----------------------
1473 function Duplicate_Clause return Boolean is
1477 -- Nothing to do if this attribute definition clause comes from
1478 -- an aspect specification, since we could not be duplicating an
1479 -- explicit clause, and we dealt with the case of duplicated aspects
1480 -- in Analyze_Aspect_Specifications.
1482 if From_Aspect_Specification (N) then
1486 -- Otherwise current clause may duplicate previous clause or a
1487 -- previously given aspect specification for the same aspect.
1489 A := Get_Rep_Item_For_Entity (U_Ent, Chars (N));
1492 if Entity (A) = U_Ent then
1493 Error_Msg_Name_1 := Chars (N);
1494 Error_Msg_Sloc := Sloc (A);
1495 Error_Msg_NE ("aspect% for & previously given#", N, U_Ent);
1501 end Duplicate_Clause;
1503 -- Start of processing for Analyze_Attribute_Definition_Clause
1506 -- Process Ignore_Rep_Clauses option
1508 if Ignore_Rep_Clauses then
1511 -- The following should be ignored. They do not affect legality
1512 -- and may be target dependent. The basic idea of -gnatI is to
1513 -- ignore any rep clauses that may be target dependent but do not
1514 -- affect legality (except possibly to be rejected because they
1515 -- are incompatible with the compilation target).
1517 when Attribute_Alignment |
1518 Attribute_Bit_Order |
1519 Attribute_Component_Size |
1520 Attribute_Machine_Radix |
1521 Attribute_Object_Size |
1524 Attribute_Stream_Size |
1525 Attribute_Value_Size =>
1527 Rewrite (N, Make_Null_Statement (Sloc (N)));
1530 -- The following should not be ignored, because in the first place
1531 -- they are reasonably portable, and should not cause problems in
1532 -- compiling code from another target, and also they do affect
1533 -- legality, e.g. failing to provide a stream attribute for a
1534 -- type may make a program illegal.
1536 when Attribute_External_Tag |
1540 Attribute_Storage_Pool |
1541 Attribute_Storage_Size |
1545 -- Other cases are errors ("attribute& cannot be set with
1546 -- definition clause"), which will be caught below.
1554 Ent := Entity (Nam);
1556 if Rep_Item_Too_Early (Ent, N) then
1560 -- Rep clause applies to full view of incomplete type or private type if
1561 -- we have one (if not, this is a premature use of the type). However,
1562 -- certain semantic checks need to be done on the specified entity (i.e.
1563 -- the private view), so we save it in Ent.
1565 if Is_Private_Type (Ent)
1566 and then Is_Derived_Type (Ent)
1567 and then not Is_Tagged_Type (Ent)
1568 and then No (Full_View (Ent))
1570 -- If this is a private type whose completion is a derivation from
1571 -- another private type, there is no full view, and the attribute
1572 -- belongs to the type itself, not its underlying parent.
1576 elsif Ekind (Ent) = E_Incomplete_Type then
1578 -- The attribute applies to the full view, set the entity of the
1579 -- attribute definition accordingly.
1581 Ent := Underlying_Type (Ent);
1583 Set_Entity (Nam, Ent);
1586 U_Ent := Underlying_Type (Ent);
1589 -- Complete other routine error checks
1591 if Etype (Nam) = Any_Type then
1594 elsif Scope (Ent) /= Current_Scope then
1595 Error_Msg_N ("entity must be declared in this scope", Nam);
1598 elsif No (U_Ent) then
1601 elsif Is_Type (U_Ent)
1602 and then not Is_First_Subtype (U_Ent)
1603 and then Id /= Attribute_Object_Size
1604 and then Id /= Attribute_Value_Size
1605 and then not From_At_Mod (N)
1607 Error_Msg_N ("cannot specify attribute for subtype", Nam);
1611 Set_Entity (N, U_Ent);
1613 -- Switch on particular attribute
1621 -- Address attribute definition clause
1623 when Attribute_Address => Address : begin
1625 -- A little error check, catch for X'Address use X'Address;
1627 if Nkind (Nam) = N_Identifier
1628 and then Nkind (Expr) = N_Attribute_Reference
1629 and then Attribute_Name (Expr) = Name_Address
1630 and then Nkind (Prefix (Expr)) = N_Identifier
1631 and then Chars (Nam) = Chars (Prefix (Expr))
1634 ("address for & is self-referencing", Prefix (Expr), Ent);
1638 -- Not that special case, carry on with analysis of expression
1640 Analyze_And_Resolve (Expr, RTE (RE_Address));
1642 -- Even when ignoring rep clauses we need to indicate that the
1643 -- entity has an address clause and thus it is legal to declare
1646 if Ignore_Rep_Clauses then
1647 if Ekind_In (U_Ent, E_Variable, E_Constant) then
1648 Record_Rep_Item (U_Ent, N);
1654 if Duplicate_Clause then
1657 -- Case of address clause for subprogram
1659 elsif Is_Subprogram (U_Ent) then
1660 if Has_Homonym (U_Ent) then
1662 ("address clause cannot be given " &
1663 "for overloaded subprogram",
1668 -- For subprograms, all address clauses are permitted, and we
1669 -- mark the subprogram as having a deferred freeze so that Gigi
1670 -- will not elaborate it too soon.
1672 -- Above needs more comments, what is too soon about???
1674 Set_Has_Delayed_Freeze (U_Ent);
1676 -- Case of address clause for entry
1678 elsif Ekind (U_Ent) = E_Entry then
1679 if Nkind (Parent (N)) = N_Task_Body then
1681 ("entry address must be specified in task spec", Nam);
1685 -- For entries, we require a constant address
1687 Check_Constant_Address_Clause (Expr, U_Ent);
1689 -- Special checks for task types
1691 if Is_Task_Type (Scope (U_Ent))
1692 and then Comes_From_Source (Scope (U_Ent))
1695 ("?entry address declared for entry in task type", N);
1697 ("\?only one task can be declared of this type", N);
1700 -- Entry address clauses are obsolescent
1702 Check_Restriction (No_Obsolescent_Features, N);
1704 if Warn_On_Obsolescent_Feature then
1706 ("attaching interrupt to task entry is an " &
1707 "obsolescent feature (RM J.7.1)?", N);
1709 ("\use interrupt procedure instead?", N);
1712 -- Case of an address clause for a controlled object which we
1713 -- consider to be erroneous.
1715 elsif Is_Controlled (Etype (U_Ent))
1716 or else Has_Controlled_Component (Etype (U_Ent))
1719 ("?controlled object& must not be overlaid", Nam, U_Ent);
1721 ("\?Program_Error will be raised at run time", Nam);
1722 Insert_Action (Declaration_Node (U_Ent),
1723 Make_Raise_Program_Error (Loc,
1724 Reason => PE_Overlaid_Controlled_Object));
1727 -- Case of address clause for a (non-controlled) object
1730 Ekind (U_Ent) = E_Variable
1732 Ekind (U_Ent) = E_Constant
1735 Expr : constant Node_Id := Expression (N);
1740 -- Exported variables cannot have an address clause, because
1741 -- this cancels the effect of the pragma Export.
1743 if Is_Exported (U_Ent) then
1745 ("cannot export object with address clause", Nam);
1749 Find_Overlaid_Entity (N, O_Ent, Off);
1751 -- Overlaying controlled objects is erroneous
1754 and then (Has_Controlled_Component (Etype (O_Ent))
1755 or else Is_Controlled (Etype (O_Ent)))
1758 ("?cannot overlay with controlled object", Expr);
1760 ("\?Program_Error will be raised at run time", Expr);
1761 Insert_Action (Declaration_Node (U_Ent),
1762 Make_Raise_Program_Error (Loc,
1763 Reason => PE_Overlaid_Controlled_Object));
1766 elsif Present (O_Ent)
1767 and then Ekind (U_Ent) = E_Constant
1768 and then not Is_Constant_Object (O_Ent)
1770 Error_Msg_N ("constant overlays a variable?", Expr);
1772 elsif Present (Renamed_Object (U_Ent)) then
1774 ("address clause not allowed"
1775 & " for a renaming declaration (RM 13.1(6))", Nam);
1778 -- Imported variables can have an address clause, but then
1779 -- the import is pretty meaningless except to suppress
1780 -- initializations, so we do not need such variables to
1781 -- be statically allocated (and in fact it causes trouble
1782 -- if the address clause is a local value).
1784 elsif Is_Imported (U_Ent) then
1785 Set_Is_Statically_Allocated (U_Ent, False);
1788 -- We mark a possible modification of a variable with an
1789 -- address clause, since it is likely aliasing is occurring.
1791 Note_Possible_Modification (Nam, Sure => False);
1793 -- Here we are checking for explicit overlap of one variable
1794 -- by another, and if we find this then mark the overlapped
1795 -- variable as also being volatile to prevent unwanted
1796 -- optimizations. This is a significant pessimization so
1797 -- avoid it when there is an offset, i.e. when the object
1798 -- is composite; they cannot be optimized easily anyway.
1801 and then Is_Object (O_Ent)
1804 Set_Treat_As_Volatile (O_Ent);
1807 -- Legality checks on the address clause for initialized
1808 -- objects is deferred until the freeze point, because
1809 -- a subsequent pragma might indicate that the object is
1810 -- imported and thus not initialized.
1812 Set_Has_Delayed_Freeze (U_Ent);
1814 -- If an initialization call has been generated for this
1815 -- object, it needs to be deferred to after the freeze node
1816 -- we have just now added, otherwise GIGI will see a
1817 -- reference to the variable (as actual to the IP call)
1818 -- before its definition.
1821 Init_Call : constant Node_Id := Find_Init_Call (U_Ent, N);
1823 if Present (Init_Call) then
1825 Append_Freeze_Action (U_Ent, Init_Call);
1829 if Is_Exported (U_Ent) then
1831 ("& cannot be exported if an address clause is given",
1834 ("\define and export a variable " &
1835 "that holds its address instead",
1839 -- Entity has delayed freeze, so we will generate an
1840 -- alignment check at the freeze point unless suppressed.
1842 if not Range_Checks_Suppressed (U_Ent)
1843 and then not Alignment_Checks_Suppressed (U_Ent)
1845 Set_Check_Address_Alignment (N);
1848 -- Kill the size check code, since we are not allocating
1849 -- the variable, it is somewhere else.
1851 Kill_Size_Check_Code (U_Ent);
1853 -- If the address clause is of the form:
1855 -- for Y'Address use X'Address
1859 -- Const : constant Address := X'Address;
1861 -- for Y'Address use Const;
1863 -- then we make an entry in the table for checking the size
1864 -- and alignment of the overlaying variable. We defer this
1865 -- check till after code generation to take full advantage
1866 -- of the annotation done by the back end. This entry is
1867 -- only made if the address clause comes from source.
1868 -- If the entity has a generic type, the check will be
1869 -- performed in the instance if the actual type justifies
1870 -- it, and we do not insert the clause in the table to
1871 -- prevent spurious warnings.
1873 if Address_Clause_Overlay_Warnings
1874 and then Comes_From_Source (N)
1875 and then Present (O_Ent)
1876 and then Is_Object (O_Ent)
1878 if not Is_Generic_Type (Etype (U_Ent)) then
1879 Address_Clause_Checks.Append ((N, U_Ent, O_Ent, Off));
1882 -- If variable overlays a constant view, and we are
1883 -- warning on overlays, then mark the variable as
1884 -- overlaying a constant (we will give warnings later
1885 -- if this variable is assigned).
1887 if Is_Constant_Object (O_Ent)
1888 and then Ekind (U_Ent) = E_Variable
1890 Set_Overlays_Constant (U_Ent);
1895 -- Not a valid entity for an address clause
1898 Error_Msg_N ("address cannot be given for &", Nam);
1906 -- Alignment attribute definition clause
1908 when Attribute_Alignment => Alignment : declare
1909 Align : constant Uint := Get_Alignment_Value (Expr);
1914 if not Is_Type (U_Ent)
1915 and then Ekind (U_Ent) /= E_Variable
1916 and then Ekind (U_Ent) /= E_Constant
1918 Error_Msg_N ("alignment cannot be given for &", Nam);
1920 elsif Duplicate_Clause then
1923 elsif Align /= No_Uint then
1924 Set_Has_Alignment_Clause (U_Ent);
1925 Set_Alignment (U_Ent, Align);
1927 -- For an array type, U_Ent is the first subtype. In that case,
1928 -- also set the alignment of the anonymous base type so that
1929 -- other subtypes (such as the itypes for aggregates of the
1930 -- type) also receive the expected alignment.
1932 if Is_Array_Type (U_Ent) then
1933 Set_Alignment (Base_Type (U_Ent), Align);
1942 -- Bit_Order attribute definition clause
1944 when Attribute_Bit_Order => Bit_Order : declare
1946 if not Is_Record_Type (U_Ent) then
1948 ("Bit_Order can only be defined for record type", Nam);
1950 elsif Duplicate_Clause then
1954 Analyze_And_Resolve (Expr, RTE (RE_Bit_Order));
1956 if Etype (Expr) = Any_Type then
1959 elsif not Is_Static_Expression (Expr) then
1960 Flag_Non_Static_Expr
1961 ("Bit_Order requires static expression!", Expr);
1964 if (Expr_Value (Expr) = 0) /= Bytes_Big_Endian then
1965 Set_Reverse_Bit_Order (U_Ent, True);
1971 --------------------
1972 -- Component_Size --
1973 --------------------
1975 -- Component_Size attribute definition clause
1977 when Attribute_Component_Size => Component_Size_Case : declare
1978 Csize : constant Uint := Static_Integer (Expr);
1982 New_Ctyp : Entity_Id;
1986 if not Is_Array_Type (U_Ent) then
1987 Error_Msg_N ("component size requires array type", Nam);
1991 Btype := Base_Type (U_Ent);
1992 Ctyp := Component_Type (Btype);
1994 if Duplicate_Clause then
1997 elsif Rep_Item_Too_Early (Btype, N) then
2000 elsif Csize /= No_Uint then
2001 Check_Size (Expr, Ctyp, Csize, Biased);
2003 -- For the biased case, build a declaration for a subtype that
2004 -- will be used to represent the biased subtype that reflects
2005 -- the biased representation of components. We need the subtype
2006 -- to get proper conversions on referencing elements of the
2007 -- array. Note: component size clauses are ignored in VM mode.
2009 if VM_Target = No_VM then
2012 Make_Defining_Identifier (Loc,
2014 New_External_Name (Chars (U_Ent), 'C', 0, 'T'));
2017 Make_Subtype_Declaration (Loc,
2018 Defining_Identifier => New_Ctyp,
2019 Subtype_Indication =>
2020 New_Occurrence_Of (Component_Type (Btype), Loc));
2022 Set_Parent (Decl, N);
2023 Analyze (Decl, Suppress => All_Checks);
2025 Set_Has_Delayed_Freeze (New_Ctyp, False);
2026 Set_Esize (New_Ctyp, Csize);
2027 Set_RM_Size (New_Ctyp, Csize);
2028 Init_Alignment (New_Ctyp);
2029 Set_Is_Itype (New_Ctyp, True);
2030 Set_Associated_Node_For_Itype (New_Ctyp, U_Ent);
2032 Set_Component_Type (Btype, New_Ctyp);
2033 Set_Biased (New_Ctyp, N, "component size clause");
2036 Set_Component_Size (Btype, Csize);
2038 -- For VM case, we ignore component size clauses
2041 -- Give a warning unless we are in GNAT mode, in which case
2042 -- the warning is suppressed since it is not useful.
2044 if not GNAT_Mode then
2046 ("?component size ignored in this configuration", N);
2050 -- Deal with warning on overridden size
2052 if Warn_On_Overridden_Size
2053 and then Has_Size_Clause (Ctyp)
2054 and then RM_Size (Ctyp) /= Csize
2057 ("?component size overrides size clause for&",
2061 Set_Has_Component_Size_Clause (Btype, True);
2062 Set_Has_Non_Standard_Rep (Btype, True);
2064 end Component_Size_Case;
2070 when Attribute_External_Tag => External_Tag :
2072 if not Is_Tagged_Type (U_Ent) then
2073 Error_Msg_N ("should be a tagged type", Nam);
2076 if Duplicate_Clause then
2080 Analyze_And_Resolve (Expr, Standard_String);
2082 if not Is_Static_Expression (Expr) then
2083 Flag_Non_Static_Expr
2084 ("static string required for tag name!", Nam);
2087 if VM_Target = No_VM then
2088 Set_Has_External_Tag_Rep_Clause (U_Ent);
2090 Error_Msg_Name_1 := Attr;
2092 ("% attribute unsupported in this configuration", Nam);
2095 if not Is_Library_Level_Entity (U_Ent) then
2097 ("?non-unique external tag supplied for &", N, U_Ent);
2099 ("?\same external tag applies to all subprogram calls", N);
2101 ("?\corresponding internal tag cannot be obtained", N);
2110 when Attribute_Input =>
2111 Analyze_Stream_TSS_Definition (TSS_Stream_Input);
2112 Set_Has_Specified_Stream_Input (Ent);
2118 -- Machine radix attribute definition clause
2120 when Attribute_Machine_Radix => Machine_Radix : declare
2121 Radix : constant Uint := Static_Integer (Expr);
2124 if not Is_Decimal_Fixed_Point_Type (U_Ent) then
2125 Error_Msg_N ("decimal fixed-point type expected for &", Nam);
2127 elsif Duplicate_Clause then
2130 elsif Radix /= No_Uint then
2131 Set_Has_Machine_Radix_Clause (U_Ent);
2132 Set_Has_Non_Standard_Rep (Base_Type (U_Ent));
2136 elsif Radix = 10 then
2137 Set_Machine_Radix_10 (U_Ent);
2139 Error_Msg_N ("machine radix value must be 2 or 10", Expr);
2148 -- Object_Size attribute definition clause
2150 when Attribute_Object_Size => Object_Size : declare
2151 Size : constant Uint := Static_Integer (Expr);
2154 pragma Warnings (Off, Biased);
2157 if not Is_Type (U_Ent) then
2158 Error_Msg_N ("Object_Size cannot be given for &", Nam);
2160 elsif Duplicate_Clause then
2164 Check_Size (Expr, U_Ent, Size, Biased);
2172 UI_Mod (Size, 64) /= 0
2175 ("Object_Size must be 8, 16, 32, or multiple of 64",
2179 Set_Esize (U_Ent, Size);
2180 Set_Has_Object_Size_Clause (U_Ent);
2181 Alignment_Check_For_Esize_Change (U_Ent);
2189 when Attribute_Output =>
2190 Analyze_Stream_TSS_Definition (TSS_Stream_Output);
2191 Set_Has_Specified_Stream_Output (Ent);
2197 when Attribute_Read =>
2198 Analyze_Stream_TSS_Definition (TSS_Stream_Read);
2199 Set_Has_Specified_Stream_Read (Ent);
2205 -- Size attribute definition clause
2207 when Attribute_Size => Size : declare
2208 Size : constant Uint := Static_Integer (Expr);
2215 if Duplicate_Clause then
2218 elsif not Is_Type (U_Ent)
2219 and then Ekind (U_Ent) /= E_Variable
2220 and then Ekind (U_Ent) /= E_Constant
2222 Error_Msg_N ("size cannot be given for &", Nam);
2224 elsif Is_Array_Type (U_Ent)
2225 and then not Is_Constrained (U_Ent)
2228 ("size cannot be given for unconstrained array", Nam);
2230 elsif Size /= No_Uint then
2232 if VM_Target /= No_VM and then not GNAT_Mode then
2234 -- Size clause is not handled properly on VM targets.
2235 -- Display a warning unless we are in GNAT mode, in which
2236 -- case this is useless.
2239 ("?size clauses are ignored in this configuration", N);
2242 if Is_Type (U_Ent) then
2245 Etyp := Etype (U_Ent);
2248 -- Check size, note that Gigi is in charge of checking that the
2249 -- size of an array or record type is OK. Also we do not check
2250 -- the size in the ordinary fixed-point case, since it is too
2251 -- early to do so (there may be subsequent small clause that
2252 -- affects the size). We can check the size if a small clause
2253 -- has already been given.
2255 if not Is_Ordinary_Fixed_Point_Type (U_Ent)
2256 or else Has_Small_Clause (U_Ent)
2258 Check_Size (Expr, Etyp, Size, Biased);
2259 Set_Biased (U_Ent, N, "size clause", Biased);
2262 -- For types set RM_Size and Esize if possible
2264 if Is_Type (U_Ent) then
2265 Set_RM_Size (U_Ent, Size);
2267 -- For scalar types, increase Object_Size to power of 2, but
2268 -- not less than a storage unit in any case (i.e., normally
2269 -- this means it will be byte addressable).
2271 if Is_Scalar_Type (U_Ent) then
2272 if Size <= System_Storage_Unit then
2273 Init_Esize (U_Ent, System_Storage_Unit);
2274 elsif Size <= 16 then
2275 Init_Esize (U_Ent, 16);
2276 elsif Size <= 32 then
2277 Init_Esize (U_Ent, 32);
2279 Set_Esize (U_Ent, (Size + 63) / 64 * 64);
2282 -- For all other types, object size = value size. The
2283 -- backend will adjust as needed.
2286 Set_Esize (U_Ent, Size);
2289 Alignment_Check_For_Esize_Change (U_Ent);
2291 -- For objects, set Esize only
2294 if Is_Elementary_Type (Etyp) then
2295 if Size /= System_Storage_Unit
2297 Size /= System_Storage_Unit * 2
2299 Size /= System_Storage_Unit * 4
2301 Size /= System_Storage_Unit * 8
2303 Error_Msg_Uint_1 := UI_From_Int (System_Storage_Unit);
2304 Error_Msg_Uint_2 := Error_Msg_Uint_1 * 8;
2306 ("size for primitive object must be a power of 2"
2307 & " in the range ^-^", N);
2311 Set_Esize (U_Ent, Size);
2314 Set_Has_Size_Clause (U_Ent);
2322 -- Small attribute definition clause
2324 when Attribute_Small => Small : declare
2325 Implicit_Base : constant Entity_Id := Base_Type (U_Ent);
2329 Analyze_And_Resolve (Expr, Any_Real);
2331 if Etype (Expr) = Any_Type then
2334 elsif not Is_Static_Expression (Expr) then
2335 Flag_Non_Static_Expr
2336 ("small requires static expression!", Expr);
2340 Small := Expr_Value_R (Expr);
2342 if Small <= Ureal_0 then
2343 Error_Msg_N ("small value must be greater than zero", Expr);
2349 if not Is_Ordinary_Fixed_Point_Type (U_Ent) then
2351 ("small requires an ordinary fixed point type", Nam);
2353 elsif Has_Small_Clause (U_Ent) then
2354 Error_Msg_N ("small already given for &", Nam);
2356 elsif Small > Delta_Value (U_Ent) then
2358 ("small value must not be greater then delta value", Nam);
2361 Set_Small_Value (U_Ent, Small);
2362 Set_Small_Value (Implicit_Base, Small);
2363 Set_Has_Small_Clause (U_Ent);
2364 Set_Has_Small_Clause (Implicit_Base);
2365 Set_Has_Non_Standard_Rep (Implicit_Base);
2373 -- Storage_Pool attribute definition clause
2375 when Attribute_Storage_Pool => Storage_Pool : declare
2380 if Ekind (U_Ent) = E_Access_Subprogram_Type then
2382 ("storage pool cannot be given for access-to-subprogram type",
2387 Ekind_In (U_Ent, E_Access_Type, E_General_Access_Type)
2390 ("storage pool can only be given for access types", Nam);
2393 elsif Is_Derived_Type (U_Ent) then
2395 ("storage pool cannot be given for a derived access type",
2398 elsif Duplicate_Clause then
2401 elsif Present (Associated_Storage_Pool (U_Ent)) then
2402 Error_Msg_N ("storage pool already given for &", Nam);
2407 (Expr, Class_Wide_Type (RTE (RE_Root_Storage_Pool)));
2409 if not Denotes_Variable (Expr) then
2410 Error_Msg_N ("storage pool must be a variable", Expr);
2414 if Nkind (Expr) = N_Type_Conversion then
2415 T := Etype (Expression (Expr));
2420 -- The Stack_Bounded_Pool is used internally for implementing
2421 -- access types with a Storage_Size. Since it only work
2422 -- properly when used on one specific type, we need to check
2423 -- that it is not hijacked improperly:
2424 -- type T is access Integer;
2425 -- for T'Storage_Size use n;
2426 -- type Q is access Float;
2427 -- for Q'Storage_Size use T'Storage_Size; -- incorrect
2429 if RTE_Available (RE_Stack_Bounded_Pool)
2430 and then Base_Type (T) = RTE (RE_Stack_Bounded_Pool)
2432 Error_Msg_N ("non-shareable internal Pool", Expr);
2436 -- If the argument is a name that is not an entity name, then
2437 -- we construct a renaming operation to define an entity of
2438 -- type storage pool.
2440 if not Is_Entity_Name (Expr)
2441 and then Is_Object_Reference (Expr)
2443 Pool := Make_Temporary (Loc, 'P', Expr);
2446 Rnode : constant Node_Id :=
2447 Make_Object_Renaming_Declaration (Loc,
2448 Defining_Identifier => Pool,
2450 New_Occurrence_Of (Etype (Expr), Loc),
2454 Insert_Before (N, Rnode);
2456 Set_Associated_Storage_Pool (U_Ent, Pool);
2459 elsif Is_Entity_Name (Expr) then
2460 Pool := Entity (Expr);
2462 -- If pool is a renamed object, get original one. This can
2463 -- happen with an explicit renaming, and within instances.
2465 while Present (Renamed_Object (Pool))
2466 and then Is_Entity_Name (Renamed_Object (Pool))
2468 Pool := Entity (Renamed_Object (Pool));
2471 if Present (Renamed_Object (Pool))
2472 and then Nkind (Renamed_Object (Pool)) = N_Type_Conversion
2473 and then Is_Entity_Name (Expression (Renamed_Object (Pool)))
2475 Pool := Entity (Expression (Renamed_Object (Pool)));
2478 Set_Associated_Storage_Pool (U_Ent, Pool);
2480 elsif Nkind (Expr) = N_Type_Conversion
2481 and then Is_Entity_Name (Expression (Expr))
2482 and then Nkind (Original_Node (Expr)) = N_Attribute_Reference
2484 Pool := Entity (Expression (Expr));
2485 Set_Associated_Storage_Pool (U_Ent, Pool);
2488 Error_Msg_N ("incorrect reference to a Storage Pool", Expr);
2497 -- Storage_Size attribute definition clause
2499 when Attribute_Storage_Size => Storage_Size : declare
2500 Btype : constant Entity_Id := Base_Type (U_Ent);
2504 if Is_Task_Type (U_Ent) then
2505 Check_Restriction (No_Obsolescent_Features, N);
2507 if Warn_On_Obsolescent_Feature then
2509 ("storage size clause for task is an " &
2510 "obsolescent feature (RM J.9)?", N);
2511 Error_Msg_N ("\use Storage_Size pragma instead?", N);
2517 if not Is_Access_Type (U_Ent)
2518 and then Ekind (U_Ent) /= E_Task_Type
2520 Error_Msg_N ("storage size cannot be given for &", Nam);
2522 elsif Is_Access_Type (U_Ent) and Is_Derived_Type (U_Ent) then
2524 ("storage size cannot be given for a derived access type",
2527 elsif Duplicate_Clause then
2531 Analyze_And_Resolve (Expr, Any_Integer);
2533 if Is_Access_Type (U_Ent) then
2534 if Present (Associated_Storage_Pool (U_Ent)) then
2535 Error_Msg_N ("storage pool already given for &", Nam);
2539 if Is_OK_Static_Expression (Expr)
2540 and then Expr_Value (Expr) = 0
2542 Set_No_Pool_Assigned (Btype);
2545 else -- Is_Task_Type (U_Ent)
2546 Sprag := Get_Rep_Pragma (Btype, Name_Storage_Size);
2548 if Present (Sprag) then
2549 Error_Msg_Sloc := Sloc (Sprag);
2551 ("Storage_Size already specified#", Nam);
2556 Set_Has_Storage_Size_Clause (Btype);
2564 when Attribute_Stream_Size => Stream_Size : declare
2565 Size : constant Uint := Static_Integer (Expr);
2568 if Ada_Version <= Ada_95 then
2569 Check_Restriction (No_Implementation_Attributes, N);
2572 if Duplicate_Clause then
2575 elsif Is_Elementary_Type (U_Ent) then
2576 if Size /= System_Storage_Unit
2578 Size /= System_Storage_Unit * 2
2580 Size /= System_Storage_Unit * 4
2582 Size /= System_Storage_Unit * 8
2584 Error_Msg_Uint_1 := UI_From_Int (System_Storage_Unit);
2586 ("stream size for elementary type must be a"
2587 & " power of 2 and at least ^", N);
2589 elsif RM_Size (U_Ent) > Size then
2590 Error_Msg_Uint_1 := RM_Size (U_Ent);
2592 ("stream size for elementary type must be a"
2593 & " power of 2 and at least ^", N);
2596 Set_Has_Stream_Size_Clause (U_Ent);
2599 Error_Msg_N ("Stream_Size cannot be given for &", Nam);
2607 -- Value_Size attribute definition clause
2609 when Attribute_Value_Size => Value_Size : declare
2610 Size : constant Uint := Static_Integer (Expr);
2614 if not Is_Type (U_Ent) then
2615 Error_Msg_N ("Value_Size cannot be given for &", Nam);
2617 elsif Duplicate_Clause then
2620 elsif Is_Array_Type (U_Ent)
2621 and then not Is_Constrained (U_Ent)
2624 ("Value_Size cannot be given for unconstrained array", Nam);
2627 if Is_Elementary_Type (U_Ent) then
2628 Check_Size (Expr, U_Ent, Size, Biased);
2629 Set_Biased (U_Ent, N, "value size clause", Biased);
2632 Set_RM_Size (U_Ent, Size);
2640 when Attribute_Write =>
2641 Analyze_Stream_TSS_Definition (TSS_Stream_Write);
2642 Set_Has_Specified_Stream_Write (Ent);
2644 -- All other attributes cannot be set
2648 ("attribute& cannot be set with definition clause", N);
2651 -- The test for the type being frozen must be performed after
2652 -- any expression the clause has been analyzed since the expression
2653 -- itself might cause freezing that makes the clause illegal.
2655 if Rep_Item_Too_Late (U_Ent, N, FOnly) then
2658 end Analyze_Attribute_Definition_Clause;
2660 ----------------------------
2661 -- Analyze_Code_Statement --
2662 ----------------------------
2664 procedure Analyze_Code_Statement (N : Node_Id) is
2665 HSS : constant Node_Id := Parent (N);
2666 SBody : constant Node_Id := Parent (HSS);
2667 Subp : constant Entity_Id := Current_Scope;
2674 -- Analyze and check we get right type, note that this implements the
2675 -- requirement (RM 13.8(1)) that Machine_Code be with'ed, since that
2676 -- is the only way that Asm_Insn could possibly be visible.
2678 Analyze_And_Resolve (Expression (N));
2680 if Etype (Expression (N)) = Any_Type then
2682 elsif Etype (Expression (N)) /= RTE (RE_Asm_Insn) then
2683 Error_Msg_N ("incorrect type for code statement", N);
2687 Check_Code_Statement (N);
2689 -- Make sure we appear in the handled statement sequence of a
2690 -- subprogram (RM 13.8(3)).
2692 if Nkind (HSS) /= N_Handled_Sequence_Of_Statements
2693 or else Nkind (SBody) /= N_Subprogram_Body
2696 ("code statement can only appear in body of subprogram", N);
2700 -- Do remaining checks (RM 13.8(3)) if not already done
2702 if not Is_Machine_Code_Subprogram (Subp) then
2703 Set_Is_Machine_Code_Subprogram (Subp);
2705 -- No exception handlers allowed
2707 if Present (Exception_Handlers (HSS)) then
2709 ("exception handlers not permitted in machine code subprogram",
2710 First (Exception_Handlers (HSS)));
2713 -- No declarations other than use clauses and pragmas (we allow
2714 -- certain internally generated declarations as well).
2716 Decl := First (Declarations (SBody));
2717 while Present (Decl) loop
2718 DeclO := Original_Node (Decl);
2719 if Comes_From_Source (DeclO)
2720 and not Nkind_In (DeclO, N_Pragma,
2721 N_Use_Package_Clause,
2723 N_Implicit_Label_Declaration)
2726 ("this declaration not allowed in machine code subprogram",
2733 -- No statements other than code statements, pragmas, and labels.
2734 -- Again we allow certain internally generated statements.
2736 Stmt := First (Statements (HSS));
2737 while Present (Stmt) loop
2738 StmtO := Original_Node (Stmt);
2739 if Comes_From_Source (StmtO)
2740 and then not Nkind_In (StmtO, N_Pragma,
2745 ("this statement is not allowed in machine code subprogram",
2752 end Analyze_Code_Statement;
2754 -----------------------------------------------
2755 -- Analyze_Enumeration_Representation_Clause --
2756 -----------------------------------------------
2758 procedure Analyze_Enumeration_Representation_Clause (N : Node_Id) is
2759 Ident : constant Node_Id := Identifier (N);
2760 Aggr : constant Node_Id := Array_Aggregate (N);
2761 Enumtype : Entity_Id;
2767 Err : Boolean := False;
2769 Lo : constant Uint := Expr_Value (Type_Low_Bound (Universal_Integer));
2770 Hi : constant Uint := Expr_Value (Type_High_Bound (Universal_Integer));
2771 -- Allowed range of universal integer (= allowed range of enum lit vals)
2775 -- Minimum and maximum values of entries
2778 -- Pointer to node for literal providing max value
2781 if Ignore_Rep_Clauses then
2785 -- First some basic error checks
2788 Enumtype := Entity (Ident);
2790 if Enumtype = Any_Type
2791 or else Rep_Item_Too_Early (Enumtype, N)
2795 Enumtype := Underlying_Type (Enumtype);
2798 if not Is_Enumeration_Type (Enumtype) then
2800 ("enumeration type required, found}",
2801 Ident, First_Subtype (Enumtype));
2805 -- Ignore rep clause on generic actual type. This will already have
2806 -- been flagged on the template as an error, and this is the safest
2807 -- way to ensure we don't get a junk cascaded message in the instance.
2809 if Is_Generic_Actual_Type (Enumtype) then
2812 -- Type must be in current scope
2814 elsif Scope (Enumtype) /= Current_Scope then
2815 Error_Msg_N ("type must be declared in this scope", Ident);
2818 -- Type must be a first subtype
2820 elsif not Is_First_Subtype (Enumtype) then
2821 Error_Msg_N ("cannot give enumeration rep clause for subtype", N);
2824 -- Ignore duplicate rep clause
2826 elsif Has_Enumeration_Rep_Clause (Enumtype) then
2827 Error_Msg_N ("duplicate enumeration rep clause ignored", N);
2830 -- Don't allow rep clause for standard [wide_[wide_]]character
2832 elsif Is_Standard_Character_Type (Enumtype) then
2833 Error_Msg_N ("enumeration rep clause not allowed for this type", N);
2836 -- Check that the expression is a proper aggregate (no parentheses)
2838 elsif Paren_Count (Aggr) /= 0 then
2840 ("extra parentheses surrounding aggregate not allowed",
2844 -- All tests passed, so set rep clause in place
2847 Set_Has_Enumeration_Rep_Clause (Enumtype);
2848 Set_Has_Enumeration_Rep_Clause (Base_Type (Enumtype));
2851 -- Now we process the aggregate. Note that we don't use the normal
2852 -- aggregate code for this purpose, because we don't want any of the
2853 -- normal expansion activities, and a number of special semantic
2854 -- rules apply (including the component type being any integer type)
2856 Elit := First_Literal (Enumtype);
2858 -- First the positional entries if any
2860 if Present (Expressions (Aggr)) then
2861 Expr := First (Expressions (Aggr));
2862 while Present (Expr) loop
2864 Error_Msg_N ("too many entries in aggregate", Expr);
2868 Val := Static_Integer (Expr);
2870 -- Err signals that we found some incorrect entries processing
2871 -- the list. The final checks for completeness and ordering are
2872 -- skipped in this case.
2874 if Val = No_Uint then
2876 elsif Val < Lo or else Hi < Val then
2877 Error_Msg_N ("value outside permitted range", Expr);
2881 Set_Enumeration_Rep (Elit, Val);
2882 Set_Enumeration_Rep_Expr (Elit, Expr);
2888 -- Now process the named entries if present
2890 if Present (Component_Associations (Aggr)) then
2891 Assoc := First (Component_Associations (Aggr));
2892 while Present (Assoc) loop
2893 Choice := First (Choices (Assoc));
2895 if Present (Next (Choice)) then
2897 ("multiple choice not allowed here", Next (Choice));
2901 if Nkind (Choice) = N_Others_Choice then
2902 Error_Msg_N ("others choice not allowed here", Choice);
2905 elsif Nkind (Choice) = N_Range then
2906 -- ??? should allow zero/one element range here
2907 Error_Msg_N ("range not allowed here", Choice);
2911 Analyze_And_Resolve (Choice, Enumtype);
2913 if Is_Entity_Name (Choice)
2914 and then Is_Type (Entity (Choice))
2916 Error_Msg_N ("subtype name not allowed here", Choice);
2918 -- ??? should allow static subtype with zero/one entry
2920 elsif Etype (Choice) = Base_Type (Enumtype) then
2921 if not Is_Static_Expression (Choice) then
2922 Flag_Non_Static_Expr
2923 ("non-static expression used for choice!", Choice);
2927 Elit := Expr_Value_E (Choice);
2929 if Present (Enumeration_Rep_Expr (Elit)) then
2930 Error_Msg_Sloc := Sloc (Enumeration_Rep_Expr (Elit));
2932 ("representation for& previously given#",
2937 Set_Enumeration_Rep_Expr (Elit, Expression (Assoc));
2939 Expr := Expression (Assoc);
2940 Val := Static_Integer (Expr);
2942 if Val = No_Uint then
2945 elsif Val < Lo or else Hi < Val then
2946 Error_Msg_N ("value outside permitted range", Expr);
2950 Set_Enumeration_Rep (Elit, Val);
2959 -- Aggregate is fully processed. Now we check that a full set of
2960 -- representations was given, and that they are in range and in order.
2961 -- These checks are only done if no other errors occurred.
2967 Elit := First_Literal (Enumtype);
2968 while Present (Elit) loop
2969 if No (Enumeration_Rep_Expr (Elit)) then
2970 Error_Msg_NE ("missing representation for&!", N, Elit);
2973 Val := Enumeration_Rep (Elit);
2975 if Min = No_Uint then
2979 if Val /= No_Uint then
2980 if Max /= No_Uint and then Val <= Max then
2982 ("enumeration value for& not ordered!",
2983 Enumeration_Rep_Expr (Elit), Elit);
2986 Max_Node := Enumeration_Rep_Expr (Elit);
2990 -- If there is at least one literal whose representation is not
2991 -- equal to the Pos value, then note that this enumeration type
2992 -- has a non-standard representation.
2994 if Val /= Enumeration_Pos (Elit) then
2995 Set_Has_Non_Standard_Rep (Base_Type (Enumtype));
3002 -- Now set proper size information
3005 Minsize : Uint := UI_From_Int (Minimum_Size (Enumtype));
3008 if Has_Size_Clause (Enumtype) then
3010 -- All OK, if size is OK now
3012 if RM_Size (Enumtype) >= Minsize then
3016 -- Try if we can get by with biasing
3019 UI_From_Int (Minimum_Size (Enumtype, Biased => True));
3021 -- Error message if even biasing does not work
3023 if RM_Size (Enumtype) < Minsize then
3024 Error_Msg_Uint_1 := RM_Size (Enumtype);
3025 Error_Msg_Uint_2 := Max;
3027 ("previously given size (^) is too small "
3028 & "for this value (^)", Max_Node);
3030 -- If biasing worked, indicate that we now have biased rep
3034 (Enumtype, Size_Clause (Enumtype), "size clause");
3039 Set_RM_Size (Enumtype, Minsize);
3040 Set_Enum_Esize (Enumtype);
3043 Set_RM_Size (Base_Type (Enumtype), RM_Size (Enumtype));
3044 Set_Esize (Base_Type (Enumtype), Esize (Enumtype));
3045 Set_Alignment (Base_Type (Enumtype), Alignment (Enumtype));
3049 -- We repeat the too late test in case it froze itself!
3051 if Rep_Item_Too_Late (Enumtype, N) then
3054 end Analyze_Enumeration_Representation_Clause;
3056 ----------------------------
3057 -- Analyze_Free_Statement --
3058 ----------------------------
3060 procedure Analyze_Free_Statement (N : Node_Id) is
3062 Analyze (Expression (N));
3063 end Analyze_Free_Statement;
3065 ---------------------------
3066 -- Analyze_Freeze_Entity --
3067 ---------------------------
3069 procedure Analyze_Freeze_Entity (N : Node_Id) is
3070 E : constant Entity_Id := Entity (N);
3073 -- Remember that we are processing a freezing entity. Required to
3074 -- ensure correct decoration of internal entities associated with
3075 -- interfaces (see New_Overloaded_Entity).
3077 Inside_Freezing_Actions := Inside_Freezing_Actions + 1;
3079 -- For tagged types covering interfaces add internal entities that link
3080 -- the primitives of the interfaces with the primitives that cover them.
3081 -- Note: These entities were originally generated only when generating
3082 -- code because their main purpose was to provide support to initialize
3083 -- the secondary dispatch tables. They are now generated also when
3084 -- compiling with no code generation to provide ASIS the relationship
3085 -- between interface primitives and tagged type primitives. They are
3086 -- also used to locate primitives covering interfaces when processing
3087 -- generics (see Derive_Subprograms).
3089 if Ada_Version >= Ada_2005
3090 and then Ekind (E) = E_Record_Type
3091 and then Is_Tagged_Type (E)
3092 and then not Is_Interface (E)
3093 and then Has_Interfaces (E)
3095 -- This would be a good common place to call the routine that checks
3096 -- overriding of interface primitives (and thus factorize calls to
3097 -- Check_Abstract_Overriding located at different contexts in the
3098 -- compiler). However, this is not possible because it causes
3099 -- spurious errors in case of late overriding.
3101 Add_Internal_Interface_Entities (E);
3106 if Ekind (E) = E_Record_Type
3107 and then Is_CPP_Class (E)
3108 and then Is_Tagged_Type (E)
3109 and then Tagged_Type_Expansion
3110 and then Expander_Active
3112 if CPP_Num_Prims (E) = 0 then
3114 -- If the CPP type has user defined components then it must import
3115 -- primitives from C++. This is required because if the C++ class
3116 -- has no primitives then the C++ compiler does not added the _tag
3117 -- component to the type.
3119 pragma Assert (Chars (First_Entity (E)) = Name_uTag);
3121 if First_Entity (E) /= Last_Entity (E) then
3123 ("?'C'P'P type must import at least one primitive from C++",
3128 -- Check that all its primitives are abstract or imported from C++.
3129 -- Check also availability of the C++ constructor.
3132 Has_Constructors : constant Boolean := Has_CPP_Constructors (E);
3134 Error_Reported : Boolean := False;
3138 Elmt := First_Elmt (Primitive_Operations (E));
3139 while Present (Elmt) loop
3140 Prim := Node (Elmt);
3142 if Comes_From_Source (Prim) then
3143 if Is_Abstract_Subprogram (Prim) then
3146 elsif not Is_Imported (Prim)
3147 or else Convention (Prim) /= Convention_CPP
3150 ("?primitives of 'C'P'P types must be imported from C++"
3151 & " or abstract", Prim);
3153 elsif not Has_Constructors
3154 and then not Error_Reported
3156 Error_Msg_Name_1 := Chars (E);
3158 ("?'C'P'P constructor required for type %", Prim);
3159 Error_Reported := True;
3168 Inside_Freezing_Actions := Inside_Freezing_Actions - 1;
3170 -- If we have a type with predicates, build predicate function
3172 if Is_Type (E) and then Has_Predicates (E) then
3173 Build_Predicate_Function (E, N);
3176 -- If type has delayed aspects, this is where we do the preanalysis
3177 -- at the freeze point, as part of the consistent visibility check.
3178 -- Note that this must be done after calling Build_Predicate_Function,
3179 -- since that call marks occurrences of the subtype name in the saved
3180 -- expression so that they will not cause trouble in the preanalysis.
3182 if Has_Delayed_Aspects (E) then
3187 -- Look for aspect specification entries for this entity
3189 Ritem := First_Rep_Item (E);
3190 while Present (Ritem) loop
3191 if Nkind (Ritem) = N_Aspect_Specification
3192 and then Entity (Ritem) = E
3193 and then Is_Delayed_Aspect (Ritem)
3195 Check_Aspect_At_Freeze_Point (Ritem);
3198 Next_Rep_Item (Ritem);
3202 end Analyze_Freeze_Entity;
3204 ------------------------------------------
3205 -- Analyze_Record_Representation_Clause --
3206 ------------------------------------------
3208 -- Note: we check as much as we can here, but we can't do any checks
3209 -- based on the position values (e.g. overlap checks) until freeze time
3210 -- because especially in Ada 2005 (machine scalar mode), the processing
3211 -- for non-standard bit order can substantially change the positions.
3212 -- See procedure Check_Record_Representation_Clause (called from Freeze)
3213 -- for the remainder of this processing.
3215 procedure Analyze_Record_Representation_Clause (N : Node_Id) is
3216 Ident : constant Node_Id := Identifier (N);
3221 Hbit : Uint := Uint_0;
3225 Rectype : Entity_Id;
3227 CR_Pragma : Node_Id := Empty;
3228 -- Points to N_Pragma node if Complete_Representation pragma present
3231 if Ignore_Rep_Clauses then
3236 Rectype := Entity (Ident);
3238 if Rectype = Any_Type
3239 or else Rep_Item_Too_Early (Rectype, N)
3243 Rectype := Underlying_Type (Rectype);
3246 -- First some basic error checks
3248 if not Is_Record_Type (Rectype) then
3250 ("record type required, found}", Ident, First_Subtype (Rectype));
3253 elsif Scope (Rectype) /= Current_Scope then
3254 Error_Msg_N ("type must be declared in this scope", N);
3257 elsif not Is_First_Subtype (Rectype) then
3258 Error_Msg_N ("cannot give record rep clause for subtype", N);
3261 elsif Has_Record_Rep_Clause (Rectype) then
3262 Error_Msg_N ("duplicate record rep clause ignored", N);
3265 elsif Rep_Item_Too_Late (Rectype, N) then
3269 if Present (Mod_Clause (N)) then
3271 Loc : constant Source_Ptr := Sloc (N);
3272 M : constant Node_Id := Mod_Clause (N);
3273 P : constant List_Id := Pragmas_Before (M);
3277 pragma Warnings (Off, Mod_Val);
3280 Check_Restriction (No_Obsolescent_Features, Mod_Clause (N));
3282 if Warn_On_Obsolescent_Feature then
3284 ("mod clause is an obsolescent feature (RM J.8)?", N);
3286 ("\use alignment attribute definition clause instead?", N);
3293 -- In ASIS_Mode mode, expansion is disabled, but we must convert
3294 -- the Mod clause into an alignment clause anyway, so that the
3295 -- back-end can compute and back-annotate properly the size and
3296 -- alignment of types that may include this record.
3298 -- This seems dubious, this destroys the source tree in a manner
3299 -- not detectable by ASIS ???
3301 if Operating_Mode = Check_Semantics
3305 Make_Attribute_Definition_Clause (Loc,
3306 Name => New_Reference_To (Base_Type (Rectype), Loc),
3307 Chars => Name_Alignment,
3308 Expression => Relocate_Node (Expression (M)));
3310 Set_From_At_Mod (AtM_Nod);
3311 Insert_After (N, AtM_Nod);
3312 Mod_Val := Get_Alignment_Value (Expression (AtM_Nod));
3313 Set_Mod_Clause (N, Empty);
3316 -- Get the alignment value to perform error checking
3318 Mod_Val := Get_Alignment_Value (Expression (M));
3323 -- For untagged types, clear any existing component clauses for the
3324 -- type. If the type is derived, this is what allows us to override
3325 -- a rep clause for the parent. For type extensions, the representation
3326 -- of the inherited components is inherited, so we want to keep previous
3327 -- component clauses for completeness.
3329 if not Is_Tagged_Type (Rectype) then
3330 Comp := First_Component_Or_Discriminant (Rectype);
3331 while Present (Comp) loop
3332 Set_Component_Clause (Comp, Empty);
3333 Next_Component_Or_Discriminant (Comp);
3337 -- All done if no component clauses
3339 CC := First (Component_Clauses (N));
3345 -- A representation like this applies to the base type
3347 Set_Has_Record_Rep_Clause (Base_Type (Rectype));
3348 Set_Has_Non_Standard_Rep (Base_Type (Rectype));
3349 Set_Has_Specified_Layout (Base_Type (Rectype));
3351 -- Process the component clauses
3353 while Present (CC) loop
3357 if Nkind (CC) = N_Pragma then
3360 -- The only pragma of interest is Complete_Representation
3362 if Pragma_Name (CC) = Name_Complete_Representation then
3366 -- Processing for real component clause
3369 Posit := Static_Integer (Position (CC));
3370 Fbit := Static_Integer (First_Bit (CC));
3371 Lbit := Static_Integer (Last_Bit (CC));
3374 and then Fbit /= No_Uint
3375 and then Lbit /= No_Uint
3379 ("position cannot be negative", Position (CC));
3383 ("first bit cannot be negative", First_Bit (CC));
3385 -- The Last_Bit specified in a component clause must not be
3386 -- less than the First_Bit minus one (RM-13.5.1(10)).
3388 elsif Lbit < Fbit - 1 then
3390 ("last bit cannot be less than first bit minus one",
3393 -- Values look OK, so find the corresponding record component
3394 -- Even though the syntax allows an attribute reference for
3395 -- implementation-defined components, GNAT does not allow the
3396 -- tag to get an explicit position.
3398 elsif Nkind (Component_Name (CC)) = N_Attribute_Reference then
3399 if Attribute_Name (Component_Name (CC)) = Name_Tag then
3400 Error_Msg_N ("position of tag cannot be specified", CC);
3402 Error_Msg_N ("illegal component name", CC);
3406 Comp := First_Entity (Rectype);
3407 while Present (Comp) loop
3408 exit when Chars (Comp) = Chars (Component_Name (CC));
3414 -- Maybe component of base type that is absent from
3415 -- statically constrained first subtype.
3417 Comp := First_Entity (Base_Type (Rectype));
3418 while Present (Comp) loop
3419 exit when Chars (Comp) = Chars (Component_Name (CC));
3426 ("component clause is for non-existent field", CC);
3428 -- Ada 2012 (AI05-0026): Any name that denotes a
3429 -- discriminant of an object of an unchecked union type
3430 -- shall not occur within a record_representation_clause.
3432 -- The general restriction of using record rep clauses on
3433 -- Unchecked_Union types has now been lifted. Since it is
3434 -- possible to introduce a record rep clause which mentions
3435 -- the discriminant of an Unchecked_Union in non-Ada 2012
3436 -- code, this check is applied to all versions of the
3439 elsif Ekind (Comp) = E_Discriminant
3440 and then Is_Unchecked_Union (Rectype)
3443 ("cannot reference discriminant of Unchecked_Union",
3444 Component_Name (CC));
3446 elsif Present (Component_Clause (Comp)) then
3448 -- Diagnose duplicate rep clause, or check consistency
3449 -- if this is an inherited component. In a double fault,
3450 -- there may be a duplicate inconsistent clause for an
3451 -- inherited component.
3453 if Scope (Original_Record_Component (Comp)) = Rectype
3454 or else Parent (Component_Clause (Comp)) = N
3456 Error_Msg_Sloc := Sloc (Component_Clause (Comp));
3457 Error_Msg_N ("component clause previously given#", CC);
3461 Rep1 : constant Node_Id := Component_Clause (Comp);
3463 if Intval (Position (Rep1)) /=
3464 Intval (Position (CC))
3465 or else Intval (First_Bit (Rep1)) /=
3466 Intval (First_Bit (CC))
3467 or else Intval (Last_Bit (Rep1)) /=
3468 Intval (Last_Bit (CC))
3470 Error_Msg_N ("component clause inconsistent "
3471 & "with representation of ancestor", CC);
3472 elsif Warn_On_Redundant_Constructs then
3473 Error_Msg_N ("?redundant component clause "
3474 & "for inherited component!", CC);
3479 -- Normal case where this is the first component clause we
3480 -- have seen for this entity, so set it up properly.
3483 -- Make reference for field in record rep clause and set
3484 -- appropriate entity field in the field identifier.
3487 (Comp, Component_Name (CC), Set_Ref => False);
3488 Set_Entity (Component_Name (CC), Comp);
3490 -- Update Fbit and Lbit to the actual bit number
3492 Fbit := Fbit + UI_From_Int (SSU) * Posit;
3493 Lbit := Lbit + UI_From_Int (SSU) * Posit;
3495 if Has_Size_Clause (Rectype)
3496 and then Esize (Rectype) <= Lbit
3499 ("bit number out of range of specified size",
3502 Set_Component_Clause (Comp, CC);
3503 Set_Component_Bit_Offset (Comp, Fbit);
3504 Set_Esize (Comp, 1 + (Lbit - Fbit));
3505 Set_Normalized_First_Bit (Comp, Fbit mod SSU);
3506 Set_Normalized_Position (Comp, Fbit / SSU);
3508 if Warn_On_Overridden_Size
3509 and then Has_Size_Clause (Etype (Comp))
3510 and then RM_Size (Etype (Comp)) /= Esize (Comp)
3513 ("?component size overrides size clause for&",
3514 Component_Name (CC), Etype (Comp));
3517 -- This information is also set in the corresponding
3518 -- component of the base type, found by accessing the
3519 -- Original_Record_Component link if it is present.
3521 Ocomp := Original_Record_Component (Comp);
3528 (Component_Name (CC),
3534 (Comp, First_Node (CC), "component clause", Biased);
3536 if Present (Ocomp) then
3537 Set_Component_Clause (Ocomp, CC);
3538 Set_Component_Bit_Offset (Ocomp, Fbit);
3539 Set_Normalized_First_Bit (Ocomp, Fbit mod SSU);
3540 Set_Normalized_Position (Ocomp, Fbit / SSU);
3541 Set_Esize (Ocomp, 1 + (Lbit - Fbit));
3543 Set_Normalized_Position_Max
3544 (Ocomp, Normalized_Position (Ocomp));
3546 -- Note: we don't use Set_Biased here, because we
3547 -- already gave a warning above if needed, and we
3548 -- would get a duplicate for the same name here.
3550 Set_Has_Biased_Representation
3551 (Ocomp, Has_Biased_Representation (Comp));
3554 if Esize (Comp) < 0 then
3555 Error_Msg_N ("component size is negative", CC);
3566 -- Check missing components if Complete_Representation pragma appeared
3568 if Present (CR_Pragma) then
3569 Comp := First_Component_Or_Discriminant (Rectype);
3570 while Present (Comp) loop
3571 if No (Component_Clause (Comp)) then
3573 ("missing component clause for &", CR_Pragma, Comp);
3576 Next_Component_Or_Discriminant (Comp);
3579 -- If no Complete_Representation pragma, warn if missing components
3581 elsif Warn_On_Unrepped_Components then
3583 Num_Repped_Components : Nat := 0;
3584 Num_Unrepped_Components : Nat := 0;
3587 -- First count number of repped and unrepped components
3589 Comp := First_Component_Or_Discriminant (Rectype);
3590 while Present (Comp) loop
3591 if Present (Component_Clause (Comp)) then
3592 Num_Repped_Components := Num_Repped_Components + 1;
3594 Num_Unrepped_Components := Num_Unrepped_Components + 1;
3597 Next_Component_Or_Discriminant (Comp);
3600 -- We are only interested in the case where there is at least one
3601 -- unrepped component, and at least half the components have rep
3602 -- clauses. We figure that if less than half have them, then the
3603 -- partial rep clause is really intentional. If the component
3604 -- type has no underlying type set at this point (as for a generic
3605 -- formal type), we don't know enough to give a warning on the
3608 if Num_Unrepped_Components > 0
3609 and then Num_Unrepped_Components < Num_Repped_Components
3611 Comp := First_Component_Or_Discriminant (Rectype);
3612 while Present (Comp) loop
3613 if No (Component_Clause (Comp))
3614 and then Comes_From_Source (Comp)
3615 and then Present (Underlying_Type (Etype (Comp)))
3616 and then (Is_Scalar_Type (Underlying_Type (Etype (Comp)))
3617 or else Size_Known_At_Compile_Time
3618 (Underlying_Type (Etype (Comp))))
3619 and then not Has_Warnings_Off (Rectype)
3621 Error_Msg_Sloc := Sloc (Comp);
3623 ("?no component clause given for & declared #",
3627 Next_Component_Or_Discriminant (Comp);
3632 end Analyze_Record_Representation_Clause;
3634 -------------------------------
3635 -- Build_Invariant_Procedure --
3636 -------------------------------
3638 -- The procedure that is constructed here has the form
3640 -- procedure typInvariant (Ixxx : typ) is
3642 -- pragma Check (Invariant, exp, "failed invariant from xxx");
3643 -- pragma Check (Invariant, exp, "failed invariant from xxx");
3645 -- pragma Check (Invariant, exp, "failed inherited invariant from xxx");
3647 -- end typInvariant;
3649 procedure Build_Invariant_Procedure (Typ : Entity_Id; N : Node_Id) is
3650 Loc : constant Source_Ptr := Sloc (Typ);
3657 Visible_Decls : constant List_Id := Visible_Declarations (N);
3658 Private_Decls : constant List_Id := Private_Declarations (N);
3660 procedure Add_Invariants (T : Entity_Id; Inherit : Boolean);
3661 -- Appends statements to Stmts for any invariants in the rep item chain
3662 -- of the given type. If Inherit is False, then we only process entries
3663 -- on the chain for the type Typ. If Inherit is True, then we ignore any
3664 -- Invariant aspects, but we process all Invariant'Class aspects, adding
3665 -- "inherited" to the exception message and generating an informational
3666 -- message about the inheritance of an invariant.
3668 Object_Name : constant Name_Id := New_Internal_Name ('I');
3669 -- Name for argument of invariant procedure
3671 Object_Entity : constant Node_Id :=
3672 Make_Defining_Identifier (Loc, Object_Name);
3673 -- The procedure declaration entity for the argument
3675 --------------------
3676 -- Add_Invariants --
3677 --------------------
3679 procedure Add_Invariants (T : Entity_Id; Inherit : Boolean) is
3689 procedure Replace_Type_Reference (N : Node_Id);
3690 -- Replace a single occurrence N of the subtype name with a reference
3691 -- to the formal of the predicate function. N can be an identifier
3692 -- referencing the subtype, or a selected component, representing an
3693 -- appropriately qualified occurrence of the subtype name.
3695 procedure Replace_Type_References is
3696 new Replace_Type_References_Generic (Replace_Type_Reference);
3697 -- Traverse an expression replacing all occurrences of the subtype
3698 -- name with appropriate references to the object that is the formal
3699 -- parameter of the predicate function. Note that we must ensure
3700 -- that the type and entity information is properly set in the
3701 -- replacement node, since we will do a Preanalyze call of this
3702 -- expression without proper visibility of the procedure argument.
3704 ----------------------------
3705 -- Replace_Type_Reference --
3706 ----------------------------
3708 procedure Replace_Type_Reference (N : Node_Id) is
3710 -- Invariant'Class, replace with T'Class (obj)
3712 if Class_Present (Ritem) then
3714 Make_Type_Conversion (Loc,
3716 Make_Attribute_Reference (Loc,
3717 Prefix => New_Occurrence_Of (T, Loc),
3718 Attribute_Name => Name_Class),
3719 Expression => Make_Identifier (Loc, Object_Name)));
3721 Set_Entity (Expression (N), Object_Entity);
3722 Set_Etype (Expression (N), Typ);
3724 -- Invariant, replace with obj
3727 Rewrite (N, Make_Identifier (Loc, Object_Name));
3728 Set_Entity (N, Object_Entity);
3731 end Replace_Type_Reference;
3733 -- Start of processing for Add_Invariants
3736 Ritem := First_Rep_Item (T);
3737 while Present (Ritem) loop
3738 if Nkind (Ritem) = N_Pragma
3739 and then Pragma_Name (Ritem) = Name_Invariant
3741 Arg1 := First (Pragma_Argument_Associations (Ritem));
3742 Arg2 := Next (Arg1);
3743 Arg3 := Next (Arg2);
3745 Arg1 := Get_Pragma_Arg (Arg1);
3746 Arg2 := Get_Pragma_Arg (Arg2);
3748 -- For Inherit case, ignore Invariant, process only Class case
3751 if not Class_Present (Ritem) then
3755 -- For Inherit false, process only item for right type
3758 if Entity (Arg1) /= Typ then
3764 Stmts := Empty_List;
3767 Exp := New_Copy_Tree (Arg2);
3770 -- We need to replace any occurrences of the name of the type
3771 -- with references to the object, converted to type'Class in
3772 -- the case of Invariant'Class aspects.
3774 Replace_Type_References (Exp, Chars (T));
3776 -- If this invariant comes from an aspect, find the aspect
3777 -- specification, and replace the saved expression because
3778 -- we need the subtype references replaced for the calls to
3779 -- Preanalyze_Spec_Expressin in Check_Aspect_At_Freeze_Point
3780 -- and Check_Aspect_At_End_Of_Declarations.
3782 if From_Aspect_Specification (Ritem) then
3787 -- Loop to find corresponding aspect, note that this
3788 -- must be present given the pragma is marked delayed.
3790 Aitem := Next_Rep_Item (Ritem);
3791 while Present (Aitem) loop
3792 if Nkind (Aitem) = N_Aspect_Specification
3793 and then Aspect_Rep_Item (Aitem) = Ritem
3796 (Identifier (Aitem), New_Copy_Tree (Exp));
3800 Aitem := Next_Rep_Item (Aitem);
3805 -- Now we need to preanalyze the expression to properly capture
3806 -- the visibility in the visible part. The expression will not
3807 -- be analyzed for real until the body is analyzed, but that is
3808 -- at the end of the private part and has the wrong visibility.
3810 Set_Parent (Exp, N);
3811 Preanalyze_Spec_Expression (Exp, Standard_Boolean);
3813 -- Build first two arguments for Check pragma
3816 Make_Pragma_Argument_Association (Loc,
3817 Expression => Make_Identifier (Loc, Name_Invariant)),
3818 Make_Pragma_Argument_Association (Loc, Expression => Exp));
3820 -- Add message if present in Invariant pragma
3822 if Present (Arg3) then
3823 Str := Strval (Get_Pragma_Arg (Arg3));
3825 -- If inherited case, and message starts "failed invariant",
3826 -- change it to be "failed inherited invariant".
3829 String_To_Name_Buffer (Str);
3831 if Name_Buffer (1 .. 16) = "failed invariant" then
3832 Insert_Str_In_Name_Buffer ("inherited ", 8);
3833 Str := String_From_Name_Buffer;
3838 Make_Pragma_Argument_Association (Loc,
3839 Expression => Make_String_Literal (Loc, Str)));
3842 -- Add Check pragma to list of statements
3846 Pragma_Identifier =>
3847 Make_Identifier (Loc, Name_Check),
3848 Pragma_Argument_Associations => Assoc));
3850 -- If Inherited case and option enabled, output info msg. Note
3851 -- that we know this is a case of Invariant'Class.
3853 if Inherit and Opt.List_Inherited_Aspects then
3854 Error_Msg_Sloc := Sloc (Ritem);
3856 ("?info: & inherits `Invariant''Class` aspect from #",
3862 Next_Rep_Item (Ritem);
3866 -- Start of processing for Build_Invariant_Procedure
3872 Set_Etype (Object_Entity, Typ);
3874 -- Add invariants for the current type
3876 Add_Invariants (Typ, Inherit => False);
3878 -- Add invariants for parent types
3881 Current_Typ : Entity_Id;
3882 Parent_Typ : Entity_Id;
3887 Parent_Typ := Etype (Current_Typ);
3889 if Is_Private_Type (Parent_Typ)
3890 and then Present (Full_View (Base_Type (Parent_Typ)))
3892 Parent_Typ := Full_View (Base_Type (Parent_Typ));
3895 exit when Parent_Typ = Current_Typ;
3897 Current_Typ := Parent_Typ;
3898 Add_Invariants (Current_Typ, Inherit => True);
3902 -- Build the procedure if we generated at least one Check pragma
3904 if Stmts /= No_List then
3906 -- Build procedure declaration
3909 Make_Defining_Identifier (Loc,
3910 Chars => New_External_Name (Chars (Typ), "Invariant"));
3911 Set_Has_Invariants (SId);
3912 Set_Invariant_Procedure (Typ, SId);
3915 Make_Procedure_Specification (Loc,
3916 Defining_Unit_Name => SId,
3917 Parameter_Specifications => New_List (
3918 Make_Parameter_Specification (Loc,
3919 Defining_Identifier => Object_Entity,
3920 Parameter_Type => New_Occurrence_Of (Typ, Loc))));
3922 PDecl := Make_Subprogram_Declaration (Loc, Specification => Spec);
3924 -- Build procedure body
3927 Make_Defining_Identifier (Loc,
3928 Chars => New_External_Name (Chars (Typ), "Invariant"));
3931 Make_Procedure_Specification (Loc,
3932 Defining_Unit_Name => SId,
3933 Parameter_Specifications => New_List (
3934 Make_Parameter_Specification (Loc,
3935 Defining_Identifier =>
3936 Make_Defining_Identifier (Loc, Object_Name),
3937 Parameter_Type => New_Occurrence_Of (Typ, Loc))));
3940 Make_Subprogram_Body (Loc,
3941 Specification => Spec,
3942 Declarations => Empty_List,
3943 Handled_Statement_Sequence =>
3944 Make_Handled_Sequence_Of_Statements (Loc,
3945 Statements => Stmts));
3947 -- Insert procedure declaration and spec at the appropriate points.
3948 -- Skip this if there are no private declarations (that's an error
3949 -- that will be diagnosed elsewhere, and there is no point in having
3950 -- an invariant procedure set if the full declaration is missing).
3952 if Present (Private_Decls) then
3954 -- The spec goes at the end of visible declarations, but they have
3955 -- already been analyzed, so we need to explicitly do the analyze.
3957 Append_To (Visible_Decls, PDecl);
3960 -- The body goes at the end of the private declarations, which we
3961 -- have not analyzed yet, so we do not need to perform an explicit
3962 -- analyze call. We skip this if there are no private declarations
3963 -- (this is an error that will be caught elsewhere);
3965 Append_To (Private_Decls, PBody);
3968 end Build_Invariant_Procedure;
3970 ------------------------------
3971 -- Build_Predicate_Function --
3972 ------------------------------
3974 -- The procedure that is constructed here has the form
3976 -- function typPredicate (Ixxx : typ) return Boolean is
3979 -- exp1 and then exp2 and then ...
3980 -- and then typ1Predicate (typ1 (Ixxx))
3981 -- and then typ2Predicate (typ2 (Ixxx))
3983 -- end typPredicate;
3985 -- Here exp1, and exp2 are expressions from Predicate pragmas. Note that
3986 -- this is the point at which these expressions get analyzed, providing the
3987 -- required delay, and typ1, typ2, are entities from which predicates are
3988 -- inherited. Note that we do NOT generate Check pragmas, that's because we
3989 -- use this function even if checks are off, e.g. for membership tests.
3991 procedure Build_Predicate_Function (Typ : Entity_Id; N : Node_Id) is
3992 Loc : constant Source_Ptr := Sloc (Typ);
3999 -- This is the expression for the return statement in the function. It
4000 -- is build by connecting the component predicates with AND THEN.
4002 procedure Add_Call (T : Entity_Id);
4003 -- Includes a call to the predicate function for type T in Expr if T
4004 -- has predicates and Predicate_Function (T) is non-empty.
4006 procedure Add_Predicates;
4007 -- Appends expressions for any Predicate pragmas in the rep item chain
4008 -- Typ to Expr. Note that we look only at items for this exact entity.
4009 -- Inheritance of predicates for the parent type is done by calling the
4010 -- Predicate_Function of the parent type, using Add_Call above.
4012 Object_Name : constant Name_Id := New_Internal_Name ('I');
4013 -- Name for argument of Predicate procedure
4015 Object_Entity : constant Entity_Id :=
4016 Make_Defining_Identifier (Loc, Object_Name);
4017 -- The entity for the spec entity for the argument
4019 Dynamic_Predicate_Present : Boolean := False;
4020 -- Set True if a dynamic predicate is present, results in the entire
4021 -- predicate being considered dynamic even if it looks static
4023 Static_Predicate_Present : Node_Id := Empty;
4024 -- Set to N_Pragma node for a static predicate if one is encountered.
4030 procedure Add_Call (T : Entity_Id) is
4034 if Present (T) and then Present (Predicate_Function (T)) then
4035 Set_Has_Predicates (Typ);
4037 -- Build the call to the predicate function of T
4041 (T, Convert_To (T, Make_Identifier (Loc, Object_Name)));
4043 -- Add call to evolving expression, using AND THEN if needed
4050 Left_Opnd => Relocate_Node (Expr),
4054 -- Output info message on inheritance if required. Note we do not
4055 -- give this information for generic actual types, since it is
4056 -- unwelcome noise in that case in instantiations. We also
4057 -- generally suppress the message in instantiations, and also
4058 -- if it involves internal names.
4060 if Opt.List_Inherited_Aspects
4061 and then not Is_Generic_Actual_Type (Typ)
4062 and then Instantiation_Depth (Sloc (Typ)) = 0
4063 and then not Is_Internal_Name (Chars (T))
4064 and then not Is_Internal_Name (Chars (Typ))
4066 Error_Msg_Sloc := Sloc (Predicate_Function (T));
4067 Error_Msg_Node_2 := T;
4068 Error_Msg_N ("?info: & inherits predicate from & #", Typ);
4073 --------------------
4074 -- Add_Predicates --
4075 --------------------
4077 procedure Add_Predicates is
4082 procedure Replace_Type_Reference (N : Node_Id);
4083 -- Replace a single occurrence N of the subtype name with a reference
4084 -- to the formal of the predicate function. N can be an identifier
4085 -- referencing the subtype, or a selected component, representing an
4086 -- appropriately qualified occurrence of the subtype name.
4088 procedure Replace_Type_References is
4089 new Replace_Type_References_Generic (Replace_Type_Reference);
4090 -- Traverse an expression changing every occurrence of an identifier
4091 -- whose name matches the name of the subtype with a reference to
4092 -- the formal parameter of the predicate function.
4094 ----------------------------
4095 -- Replace_Type_Reference --
4096 ----------------------------
4098 procedure Replace_Type_Reference (N : Node_Id) is
4100 Rewrite (N, Make_Identifier (Loc, Object_Name));
4101 Set_Entity (N, Object_Entity);
4103 end Replace_Type_Reference;
4105 -- Start of processing for Add_Predicates
4108 Ritem := First_Rep_Item (Typ);
4109 while Present (Ritem) loop
4110 if Nkind (Ritem) = N_Pragma
4111 and then Pragma_Name (Ritem) = Name_Predicate
4113 if From_Dynamic_Predicate (Ritem) then
4114 Dynamic_Predicate_Present := True;
4115 elsif From_Static_Predicate (Ritem) then
4116 Static_Predicate_Present := Ritem;
4119 -- Acquire arguments
4121 Arg1 := First (Pragma_Argument_Associations (Ritem));
4122 Arg2 := Next (Arg1);
4124 Arg1 := Get_Pragma_Arg (Arg1);
4125 Arg2 := Get_Pragma_Arg (Arg2);
4127 -- See if this predicate pragma is for the current type
4129 if Entity (Arg1) = Typ then
4131 -- We have a match, this entry is for our subtype
4133 -- We need to replace any occurrences of the name of the
4134 -- type with references to the object.
4136 Replace_Type_References (Arg2, Chars (Typ));
4138 -- If this predicate comes from an aspect, find the aspect
4139 -- specification, and replace the saved expression because
4140 -- we need the subtype references replaced for the calls to
4141 -- Preanalyze_Spec_Expressin in Check_Aspect_At_Freeze_Point
4142 -- and Check_Aspect_At_End_Of_Declarations.
4144 if From_Aspect_Specification (Ritem) then
4149 -- Loop to find corresponding aspect, note that this
4150 -- must be present given the pragma is marked delayed.
4152 Aitem := Next_Rep_Item (Ritem);
4154 if Nkind (Aitem) = N_Aspect_Specification
4155 and then Aspect_Rep_Item (Aitem) = Ritem
4158 (Identifier (Aitem), New_Copy_Tree (Arg2));
4162 Aitem := Next_Rep_Item (Aitem);
4167 -- Now we can add the expression
4170 Expr := Relocate_Node (Arg2);
4172 -- There already was a predicate, so add to it
4177 Left_Opnd => Relocate_Node (Expr),
4178 Right_Opnd => Relocate_Node (Arg2));
4183 Next_Rep_Item (Ritem);
4187 -- Start of processing for Build_Predicate_Function
4190 -- Initialize for construction of statement list
4194 -- Return if already built or if type does not have predicates
4196 if not Has_Predicates (Typ)
4197 or else Present (Predicate_Function (Typ))
4202 -- Add Predicates for the current type
4206 -- Add predicates for ancestor if present
4209 Atyp : constant Entity_Id := Nearest_Ancestor (Typ);
4211 if Present (Atyp) then
4216 -- If we have predicates, build the function
4218 if Present (Expr) then
4220 -- Build function declaration
4222 pragma Assert (Has_Predicates (Typ));
4224 Make_Defining_Identifier (Loc,
4225 Chars => New_External_Name (Chars (Typ), "Predicate"));
4226 Set_Has_Predicates (SId);
4227 Set_Predicate_Function (Typ, SId);
4230 Make_Function_Specification (Loc,
4231 Defining_Unit_Name => SId,
4232 Parameter_Specifications => New_List (
4233 Make_Parameter_Specification (Loc,
4234 Defining_Identifier => Object_Entity,
4235 Parameter_Type => New_Occurrence_Of (Typ, Loc))),
4236 Result_Definition =>
4237 New_Occurrence_Of (Standard_Boolean, Loc));
4239 FDecl := Make_Subprogram_Declaration (Loc, Specification => Spec);
4241 -- Build function body
4244 Make_Defining_Identifier (Loc,
4245 Chars => New_External_Name (Chars (Typ), "Predicate"));
4248 Make_Function_Specification (Loc,
4249 Defining_Unit_Name => SId,
4250 Parameter_Specifications => New_List (
4251 Make_Parameter_Specification (Loc,
4252 Defining_Identifier =>
4253 Make_Defining_Identifier (Loc, Object_Name),
4255 New_Occurrence_Of (Typ, Loc))),
4256 Result_Definition =>
4257 New_Occurrence_Of (Standard_Boolean, Loc));
4260 Make_Subprogram_Body (Loc,
4261 Specification => Spec,
4262 Declarations => Empty_List,
4263 Handled_Statement_Sequence =>
4264 Make_Handled_Sequence_Of_Statements (Loc,
4265 Statements => New_List (
4266 Make_Simple_Return_Statement (Loc,
4267 Expression => Expr))));
4269 -- Insert declaration before freeze node and body after
4271 Insert_Before_And_Analyze (N, FDecl);
4272 Insert_After_And_Analyze (N, FBody);
4274 -- Deal with static predicate case
4276 if Ekind_In (Typ, E_Enumeration_Subtype,
4277 E_Modular_Integer_Subtype,
4278 E_Signed_Integer_Subtype)
4279 and then Is_Static_Subtype (Typ)
4280 and then not Dynamic_Predicate_Present
4282 Build_Static_Predicate (Typ, Expr, Object_Name);
4284 if Present (Static_Predicate_Present)
4285 and No (Static_Predicate (Typ))
4288 ("expression does not have required form for "
4289 & "static predicate",
4290 Next (First (Pragma_Argument_Associations
4291 (Static_Predicate_Present))));
4295 end Build_Predicate_Function;
4297 ----------------------------
4298 -- Build_Static_Predicate --
4299 ----------------------------
4301 procedure Build_Static_Predicate
4306 Loc : constant Source_Ptr := Sloc (Expr);
4308 Non_Static : exception;
4309 -- Raised if something non-static is found
4311 Btyp : constant Entity_Id := Base_Type (Typ);
4313 BLo : constant Uint := Expr_Value (Type_Low_Bound (Btyp));
4314 BHi : constant Uint := Expr_Value (Type_High_Bound (Btyp));
4315 -- Low bound and high bound value of base type of Typ
4317 TLo : constant Uint := Expr_Value (Type_Low_Bound (Typ));
4318 THi : constant Uint := Expr_Value (Type_High_Bound (Typ));
4319 -- Low bound and high bound values of static subtype Typ
4324 -- One entry in a Rlist value, a single REnt (range entry) value
4325 -- denotes one range from Lo to Hi. To represent a single value
4326 -- range Lo = Hi = value.
4328 type RList is array (Nat range <>) of REnt;
4329 -- A list of ranges. The ranges are sorted in increasing order,
4330 -- and are disjoint (there is a gap of at least one value between
4331 -- each range in the table). A value is in the set of ranges in
4332 -- Rlist if it lies within one of these ranges
4334 False_Range : constant RList :=
4335 RList'(1 .. 0 => REnt'(No_Uint, No_Uint));
4336 -- An empty set of ranges represents a range list that can never be
4337 -- satisfied, since there are no ranges in which the value could lie,
4338 -- so it does not lie in any of them. False_Range is a canonical value
4339 -- for this empty set, but general processing should test for an Rlist
4340 -- with length zero (see Is_False predicate), since other null ranges
4341 -- may appear which must be treated as False.
4343 True_Range : constant RList := RList'(1 => REnt'(BLo, BHi));
4344 -- Range representing True, value must be in the base range
4346 function "and" (Left, Right : RList) return RList;
4347 -- And's together two range lists, returning a range list. This is
4348 -- a set intersection operation.
4350 function "or" (Left, Right : RList) return RList;
4351 -- Or's together two range lists, returning a range list. This is a
4352 -- set union operation.
4354 function "not" (Right : RList) return RList;
4355 -- Returns complement of a given range list, i.e. a range list
4356 -- representing all the values in TLo .. THi that are not in the
4357 -- input operand Right.
4359 function Build_Val (V : Uint) return Node_Id;
4360 -- Return an analyzed N_Identifier node referencing this value, suitable
4361 -- for use as an entry in the Static_Predicate list. This node is typed
4362 -- with the base type.
4364 function Build_Range (Lo, Hi : Uint) return Node_Id;
4365 -- Return an analyzed N_Range node referencing this range, suitable
4366 -- for use as an entry in the Static_Predicate list. This node is typed
4367 -- with the base type.
4369 function Get_RList (Exp : Node_Id) return RList;
4370 -- This is a recursive routine that converts the given expression into
4371 -- a list of ranges, suitable for use in building the static predicate.
4373 function Is_False (R : RList) return Boolean;
4374 pragma Inline (Is_False);
4375 -- Returns True if the given range list is empty, and thus represents
4376 -- a False list of ranges that can never be satisfied.
4378 function Is_True (R : RList) return Boolean;
4379 -- Returns True if R trivially represents the True predicate by having
4380 -- a single range from BLo to BHi.
4382 function Is_Type_Ref (N : Node_Id) return Boolean;
4383 pragma Inline (Is_Type_Ref);
4384 -- Returns if True if N is a reference to the type for the predicate in
4385 -- the expression (i.e. if it is an identifier whose Chars field matches
4386 -- the Nam given in the call).
4388 function Lo_Val (N : Node_Id) return Uint;
4389 -- Given static expression or static range from a Static_Predicate list,
4390 -- gets expression value or low bound of range.
4392 function Hi_Val (N : Node_Id) return Uint;
4393 -- Given static expression or static range from a Static_Predicate list,
4394 -- gets expression value of high bound of range.
4396 function Membership_Entry (N : Node_Id) return RList;
4397 -- Given a single membership entry (range, value, or subtype), returns
4398 -- the corresponding range list. Raises Static_Error if not static.
4400 function Membership_Entries (N : Node_Id) return RList;
4401 -- Given an element on an alternatives list of a membership operation,
4402 -- returns the range list corresponding to this entry and all following
4403 -- entries (i.e. returns the "or" of this list of values).
4405 function Stat_Pred (Typ : Entity_Id) return RList;
4406 -- Given a type, if it has a static predicate, then return the predicate
4407 -- as a range list, otherwise raise Non_Static.
4413 function "and" (Left, Right : RList) return RList is
4415 -- First range of result
4417 SLeft : Nat := Left'First;
4418 -- Start of rest of left entries
4420 SRight : Nat := Right'First;
4421 -- Start of rest of right entries
4424 -- If either range is True, return the other
4426 if Is_True (Left) then
4428 elsif Is_True (Right) then
4432 -- If either range is False, return False
4434 if Is_False (Left) or else Is_False (Right) then
4438 -- Loop to remove entries at start that are disjoint, and thus
4439 -- just get discarded from the result entirely.
4442 -- If no operands left in either operand, result is false
4444 if SLeft > Left'Last or else SRight > Right'Last then
4447 -- Discard first left operand entry if disjoint with right
4449 elsif Left (SLeft).Hi < Right (SRight).Lo then
4452 -- Discard first right operand entry if disjoint with left
4454 elsif Right (SRight).Hi < Left (SLeft).Lo then
4455 SRight := SRight + 1;
4457 -- Otherwise we have an overlapping entry
4464 -- Now we have two non-null operands, and first entries overlap.
4465 -- The first entry in the result will be the overlapping part of
4466 -- these two entries.
4468 FEnt := REnt'(Lo => UI_Max (Left (SLeft).Lo, Right (SRight).Lo),
4469 Hi => UI_Min (Left (SLeft).Hi, Right (SRight).Hi));
4471 -- Now we can remove the entry that ended at a lower value, since
4472 -- its contribution is entirely contained in Fent.
4474 if Left (SLeft).Hi <= Right (SRight).Hi then
4477 SRight := SRight + 1;
4480 -- Compute result by concatenating this first entry with the "and"
4481 -- of the remaining parts of the left and right operands. Note that
4482 -- if either of these is empty, "and" will yield empty, so that we
4483 -- will end up with just Fent, which is what we want in that case.
4486 FEnt & (Left (SLeft .. Left'Last) and Right (SRight .. Right'Last));
4493 function "not" (Right : RList) return RList is
4495 -- Return True if False range
4497 if Is_False (Right) then
4501 -- Return False if True range
4503 if Is_True (Right) then
4507 -- Here if not trivial case
4510 Result : RList (1 .. Right'Length + 1);
4511 -- May need one more entry for gap at beginning and end
4514 -- Number of entries stored in Result
4519 if Right (Right'First).Lo > TLo then
4521 Result (Count) := REnt'(TLo, Right (Right'First).Lo - 1);
4524 -- Gaps between ranges
4526 for J in Right'First .. Right'Last - 1 loop
4529 REnt'(Right (J).Hi + 1, Right (J + 1).Lo - 1);
4534 if Right (Right'Last).Hi < THi then
4536 Result (Count) := REnt'(Right (Right'Last).Hi + 1, THi);
4539 return Result (1 .. Count);
4547 function "or" (Left, Right : RList) return RList is
4549 -- First range of result
4551 SLeft : Nat := Left'First;
4552 -- Start of rest of left entries
4554 SRight : Nat := Right'First;
4555 -- Start of rest of right entries
4558 -- If either range is True, return True
4560 if Is_True (Left) or else Is_True (Right) then
4564 -- If either range is False (empty), return the other
4566 if Is_False (Left) then
4568 elsif Is_False (Right) then
4572 -- Initialize result first entry from left or right operand
4573 -- depending on which starts with the lower range.
4575 if Left (SLeft).Lo < Right (SRight).Lo then
4576 FEnt := Left (SLeft);
4579 FEnt := Right (SRight);
4580 SRight := SRight + 1;
4583 -- This loop eats ranges from left and right operands that
4584 -- are contiguous with the first range we are gathering.
4587 -- Eat first entry in left operand if contiguous or
4588 -- overlapped by gathered first operand of result.
4590 if SLeft <= Left'Last
4591 and then Left (SLeft).Lo <= FEnt.Hi + 1
4593 FEnt.Hi := UI_Max (FEnt.Hi, Left (SLeft).Hi);
4596 -- Eat first entry in right operand if contiguous or
4597 -- overlapped by gathered right operand of result.
4599 elsif SRight <= Right'Last
4600 and then Right (SRight).Lo <= FEnt.Hi + 1
4602 FEnt.Hi := UI_Max (FEnt.Hi, Right (SRight).Hi);
4603 SRight := SRight + 1;
4605 -- All done if no more entries to eat!
4612 -- Obtain result as the first entry we just computed, concatenated
4613 -- to the "or" of the remaining results (if one operand is empty,
4614 -- this will just concatenate with the other
4617 FEnt & (Left (SLeft .. Left'Last) or Right (SRight .. Right'Last));
4624 function Build_Range (Lo, Hi : Uint) return Node_Id is
4628 return Build_Val (Hi);
4632 Low_Bound => Build_Val (Lo),
4633 High_Bound => Build_Val (Hi));
4634 Set_Etype (Result, Btyp);
4635 Set_Analyzed (Result);
4644 function Build_Val (V : Uint) return Node_Id is
4648 if Is_Enumeration_Type (Typ) then
4649 Result := Get_Enum_Lit_From_Pos (Typ, V, Loc);
4651 Result := Make_Integer_Literal (Loc, V);
4654 Set_Etype (Result, Btyp);
4655 Set_Is_Static_Expression (Result);
4656 Set_Analyzed (Result);
4664 function Get_RList (Exp : Node_Id) return RList is
4669 -- Static expression can only be true or false
4671 if Is_OK_Static_Expression (Exp) then
4675 if Expr_Value (Exp) = 0 then
4682 -- Otherwise test node type
4690 when N_Op_And | N_And_Then =>
4691 return Get_RList (Left_Opnd (Exp))
4693 Get_RList (Right_Opnd (Exp));
4697 when N_Op_Or | N_Or_Else =>
4698 return Get_RList (Left_Opnd (Exp))
4700 Get_RList (Right_Opnd (Exp));
4705 return not Get_RList (Right_Opnd (Exp));
4707 -- Comparisons of type with static value
4709 when N_Op_Compare =>
4710 -- Type is left operand
4712 if Is_Type_Ref (Left_Opnd (Exp))
4713 and then Is_OK_Static_Expression (Right_Opnd (Exp))
4715 Val := Expr_Value (Right_Opnd (Exp));
4717 -- Typ is right operand
4719 elsif Is_Type_Ref (Right_Opnd (Exp))
4720 and then Is_OK_Static_Expression (Left_Opnd (Exp))
4722 Val := Expr_Value (Left_Opnd (Exp));
4724 -- Invert sense of comparison
4727 when N_Op_Gt => Op := N_Op_Lt;
4728 when N_Op_Lt => Op := N_Op_Gt;
4729 when N_Op_Ge => Op := N_Op_Le;
4730 when N_Op_Le => Op := N_Op_Ge;
4731 when others => null;
4734 -- Other cases are non-static
4740 -- Construct range according to comparison operation
4744 return RList'(1 => REnt'(Val, Val));
4747 return RList'(1 => REnt'(Val, BHi));
4750 return RList'(1 => REnt'(Val + 1, BHi));
4753 return RList'(1 => REnt'(BLo, Val));
4756 return RList'(1 => REnt'(BLo, Val - 1));
4759 return RList'(REnt'(BLo, Val - 1),
4760 REnt'(Val + 1, BHi));
4763 raise Program_Error;
4769 if not Is_Type_Ref (Left_Opnd (Exp)) then
4773 if Present (Right_Opnd (Exp)) then
4774 return Membership_Entry (Right_Opnd (Exp));
4776 return Membership_Entries (First (Alternatives (Exp)));
4779 -- Negative membership (NOT IN)
4782 if not Is_Type_Ref (Left_Opnd (Exp)) then
4786 if Present (Right_Opnd (Exp)) then
4787 return not Membership_Entry (Right_Opnd (Exp));
4789 return not Membership_Entries (First (Alternatives (Exp)));
4792 -- Function call, may be call to static predicate
4794 when N_Function_Call =>
4795 if Is_Entity_Name (Name (Exp)) then
4797 Ent : constant Entity_Id := Entity (Name (Exp));
4799 if Has_Predicates (Ent) then
4800 return Stat_Pred (Etype (First_Formal (Ent)));
4805 -- Other function call cases are non-static
4809 -- Qualified expression, dig out the expression
4811 when N_Qualified_Expression =>
4812 return Get_RList (Expression (Exp));
4817 return (Get_RList (Left_Opnd (Exp))
4818 and not Get_RList (Right_Opnd (Exp)))
4819 or (Get_RList (Right_Opnd (Exp))
4820 and not Get_RList (Left_Opnd (Exp)));
4822 -- Any other node type is non-static
4833 function Hi_Val (N : Node_Id) return Uint is
4835 if Is_Static_Expression (N) then
4836 return Expr_Value (N);
4838 pragma Assert (Nkind (N) = N_Range);
4839 return Expr_Value (High_Bound (N));
4847 function Is_False (R : RList) return Boolean is
4849 return R'Length = 0;
4856 function Is_True (R : RList) return Boolean is
4859 and then R (R'First).Lo = BLo
4860 and then R (R'First).Hi = BHi;
4867 function Is_Type_Ref (N : Node_Id) return Boolean is
4869 return Nkind (N) = N_Identifier and then Chars (N) = Nam;
4876 function Lo_Val (N : Node_Id) return Uint is
4878 if Is_Static_Expression (N) then
4879 return Expr_Value (N);
4881 pragma Assert (Nkind (N) = N_Range);
4882 return Expr_Value (Low_Bound (N));
4886 ------------------------
4887 -- Membership_Entries --
4888 ------------------------
4890 function Membership_Entries (N : Node_Id) return RList is
4892 if No (Next (N)) then
4893 return Membership_Entry (N);
4895 return Membership_Entry (N) or Membership_Entries (Next (N));
4897 end Membership_Entries;
4899 ----------------------
4900 -- Membership_Entry --
4901 ----------------------
4903 function Membership_Entry (N : Node_Id) return RList is
4911 if Nkind (N) = N_Range then
4912 if not Is_Static_Expression (Low_Bound (N))
4914 not Is_Static_Expression (High_Bound (N))
4918 SLo := Expr_Value (Low_Bound (N));
4919 SHi := Expr_Value (High_Bound (N));
4920 return RList'(1 => REnt'(SLo, SHi));
4923 -- Static expression case
4925 elsif Is_Static_Expression (N) then
4926 Val := Expr_Value (N);
4927 return RList'(1 => REnt'(Val, Val));
4929 -- Identifier (other than static expression) case
4931 else pragma Assert (Nkind (N) = N_Identifier);
4935 if Is_Type (Entity (N)) then
4937 -- If type has predicates, process them
4939 if Has_Predicates (Entity (N)) then
4940 return Stat_Pred (Entity (N));
4942 -- For static subtype without predicates, get range
4944 elsif Is_Static_Subtype (Entity (N)) then
4945 SLo := Expr_Value (Type_Low_Bound (Entity (N)));
4946 SHi := Expr_Value (Type_High_Bound (Entity (N)));
4947 return RList'(1 => REnt'(SLo, SHi));
4949 -- Any other type makes us non-static
4955 -- Any other kind of identifier in predicate (e.g. a non-static
4956 -- expression value) means this is not a static predicate.
4962 end Membership_Entry;
4968 function Stat_Pred (Typ : Entity_Id) return RList is
4970 -- Not static if type does not have static predicates
4972 if not Has_Predicates (Typ)
4973 or else No (Static_Predicate (Typ))
4978 -- Otherwise we convert the predicate list to a range list
4981 Result : RList (1 .. List_Length (Static_Predicate (Typ)));
4985 P := First (Static_Predicate (Typ));
4986 for J in Result'Range loop
4987 Result (J) := REnt'(Lo_Val (P), Hi_Val (P));
4995 -- Start of processing for Build_Static_Predicate
4998 -- Now analyze the expression to see if it is a static predicate
5001 Ranges : constant RList := Get_RList (Expr);
5002 -- Range list from expression if it is static
5007 -- Convert range list into a form for the static predicate. In the
5008 -- Ranges array, we just have raw ranges, these must be converted
5009 -- to properly typed and analyzed static expressions or range nodes.
5011 -- Note: here we limit ranges to the ranges of the subtype, so that
5012 -- a predicate is always false for values outside the subtype. That
5013 -- seems fine, such values are invalid anyway, and considering them
5014 -- to fail the predicate seems allowed and friendly, and furthermore
5015 -- simplifies processing for case statements and loops.
5019 for J in Ranges'Range loop
5021 Lo : Uint := Ranges (J).Lo;
5022 Hi : Uint := Ranges (J).Hi;
5025 -- Ignore completely out of range entry
5027 if Hi < TLo or else Lo > THi then
5030 -- Otherwise process entry
5033 -- Adjust out of range value to subtype range
5043 -- Convert range into required form
5046 Append_To (Plist, Build_Val (Lo));
5048 Append_To (Plist, Build_Range (Lo, Hi));
5054 -- Processing was successful and all entries were static, so now we
5055 -- can store the result as the predicate list.
5057 Set_Static_Predicate (Typ, Plist);
5059 -- The processing for static predicates put the expression into
5060 -- canonical form as a series of ranges. It also eliminated
5061 -- duplicates and collapsed and combined ranges. We might as well
5062 -- replace the alternatives list of the right operand of the
5063 -- membership test with the static predicate list, which will
5064 -- usually be more efficient.
5067 New_Alts : constant List_Id := New_List;
5072 Old_Node := First (Plist);
5073 while Present (Old_Node) loop
5074 New_Node := New_Copy (Old_Node);
5076 if Nkind (New_Node) = N_Range then
5077 Set_Low_Bound (New_Node, New_Copy (Low_Bound (Old_Node)));
5078 Set_High_Bound (New_Node, New_Copy (High_Bound (Old_Node)));
5081 Append_To (New_Alts, New_Node);
5085 -- If empty list, replace by False
5087 if Is_Empty_List (New_Alts) then
5088 Rewrite (Expr, New_Occurrence_Of (Standard_False, Loc));
5090 -- Else replace by set membership test
5095 Left_Opnd => Make_Identifier (Loc, Nam),
5096 Right_Opnd => Empty,
5097 Alternatives => New_Alts));
5099 -- Resolve new expression in function context
5101 Install_Formals (Predicate_Function (Typ));
5102 Push_Scope (Predicate_Function (Typ));
5103 Analyze_And_Resolve (Expr, Standard_Boolean);
5109 -- If non-static, return doing nothing
5114 end Build_Static_Predicate;
5116 -----------------------------------------
5117 -- Check_Aspect_At_End_Of_Declarations --
5118 -----------------------------------------
5120 procedure Check_Aspect_At_End_Of_Declarations (ASN : Node_Id) is
5121 Ent : constant Entity_Id := Entity (ASN);
5122 Ident : constant Node_Id := Identifier (ASN);
5124 Freeze_Expr : constant Node_Id := Expression (ASN);
5125 -- Preanalyzed expression from call to Check_Aspect_At_Freeze_Point
5127 End_Decl_Expr : constant Node_Id := Entity (Ident);
5128 -- Expression to be analyzed at end of declarations
5130 T : constant Entity_Id := Etype (Freeze_Expr);
5131 -- Type required for preanalyze call
5133 A_Id : constant Aspect_Id := Get_Aspect_Id (Chars (Ident));
5136 -- Set False if error
5138 -- On entry to this procedure, Entity (Ident) contains a copy of the
5139 -- original expression from the aspect, saved for this purpose, and
5140 -- but Expression (Ident) is a preanalyzed copy of the expression,
5141 -- preanalyzed just after the freeze point.
5144 -- Case of stream attributes, just have to compare entities
5146 if A_Id = Aspect_Input or else
5147 A_Id = Aspect_Output or else
5148 A_Id = Aspect_Read or else
5151 Analyze (End_Decl_Expr);
5152 Err := Entity (End_Decl_Expr) /= Entity (Freeze_Expr);
5157 Preanalyze_Spec_Expression (End_Decl_Expr, T);
5158 Err := not Fully_Conformant_Expressions (End_Decl_Expr, Freeze_Expr);
5161 -- Output error message if error
5165 ("visibility of aspect for& changes after freeze point",
5168 ("?info: & is frozen here, aspects evaluated at this point",
5169 Freeze_Node (Ent), Ent);
5171 end Check_Aspect_At_End_Of_Declarations;
5173 ----------------------------------
5174 -- Check_Aspect_At_Freeze_Point --
5175 ----------------------------------
5177 procedure Check_Aspect_At_Freeze_Point (ASN : Node_Id) is
5178 Ident : constant Node_Id := Identifier (ASN);
5179 -- Identifier (use Entity field to save expression)
5182 -- Type required for preanalyze call
5184 A_Id : constant Aspect_Id := Get_Aspect_Id (Chars (Ident));
5187 -- On entry to this procedure, Entity (Ident) contains a copy of the
5188 -- original expression from the aspect, saved for this purpose.
5190 -- On exit from this procedure Entity (Ident) is unchanged, still
5191 -- containing that copy, but Expression (Ident) is a preanalyzed copy
5192 -- of the expression, preanalyzed just after the freeze point.
5194 -- Make a copy of the expression to be preanalyed
5196 Set_Expression (ASN, New_Copy_Tree (Entity (Ident)));
5198 -- Find type for preanalyze call
5202 -- No_Aspect should be impossible
5205 raise Program_Error;
5207 -- Library unit aspects should be impossible (never delayed)
5209 when Library_Unit_Aspects =>
5210 raise Program_Error;
5212 -- Aspects taking an optional boolean argument. Note that we will
5213 -- never be called with an empty expression, because such aspects
5214 -- never need to be delayed anyway.
5216 when Boolean_Aspects =>
5217 pragma Assert (Present (Expression (ASN)));
5218 T := Standard_Boolean;
5220 -- Aspects corresponding to attribute definition clauses
5222 when Aspect_Address =>
5223 T := RTE (RE_Address);
5225 when Aspect_Bit_Order =>
5226 T := RTE (RE_Bit_Order);
5228 when Aspect_External_Tag =>
5229 T := Standard_String;
5231 when Aspect_Storage_Pool =>
5232 T := Class_Wide_Type (RTE (RE_Root_Storage_Pool));
5236 Aspect_Component_Size |
5237 Aspect_Machine_Radix |
5238 Aspect_Object_Size |
5240 Aspect_Storage_Size |
5241 Aspect_Stream_Size |
5242 Aspect_Value_Size =>
5245 -- Stream attribute. Special case, the expression is just an entity
5246 -- that does not need any resolution, so just analyze.
5252 Analyze (Expression (ASN));
5255 -- Suppress/Unsupress/Warnings should never be delayed
5257 when Aspect_Suppress |
5260 raise Program_Error;
5262 -- Pre/Post/Invariant/Predicate take boolean expressions
5264 when Aspect_Dynamic_Predicate |
5267 Aspect_Precondition |
5269 Aspect_Postcondition |
5271 Aspect_Static_Predicate |
5272 Aspect_Type_Invariant =>
5273 T := Standard_Boolean;
5276 -- Do the preanalyze call
5278 Preanalyze_Spec_Expression (Expression (ASN), T);
5279 end Check_Aspect_At_Freeze_Point;
5281 -----------------------------------
5282 -- Check_Constant_Address_Clause --
5283 -----------------------------------
5285 procedure Check_Constant_Address_Clause
5289 procedure Check_At_Constant_Address (Nod : Node_Id);
5290 -- Checks that the given node N represents a name whose 'Address is
5291 -- constant (in the same sense as OK_Constant_Address_Clause, i.e. the
5292 -- address value is the same at the point of declaration of U_Ent and at
5293 -- the time of elaboration of the address clause.
5295 procedure Check_Expr_Constants (Nod : Node_Id);
5296 -- Checks that Nod meets the requirements for a constant address clause
5297 -- in the sense of the enclosing procedure.
5299 procedure Check_List_Constants (Lst : List_Id);
5300 -- Check that all elements of list Lst meet the requirements for a
5301 -- constant address clause in the sense of the enclosing procedure.
5303 -------------------------------
5304 -- Check_At_Constant_Address --
5305 -------------------------------
5307 procedure Check_At_Constant_Address (Nod : Node_Id) is
5309 if Is_Entity_Name (Nod) then
5310 if Present (Address_Clause (Entity ((Nod)))) then
5312 ("invalid address clause for initialized object &!",
5315 ("address for& cannot" &
5316 " depend on another address clause! (RM 13.1(22))!",
5319 elsif In_Same_Source_Unit (Entity (Nod), U_Ent)
5320 and then Sloc (U_Ent) < Sloc (Entity (Nod))
5323 ("invalid address clause for initialized object &!",
5325 Error_Msg_Node_2 := U_Ent;
5327 ("\& must be defined before & (RM 13.1(22))!",
5331 elsif Nkind (Nod) = N_Selected_Component then
5333 T : constant Entity_Id := Etype (Prefix (Nod));
5336 if (Is_Record_Type (T)
5337 and then Has_Discriminants (T))
5340 and then Is_Record_Type (Designated_Type (T))
5341 and then Has_Discriminants (Designated_Type (T)))
5344 ("invalid address clause for initialized object &!",
5347 ("\address cannot depend on component" &
5348 " of discriminated record (RM 13.1(22))!",
5351 Check_At_Constant_Address (Prefix (Nod));
5355 elsif Nkind (Nod) = N_Indexed_Component then
5356 Check_At_Constant_Address (Prefix (Nod));
5357 Check_List_Constants (Expressions (Nod));
5360 Check_Expr_Constants (Nod);
5362 end Check_At_Constant_Address;
5364 --------------------------
5365 -- Check_Expr_Constants --
5366 --------------------------
5368 procedure Check_Expr_Constants (Nod : Node_Id) is
5369 Loc_U_Ent : constant Source_Ptr := Sloc (U_Ent);
5370 Ent : Entity_Id := Empty;
5373 if Nkind (Nod) in N_Has_Etype
5374 and then Etype (Nod) = Any_Type
5380 when N_Empty | N_Error =>
5383 when N_Identifier | N_Expanded_Name =>
5384 Ent := Entity (Nod);
5386 -- We need to look at the original node if it is different
5387 -- from the node, since we may have rewritten things and
5388 -- substituted an identifier representing the rewrite.
5390 if Original_Node (Nod) /= Nod then
5391 Check_Expr_Constants (Original_Node (Nod));
5393 -- If the node is an object declaration without initial
5394 -- value, some code has been expanded, and the expression
5395 -- is not constant, even if the constituents might be
5396 -- acceptable, as in A'Address + offset.
5398 if Ekind (Ent) = E_Variable
5400 Nkind (Declaration_Node (Ent)) = N_Object_Declaration
5402 No (Expression (Declaration_Node (Ent)))
5405 ("invalid address clause for initialized object &!",
5408 -- If entity is constant, it may be the result of expanding
5409 -- a check. We must verify that its declaration appears
5410 -- before the object in question, else we also reject the
5413 elsif Ekind (Ent) = E_Constant
5414 and then In_Same_Source_Unit (Ent, U_Ent)
5415 and then Sloc (Ent) > Loc_U_Ent
5418 ("invalid address clause for initialized object &!",
5425 -- Otherwise look at the identifier and see if it is OK
5427 if Ekind_In (Ent, E_Named_Integer, E_Named_Real)
5428 or else Is_Type (Ent)
5433 Ekind (Ent) = E_Constant
5435 Ekind (Ent) = E_In_Parameter
5437 -- This is the case where we must have Ent defined before
5438 -- U_Ent. Clearly if they are in different units this
5439 -- requirement is met since the unit containing Ent is
5440 -- already processed.
5442 if not In_Same_Source_Unit (Ent, U_Ent) then
5445 -- Otherwise location of Ent must be before the location
5446 -- of U_Ent, that's what prior defined means.
5448 elsif Sloc (Ent) < Loc_U_Ent then
5453 ("invalid address clause for initialized object &!",
5455 Error_Msg_Node_2 := U_Ent;
5457 ("\& must be defined before & (RM 13.1(22))!",
5461 elsif Nkind (Original_Node (Nod)) = N_Function_Call then
5462 Check_Expr_Constants (Original_Node (Nod));
5466 ("invalid address clause for initialized object &!",
5469 if Comes_From_Source (Ent) then
5471 ("\reference to variable& not allowed"
5472 & " (RM 13.1(22))!", Nod, Ent);
5475 ("non-static expression not allowed"
5476 & " (RM 13.1(22))!", Nod);
5480 when N_Integer_Literal =>
5482 -- If this is a rewritten unchecked conversion, in a system
5483 -- where Address is an integer type, always use the base type
5484 -- for a literal value. This is user-friendly and prevents
5485 -- order-of-elaboration issues with instances of unchecked
5488 if Nkind (Original_Node (Nod)) = N_Function_Call then
5489 Set_Etype (Nod, Base_Type (Etype (Nod)));
5492 when N_Real_Literal |
5494 N_Character_Literal =>
5498 Check_Expr_Constants (Low_Bound (Nod));
5499 Check_Expr_Constants (High_Bound (Nod));
5501 when N_Explicit_Dereference =>
5502 Check_Expr_Constants (Prefix (Nod));
5504 when N_Indexed_Component =>
5505 Check_Expr_Constants (Prefix (Nod));
5506 Check_List_Constants (Expressions (Nod));
5509 Check_Expr_Constants (Prefix (Nod));
5510 Check_Expr_Constants (Discrete_Range (Nod));
5512 when N_Selected_Component =>
5513 Check_Expr_Constants (Prefix (Nod));
5515 when N_Attribute_Reference =>
5516 if Attribute_Name (Nod) = Name_Address
5518 Attribute_Name (Nod) = Name_Access
5520 Attribute_Name (Nod) = Name_Unchecked_Access
5522 Attribute_Name (Nod) = Name_Unrestricted_Access
5524 Check_At_Constant_Address (Prefix (Nod));
5527 Check_Expr_Constants (Prefix (Nod));
5528 Check_List_Constants (Expressions (Nod));
5532 Check_List_Constants (Component_Associations (Nod));
5533 Check_List_Constants (Expressions (Nod));
5535 when N_Component_Association =>
5536 Check_Expr_Constants (Expression (Nod));
5538 when N_Extension_Aggregate =>
5539 Check_Expr_Constants (Ancestor_Part (Nod));
5540 Check_List_Constants (Component_Associations (Nod));
5541 Check_List_Constants (Expressions (Nod));
5546 when N_Binary_Op | N_Short_Circuit | N_Membership_Test =>
5547 Check_Expr_Constants (Left_Opnd (Nod));
5548 Check_Expr_Constants (Right_Opnd (Nod));
5551 Check_Expr_Constants (Right_Opnd (Nod));
5553 when N_Type_Conversion |
5554 N_Qualified_Expression |
5556 Check_Expr_Constants (Expression (Nod));
5558 when N_Unchecked_Type_Conversion =>
5559 Check_Expr_Constants (Expression (Nod));
5561 -- If this is a rewritten unchecked conversion, subtypes in
5562 -- this node are those created within the instance. To avoid
5563 -- order of elaboration issues, replace them with their base
5564 -- types. Note that address clauses can cause order of
5565 -- elaboration problems because they are elaborated by the
5566 -- back-end at the point of definition, and may mention
5567 -- entities declared in between (as long as everything is
5568 -- static). It is user-friendly to allow unchecked conversions
5571 if Nkind (Original_Node (Nod)) = N_Function_Call then
5572 Set_Etype (Expression (Nod),
5573 Base_Type (Etype (Expression (Nod))));
5574 Set_Etype (Nod, Base_Type (Etype (Nod)));
5577 when N_Function_Call =>
5578 if not Is_Pure (Entity (Name (Nod))) then
5580 ("invalid address clause for initialized object &!",
5584 ("\function & is not pure (RM 13.1(22))!",
5585 Nod, Entity (Name (Nod)));
5588 Check_List_Constants (Parameter_Associations (Nod));
5591 when N_Parameter_Association =>
5592 Check_Expr_Constants (Explicit_Actual_Parameter (Nod));
5596 ("invalid address clause for initialized object &!",
5599 ("\must be constant defined before& (RM 13.1(22))!",
5602 end Check_Expr_Constants;
5604 --------------------------
5605 -- Check_List_Constants --
5606 --------------------------
5608 procedure Check_List_Constants (Lst : List_Id) is
5612 if Present (Lst) then
5613 Nod1 := First (Lst);
5614 while Present (Nod1) loop
5615 Check_Expr_Constants (Nod1);
5619 end Check_List_Constants;
5621 -- Start of processing for Check_Constant_Address_Clause
5624 -- If rep_clauses are to be ignored, no need for legality checks. In
5625 -- particular, no need to pester user about rep clauses that violate
5626 -- the rule on constant addresses, given that these clauses will be
5627 -- removed by Freeze before they reach the back end.
5629 if not Ignore_Rep_Clauses then
5630 Check_Expr_Constants (Expr);
5632 end Check_Constant_Address_Clause;
5634 ----------------------------------------
5635 -- Check_Record_Representation_Clause --
5636 ----------------------------------------
5638 procedure Check_Record_Representation_Clause (N : Node_Id) is
5639 Loc : constant Source_Ptr := Sloc (N);
5640 Ident : constant Node_Id := Identifier (N);
5641 Rectype : Entity_Id;
5646 Hbit : Uint := Uint_0;
5650 Max_Bit_So_Far : Uint;
5651 -- Records the maximum bit position so far. If all field positions
5652 -- are monotonically increasing, then we can skip the circuit for
5653 -- checking for overlap, since no overlap is possible.
5655 Tagged_Parent : Entity_Id := Empty;
5656 -- This is set in the case of a derived tagged type for which we have
5657 -- Is_Fully_Repped_Tagged_Type True (indicating that all components are
5658 -- positioned by record representation clauses). In this case we must
5659 -- check for overlap between components of this tagged type, and the
5660 -- components of its parent. Tagged_Parent will point to this parent
5661 -- type. For all other cases Tagged_Parent is left set to Empty.
5663 Parent_Last_Bit : Uint;
5664 -- Relevant only if Tagged_Parent is set, Parent_Last_Bit indicates the
5665 -- last bit position for any field in the parent type. We only need to
5666 -- check overlap for fields starting below this point.
5668 Overlap_Check_Required : Boolean;
5669 -- Used to keep track of whether or not an overlap check is required
5671 Overlap_Detected : Boolean := False;
5672 -- Set True if an overlap is detected
5674 Ccount : Natural := 0;
5675 -- Number of component clauses in record rep clause
5677 procedure Check_Component_Overlap (C1_Ent, C2_Ent : Entity_Id);
5678 -- Given two entities for record components or discriminants, checks
5679 -- if they have overlapping component clauses and issues errors if so.
5681 procedure Find_Component;
5682 -- Finds component entity corresponding to current component clause (in
5683 -- CC), and sets Comp to the entity, and Fbit/Lbit to the zero origin
5684 -- start/stop bits for the field. If there is no matching component or
5685 -- if the matching component does not have a component clause, then
5686 -- that's an error and Comp is set to Empty, but no error message is
5687 -- issued, since the message was already given. Comp is also set to
5688 -- Empty if the current "component clause" is in fact a pragma.
5690 -----------------------------
5691 -- Check_Component_Overlap --
5692 -----------------------------
5694 procedure Check_Component_Overlap (C1_Ent, C2_Ent : Entity_Id) is
5695 CC1 : constant Node_Id := Component_Clause (C1_Ent);
5696 CC2 : constant Node_Id := Component_Clause (C2_Ent);
5699 if Present (CC1) and then Present (CC2) then
5701 -- Exclude odd case where we have two tag fields in the same
5702 -- record, both at location zero. This seems a bit strange, but
5703 -- it seems to happen in some circumstances, perhaps on an error.
5705 if Chars (C1_Ent) = Name_uTag
5707 Chars (C2_Ent) = Name_uTag
5712 -- Here we check if the two fields overlap
5715 S1 : constant Uint := Component_Bit_Offset (C1_Ent);
5716 S2 : constant Uint := Component_Bit_Offset (C2_Ent);
5717 E1 : constant Uint := S1 + Esize (C1_Ent);
5718 E2 : constant Uint := S2 + Esize (C2_Ent);
5721 if E2 <= S1 or else E1 <= S2 then
5724 Error_Msg_Node_2 := Component_Name (CC2);
5725 Error_Msg_Sloc := Sloc (Error_Msg_Node_2);
5726 Error_Msg_Node_1 := Component_Name (CC1);
5728 ("component& overlaps & #", Component_Name (CC1));
5729 Overlap_Detected := True;
5733 end Check_Component_Overlap;
5735 --------------------
5736 -- Find_Component --
5737 --------------------
5739 procedure Find_Component is
5741 procedure Search_Component (R : Entity_Id);
5742 -- Search components of R for a match. If found, Comp is set.
5744 ----------------------
5745 -- Search_Component --
5746 ----------------------
5748 procedure Search_Component (R : Entity_Id) is
5750 Comp := First_Component_Or_Discriminant (R);
5751 while Present (Comp) loop
5753 -- Ignore error of attribute name for component name (we
5754 -- already gave an error message for this, so no need to
5757 if Nkind (Component_Name (CC)) = N_Attribute_Reference then
5760 exit when Chars (Comp) = Chars (Component_Name (CC));
5763 Next_Component_Or_Discriminant (Comp);
5765 end Search_Component;
5767 -- Start of processing for Find_Component
5770 -- Return with Comp set to Empty if we have a pragma
5772 if Nkind (CC) = N_Pragma then
5777 -- Search current record for matching component
5779 Search_Component (Rectype);
5781 -- If not found, maybe component of base type that is absent from
5782 -- statically constrained first subtype.
5785 Search_Component (Base_Type (Rectype));
5788 -- If no component, or the component does not reference the component
5789 -- clause in question, then there was some previous error for which
5790 -- we already gave a message, so just return with Comp Empty.
5793 or else Component_Clause (Comp) /= CC
5797 -- Normal case where we have a component clause
5800 Fbit := Component_Bit_Offset (Comp);
5801 Lbit := Fbit + Esize (Comp) - 1;
5805 -- Start of processing for Check_Record_Representation_Clause
5809 Rectype := Entity (Ident);
5811 if Rectype = Any_Type then
5814 Rectype := Underlying_Type (Rectype);
5817 -- See if we have a fully repped derived tagged type
5820 PS : constant Entity_Id := Parent_Subtype (Rectype);
5823 if Present (PS) and then Is_Fully_Repped_Tagged_Type (PS) then
5824 Tagged_Parent := PS;
5826 -- Find maximum bit of any component of the parent type
5828 Parent_Last_Bit := UI_From_Int (System_Address_Size - 1);
5829 Pcomp := First_Entity (Tagged_Parent);
5830 while Present (Pcomp) loop
5831 if Ekind_In (Pcomp, E_Discriminant, E_Component) then
5832 if Component_Bit_Offset (Pcomp) /= No_Uint
5833 and then Known_Static_Esize (Pcomp)
5838 Component_Bit_Offset (Pcomp) + Esize (Pcomp) - 1);
5841 Next_Entity (Pcomp);
5847 -- All done if no component clauses
5849 CC := First (Component_Clauses (N));
5855 -- If a tag is present, then create a component clause that places it
5856 -- at the start of the record (otherwise gigi may place it after other
5857 -- fields that have rep clauses).
5859 Fent := First_Entity (Rectype);
5861 if Nkind (Fent) = N_Defining_Identifier
5862 and then Chars (Fent) = Name_uTag
5864 Set_Component_Bit_Offset (Fent, Uint_0);
5865 Set_Normalized_Position (Fent, Uint_0);
5866 Set_Normalized_First_Bit (Fent, Uint_0);
5867 Set_Normalized_Position_Max (Fent, Uint_0);
5868 Init_Esize (Fent, System_Address_Size);
5870 Set_Component_Clause (Fent,
5871 Make_Component_Clause (Loc,
5872 Component_Name => Make_Identifier (Loc, Name_uTag),
5874 Position => Make_Integer_Literal (Loc, Uint_0),
5875 First_Bit => Make_Integer_Literal (Loc, Uint_0),
5877 Make_Integer_Literal (Loc,
5878 UI_From_Int (System_Address_Size))));
5880 Ccount := Ccount + 1;
5883 Max_Bit_So_Far := Uint_Minus_1;
5884 Overlap_Check_Required := False;
5886 -- Process the component clauses
5888 while Present (CC) loop
5891 if Present (Comp) then
5892 Ccount := Ccount + 1;
5894 -- We need a full overlap check if record positions non-monotonic
5896 if Fbit <= Max_Bit_So_Far then
5897 Overlap_Check_Required := True;
5900 Max_Bit_So_Far := Lbit;
5902 -- Check bit position out of range of specified size
5904 if Has_Size_Clause (Rectype)
5905 and then Esize (Rectype) <= Lbit
5908 ("bit number out of range of specified size",
5911 -- Check for overlap with tag field
5914 if Is_Tagged_Type (Rectype)
5915 and then Fbit < System_Address_Size
5918 ("component overlaps tag field of&",
5919 Component_Name (CC), Rectype);
5920 Overlap_Detected := True;
5928 -- Check parent overlap if component might overlap parent field
5930 if Present (Tagged_Parent)
5931 and then Fbit <= Parent_Last_Bit
5933 Pcomp := First_Component_Or_Discriminant (Tagged_Parent);
5934 while Present (Pcomp) loop
5935 if not Is_Tag (Pcomp)
5936 and then Chars (Pcomp) /= Name_uParent
5938 Check_Component_Overlap (Comp, Pcomp);
5941 Next_Component_Or_Discriminant (Pcomp);
5949 -- Now that we have processed all the component clauses, check for
5950 -- overlap. We have to leave this till last, since the components can
5951 -- appear in any arbitrary order in the representation clause.
5953 -- We do not need this check if all specified ranges were monotonic,
5954 -- as recorded by Overlap_Check_Required being False at this stage.
5956 -- This first section checks if there are any overlapping entries at
5957 -- all. It does this by sorting all entries and then seeing if there are
5958 -- any overlaps. If there are none, then that is decisive, but if there
5959 -- are overlaps, they may still be OK (they may result from fields in
5960 -- different variants).
5962 if Overlap_Check_Required then
5963 Overlap_Check1 : declare
5965 OC_Fbit : array (0 .. Ccount) of Uint;
5966 -- First-bit values for component clauses, the value is the offset
5967 -- of the first bit of the field from start of record. The zero
5968 -- entry is for use in sorting.
5970 OC_Lbit : array (0 .. Ccount) of Uint;
5971 -- Last-bit values for component clauses, the value is the offset
5972 -- of the last bit of the field from start of record. The zero
5973 -- entry is for use in sorting.
5975 OC_Count : Natural := 0;
5976 -- Count of entries in OC_Fbit and OC_Lbit
5978 function OC_Lt (Op1, Op2 : Natural) return Boolean;
5979 -- Compare routine for Sort
5981 procedure OC_Move (From : Natural; To : Natural);
5982 -- Move routine for Sort
5984 package Sorting is new GNAT.Heap_Sort_G (OC_Move, OC_Lt);
5990 function OC_Lt (Op1, Op2 : Natural) return Boolean is
5992 return OC_Fbit (Op1) < OC_Fbit (Op2);
5999 procedure OC_Move (From : Natural; To : Natural) is
6001 OC_Fbit (To) := OC_Fbit (From);
6002 OC_Lbit (To) := OC_Lbit (From);
6005 -- Start of processing for Overlap_Check
6008 CC := First (Component_Clauses (N));
6009 while Present (CC) loop
6011 -- Exclude component clause already marked in error
6013 if not Error_Posted (CC) then
6016 if Present (Comp) then
6017 OC_Count := OC_Count + 1;
6018 OC_Fbit (OC_Count) := Fbit;
6019 OC_Lbit (OC_Count) := Lbit;
6026 Sorting.Sort (OC_Count);
6028 Overlap_Check_Required := False;
6029 for J in 1 .. OC_Count - 1 loop
6030 if OC_Lbit (J) >= OC_Fbit (J + 1) then
6031 Overlap_Check_Required := True;
6038 -- If Overlap_Check_Required is still True, then we have to do the full
6039 -- scale overlap check, since we have at least two fields that do
6040 -- overlap, and we need to know if that is OK since they are in
6041 -- different variant, or whether we have a definite problem.
6043 if Overlap_Check_Required then
6044 Overlap_Check2 : declare
6045 C1_Ent, C2_Ent : Entity_Id;
6046 -- Entities of components being checked for overlap
6049 -- Component_List node whose Component_Items are being checked
6052 -- Component declaration for component being checked
6055 C1_Ent := First_Entity (Base_Type (Rectype));
6057 -- Loop through all components in record. For each component check
6058 -- for overlap with any of the preceding elements on the component
6059 -- list containing the component and also, if the component is in
6060 -- a variant, check against components outside the case structure.
6061 -- This latter test is repeated recursively up the variant tree.
6063 Main_Component_Loop : while Present (C1_Ent) loop
6064 if not Ekind_In (C1_Ent, E_Component, E_Discriminant) then
6065 goto Continue_Main_Component_Loop;
6068 -- Skip overlap check if entity has no declaration node. This
6069 -- happens with discriminants in constrained derived types.
6070 -- Possibly we are missing some checks as a result, but that
6071 -- does not seem terribly serious.
6073 if No (Declaration_Node (C1_Ent)) then
6074 goto Continue_Main_Component_Loop;
6077 Clist := Parent (List_Containing (Declaration_Node (C1_Ent)));
6079 -- Loop through component lists that need checking. Check the
6080 -- current component list and all lists in variants above us.
6082 Component_List_Loop : loop
6084 -- If derived type definition, go to full declaration
6085 -- If at outer level, check discriminants if there are any.
6087 if Nkind (Clist) = N_Derived_Type_Definition then
6088 Clist := Parent (Clist);
6091 -- Outer level of record definition, check discriminants
6093 if Nkind_In (Clist, N_Full_Type_Declaration,
6094 N_Private_Type_Declaration)
6096 if Has_Discriminants (Defining_Identifier (Clist)) then
6098 First_Discriminant (Defining_Identifier (Clist));
6099 while Present (C2_Ent) loop
6100 exit when C1_Ent = C2_Ent;
6101 Check_Component_Overlap (C1_Ent, C2_Ent);
6102 Next_Discriminant (C2_Ent);
6106 -- Record extension case
6108 elsif Nkind (Clist) = N_Derived_Type_Definition then
6111 -- Otherwise check one component list
6114 Citem := First (Component_Items (Clist));
6115 while Present (Citem) loop
6116 if Nkind (Citem) = N_Component_Declaration then
6117 C2_Ent := Defining_Identifier (Citem);
6118 exit when C1_Ent = C2_Ent;
6119 Check_Component_Overlap (C1_Ent, C2_Ent);
6126 -- Check for variants above us (the parent of the Clist can
6127 -- be a variant, in which case its parent is a variant part,
6128 -- and the parent of the variant part is a component list
6129 -- whose components must all be checked against the current
6130 -- component for overlap).
6132 if Nkind (Parent (Clist)) = N_Variant then
6133 Clist := Parent (Parent (Parent (Clist)));
6135 -- Check for possible discriminant part in record, this
6136 -- is treated essentially as another level in the
6137 -- recursion. For this case the parent of the component
6138 -- list is the record definition, and its parent is the
6139 -- full type declaration containing the discriminant
6142 elsif Nkind (Parent (Clist)) = N_Record_Definition then
6143 Clist := Parent (Parent ((Clist)));
6145 -- If neither of these two cases, we are at the top of
6149 exit Component_List_Loop;
6151 end loop Component_List_Loop;
6153 <<Continue_Main_Component_Loop>>
6154 Next_Entity (C1_Ent);
6156 end loop Main_Component_Loop;
6160 -- The following circuit deals with warning on record holes (gaps). We
6161 -- skip this check if overlap was detected, since it makes sense for the
6162 -- programmer to fix this illegality before worrying about warnings.
6164 if not Overlap_Detected and Warn_On_Record_Holes then
6165 Record_Hole_Check : declare
6166 Decl : constant Node_Id := Declaration_Node (Base_Type (Rectype));
6167 -- Full declaration of record type
6169 procedure Check_Component_List
6173 -- Check component list CL for holes. The starting bit should be
6174 -- Sbit. which is zero for the main record component list and set
6175 -- appropriately for recursive calls for variants. DS is set to
6176 -- a list of discriminant specifications to be included in the
6177 -- consideration of components. It is No_List if none to consider.
6179 --------------------------
6180 -- Check_Component_List --
6181 --------------------------
6183 procedure Check_Component_List
6191 Compl := Integer (List_Length (Component_Items (CL)));
6193 if DS /= No_List then
6194 Compl := Compl + Integer (List_Length (DS));
6198 Comps : array (Natural range 0 .. Compl) of Entity_Id;
6199 -- Gather components (zero entry is for sort routine)
6201 Ncomps : Natural := 0;
6202 -- Number of entries stored in Comps (starting at Comps (1))
6205 -- One component item or discriminant specification
6208 -- Starting bit for next component
6216 function Lt (Op1, Op2 : Natural) return Boolean;
6217 -- Compare routine for Sort
6219 procedure Move (From : Natural; To : Natural);
6220 -- Move routine for Sort
6222 package Sorting is new GNAT.Heap_Sort_G (Move, Lt);
6228 function Lt (Op1, Op2 : Natural) return Boolean is
6230 return Component_Bit_Offset (Comps (Op1))
6232 Component_Bit_Offset (Comps (Op2));
6239 procedure Move (From : Natural; To : Natural) is
6241 Comps (To) := Comps (From);
6245 -- Gather discriminants into Comp
6247 if DS /= No_List then
6248 Citem := First (DS);
6249 while Present (Citem) loop
6250 if Nkind (Citem) = N_Discriminant_Specification then
6252 Ent : constant Entity_Id :=
6253 Defining_Identifier (Citem);
6255 if Ekind (Ent) = E_Discriminant then
6256 Ncomps := Ncomps + 1;
6257 Comps (Ncomps) := Ent;
6266 -- Gather component entities into Comp
6268 Citem := First (Component_Items (CL));
6269 while Present (Citem) loop
6270 if Nkind (Citem) = N_Component_Declaration then
6271 Ncomps := Ncomps + 1;
6272 Comps (Ncomps) := Defining_Identifier (Citem);
6278 -- Now sort the component entities based on the first bit.
6279 -- Note we already know there are no overlapping components.
6281 Sorting.Sort (Ncomps);
6283 -- Loop through entries checking for holes
6286 for J in 1 .. Ncomps loop
6288 Error_Msg_Uint_1 := Component_Bit_Offset (CEnt) - Nbit;
6290 if Error_Msg_Uint_1 > 0 then
6292 ("?^-bit gap before component&",
6293 Component_Name (Component_Clause (CEnt)), CEnt);
6296 Nbit := Component_Bit_Offset (CEnt) + Esize (CEnt);
6299 -- Process variant parts recursively if present
6301 if Present (Variant_Part (CL)) then
6302 Variant := First (Variants (Variant_Part (CL)));
6303 while Present (Variant) loop
6304 Check_Component_List
6305 (Component_List (Variant), Nbit, No_List);
6310 end Check_Component_List;
6312 -- Start of processing for Record_Hole_Check
6319 if Is_Tagged_Type (Rectype) then
6320 Sbit := UI_From_Int (System_Address_Size);
6325 if Nkind (Decl) = N_Full_Type_Declaration
6326 and then Nkind (Type_Definition (Decl)) = N_Record_Definition
6328 Check_Component_List
6329 (Component_List (Type_Definition (Decl)),
6331 Discriminant_Specifications (Decl));
6334 end Record_Hole_Check;
6337 -- For records that have component clauses for all components, and whose
6338 -- size is less than or equal to 32, we need to know the size in the
6339 -- front end to activate possible packed array processing where the
6340 -- component type is a record.
6342 -- At this stage Hbit + 1 represents the first unused bit from all the
6343 -- component clauses processed, so if the component clauses are
6344 -- complete, then this is the length of the record.
6346 -- For records longer than System.Storage_Unit, and for those where not
6347 -- all components have component clauses, the back end determines the
6348 -- length (it may for example be appropriate to round up the size
6349 -- to some convenient boundary, based on alignment considerations, etc).
6351 if Unknown_RM_Size (Rectype) and then Hbit + 1 <= 32 then
6353 -- Nothing to do if at least one component has no component clause
6355 Comp := First_Component_Or_Discriminant (Rectype);
6356 while Present (Comp) loop
6357 exit when No (Component_Clause (Comp));
6358 Next_Component_Or_Discriminant (Comp);
6361 -- If we fall out of loop, all components have component clauses
6362 -- and so we can set the size to the maximum value.
6365 Set_RM_Size (Rectype, Hbit + 1);
6368 end Check_Record_Representation_Clause;
6374 procedure Check_Size
6378 Biased : out Boolean)
6380 UT : constant Entity_Id := Underlying_Type (T);
6386 -- Dismiss cases for generic types or types with previous errors
6389 or else UT = Any_Type
6390 or else Is_Generic_Type (UT)
6391 or else Is_Generic_Type (Root_Type (UT))
6395 -- Check case of bit packed array
6397 elsif Is_Array_Type (UT)
6398 and then Known_Static_Component_Size (UT)
6399 and then Is_Bit_Packed_Array (UT)
6407 Asiz := Component_Size (UT);
6408 Indx := First_Index (UT);
6410 Ityp := Etype (Indx);
6412 -- If non-static bound, then we are not in the business of
6413 -- trying to check the length, and indeed an error will be
6414 -- issued elsewhere, since sizes of non-static array types
6415 -- cannot be set implicitly or explicitly.
6417 if not Is_Static_Subtype (Ityp) then
6421 -- Otherwise accumulate next dimension
6423 Asiz := Asiz * (Expr_Value (Type_High_Bound (Ityp)) -
6424 Expr_Value (Type_Low_Bound (Ityp)) +
6428 exit when No (Indx);
6434 Error_Msg_Uint_1 := Asiz;
6436 ("size for& too small, minimum allowed is ^", N, T);
6437 Set_Esize (T, Asiz);
6438 Set_RM_Size (T, Asiz);
6442 -- All other composite types are ignored
6444 elsif Is_Composite_Type (UT) then
6447 -- For fixed-point types, don't check minimum if type is not frozen,
6448 -- since we don't know all the characteristics of the type that can
6449 -- affect the size (e.g. a specified small) till freeze time.
6451 elsif Is_Fixed_Point_Type (UT)
6452 and then not Is_Frozen (UT)
6456 -- Cases for which a minimum check is required
6459 -- Ignore if specified size is correct for the type
6461 if Known_Esize (UT) and then Siz = Esize (UT) then
6465 -- Otherwise get minimum size
6467 M := UI_From_Int (Minimum_Size (UT));
6471 -- Size is less than minimum size, but one possibility remains
6472 -- that we can manage with the new size if we bias the type.
6474 M := UI_From_Int (Minimum_Size (UT, Biased => True));
6477 Error_Msg_Uint_1 := M;
6479 ("size for& too small, minimum allowed is ^", N, T);
6489 -------------------------
6490 -- Get_Alignment_Value --
6491 -------------------------
6493 function Get_Alignment_Value (Expr : Node_Id) return Uint is
6494 Align : constant Uint := Static_Integer (Expr);
6497 if Align = No_Uint then
6500 elsif Align <= 0 then
6501 Error_Msg_N ("alignment value must be positive", Expr);
6505 for J in Int range 0 .. 64 loop
6507 M : constant Uint := Uint_2 ** J;
6510 exit when M = Align;
6514 ("alignment value must be power of 2", Expr);
6522 end Get_Alignment_Value;
6528 procedure Initialize is
6530 Address_Clause_Checks.Init;
6531 Independence_Checks.Init;
6532 Unchecked_Conversions.Init;
6535 -------------------------
6536 -- Is_Operational_Item --
6537 -------------------------
6539 function Is_Operational_Item (N : Node_Id) return Boolean is
6541 if Nkind (N) /= N_Attribute_Definition_Clause then
6545 Id : constant Attribute_Id := Get_Attribute_Id (Chars (N));
6547 return Id = Attribute_Input
6548 or else Id = Attribute_Output
6549 or else Id = Attribute_Read
6550 or else Id = Attribute_Write
6551 or else Id = Attribute_External_Tag;
6554 end Is_Operational_Item;
6560 function Minimum_Size
6562 Biased : Boolean := False) return Nat
6564 Lo : Uint := No_Uint;
6565 Hi : Uint := No_Uint;
6566 LoR : Ureal := No_Ureal;
6567 HiR : Ureal := No_Ureal;
6568 LoSet : Boolean := False;
6569 HiSet : Boolean := False;
6573 R_Typ : constant Entity_Id := Root_Type (T);
6576 -- If bad type, return 0
6578 if T = Any_Type then
6581 -- For generic types, just return zero. There cannot be any legitimate
6582 -- need to know such a size, but this routine may be called with a
6583 -- generic type as part of normal processing.
6585 elsif Is_Generic_Type (R_Typ)
6586 or else R_Typ = Any_Type
6590 -- Access types. Normally an access type cannot have a size smaller
6591 -- than the size of System.Address. The exception is on VMS, where
6592 -- we have short and long addresses, and it is possible for an access
6593 -- type to have a short address size (and thus be less than the size
6594 -- of System.Address itself). We simply skip the check for VMS, and
6595 -- leave it to the back end to do the check.
6597 elsif Is_Access_Type (T) then
6598 if OpenVMS_On_Target then
6601 return System_Address_Size;
6604 -- Floating-point types
6606 elsif Is_Floating_Point_Type (T) then
6607 return UI_To_Int (Esize (R_Typ));
6611 elsif Is_Discrete_Type (T) then
6613 -- The following loop is looking for the nearest compile time known
6614 -- bounds following the ancestor subtype chain. The idea is to find
6615 -- the most restrictive known bounds information.
6619 if Ancest = Any_Type or else Etype (Ancest) = Any_Type then
6624 if Compile_Time_Known_Value (Type_Low_Bound (Ancest)) then
6625 Lo := Expr_Rep_Value (Type_Low_Bound (Ancest));
6632 if Compile_Time_Known_Value (Type_High_Bound (Ancest)) then
6633 Hi := Expr_Rep_Value (Type_High_Bound (Ancest));
6639 Ancest := Ancestor_Subtype (Ancest);
6642 Ancest := Base_Type (T);
6644 if Is_Generic_Type (Ancest) then
6650 -- Fixed-point types. We can't simply use Expr_Value to get the
6651 -- Corresponding_Integer_Value values of the bounds, since these do not
6652 -- get set till the type is frozen, and this routine can be called
6653 -- before the type is frozen. Similarly the test for bounds being static
6654 -- needs to include the case where we have unanalyzed real literals for
6657 elsif Is_Fixed_Point_Type (T) then
6659 -- The following loop is looking for the nearest compile time known
6660 -- bounds following the ancestor subtype chain. The idea is to find
6661 -- the most restrictive known bounds information.
6665 if Ancest = Any_Type or else Etype (Ancest) = Any_Type then
6669 -- Note: In the following two tests for LoSet and HiSet, it may
6670 -- seem redundant to test for N_Real_Literal here since normally
6671 -- one would assume that the test for the value being known at
6672 -- compile time includes this case. However, there is a glitch.
6673 -- If the real literal comes from folding a non-static expression,
6674 -- then we don't consider any non- static expression to be known
6675 -- at compile time if we are in configurable run time mode (needed
6676 -- in some cases to give a clearer definition of what is and what
6677 -- is not accepted). So the test is indeed needed. Without it, we
6678 -- would set neither Lo_Set nor Hi_Set and get an infinite loop.
6681 if Nkind (Type_Low_Bound (Ancest)) = N_Real_Literal
6682 or else Compile_Time_Known_Value (Type_Low_Bound (Ancest))
6684 LoR := Expr_Value_R (Type_Low_Bound (Ancest));
6691 if Nkind (Type_High_Bound (Ancest)) = N_Real_Literal
6692 or else Compile_Time_Known_Value (Type_High_Bound (Ancest))
6694 HiR := Expr_Value_R (Type_High_Bound (Ancest));
6700 Ancest := Ancestor_Subtype (Ancest);
6703 Ancest := Base_Type (T);
6705 if Is_Generic_Type (Ancest) then
6711 Lo := UR_To_Uint (LoR / Small_Value (T));
6712 Hi := UR_To_Uint (HiR / Small_Value (T));
6714 -- No other types allowed
6717 raise Program_Error;
6720 -- Fall through with Hi and Lo set. Deal with biased case
6723 and then not Is_Fixed_Point_Type (T)
6724 and then not (Is_Enumeration_Type (T)
6725 and then Has_Non_Standard_Rep (T)))
6726 or else Has_Biased_Representation (T)
6732 -- Signed case. Note that we consider types like range 1 .. -1 to be
6733 -- signed for the purpose of computing the size, since the bounds have
6734 -- to be accommodated in the base type.
6736 if Lo < 0 or else Hi < 0 then
6740 -- S = size, B = 2 ** (size - 1) (can accommodate -B .. +(B - 1))
6741 -- Note that we accommodate the case where the bounds cross. This
6742 -- can happen either because of the way the bounds are declared
6743 -- or because of the algorithm in Freeze_Fixed_Point_Type.
6757 -- If both bounds are positive, make sure that both are represen-
6758 -- table in the case where the bounds are crossed. This can happen
6759 -- either because of the way the bounds are declared, or because of
6760 -- the algorithm in Freeze_Fixed_Point_Type.
6766 -- S = size, (can accommodate 0 .. (2**size - 1))
6769 while Hi >= Uint_2 ** S loop
6777 ---------------------------
6778 -- New_Stream_Subprogram --
6779 ---------------------------
6781 procedure New_Stream_Subprogram
6785 Nam : TSS_Name_Type)
6787 Loc : constant Source_Ptr := Sloc (N);
6788 Sname : constant Name_Id := Make_TSS_Name (Base_Type (Ent), Nam);
6789 Subp_Id : Entity_Id;
6790 Subp_Decl : Node_Id;
6794 Defer_Declaration : constant Boolean :=
6795 Is_Tagged_Type (Ent) or else Is_Private_Type (Ent);
6796 -- For a tagged type, there is a declaration for each stream attribute
6797 -- at the freeze point, and we must generate only a completion of this
6798 -- declaration. We do the same for private types, because the full view
6799 -- might be tagged. Otherwise we generate a declaration at the point of
6800 -- the attribute definition clause.
6802 function Build_Spec return Node_Id;
6803 -- Used for declaration and renaming declaration, so that this is
6804 -- treated as a renaming_as_body.
6810 function Build_Spec return Node_Id is
6811 Out_P : constant Boolean := (Nam = TSS_Stream_Read);
6814 T_Ref : constant Node_Id := New_Reference_To (Etyp, Loc);
6817 Subp_Id := Make_Defining_Identifier (Loc, Sname);
6819 -- S : access Root_Stream_Type'Class
6821 Formals := New_List (
6822 Make_Parameter_Specification (Loc,
6823 Defining_Identifier =>
6824 Make_Defining_Identifier (Loc, Name_S),
6826 Make_Access_Definition (Loc,
6829 Designated_Type (Etype (F)), Loc))));
6831 if Nam = TSS_Stream_Input then
6832 Spec := Make_Function_Specification (Loc,
6833 Defining_Unit_Name => Subp_Id,
6834 Parameter_Specifications => Formals,
6835 Result_Definition => T_Ref);
6840 Make_Parameter_Specification (Loc,
6841 Defining_Identifier => Make_Defining_Identifier (Loc, Name_V),
6842 Out_Present => Out_P,
6843 Parameter_Type => T_Ref));
6846 Make_Procedure_Specification (Loc,
6847 Defining_Unit_Name => Subp_Id,
6848 Parameter_Specifications => Formals);
6854 -- Start of processing for New_Stream_Subprogram
6857 F := First_Formal (Subp);
6859 if Ekind (Subp) = E_Procedure then
6860 Etyp := Etype (Next_Formal (F));
6862 Etyp := Etype (Subp);
6865 -- Prepare subprogram declaration and insert it as an action on the
6866 -- clause node. The visibility for this entity is used to test for
6867 -- visibility of the attribute definition clause (in the sense of
6868 -- 8.3(23) as amended by AI-195).
6870 if not Defer_Declaration then
6872 Make_Subprogram_Declaration (Loc,
6873 Specification => Build_Spec);
6875 -- For a tagged type, there is always a visible declaration for each
6876 -- stream TSS (it is a predefined primitive operation), and the
6877 -- completion of this declaration occurs at the freeze point, which is
6878 -- not always visible at places where the attribute definition clause is
6879 -- visible. So, we create a dummy entity here for the purpose of
6880 -- tracking the visibility of the attribute definition clause itself.
6884 Make_Defining_Identifier (Loc, New_External_Name (Sname, 'V'));
6886 Make_Object_Declaration (Loc,
6887 Defining_Identifier => Subp_Id,
6888 Object_Definition => New_Occurrence_Of (Standard_Boolean, Loc));
6891 Insert_Action (N, Subp_Decl);
6892 Set_Entity (N, Subp_Id);
6895 Make_Subprogram_Renaming_Declaration (Loc,
6896 Specification => Build_Spec,
6897 Name => New_Reference_To (Subp, Loc));
6899 if Defer_Declaration then
6900 Set_TSS (Base_Type (Ent), Subp_Id);
6902 Insert_Action (N, Subp_Decl);
6903 Copy_TSS (Subp_Id, Base_Type (Ent));
6905 end New_Stream_Subprogram;
6907 ------------------------
6908 -- Rep_Item_Too_Early --
6909 ------------------------
6911 function Rep_Item_Too_Early (T : Entity_Id; N : Node_Id) return Boolean is
6913 -- Cannot apply non-operational rep items to generic types
6915 if Is_Operational_Item (N) then
6919 and then Is_Generic_Type (Root_Type (T))
6921 Error_Msg_N ("representation item not allowed for generic type", N);
6925 -- Otherwise check for incomplete type
6927 if Is_Incomplete_Or_Private_Type (T)
6928 and then No (Underlying_Type (T))
6931 ("representation item must be after full type declaration", N);
6934 -- If the type has incomplete components, a representation clause is
6935 -- illegal but stream attributes and Convention pragmas are correct.
6937 elsif Has_Private_Component (T) then
6938 if Nkind (N) = N_Pragma then
6942 ("representation item must appear after type is fully defined",
6949 end Rep_Item_Too_Early;
6951 -----------------------
6952 -- Rep_Item_Too_Late --
6953 -----------------------
6955 function Rep_Item_Too_Late
6958 FOnly : Boolean := False) return Boolean
6961 Parent_Type : Entity_Id;
6964 -- Output the too late message. Note that this is not considered a
6965 -- serious error, since the effect is simply that we ignore the
6966 -- representation clause in this case.
6972 procedure Too_Late is
6974 Error_Msg_N ("|representation item appears too late!", N);
6977 -- Start of processing for Rep_Item_Too_Late
6980 -- First make sure entity is not frozen (RM 13.1(9)). Exclude imported
6981 -- types, which may be frozen if they appear in a representation clause
6982 -- for a local type.
6985 and then not From_With_Type (T)
6988 S := First_Subtype (T);
6990 if Present (Freeze_Node (S)) then
6992 ("?no more representation items for }", Freeze_Node (S), S);
6997 -- Check for case of non-tagged derived type whose parent either has
6998 -- primitive operations, or is a by reference type (RM 13.1(10)).
7002 and then Is_Derived_Type (T)
7003 and then not Is_Tagged_Type (T)
7005 Parent_Type := Etype (Base_Type (T));
7007 if Has_Primitive_Operations (Parent_Type) then
7010 ("primitive operations already defined for&!", N, Parent_Type);
7013 elsif Is_By_Reference_Type (Parent_Type) then
7016 ("parent type & is a by reference type!", N, Parent_Type);
7021 -- No error, link item into head of chain of rep items for the entity,
7022 -- but avoid chaining if we have an overloadable entity, and the pragma
7023 -- is one that can apply to multiple overloaded entities.
7025 if Is_Overloadable (T)
7026 and then Nkind (N) = N_Pragma
7029 Pname : constant Name_Id := Pragma_Name (N);
7031 if Pname = Name_Convention or else
7032 Pname = Name_Import or else
7033 Pname = Name_Export or else
7034 Pname = Name_External or else
7035 Pname = Name_Interface
7042 Record_Rep_Item (T, N);
7044 end Rep_Item_Too_Late;
7046 -------------------------------------
7047 -- Replace_Type_References_Generic --
7048 -------------------------------------
7050 procedure Replace_Type_References_Generic (N : Node_Id; TName : Name_Id) is
7052 function Replace_Node (N : Node_Id) return Traverse_Result;
7053 -- Processes a single node in the traversal procedure below, checking
7054 -- if node N should be replaced, and if so, doing the replacement.
7056 procedure Replace_Type_Refs is new Traverse_Proc (Replace_Node);
7057 -- This instantiation provides the body of Replace_Type_References
7063 function Replace_Node (N : Node_Id) return Traverse_Result is
7068 -- Case of identifier
7070 if Nkind (N) = N_Identifier then
7072 -- If not the type name, all done with this node
7074 if Chars (N) /= TName then
7077 -- Otherwise do the replacement and we are done with this node
7080 Replace_Type_Reference (N);
7084 -- Case of selected component (which is what a qualification
7085 -- looks like in the unanalyzed tree, which is what we have.
7087 elsif Nkind (N) = N_Selected_Component then
7089 -- If selector name is not our type, keeping going (we might
7090 -- still have an occurrence of the type in the prefix).
7092 if Nkind (Selector_Name (N)) /= N_Identifier
7093 or else Chars (Selector_Name (N)) /= TName
7097 -- Selector name is our type, check qualification
7100 -- Loop through scopes and prefixes, doing comparison
7105 -- Continue if no more scopes or scope with no name
7107 if No (S) or else Nkind (S) not in N_Has_Chars then
7111 -- Do replace if prefix is an identifier matching the
7112 -- scope that we are currently looking at.
7114 if Nkind (P) = N_Identifier
7115 and then Chars (P) = Chars (S)
7117 Replace_Type_Reference (N);
7121 -- Go check scope above us if prefix is itself of the
7122 -- form of a selected component, whose selector matches
7123 -- the scope we are currently looking at.
7125 if Nkind (P) = N_Selected_Component
7126 and then Nkind (Selector_Name (P)) = N_Identifier
7127 and then Chars (Selector_Name (P)) = Chars (S)
7132 -- For anything else, we don't have a match, so keep on
7133 -- going, there are still some weird cases where we may
7134 -- still have a replacement within the prefix.
7142 -- Continue for any other node kind
7150 Replace_Type_Refs (N);
7151 end Replace_Type_References_Generic;
7153 -------------------------
7154 -- Same_Representation --
7155 -------------------------
7157 function Same_Representation (Typ1, Typ2 : Entity_Id) return Boolean is
7158 T1 : constant Entity_Id := Underlying_Type (Typ1);
7159 T2 : constant Entity_Id := Underlying_Type (Typ2);
7162 -- A quick check, if base types are the same, then we definitely have
7163 -- the same representation, because the subtype specific representation
7164 -- attributes (Size and Alignment) do not affect representation from
7165 -- the point of view of this test.
7167 if Base_Type (T1) = Base_Type (T2) then
7170 elsif Is_Private_Type (Base_Type (T2))
7171 and then Base_Type (T1) = Full_View (Base_Type (T2))
7176 -- Tagged types never have differing representations
7178 if Is_Tagged_Type (T1) then
7182 -- Representations are definitely different if conventions differ
7184 if Convention (T1) /= Convention (T2) then
7188 -- Representations are different if component alignments differ
7190 if (Is_Record_Type (T1) or else Is_Array_Type (T1))
7192 (Is_Record_Type (T2) or else Is_Array_Type (T2))
7193 and then Component_Alignment (T1) /= Component_Alignment (T2)
7198 -- For arrays, the only real issue is component size. If we know the
7199 -- component size for both arrays, and it is the same, then that's
7200 -- good enough to know we don't have a change of representation.
7202 if Is_Array_Type (T1) then
7203 if Known_Component_Size (T1)
7204 and then Known_Component_Size (T2)
7205 and then Component_Size (T1) = Component_Size (T2)
7211 -- Types definitely have same representation if neither has non-standard
7212 -- representation since default representations are always consistent.
7213 -- If only one has non-standard representation, and the other does not,
7214 -- then we consider that they do not have the same representation. They
7215 -- might, but there is no way of telling early enough.
7217 if Has_Non_Standard_Rep (T1) then
7218 if not Has_Non_Standard_Rep (T2) then
7222 return not Has_Non_Standard_Rep (T2);
7225 -- Here the two types both have non-standard representation, and we need
7226 -- to determine if they have the same non-standard representation.
7228 -- For arrays, we simply need to test if the component sizes are the
7229 -- same. Pragma Pack is reflected in modified component sizes, so this
7230 -- check also deals with pragma Pack.
7232 if Is_Array_Type (T1) then
7233 return Component_Size (T1) = Component_Size (T2);
7235 -- Tagged types always have the same representation, because it is not
7236 -- possible to specify different representations for common fields.
7238 elsif Is_Tagged_Type (T1) then
7241 -- Case of record types
7243 elsif Is_Record_Type (T1) then
7245 -- Packed status must conform
7247 if Is_Packed (T1) /= Is_Packed (T2) then
7250 -- Otherwise we must check components. Typ2 maybe a constrained
7251 -- subtype with fewer components, so we compare the components
7252 -- of the base types.
7255 Record_Case : declare
7256 CD1, CD2 : Entity_Id;
7258 function Same_Rep return Boolean;
7259 -- CD1 and CD2 are either components or discriminants. This
7260 -- function tests whether the two have the same representation
7266 function Same_Rep return Boolean is
7268 if No (Component_Clause (CD1)) then
7269 return No (Component_Clause (CD2));
7273 Present (Component_Clause (CD2))
7275 Component_Bit_Offset (CD1) = Component_Bit_Offset (CD2)
7277 Esize (CD1) = Esize (CD2);
7281 -- Start of processing for Record_Case
7284 if Has_Discriminants (T1) then
7285 CD1 := First_Discriminant (T1);
7286 CD2 := First_Discriminant (T2);
7288 -- The number of discriminants may be different if the
7289 -- derived type has fewer (constrained by values). The
7290 -- invisible discriminants retain the representation of
7291 -- the original, so the discrepancy does not per se
7292 -- indicate a different representation.
7295 and then Present (CD2)
7297 if not Same_Rep then
7300 Next_Discriminant (CD1);
7301 Next_Discriminant (CD2);
7306 CD1 := First_Component (Underlying_Type (Base_Type (T1)));
7307 CD2 := First_Component (Underlying_Type (Base_Type (T2)));
7309 while Present (CD1) loop
7310 if not Same_Rep then
7313 Next_Component (CD1);
7314 Next_Component (CD2);
7322 -- For enumeration types, we must check each literal to see if the
7323 -- representation is the same. Note that we do not permit enumeration
7324 -- representation clauses for Character and Wide_Character, so these
7325 -- cases were already dealt with.
7327 elsif Is_Enumeration_Type (T1) then
7328 Enumeration_Case : declare
7332 L1 := First_Literal (T1);
7333 L2 := First_Literal (T2);
7335 while Present (L1) loop
7336 if Enumeration_Rep (L1) /= Enumeration_Rep (L2) then
7346 end Enumeration_Case;
7348 -- Any other types have the same representation for these purposes
7353 end Same_Representation;
7359 procedure Set_Biased
7363 Biased : Boolean := True)
7367 Set_Has_Biased_Representation (E);
7369 if Warn_On_Biased_Representation then
7371 ("?" & Msg & " forces biased representation for&", N, E);
7376 --------------------
7377 -- Set_Enum_Esize --
7378 --------------------
7380 procedure Set_Enum_Esize (T : Entity_Id) is
7388 -- Find the minimum standard size (8,16,32,64) that fits
7390 Lo := Enumeration_Rep (Entity (Type_Low_Bound (T)));
7391 Hi := Enumeration_Rep (Entity (Type_High_Bound (T)));
7394 if Lo >= -Uint_2**07 and then Hi < Uint_2**07 then
7395 Sz := Standard_Character_Size; -- May be > 8 on some targets
7397 elsif Lo >= -Uint_2**15 and then Hi < Uint_2**15 then
7400 elsif Lo >= -Uint_2**31 and then Hi < Uint_2**31 then
7403 else pragma Assert (Lo >= -Uint_2**63 and then Hi < Uint_2**63);
7408 if Hi < Uint_2**08 then
7409 Sz := Standard_Character_Size; -- May be > 8 on some targets
7411 elsif Hi < Uint_2**16 then
7414 elsif Hi < Uint_2**32 then
7417 else pragma Assert (Hi < Uint_2**63);
7422 -- That minimum is the proper size unless we have a foreign convention
7423 -- and the size required is 32 or less, in which case we bump the size
7424 -- up to 32. This is required for C and C++ and seems reasonable for
7425 -- all other foreign conventions.
7427 if Has_Foreign_Convention (T)
7428 and then Esize (T) < Standard_Integer_Size
7430 Init_Esize (T, Standard_Integer_Size);
7436 ------------------------------
7437 -- Validate_Address_Clauses --
7438 ------------------------------
7440 procedure Validate_Address_Clauses is
7442 for J in Address_Clause_Checks.First .. Address_Clause_Checks.Last loop
7444 ACCR : Address_Clause_Check_Record
7445 renames Address_Clause_Checks.Table (J);
7456 -- Skip processing of this entry if warning already posted
7458 if not Address_Warning_Posted (ACCR.N) then
7460 Expr := Original_Node (Expression (ACCR.N));
7464 X_Alignment := Alignment (ACCR.X);
7465 Y_Alignment := Alignment (ACCR.Y);
7467 -- Similarly obtain sizes
7469 X_Size := Esize (ACCR.X);
7470 Y_Size := Esize (ACCR.Y);
7472 -- Check for large object overlaying smaller one
7475 and then X_Size > Uint_0
7476 and then X_Size > Y_Size
7479 ("?& overlays smaller object", ACCR.N, ACCR.X);
7481 ("\?program execution may be erroneous", ACCR.N);
7482 Error_Msg_Uint_1 := X_Size;
7484 ("\?size of & is ^", ACCR.N, ACCR.X);
7485 Error_Msg_Uint_1 := Y_Size;
7487 ("\?size of & is ^", ACCR.N, ACCR.Y);
7489 -- Check for inadequate alignment, both of the base object
7490 -- and of the offset, if any.
7492 -- Note: we do not check the alignment if we gave a size
7493 -- warning, since it would likely be redundant.
7495 elsif Y_Alignment /= Uint_0
7496 and then (Y_Alignment < X_Alignment
7499 Nkind (Expr) = N_Attribute_Reference
7501 Attribute_Name (Expr) = Name_Address
7503 Has_Compatible_Alignment
7504 (ACCR.X, Prefix (Expr))
7505 /= Known_Compatible))
7508 ("?specified address for& may be inconsistent "
7512 ("\?program execution may be erroneous (RM 13.3(27))",
7514 Error_Msg_Uint_1 := X_Alignment;
7516 ("\?alignment of & is ^",
7518 Error_Msg_Uint_1 := Y_Alignment;
7520 ("\?alignment of & is ^",
7522 if Y_Alignment >= X_Alignment then
7524 ("\?but offset is not multiple of alignment",
7531 end Validate_Address_Clauses;
7533 ---------------------------
7534 -- Validate_Independence --
7535 ---------------------------
7537 procedure Validate_Independence is
7538 SU : constant Uint := UI_From_Int (System_Storage_Unit);
7546 procedure Check_Array_Type (Atyp : Entity_Id);
7547 -- Checks if the array type Atyp has independent components, and
7548 -- if not, outputs an appropriate set of error messages.
7550 procedure No_Independence;
7551 -- Output message that independence cannot be guaranteed
7553 function OK_Component (C : Entity_Id) return Boolean;
7554 -- Checks one component to see if it is independently accessible, and
7555 -- if so yields True, otherwise yields False if independent access
7556 -- cannot be guaranteed. This is a conservative routine, it only
7557 -- returns True if it knows for sure, it returns False if it knows
7558 -- there is a problem, or it cannot be sure there is no problem.
7560 procedure Reason_Bad_Component (C : Entity_Id);
7561 -- Outputs continuation message if a reason can be determined for
7562 -- the component C being bad.
7564 ----------------------
7565 -- Check_Array_Type --
7566 ----------------------
7568 procedure Check_Array_Type (Atyp : Entity_Id) is
7569 Ctyp : constant Entity_Id := Component_Type (Atyp);
7572 -- OK if no alignment clause, no pack, and no component size
7574 if not Has_Component_Size_Clause (Atyp)
7575 and then not Has_Alignment_Clause (Atyp)
7576 and then not Is_Packed (Atyp)
7581 -- Check actual component size
7583 if not Known_Component_Size (Atyp)
7584 or else not (Addressable (Component_Size (Atyp))
7585 and then Component_Size (Atyp) < 64)
7586 or else Component_Size (Atyp) mod Esize (Ctyp) /= 0
7590 -- Bad component size, check reason
7592 if Has_Component_Size_Clause (Atyp) then
7594 Get_Attribute_Definition_Clause
7595 (Atyp, Attribute_Component_Size);
7598 Error_Msg_Sloc := Sloc (P);
7599 Error_Msg_N ("\because of Component_Size clause#", N);
7604 if Is_Packed (Atyp) then
7605 P := Get_Rep_Pragma (Atyp, Name_Pack);
7608 Error_Msg_Sloc := Sloc (P);
7609 Error_Msg_N ("\because of pragma Pack#", N);
7614 -- No reason found, just return
7619 -- Array type is OK independence-wise
7622 end Check_Array_Type;
7624 ---------------------
7625 -- No_Independence --
7626 ---------------------
7628 procedure No_Independence is
7630 if Pragma_Name (N) = Name_Independent then
7632 ("independence cannot be guaranteed for&", N, E);
7635 ("independent components cannot be guaranteed for&", N, E);
7637 end No_Independence;
7643 function OK_Component (C : Entity_Id) return Boolean is
7644 Rec : constant Entity_Id := Scope (C);
7645 Ctyp : constant Entity_Id := Etype (C);
7648 -- OK if no component clause, no Pack, and no alignment clause
7650 if No (Component_Clause (C))
7651 and then not Is_Packed (Rec)
7652 and then not Has_Alignment_Clause (Rec)
7657 -- Here we look at the actual component layout. A component is
7658 -- addressable if its size is a multiple of the Esize of the
7659 -- component type, and its starting position in the record has
7660 -- appropriate alignment, and the record itself has appropriate
7661 -- alignment to guarantee the component alignment.
7663 -- Make sure sizes are static, always assume the worst for any
7664 -- cases where we cannot check static values.
7666 if not (Known_Static_Esize (C)
7667 and then Known_Static_Esize (Ctyp))
7672 -- Size of component must be addressable or greater than 64 bits
7673 -- and a multiple of bytes.
7675 if not Addressable (Esize (C))
7676 and then Esize (C) < Uint_64
7681 -- Check size is proper multiple
7683 if Esize (C) mod Esize (Ctyp) /= 0 then
7687 -- Check alignment of component is OK
7689 if not Known_Component_Bit_Offset (C)
7690 or else Component_Bit_Offset (C) < Uint_0
7691 or else Component_Bit_Offset (C) mod Esize (Ctyp) /= 0
7696 -- Check alignment of record type is OK
7698 if not Known_Alignment (Rec)
7699 or else (Alignment (Rec) * SU) mod Esize (Ctyp) /= 0
7704 -- All tests passed, component is addressable
7709 --------------------------
7710 -- Reason_Bad_Component --
7711 --------------------------
7713 procedure Reason_Bad_Component (C : Entity_Id) is
7714 Rec : constant Entity_Id := Scope (C);
7715 Ctyp : constant Entity_Id := Etype (C);
7718 -- If component clause present assume that's the problem
7720 if Present (Component_Clause (C)) then
7721 Error_Msg_Sloc := Sloc (Component_Clause (C));
7722 Error_Msg_N ("\because of Component_Clause#", N);
7726 -- If pragma Pack clause present, assume that's the problem
7728 if Is_Packed (Rec) then
7729 P := Get_Rep_Pragma (Rec, Name_Pack);
7732 Error_Msg_Sloc := Sloc (P);
7733 Error_Msg_N ("\because of pragma Pack#", N);
7738 -- See if record has bad alignment clause
7740 if Has_Alignment_Clause (Rec)
7741 and then Known_Alignment (Rec)
7742 and then (Alignment (Rec) * SU) mod Esize (Ctyp) /= 0
7744 P := Get_Attribute_Definition_Clause (Rec, Attribute_Alignment);
7747 Error_Msg_Sloc := Sloc (P);
7748 Error_Msg_N ("\because of Alignment clause#", N);
7752 -- Couldn't find a reason, so return without a message
7755 end Reason_Bad_Component;
7757 -- Start of processing for Validate_Independence
7760 for J in Independence_Checks.First .. Independence_Checks.Last loop
7761 N := Independence_Checks.Table (J).N;
7762 E := Independence_Checks.Table (J).E;
7763 IC := Pragma_Name (N) = Name_Independent_Components;
7765 -- Deal with component case
7767 if Ekind (E) = E_Discriminant or else Ekind (E) = E_Component then
7768 if not OK_Component (E) then
7770 Reason_Bad_Component (E);
7775 -- Deal with record with Independent_Components
7777 if IC and then Is_Record_Type (E) then
7778 Comp := First_Component_Or_Discriminant (E);
7779 while Present (Comp) loop
7780 if not OK_Component (Comp) then
7782 Reason_Bad_Component (Comp);
7786 Next_Component_Or_Discriminant (Comp);
7790 -- Deal with address clause case
7792 if Is_Object (E) then
7793 Addr := Address_Clause (E);
7795 if Present (Addr) then
7797 Error_Msg_Sloc := Sloc (Addr);
7798 Error_Msg_N ("\because of Address clause#", N);
7803 -- Deal with independent components for array type
7805 if IC and then Is_Array_Type (E) then
7806 Check_Array_Type (E);
7809 -- Deal with independent components for array object
7811 if IC and then Is_Object (E) and then Is_Array_Type (Etype (E)) then
7812 Check_Array_Type (Etype (E));
7817 end Validate_Independence;
7819 -----------------------------------
7820 -- Validate_Unchecked_Conversion --
7821 -----------------------------------
7823 procedure Validate_Unchecked_Conversion
7825 Act_Unit : Entity_Id)
7832 -- Obtain source and target types. Note that we call Ancestor_Subtype
7833 -- here because the processing for generic instantiation always makes
7834 -- subtypes, and we want the original frozen actual types.
7836 -- If we are dealing with private types, then do the check on their
7837 -- fully declared counterparts if the full declarations have been
7838 -- encountered (they don't have to be visible, but they must exist!)
7840 Source := Ancestor_Subtype (Etype (First_Formal (Act_Unit)));
7842 if Is_Private_Type (Source)
7843 and then Present (Underlying_Type (Source))
7845 Source := Underlying_Type (Source);
7848 Target := Ancestor_Subtype (Etype (Act_Unit));
7850 -- If either type is generic, the instantiation happens within a generic
7851 -- unit, and there is nothing to check. The proper check
7852 -- will happen when the enclosing generic is instantiated.
7854 if Is_Generic_Type (Source) or else Is_Generic_Type (Target) then
7858 if Is_Private_Type (Target)
7859 and then Present (Underlying_Type (Target))
7861 Target := Underlying_Type (Target);
7864 -- Source may be unconstrained array, but not target
7866 if Is_Array_Type (Target)
7867 and then not Is_Constrained (Target)
7870 ("unchecked conversion to unconstrained array not allowed", N);
7874 -- Warn if conversion between two different convention pointers
7876 if Is_Access_Type (Target)
7877 and then Is_Access_Type (Source)
7878 and then Convention (Target) /= Convention (Source)
7879 and then Warn_On_Unchecked_Conversion
7881 -- Give warnings for subprogram pointers only on most targets. The
7882 -- exception is VMS, where data pointers can have different lengths
7883 -- depending on the pointer convention.
7885 if Is_Access_Subprogram_Type (Target)
7886 or else Is_Access_Subprogram_Type (Source)
7887 or else OpenVMS_On_Target
7890 ("?conversion between pointers with different conventions!", N);
7894 -- Warn if one of the operands is Ada.Calendar.Time. Do not emit a
7895 -- warning when compiling GNAT-related sources.
7897 if Warn_On_Unchecked_Conversion
7898 and then not In_Predefined_Unit (N)
7899 and then RTU_Loaded (Ada_Calendar)
7901 (Chars (Source) = Name_Time
7903 Chars (Target) = Name_Time)
7905 -- If Ada.Calendar is loaded and the name of one of the operands is
7906 -- Time, there is a good chance that this is Ada.Calendar.Time.
7909 Calendar_Time : constant Entity_Id :=
7910 Full_View (RTE (RO_CA_Time));
7912 pragma Assert (Present (Calendar_Time));
7914 if Source = Calendar_Time
7915 or else Target = Calendar_Time
7918 ("?representation of 'Time values may change between " &
7919 "'G'N'A'T versions", N);
7924 -- Make entry in unchecked conversion table for later processing by
7925 -- Validate_Unchecked_Conversions, which will check sizes and alignments
7926 -- (using values set by the back-end where possible). This is only done
7927 -- if the appropriate warning is active.
7929 if Warn_On_Unchecked_Conversion then
7930 Unchecked_Conversions.Append
7931 (New_Val => UC_Entry'
7936 -- If both sizes are known statically now, then back end annotation
7937 -- is not required to do a proper check but if either size is not
7938 -- known statically, then we need the annotation.
7940 if Known_Static_RM_Size (Source)
7941 and then Known_Static_RM_Size (Target)
7945 Back_Annotate_Rep_Info := True;
7949 -- If unchecked conversion to access type, and access type is declared
7950 -- in the same unit as the unchecked conversion, then set the
7951 -- No_Strict_Aliasing flag (no strict aliasing is implicit in this
7954 if Is_Access_Type (Target) and then
7955 In_Same_Source_Unit (Target, N)
7957 Set_No_Strict_Aliasing (Implementation_Base_Type (Target));
7960 -- Generate N_Validate_Unchecked_Conversion node for back end in
7961 -- case the back end needs to perform special validation checks.
7963 -- Shouldn't this be in Exp_Ch13, since the check only gets done
7964 -- if we have full expansion and the back end is called ???
7967 Make_Validate_Unchecked_Conversion (Sloc (N));
7968 Set_Source_Type (Vnode, Source);
7969 Set_Target_Type (Vnode, Target);
7971 -- If the unchecked conversion node is in a list, just insert before it.
7972 -- If not we have some strange case, not worth bothering about.
7974 if Is_List_Member (N) then
7975 Insert_After (N, Vnode);
7977 end Validate_Unchecked_Conversion;
7979 ------------------------------------
7980 -- Validate_Unchecked_Conversions --
7981 ------------------------------------
7983 procedure Validate_Unchecked_Conversions is
7985 for N in Unchecked_Conversions.First .. Unchecked_Conversions.Last loop
7987 T : UC_Entry renames Unchecked_Conversions.Table (N);
7989 Eloc : constant Source_Ptr := T.Eloc;
7990 Source : constant Entity_Id := T.Source;
7991 Target : constant Entity_Id := T.Target;
7997 -- This validation check, which warns if we have unequal sizes for
7998 -- unchecked conversion, and thus potentially implementation
7999 -- dependent semantics, is one of the few occasions on which we
8000 -- use the official RM size instead of Esize. See description in
8001 -- Einfo "Handling of Type'Size Values" for details.
8003 if Serious_Errors_Detected = 0
8004 and then Known_Static_RM_Size (Source)
8005 and then Known_Static_RM_Size (Target)
8007 -- Don't do the check if warnings off for either type, note the
8008 -- deliberate use of OR here instead of OR ELSE to get the flag
8009 -- Warnings_Off_Used set for both types if appropriate.
8011 and then not (Has_Warnings_Off (Source)
8013 Has_Warnings_Off (Target))
8015 Source_Siz := RM_Size (Source);
8016 Target_Siz := RM_Size (Target);
8018 if Source_Siz /= Target_Siz then
8020 ("?types for unchecked conversion have different sizes!",
8023 if All_Errors_Mode then
8024 Error_Msg_Name_1 := Chars (Source);
8025 Error_Msg_Uint_1 := Source_Siz;
8026 Error_Msg_Name_2 := Chars (Target);
8027 Error_Msg_Uint_2 := Target_Siz;
8028 Error_Msg ("\size of % is ^, size of % is ^?", Eloc);
8030 Error_Msg_Uint_1 := UI_Abs (Source_Siz - Target_Siz);
8032 if Is_Discrete_Type (Source)
8033 and then Is_Discrete_Type (Target)
8035 if Source_Siz > Target_Siz then
8037 ("\?^ high order bits of source will be ignored!",
8040 elsif Is_Unsigned_Type (Source) then
8042 ("\?source will be extended with ^ high order " &
8043 "zero bits?!", Eloc);
8047 ("\?source will be extended with ^ high order " &
8052 elsif Source_Siz < Target_Siz then
8053 if Is_Discrete_Type (Target) then
8054 if Bytes_Big_Endian then
8056 ("\?target value will include ^ undefined " &
8061 ("\?target value will include ^ undefined " &
8068 ("\?^ trailing bits of target value will be " &
8069 "undefined!", Eloc);
8072 else pragma Assert (Source_Siz > Target_Siz);
8074 ("\?^ trailing bits of source will be ignored!",
8081 -- If both types are access types, we need to check the alignment.
8082 -- If the alignment of both is specified, we can do it here.
8084 if Serious_Errors_Detected = 0
8085 and then Ekind (Source) in Access_Kind
8086 and then Ekind (Target) in Access_Kind
8087 and then Target_Strict_Alignment
8088 and then Present (Designated_Type (Source))
8089 and then Present (Designated_Type (Target))
8092 D_Source : constant Entity_Id := Designated_Type (Source);
8093 D_Target : constant Entity_Id := Designated_Type (Target);
8096 if Known_Alignment (D_Source)
8097 and then Known_Alignment (D_Target)
8100 Source_Align : constant Uint := Alignment (D_Source);
8101 Target_Align : constant Uint := Alignment (D_Target);
8104 if Source_Align < Target_Align
8105 and then not Is_Tagged_Type (D_Source)
8107 -- Suppress warning if warnings suppressed on either
8108 -- type or either designated type. Note the use of
8109 -- OR here instead of OR ELSE. That is intentional,
8110 -- we would like to set flag Warnings_Off_Used in
8111 -- all types for which warnings are suppressed.
8113 and then not (Has_Warnings_Off (D_Source)
8115 Has_Warnings_Off (D_Target)
8117 Has_Warnings_Off (Source)
8119 Has_Warnings_Off (Target))
8121 Error_Msg_Uint_1 := Target_Align;
8122 Error_Msg_Uint_2 := Source_Align;
8123 Error_Msg_Node_1 := D_Target;
8124 Error_Msg_Node_2 := D_Source;
8126 ("?alignment of & (^) is stricter than " &
8127 "alignment of & (^)!", Eloc);
8129 ("\?resulting access value may have invalid " &
8130 "alignment!", Eloc);
8138 end Validate_Unchecked_Conversions;