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 Atree; use Atree;
27 with Checks; use Checks;
28 with Einfo; use Einfo;
29 with Elists; use Elists;
30 with Errout; use Errout;
31 with Exp_Disp; use Exp_Disp;
32 with Exp_Tss; use Exp_Tss;
33 with Exp_Util; use Exp_Util;
35 with Lib.Xref; use Lib.Xref;
36 with Namet; use Namet;
37 with Nlists; use Nlists;
38 with Nmake; use Nmake;
40 with Restrict; use Restrict;
41 with Rident; use Rident;
42 with Rtsfind; use Rtsfind;
44 with Sem_Aux; use Sem_Aux;
45 with Sem_Ch3; use Sem_Ch3;
46 with Sem_Ch8; use Sem_Ch8;
47 with Sem_Eval; use Sem_Eval;
48 with Sem_Res; use Sem_Res;
49 with Sem_Type; use Sem_Type;
50 with Sem_Util; use Sem_Util;
51 with Sem_Warn; use Sem_Warn;
52 with Snames; use Snames;
53 with Stand; use Stand;
54 with Sinfo; use Sinfo;
55 with Targparm; use Targparm;
56 with Ttypes; use Ttypes;
57 with Tbuild; use Tbuild;
58 with Urealp; use Urealp;
60 with GNAT.Heap_Sort_G;
62 package body Sem_Ch13 is
64 SSU : constant Pos := System_Storage_Unit;
65 -- Convenient short hand for commonly used constant
67 -----------------------
68 -- Local Subprograms --
69 -----------------------
71 procedure Alignment_Check_For_Esize_Change (Typ : Entity_Id);
72 -- This routine is called after setting the Esize of type entity Typ.
73 -- The purpose is to deal with the situation where an alignment has been
74 -- inherited from a derived type that is no longer appropriate for the
75 -- new Esize value. In this case, we reset the Alignment to unknown.
77 function Get_Alignment_Value (Expr : Node_Id) return Uint;
78 -- Given the expression for an alignment value, returns the corresponding
79 -- Uint value. If the value is inappropriate, then error messages are
80 -- posted as required, and a value of No_Uint is returned.
82 function Is_Operational_Item (N : Node_Id) return Boolean;
83 -- A specification for a stream attribute is allowed before the full
84 -- type is declared, as explained in AI-00137 and the corrigendum.
85 -- Attributes that do not specify a representation characteristic are
86 -- operational attributes.
88 procedure New_Stream_Subprogram
93 -- Create a subprogram renaming of a given stream attribute to the
94 -- designated subprogram and then in the tagged case, provide this as a
95 -- primitive operation, or in the non-tagged case make an appropriate TSS
96 -- entry. This is more properly an expansion activity than just semantics,
97 -- but the presence of user-defined stream functions for limited types is a
98 -- legality check, which is why this takes place here rather than in
99 -- exp_ch13, where it was previously. Nam indicates the name of the TSS
100 -- function to be generated.
102 -- To avoid elaboration anomalies with freeze nodes, for untagged types
103 -- we generate both a subprogram declaration and a subprogram renaming
104 -- declaration, so that the attribute specification is handled as a
105 -- renaming_as_body. For tagged types, the specification is one of the
112 Biased : Boolean := True);
113 -- If Biased is True, sets Has_Biased_Representation flag for E, and
114 -- outputs a warning message at node N if Warn_On_Biased_Representation is
115 -- is True. This warning inserts the string Msg to describe the construct
118 ----------------------------------------------
119 -- Table for Validate_Unchecked_Conversions --
120 ----------------------------------------------
122 -- The following table collects unchecked conversions for validation.
123 -- Entries are made by Validate_Unchecked_Conversion and then the
124 -- call to Validate_Unchecked_Conversions does the actual error
125 -- checking and posting of warnings. The reason for this delayed
126 -- processing is to take advantage of back-annotations of size and
127 -- alignment values performed by the back end.
129 -- Note: the reason we store a Source_Ptr value instead of a Node_Id
130 -- is that by the time Validate_Unchecked_Conversions is called, Sprint
131 -- will already have modified all Sloc values if the -gnatD option is set.
133 type UC_Entry is record
134 Eloc : Source_Ptr; -- node used for posting warnings
135 Source : Entity_Id; -- source type for unchecked conversion
136 Target : Entity_Id; -- target type for unchecked conversion
139 package Unchecked_Conversions is new Table.Table (
140 Table_Component_Type => UC_Entry,
141 Table_Index_Type => Int,
142 Table_Low_Bound => 1,
144 Table_Increment => 200,
145 Table_Name => "Unchecked_Conversions");
147 ----------------------------------------
148 -- Table for Validate_Address_Clauses --
149 ----------------------------------------
151 -- If an address clause has the form
153 -- for X'Address use Expr
155 -- where Expr is of the form Y'Address or recursively is a reference
156 -- to a constant of either of these forms, and X and Y are entities of
157 -- objects, then if Y has a smaller alignment than X, that merits a
158 -- warning about possible bad alignment. The following table collects
159 -- address clauses of this kind. We put these in a table so that they
160 -- can be checked after the back end has completed annotation of the
161 -- alignments of objects, since we can catch more cases that way.
163 type Address_Clause_Check_Record is record
165 -- The address clause
168 -- The entity of the object overlaying Y
171 -- The entity of the object being overlaid
174 -- Whether the address is offseted within Y
177 package Address_Clause_Checks is new Table.Table (
178 Table_Component_Type => Address_Clause_Check_Record,
179 Table_Index_Type => Int,
180 Table_Low_Bound => 1,
182 Table_Increment => 200,
183 Table_Name => "Address_Clause_Checks");
185 -----------------------------------------
186 -- Adjust_Record_For_Reverse_Bit_Order --
187 -----------------------------------------
189 procedure Adjust_Record_For_Reverse_Bit_Order (R : Entity_Id) is
194 -- Processing depends on version of Ada
196 -- For Ada 95, we just renumber bits within a storage unit. We do the
197 -- same for Ada 83 mode, since we recognize pragma Bit_Order in Ada 83,
198 -- and are free to add this extension.
200 if Ada_Version < Ada_2005 then
201 Comp := First_Component_Or_Discriminant (R);
202 while Present (Comp) loop
203 CC := Component_Clause (Comp);
205 -- If component clause is present, then deal with the non-default
206 -- bit order case for Ada 95 mode.
208 -- We only do this processing for the base type, and in fact that
209 -- is important, since otherwise if there are record subtypes, we
210 -- could reverse the bits once for each subtype, which is wrong.
213 and then Ekind (R) = E_Record_Type
216 CFB : constant Uint := Component_Bit_Offset (Comp);
217 CSZ : constant Uint := Esize (Comp);
218 CLC : constant Node_Id := Component_Clause (Comp);
219 Pos : constant Node_Id := Position (CLC);
220 FB : constant Node_Id := First_Bit (CLC);
222 Storage_Unit_Offset : constant Uint :=
223 CFB / System_Storage_Unit;
225 Start_Bit : constant Uint :=
226 CFB mod System_Storage_Unit;
229 -- Cases where field goes over storage unit boundary
231 if Start_Bit + CSZ > System_Storage_Unit then
233 -- Allow multi-byte field but generate warning
235 if Start_Bit mod System_Storage_Unit = 0
236 and then CSZ mod System_Storage_Unit = 0
239 ("multi-byte field specified with non-standard"
240 & " Bit_Order?", CLC);
242 if Bytes_Big_Endian then
244 ("bytes are not reversed "
245 & "(component is big-endian)?", CLC);
248 ("bytes are not reversed "
249 & "(component is little-endian)?", CLC);
252 -- Do not allow non-contiguous field
256 ("attempt to specify non-contiguous field "
257 & "not permitted", CLC);
259 ("\caused by non-standard Bit_Order "
262 ("\consider possibility of using "
263 & "Ada 2005 mode here", CLC);
266 -- Case where field fits in one storage unit
269 -- Give warning if suspicious component clause
271 if Intval (FB) >= System_Storage_Unit
272 and then Warn_On_Reverse_Bit_Order
275 ("?Bit_Order clause does not affect " &
276 "byte ordering", Pos);
278 Intval (Pos) + Intval (FB) /
281 ("?position normalized to ^ before bit " &
282 "order interpreted", Pos);
285 -- Here is where we fix up the Component_Bit_Offset value
286 -- to account for the reverse bit order. Some examples of
287 -- what needs to be done are:
289 -- First_Bit .. Last_Bit Component_Bit_Offset
301 -- The rule is that the first bit is is obtained by
302 -- subtracting the old ending bit from storage_unit - 1.
304 Set_Component_Bit_Offset
306 (Storage_Unit_Offset * System_Storage_Unit) +
307 (System_Storage_Unit - 1) -
308 (Start_Bit + CSZ - 1));
310 Set_Normalized_First_Bit
312 Component_Bit_Offset (Comp) mod
313 System_Storage_Unit);
318 Next_Component_Or_Discriminant (Comp);
321 -- For Ada 2005, we do machine scalar processing, as fully described In
322 -- AI-133. This involves gathering all components which start at the
323 -- same byte offset and processing them together. Same approach is still
324 -- valid in later versions including Ada 2012.
328 Max_Machine_Scalar_Size : constant Uint :=
330 (Standard_Long_Long_Integer_Size);
331 -- We use this as the maximum machine scalar size
334 SSU : constant Uint := UI_From_Int (System_Storage_Unit);
337 -- This first loop through components does two things. First it
338 -- deals with the case of components with component clauses whose
339 -- length is greater than the maximum machine scalar size (either
340 -- accepting them or rejecting as needed). Second, it counts the
341 -- number of components with component clauses whose length does
342 -- not exceed this maximum for later processing.
345 Comp := First_Component_Or_Discriminant (R);
346 while Present (Comp) loop
347 CC := Component_Clause (Comp);
351 Fbit : constant Uint :=
352 Static_Integer (First_Bit (CC));
355 -- Case of component with size > max machine scalar
357 if Esize (Comp) > Max_Machine_Scalar_Size then
359 -- Must begin on byte boundary
361 if Fbit mod SSU /= 0 then
363 ("illegal first bit value for "
364 & "reverse bit order",
366 Error_Msg_Uint_1 := SSU;
367 Error_Msg_Uint_2 := Max_Machine_Scalar_Size;
370 ("\must be a multiple of ^ "
371 & "if size greater than ^",
374 -- Must end on byte boundary
376 elsif Esize (Comp) mod SSU /= 0 then
378 ("illegal last bit value for "
379 & "reverse bit order",
381 Error_Msg_Uint_1 := SSU;
382 Error_Msg_Uint_2 := Max_Machine_Scalar_Size;
385 ("\must be a multiple of ^ if size "
389 -- OK, give warning if enabled
391 elsif Warn_On_Reverse_Bit_Order then
393 ("multi-byte field specified with "
394 & " non-standard Bit_Order?", CC);
396 if Bytes_Big_Endian then
398 ("\bytes are not reversed "
399 & "(component is big-endian)?", CC);
402 ("\bytes are not reversed "
403 & "(component is little-endian)?", CC);
407 -- Case where size is not greater than max machine
408 -- scalar. For now, we just count these.
411 Num_CC := Num_CC + 1;
416 Next_Component_Or_Discriminant (Comp);
419 -- We need to sort the component clauses on the basis of the
420 -- Position values in the clause, so we can group clauses with
421 -- the same Position. together to determine the relevant machine
425 Comps : array (0 .. Num_CC) of Entity_Id;
426 -- Array to collect component and discriminant entities. The
427 -- data starts at index 1, the 0'th entry is for the sort
430 function CP_Lt (Op1, Op2 : Natural) return Boolean;
431 -- Compare routine for Sort
433 procedure CP_Move (From : Natural; To : Natural);
434 -- Move routine for Sort
436 package Sorting is new GNAT.Heap_Sort_G (CP_Move, CP_Lt);
440 -- Start and stop positions in the component list of the set of
441 -- components with the same starting position (that constitute
442 -- components in a single machine scalar).
445 -- Maximum last bit value of any component in this set
448 -- Corresponding machine scalar size
454 function CP_Lt (Op1, Op2 : Natural) return Boolean is
456 return Position (Component_Clause (Comps (Op1))) <
457 Position (Component_Clause (Comps (Op2)));
464 procedure CP_Move (From : Natural; To : Natural) is
466 Comps (To) := Comps (From);
469 -- Start of processing for Sort_CC
472 -- Collect the component clauses
475 Comp := First_Component_Or_Discriminant (R);
476 while Present (Comp) loop
477 if Present (Component_Clause (Comp))
478 and then Esize (Comp) <= Max_Machine_Scalar_Size
480 Num_CC := Num_CC + 1;
481 Comps (Num_CC) := Comp;
484 Next_Component_Or_Discriminant (Comp);
487 -- Sort by ascending position number
489 Sorting.Sort (Num_CC);
491 -- We now have all the components whose size does not exceed
492 -- the max machine scalar value, sorted by starting position.
493 -- In this loop we gather groups of clauses starting at the
494 -- same position, to process them in accordance with AI-133.
497 while Stop < Num_CC loop
502 (Last_Bit (Component_Clause (Comps (Start))));
503 while Stop < Num_CC loop
505 (Position (Component_Clause (Comps (Stop + 1)))) =
507 (Position (Component_Clause (Comps (Stop))))
515 (Component_Clause (Comps (Stop)))));
521 -- Now we have a group of component clauses from Start to
522 -- Stop whose positions are identical, and MaxL is the
523 -- maximum last bit value of any of these components.
525 -- We need to determine the corresponding machine scalar
526 -- size. This loop assumes that machine scalar sizes are
527 -- even, and that each possible machine scalar has twice
528 -- as many bits as the next smaller one.
530 MSS := Max_Machine_Scalar_Size;
532 and then (MSS / 2) >= SSU
533 and then (MSS / 2) > MaxL
538 -- Here is where we fix up the Component_Bit_Offset value
539 -- to account for the reverse bit order. Some examples of
540 -- what needs to be done for the case of a machine scalar
543 -- First_Bit .. Last_Bit Component_Bit_Offset
555 -- The rule is that the first bit is obtained by subtracting
556 -- the old ending bit from machine scalar size - 1.
558 for C in Start .. Stop loop
560 Comp : constant Entity_Id := Comps (C);
561 CC : constant Node_Id :=
562 Component_Clause (Comp);
563 LB : constant Uint :=
564 Static_Integer (Last_Bit (CC));
565 NFB : constant Uint := MSS - Uint_1 - LB;
566 NLB : constant Uint := NFB + Esize (Comp) - 1;
567 Pos : constant Uint :=
568 Static_Integer (Position (CC));
571 if Warn_On_Reverse_Bit_Order then
572 Error_Msg_Uint_1 := MSS;
574 ("info: reverse bit order in machine " &
575 "scalar of length^?", First_Bit (CC));
576 Error_Msg_Uint_1 := NFB;
577 Error_Msg_Uint_2 := NLB;
579 if Bytes_Big_Endian then
581 ("?\info: big-endian range for "
582 & "component & is ^ .. ^",
583 First_Bit (CC), Comp);
586 ("?\info: little-endian range "
587 & "for component & is ^ .. ^",
588 First_Bit (CC), Comp);
592 Set_Component_Bit_Offset (Comp, Pos * SSU + NFB);
593 Set_Normalized_First_Bit (Comp, NFB mod SSU);
600 end Adjust_Record_For_Reverse_Bit_Order;
602 --------------------------------------
603 -- Alignment_Check_For_Esize_Change --
604 --------------------------------------
606 procedure Alignment_Check_For_Esize_Change (Typ : Entity_Id) is
608 -- If the alignment is known, and not set by a rep clause, and is
609 -- inconsistent with the size being set, then reset it to unknown,
610 -- we assume in this case that the size overrides the inherited
611 -- alignment, and that the alignment must be recomputed.
613 if Known_Alignment (Typ)
614 and then not Has_Alignment_Clause (Typ)
615 and then Esize (Typ) mod (Alignment (Typ) * SSU) /= 0
617 Init_Alignment (Typ);
619 end Alignment_Check_For_Esize_Change;
621 -----------------------
622 -- Analyze_At_Clause --
623 -----------------------
625 -- An at clause is replaced by the corresponding Address attribute
626 -- definition clause that is the preferred approach in Ada 95.
628 procedure Analyze_At_Clause (N : Node_Id) is
629 CS : constant Boolean := Comes_From_Source (N);
632 -- This is an obsolescent feature
634 Check_Restriction (No_Obsolescent_Features, N);
636 if Warn_On_Obsolescent_Feature then
638 ("at clause is an obsolescent feature (RM J.7(2))?", N);
640 ("\use address attribute definition clause instead?", N);
643 -- Rewrite as address clause
646 Make_Attribute_Definition_Clause (Sloc (N),
647 Name => Identifier (N),
648 Chars => Name_Address,
649 Expression => Expression (N)));
651 -- We preserve Comes_From_Source, since logically the clause still
652 -- comes from the source program even though it is changed in form.
654 Set_Comes_From_Source (N, CS);
656 -- Analyze rewritten clause
658 Analyze_Attribute_Definition_Clause (N);
659 end Analyze_At_Clause;
661 -----------------------------------------
662 -- Analyze_Attribute_Definition_Clause --
663 -----------------------------------------
665 procedure Analyze_Attribute_Definition_Clause (N : Node_Id) is
666 Loc : constant Source_Ptr := Sloc (N);
667 Nam : constant Node_Id := Name (N);
668 Attr : constant Name_Id := Chars (N);
669 Expr : constant Node_Id := Expression (N);
670 Id : constant Attribute_Id := Get_Attribute_Id (Attr);
674 FOnly : Boolean := False;
675 -- Reset to True for subtype specific attribute (Alignment, Size)
676 -- and for stream attributes, i.e. those cases where in the call
677 -- to Rep_Item_Too_Late, FOnly is set True so that only the freezing
678 -- rules are checked. Note that the case of stream attributes is not
679 -- clear from the RM, but see AI95-00137. Also, the RM seems to
680 -- disallow Storage_Size for derived task types, but that is also
681 -- clearly unintentional.
683 procedure Analyze_Stream_TSS_Definition (TSS_Nam : TSS_Name_Type);
684 -- Common processing for 'Read, 'Write, 'Input and 'Output attribute
685 -- definition clauses.
687 -----------------------------------
688 -- Analyze_Stream_TSS_Definition --
689 -----------------------------------
691 procedure Analyze_Stream_TSS_Definition (TSS_Nam : TSS_Name_Type) is
692 Subp : Entity_Id := Empty;
697 Is_Read : constant Boolean := (TSS_Nam = TSS_Stream_Read);
699 function Has_Good_Profile (Subp : Entity_Id) return Boolean;
700 -- Return true if the entity is a subprogram with an appropriate
701 -- profile for the attribute being defined.
703 ----------------------
704 -- Has_Good_Profile --
705 ----------------------
707 function Has_Good_Profile (Subp : Entity_Id) return Boolean is
709 Is_Function : constant Boolean := (TSS_Nam = TSS_Stream_Input);
710 Expected_Ekind : constant array (Boolean) of Entity_Kind :=
711 (False => E_Procedure, True => E_Function);
715 if Ekind (Subp) /= Expected_Ekind (Is_Function) then
719 F := First_Formal (Subp);
722 or else Ekind (Etype (F)) /= E_Anonymous_Access_Type
723 or else Designated_Type (Etype (F)) /=
724 Class_Wide_Type (RTE (RE_Root_Stream_Type))
729 if not Is_Function then
733 Expected_Mode : constant array (Boolean) of Entity_Kind :=
734 (False => E_In_Parameter,
735 True => E_Out_Parameter);
737 if Parameter_Mode (F) /= Expected_Mode (Is_Read) then
748 return Base_Type (Typ) = Base_Type (Ent)
749 and then No (Next_Formal (F));
750 end Has_Good_Profile;
752 -- Start of processing for Analyze_Stream_TSS_Definition
757 if not Is_Type (U_Ent) then
758 Error_Msg_N ("local name must be a subtype", Nam);
762 Pnam := TSS (Base_Type (U_Ent), TSS_Nam);
764 -- If Pnam is present, it can be either inherited from an ancestor
765 -- type (in which case it is legal to redefine it for this type), or
766 -- be a previous definition of the attribute for the same type (in
767 -- which case it is illegal).
769 -- In the first case, it will have been analyzed already, and we
770 -- can check that its profile does not match the expected profile
771 -- for a stream attribute of U_Ent. In the second case, either Pnam
772 -- has been analyzed (and has the expected profile), or it has not
773 -- been analyzed yet (case of a type that has not been frozen yet
774 -- and for which the stream attribute has been set using Set_TSS).
777 and then (No (First_Entity (Pnam)) or else Has_Good_Profile (Pnam))
779 Error_Msg_Sloc := Sloc (Pnam);
780 Error_Msg_Name_1 := Attr;
781 Error_Msg_N ("% attribute already defined #", Nam);
787 if Is_Entity_Name (Expr) then
788 if not Is_Overloaded (Expr) then
789 if Has_Good_Profile (Entity (Expr)) then
790 Subp := Entity (Expr);
794 Get_First_Interp (Expr, I, It);
795 while Present (It.Nam) loop
796 if Has_Good_Profile (It.Nam) then
801 Get_Next_Interp (I, It);
806 if Present (Subp) then
807 if Is_Abstract_Subprogram (Subp) then
808 Error_Msg_N ("stream subprogram must not be abstract", Expr);
812 Set_Entity (Expr, Subp);
813 Set_Etype (Expr, Etype (Subp));
815 New_Stream_Subprogram (N, U_Ent, Subp, TSS_Nam);
818 Error_Msg_Name_1 := Attr;
819 Error_Msg_N ("incorrect expression for% attribute", Expr);
821 end Analyze_Stream_TSS_Definition;
823 -- Start of processing for Analyze_Attribute_Definition_Clause
826 -- Process Ignore_Rep_Clauses option
828 if Ignore_Rep_Clauses then
831 -- The following should be ignored. They do not affect legality
832 -- and may be target dependent. The basic idea of -gnatI is to
833 -- ignore any rep clauses that may be target dependent but do not
834 -- affect legality (except possibly to be rejected because they
835 -- are incompatible with the compilation target).
837 when Attribute_Alignment |
838 Attribute_Bit_Order |
839 Attribute_Component_Size |
840 Attribute_Machine_Radix |
841 Attribute_Object_Size |
844 Attribute_Stream_Size |
845 Attribute_Value_Size =>
847 Rewrite (N, Make_Null_Statement (Sloc (N)));
850 -- The following should not be ignored, because in the first place
851 -- they are reasonably portable, and should not cause problems in
852 -- compiling code from another target, and also they do affect
853 -- legality, e.g. failing to provide a stream attribute for a
854 -- type may make a program illegal.
856 when Attribute_External_Tag |
860 Attribute_Storage_Pool |
861 Attribute_Storage_Size |
865 -- Other cases are errors ("attribute& cannot be set with
866 -- definition clause"), which will be caught below.
876 if Rep_Item_Too_Early (Ent, N) then
880 -- Rep clause applies to full view of incomplete type or private type if
881 -- we have one (if not, this is a premature use of the type). However,
882 -- certain semantic checks need to be done on the specified entity (i.e.
883 -- the private view), so we save it in Ent.
885 if Is_Private_Type (Ent)
886 and then Is_Derived_Type (Ent)
887 and then not Is_Tagged_Type (Ent)
888 and then No (Full_View (Ent))
890 -- If this is a private type whose completion is a derivation from
891 -- another private type, there is no full view, and the attribute
892 -- belongs to the type itself, not its underlying parent.
896 elsif Ekind (Ent) = E_Incomplete_Type then
898 -- The attribute applies to the full view, set the entity of the
899 -- attribute definition accordingly.
901 Ent := Underlying_Type (Ent);
903 Set_Entity (Nam, Ent);
906 U_Ent := Underlying_Type (Ent);
909 -- Complete other routine error checks
911 if Etype (Nam) = Any_Type then
914 elsif Scope (Ent) /= Current_Scope then
915 Error_Msg_N ("entity must be declared in this scope", Nam);
918 elsif No (U_Ent) then
921 elsif Is_Type (U_Ent)
922 and then not Is_First_Subtype (U_Ent)
923 and then Id /= Attribute_Object_Size
924 and then Id /= Attribute_Value_Size
925 and then not From_At_Mod (N)
927 Error_Msg_N ("cannot specify attribute for subtype", Nam);
931 -- Switch on particular attribute
939 -- Address attribute definition clause
941 when Attribute_Address => Address : begin
943 -- A little error check, catch for X'Address use X'Address;
945 if Nkind (Nam) = N_Identifier
946 and then Nkind (Expr) = N_Attribute_Reference
947 and then Attribute_Name (Expr) = Name_Address
948 and then Nkind (Prefix (Expr)) = N_Identifier
949 and then Chars (Nam) = Chars (Prefix (Expr))
952 ("address for & is self-referencing", Prefix (Expr), Ent);
956 -- Not that special case, carry on with analysis of expression
958 Analyze_And_Resolve (Expr, RTE (RE_Address));
960 -- Even when ignoring rep clauses we need to indicate that the
961 -- entity has an address clause and thus it is legal to declare
964 if Ignore_Rep_Clauses then
965 if Ekind_In (U_Ent, E_Variable, E_Constant) then
966 Record_Rep_Item (U_Ent, N);
972 if Present (Address_Clause (U_Ent)) then
973 Error_Msg_N ("address already given for &", Nam);
975 -- Case of address clause for subprogram
977 elsif Is_Subprogram (U_Ent) then
978 if Has_Homonym (U_Ent) then
980 ("address clause cannot be given " &
981 "for overloaded subprogram",
986 -- For subprograms, all address clauses are permitted, and we
987 -- mark the subprogram as having a deferred freeze so that Gigi
988 -- will not elaborate it too soon.
990 -- Above needs more comments, what is too soon about???
992 Set_Has_Delayed_Freeze (U_Ent);
994 -- Case of address clause for entry
996 elsif Ekind (U_Ent) = E_Entry then
997 if Nkind (Parent (N)) = N_Task_Body then
999 ("entry address must be specified in task spec", Nam);
1003 -- For entries, we require a constant address
1005 Check_Constant_Address_Clause (Expr, U_Ent);
1007 -- Special checks for task types
1009 if Is_Task_Type (Scope (U_Ent))
1010 and then Comes_From_Source (Scope (U_Ent))
1013 ("?entry address declared for entry in task type", N);
1015 ("\?only one task can be declared of this type", N);
1018 -- Entry address clauses are obsolescent
1020 Check_Restriction (No_Obsolescent_Features, N);
1022 if Warn_On_Obsolescent_Feature then
1024 ("attaching interrupt to task entry is an " &
1025 "obsolescent feature (RM J.7.1)?", N);
1027 ("\use interrupt procedure instead?", N);
1030 -- Case of an address clause for a controlled object which we
1031 -- consider to be erroneous.
1033 elsif Is_Controlled (Etype (U_Ent))
1034 or else Has_Controlled_Component (Etype (U_Ent))
1037 ("?controlled object& must not be overlaid", Nam, U_Ent);
1039 ("\?Program_Error will be raised at run time", Nam);
1040 Insert_Action (Declaration_Node (U_Ent),
1041 Make_Raise_Program_Error (Loc,
1042 Reason => PE_Overlaid_Controlled_Object));
1045 -- Case of address clause for a (non-controlled) object
1048 Ekind (U_Ent) = E_Variable
1050 Ekind (U_Ent) = E_Constant
1053 Expr : constant Node_Id := Expression (N);
1058 -- Exported variables cannot have an address clause, because
1059 -- this cancels the effect of the pragma Export.
1061 if Is_Exported (U_Ent) then
1063 ("cannot export object with address clause", Nam);
1067 Find_Overlaid_Entity (N, O_Ent, Off);
1069 -- Overlaying controlled objects is erroneous
1072 and then (Has_Controlled_Component (Etype (O_Ent))
1073 or else Is_Controlled (Etype (O_Ent)))
1076 ("?cannot overlay with controlled object", Expr);
1078 ("\?Program_Error will be raised at run time", Expr);
1079 Insert_Action (Declaration_Node (U_Ent),
1080 Make_Raise_Program_Error (Loc,
1081 Reason => PE_Overlaid_Controlled_Object));
1084 elsif Present (O_Ent)
1085 and then Ekind (U_Ent) = E_Constant
1086 and then not Is_Constant_Object (O_Ent)
1088 Error_Msg_N ("constant overlays a variable?", Expr);
1090 elsif Present (Renamed_Object (U_Ent)) then
1092 ("address clause not allowed"
1093 & " for a renaming declaration (RM 13.1(6))", Nam);
1096 -- Imported variables can have an address clause, but then
1097 -- the import is pretty meaningless except to suppress
1098 -- initializations, so we do not need such variables to
1099 -- be statically allocated (and in fact it causes trouble
1100 -- if the address clause is a local value).
1102 elsif Is_Imported (U_Ent) then
1103 Set_Is_Statically_Allocated (U_Ent, False);
1106 -- We mark a possible modification of a variable with an
1107 -- address clause, since it is likely aliasing is occurring.
1109 Note_Possible_Modification (Nam, Sure => False);
1111 -- Here we are checking for explicit overlap of one variable
1112 -- by another, and if we find this then mark the overlapped
1113 -- variable as also being volatile to prevent unwanted
1114 -- optimizations. This is a significant pessimization so
1115 -- avoid it when there is an offset, i.e. when the object
1116 -- is composite; they cannot be optimized easily anyway.
1119 and then Is_Object (O_Ent)
1122 Set_Treat_As_Volatile (O_Ent);
1125 -- Legality checks on the address clause for initialized
1126 -- objects is deferred until the freeze point, because
1127 -- a subsequent pragma might indicate that the object is
1128 -- imported and thus not initialized.
1130 Set_Has_Delayed_Freeze (U_Ent);
1132 -- If an initialization call has been generated for this
1133 -- object, it needs to be deferred to after the freeze node
1134 -- we have just now added, otherwise GIGI will see a
1135 -- reference to the variable (as actual to the IP call)
1136 -- before its definition.
1139 Init_Call : constant Node_Id := Find_Init_Call (U_Ent, N);
1141 if Present (Init_Call) then
1143 Append_Freeze_Action (U_Ent, Init_Call);
1147 if Is_Exported (U_Ent) then
1149 ("& cannot be exported if an address clause is given",
1152 ("\define and export a variable " &
1153 "that holds its address instead",
1157 -- Entity has delayed freeze, so we will generate an
1158 -- alignment check at the freeze point unless suppressed.
1160 if not Range_Checks_Suppressed (U_Ent)
1161 and then not Alignment_Checks_Suppressed (U_Ent)
1163 Set_Check_Address_Alignment (N);
1166 -- Kill the size check code, since we are not allocating
1167 -- the variable, it is somewhere else.
1169 Kill_Size_Check_Code (U_Ent);
1171 -- If the address clause is of the form:
1173 -- for Y'Address use X'Address
1177 -- Const : constant Address := X'Address;
1179 -- for Y'Address use Const;
1181 -- then we make an entry in the table for checking the size
1182 -- and alignment of the overlaying variable. We defer this
1183 -- check till after code generation to take full advantage
1184 -- of the annotation done by the back end. This entry is
1185 -- only made if the address clause comes from source.
1186 -- If the entity has a generic type, the check will be
1187 -- performed in the instance if the actual type justifies
1188 -- it, and we do not insert the clause in the table to
1189 -- prevent spurious warnings.
1191 if Address_Clause_Overlay_Warnings
1192 and then Comes_From_Source (N)
1193 and then Present (O_Ent)
1194 and then Is_Object (O_Ent)
1196 if not Is_Generic_Type (Etype (U_Ent)) then
1197 Address_Clause_Checks.Append ((N, U_Ent, O_Ent, Off));
1200 -- If variable overlays a constant view, and we are
1201 -- warning on overlays, then mark the variable as
1202 -- overlaying a constant (we will give warnings later
1203 -- if this variable is assigned).
1205 if Is_Constant_Object (O_Ent)
1206 and then Ekind (U_Ent) = E_Variable
1208 Set_Overlays_Constant (U_Ent);
1213 -- Not a valid entity for an address clause
1216 Error_Msg_N ("address cannot be given for &", Nam);
1224 -- Alignment attribute definition clause
1226 when Attribute_Alignment => Alignment : declare
1227 Align : constant Uint := Get_Alignment_Value (Expr);
1232 if not Is_Type (U_Ent)
1233 and then Ekind (U_Ent) /= E_Variable
1234 and then Ekind (U_Ent) /= E_Constant
1236 Error_Msg_N ("alignment cannot be given for &", Nam);
1238 elsif Has_Alignment_Clause (U_Ent) then
1239 Error_Msg_Sloc := Sloc (Alignment_Clause (U_Ent));
1240 Error_Msg_N ("alignment clause previously given#", N);
1242 elsif Align /= No_Uint then
1243 Set_Has_Alignment_Clause (U_Ent);
1244 Set_Alignment (U_Ent, Align);
1246 -- For an array type, U_Ent is the first subtype. In that case,
1247 -- also set the alignment of the anonymous base type so that
1248 -- other subtypes (such as the itypes for aggregates of the
1249 -- type) also receive the expected alignment.
1251 if Is_Array_Type (U_Ent) then
1252 Set_Alignment (Base_Type (U_Ent), Align);
1261 -- Bit_Order attribute definition clause
1263 when Attribute_Bit_Order => Bit_Order : declare
1265 if not Is_Record_Type (U_Ent) then
1267 ("Bit_Order can only be defined for record type", Nam);
1270 Analyze_And_Resolve (Expr, RTE (RE_Bit_Order));
1272 if Etype (Expr) = Any_Type then
1275 elsif not Is_Static_Expression (Expr) then
1276 Flag_Non_Static_Expr
1277 ("Bit_Order requires static expression!", Expr);
1280 if (Expr_Value (Expr) = 0) /= Bytes_Big_Endian then
1281 Set_Reverse_Bit_Order (U_Ent, True);
1287 --------------------
1288 -- Component_Size --
1289 --------------------
1291 -- Component_Size attribute definition clause
1293 when Attribute_Component_Size => Component_Size_Case : declare
1294 Csize : constant Uint := Static_Integer (Expr);
1298 New_Ctyp : Entity_Id;
1302 if not Is_Array_Type (U_Ent) then
1303 Error_Msg_N ("component size requires array type", Nam);
1307 Btype := Base_Type (U_Ent);
1308 Ctyp := Component_Type (Btype);
1310 if Has_Component_Size_Clause (Btype) then
1312 ("component size clause for& previously given", Nam);
1314 elsif Rep_Item_Too_Early (Btype, N) then
1317 elsif Csize /= No_Uint then
1318 Check_Size (Expr, Ctyp, Csize, Biased);
1320 -- For the biased case, build a declaration for a subtype
1321 -- that will be used to represent the biased subtype that
1322 -- reflects the biased representation of components. We need
1323 -- this subtype to get proper conversions on referencing
1324 -- elements of the array. Note that component size clauses
1325 -- are ignored in VM mode.
1327 if VM_Target = No_VM then
1330 Make_Defining_Identifier (Loc,
1332 New_External_Name (Chars (U_Ent), 'C', 0, 'T'));
1335 Make_Subtype_Declaration (Loc,
1336 Defining_Identifier => New_Ctyp,
1337 Subtype_Indication =>
1338 New_Occurrence_Of (Component_Type (Btype), Loc));
1340 Set_Parent (Decl, N);
1341 Analyze (Decl, Suppress => All_Checks);
1343 Set_Has_Delayed_Freeze (New_Ctyp, False);
1344 Set_Esize (New_Ctyp, Csize);
1345 Set_RM_Size (New_Ctyp, Csize);
1346 Init_Alignment (New_Ctyp);
1347 Set_Is_Itype (New_Ctyp, True);
1348 Set_Associated_Node_For_Itype (New_Ctyp, U_Ent);
1350 Set_Component_Type (Btype, New_Ctyp);
1351 Set_Biased (New_Ctyp, N, "component size clause");
1354 Set_Component_Size (Btype, Csize);
1356 -- For VM case, we ignore component size clauses
1359 -- Give a warning unless we are in GNAT mode, in which case
1360 -- the warning is suppressed since it is not useful.
1362 if not GNAT_Mode then
1364 ("?component size ignored in this configuration", N);
1368 -- Deal with warning on overridden size
1370 if Warn_On_Overridden_Size
1371 and then Has_Size_Clause (Ctyp)
1372 and then RM_Size (Ctyp) /= Csize
1375 ("?component size overrides size clause for&",
1379 Set_Has_Component_Size_Clause (Btype, True);
1380 Set_Has_Non_Standard_Rep (Btype, True);
1382 end Component_Size_Case;
1388 when Attribute_External_Tag => External_Tag :
1390 if not Is_Tagged_Type (U_Ent) then
1391 Error_Msg_N ("should be a tagged type", Nam);
1394 Analyze_And_Resolve (Expr, Standard_String);
1396 if not Is_Static_Expression (Expr) then
1397 Flag_Non_Static_Expr
1398 ("static string required for tag name!", Nam);
1401 if VM_Target = No_VM then
1402 Set_Has_External_Tag_Rep_Clause (U_Ent);
1404 Error_Msg_Name_1 := Attr;
1406 ("% attribute unsupported in this configuration", Nam);
1409 if not Is_Library_Level_Entity (U_Ent) then
1411 ("?non-unique external tag supplied for &", N, U_Ent);
1413 ("?\same external tag applies to all subprogram calls", N);
1415 ("?\corresponding internal tag cannot be obtained", N);
1423 when Attribute_Input =>
1424 Analyze_Stream_TSS_Definition (TSS_Stream_Input);
1425 Set_Has_Specified_Stream_Input (Ent);
1431 -- Machine radix attribute definition clause
1433 when Attribute_Machine_Radix => Machine_Radix : declare
1434 Radix : constant Uint := Static_Integer (Expr);
1437 if not Is_Decimal_Fixed_Point_Type (U_Ent) then
1438 Error_Msg_N ("decimal fixed-point type expected for &", Nam);
1440 elsif Has_Machine_Radix_Clause (U_Ent) then
1441 Error_Msg_Sloc := Sloc (Alignment_Clause (U_Ent));
1442 Error_Msg_N ("machine radix clause previously given#", N);
1444 elsif Radix /= No_Uint then
1445 Set_Has_Machine_Radix_Clause (U_Ent);
1446 Set_Has_Non_Standard_Rep (Base_Type (U_Ent));
1450 elsif Radix = 10 then
1451 Set_Machine_Radix_10 (U_Ent);
1453 Error_Msg_N ("machine radix value must be 2 or 10", Expr);
1462 -- Object_Size attribute definition clause
1464 when Attribute_Object_Size => Object_Size : declare
1465 Size : constant Uint := Static_Integer (Expr);
1468 pragma Warnings (Off, Biased);
1471 if not Is_Type (U_Ent) then
1472 Error_Msg_N ("Object_Size cannot be given for &", Nam);
1474 elsif Has_Object_Size_Clause (U_Ent) then
1475 Error_Msg_N ("Object_Size already given for &", Nam);
1478 Check_Size (Expr, U_Ent, Size, Biased);
1486 UI_Mod (Size, 64) /= 0
1489 ("Object_Size must be 8, 16, 32, or multiple of 64",
1493 Set_Esize (U_Ent, Size);
1494 Set_Has_Object_Size_Clause (U_Ent);
1495 Alignment_Check_For_Esize_Change (U_Ent);
1503 when Attribute_Output =>
1504 Analyze_Stream_TSS_Definition (TSS_Stream_Output);
1505 Set_Has_Specified_Stream_Output (Ent);
1511 when Attribute_Read =>
1512 Analyze_Stream_TSS_Definition (TSS_Stream_Read);
1513 Set_Has_Specified_Stream_Read (Ent);
1519 -- Size attribute definition clause
1521 when Attribute_Size => Size : declare
1522 Size : constant Uint := Static_Integer (Expr);
1529 if Has_Size_Clause (U_Ent) then
1530 Error_Msg_N ("size already given for &", Nam);
1532 elsif not Is_Type (U_Ent)
1533 and then Ekind (U_Ent) /= E_Variable
1534 and then Ekind (U_Ent) /= E_Constant
1536 Error_Msg_N ("size cannot be given for &", Nam);
1538 elsif Is_Array_Type (U_Ent)
1539 and then not Is_Constrained (U_Ent)
1542 ("size cannot be given for unconstrained array", Nam);
1544 elsif Size /= No_Uint then
1546 if VM_Target /= No_VM and then not GNAT_Mode then
1548 -- Size clause is not handled properly on VM targets.
1549 -- Display a warning unless we are in GNAT mode, in which
1550 -- case this is useless.
1553 ("?size clauses are ignored in this configuration", N);
1556 if Is_Type (U_Ent) then
1559 Etyp := Etype (U_Ent);
1562 -- Check size, note that Gigi is in charge of checking that the
1563 -- size of an array or record type is OK. Also we do not check
1564 -- the size in the ordinary fixed-point case, since it is too
1565 -- early to do so (there may be subsequent small clause that
1566 -- affects the size). We can check the size if a small clause
1567 -- has already been given.
1569 if not Is_Ordinary_Fixed_Point_Type (U_Ent)
1570 or else Has_Small_Clause (U_Ent)
1572 Check_Size (Expr, Etyp, Size, Biased);
1573 Set_Biased (U_Ent, N, "size clause", Biased);
1576 -- For types set RM_Size and Esize if possible
1578 if Is_Type (U_Ent) then
1579 Set_RM_Size (U_Ent, Size);
1581 -- For scalar types, increase Object_Size to power of 2, but
1582 -- not less than a storage unit in any case (i.e., normally
1583 -- this means it will be byte addressable).
1585 if Is_Scalar_Type (U_Ent) then
1586 if Size <= System_Storage_Unit then
1587 Init_Esize (U_Ent, System_Storage_Unit);
1588 elsif Size <= 16 then
1589 Init_Esize (U_Ent, 16);
1590 elsif Size <= 32 then
1591 Init_Esize (U_Ent, 32);
1593 Set_Esize (U_Ent, (Size + 63) / 64 * 64);
1596 -- For all other types, object size = value size. The
1597 -- backend will adjust as needed.
1600 Set_Esize (U_Ent, Size);
1603 Alignment_Check_For_Esize_Change (U_Ent);
1605 -- For objects, set Esize only
1608 if Is_Elementary_Type (Etyp) then
1609 if Size /= System_Storage_Unit
1611 Size /= System_Storage_Unit * 2
1613 Size /= System_Storage_Unit * 4
1615 Size /= System_Storage_Unit * 8
1617 Error_Msg_Uint_1 := UI_From_Int (System_Storage_Unit);
1618 Error_Msg_Uint_2 := Error_Msg_Uint_1 * 8;
1620 ("size for primitive object must be a power of 2"
1621 & " in the range ^-^", N);
1625 Set_Esize (U_Ent, Size);
1628 Set_Has_Size_Clause (U_Ent);
1636 -- Small attribute definition clause
1638 when Attribute_Small => Small : declare
1639 Implicit_Base : constant Entity_Id := Base_Type (U_Ent);
1643 Analyze_And_Resolve (Expr, Any_Real);
1645 if Etype (Expr) = Any_Type then
1648 elsif not Is_Static_Expression (Expr) then
1649 Flag_Non_Static_Expr
1650 ("small requires static expression!", Expr);
1654 Small := Expr_Value_R (Expr);
1656 if Small <= Ureal_0 then
1657 Error_Msg_N ("small value must be greater than zero", Expr);
1663 if not Is_Ordinary_Fixed_Point_Type (U_Ent) then
1665 ("small requires an ordinary fixed point type", Nam);
1667 elsif Has_Small_Clause (U_Ent) then
1668 Error_Msg_N ("small already given for &", Nam);
1670 elsif Small > Delta_Value (U_Ent) then
1672 ("small value must not be greater then delta value", Nam);
1675 Set_Small_Value (U_Ent, Small);
1676 Set_Small_Value (Implicit_Base, Small);
1677 Set_Has_Small_Clause (U_Ent);
1678 Set_Has_Small_Clause (Implicit_Base);
1679 Set_Has_Non_Standard_Rep (Implicit_Base);
1687 -- Storage_Pool attribute definition clause
1689 when Attribute_Storage_Pool => Storage_Pool : declare
1694 if Ekind (U_Ent) = E_Access_Subprogram_Type then
1696 ("storage pool cannot be given for access-to-subprogram type",
1701 Ekind_In (U_Ent, E_Access_Type, E_General_Access_Type)
1704 ("storage pool can only be given for access types", Nam);
1707 elsif Is_Derived_Type (U_Ent) then
1709 ("storage pool cannot be given for a derived access type",
1712 elsif Has_Storage_Size_Clause (U_Ent) then
1713 Error_Msg_N ("storage size already given for &", Nam);
1716 elsif Present (Associated_Storage_Pool (U_Ent)) then
1717 Error_Msg_N ("storage pool already given for &", Nam);
1722 (Expr, Class_Wide_Type (RTE (RE_Root_Storage_Pool)));
1724 if not Denotes_Variable (Expr) then
1725 Error_Msg_N ("storage pool must be a variable", Expr);
1729 if Nkind (Expr) = N_Type_Conversion then
1730 T := Etype (Expression (Expr));
1735 -- The Stack_Bounded_Pool is used internally for implementing
1736 -- access types with a Storage_Size. Since it only work
1737 -- properly when used on one specific type, we need to check
1738 -- that it is not hijacked improperly:
1739 -- type T is access Integer;
1740 -- for T'Storage_Size use n;
1741 -- type Q is access Float;
1742 -- for Q'Storage_Size use T'Storage_Size; -- incorrect
1744 if RTE_Available (RE_Stack_Bounded_Pool)
1745 and then Base_Type (T) = RTE (RE_Stack_Bounded_Pool)
1747 Error_Msg_N ("non-shareable internal Pool", Expr);
1751 -- If the argument is a name that is not an entity name, then
1752 -- we construct a renaming operation to define an entity of
1753 -- type storage pool.
1755 if not Is_Entity_Name (Expr)
1756 and then Is_Object_Reference (Expr)
1758 Pool := Make_Temporary (Loc, 'P', Expr);
1761 Rnode : constant Node_Id :=
1762 Make_Object_Renaming_Declaration (Loc,
1763 Defining_Identifier => Pool,
1765 New_Occurrence_Of (Etype (Expr), Loc),
1769 Insert_Before (N, Rnode);
1771 Set_Associated_Storage_Pool (U_Ent, Pool);
1774 elsif Is_Entity_Name (Expr) then
1775 Pool := Entity (Expr);
1777 -- If pool is a renamed object, get original one. This can
1778 -- happen with an explicit renaming, and within instances.
1780 while Present (Renamed_Object (Pool))
1781 and then Is_Entity_Name (Renamed_Object (Pool))
1783 Pool := Entity (Renamed_Object (Pool));
1786 if Present (Renamed_Object (Pool))
1787 and then Nkind (Renamed_Object (Pool)) = N_Type_Conversion
1788 and then Is_Entity_Name (Expression (Renamed_Object (Pool)))
1790 Pool := Entity (Expression (Renamed_Object (Pool)));
1793 Set_Associated_Storage_Pool (U_Ent, Pool);
1795 elsif Nkind (Expr) = N_Type_Conversion
1796 and then Is_Entity_Name (Expression (Expr))
1797 and then Nkind (Original_Node (Expr)) = N_Attribute_Reference
1799 Pool := Entity (Expression (Expr));
1800 Set_Associated_Storage_Pool (U_Ent, Pool);
1803 Error_Msg_N ("incorrect reference to a Storage Pool", Expr);
1812 -- Storage_Size attribute definition clause
1814 when Attribute_Storage_Size => Storage_Size : declare
1815 Btype : constant Entity_Id := Base_Type (U_Ent);
1819 if Is_Task_Type (U_Ent) then
1820 Check_Restriction (No_Obsolescent_Features, N);
1822 if Warn_On_Obsolescent_Feature then
1824 ("storage size clause for task is an " &
1825 "obsolescent feature (RM J.9)?", N);
1826 Error_Msg_N ("\use Storage_Size pragma instead?", N);
1832 if not Is_Access_Type (U_Ent)
1833 and then Ekind (U_Ent) /= E_Task_Type
1835 Error_Msg_N ("storage size cannot be given for &", Nam);
1837 elsif Is_Access_Type (U_Ent) and Is_Derived_Type (U_Ent) then
1839 ("storage size cannot be given for a derived access type",
1842 elsif Has_Storage_Size_Clause (Btype) then
1843 Error_Msg_N ("storage size already given for &", Nam);
1846 Analyze_And_Resolve (Expr, Any_Integer);
1848 if Is_Access_Type (U_Ent) then
1849 if Present (Associated_Storage_Pool (U_Ent)) then
1850 Error_Msg_N ("storage pool already given for &", Nam);
1854 if Is_OK_Static_Expression (Expr)
1855 and then Expr_Value (Expr) = 0
1857 Set_No_Pool_Assigned (Btype);
1860 else -- Is_Task_Type (U_Ent)
1861 Sprag := Get_Rep_Pragma (Btype, Name_Storage_Size);
1863 if Present (Sprag) then
1864 Error_Msg_Sloc := Sloc (Sprag);
1866 ("Storage_Size already specified#", Nam);
1871 Set_Has_Storage_Size_Clause (Btype);
1879 when Attribute_Stream_Size => Stream_Size : declare
1880 Size : constant Uint := Static_Integer (Expr);
1883 if Ada_Version <= Ada_95 then
1884 Check_Restriction (No_Implementation_Attributes, N);
1887 if Has_Stream_Size_Clause (U_Ent) then
1888 Error_Msg_N ("Stream_Size already given for &", Nam);
1890 elsif Is_Elementary_Type (U_Ent) then
1891 if Size /= System_Storage_Unit
1893 Size /= System_Storage_Unit * 2
1895 Size /= System_Storage_Unit * 4
1897 Size /= System_Storage_Unit * 8
1899 Error_Msg_Uint_1 := UI_From_Int (System_Storage_Unit);
1901 ("stream size for elementary type must be a"
1902 & " power of 2 and at least ^", N);
1904 elsif RM_Size (U_Ent) > Size then
1905 Error_Msg_Uint_1 := RM_Size (U_Ent);
1907 ("stream size for elementary type must be a"
1908 & " power of 2 and at least ^", N);
1911 Set_Has_Stream_Size_Clause (U_Ent);
1914 Error_Msg_N ("Stream_Size cannot be given for &", Nam);
1922 -- Value_Size attribute definition clause
1924 when Attribute_Value_Size => Value_Size : declare
1925 Size : constant Uint := Static_Integer (Expr);
1929 if not Is_Type (U_Ent) then
1930 Error_Msg_N ("Value_Size cannot be given for &", Nam);
1933 (Get_Attribute_Definition_Clause
1934 (U_Ent, Attribute_Value_Size))
1936 Error_Msg_N ("Value_Size already given for &", Nam);
1938 elsif Is_Array_Type (U_Ent)
1939 and then not Is_Constrained (U_Ent)
1942 ("Value_Size cannot be given for unconstrained array", Nam);
1945 if Is_Elementary_Type (U_Ent) then
1946 Check_Size (Expr, U_Ent, Size, Biased);
1947 Set_Biased (U_Ent, N, "value size clause", Biased);
1950 Set_RM_Size (U_Ent, Size);
1958 when Attribute_Write =>
1959 Analyze_Stream_TSS_Definition (TSS_Stream_Write);
1960 Set_Has_Specified_Stream_Write (Ent);
1962 -- All other attributes cannot be set
1966 ("attribute& cannot be set with definition clause", N);
1969 -- The test for the type being frozen must be performed after
1970 -- any expression the clause has been analyzed since the expression
1971 -- itself might cause freezing that makes the clause illegal.
1973 if Rep_Item_Too_Late (U_Ent, N, FOnly) then
1976 end Analyze_Attribute_Definition_Clause;
1978 ----------------------------
1979 -- Analyze_Code_Statement --
1980 ----------------------------
1982 procedure Analyze_Code_Statement (N : Node_Id) is
1983 HSS : constant Node_Id := Parent (N);
1984 SBody : constant Node_Id := Parent (HSS);
1985 Subp : constant Entity_Id := Current_Scope;
1992 -- Analyze and check we get right type, note that this implements the
1993 -- requirement (RM 13.8(1)) that Machine_Code be with'ed, since that
1994 -- is the only way that Asm_Insn could possibly be visible.
1996 Analyze_And_Resolve (Expression (N));
1998 if Etype (Expression (N)) = Any_Type then
2000 elsif Etype (Expression (N)) /= RTE (RE_Asm_Insn) then
2001 Error_Msg_N ("incorrect type for code statement", N);
2005 Check_Code_Statement (N);
2007 -- Make sure we appear in the handled statement sequence of a
2008 -- subprogram (RM 13.8(3)).
2010 if Nkind (HSS) /= N_Handled_Sequence_Of_Statements
2011 or else Nkind (SBody) /= N_Subprogram_Body
2014 ("code statement can only appear in body of subprogram", N);
2018 -- Do remaining checks (RM 13.8(3)) if not already done
2020 if not Is_Machine_Code_Subprogram (Subp) then
2021 Set_Is_Machine_Code_Subprogram (Subp);
2023 -- No exception handlers allowed
2025 if Present (Exception_Handlers (HSS)) then
2027 ("exception handlers not permitted in machine code subprogram",
2028 First (Exception_Handlers (HSS)));
2031 -- No declarations other than use clauses and pragmas (we allow
2032 -- certain internally generated declarations as well).
2034 Decl := First (Declarations (SBody));
2035 while Present (Decl) loop
2036 DeclO := Original_Node (Decl);
2037 if Comes_From_Source (DeclO)
2038 and not Nkind_In (DeclO, N_Pragma,
2039 N_Use_Package_Clause,
2041 N_Implicit_Label_Declaration)
2044 ("this declaration not allowed in machine code subprogram",
2051 -- No statements other than code statements, pragmas, and labels.
2052 -- Again we allow certain internally generated statements.
2054 Stmt := First (Statements (HSS));
2055 while Present (Stmt) loop
2056 StmtO := Original_Node (Stmt);
2057 if Comes_From_Source (StmtO)
2058 and then not Nkind_In (StmtO, N_Pragma,
2063 ("this statement is not allowed in machine code subprogram",
2070 end Analyze_Code_Statement;
2072 -----------------------------------------------
2073 -- Analyze_Enumeration_Representation_Clause --
2074 -----------------------------------------------
2076 procedure Analyze_Enumeration_Representation_Clause (N : Node_Id) is
2077 Ident : constant Node_Id := Identifier (N);
2078 Aggr : constant Node_Id := Array_Aggregate (N);
2079 Enumtype : Entity_Id;
2085 Err : Boolean := False;
2087 Lo : constant Uint := Expr_Value (Type_Low_Bound (Universal_Integer));
2088 Hi : constant Uint := Expr_Value (Type_High_Bound (Universal_Integer));
2089 -- Allowed range of universal integer (= allowed range of enum lit vals)
2093 -- Minimum and maximum values of entries
2096 -- Pointer to node for literal providing max value
2099 if Ignore_Rep_Clauses then
2103 -- First some basic error checks
2106 Enumtype := Entity (Ident);
2108 if Enumtype = Any_Type
2109 or else Rep_Item_Too_Early (Enumtype, N)
2113 Enumtype := Underlying_Type (Enumtype);
2116 if not Is_Enumeration_Type (Enumtype) then
2118 ("enumeration type required, found}",
2119 Ident, First_Subtype (Enumtype));
2123 -- Ignore rep clause on generic actual type. This will already have
2124 -- been flagged on the template as an error, and this is the safest
2125 -- way to ensure we don't get a junk cascaded message in the instance.
2127 if Is_Generic_Actual_Type (Enumtype) then
2130 -- Type must be in current scope
2132 elsif Scope (Enumtype) /= Current_Scope then
2133 Error_Msg_N ("type must be declared in this scope", Ident);
2136 -- Type must be a first subtype
2138 elsif not Is_First_Subtype (Enumtype) then
2139 Error_Msg_N ("cannot give enumeration rep clause for subtype", N);
2142 -- Ignore duplicate rep clause
2144 elsif Has_Enumeration_Rep_Clause (Enumtype) then
2145 Error_Msg_N ("duplicate enumeration rep clause ignored", N);
2148 -- Don't allow rep clause for standard [wide_[wide_]]character
2150 elsif Is_Standard_Character_Type (Enumtype) then
2151 Error_Msg_N ("enumeration rep clause not allowed for this type", N);
2154 -- Check that the expression is a proper aggregate (no parentheses)
2156 elsif Paren_Count (Aggr) /= 0 then
2158 ("extra parentheses surrounding aggregate not allowed",
2162 -- All tests passed, so set rep clause in place
2165 Set_Has_Enumeration_Rep_Clause (Enumtype);
2166 Set_Has_Enumeration_Rep_Clause (Base_Type (Enumtype));
2169 -- Now we process the aggregate. Note that we don't use the normal
2170 -- aggregate code for this purpose, because we don't want any of the
2171 -- normal expansion activities, and a number of special semantic
2172 -- rules apply (including the component type being any integer type)
2174 Elit := First_Literal (Enumtype);
2176 -- First the positional entries if any
2178 if Present (Expressions (Aggr)) then
2179 Expr := First (Expressions (Aggr));
2180 while Present (Expr) loop
2182 Error_Msg_N ("too many entries in aggregate", Expr);
2186 Val := Static_Integer (Expr);
2188 -- Err signals that we found some incorrect entries processing
2189 -- the list. The final checks for completeness and ordering are
2190 -- skipped in this case.
2192 if Val = No_Uint then
2194 elsif Val < Lo or else Hi < Val then
2195 Error_Msg_N ("value outside permitted range", Expr);
2199 Set_Enumeration_Rep (Elit, Val);
2200 Set_Enumeration_Rep_Expr (Elit, Expr);
2206 -- Now process the named entries if present
2208 if Present (Component_Associations (Aggr)) then
2209 Assoc := First (Component_Associations (Aggr));
2210 while Present (Assoc) loop
2211 Choice := First (Choices (Assoc));
2213 if Present (Next (Choice)) then
2215 ("multiple choice not allowed here", Next (Choice));
2219 if Nkind (Choice) = N_Others_Choice then
2220 Error_Msg_N ("others choice not allowed here", Choice);
2223 elsif Nkind (Choice) = N_Range then
2224 -- ??? should allow zero/one element range here
2225 Error_Msg_N ("range not allowed here", Choice);
2229 Analyze_And_Resolve (Choice, Enumtype);
2231 if Is_Entity_Name (Choice)
2232 and then Is_Type (Entity (Choice))
2234 Error_Msg_N ("subtype name not allowed here", Choice);
2236 -- ??? should allow static subtype with zero/one entry
2238 elsif Etype (Choice) = Base_Type (Enumtype) then
2239 if not Is_Static_Expression (Choice) then
2240 Flag_Non_Static_Expr
2241 ("non-static expression used for choice!", Choice);
2245 Elit := Expr_Value_E (Choice);
2247 if Present (Enumeration_Rep_Expr (Elit)) then
2248 Error_Msg_Sloc := Sloc (Enumeration_Rep_Expr (Elit));
2250 ("representation for& previously given#",
2255 Set_Enumeration_Rep_Expr (Elit, Expression (Assoc));
2257 Expr := Expression (Assoc);
2258 Val := Static_Integer (Expr);
2260 if Val = No_Uint then
2263 elsif Val < Lo or else Hi < Val then
2264 Error_Msg_N ("value outside permitted range", Expr);
2268 Set_Enumeration_Rep (Elit, Val);
2277 -- Aggregate is fully processed. Now we check that a full set of
2278 -- representations was given, and that they are in range and in order.
2279 -- These checks are only done if no other errors occurred.
2285 Elit := First_Literal (Enumtype);
2286 while Present (Elit) loop
2287 if No (Enumeration_Rep_Expr (Elit)) then
2288 Error_Msg_NE ("missing representation for&!", N, Elit);
2291 Val := Enumeration_Rep (Elit);
2293 if Min = No_Uint then
2297 if Val /= No_Uint then
2298 if Max /= No_Uint and then Val <= Max then
2300 ("enumeration value for& not ordered!",
2301 Enumeration_Rep_Expr (Elit), Elit);
2304 Max_Node := Enumeration_Rep_Expr (Elit);
2308 -- If there is at least one literal whose representation is not
2309 -- equal to the Pos value, then note that this enumeration type
2310 -- has a non-standard representation.
2312 if Val /= Enumeration_Pos (Elit) then
2313 Set_Has_Non_Standard_Rep (Base_Type (Enumtype));
2320 -- Now set proper size information
2323 Minsize : Uint := UI_From_Int (Minimum_Size (Enumtype));
2326 if Has_Size_Clause (Enumtype) then
2328 -- All OK, if size is OK now
2330 if RM_Size (Enumtype) >= Minsize then
2334 -- Try if we can get by with biasing
2337 UI_From_Int (Minimum_Size (Enumtype, Biased => True));
2339 -- Error message if even biasing does not work
2341 if RM_Size (Enumtype) < Minsize then
2342 Error_Msg_Uint_1 := RM_Size (Enumtype);
2343 Error_Msg_Uint_2 := Max;
2345 ("previously given size (^) is too small "
2346 & "for this value (^)", Max_Node);
2348 -- If biasing worked, indicate that we now have biased rep
2352 (Enumtype, Size_Clause (Enumtype), "size clause");
2357 Set_RM_Size (Enumtype, Minsize);
2358 Set_Enum_Esize (Enumtype);
2361 Set_RM_Size (Base_Type (Enumtype), RM_Size (Enumtype));
2362 Set_Esize (Base_Type (Enumtype), Esize (Enumtype));
2363 Set_Alignment (Base_Type (Enumtype), Alignment (Enumtype));
2367 -- We repeat the too late test in case it froze itself!
2369 if Rep_Item_Too_Late (Enumtype, N) then
2372 end Analyze_Enumeration_Representation_Clause;
2374 ----------------------------
2375 -- Analyze_Free_Statement --
2376 ----------------------------
2378 procedure Analyze_Free_Statement (N : Node_Id) is
2380 Analyze (Expression (N));
2381 end Analyze_Free_Statement;
2383 ---------------------------
2384 -- Analyze_Freeze_Entity --
2385 ---------------------------
2387 procedure Analyze_Freeze_Entity (N : Node_Id) is
2388 E : constant Entity_Id := Entity (N);
2391 -- Remember that we are processing a freezing entity. Required to
2392 -- ensure correct decoration of internal entities associated with
2393 -- interfaces (see New_Overloaded_Entity).
2395 Inside_Freezing_Actions := Inside_Freezing_Actions + 1;
2397 -- For tagged types covering interfaces add internal entities that link
2398 -- the primitives of the interfaces with the primitives that cover them.
2399 -- Note: These entities were originally generated only when generating
2400 -- code because their main purpose was to provide support to initialize
2401 -- the secondary dispatch tables. They are now generated also when
2402 -- compiling with no code generation to provide ASIS the relationship
2403 -- between interface primitives and tagged type primitives. They are
2404 -- also used to locate primitives covering interfaces when processing
2405 -- generics (see Derive_Subprograms).
2407 if Ada_Version >= Ada_2005
2408 and then Ekind (E) = E_Record_Type
2409 and then Is_Tagged_Type (E)
2410 and then not Is_Interface (E)
2411 and then Has_Interfaces (E)
2413 -- This would be a good common place to call the routine that checks
2414 -- overriding of interface primitives (and thus factorize calls to
2415 -- Check_Abstract_Overriding located at different contexts in the
2416 -- compiler). However, this is not possible because it causes
2417 -- spurious errors in case of late overriding.
2419 Add_Internal_Interface_Entities (E);
2424 if Ekind (E) = E_Record_Type
2425 and then Is_CPP_Class (E)
2426 and then Is_Tagged_Type (E)
2427 and then Tagged_Type_Expansion
2428 and then Expander_Active
2430 if CPP_Num_Prims (E) = 0 then
2432 -- If the CPP type has user defined components then it must import
2433 -- primitives from C++. This is required because if the C++ class
2434 -- has no primitives then the C++ compiler does not added the _tag
2435 -- component to the type.
2437 pragma Assert (Chars (First_Entity (E)) = Name_uTag);
2439 if First_Entity (E) /= Last_Entity (E) then
2441 ("?'C'P'P type must import at least one primitive from C++",
2446 -- Check that all its primitives are abstract or imported from C++.
2447 -- Check also availability of the C++ constructor.
2450 Has_Constructors : constant Boolean := Has_CPP_Constructors (E);
2452 Error_Reported : Boolean := False;
2456 Elmt := First_Elmt (Primitive_Operations (E));
2457 while Present (Elmt) loop
2458 Prim := Node (Elmt);
2460 if Comes_From_Source (Prim) then
2461 if Is_Abstract_Subprogram (Prim) then
2464 elsif not Is_Imported (Prim)
2465 or else Convention (Prim) /= Convention_CPP
2468 ("?primitives of 'C'P'P types must be imported from C++"
2469 & " or abstract", Prim);
2471 elsif not Has_Constructors
2472 and then not Error_Reported
2474 Error_Msg_Name_1 := Chars (E);
2476 ("?'C'P'P constructor required for type %", Prim);
2477 Error_Reported := True;
2486 Inside_Freezing_Actions := Inside_Freezing_Actions - 1;
2487 end Analyze_Freeze_Entity;
2489 ------------------------------------------
2490 -- Analyze_Record_Representation_Clause --
2491 ------------------------------------------
2493 -- Note: we check as much as we can here, but we can't do any checks
2494 -- based on the position values (e.g. overlap checks) until freeze time
2495 -- because especially in Ada 2005 (machine scalar mode), the processing
2496 -- for non-standard bit order can substantially change the positions.
2497 -- See procedure Check_Record_Representation_Clause (called from Freeze)
2498 -- for the remainder of this processing.
2500 procedure Analyze_Record_Representation_Clause (N : Node_Id) is
2501 Ident : constant Node_Id := Identifier (N);
2506 Hbit : Uint := Uint_0;
2510 Rectype : Entity_Id;
2512 CR_Pragma : Node_Id := Empty;
2513 -- Points to N_Pragma node if Complete_Representation pragma present
2516 if Ignore_Rep_Clauses then
2521 Rectype := Entity (Ident);
2523 if Rectype = Any_Type
2524 or else Rep_Item_Too_Early (Rectype, N)
2528 Rectype := Underlying_Type (Rectype);
2531 -- First some basic error checks
2533 if not Is_Record_Type (Rectype) then
2535 ("record type required, found}", Ident, First_Subtype (Rectype));
2538 elsif Scope (Rectype) /= Current_Scope then
2539 Error_Msg_N ("type must be declared in this scope", N);
2542 elsif not Is_First_Subtype (Rectype) then
2543 Error_Msg_N ("cannot give record rep clause for subtype", N);
2546 elsif Has_Record_Rep_Clause (Rectype) then
2547 Error_Msg_N ("duplicate record rep clause ignored", N);
2550 elsif Rep_Item_Too_Late (Rectype, N) then
2554 if Present (Mod_Clause (N)) then
2556 Loc : constant Source_Ptr := Sloc (N);
2557 M : constant Node_Id := Mod_Clause (N);
2558 P : constant List_Id := Pragmas_Before (M);
2562 pragma Warnings (Off, Mod_Val);
2565 Check_Restriction (No_Obsolescent_Features, Mod_Clause (N));
2567 if Warn_On_Obsolescent_Feature then
2569 ("mod clause is an obsolescent feature (RM J.8)?", N);
2571 ("\use alignment attribute definition clause instead?", N);
2578 -- In ASIS_Mode mode, expansion is disabled, but we must convert
2579 -- the Mod clause into an alignment clause anyway, so that the
2580 -- back-end can compute and back-annotate properly the size and
2581 -- alignment of types that may include this record.
2583 -- This seems dubious, this destroys the source tree in a manner
2584 -- not detectable by ASIS ???
2586 if Operating_Mode = Check_Semantics
2590 Make_Attribute_Definition_Clause (Loc,
2591 Name => New_Reference_To (Base_Type (Rectype), Loc),
2592 Chars => Name_Alignment,
2593 Expression => Relocate_Node (Expression (M)));
2595 Set_From_At_Mod (AtM_Nod);
2596 Insert_After (N, AtM_Nod);
2597 Mod_Val := Get_Alignment_Value (Expression (AtM_Nod));
2598 Set_Mod_Clause (N, Empty);
2601 -- Get the alignment value to perform error checking
2603 Mod_Val := Get_Alignment_Value (Expression (M));
2608 -- For untagged types, clear any existing component clauses for the
2609 -- type. If the type is derived, this is what allows us to override
2610 -- a rep clause for the parent. For type extensions, the representation
2611 -- of the inherited components is inherited, so we want to keep previous
2612 -- component clauses for completeness.
2614 if not Is_Tagged_Type (Rectype) then
2615 Comp := First_Component_Or_Discriminant (Rectype);
2616 while Present (Comp) loop
2617 Set_Component_Clause (Comp, Empty);
2618 Next_Component_Or_Discriminant (Comp);
2622 -- All done if no component clauses
2624 CC := First (Component_Clauses (N));
2630 -- A representation like this applies to the base type
2632 Set_Has_Record_Rep_Clause (Base_Type (Rectype));
2633 Set_Has_Non_Standard_Rep (Base_Type (Rectype));
2634 Set_Has_Specified_Layout (Base_Type (Rectype));
2636 -- Process the component clauses
2638 while Present (CC) loop
2642 if Nkind (CC) = N_Pragma then
2645 -- The only pragma of interest is Complete_Representation
2647 if Pragma_Name (CC) = Name_Complete_Representation then
2651 -- Processing for real component clause
2654 Posit := Static_Integer (Position (CC));
2655 Fbit := Static_Integer (First_Bit (CC));
2656 Lbit := Static_Integer (Last_Bit (CC));
2659 and then Fbit /= No_Uint
2660 and then Lbit /= No_Uint
2664 ("position cannot be negative", Position (CC));
2668 ("first bit cannot be negative", First_Bit (CC));
2670 -- The Last_Bit specified in a component clause must not be
2671 -- less than the First_Bit minus one (RM-13.5.1(10)).
2673 elsif Lbit < Fbit - 1 then
2675 ("last bit cannot be less than first bit minus one",
2678 -- Values look OK, so find the corresponding record component
2679 -- Even though the syntax allows an attribute reference for
2680 -- implementation-defined components, GNAT does not allow the
2681 -- tag to get an explicit position.
2683 elsif Nkind (Component_Name (CC)) = N_Attribute_Reference then
2684 if Attribute_Name (Component_Name (CC)) = Name_Tag then
2685 Error_Msg_N ("position of tag cannot be specified", CC);
2687 Error_Msg_N ("illegal component name", CC);
2691 Comp := First_Entity (Rectype);
2692 while Present (Comp) loop
2693 exit when Chars (Comp) = Chars (Component_Name (CC));
2699 -- Maybe component of base type that is absent from
2700 -- statically constrained first subtype.
2702 Comp := First_Entity (Base_Type (Rectype));
2703 while Present (Comp) loop
2704 exit when Chars (Comp) = Chars (Component_Name (CC));
2711 ("component clause is for non-existent field", CC);
2713 -- Ada 2012 (AI05-0026): Any name that denotes a
2714 -- discriminant of an object of an unchecked union type
2715 -- shall not occur within a record_representation_clause.
2717 -- The general restriction of using record rep clauses on
2718 -- Unchecked_Union types has now been lifted. Since it is
2719 -- possible to introduce a record rep clause which mentions
2720 -- the discriminant of an Unchecked_Union in non-Ada 2012
2721 -- code, this check is applied to all versions of the
2724 elsif Ekind (Comp) = E_Discriminant
2725 and then Is_Unchecked_Union (Rectype)
2728 ("cannot reference discriminant of Unchecked_Union",
2729 Component_Name (CC));
2731 elsif Present (Component_Clause (Comp)) then
2733 -- Diagnose duplicate rep clause, or check consistency
2734 -- if this is an inherited component. In a double fault,
2735 -- there may be a duplicate inconsistent clause for an
2736 -- inherited component.
2738 if Scope (Original_Record_Component (Comp)) = Rectype
2739 or else Parent (Component_Clause (Comp)) = N
2741 Error_Msg_Sloc := Sloc (Component_Clause (Comp));
2742 Error_Msg_N ("component clause previously given#", CC);
2746 Rep1 : constant Node_Id := Component_Clause (Comp);
2748 if Intval (Position (Rep1)) /=
2749 Intval (Position (CC))
2750 or else Intval (First_Bit (Rep1)) /=
2751 Intval (First_Bit (CC))
2752 or else Intval (Last_Bit (Rep1)) /=
2753 Intval (Last_Bit (CC))
2755 Error_Msg_N ("component clause inconsistent "
2756 & "with representation of ancestor", CC);
2757 elsif Warn_On_Redundant_Constructs then
2758 Error_Msg_N ("?redundant component clause "
2759 & "for inherited component!", CC);
2764 -- Normal case where this is the first component clause we
2765 -- have seen for this entity, so set it up properly.
2768 -- Make reference for field in record rep clause and set
2769 -- appropriate entity field in the field identifier.
2772 (Comp, Component_Name (CC), Set_Ref => False);
2773 Set_Entity (Component_Name (CC), Comp);
2775 -- Update Fbit and Lbit to the actual bit number
2777 Fbit := Fbit + UI_From_Int (SSU) * Posit;
2778 Lbit := Lbit + UI_From_Int (SSU) * Posit;
2780 if Has_Size_Clause (Rectype)
2781 and then Esize (Rectype) <= Lbit
2784 ("bit number out of range of specified size",
2787 Set_Component_Clause (Comp, CC);
2788 Set_Component_Bit_Offset (Comp, Fbit);
2789 Set_Esize (Comp, 1 + (Lbit - Fbit));
2790 Set_Normalized_First_Bit (Comp, Fbit mod SSU);
2791 Set_Normalized_Position (Comp, Fbit / SSU);
2793 if Warn_On_Overridden_Size
2794 and then Has_Size_Clause (Etype (Comp))
2795 and then RM_Size (Etype (Comp)) /= Esize (Comp)
2798 ("?component size overrides size clause for&",
2799 Component_Name (CC), Etype (Comp));
2802 -- This information is also set in the corresponding
2803 -- component of the base type, found by accessing the
2804 -- Original_Record_Component link if it is present.
2806 Ocomp := Original_Record_Component (Comp);
2813 (Component_Name (CC),
2819 (Comp, First_Node (CC), "component clause", Biased);
2821 if Present (Ocomp) then
2822 Set_Component_Clause (Ocomp, CC);
2823 Set_Component_Bit_Offset (Ocomp, Fbit);
2824 Set_Normalized_First_Bit (Ocomp, Fbit mod SSU);
2825 Set_Normalized_Position (Ocomp, Fbit / SSU);
2826 Set_Esize (Ocomp, 1 + (Lbit - Fbit));
2828 Set_Normalized_Position_Max
2829 (Ocomp, Normalized_Position (Ocomp));
2831 -- Note: we don't use Set_Biased here, because we
2832 -- already gave a warning above if needed, and we
2833 -- would get a duplicate for the same name here.
2835 Set_Has_Biased_Representation
2836 (Ocomp, Has_Biased_Representation (Comp));
2839 if Esize (Comp) < 0 then
2840 Error_Msg_N ("component size is negative", CC);
2851 -- Check missing components if Complete_Representation pragma appeared
2853 if Present (CR_Pragma) then
2854 Comp := First_Component_Or_Discriminant (Rectype);
2855 while Present (Comp) loop
2856 if No (Component_Clause (Comp)) then
2858 ("missing component clause for &", CR_Pragma, Comp);
2861 Next_Component_Or_Discriminant (Comp);
2864 -- If no Complete_Representation pragma, warn if missing components
2866 elsif Warn_On_Unrepped_Components then
2868 Num_Repped_Components : Nat := 0;
2869 Num_Unrepped_Components : Nat := 0;
2872 -- First count number of repped and unrepped components
2874 Comp := First_Component_Or_Discriminant (Rectype);
2875 while Present (Comp) loop
2876 if Present (Component_Clause (Comp)) then
2877 Num_Repped_Components := Num_Repped_Components + 1;
2879 Num_Unrepped_Components := Num_Unrepped_Components + 1;
2882 Next_Component_Or_Discriminant (Comp);
2885 -- We are only interested in the case where there is at least one
2886 -- unrepped component, and at least half the components have rep
2887 -- clauses. We figure that if less than half have them, then the
2888 -- partial rep clause is really intentional. If the component
2889 -- type has no underlying type set at this point (as for a generic
2890 -- formal type), we don't know enough to give a warning on the
2893 if Num_Unrepped_Components > 0
2894 and then Num_Unrepped_Components < Num_Repped_Components
2896 Comp := First_Component_Or_Discriminant (Rectype);
2897 while Present (Comp) loop
2898 if No (Component_Clause (Comp))
2899 and then Comes_From_Source (Comp)
2900 and then Present (Underlying_Type (Etype (Comp)))
2901 and then (Is_Scalar_Type (Underlying_Type (Etype (Comp)))
2902 or else Size_Known_At_Compile_Time
2903 (Underlying_Type (Etype (Comp))))
2904 and then not Has_Warnings_Off (Rectype)
2906 Error_Msg_Sloc := Sloc (Comp);
2908 ("?no component clause given for & declared #",
2912 Next_Component_Or_Discriminant (Comp);
2917 end Analyze_Record_Representation_Clause;
2919 -----------------------------------
2920 -- Check_Constant_Address_Clause --
2921 -----------------------------------
2923 procedure Check_Constant_Address_Clause
2927 procedure Check_At_Constant_Address (Nod : Node_Id);
2928 -- Checks that the given node N represents a name whose 'Address is
2929 -- constant (in the same sense as OK_Constant_Address_Clause, i.e. the
2930 -- address value is the same at the point of declaration of U_Ent and at
2931 -- the time of elaboration of the address clause.
2933 procedure Check_Expr_Constants (Nod : Node_Id);
2934 -- Checks that Nod meets the requirements for a constant address clause
2935 -- in the sense of the enclosing procedure.
2937 procedure Check_List_Constants (Lst : List_Id);
2938 -- Check that all elements of list Lst meet the requirements for a
2939 -- constant address clause in the sense of the enclosing procedure.
2941 -------------------------------
2942 -- Check_At_Constant_Address --
2943 -------------------------------
2945 procedure Check_At_Constant_Address (Nod : Node_Id) is
2947 if Is_Entity_Name (Nod) then
2948 if Present (Address_Clause (Entity ((Nod)))) then
2950 ("invalid address clause for initialized object &!",
2953 ("address for& cannot" &
2954 " depend on another address clause! (RM 13.1(22))!",
2957 elsif In_Same_Source_Unit (Entity (Nod), U_Ent)
2958 and then Sloc (U_Ent) < Sloc (Entity (Nod))
2961 ("invalid address clause for initialized object &!",
2963 Error_Msg_Node_2 := U_Ent;
2965 ("\& must be defined before & (RM 13.1(22))!",
2969 elsif Nkind (Nod) = N_Selected_Component then
2971 T : constant Entity_Id := Etype (Prefix (Nod));
2974 if (Is_Record_Type (T)
2975 and then Has_Discriminants (T))
2978 and then Is_Record_Type (Designated_Type (T))
2979 and then Has_Discriminants (Designated_Type (T)))
2982 ("invalid address clause for initialized object &!",
2985 ("\address cannot depend on component" &
2986 " of discriminated record (RM 13.1(22))!",
2989 Check_At_Constant_Address (Prefix (Nod));
2993 elsif Nkind (Nod) = N_Indexed_Component then
2994 Check_At_Constant_Address (Prefix (Nod));
2995 Check_List_Constants (Expressions (Nod));
2998 Check_Expr_Constants (Nod);
3000 end Check_At_Constant_Address;
3002 --------------------------
3003 -- Check_Expr_Constants --
3004 --------------------------
3006 procedure Check_Expr_Constants (Nod : Node_Id) is
3007 Loc_U_Ent : constant Source_Ptr := Sloc (U_Ent);
3008 Ent : Entity_Id := Empty;
3011 if Nkind (Nod) in N_Has_Etype
3012 and then Etype (Nod) = Any_Type
3018 when N_Empty | N_Error =>
3021 when N_Identifier | N_Expanded_Name =>
3022 Ent := Entity (Nod);
3024 -- We need to look at the original node if it is different
3025 -- from the node, since we may have rewritten things and
3026 -- substituted an identifier representing the rewrite.
3028 if Original_Node (Nod) /= Nod then
3029 Check_Expr_Constants (Original_Node (Nod));
3031 -- If the node is an object declaration without initial
3032 -- value, some code has been expanded, and the expression
3033 -- is not constant, even if the constituents might be
3034 -- acceptable, as in A'Address + offset.
3036 if Ekind (Ent) = E_Variable
3038 Nkind (Declaration_Node (Ent)) = N_Object_Declaration
3040 No (Expression (Declaration_Node (Ent)))
3043 ("invalid address clause for initialized object &!",
3046 -- If entity is constant, it may be the result of expanding
3047 -- a check. We must verify that its declaration appears
3048 -- before the object in question, else we also reject the
3051 elsif Ekind (Ent) = E_Constant
3052 and then In_Same_Source_Unit (Ent, U_Ent)
3053 and then Sloc (Ent) > Loc_U_Ent
3056 ("invalid address clause for initialized object &!",
3063 -- Otherwise look at the identifier and see if it is OK
3065 if Ekind_In (Ent, E_Named_Integer, E_Named_Real)
3066 or else Is_Type (Ent)
3071 Ekind (Ent) = E_Constant
3073 Ekind (Ent) = E_In_Parameter
3075 -- This is the case where we must have Ent defined before
3076 -- U_Ent. Clearly if they are in different units this
3077 -- requirement is met since the unit containing Ent is
3078 -- already processed.
3080 if not In_Same_Source_Unit (Ent, U_Ent) then
3083 -- Otherwise location of Ent must be before the location
3084 -- of U_Ent, that's what prior defined means.
3086 elsif Sloc (Ent) < Loc_U_Ent then
3091 ("invalid address clause for initialized object &!",
3093 Error_Msg_Node_2 := U_Ent;
3095 ("\& must be defined before & (RM 13.1(22))!",
3099 elsif Nkind (Original_Node (Nod)) = N_Function_Call then
3100 Check_Expr_Constants (Original_Node (Nod));
3104 ("invalid address clause for initialized object &!",
3107 if Comes_From_Source (Ent) then
3109 ("\reference to variable& not allowed"
3110 & " (RM 13.1(22))!", Nod, Ent);
3113 ("non-static expression not allowed"
3114 & " (RM 13.1(22))!", Nod);
3118 when N_Integer_Literal =>
3120 -- If this is a rewritten unchecked conversion, in a system
3121 -- where Address is an integer type, always use the base type
3122 -- for a literal value. This is user-friendly and prevents
3123 -- order-of-elaboration issues with instances of unchecked
3126 if Nkind (Original_Node (Nod)) = N_Function_Call then
3127 Set_Etype (Nod, Base_Type (Etype (Nod)));
3130 when N_Real_Literal |
3132 N_Character_Literal =>
3136 Check_Expr_Constants (Low_Bound (Nod));
3137 Check_Expr_Constants (High_Bound (Nod));
3139 when N_Explicit_Dereference =>
3140 Check_Expr_Constants (Prefix (Nod));
3142 when N_Indexed_Component =>
3143 Check_Expr_Constants (Prefix (Nod));
3144 Check_List_Constants (Expressions (Nod));
3147 Check_Expr_Constants (Prefix (Nod));
3148 Check_Expr_Constants (Discrete_Range (Nod));
3150 when N_Selected_Component =>
3151 Check_Expr_Constants (Prefix (Nod));
3153 when N_Attribute_Reference =>
3154 if Attribute_Name (Nod) = Name_Address
3156 Attribute_Name (Nod) = Name_Access
3158 Attribute_Name (Nod) = Name_Unchecked_Access
3160 Attribute_Name (Nod) = Name_Unrestricted_Access
3162 Check_At_Constant_Address (Prefix (Nod));
3165 Check_Expr_Constants (Prefix (Nod));
3166 Check_List_Constants (Expressions (Nod));
3170 Check_List_Constants (Component_Associations (Nod));
3171 Check_List_Constants (Expressions (Nod));
3173 when N_Component_Association =>
3174 Check_Expr_Constants (Expression (Nod));
3176 when N_Extension_Aggregate =>
3177 Check_Expr_Constants (Ancestor_Part (Nod));
3178 Check_List_Constants (Component_Associations (Nod));
3179 Check_List_Constants (Expressions (Nod));
3184 when N_Binary_Op | N_Short_Circuit | N_Membership_Test =>
3185 Check_Expr_Constants (Left_Opnd (Nod));
3186 Check_Expr_Constants (Right_Opnd (Nod));
3189 Check_Expr_Constants (Right_Opnd (Nod));
3191 when N_Type_Conversion |
3192 N_Qualified_Expression |
3194 Check_Expr_Constants (Expression (Nod));
3196 when N_Unchecked_Type_Conversion =>
3197 Check_Expr_Constants (Expression (Nod));
3199 -- If this is a rewritten unchecked conversion, subtypes in
3200 -- this node are those created within the instance. To avoid
3201 -- order of elaboration issues, replace them with their base
3202 -- types. Note that address clauses can cause order of
3203 -- elaboration problems because they are elaborated by the
3204 -- back-end at the point of definition, and may mention
3205 -- entities declared in between (as long as everything is
3206 -- static). It is user-friendly to allow unchecked conversions
3209 if Nkind (Original_Node (Nod)) = N_Function_Call then
3210 Set_Etype (Expression (Nod),
3211 Base_Type (Etype (Expression (Nod))));
3212 Set_Etype (Nod, Base_Type (Etype (Nod)));
3215 when N_Function_Call =>
3216 if not Is_Pure (Entity (Name (Nod))) then
3218 ("invalid address clause for initialized object &!",
3222 ("\function & is not pure (RM 13.1(22))!",
3223 Nod, Entity (Name (Nod)));
3226 Check_List_Constants (Parameter_Associations (Nod));
3229 when N_Parameter_Association =>
3230 Check_Expr_Constants (Explicit_Actual_Parameter (Nod));
3234 ("invalid address clause for initialized object &!",
3237 ("\must be constant defined before& (RM 13.1(22))!",
3240 end Check_Expr_Constants;
3242 --------------------------
3243 -- Check_List_Constants --
3244 --------------------------
3246 procedure Check_List_Constants (Lst : List_Id) is
3250 if Present (Lst) then
3251 Nod1 := First (Lst);
3252 while Present (Nod1) loop
3253 Check_Expr_Constants (Nod1);
3257 end Check_List_Constants;
3259 -- Start of processing for Check_Constant_Address_Clause
3262 -- If rep_clauses are to be ignored, no need for legality checks. In
3263 -- particular, no need to pester user about rep clauses that violate
3264 -- the rule on constant addresses, given that these clauses will be
3265 -- removed by Freeze before they reach the back end.
3267 if not Ignore_Rep_Clauses then
3268 Check_Expr_Constants (Expr);
3270 end Check_Constant_Address_Clause;
3272 ----------------------------------------
3273 -- Check_Record_Representation_Clause --
3274 ----------------------------------------
3276 procedure Check_Record_Representation_Clause (N : Node_Id) is
3277 Loc : constant Source_Ptr := Sloc (N);
3278 Ident : constant Node_Id := Identifier (N);
3279 Rectype : Entity_Id;
3284 Hbit : Uint := Uint_0;
3288 Max_Bit_So_Far : Uint;
3289 -- Records the maximum bit position so far. If all field positions
3290 -- are monotonically increasing, then we can skip the circuit for
3291 -- checking for overlap, since no overlap is possible.
3293 Tagged_Parent : Entity_Id := Empty;
3294 -- This is set in the case of a derived tagged type for which we have
3295 -- Is_Fully_Repped_Tagged_Type True (indicating that all components are
3296 -- positioned by record representation clauses). In this case we must
3297 -- check for overlap between components of this tagged type, and the
3298 -- components of its parent. Tagged_Parent will point to this parent
3299 -- type. For all other cases Tagged_Parent is left set to Empty.
3301 Parent_Last_Bit : Uint;
3302 -- Relevant only if Tagged_Parent is set, Parent_Last_Bit indicates the
3303 -- last bit position for any field in the parent type. We only need to
3304 -- check overlap for fields starting below this point.
3306 Overlap_Check_Required : Boolean;
3307 -- Used to keep track of whether or not an overlap check is required
3309 Overlap_Detected : Boolean := False;
3310 -- Set True if an overlap is detected
3312 Ccount : Natural := 0;
3313 -- Number of component clauses in record rep clause
3315 procedure Check_Component_Overlap (C1_Ent, C2_Ent : Entity_Id);
3316 -- Given two entities for record components or discriminants, checks
3317 -- if they have overlapping component clauses and issues errors if so.
3319 procedure Find_Component;
3320 -- Finds component entity corresponding to current component clause (in
3321 -- CC), and sets Comp to the entity, and Fbit/Lbit to the zero origin
3322 -- start/stop bits for the field. If there is no matching component or
3323 -- if the matching component does not have a component clause, then
3324 -- that's an error and Comp is set to Empty, but no error message is
3325 -- issued, since the message was already given. Comp is also set to
3326 -- Empty if the current "component clause" is in fact a pragma.
3328 -----------------------------
3329 -- Check_Component_Overlap --
3330 -----------------------------
3332 procedure Check_Component_Overlap (C1_Ent, C2_Ent : Entity_Id) is
3333 CC1 : constant Node_Id := Component_Clause (C1_Ent);
3334 CC2 : constant Node_Id := Component_Clause (C2_Ent);
3337 if Present (CC1) and then Present (CC2) then
3339 -- Exclude odd case where we have two tag fields in the same
3340 -- record, both at location zero. This seems a bit strange, but
3341 -- it seems to happen in some circumstances, perhaps on an error.
3343 if Chars (C1_Ent) = Name_uTag
3345 Chars (C2_Ent) = Name_uTag
3350 -- Here we check if the two fields overlap
3353 S1 : constant Uint := Component_Bit_Offset (C1_Ent);
3354 S2 : constant Uint := Component_Bit_Offset (C2_Ent);
3355 E1 : constant Uint := S1 + Esize (C1_Ent);
3356 E2 : constant Uint := S2 + Esize (C2_Ent);
3359 if E2 <= S1 or else E1 <= S2 then
3362 Error_Msg_Node_2 := Component_Name (CC2);
3363 Error_Msg_Sloc := Sloc (Error_Msg_Node_2);
3364 Error_Msg_Node_1 := Component_Name (CC1);
3366 ("component& overlaps & #", Component_Name (CC1));
3367 Overlap_Detected := True;
3371 end Check_Component_Overlap;
3373 --------------------
3374 -- Find_Component --
3375 --------------------
3377 procedure Find_Component is
3379 procedure Search_Component (R : Entity_Id);
3380 -- Search components of R for a match. If found, Comp is set.
3382 ----------------------
3383 -- Search_Component --
3384 ----------------------
3386 procedure Search_Component (R : Entity_Id) is
3388 Comp := First_Component_Or_Discriminant (R);
3389 while Present (Comp) loop
3391 -- Ignore error of attribute name for component name (we
3392 -- already gave an error message for this, so no need to
3395 if Nkind (Component_Name (CC)) = N_Attribute_Reference then
3398 exit when Chars (Comp) = Chars (Component_Name (CC));
3401 Next_Component_Or_Discriminant (Comp);
3403 end Search_Component;
3405 -- Start of processing for Find_Component
3408 -- Return with Comp set to Empty if we have a pragma
3410 if Nkind (CC) = N_Pragma then
3415 -- Search current record for matching component
3417 Search_Component (Rectype);
3419 -- If not found, maybe component of base type that is absent from
3420 -- statically constrained first subtype.
3423 Search_Component (Base_Type (Rectype));
3426 -- If no component, or the component does not reference the component
3427 -- clause in question, then there was some previous error for which
3428 -- we already gave a message, so just return with Comp Empty.
3431 or else Component_Clause (Comp) /= CC
3435 -- Normal case where we have a component clause
3438 Fbit := Component_Bit_Offset (Comp);
3439 Lbit := Fbit + Esize (Comp) - 1;
3443 -- Start of processing for Check_Record_Representation_Clause
3447 Rectype := Entity (Ident);
3449 if Rectype = Any_Type then
3452 Rectype := Underlying_Type (Rectype);
3455 -- See if we have a fully repped derived tagged type
3458 PS : constant Entity_Id := Parent_Subtype (Rectype);
3461 if Present (PS) and then Is_Fully_Repped_Tagged_Type (PS) then
3462 Tagged_Parent := PS;
3464 -- Find maximum bit of any component of the parent type
3466 Parent_Last_Bit := UI_From_Int (System_Address_Size - 1);
3467 Pcomp := First_Entity (Tagged_Parent);
3468 while Present (Pcomp) loop
3469 if Ekind_In (Pcomp, E_Discriminant, E_Component) then
3470 if Component_Bit_Offset (Pcomp) /= No_Uint
3471 and then Known_Static_Esize (Pcomp)
3476 Component_Bit_Offset (Pcomp) + Esize (Pcomp) - 1);
3479 Next_Entity (Pcomp);
3485 -- All done if no component clauses
3487 CC := First (Component_Clauses (N));
3493 -- If a tag is present, then create a component clause that places it
3494 -- at the start of the record (otherwise gigi may place it after other
3495 -- fields that have rep clauses).
3497 Fent := First_Entity (Rectype);
3499 if Nkind (Fent) = N_Defining_Identifier
3500 and then Chars (Fent) = Name_uTag
3502 Set_Component_Bit_Offset (Fent, Uint_0);
3503 Set_Normalized_Position (Fent, Uint_0);
3504 Set_Normalized_First_Bit (Fent, Uint_0);
3505 Set_Normalized_Position_Max (Fent, Uint_0);
3506 Init_Esize (Fent, System_Address_Size);
3508 Set_Component_Clause (Fent,
3509 Make_Component_Clause (Loc,
3511 Make_Identifier (Loc,
3512 Chars => Name_uTag),
3515 Make_Integer_Literal (Loc,
3519 Make_Integer_Literal (Loc,
3523 Make_Integer_Literal (Loc,
3524 UI_From_Int (System_Address_Size))));
3526 Ccount := Ccount + 1;
3529 Max_Bit_So_Far := Uint_Minus_1;
3530 Overlap_Check_Required := False;
3532 -- Process the component clauses
3534 while Present (CC) loop
3537 if Present (Comp) then
3538 Ccount := Ccount + 1;
3540 -- We need a full overlap check if record positions non-monotonic
3542 if Fbit <= Max_Bit_So_Far then
3543 Overlap_Check_Required := True;
3546 Max_Bit_So_Far := Lbit;
3548 -- Check bit position out of range of specified size
3550 if Has_Size_Clause (Rectype)
3551 and then Esize (Rectype) <= Lbit
3554 ("bit number out of range of specified size",
3557 -- Check for overlap with tag field
3560 if Is_Tagged_Type (Rectype)
3561 and then Fbit < System_Address_Size
3564 ("component overlaps tag field of&",
3565 Component_Name (CC), Rectype);
3566 Overlap_Detected := True;
3574 -- Check parent overlap if component might overlap parent field
3576 if Present (Tagged_Parent)
3577 and then Fbit <= Parent_Last_Bit
3579 Pcomp := First_Component_Or_Discriminant (Tagged_Parent);
3580 while Present (Pcomp) loop
3581 if not Is_Tag (Pcomp)
3582 and then Chars (Pcomp) /= Name_uParent
3584 Check_Component_Overlap (Comp, Pcomp);
3587 Next_Component_Or_Discriminant (Pcomp);
3595 -- Now that we have processed all the component clauses, check for
3596 -- overlap. We have to leave this till last, since the components can
3597 -- appear in any arbitrary order in the representation clause.
3599 -- We do not need this check if all specified ranges were monotonic,
3600 -- as recorded by Overlap_Check_Required being False at this stage.
3602 -- This first section checks if there are any overlapping entries at
3603 -- all. It does this by sorting all entries and then seeing if there are
3604 -- any overlaps. If there are none, then that is decisive, but if there
3605 -- are overlaps, they may still be OK (they may result from fields in
3606 -- different variants).
3608 if Overlap_Check_Required then
3609 Overlap_Check1 : declare
3611 OC_Fbit : array (0 .. Ccount) of Uint;
3612 -- First-bit values for component clauses, the value is the offset
3613 -- of the first bit of the field from start of record. The zero
3614 -- entry is for use in sorting.
3616 OC_Lbit : array (0 .. Ccount) of Uint;
3617 -- Last-bit values for component clauses, the value is the offset
3618 -- of the last bit of the field from start of record. The zero
3619 -- entry is for use in sorting.
3621 OC_Count : Natural := 0;
3622 -- Count of entries in OC_Fbit and OC_Lbit
3624 function OC_Lt (Op1, Op2 : Natural) return Boolean;
3625 -- Compare routine for Sort
3627 procedure OC_Move (From : Natural; To : Natural);
3628 -- Move routine for Sort
3630 package Sorting is new GNAT.Heap_Sort_G (OC_Move, OC_Lt);
3636 function OC_Lt (Op1, Op2 : Natural) return Boolean is
3638 return OC_Fbit (Op1) < OC_Fbit (Op2);
3645 procedure OC_Move (From : Natural; To : Natural) is
3647 OC_Fbit (To) := OC_Fbit (From);
3648 OC_Lbit (To) := OC_Lbit (From);
3651 -- Start of processing for Overlap_Check
3654 CC := First (Component_Clauses (N));
3655 while Present (CC) loop
3657 -- Exclude component clause already marked in error
3659 if not Error_Posted (CC) then
3662 if Present (Comp) then
3663 OC_Count := OC_Count + 1;
3664 OC_Fbit (OC_Count) := Fbit;
3665 OC_Lbit (OC_Count) := Lbit;
3672 Sorting.Sort (OC_Count);
3674 Overlap_Check_Required := False;
3675 for J in 1 .. OC_Count - 1 loop
3676 if OC_Lbit (J) >= OC_Fbit (J + 1) then
3677 Overlap_Check_Required := True;
3684 -- If Overlap_Check_Required is still True, then we have to do the full
3685 -- scale overlap check, since we have at least two fields that do
3686 -- overlap, and we need to know if that is OK since they are in
3687 -- different variant, or whether we have a definite problem.
3689 if Overlap_Check_Required then
3690 Overlap_Check2 : declare
3691 C1_Ent, C2_Ent : Entity_Id;
3692 -- Entities of components being checked for overlap
3695 -- Component_List node whose Component_Items are being checked
3698 -- Component declaration for component being checked
3701 C1_Ent := First_Entity (Base_Type (Rectype));
3703 -- Loop through all components in record. For each component check
3704 -- for overlap with any of the preceding elements on the component
3705 -- list containing the component and also, if the component is in
3706 -- a variant, check against components outside the case structure.
3707 -- This latter test is repeated recursively up the variant tree.
3709 Main_Component_Loop : while Present (C1_Ent) loop
3710 if not Ekind_In (C1_Ent, E_Component, E_Discriminant) then
3711 goto Continue_Main_Component_Loop;
3714 -- Skip overlap check if entity has no declaration node. This
3715 -- happens with discriminants in constrained derived types.
3716 -- Possibly we are missing some checks as a result, but that
3717 -- does not seem terribly serious.
3719 if No (Declaration_Node (C1_Ent)) then
3720 goto Continue_Main_Component_Loop;
3723 Clist := Parent (List_Containing (Declaration_Node (C1_Ent)));
3725 -- Loop through component lists that need checking. Check the
3726 -- current component list and all lists in variants above us.
3728 Component_List_Loop : loop
3730 -- If derived type definition, go to full declaration
3731 -- If at outer level, check discriminants if there are any.
3733 if Nkind (Clist) = N_Derived_Type_Definition then
3734 Clist := Parent (Clist);
3737 -- Outer level of record definition, check discriminants
3739 if Nkind_In (Clist, N_Full_Type_Declaration,
3740 N_Private_Type_Declaration)
3742 if Has_Discriminants (Defining_Identifier (Clist)) then
3744 First_Discriminant (Defining_Identifier (Clist));
3745 while Present (C2_Ent) loop
3746 exit when C1_Ent = C2_Ent;
3747 Check_Component_Overlap (C1_Ent, C2_Ent);
3748 Next_Discriminant (C2_Ent);
3752 -- Record extension case
3754 elsif Nkind (Clist) = N_Derived_Type_Definition then
3757 -- Otherwise check one component list
3760 Citem := First (Component_Items (Clist));
3761 while Present (Citem) loop
3762 if Nkind (Citem) = N_Component_Declaration then
3763 C2_Ent := Defining_Identifier (Citem);
3764 exit when C1_Ent = C2_Ent;
3765 Check_Component_Overlap (C1_Ent, C2_Ent);
3772 -- Check for variants above us (the parent of the Clist can
3773 -- be a variant, in which case its parent is a variant part,
3774 -- and the parent of the variant part is a component list
3775 -- whose components must all be checked against the current
3776 -- component for overlap).
3778 if Nkind (Parent (Clist)) = N_Variant then
3779 Clist := Parent (Parent (Parent (Clist)));
3781 -- Check for possible discriminant part in record, this
3782 -- is treated essentially as another level in the
3783 -- recursion. For this case the parent of the component
3784 -- list is the record definition, and its parent is the
3785 -- full type declaration containing the discriminant
3788 elsif Nkind (Parent (Clist)) = N_Record_Definition then
3789 Clist := Parent (Parent ((Clist)));
3791 -- If neither of these two cases, we are at the top of
3795 exit Component_List_Loop;
3797 end loop Component_List_Loop;
3799 <<Continue_Main_Component_Loop>>
3800 Next_Entity (C1_Ent);
3802 end loop Main_Component_Loop;
3806 -- The following circuit deals with warning on record holes (gaps). We
3807 -- skip this check if overlap was detected, since it makes sense for the
3808 -- programmer to fix this illegality before worrying about warnings.
3810 if not Overlap_Detected and Warn_On_Record_Holes then
3811 Record_Hole_Check : declare
3812 Decl : constant Node_Id := Declaration_Node (Base_Type (Rectype));
3813 -- Full declaration of record type
3815 procedure Check_Component_List
3819 -- Check component list CL for holes. The starting bit should be
3820 -- Sbit. which is zero for the main record component list and set
3821 -- appropriately for recursive calls for variants. DS is set to
3822 -- a list of discriminant specifications to be included in the
3823 -- consideration of components. It is No_List if none to consider.
3825 --------------------------
3826 -- Check_Component_List --
3827 --------------------------
3829 procedure Check_Component_List
3837 Compl := Integer (List_Length (Component_Items (CL)));
3839 if DS /= No_List then
3840 Compl := Compl + Integer (List_Length (DS));
3844 Comps : array (Natural range 0 .. Compl) of Entity_Id;
3845 -- Gather components (zero entry is for sort routine)
3847 Ncomps : Natural := 0;
3848 -- Number of entries stored in Comps (starting at Comps (1))
3851 -- One component item or discriminant specification
3854 -- Starting bit for next component
3862 function Lt (Op1, Op2 : Natural) return Boolean;
3863 -- Compare routine for Sort
3865 procedure Move (From : Natural; To : Natural);
3866 -- Move routine for Sort
3868 package Sorting is new GNAT.Heap_Sort_G (Move, Lt);
3874 function Lt (Op1, Op2 : Natural) return Boolean is
3876 return Component_Bit_Offset (Comps (Op1))
3878 Component_Bit_Offset (Comps (Op2));
3885 procedure Move (From : Natural; To : Natural) is
3887 Comps (To) := Comps (From);
3891 -- Gather discriminants into Comp
3893 if DS /= No_List then
3894 Citem := First (DS);
3895 while Present (Citem) loop
3896 if Nkind (Citem) = N_Discriminant_Specification then
3898 Ent : constant Entity_Id :=
3899 Defining_Identifier (Citem);
3901 if Ekind (Ent) = E_Discriminant then
3902 Ncomps := Ncomps + 1;
3903 Comps (Ncomps) := Ent;
3912 -- Gather component entities into Comp
3914 Citem := First (Component_Items (CL));
3915 while Present (Citem) loop
3916 if Nkind (Citem) = N_Component_Declaration then
3917 Ncomps := Ncomps + 1;
3918 Comps (Ncomps) := Defining_Identifier (Citem);
3924 -- Now sort the component entities based on the first bit.
3925 -- Note we already know there are no overlapping components.
3927 Sorting.Sort (Ncomps);
3929 -- Loop through entries checking for holes
3932 for J in 1 .. Ncomps loop
3934 Error_Msg_Uint_1 := Component_Bit_Offset (CEnt) - Nbit;
3936 if Error_Msg_Uint_1 > 0 then
3938 ("?^-bit gap before component&",
3939 Component_Name (Component_Clause (CEnt)), CEnt);
3942 Nbit := Component_Bit_Offset (CEnt) + Esize (CEnt);
3945 -- Process variant parts recursively if present
3947 if Present (Variant_Part (CL)) then
3948 Variant := First (Variants (Variant_Part (CL)));
3949 while Present (Variant) loop
3950 Check_Component_List
3951 (Component_List (Variant), Nbit, No_List);
3956 end Check_Component_List;
3958 -- Start of processing for Record_Hole_Check
3965 if Is_Tagged_Type (Rectype) then
3966 Sbit := UI_From_Int (System_Address_Size);
3971 if Nkind (Decl) = N_Full_Type_Declaration
3972 and then Nkind (Type_Definition (Decl)) = N_Record_Definition
3974 Check_Component_List
3975 (Component_List (Type_Definition (Decl)),
3977 Discriminant_Specifications (Decl));
3980 end Record_Hole_Check;
3983 -- For records that have component clauses for all components, and whose
3984 -- size is less than or equal to 32, we need to know the size in the
3985 -- front end to activate possible packed array processing where the
3986 -- component type is a record.
3988 -- At this stage Hbit + 1 represents the first unused bit from all the
3989 -- component clauses processed, so if the component clauses are
3990 -- complete, then this is the length of the record.
3992 -- For records longer than System.Storage_Unit, and for those where not
3993 -- all components have component clauses, the back end determines the
3994 -- length (it may for example be appropriate to round up the size
3995 -- to some convenient boundary, based on alignment considerations, etc).
3997 if Unknown_RM_Size (Rectype) and then Hbit + 1 <= 32 then
3999 -- Nothing to do if at least one component has no component clause
4001 Comp := First_Component_Or_Discriminant (Rectype);
4002 while Present (Comp) loop
4003 exit when No (Component_Clause (Comp));
4004 Next_Component_Or_Discriminant (Comp);
4007 -- If we fall out of loop, all components have component clauses
4008 -- and so we can set the size to the maximum value.
4011 Set_RM_Size (Rectype, Hbit + 1);
4014 end Check_Record_Representation_Clause;
4020 procedure Check_Size
4024 Biased : out Boolean)
4026 UT : constant Entity_Id := Underlying_Type (T);
4032 -- Dismiss cases for generic types or types with previous errors
4035 or else UT = Any_Type
4036 or else Is_Generic_Type (UT)
4037 or else Is_Generic_Type (Root_Type (UT))
4041 -- Check case of bit packed array
4043 elsif Is_Array_Type (UT)
4044 and then Known_Static_Component_Size (UT)
4045 and then Is_Bit_Packed_Array (UT)
4053 Asiz := Component_Size (UT);
4054 Indx := First_Index (UT);
4056 Ityp := Etype (Indx);
4058 -- If non-static bound, then we are not in the business of
4059 -- trying to check the length, and indeed an error will be
4060 -- issued elsewhere, since sizes of non-static array types
4061 -- cannot be set implicitly or explicitly.
4063 if not Is_Static_Subtype (Ityp) then
4067 -- Otherwise accumulate next dimension
4069 Asiz := Asiz * (Expr_Value (Type_High_Bound (Ityp)) -
4070 Expr_Value (Type_Low_Bound (Ityp)) +
4074 exit when No (Indx);
4080 Error_Msg_Uint_1 := Asiz;
4082 ("size for& too small, minimum allowed is ^", N, T);
4083 Set_Esize (T, Asiz);
4084 Set_RM_Size (T, Asiz);
4088 -- All other composite types are ignored
4090 elsif Is_Composite_Type (UT) then
4093 -- For fixed-point types, don't check minimum if type is not frozen,
4094 -- since we don't know all the characteristics of the type that can
4095 -- affect the size (e.g. a specified small) till freeze time.
4097 elsif Is_Fixed_Point_Type (UT)
4098 and then not Is_Frozen (UT)
4102 -- Cases for which a minimum check is required
4105 -- Ignore if specified size is correct for the type
4107 if Known_Esize (UT) and then Siz = Esize (UT) then
4111 -- Otherwise get minimum size
4113 M := UI_From_Int (Minimum_Size (UT));
4117 -- Size is less than minimum size, but one possibility remains
4118 -- that we can manage with the new size if we bias the type.
4120 M := UI_From_Int (Minimum_Size (UT, Biased => True));
4123 Error_Msg_Uint_1 := M;
4125 ("size for& too small, minimum allowed is ^", N, T);
4135 -------------------------
4136 -- Get_Alignment_Value --
4137 -------------------------
4139 function Get_Alignment_Value (Expr : Node_Id) return Uint is
4140 Align : constant Uint := Static_Integer (Expr);
4143 if Align = No_Uint then
4146 elsif Align <= 0 then
4147 Error_Msg_N ("alignment value must be positive", Expr);
4151 for J in Int range 0 .. 64 loop
4153 M : constant Uint := Uint_2 ** J;
4156 exit when M = Align;
4160 ("alignment value must be power of 2", Expr);
4168 end Get_Alignment_Value;
4174 procedure Initialize is
4176 Address_Clause_Checks.Init;
4177 Independence_Checks.Init;
4178 Unchecked_Conversions.Init;
4181 -------------------------
4182 -- Is_Operational_Item --
4183 -------------------------
4185 function Is_Operational_Item (N : Node_Id) return Boolean is
4187 if Nkind (N) /= N_Attribute_Definition_Clause then
4191 Id : constant Attribute_Id := Get_Attribute_Id (Chars (N));
4193 return Id = Attribute_Input
4194 or else Id = Attribute_Output
4195 or else Id = Attribute_Read
4196 or else Id = Attribute_Write
4197 or else Id = Attribute_External_Tag;
4200 end Is_Operational_Item;
4206 function Minimum_Size
4208 Biased : Boolean := False) return Nat
4210 Lo : Uint := No_Uint;
4211 Hi : Uint := No_Uint;
4212 LoR : Ureal := No_Ureal;
4213 HiR : Ureal := No_Ureal;
4214 LoSet : Boolean := False;
4215 HiSet : Boolean := False;
4219 R_Typ : constant Entity_Id := Root_Type (T);
4222 -- If bad type, return 0
4224 if T = Any_Type then
4227 -- For generic types, just return zero. There cannot be any legitimate
4228 -- need to know such a size, but this routine may be called with a
4229 -- generic type as part of normal processing.
4231 elsif Is_Generic_Type (R_Typ)
4232 or else R_Typ = Any_Type
4236 -- Access types. Normally an access type cannot have a size smaller
4237 -- than the size of System.Address. The exception is on VMS, where
4238 -- we have short and long addresses, and it is possible for an access
4239 -- type to have a short address size (and thus be less than the size
4240 -- of System.Address itself). We simply skip the check for VMS, and
4241 -- leave it to the back end to do the check.
4243 elsif Is_Access_Type (T) then
4244 if OpenVMS_On_Target then
4247 return System_Address_Size;
4250 -- Floating-point types
4252 elsif Is_Floating_Point_Type (T) then
4253 return UI_To_Int (Esize (R_Typ));
4257 elsif Is_Discrete_Type (T) then
4259 -- The following loop is looking for the nearest compile time known
4260 -- bounds following the ancestor subtype chain. The idea is to find
4261 -- the most restrictive known bounds information.
4265 if Ancest = Any_Type or else Etype (Ancest) = Any_Type then
4270 if Compile_Time_Known_Value (Type_Low_Bound (Ancest)) then
4271 Lo := Expr_Rep_Value (Type_Low_Bound (Ancest));
4278 if Compile_Time_Known_Value (Type_High_Bound (Ancest)) then
4279 Hi := Expr_Rep_Value (Type_High_Bound (Ancest));
4285 Ancest := Ancestor_Subtype (Ancest);
4288 Ancest := Base_Type (T);
4290 if Is_Generic_Type (Ancest) then
4296 -- Fixed-point types. We can't simply use Expr_Value to get the
4297 -- Corresponding_Integer_Value values of the bounds, since these do not
4298 -- get set till the type is frozen, and this routine can be called
4299 -- before the type is frozen. Similarly the test for bounds being static
4300 -- needs to include the case where we have unanalyzed real literals for
4303 elsif Is_Fixed_Point_Type (T) then
4305 -- The following loop is looking for the nearest compile time known
4306 -- bounds following the ancestor subtype chain. The idea is to find
4307 -- the most restrictive known bounds information.
4311 if Ancest = Any_Type or else Etype (Ancest) = Any_Type then
4315 -- Note: In the following two tests for LoSet and HiSet, it may
4316 -- seem redundant to test for N_Real_Literal here since normally
4317 -- one would assume that the test for the value being known at
4318 -- compile time includes this case. However, there is a glitch.
4319 -- If the real literal comes from folding a non-static expression,
4320 -- then we don't consider any non- static expression to be known
4321 -- at compile time if we are in configurable run time mode (needed
4322 -- in some cases to give a clearer definition of what is and what
4323 -- is not accepted). So the test is indeed needed. Without it, we
4324 -- would set neither Lo_Set nor Hi_Set and get an infinite loop.
4327 if Nkind (Type_Low_Bound (Ancest)) = N_Real_Literal
4328 or else Compile_Time_Known_Value (Type_Low_Bound (Ancest))
4330 LoR := Expr_Value_R (Type_Low_Bound (Ancest));
4337 if Nkind (Type_High_Bound (Ancest)) = N_Real_Literal
4338 or else Compile_Time_Known_Value (Type_High_Bound (Ancest))
4340 HiR := Expr_Value_R (Type_High_Bound (Ancest));
4346 Ancest := Ancestor_Subtype (Ancest);
4349 Ancest := Base_Type (T);
4351 if Is_Generic_Type (Ancest) then
4357 Lo := UR_To_Uint (LoR / Small_Value (T));
4358 Hi := UR_To_Uint (HiR / Small_Value (T));
4360 -- No other types allowed
4363 raise Program_Error;
4366 -- Fall through with Hi and Lo set. Deal with biased case
4369 and then not Is_Fixed_Point_Type (T)
4370 and then not (Is_Enumeration_Type (T)
4371 and then Has_Non_Standard_Rep (T)))
4372 or else Has_Biased_Representation (T)
4378 -- Signed case. Note that we consider types like range 1 .. -1 to be
4379 -- signed for the purpose of computing the size, since the bounds have
4380 -- to be accommodated in the base type.
4382 if Lo < 0 or else Hi < 0 then
4386 -- S = size, B = 2 ** (size - 1) (can accommodate -B .. +(B - 1))
4387 -- Note that we accommodate the case where the bounds cross. This
4388 -- can happen either because of the way the bounds are declared
4389 -- or because of the algorithm in Freeze_Fixed_Point_Type.
4403 -- If both bounds are positive, make sure that both are represen-
4404 -- table in the case where the bounds are crossed. This can happen
4405 -- either because of the way the bounds are declared, or because of
4406 -- the algorithm in Freeze_Fixed_Point_Type.
4412 -- S = size, (can accommodate 0 .. (2**size - 1))
4415 while Hi >= Uint_2 ** S loop
4423 ---------------------------
4424 -- New_Stream_Subprogram --
4425 ---------------------------
4427 procedure New_Stream_Subprogram
4431 Nam : TSS_Name_Type)
4433 Loc : constant Source_Ptr := Sloc (N);
4434 Sname : constant Name_Id := Make_TSS_Name (Base_Type (Ent), Nam);
4435 Subp_Id : Entity_Id;
4436 Subp_Decl : Node_Id;
4440 Defer_Declaration : constant Boolean :=
4441 Is_Tagged_Type (Ent) or else Is_Private_Type (Ent);
4442 -- For a tagged type, there is a declaration for each stream attribute
4443 -- at the freeze point, and we must generate only a completion of this
4444 -- declaration. We do the same for private types, because the full view
4445 -- might be tagged. Otherwise we generate a declaration at the point of
4446 -- the attribute definition clause.
4448 function Build_Spec return Node_Id;
4449 -- Used for declaration and renaming declaration, so that this is
4450 -- treated as a renaming_as_body.
4456 function Build_Spec return Node_Id is
4457 Out_P : constant Boolean := (Nam = TSS_Stream_Read);
4460 T_Ref : constant Node_Id := New_Reference_To (Etyp, Loc);
4463 Subp_Id := Make_Defining_Identifier (Loc, Sname);
4465 -- S : access Root_Stream_Type'Class
4467 Formals := New_List (
4468 Make_Parameter_Specification (Loc,
4469 Defining_Identifier =>
4470 Make_Defining_Identifier (Loc, Name_S),
4472 Make_Access_Definition (Loc,
4475 Designated_Type (Etype (F)), Loc))));
4477 if Nam = TSS_Stream_Input then
4478 Spec := Make_Function_Specification (Loc,
4479 Defining_Unit_Name => Subp_Id,
4480 Parameter_Specifications => Formals,
4481 Result_Definition => T_Ref);
4486 Make_Parameter_Specification (Loc,
4487 Defining_Identifier => Make_Defining_Identifier (Loc, Name_V),
4488 Out_Present => Out_P,
4489 Parameter_Type => T_Ref));
4492 Make_Procedure_Specification (Loc,
4493 Defining_Unit_Name => Subp_Id,
4494 Parameter_Specifications => Formals);
4500 -- Start of processing for New_Stream_Subprogram
4503 F := First_Formal (Subp);
4505 if Ekind (Subp) = E_Procedure then
4506 Etyp := Etype (Next_Formal (F));
4508 Etyp := Etype (Subp);
4511 -- Prepare subprogram declaration and insert it as an action on the
4512 -- clause node. The visibility for this entity is used to test for
4513 -- visibility of the attribute definition clause (in the sense of
4514 -- 8.3(23) as amended by AI-195).
4516 if not Defer_Declaration then
4518 Make_Subprogram_Declaration (Loc,
4519 Specification => Build_Spec);
4521 -- For a tagged type, there is always a visible declaration for each
4522 -- stream TSS (it is a predefined primitive operation), and the
4523 -- completion of this declaration occurs at the freeze point, which is
4524 -- not always visible at places where the attribute definition clause is
4525 -- visible. So, we create a dummy entity here for the purpose of
4526 -- tracking the visibility of the attribute definition clause itself.
4530 Make_Defining_Identifier (Loc,
4531 Chars => New_External_Name (Sname, 'V'));
4533 Make_Object_Declaration (Loc,
4534 Defining_Identifier => Subp_Id,
4535 Object_Definition => New_Occurrence_Of (Standard_Boolean, Loc));
4538 Insert_Action (N, Subp_Decl);
4539 Set_Entity (N, Subp_Id);
4542 Make_Subprogram_Renaming_Declaration (Loc,
4543 Specification => Build_Spec,
4544 Name => New_Reference_To (Subp, Loc));
4546 if Defer_Declaration then
4547 Set_TSS (Base_Type (Ent), Subp_Id);
4549 Insert_Action (N, Subp_Decl);
4550 Copy_TSS (Subp_Id, Base_Type (Ent));
4552 end New_Stream_Subprogram;
4554 ------------------------
4555 -- Rep_Item_Too_Early --
4556 ------------------------
4558 function Rep_Item_Too_Early (T : Entity_Id; N : Node_Id) return Boolean is
4560 -- Cannot apply non-operational rep items to generic types
4562 if Is_Operational_Item (N) then
4566 and then Is_Generic_Type (Root_Type (T))
4568 Error_Msg_N ("representation item not allowed for generic type", N);
4572 -- Otherwise check for incomplete type
4574 if Is_Incomplete_Or_Private_Type (T)
4575 and then No (Underlying_Type (T))
4578 ("representation item must be after full type declaration", N);
4581 -- If the type has incomplete components, a representation clause is
4582 -- illegal but stream attributes and Convention pragmas are correct.
4584 elsif Has_Private_Component (T) then
4585 if Nkind (N) = N_Pragma then
4589 ("representation item must appear after type is fully defined",
4596 end Rep_Item_Too_Early;
4598 -----------------------
4599 -- Rep_Item_Too_Late --
4600 -----------------------
4602 function Rep_Item_Too_Late
4605 FOnly : Boolean := False) return Boolean
4608 Parent_Type : Entity_Id;
4611 -- Output the too late message. Note that this is not considered a
4612 -- serious error, since the effect is simply that we ignore the
4613 -- representation clause in this case.
4619 procedure Too_Late is
4621 Error_Msg_N ("|representation item appears too late!", N);
4624 -- Start of processing for Rep_Item_Too_Late
4627 -- First make sure entity is not frozen (RM 13.1(9)). Exclude imported
4628 -- types, which may be frozen if they appear in a representation clause
4629 -- for a local type.
4632 and then not From_With_Type (T)
4635 S := First_Subtype (T);
4637 if Present (Freeze_Node (S)) then
4639 ("?no more representation items for }", Freeze_Node (S), S);
4644 -- Check for case of non-tagged derived type whose parent either has
4645 -- primitive operations, or is a by reference type (RM 13.1(10)).
4649 and then Is_Derived_Type (T)
4650 and then not Is_Tagged_Type (T)
4652 Parent_Type := Etype (Base_Type (T));
4654 if Has_Primitive_Operations (Parent_Type) then
4657 ("primitive operations already defined for&!", N, Parent_Type);
4660 elsif Is_By_Reference_Type (Parent_Type) then
4663 ("parent type & is a by reference type!", N, Parent_Type);
4668 -- No error, link item into head of chain of rep items for the entity,
4669 -- but avoid chaining if we have an overloadable entity, and the pragma
4670 -- is one that can apply to multiple overloaded entities.
4672 if Is_Overloadable (T)
4673 and then Nkind (N) = N_Pragma
4676 Pname : constant Name_Id := Pragma_Name (N);
4678 if Pname = Name_Convention or else
4679 Pname = Name_Import or else
4680 Pname = Name_Export or else
4681 Pname = Name_External or else
4682 Pname = Name_Interface
4689 Record_Rep_Item (T, N);
4691 end Rep_Item_Too_Late;
4693 -------------------------
4694 -- Same_Representation --
4695 -------------------------
4697 function Same_Representation (Typ1, Typ2 : Entity_Id) return Boolean is
4698 T1 : constant Entity_Id := Underlying_Type (Typ1);
4699 T2 : constant Entity_Id := Underlying_Type (Typ2);
4702 -- A quick check, if base types are the same, then we definitely have
4703 -- the same representation, because the subtype specific representation
4704 -- attributes (Size and Alignment) do not affect representation from
4705 -- the point of view of this test.
4707 if Base_Type (T1) = Base_Type (T2) then
4710 elsif Is_Private_Type (Base_Type (T2))
4711 and then Base_Type (T1) = Full_View (Base_Type (T2))
4716 -- Tagged types never have differing representations
4718 if Is_Tagged_Type (T1) then
4722 -- Representations are definitely different if conventions differ
4724 if Convention (T1) /= Convention (T2) then
4728 -- Representations are different if component alignments differ
4730 if (Is_Record_Type (T1) or else Is_Array_Type (T1))
4732 (Is_Record_Type (T2) or else Is_Array_Type (T2))
4733 and then Component_Alignment (T1) /= Component_Alignment (T2)
4738 -- For arrays, the only real issue is component size. If we know the
4739 -- component size for both arrays, and it is the same, then that's
4740 -- good enough to know we don't have a change of representation.
4742 if Is_Array_Type (T1) then
4743 if Known_Component_Size (T1)
4744 and then Known_Component_Size (T2)
4745 and then Component_Size (T1) = Component_Size (T2)
4751 -- Types definitely have same representation if neither has non-standard
4752 -- representation since default representations are always consistent.
4753 -- If only one has non-standard representation, and the other does not,
4754 -- then we consider that they do not have the same representation. They
4755 -- might, but there is no way of telling early enough.
4757 if Has_Non_Standard_Rep (T1) then
4758 if not Has_Non_Standard_Rep (T2) then
4762 return not Has_Non_Standard_Rep (T2);
4765 -- Here the two types both have non-standard representation, and we need
4766 -- to determine if they have the same non-standard representation.
4768 -- For arrays, we simply need to test if the component sizes are the
4769 -- same. Pragma Pack is reflected in modified component sizes, so this
4770 -- check also deals with pragma Pack.
4772 if Is_Array_Type (T1) then
4773 return Component_Size (T1) = Component_Size (T2);
4775 -- Tagged types always have the same representation, because it is not
4776 -- possible to specify different representations for common fields.
4778 elsif Is_Tagged_Type (T1) then
4781 -- Case of record types
4783 elsif Is_Record_Type (T1) then
4785 -- Packed status must conform
4787 if Is_Packed (T1) /= Is_Packed (T2) then
4790 -- Otherwise we must check components. Typ2 maybe a constrained
4791 -- subtype with fewer components, so we compare the components
4792 -- of the base types.
4795 Record_Case : declare
4796 CD1, CD2 : Entity_Id;
4798 function Same_Rep return Boolean;
4799 -- CD1 and CD2 are either components or discriminants. This
4800 -- function tests whether the two have the same representation
4806 function Same_Rep return Boolean is
4808 if No (Component_Clause (CD1)) then
4809 return No (Component_Clause (CD2));
4813 Present (Component_Clause (CD2))
4815 Component_Bit_Offset (CD1) = Component_Bit_Offset (CD2)
4817 Esize (CD1) = Esize (CD2);
4821 -- Start of processing for Record_Case
4824 if Has_Discriminants (T1) then
4825 CD1 := First_Discriminant (T1);
4826 CD2 := First_Discriminant (T2);
4828 -- The number of discriminants may be different if the
4829 -- derived type has fewer (constrained by values). The
4830 -- invisible discriminants retain the representation of
4831 -- the original, so the discrepancy does not per se
4832 -- indicate a different representation.
4835 and then Present (CD2)
4837 if not Same_Rep then
4840 Next_Discriminant (CD1);
4841 Next_Discriminant (CD2);
4846 CD1 := First_Component (Underlying_Type (Base_Type (T1)));
4847 CD2 := First_Component (Underlying_Type (Base_Type (T2)));
4849 while Present (CD1) loop
4850 if not Same_Rep then
4853 Next_Component (CD1);
4854 Next_Component (CD2);
4862 -- For enumeration types, we must check each literal to see if the
4863 -- representation is the same. Note that we do not permit enumeration
4864 -- representation clauses for Character and Wide_Character, so these
4865 -- cases were already dealt with.
4867 elsif Is_Enumeration_Type (T1) then
4868 Enumeration_Case : declare
4872 L1 := First_Literal (T1);
4873 L2 := First_Literal (T2);
4875 while Present (L1) loop
4876 if Enumeration_Rep (L1) /= Enumeration_Rep (L2) then
4886 end Enumeration_Case;
4888 -- Any other types have the same representation for these purposes
4893 end Same_Representation;
4899 procedure Set_Biased
4903 Biased : Boolean := True)
4907 Set_Has_Biased_Representation (E);
4909 if Warn_On_Biased_Representation then
4911 ("?" & Msg & " forces biased representation for&", N, E);
4916 --------------------
4917 -- Set_Enum_Esize --
4918 --------------------
4920 procedure Set_Enum_Esize (T : Entity_Id) is
4928 -- Find the minimum standard size (8,16,32,64) that fits
4930 Lo := Enumeration_Rep (Entity (Type_Low_Bound (T)));
4931 Hi := Enumeration_Rep (Entity (Type_High_Bound (T)));
4934 if Lo >= -Uint_2**07 and then Hi < Uint_2**07 then
4935 Sz := Standard_Character_Size; -- May be > 8 on some targets
4937 elsif Lo >= -Uint_2**15 and then Hi < Uint_2**15 then
4940 elsif Lo >= -Uint_2**31 and then Hi < Uint_2**31 then
4943 else pragma Assert (Lo >= -Uint_2**63 and then Hi < Uint_2**63);
4948 if Hi < Uint_2**08 then
4949 Sz := Standard_Character_Size; -- May be > 8 on some targets
4951 elsif Hi < Uint_2**16 then
4954 elsif Hi < Uint_2**32 then
4957 else pragma Assert (Hi < Uint_2**63);
4962 -- That minimum is the proper size unless we have a foreign convention
4963 -- and the size required is 32 or less, in which case we bump the size
4964 -- up to 32. This is required for C and C++ and seems reasonable for
4965 -- all other foreign conventions.
4967 if Has_Foreign_Convention (T)
4968 and then Esize (T) < Standard_Integer_Size
4970 Init_Esize (T, Standard_Integer_Size);
4976 ------------------------------
4977 -- Validate_Address_Clauses --
4978 ------------------------------
4980 procedure Validate_Address_Clauses is
4982 for J in Address_Clause_Checks.First .. Address_Clause_Checks.Last loop
4984 ACCR : Address_Clause_Check_Record
4985 renames Address_Clause_Checks.Table (J);
4996 -- Skip processing of this entry if warning already posted
4998 if not Address_Warning_Posted (ACCR.N) then
5000 Expr := Original_Node (Expression (ACCR.N));
5004 X_Alignment := Alignment (ACCR.X);
5005 Y_Alignment := Alignment (ACCR.Y);
5007 -- Similarly obtain sizes
5009 X_Size := Esize (ACCR.X);
5010 Y_Size := Esize (ACCR.Y);
5012 -- Check for large object overlaying smaller one
5015 and then X_Size > Uint_0
5016 and then X_Size > Y_Size
5019 ("?& overlays smaller object", ACCR.N, ACCR.X);
5021 ("\?program execution may be erroneous", ACCR.N);
5022 Error_Msg_Uint_1 := X_Size;
5024 ("\?size of & is ^", ACCR.N, ACCR.X);
5025 Error_Msg_Uint_1 := Y_Size;
5027 ("\?size of & is ^", ACCR.N, ACCR.Y);
5029 -- Check for inadequate alignment, both of the base object
5030 -- and of the offset, if any.
5032 -- Note: we do not check the alignment if we gave a size
5033 -- warning, since it would likely be redundant.
5035 elsif Y_Alignment /= Uint_0
5036 and then (Y_Alignment < X_Alignment
5039 Nkind (Expr) = N_Attribute_Reference
5041 Attribute_Name (Expr) = Name_Address
5043 Has_Compatible_Alignment
5044 (ACCR.X, Prefix (Expr))
5045 /= Known_Compatible))
5048 ("?specified address for& may be inconsistent "
5052 ("\?program execution may be erroneous (RM 13.3(27))",
5054 Error_Msg_Uint_1 := X_Alignment;
5056 ("\?alignment of & is ^",
5058 Error_Msg_Uint_1 := Y_Alignment;
5060 ("\?alignment of & is ^",
5062 if Y_Alignment >= X_Alignment then
5064 ("\?but offset is not multiple of alignment",
5071 end Validate_Address_Clauses;
5073 ---------------------------
5074 -- Validate_Independence --
5075 ---------------------------
5077 procedure Validate_Independence is
5078 SU : constant Uint := UI_From_Int (System_Storage_Unit);
5086 procedure Check_Array_Type (Atyp : Entity_Id);
5087 -- Checks if the array type Atyp has independent components, and
5088 -- if not, outputs an appropriate set of error messages.
5090 procedure No_Independence;
5091 -- Output message that independence cannot be guaranteed
5093 function OK_Component (C : Entity_Id) return Boolean;
5094 -- Checks one component to see if it is independently accessible, and
5095 -- if so yields True, otherwise yields False if independent access
5096 -- cannot be guaranteed. This is a conservative routine, it only
5097 -- returns True if it knows for sure, it returns False if it knows
5098 -- there is a problem, or it cannot be sure there is no problem.
5100 procedure Reason_Bad_Component (C : Entity_Id);
5101 -- Outputs continuation message if a reason can be determined for
5102 -- the component C being bad.
5104 ----------------------
5105 -- Check_Array_Type --
5106 ----------------------
5108 procedure Check_Array_Type (Atyp : Entity_Id) is
5109 Ctyp : constant Entity_Id := Component_Type (Atyp);
5112 -- OK if no alignment clause, no pack, and no component size
5114 if not Has_Component_Size_Clause (Atyp)
5115 and then not Has_Alignment_Clause (Atyp)
5116 and then not Is_Packed (Atyp)
5121 -- Check actual component size
5123 if not Known_Component_Size (Atyp)
5124 or else not (Addressable (Component_Size (Atyp))
5125 and then Component_Size (Atyp) < 64)
5126 or else Component_Size (Atyp) mod Esize (Ctyp) /= 0
5130 -- Bad component size, check reason
5132 if Has_Component_Size_Clause (Atyp) then
5134 Get_Attribute_Definition_Clause
5135 (Atyp, Attribute_Component_Size);
5138 Error_Msg_Sloc := Sloc (P);
5139 Error_Msg_N ("\because of Component_Size clause#", N);
5144 if Is_Packed (Atyp) then
5145 P := Get_Rep_Pragma (Atyp, Name_Pack);
5148 Error_Msg_Sloc := Sloc (P);
5149 Error_Msg_N ("\because of pragma Pack#", N);
5154 -- No reason found, just return
5159 -- Array type is OK independence-wise
5162 end Check_Array_Type;
5164 ---------------------
5165 -- No_Independence --
5166 ---------------------
5168 procedure No_Independence is
5170 if Pragma_Name (N) = Name_Independent then
5172 ("independence cannot be guaranteed for&", N, E);
5175 ("independent components cannot be guaranteed for&", N, E);
5177 end No_Independence;
5183 function OK_Component (C : Entity_Id) return Boolean is
5184 Rec : constant Entity_Id := Scope (C);
5185 Ctyp : constant Entity_Id := Etype (C);
5188 -- OK if no component clause, no Pack, and no alignment clause
5190 if No (Component_Clause (C))
5191 and then not Is_Packed (Rec)
5192 and then not Has_Alignment_Clause (Rec)
5197 -- Here we look at the actual component layout. A component is
5198 -- addressable if its size is a multiple of the Esize of the
5199 -- component type, and its starting position in the record has
5200 -- appropriate alignment, and the record itself has appropriate
5201 -- alignment to guarantee the component alignment.
5203 -- Make sure sizes are static, always assume the worst for any
5204 -- cases where we cannot check static values.
5206 if not (Known_Static_Esize (C)
5207 and then Known_Static_Esize (Ctyp))
5212 -- Size of component must be addressable or greater than 64 bits
5213 -- and a multiple of bytes.
5215 if not Addressable (Esize (C))
5216 and then Esize (C) < Uint_64
5221 -- Check size is proper multiple
5223 if Esize (C) mod Esize (Ctyp) /= 0 then
5227 -- Check alignment of component is OK
5229 if not Known_Component_Bit_Offset (C)
5230 or else Component_Bit_Offset (C) < Uint_0
5231 or else Component_Bit_Offset (C) mod Esize (Ctyp) /= 0
5236 -- Check alignment of record type is OK
5238 if not Known_Alignment (Rec)
5239 or else (Alignment (Rec) * SU) mod Esize (Ctyp) /= 0
5244 -- All tests passed, component is addressable
5249 --------------------------
5250 -- Reason_Bad_Component --
5251 --------------------------
5253 procedure Reason_Bad_Component (C : Entity_Id) is
5254 Rec : constant Entity_Id := Scope (C);
5255 Ctyp : constant Entity_Id := Etype (C);
5258 -- If component clause present assume that's the problem
5260 if Present (Component_Clause (C)) then
5261 Error_Msg_Sloc := Sloc (Component_Clause (C));
5262 Error_Msg_N ("\because of Component_Clause#", N);
5266 -- If pragma Pack clause present, assume that's the problem
5268 if Is_Packed (Rec) then
5269 P := Get_Rep_Pragma (Rec, Name_Pack);
5272 Error_Msg_Sloc := Sloc (P);
5273 Error_Msg_N ("\because of pragma Pack#", N);
5278 -- See if record has bad alignment clause
5280 if Has_Alignment_Clause (Rec)
5281 and then Known_Alignment (Rec)
5282 and then (Alignment (Rec) * SU) mod Esize (Ctyp) /= 0
5284 P := Get_Attribute_Definition_Clause (Rec, Attribute_Alignment);
5287 Error_Msg_Sloc := Sloc (P);
5288 Error_Msg_N ("\because of Alignment clause#", N);
5292 -- Couldn't find a reason, so return without a message
5295 end Reason_Bad_Component;
5297 -- Start of processing for Validate_Independence
5300 for J in Independence_Checks.First .. Independence_Checks.Last loop
5301 N := Independence_Checks.Table (J).N;
5302 E := Independence_Checks.Table (J).E;
5303 IC := Pragma_Name (N) = Name_Independent_Components;
5305 -- Deal with component case
5307 if Ekind (E) = E_Discriminant or else Ekind (E) = E_Component then
5308 if not OK_Component (E) then
5310 Reason_Bad_Component (E);
5315 -- Deal with record with Independent_Components
5317 if IC and then Is_Record_Type (E) then
5318 Comp := First_Component_Or_Discriminant (E);
5319 while Present (Comp) loop
5320 if not OK_Component (Comp) then
5322 Reason_Bad_Component (Comp);
5326 Next_Component_Or_Discriminant (Comp);
5330 -- Deal with address clause case
5332 if Is_Object (E) then
5333 Addr := Address_Clause (E);
5335 if Present (Addr) then
5337 Error_Msg_Sloc := Sloc (Addr);
5338 Error_Msg_N ("\because of Address clause#", N);
5343 -- Deal with independent components for array type
5345 if IC and then Is_Array_Type (E) then
5346 Check_Array_Type (E);
5349 -- Deal with independent components for array object
5351 if IC and then Is_Object (E) and then Is_Array_Type (Etype (E)) then
5352 Check_Array_Type (Etype (E));
5357 end Validate_Independence;
5359 -----------------------------------
5360 -- Validate_Unchecked_Conversion --
5361 -----------------------------------
5363 procedure Validate_Unchecked_Conversion
5365 Act_Unit : Entity_Id)
5372 -- Obtain source and target types. Note that we call Ancestor_Subtype
5373 -- here because the processing for generic instantiation always makes
5374 -- subtypes, and we want the original frozen actual types.
5376 -- If we are dealing with private types, then do the check on their
5377 -- fully declared counterparts if the full declarations have been
5378 -- encountered (they don't have to be visible, but they must exist!)
5380 Source := Ancestor_Subtype (Etype (First_Formal (Act_Unit)));
5382 if Is_Private_Type (Source)
5383 and then Present (Underlying_Type (Source))
5385 Source := Underlying_Type (Source);
5388 Target := Ancestor_Subtype (Etype (Act_Unit));
5390 -- If either type is generic, the instantiation happens within a generic
5391 -- unit, and there is nothing to check. The proper check
5392 -- will happen when the enclosing generic is instantiated.
5394 if Is_Generic_Type (Source) or else Is_Generic_Type (Target) then
5398 if Is_Private_Type (Target)
5399 and then Present (Underlying_Type (Target))
5401 Target := Underlying_Type (Target);
5404 -- Source may be unconstrained array, but not target
5406 if Is_Array_Type (Target)
5407 and then not Is_Constrained (Target)
5410 ("unchecked conversion to unconstrained array not allowed", N);
5414 -- Warn if conversion between two different convention pointers
5416 if Is_Access_Type (Target)
5417 and then Is_Access_Type (Source)
5418 and then Convention (Target) /= Convention (Source)
5419 and then Warn_On_Unchecked_Conversion
5421 -- Give warnings for subprogram pointers only on most targets. The
5422 -- exception is VMS, where data pointers can have different lengths
5423 -- depending on the pointer convention.
5425 if Is_Access_Subprogram_Type (Target)
5426 or else Is_Access_Subprogram_Type (Source)
5427 or else OpenVMS_On_Target
5430 ("?conversion between pointers with different conventions!", N);
5434 -- Warn if one of the operands is Ada.Calendar.Time. Do not emit a
5435 -- warning when compiling GNAT-related sources.
5437 if Warn_On_Unchecked_Conversion
5438 and then not In_Predefined_Unit (N)
5439 and then RTU_Loaded (Ada_Calendar)
5441 (Chars (Source) = Name_Time
5443 Chars (Target) = Name_Time)
5445 -- If Ada.Calendar is loaded and the name of one of the operands is
5446 -- Time, there is a good chance that this is Ada.Calendar.Time.
5449 Calendar_Time : constant Entity_Id :=
5450 Full_View (RTE (RO_CA_Time));
5452 pragma Assert (Present (Calendar_Time));
5454 if Source = Calendar_Time
5455 or else Target = Calendar_Time
5458 ("?representation of 'Time values may change between " &
5459 "'G'N'A'T versions", N);
5464 -- Make entry in unchecked conversion table for later processing by
5465 -- Validate_Unchecked_Conversions, which will check sizes and alignments
5466 -- (using values set by the back-end where possible). This is only done
5467 -- if the appropriate warning is active.
5469 if Warn_On_Unchecked_Conversion then
5470 Unchecked_Conversions.Append
5471 (New_Val => UC_Entry'
5476 -- If both sizes are known statically now, then back end annotation
5477 -- is not required to do a proper check but if either size is not
5478 -- known statically, then we need the annotation.
5480 if Known_Static_RM_Size (Source)
5481 and then Known_Static_RM_Size (Target)
5485 Back_Annotate_Rep_Info := True;
5489 -- If unchecked conversion to access type, and access type is declared
5490 -- in the same unit as the unchecked conversion, then set the
5491 -- No_Strict_Aliasing flag (no strict aliasing is implicit in this
5494 if Is_Access_Type (Target) and then
5495 In_Same_Source_Unit (Target, N)
5497 Set_No_Strict_Aliasing (Implementation_Base_Type (Target));
5500 -- Generate N_Validate_Unchecked_Conversion node for back end in
5501 -- case the back end needs to perform special validation checks.
5503 -- Shouldn't this be in Exp_Ch13, since the check only gets done
5504 -- if we have full expansion and the back end is called ???
5507 Make_Validate_Unchecked_Conversion (Sloc (N));
5508 Set_Source_Type (Vnode, Source);
5509 Set_Target_Type (Vnode, Target);
5511 -- If the unchecked conversion node is in a list, just insert before it.
5512 -- If not we have some strange case, not worth bothering about.
5514 if Is_List_Member (N) then
5515 Insert_After (N, Vnode);
5517 end Validate_Unchecked_Conversion;
5519 ------------------------------------
5520 -- Validate_Unchecked_Conversions --
5521 ------------------------------------
5523 procedure Validate_Unchecked_Conversions is
5525 for N in Unchecked_Conversions.First .. Unchecked_Conversions.Last loop
5527 T : UC_Entry renames Unchecked_Conversions.Table (N);
5529 Eloc : constant Source_Ptr := T.Eloc;
5530 Source : constant Entity_Id := T.Source;
5531 Target : constant Entity_Id := T.Target;
5537 -- This validation check, which warns if we have unequal sizes for
5538 -- unchecked conversion, and thus potentially implementation
5539 -- dependent semantics, is one of the few occasions on which we
5540 -- use the official RM size instead of Esize. See description in
5541 -- Einfo "Handling of Type'Size Values" for details.
5543 if Serious_Errors_Detected = 0
5544 and then Known_Static_RM_Size (Source)
5545 and then Known_Static_RM_Size (Target)
5547 -- Don't do the check if warnings off for either type, note the
5548 -- deliberate use of OR here instead of OR ELSE to get the flag
5549 -- Warnings_Off_Used set for both types if appropriate.
5551 and then not (Has_Warnings_Off (Source)
5553 Has_Warnings_Off (Target))
5555 Source_Siz := RM_Size (Source);
5556 Target_Siz := RM_Size (Target);
5558 if Source_Siz /= Target_Siz then
5560 ("?types for unchecked conversion have different sizes!",
5563 if All_Errors_Mode then
5564 Error_Msg_Name_1 := Chars (Source);
5565 Error_Msg_Uint_1 := Source_Siz;
5566 Error_Msg_Name_2 := Chars (Target);
5567 Error_Msg_Uint_2 := Target_Siz;
5568 Error_Msg ("\size of % is ^, size of % is ^?", Eloc);
5570 Error_Msg_Uint_1 := UI_Abs (Source_Siz - Target_Siz);
5572 if Is_Discrete_Type (Source)
5573 and then Is_Discrete_Type (Target)
5575 if Source_Siz > Target_Siz then
5577 ("\?^ high order bits of source will be ignored!",
5580 elsif Is_Unsigned_Type (Source) then
5582 ("\?source will be extended with ^ high order " &
5583 "zero bits?!", Eloc);
5587 ("\?source will be extended with ^ high order " &
5592 elsif Source_Siz < Target_Siz then
5593 if Is_Discrete_Type (Target) then
5594 if Bytes_Big_Endian then
5596 ("\?target value will include ^ undefined " &
5601 ("\?target value will include ^ undefined " &
5608 ("\?^ trailing bits of target value will be " &
5609 "undefined!", Eloc);
5612 else pragma Assert (Source_Siz > Target_Siz);
5614 ("\?^ trailing bits of source will be ignored!",
5621 -- If both types are access types, we need to check the alignment.
5622 -- If the alignment of both is specified, we can do it here.
5624 if Serious_Errors_Detected = 0
5625 and then Ekind (Source) in Access_Kind
5626 and then Ekind (Target) in Access_Kind
5627 and then Target_Strict_Alignment
5628 and then Present (Designated_Type (Source))
5629 and then Present (Designated_Type (Target))
5632 D_Source : constant Entity_Id := Designated_Type (Source);
5633 D_Target : constant Entity_Id := Designated_Type (Target);
5636 if Known_Alignment (D_Source)
5637 and then Known_Alignment (D_Target)
5640 Source_Align : constant Uint := Alignment (D_Source);
5641 Target_Align : constant Uint := Alignment (D_Target);
5644 if Source_Align < Target_Align
5645 and then not Is_Tagged_Type (D_Source)
5647 -- Suppress warning if warnings suppressed on either
5648 -- type or either designated type. Note the use of
5649 -- OR here instead of OR ELSE. That is intentional,
5650 -- we would like to set flag Warnings_Off_Used in
5651 -- all types for which warnings are suppressed.
5653 and then not (Has_Warnings_Off (D_Source)
5655 Has_Warnings_Off (D_Target)
5657 Has_Warnings_Off (Source)
5659 Has_Warnings_Off (Target))
5661 Error_Msg_Uint_1 := Target_Align;
5662 Error_Msg_Uint_2 := Source_Align;
5663 Error_Msg_Node_1 := D_Target;
5664 Error_Msg_Node_2 := D_Source;
5666 ("?alignment of & (^) is stricter than " &
5667 "alignment of & (^)!", Eloc);
5669 ("\?resulting access value may have invalid " &
5670 "alignment!", Eloc);
5678 end Validate_Unchecked_Conversions;