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;
56 with Targparm; use Targparm;
57 with Ttypes; use Ttypes;
58 with Tbuild; use Tbuild;
59 with Urealp; use Urealp;
61 with GNAT.Heap_Sort_G;
63 package body Sem_Ch13 is
65 SSU : constant Pos := System_Storage_Unit;
66 -- Convenient short hand for commonly used constant
68 -----------------------
69 -- Local Subprograms --
70 -----------------------
72 procedure Alignment_Check_For_Esize_Change (Typ : Entity_Id);
73 -- This routine is called after setting the Esize of type entity Typ.
74 -- The purpose is to deal with the situation where an alignment has been
75 -- inherited from a derived type that is no longer appropriate for the
76 -- new Esize value. In this case, we reset the Alignment to unknown.
78 function Get_Alignment_Value (Expr : Node_Id) return Uint;
79 -- Given the expression for an alignment value, returns the corresponding
80 -- Uint value. If the value is inappropriate, then error messages are
81 -- posted as required, and a value of No_Uint is returned.
83 function Is_Operational_Item (N : Node_Id) return Boolean;
84 -- A specification for a stream attribute is allowed before the full
85 -- type is declared, as explained in AI-00137 and the corrigendum.
86 -- Attributes that do not specify a representation characteristic are
87 -- operational attributes.
89 procedure New_Stream_Subprogram
94 -- Create a subprogram renaming of a given stream attribute to the
95 -- designated subprogram and then in the tagged case, provide this as a
96 -- primitive operation, or in the non-tagged case make an appropriate TSS
97 -- entry. This is more properly an expansion activity than just semantics,
98 -- but the presence of user-defined stream functions for limited types is a
99 -- legality check, which is why this takes place here rather than in
100 -- exp_ch13, where it was previously. Nam indicates the name of the TSS
101 -- function to be generated.
103 -- To avoid elaboration anomalies with freeze nodes, for untagged types
104 -- we generate both a subprogram declaration and a subprogram renaming
105 -- declaration, so that the attribute specification is handled as a
106 -- renaming_as_body. For tagged types, the specification is one of the
113 Biased : Boolean := True);
114 -- If Biased is True, sets Has_Biased_Representation flag for E, and
115 -- outputs a warning message at node N if Warn_On_Biased_Representation is
116 -- is True. This warning inserts the string Msg to describe the construct
119 ----------------------------------------------
120 -- Table for Validate_Unchecked_Conversions --
121 ----------------------------------------------
123 -- The following table collects unchecked conversions for validation.
124 -- Entries are made by Validate_Unchecked_Conversion and then the
125 -- call to Validate_Unchecked_Conversions does the actual error
126 -- checking and posting of warnings. The reason for this delayed
127 -- processing is to take advantage of back-annotations of size and
128 -- alignment values performed by the back end.
130 -- Note: the reason we store a Source_Ptr value instead of a Node_Id
131 -- is that by the time Validate_Unchecked_Conversions is called, Sprint
132 -- will already have modified all Sloc values if the -gnatD option is set.
134 type UC_Entry is record
135 Eloc : Source_Ptr; -- node used for posting warnings
136 Source : Entity_Id; -- source type for unchecked conversion
137 Target : Entity_Id; -- target type for unchecked conversion
140 package Unchecked_Conversions is new Table.Table (
141 Table_Component_Type => UC_Entry,
142 Table_Index_Type => Int,
143 Table_Low_Bound => 1,
145 Table_Increment => 200,
146 Table_Name => "Unchecked_Conversions");
148 ----------------------------------------
149 -- Table for Validate_Address_Clauses --
150 ----------------------------------------
152 -- If an address clause has the form
154 -- for X'Address use Expr
156 -- where Expr is of the form Y'Address or recursively is a reference
157 -- to a constant of either of these forms, and X and Y are entities of
158 -- objects, then if Y has a smaller alignment than X, that merits a
159 -- warning about possible bad alignment. The following table collects
160 -- address clauses of this kind. We put these in a table so that they
161 -- can be checked after the back end has completed annotation of the
162 -- alignments of objects, since we can catch more cases that way.
164 type Address_Clause_Check_Record is record
166 -- The address clause
169 -- The entity of the object overlaying Y
172 -- The entity of the object being overlaid
175 -- Whether the address is offseted within Y
178 package Address_Clause_Checks is new Table.Table (
179 Table_Component_Type => Address_Clause_Check_Record,
180 Table_Index_Type => Int,
181 Table_Low_Bound => 1,
183 Table_Increment => 200,
184 Table_Name => "Address_Clause_Checks");
186 -----------------------------------------
187 -- Adjust_Record_For_Reverse_Bit_Order --
188 -----------------------------------------
190 procedure Adjust_Record_For_Reverse_Bit_Order (R : Entity_Id) is
195 -- Processing depends on version of Ada
197 -- For Ada 95, we just renumber bits within a storage unit. We do the
198 -- same for Ada 83 mode, since we recognize pragma Bit_Order in Ada 83,
199 -- and are free to add this extension.
201 if Ada_Version < Ada_2005 then
202 Comp := First_Component_Or_Discriminant (R);
203 while Present (Comp) loop
204 CC := Component_Clause (Comp);
206 -- If component clause is present, then deal with the non-default
207 -- bit order case for Ada 95 mode.
209 -- We only do this processing for the base type, and in fact that
210 -- is important, since otherwise if there are record subtypes, we
211 -- could reverse the bits once for each subtype, which is wrong.
214 and then Ekind (R) = E_Record_Type
217 CFB : constant Uint := Component_Bit_Offset (Comp);
218 CSZ : constant Uint := Esize (Comp);
219 CLC : constant Node_Id := Component_Clause (Comp);
220 Pos : constant Node_Id := Position (CLC);
221 FB : constant Node_Id := First_Bit (CLC);
223 Storage_Unit_Offset : constant Uint :=
224 CFB / System_Storage_Unit;
226 Start_Bit : constant Uint :=
227 CFB mod System_Storage_Unit;
230 -- Cases where field goes over storage unit boundary
232 if Start_Bit + CSZ > System_Storage_Unit then
234 -- Allow multi-byte field but generate warning
236 if Start_Bit mod System_Storage_Unit = 0
237 and then CSZ mod System_Storage_Unit = 0
240 ("multi-byte field specified with non-standard"
241 & " Bit_Order?", CLC);
243 if Bytes_Big_Endian then
245 ("bytes are not reversed "
246 & "(component is big-endian)?", CLC);
249 ("bytes are not reversed "
250 & "(component is little-endian)?", CLC);
253 -- Do not allow non-contiguous field
257 ("attempt to specify non-contiguous field "
258 & "not permitted", CLC);
260 ("\caused by non-standard Bit_Order "
263 ("\consider possibility of using "
264 & "Ada 2005 mode here", CLC);
267 -- Case where field fits in one storage unit
270 -- Give warning if suspicious component clause
272 if Intval (FB) >= System_Storage_Unit
273 and then Warn_On_Reverse_Bit_Order
276 ("?Bit_Order clause does not affect " &
277 "byte ordering", Pos);
279 Intval (Pos) + Intval (FB) /
282 ("?position normalized to ^ before bit " &
283 "order interpreted", Pos);
286 -- Here is where we fix up the Component_Bit_Offset value
287 -- to account for the reverse bit order. Some examples of
288 -- what needs to be done are:
290 -- First_Bit .. Last_Bit Component_Bit_Offset
302 -- The rule is that the first bit is is obtained by
303 -- subtracting the old ending bit from storage_unit - 1.
305 Set_Component_Bit_Offset
307 (Storage_Unit_Offset * System_Storage_Unit) +
308 (System_Storage_Unit - 1) -
309 (Start_Bit + CSZ - 1));
311 Set_Normalized_First_Bit
313 Component_Bit_Offset (Comp) mod
314 System_Storage_Unit);
319 Next_Component_Or_Discriminant (Comp);
322 -- For Ada 2005, we do machine scalar processing, as fully described In
323 -- AI-133. This involves gathering all components which start at the
324 -- same byte offset and processing them together. Same approach is still
325 -- valid in later versions including Ada 2012.
329 Max_Machine_Scalar_Size : constant Uint :=
331 (Standard_Long_Long_Integer_Size);
332 -- We use this as the maximum machine scalar size
335 SSU : constant Uint := UI_From_Int (System_Storage_Unit);
338 -- This first loop through components does two things. First it
339 -- deals with the case of components with component clauses whose
340 -- length is greater than the maximum machine scalar size (either
341 -- accepting them or rejecting as needed). Second, it counts the
342 -- number of components with component clauses whose length does
343 -- not exceed this maximum for later processing.
346 Comp := First_Component_Or_Discriminant (R);
347 while Present (Comp) loop
348 CC := Component_Clause (Comp);
352 Fbit : constant Uint :=
353 Static_Integer (First_Bit (CC));
356 -- Case of component with size > max machine scalar
358 if Esize (Comp) > Max_Machine_Scalar_Size then
360 -- Must begin on byte boundary
362 if Fbit mod SSU /= 0 then
364 ("illegal first bit value for "
365 & "reverse bit order",
367 Error_Msg_Uint_1 := SSU;
368 Error_Msg_Uint_2 := Max_Machine_Scalar_Size;
371 ("\must be a multiple of ^ "
372 & "if size greater than ^",
375 -- Must end on byte boundary
377 elsif Esize (Comp) mod SSU /= 0 then
379 ("illegal last bit value for "
380 & "reverse bit order",
382 Error_Msg_Uint_1 := SSU;
383 Error_Msg_Uint_2 := Max_Machine_Scalar_Size;
386 ("\must be a multiple of ^ if size "
390 -- OK, give warning if enabled
392 elsif Warn_On_Reverse_Bit_Order then
394 ("multi-byte field specified with "
395 & " non-standard Bit_Order?", CC);
397 if Bytes_Big_Endian then
399 ("\bytes are not reversed "
400 & "(component is big-endian)?", CC);
403 ("\bytes are not reversed "
404 & "(component is little-endian)?", CC);
408 -- Case where size is not greater than max machine
409 -- scalar. For now, we just count these.
412 Num_CC := Num_CC + 1;
417 Next_Component_Or_Discriminant (Comp);
420 -- We need to sort the component clauses on the basis of the
421 -- Position values in the clause, so we can group clauses with
422 -- the same Position. together to determine the relevant machine
426 Comps : array (0 .. Num_CC) of Entity_Id;
427 -- Array to collect component and discriminant entities. The
428 -- data starts at index 1, the 0'th entry is for the sort
431 function CP_Lt (Op1, Op2 : Natural) return Boolean;
432 -- Compare routine for Sort
434 procedure CP_Move (From : Natural; To : Natural);
435 -- Move routine for Sort
437 package Sorting is new GNAT.Heap_Sort_G (CP_Move, CP_Lt);
441 -- Start and stop positions in the component list of the set of
442 -- components with the same starting position (that constitute
443 -- components in a single machine scalar).
446 -- Maximum last bit value of any component in this set
449 -- Corresponding machine scalar size
455 function CP_Lt (Op1, Op2 : Natural) return Boolean is
457 return Position (Component_Clause (Comps (Op1))) <
458 Position (Component_Clause (Comps (Op2)));
465 procedure CP_Move (From : Natural; To : Natural) is
467 Comps (To) := Comps (From);
470 -- Start of processing for Sort_CC
473 -- Collect the component clauses
476 Comp := First_Component_Or_Discriminant (R);
477 while Present (Comp) loop
478 if Present (Component_Clause (Comp))
479 and then Esize (Comp) <= Max_Machine_Scalar_Size
481 Num_CC := Num_CC + 1;
482 Comps (Num_CC) := Comp;
485 Next_Component_Or_Discriminant (Comp);
488 -- Sort by ascending position number
490 Sorting.Sort (Num_CC);
492 -- We now have all the components whose size does not exceed
493 -- the max machine scalar value, sorted by starting position.
494 -- In this loop we gather groups of clauses starting at the
495 -- same position, to process them in accordance with AI-133.
498 while Stop < Num_CC loop
503 (Last_Bit (Component_Clause (Comps (Start))));
504 while Stop < Num_CC loop
506 (Position (Component_Clause (Comps (Stop + 1)))) =
508 (Position (Component_Clause (Comps (Stop))))
516 (Component_Clause (Comps (Stop)))));
522 -- Now we have a group of component clauses from Start to
523 -- Stop whose positions are identical, and MaxL is the
524 -- maximum last bit value of any of these components.
526 -- We need to determine the corresponding machine scalar
527 -- size. This loop assumes that machine scalar sizes are
528 -- even, and that each possible machine scalar has twice
529 -- as many bits as the next smaller one.
531 MSS := Max_Machine_Scalar_Size;
533 and then (MSS / 2) >= SSU
534 and then (MSS / 2) > MaxL
539 -- Here is where we fix up the Component_Bit_Offset value
540 -- to account for the reverse bit order. Some examples of
541 -- what needs to be done for the case of a machine scalar
544 -- First_Bit .. Last_Bit Component_Bit_Offset
556 -- The rule is that the first bit is obtained by subtracting
557 -- the old ending bit from machine scalar size - 1.
559 for C in Start .. Stop loop
561 Comp : constant Entity_Id := Comps (C);
562 CC : constant Node_Id :=
563 Component_Clause (Comp);
564 LB : constant Uint :=
565 Static_Integer (Last_Bit (CC));
566 NFB : constant Uint := MSS - Uint_1 - LB;
567 NLB : constant Uint := NFB + Esize (Comp) - 1;
568 Pos : constant Uint :=
569 Static_Integer (Position (CC));
572 if Warn_On_Reverse_Bit_Order then
573 Error_Msg_Uint_1 := MSS;
575 ("info: reverse bit order in machine " &
576 "scalar of length^?", First_Bit (CC));
577 Error_Msg_Uint_1 := NFB;
578 Error_Msg_Uint_2 := NLB;
580 if Bytes_Big_Endian then
582 ("?\info: big-endian range for "
583 & "component & is ^ .. ^",
584 First_Bit (CC), Comp);
587 ("?\info: little-endian range "
588 & "for component & is ^ .. ^",
589 First_Bit (CC), Comp);
593 Set_Component_Bit_Offset (Comp, Pos * SSU + NFB);
594 Set_Normalized_First_Bit (Comp, NFB mod SSU);
601 end Adjust_Record_For_Reverse_Bit_Order;
603 --------------------------------------
604 -- Alignment_Check_For_Esize_Change --
605 --------------------------------------
607 procedure Alignment_Check_For_Esize_Change (Typ : Entity_Id) is
609 -- If the alignment is known, and not set by a rep clause, and is
610 -- inconsistent with the size being set, then reset it to unknown,
611 -- we assume in this case that the size overrides the inherited
612 -- alignment, and that the alignment must be recomputed.
614 if Known_Alignment (Typ)
615 and then not Has_Alignment_Clause (Typ)
616 and then Esize (Typ) mod (Alignment (Typ) * SSU) /= 0
618 Init_Alignment (Typ);
620 end Alignment_Check_For_Esize_Change;
622 -----------------------
623 -- Analyze_At_Clause --
624 -----------------------
626 -- An at clause is replaced by the corresponding Address attribute
627 -- definition clause that is the preferred approach in Ada 95.
629 procedure Analyze_At_Clause (N : Node_Id) is
630 CS : constant Boolean := Comes_From_Source (N);
633 -- This is an obsolescent feature
635 Check_Restriction (No_Obsolescent_Features, N);
637 if Warn_On_Obsolescent_Feature then
639 ("at clause is an obsolescent feature (RM J.7(2))?", N);
641 ("\use address attribute definition clause instead?", N);
644 -- Rewrite as address clause
647 Make_Attribute_Definition_Clause (Sloc (N),
648 Name => Identifier (N),
649 Chars => Name_Address,
650 Expression => Expression (N)));
652 -- We preserve Comes_From_Source, since logically the clause still
653 -- comes from the source program even though it is changed in form.
655 Set_Comes_From_Source (N, CS);
657 -- Analyze rewritten clause
659 Analyze_Attribute_Definition_Clause (N);
660 end Analyze_At_Clause;
662 -----------------------------------------
663 -- Analyze_Attribute_Definition_Clause --
664 -----------------------------------------
666 procedure Analyze_Attribute_Definition_Clause (N : Node_Id) is
667 Loc : constant Source_Ptr := Sloc (N);
668 Nam : constant Node_Id := Name (N);
669 Attr : constant Name_Id := Chars (N);
670 Expr : constant Node_Id := Expression (N);
671 Id : constant Attribute_Id := Get_Attribute_Id (Attr);
675 FOnly : Boolean := False;
676 -- Reset to True for subtype specific attribute (Alignment, Size)
677 -- and for stream attributes, i.e. those cases where in the call
678 -- to Rep_Item_Too_Late, FOnly is set True so that only the freezing
679 -- rules are checked. Note that the case of stream attributes is not
680 -- clear from the RM, but see AI95-00137. Also, the RM seems to
681 -- disallow Storage_Size for derived task types, but that is also
682 -- clearly unintentional.
684 procedure Analyze_Stream_TSS_Definition (TSS_Nam : TSS_Name_Type);
685 -- Common processing for 'Read, 'Write, 'Input and 'Output attribute
686 -- definition clauses.
688 -----------------------------------
689 -- Analyze_Stream_TSS_Definition --
690 -----------------------------------
692 procedure Analyze_Stream_TSS_Definition (TSS_Nam : TSS_Name_Type) is
693 Subp : Entity_Id := Empty;
698 Is_Read : constant Boolean := (TSS_Nam = TSS_Stream_Read);
700 function Has_Good_Profile (Subp : Entity_Id) return Boolean;
701 -- Return true if the entity is a subprogram with an appropriate
702 -- profile for the attribute being defined.
704 ----------------------
705 -- Has_Good_Profile --
706 ----------------------
708 function Has_Good_Profile (Subp : Entity_Id) return Boolean is
710 Is_Function : constant Boolean := (TSS_Nam = TSS_Stream_Input);
711 Expected_Ekind : constant array (Boolean) of Entity_Kind :=
712 (False => E_Procedure, True => E_Function);
716 if Ekind (Subp) /= Expected_Ekind (Is_Function) then
720 F := First_Formal (Subp);
723 or else Ekind (Etype (F)) /= E_Anonymous_Access_Type
724 or else Designated_Type (Etype (F)) /=
725 Class_Wide_Type (RTE (RE_Root_Stream_Type))
730 if not Is_Function then
734 Expected_Mode : constant array (Boolean) of Entity_Kind :=
735 (False => E_In_Parameter,
736 True => E_Out_Parameter);
738 if Parameter_Mode (F) /= Expected_Mode (Is_Read) then
749 return Base_Type (Typ) = Base_Type (Ent)
750 and then No (Next_Formal (F));
751 end Has_Good_Profile;
753 -- Start of processing for Analyze_Stream_TSS_Definition
758 if not Is_Type (U_Ent) then
759 Error_Msg_N ("local name must be a subtype", Nam);
763 Pnam := TSS (Base_Type (U_Ent), TSS_Nam);
765 -- If Pnam is present, it can be either inherited from an ancestor
766 -- type (in which case it is legal to redefine it for this type), or
767 -- be a previous definition of the attribute for the same type (in
768 -- which case it is illegal).
770 -- In the first case, it will have been analyzed already, and we
771 -- can check that its profile does not match the expected profile
772 -- for a stream attribute of U_Ent. In the second case, either Pnam
773 -- has been analyzed (and has the expected profile), or it has not
774 -- been analyzed yet (case of a type that has not been frozen yet
775 -- and for which the stream attribute has been set using Set_TSS).
778 and then (No (First_Entity (Pnam)) or else Has_Good_Profile (Pnam))
780 Error_Msg_Sloc := Sloc (Pnam);
781 Error_Msg_Name_1 := Attr;
782 Error_Msg_N ("% attribute already defined #", Nam);
788 if Is_Entity_Name (Expr) then
789 if not Is_Overloaded (Expr) then
790 if Has_Good_Profile (Entity (Expr)) then
791 Subp := Entity (Expr);
795 Get_First_Interp (Expr, I, It);
796 while Present (It.Nam) loop
797 if Has_Good_Profile (It.Nam) then
802 Get_Next_Interp (I, It);
807 if Present (Subp) then
808 if Is_Abstract_Subprogram (Subp) then
809 Error_Msg_N ("stream subprogram must not be abstract", Expr);
813 Set_Entity (Expr, Subp);
814 Set_Etype (Expr, Etype (Subp));
816 New_Stream_Subprogram (N, U_Ent, Subp, TSS_Nam);
819 Error_Msg_Name_1 := Attr;
820 Error_Msg_N ("incorrect expression for% attribute", Expr);
822 end Analyze_Stream_TSS_Definition;
824 -- Start of processing for Analyze_Attribute_Definition_Clause
827 -- Process Ignore_Rep_Clauses option
829 if Ignore_Rep_Clauses then
832 -- The following should be ignored. They do not affect legality
833 -- and may be target dependent. The basic idea of -gnatI is to
834 -- ignore any rep clauses that may be target dependent but do not
835 -- affect legality (except possibly to be rejected because they
836 -- are incompatible with the compilation target).
838 when Attribute_Alignment |
839 Attribute_Bit_Order |
840 Attribute_Component_Size |
841 Attribute_Machine_Radix |
842 Attribute_Object_Size |
845 Attribute_Stream_Size |
846 Attribute_Value_Size =>
848 Rewrite (N, Make_Null_Statement (Sloc (N)));
851 -- The following should not be ignored, because in the first place
852 -- they are reasonably portable, and should not cause problems in
853 -- compiling code from another target, and also they do affect
854 -- legality, e.g. failing to provide a stream attribute for a
855 -- type may make a program illegal.
857 when Attribute_External_Tag |
861 Attribute_Storage_Pool |
862 Attribute_Storage_Size |
866 -- Other cases are errors ("attribute& cannot be set with
867 -- definition clause"), which will be caught below.
877 if Rep_Item_Too_Early (Ent, N) then
881 -- Rep clause applies to full view of incomplete type or private type if
882 -- we have one (if not, this is a premature use of the type). However,
883 -- certain semantic checks need to be done on the specified entity (i.e.
884 -- the private view), so we save it in Ent.
886 if Is_Private_Type (Ent)
887 and then Is_Derived_Type (Ent)
888 and then not Is_Tagged_Type (Ent)
889 and then No (Full_View (Ent))
891 -- If this is a private type whose completion is a derivation from
892 -- another private type, there is no full view, and the attribute
893 -- belongs to the type itself, not its underlying parent.
897 elsif Ekind (Ent) = E_Incomplete_Type then
899 -- The attribute applies to the full view, set the entity of the
900 -- attribute definition accordingly.
902 Ent := Underlying_Type (Ent);
904 Set_Entity (Nam, Ent);
907 U_Ent := Underlying_Type (Ent);
910 -- Complete other routine error checks
912 if Etype (Nam) = Any_Type then
915 elsif Scope (Ent) /= Current_Scope then
916 Error_Msg_N ("entity must be declared in this scope", Nam);
919 elsif No (U_Ent) then
922 elsif Is_Type (U_Ent)
923 and then not Is_First_Subtype (U_Ent)
924 and then Id /= Attribute_Object_Size
925 and then Id /= Attribute_Value_Size
926 and then not From_At_Mod (N)
928 Error_Msg_N ("cannot specify attribute for subtype", Nam);
932 -- Switch on particular attribute
940 -- Address attribute definition clause
942 when Attribute_Address => Address : begin
944 -- A little error check, catch for X'Address use X'Address;
946 if Nkind (Nam) = N_Identifier
947 and then Nkind (Expr) = N_Attribute_Reference
948 and then Attribute_Name (Expr) = Name_Address
949 and then Nkind (Prefix (Expr)) = N_Identifier
950 and then Chars (Nam) = Chars (Prefix (Expr))
953 ("address for & is self-referencing", Prefix (Expr), Ent);
957 -- Not that special case, carry on with analysis of expression
959 Analyze_And_Resolve (Expr, RTE (RE_Address));
961 -- Even when ignoring rep clauses we need to indicate that the
962 -- entity has an address clause and thus it is legal to declare
965 if Ignore_Rep_Clauses then
966 if Ekind_In (U_Ent, E_Variable, E_Constant) then
967 Record_Rep_Item (U_Ent, N);
973 if Present (Address_Clause (U_Ent)) then
974 Error_Msg_N ("address already given for &", Nam);
976 -- Case of address clause for subprogram
978 elsif Is_Subprogram (U_Ent) then
979 if Has_Homonym (U_Ent) then
981 ("address clause cannot be given " &
982 "for overloaded subprogram",
987 -- For subprograms, all address clauses are permitted, and we
988 -- mark the subprogram as having a deferred freeze so that Gigi
989 -- will not elaborate it too soon.
991 -- Above needs more comments, what is too soon about???
993 Set_Has_Delayed_Freeze (U_Ent);
995 -- Case of address clause for entry
997 elsif Ekind (U_Ent) = E_Entry then
998 if Nkind (Parent (N)) = N_Task_Body then
1000 ("entry address must be specified in task spec", Nam);
1004 -- For entries, we require a constant address
1006 Check_Constant_Address_Clause (Expr, U_Ent);
1008 -- Special checks for task types
1010 if Is_Task_Type (Scope (U_Ent))
1011 and then Comes_From_Source (Scope (U_Ent))
1014 ("?entry address declared for entry in task type", N);
1016 ("\?only one task can be declared of this type", N);
1019 -- Entry address clauses are obsolescent
1021 Check_Restriction (No_Obsolescent_Features, N);
1023 if Warn_On_Obsolescent_Feature then
1025 ("attaching interrupt to task entry is an " &
1026 "obsolescent feature (RM J.7.1)?", N);
1028 ("\use interrupt procedure instead?", N);
1031 -- Case of an address clause for a controlled object which we
1032 -- consider to be erroneous.
1034 elsif Is_Controlled (Etype (U_Ent))
1035 or else Has_Controlled_Component (Etype (U_Ent))
1038 ("?controlled object& must not be overlaid", Nam, U_Ent);
1040 ("\?Program_Error will be raised at run time", Nam);
1041 Insert_Action (Declaration_Node (U_Ent),
1042 Make_Raise_Program_Error (Loc,
1043 Reason => PE_Overlaid_Controlled_Object));
1046 -- Case of address clause for a (non-controlled) object
1049 Ekind (U_Ent) = E_Variable
1051 Ekind (U_Ent) = E_Constant
1054 Expr : constant Node_Id := Expression (N);
1059 -- Exported variables cannot have an address clause, because
1060 -- this cancels the effect of the pragma Export.
1062 if Is_Exported (U_Ent) then
1064 ("cannot export object with address clause", Nam);
1068 Find_Overlaid_Entity (N, O_Ent, Off);
1070 -- Overlaying controlled objects is erroneous
1073 and then (Has_Controlled_Component (Etype (O_Ent))
1074 or else Is_Controlled (Etype (O_Ent)))
1077 ("?cannot overlay with controlled object", Expr);
1079 ("\?Program_Error will be raised at run time", Expr);
1080 Insert_Action (Declaration_Node (U_Ent),
1081 Make_Raise_Program_Error (Loc,
1082 Reason => PE_Overlaid_Controlled_Object));
1085 elsif Present (O_Ent)
1086 and then Ekind (U_Ent) = E_Constant
1087 and then not Is_Constant_Object (O_Ent)
1089 Error_Msg_N ("constant overlays a variable?", Expr);
1091 elsif Present (Renamed_Object (U_Ent)) then
1093 ("address clause not allowed"
1094 & " for a renaming declaration (RM 13.1(6))", Nam);
1097 -- Imported variables can have an address clause, but then
1098 -- the import is pretty meaningless except to suppress
1099 -- initializations, so we do not need such variables to
1100 -- be statically allocated (and in fact it causes trouble
1101 -- if the address clause is a local value).
1103 elsif Is_Imported (U_Ent) then
1104 Set_Is_Statically_Allocated (U_Ent, False);
1107 -- We mark a possible modification of a variable with an
1108 -- address clause, since it is likely aliasing is occurring.
1110 Note_Possible_Modification (Nam, Sure => False);
1112 -- Here we are checking for explicit overlap of one variable
1113 -- by another, and if we find this then mark the overlapped
1114 -- variable as also being volatile to prevent unwanted
1115 -- optimizations. This is a significant pessimization so
1116 -- avoid it when there is an offset, i.e. when the object
1117 -- is composite; they cannot be optimized easily anyway.
1120 and then Is_Object (O_Ent)
1123 Set_Treat_As_Volatile (O_Ent);
1126 -- Legality checks on the address clause for initialized
1127 -- objects is deferred until the freeze point, because
1128 -- a subsequent pragma might indicate that the object is
1129 -- imported and thus not initialized.
1131 Set_Has_Delayed_Freeze (U_Ent);
1133 -- If an initialization call has been generated for this
1134 -- object, it needs to be deferred to after the freeze node
1135 -- we have just now added, otherwise GIGI will see a
1136 -- reference to the variable (as actual to the IP call)
1137 -- before its definition.
1140 Init_Call : constant Node_Id := Find_Init_Call (U_Ent, N);
1142 if Present (Init_Call) then
1144 Append_Freeze_Action (U_Ent, Init_Call);
1148 if Is_Exported (U_Ent) then
1150 ("& cannot be exported if an address clause is given",
1153 ("\define and export a variable " &
1154 "that holds its address instead",
1158 -- Entity has delayed freeze, so we will generate an
1159 -- alignment check at the freeze point unless suppressed.
1161 if not Range_Checks_Suppressed (U_Ent)
1162 and then not Alignment_Checks_Suppressed (U_Ent)
1164 Set_Check_Address_Alignment (N);
1167 -- Kill the size check code, since we are not allocating
1168 -- the variable, it is somewhere else.
1170 Kill_Size_Check_Code (U_Ent);
1172 -- If the address clause is of the form:
1174 -- for Y'Address use X'Address
1178 -- Const : constant Address := X'Address;
1180 -- for Y'Address use Const;
1182 -- then we make an entry in the table for checking the size
1183 -- and alignment of the overlaying variable. We defer this
1184 -- check till after code generation to take full advantage
1185 -- of the annotation done by the back end. This entry is
1186 -- only made if the address clause comes from source.
1187 -- If the entity has a generic type, the check will be
1188 -- performed in the instance if the actual type justifies
1189 -- it, and we do not insert the clause in the table to
1190 -- prevent spurious warnings.
1192 if Address_Clause_Overlay_Warnings
1193 and then Comes_From_Source (N)
1194 and then Present (O_Ent)
1195 and then Is_Object (O_Ent)
1197 if not Is_Generic_Type (Etype (U_Ent)) then
1198 Address_Clause_Checks.Append ((N, U_Ent, O_Ent, Off));
1201 -- If variable overlays a constant view, and we are
1202 -- warning on overlays, then mark the variable as
1203 -- overlaying a constant (we will give warnings later
1204 -- if this variable is assigned).
1206 if Is_Constant_Object (O_Ent)
1207 and then Ekind (U_Ent) = E_Variable
1209 Set_Overlays_Constant (U_Ent);
1214 -- Not a valid entity for an address clause
1217 Error_Msg_N ("address cannot be given for &", Nam);
1225 -- Alignment attribute definition clause
1227 when Attribute_Alignment => Alignment : declare
1228 Align : constant Uint := Get_Alignment_Value (Expr);
1233 if not Is_Type (U_Ent)
1234 and then Ekind (U_Ent) /= E_Variable
1235 and then Ekind (U_Ent) /= E_Constant
1237 Error_Msg_N ("alignment cannot be given for &", Nam);
1239 elsif Has_Alignment_Clause (U_Ent) then
1240 Error_Msg_Sloc := Sloc (Alignment_Clause (U_Ent));
1241 Error_Msg_N ("alignment clause previously given#", N);
1243 elsif Align /= No_Uint then
1244 Set_Has_Alignment_Clause (U_Ent);
1245 Set_Alignment (U_Ent, Align);
1247 -- For an array type, U_Ent is the first subtype. In that case,
1248 -- also set the alignment of the anonymous base type so that
1249 -- other subtypes (such as the itypes for aggregates of the
1250 -- type) also receive the expected alignment.
1252 if Is_Array_Type (U_Ent) then
1253 Set_Alignment (Base_Type (U_Ent), Align);
1262 -- Bit_Order attribute definition clause
1264 when Attribute_Bit_Order => Bit_Order : declare
1266 if not Is_Record_Type (U_Ent) then
1268 ("Bit_Order can only be defined for record type", Nam);
1271 Analyze_And_Resolve (Expr, RTE (RE_Bit_Order));
1273 if Etype (Expr) = Any_Type then
1276 elsif not Is_Static_Expression (Expr) then
1277 Flag_Non_Static_Expr
1278 ("Bit_Order requires static expression!", Expr);
1281 if (Expr_Value (Expr) = 0) /= Bytes_Big_Endian then
1282 Set_Reverse_Bit_Order (U_Ent, True);
1288 --------------------
1289 -- Component_Size --
1290 --------------------
1292 -- Component_Size attribute definition clause
1294 when Attribute_Component_Size => Component_Size_Case : declare
1295 Csize : constant Uint := Static_Integer (Expr);
1299 New_Ctyp : Entity_Id;
1303 if not Is_Array_Type (U_Ent) then
1304 Error_Msg_N ("component size requires array type", Nam);
1308 Btype := Base_Type (U_Ent);
1309 Ctyp := Component_Type (Btype);
1311 if Has_Component_Size_Clause (Btype) then
1313 ("component size clause for& previously given", Nam);
1315 elsif Csize /= No_Uint then
1316 Check_Size (Expr, Ctyp, Csize, Biased);
1318 if Has_Aliased_Components (Btype)
1321 and then Csize /= 16
1324 ("component size incorrect for aliased components", N);
1328 -- For the biased case, build a declaration for a subtype
1329 -- that will be used to represent the biased subtype that
1330 -- reflects the biased representation of components. We need
1331 -- this subtype to get proper conversions on referencing
1332 -- elements of the array. Note that component size clauses
1333 -- are ignored in VM mode.
1335 if VM_Target = No_VM then
1338 Make_Defining_Identifier (Loc,
1340 New_External_Name (Chars (U_Ent), 'C', 0, 'T'));
1343 Make_Subtype_Declaration (Loc,
1344 Defining_Identifier => New_Ctyp,
1345 Subtype_Indication =>
1346 New_Occurrence_Of (Component_Type (Btype), Loc));
1348 Set_Parent (Decl, N);
1349 Analyze (Decl, Suppress => All_Checks);
1351 Set_Has_Delayed_Freeze (New_Ctyp, False);
1352 Set_Esize (New_Ctyp, Csize);
1353 Set_RM_Size (New_Ctyp, Csize);
1354 Init_Alignment (New_Ctyp);
1355 Set_Is_Itype (New_Ctyp, True);
1356 Set_Associated_Node_For_Itype (New_Ctyp, U_Ent);
1358 Set_Component_Type (Btype, New_Ctyp);
1359 Set_Biased (New_Ctyp, N, "component size clause");
1362 Set_Component_Size (Btype, Csize);
1364 -- For VM case, we ignore component size clauses
1367 -- Give a warning unless we are in GNAT mode, in which case
1368 -- the warning is suppressed since it is not useful.
1370 if not GNAT_Mode then
1372 ("?component size ignored in this configuration", N);
1376 -- Deal with warning on overridden size
1378 if Warn_On_Overridden_Size
1379 and then Has_Size_Clause (Ctyp)
1380 and then RM_Size (Ctyp) /= Csize
1383 ("?component size overrides size clause for&",
1387 Set_Has_Component_Size_Clause (Btype, True);
1388 Set_Has_Non_Standard_Rep (Btype, True);
1390 end Component_Size_Case;
1396 when Attribute_External_Tag => External_Tag :
1398 if not Is_Tagged_Type (U_Ent) then
1399 Error_Msg_N ("should be a tagged type", Nam);
1402 Analyze_And_Resolve (Expr, Standard_String);
1404 if not Is_Static_Expression (Expr) then
1405 Flag_Non_Static_Expr
1406 ("static string required for tag name!", Nam);
1409 if VM_Target = No_VM then
1410 Set_Has_External_Tag_Rep_Clause (U_Ent);
1412 Error_Msg_Name_1 := Attr;
1414 ("% attribute unsupported in this configuration", Nam);
1417 if not Is_Library_Level_Entity (U_Ent) then
1419 ("?non-unique external tag supplied for &", N, U_Ent);
1421 ("?\same external tag applies to all subprogram calls", N);
1423 ("?\corresponding internal tag cannot be obtained", N);
1431 when Attribute_Input =>
1432 Analyze_Stream_TSS_Definition (TSS_Stream_Input);
1433 Set_Has_Specified_Stream_Input (Ent);
1439 -- Machine radix attribute definition clause
1441 when Attribute_Machine_Radix => Machine_Radix : declare
1442 Radix : constant Uint := Static_Integer (Expr);
1445 if not Is_Decimal_Fixed_Point_Type (U_Ent) then
1446 Error_Msg_N ("decimal fixed-point type expected for &", Nam);
1448 elsif Has_Machine_Radix_Clause (U_Ent) then
1449 Error_Msg_Sloc := Sloc (Alignment_Clause (U_Ent));
1450 Error_Msg_N ("machine radix clause previously given#", N);
1452 elsif Radix /= No_Uint then
1453 Set_Has_Machine_Radix_Clause (U_Ent);
1454 Set_Has_Non_Standard_Rep (Base_Type (U_Ent));
1458 elsif Radix = 10 then
1459 Set_Machine_Radix_10 (U_Ent);
1461 Error_Msg_N ("machine radix value must be 2 or 10", Expr);
1470 -- Object_Size attribute definition clause
1472 when Attribute_Object_Size => Object_Size : declare
1473 Size : constant Uint := Static_Integer (Expr);
1476 pragma Warnings (Off, Biased);
1479 if not Is_Type (U_Ent) then
1480 Error_Msg_N ("Object_Size cannot be given for &", Nam);
1482 elsif Has_Object_Size_Clause (U_Ent) then
1483 Error_Msg_N ("Object_Size already given for &", Nam);
1486 Check_Size (Expr, U_Ent, Size, Biased);
1494 UI_Mod (Size, 64) /= 0
1497 ("Object_Size must be 8, 16, 32, or multiple of 64",
1501 Set_Esize (U_Ent, Size);
1502 Set_Has_Object_Size_Clause (U_Ent);
1503 Alignment_Check_For_Esize_Change (U_Ent);
1511 when Attribute_Output =>
1512 Analyze_Stream_TSS_Definition (TSS_Stream_Output);
1513 Set_Has_Specified_Stream_Output (Ent);
1519 when Attribute_Read =>
1520 Analyze_Stream_TSS_Definition (TSS_Stream_Read);
1521 Set_Has_Specified_Stream_Read (Ent);
1527 -- Size attribute definition clause
1529 when Attribute_Size => Size : declare
1530 Size : constant Uint := Static_Integer (Expr);
1537 if Has_Size_Clause (U_Ent) then
1538 Error_Msg_N ("size already given for &", Nam);
1540 elsif not Is_Type (U_Ent)
1541 and then Ekind (U_Ent) /= E_Variable
1542 and then Ekind (U_Ent) /= E_Constant
1544 Error_Msg_N ("size cannot be given for &", Nam);
1546 elsif Is_Array_Type (U_Ent)
1547 and then not Is_Constrained (U_Ent)
1550 ("size cannot be given for unconstrained array", Nam);
1552 elsif Size /= No_Uint then
1554 if VM_Target /= No_VM and then not GNAT_Mode then
1556 -- Size clause is not handled properly on VM targets.
1557 -- Display a warning unless we are in GNAT mode, in which
1558 -- case this is useless.
1561 ("?size clauses are ignored in this configuration", N);
1564 if Is_Type (U_Ent) then
1567 Etyp := Etype (U_Ent);
1570 -- Check size, note that Gigi is in charge of checking that the
1571 -- size of an array or record type is OK. Also we do not check
1572 -- the size in the ordinary fixed-point case, since it is too
1573 -- early to do so (there may be subsequent small clause that
1574 -- affects the size). We can check the size if a small clause
1575 -- has already been given.
1577 if not Is_Ordinary_Fixed_Point_Type (U_Ent)
1578 or else Has_Small_Clause (U_Ent)
1580 Check_Size (Expr, Etyp, Size, Biased);
1581 Set_Biased (U_Ent, N, "size clause", Biased);
1584 -- For types set RM_Size and Esize if possible
1586 if Is_Type (U_Ent) then
1587 Set_RM_Size (U_Ent, Size);
1589 -- For scalar types, increase Object_Size to power of 2, but
1590 -- not less than a storage unit in any case (i.e., normally
1591 -- this means it will be byte addressable).
1593 if Is_Scalar_Type (U_Ent) then
1594 if Size <= System_Storage_Unit then
1595 Init_Esize (U_Ent, System_Storage_Unit);
1596 elsif Size <= 16 then
1597 Init_Esize (U_Ent, 16);
1598 elsif Size <= 32 then
1599 Init_Esize (U_Ent, 32);
1601 Set_Esize (U_Ent, (Size + 63) / 64 * 64);
1604 -- For all other types, object size = value size. The
1605 -- backend will adjust as needed.
1608 Set_Esize (U_Ent, Size);
1611 Alignment_Check_For_Esize_Change (U_Ent);
1613 -- For objects, set Esize only
1616 if Is_Elementary_Type (Etyp) then
1617 if Size /= System_Storage_Unit
1619 Size /= System_Storage_Unit * 2
1621 Size /= System_Storage_Unit * 4
1623 Size /= System_Storage_Unit * 8
1625 Error_Msg_Uint_1 := UI_From_Int (System_Storage_Unit);
1626 Error_Msg_Uint_2 := Error_Msg_Uint_1 * 8;
1628 ("size for primitive object must be a power of 2"
1629 & " in the range ^-^", N);
1633 Set_Esize (U_Ent, Size);
1636 Set_Has_Size_Clause (U_Ent);
1644 -- Small attribute definition clause
1646 when Attribute_Small => Small : declare
1647 Implicit_Base : constant Entity_Id := Base_Type (U_Ent);
1651 Analyze_And_Resolve (Expr, Any_Real);
1653 if Etype (Expr) = Any_Type then
1656 elsif not Is_Static_Expression (Expr) then
1657 Flag_Non_Static_Expr
1658 ("small requires static expression!", Expr);
1662 Small := Expr_Value_R (Expr);
1664 if Small <= Ureal_0 then
1665 Error_Msg_N ("small value must be greater than zero", Expr);
1671 if not Is_Ordinary_Fixed_Point_Type (U_Ent) then
1673 ("small requires an ordinary fixed point type", Nam);
1675 elsif Has_Small_Clause (U_Ent) then
1676 Error_Msg_N ("small already given for &", Nam);
1678 elsif Small > Delta_Value (U_Ent) then
1680 ("small value must not be greater then delta value", Nam);
1683 Set_Small_Value (U_Ent, Small);
1684 Set_Small_Value (Implicit_Base, Small);
1685 Set_Has_Small_Clause (U_Ent);
1686 Set_Has_Small_Clause (Implicit_Base);
1687 Set_Has_Non_Standard_Rep (Implicit_Base);
1695 -- Storage_Pool attribute definition clause
1697 when Attribute_Storage_Pool => Storage_Pool : declare
1702 if Ekind (U_Ent) = E_Access_Subprogram_Type then
1704 ("storage pool cannot be given for access-to-subprogram type",
1709 Ekind_In (U_Ent, E_Access_Type, E_General_Access_Type)
1712 ("storage pool can only be given for access types", Nam);
1715 elsif Is_Derived_Type (U_Ent) then
1717 ("storage pool cannot be given for a derived access type",
1720 elsif Has_Storage_Size_Clause (U_Ent) then
1721 Error_Msg_N ("storage size already given for &", Nam);
1724 elsif Present (Associated_Storage_Pool (U_Ent)) then
1725 Error_Msg_N ("storage pool already given for &", Nam);
1730 (Expr, Class_Wide_Type (RTE (RE_Root_Storage_Pool)));
1732 if not Denotes_Variable (Expr) then
1733 Error_Msg_N ("storage pool must be a variable", Expr);
1737 if Nkind (Expr) = N_Type_Conversion then
1738 T := Etype (Expression (Expr));
1743 -- The Stack_Bounded_Pool is used internally for implementing
1744 -- access types with a Storage_Size. Since it only work
1745 -- properly when used on one specific type, we need to check
1746 -- that it is not hijacked improperly:
1747 -- type T is access Integer;
1748 -- for T'Storage_Size use n;
1749 -- type Q is access Float;
1750 -- for Q'Storage_Size use T'Storage_Size; -- incorrect
1752 if RTE_Available (RE_Stack_Bounded_Pool)
1753 and then Base_Type (T) = RTE (RE_Stack_Bounded_Pool)
1755 Error_Msg_N ("non-shareable internal Pool", Expr);
1759 -- If the argument is a name that is not an entity name, then
1760 -- we construct a renaming operation to define an entity of
1761 -- type storage pool.
1763 if not Is_Entity_Name (Expr)
1764 and then Is_Object_Reference (Expr)
1766 Pool := Make_Temporary (Loc, 'P', Expr);
1769 Rnode : constant Node_Id :=
1770 Make_Object_Renaming_Declaration (Loc,
1771 Defining_Identifier => Pool,
1773 New_Occurrence_Of (Etype (Expr), Loc),
1777 Insert_Before (N, Rnode);
1779 Set_Associated_Storage_Pool (U_Ent, Pool);
1782 elsif Is_Entity_Name (Expr) then
1783 Pool := Entity (Expr);
1785 -- If pool is a renamed object, get original one. This can
1786 -- happen with an explicit renaming, and within instances.
1788 while Present (Renamed_Object (Pool))
1789 and then Is_Entity_Name (Renamed_Object (Pool))
1791 Pool := Entity (Renamed_Object (Pool));
1794 if Present (Renamed_Object (Pool))
1795 and then Nkind (Renamed_Object (Pool)) = N_Type_Conversion
1796 and then Is_Entity_Name (Expression (Renamed_Object (Pool)))
1798 Pool := Entity (Expression (Renamed_Object (Pool)));
1801 Set_Associated_Storage_Pool (U_Ent, Pool);
1803 elsif Nkind (Expr) = N_Type_Conversion
1804 and then Is_Entity_Name (Expression (Expr))
1805 and then Nkind (Original_Node (Expr)) = N_Attribute_Reference
1807 Pool := Entity (Expression (Expr));
1808 Set_Associated_Storage_Pool (U_Ent, Pool);
1811 Error_Msg_N ("incorrect reference to a Storage Pool", Expr);
1820 -- Storage_Size attribute definition clause
1822 when Attribute_Storage_Size => Storage_Size : declare
1823 Btype : constant Entity_Id := Base_Type (U_Ent);
1827 if Is_Task_Type (U_Ent) then
1828 Check_Restriction (No_Obsolescent_Features, N);
1830 if Warn_On_Obsolescent_Feature then
1832 ("storage size clause for task is an " &
1833 "obsolescent feature (RM J.9)?", N);
1834 Error_Msg_N ("\use Storage_Size pragma instead?", N);
1840 if not Is_Access_Type (U_Ent)
1841 and then Ekind (U_Ent) /= E_Task_Type
1843 Error_Msg_N ("storage size cannot be given for &", Nam);
1845 elsif Is_Access_Type (U_Ent) and Is_Derived_Type (U_Ent) then
1847 ("storage size cannot be given for a derived access type",
1850 elsif Has_Storage_Size_Clause (Btype) then
1851 Error_Msg_N ("storage size already given for &", Nam);
1854 Analyze_And_Resolve (Expr, Any_Integer);
1856 if Is_Access_Type (U_Ent) then
1857 if Present (Associated_Storage_Pool (U_Ent)) then
1858 Error_Msg_N ("storage pool already given for &", Nam);
1862 if Is_OK_Static_Expression (Expr)
1863 and then Expr_Value (Expr) = 0
1865 Set_No_Pool_Assigned (Btype);
1868 else -- Is_Task_Type (U_Ent)
1869 Sprag := Get_Rep_Pragma (Btype, Name_Storage_Size);
1871 if Present (Sprag) then
1872 Error_Msg_Sloc := Sloc (Sprag);
1874 ("Storage_Size already specified#", Nam);
1879 Set_Has_Storage_Size_Clause (Btype);
1887 when Attribute_Stream_Size => Stream_Size : declare
1888 Size : constant Uint := Static_Integer (Expr);
1891 if Ada_Version <= Ada_95 then
1892 Check_Restriction (No_Implementation_Attributes, N);
1895 if Has_Stream_Size_Clause (U_Ent) then
1896 Error_Msg_N ("Stream_Size already given for &", Nam);
1898 elsif Is_Elementary_Type (U_Ent) then
1899 if Size /= System_Storage_Unit
1901 Size /= System_Storage_Unit * 2
1903 Size /= System_Storage_Unit * 4
1905 Size /= System_Storage_Unit * 8
1907 Error_Msg_Uint_1 := UI_From_Int (System_Storage_Unit);
1909 ("stream size for elementary type must be a"
1910 & " power of 2 and at least ^", N);
1912 elsif RM_Size (U_Ent) > Size then
1913 Error_Msg_Uint_1 := RM_Size (U_Ent);
1915 ("stream size for elementary type must be a"
1916 & " power of 2 and at least ^", N);
1919 Set_Has_Stream_Size_Clause (U_Ent);
1922 Error_Msg_N ("Stream_Size cannot be given for &", Nam);
1930 -- Value_Size attribute definition clause
1932 when Attribute_Value_Size => Value_Size : declare
1933 Size : constant Uint := Static_Integer (Expr);
1937 if not Is_Type (U_Ent) then
1938 Error_Msg_N ("Value_Size cannot be given for &", Nam);
1941 (Get_Attribute_Definition_Clause
1942 (U_Ent, Attribute_Value_Size))
1944 Error_Msg_N ("Value_Size already given for &", Nam);
1946 elsif Is_Array_Type (U_Ent)
1947 and then not Is_Constrained (U_Ent)
1950 ("Value_Size cannot be given for unconstrained array", Nam);
1953 if Is_Elementary_Type (U_Ent) then
1954 Check_Size (Expr, U_Ent, Size, Biased);
1955 Set_Biased (U_Ent, N, "value size clause", Biased);
1958 Set_RM_Size (U_Ent, Size);
1966 when Attribute_Write =>
1967 Analyze_Stream_TSS_Definition (TSS_Stream_Write);
1968 Set_Has_Specified_Stream_Write (Ent);
1970 -- All other attributes cannot be set
1974 ("attribute& cannot be set with definition clause", N);
1977 -- The test for the type being frozen must be performed after
1978 -- any expression the clause has been analyzed since the expression
1979 -- itself might cause freezing that makes the clause illegal.
1981 if Rep_Item_Too_Late (U_Ent, N, FOnly) then
1984 end Analyze_Attribute_Definition_Clause;
1986 ----------------------------
1987 -- Analyze_Code_Statement --
1988 ----------------------------
1990 procedure Analyze_Code_Statement (N : Node_Id) is
1991 HSS : constant Node_Id := Parent (N);
1992 SBody : constant Node_Id := Parent (HSS);
1993 Subp : constant Entity_Id := Current_Scope;
2000 -- Analyze and check we get right type, note that this implements the
2001 -- requirement (RM 13.8(1)) that Machine_Code be with'ed, since that
2002 -- is the only way that Asm_Insn could possibly be visible.
2004 Analyze_And_Resolve (Expression (N));
2006 if Etype (Expression (N)) = Any_Type then
2008 elsif Etype (Expression (N)) /= RTE (RE_Asm_Insn) then
2009 Error_Msg_N ("incorrect type for code statement", N);
2013 Check_Code_Statement (N);
2015 -- Make sure we appear in the handled statement sequence of a
2016 -- subprogram (RM 13.8(3)).
2018 if Nkind (HSS) /= N_Handled_Sequence_Of_Statements
2019 or else Nkind (SBody) /= N_Subprogram_Body
2022 ("code statement can only appear in body of subprogram", N);
2026 -- Do remaining checks (RM 13.8(3)) if not already done
2028 if not Is_Machine_Code_Subprogram (Subp) then
2029 Set_Is_Machine_Code_Subprogram (Subp);
2031 -- No exception handlers allowed
2033 if Present (Exception_Handlers (HSS)) then
2035 ("exception handlers not permitted in machine code subprogram",
2036 First (Exception_Handlers (HSS)));
2039 -- No declarations other than use clauses and pragmas (we allow
2040 -- certain internally generated declarations as well).
2042 Decl := First (Declarations (SBody));
2043 while Present (Decl) loop
2044 DeclO := Original_Node (Decl);
2045 if Comes_From_Source (DeclO)
2046 and not Nkind_In (DeclO, N_Pragma,
2047 N_Use_Package_Clause,
2049 N_Implicit_Label_Declaration)
2052 ("this declaration not allowed in machine code subprogram",
2059 -- No statements other than code statements, pragmas, and labels.
2060 -- Again we allow certain internally generated statements.
2062 Stmt := First (Statements (HSS));
2063 while Present (Stmt) loop
2064 StmtO := Original_Node (Stmt);
2065 if Comes_From_Source (StmtO)
2066 and then not Nkind_In (StmtO, N_Pragma,
2071 ("this statement is not allowed in machine code subprogram",
2078 end Analyze_Code_Statement;
2080 -----------------------------------------------
2081 -- Analyze_Enumeration_Representation_Clause --
2082 -----------------------------------------------
2084 procedure Analyze_Enumeration_Representation_Clause (N : Node_Id) is
2085 Ident : constant Node_Id := Identifier (N);
2086 Aggr : constant Node_Id := Array_Aggregate (N);
2087 Enumtype : Entity_Id;
2093 Err : Boolean := False;
2095 Lo : constant Uint := Expr_Value (Type_Low_Bound (Universal_Integer));
2096 Hi : constant Uint := Expr_Value (Type_High_Bound (Universal_Integer));
2097 -- Allowed range of universal integer (= allowed range of enum lit vals)
2101 -- Minimum and maximum values of entries
2104 -- Pointer to node for literal providing max value
2107 if Ignore_Rep_Clauses then
2111 -- First some basic error checks
2114 Enumtype := Entity (Ident);
2116 if Enumtype = Any_Type
2117 or else Rep_Item_Too_Early (Enumtype, N)
2121 Enumtype := Underlying_Type (Enumtype);
2124 if not Is_Enumeration_Type (Enumtype) then
2126 ("enumeration type required, found}",
2127 Ident, First_Subtype (Enumtype));
2131 -- Ignore rep clause on generic actual type. This will already have
2132 -- been flagged on the template as an error, and this is the safest
2133 -- way to ensure we don't get a junk cascaded message in the instance.
2135 if Is_Generic_Actual_Type (Enumtype) then
2138 -- Type must be in current scope
2140 elsif Scope (Enumtype) /= Current_Scope then
2141 Error_Msg_N ("type must be declared in this scope", Ident);
2144 -- Type must be a first subtype
2146 elsif not Is_First_Subtype (Enumtype) then
2147 Error_Msg_N ("cannot give enumeration rep clause for subtype", N);
2150 -- Ignore duplicate rep clause
2152 elsif Has_Enumeration_Rep_Clause (Enumtype) then
2153 Error_Msg_N ("duplicate enumeration rep clause ignored", N);
2156 -- Don't allow rep clause for standard [wide_[wide_]]character
2158 elsif Is_Standard_Character_Type (Enumtype) then
2159 Error_Msg_N ("enumeration rep clause not allowed for this type", N);
2162 -- Check that the expression is a proper aggregate (no parentheses)
2164 elsif Paren_Count (Aggr) /= 0 then
2166 ("extra parentheses surrounding aggregate not allowed",
2170 -- All tests passed, so set rep clause in place
2173 Set_Has_Enumeration_Rep_Clause (Enumtype);
2174 Set_Has_Enumeration_Rep_Clause (Base_Type (Enumtype));
2177 -- Now we process the aggregate. Note that we don't use the normal
2178 -- aggregate code for this purpose, because we don't want any of the
2179 -- normal expansion activities, and a number of special semantic
2180 -- rules apply (including the component type being any integer type)
2182 Elit := First_Literal (Enumtype);
2184 -- First the positional entries if any
2186 if Present (Expressions (Aggr)) then
2187 Expr := First (Expressions (Aggr));
2188 while Present (Expr) loop
2190 Error_Msg_N ("too many entries in aggregate", Expr);
2194 Val := Static_Integer (Expr);
2196 -- Err signals that we found some incorrect entries processing
2197 -- the list. The final checks for completeness and ordering are
2198 -- skipped in this case.
2200 if Val = No_Uint then
2202 elsif Val < Lo or else Hi < Val then
2203 Error_Msg_N ("value outside permitted range", Expr);
2207 Set_Enumeration_Rep (Elit, Val);
2208 Set_Enumeration_Rep_Expr (Elit, Expr);
2214 -- Now process the named entries if present
2216 if Present (Component_Associations (Aggr)) then
2217 Assoc := First (Component_Associations (Aggr));
2218 while Present (Assoc) loop
2219 Choice := First (Choices (Assoc));
2221 if Present (Next (Choice)) then
2223 ("multiple choice not allowed here", Next (Choice));
2227 if Nkind (Choice) = N_Others_Choice then
2228 Error_Msg_N ("others choice not allowed here", Choice);
2231 elsif Nkind (Choice) = N_Range then
2232 -- ??? should allow zero/one element range here
2233 Error_Msg_N ("range not allowed here", Choice);
2237 Analyze_And_Resolve (Choice, Enumtype);
2239 if Is_Entity_Name (Choice)
2240 and then Is_Type (Entity (Choice))
2242 Error_Msg_N ("subtype name not allowed here", Choice);
2244 -- ??? should allow static subtype with zero/one entry
2246 elsif Etype (Choice) = Base_Type (Enumtype) then
2247 if not Is_Static_Expression (Choice) then
2248 Flag_Non_Static_Expr
2249 ("non-static expression used for choice!", Choice);
2253 Elit := Expr_Value_E (Choice);
2255 if Present (Enumeration_Rep_Expr (Elit)) then
2256 Error_Msg_Sloc := Sloc (Enumeration_Rep_Expr (Elit));
2258 ("representation for& previously given#",
2263 Set_Enumeration_Rep_Expr (Elit, Expression (Assoc));
2265 Expr := Expression (Assoc);
2266 Val := Static_Integer (Expr);
2268 if Val = No_Uint then
2271 elsif Val < Lo or else Hi < Val then
2272 Error_Msg_N ("value outside permitted range", Expr);
2276 Set_Enumeration_Rep (Elit, Val);
2285 -- Aggregate is fully processed. Now we check that a full set of
2286 -- representations was given, and that they are in range and in order.
2287 -- These checks are only done if no other errors occurred.
2293 Elit := First_Literal (Enumtype);
2294 while Present (Elit) loop
2295 if No (Enumeration_Rep_Expr (Elit)) then
2296 Error_Msg_NE ("missing representation for&!", N, Elit);
2299 Val := Enumeration_Rep (Elit);
2301 if Min = No_Uint then
2305 if Val /= No_Uint then
2306 if Max /= No_Uint and then Val <= Max then
2308 ("enumeration value for& not ordered!",
2309 Enumeration_Rep_Expr (Elit), Elit);
2312 Max_Node := Enumeration_Rep_Expr (Elit);
2316 -- If there is at least one literal whose representation is not
2317 -- equal to the Pos value, then note that this enumeration type
2318 -- has a non-standard representation.
2320 if Val /= Enumeration_Pos (Elit) then
2321 Set_Has_Non_Standard_Rep (Base_Type (Enumtype));
2328 -- Now set proper size information
2331 Minsize : Uint := UI_From_Int (Minimum_Size (Enumtype));
2334 if Has_Size_Clause (Enumtype) then
2336 -- All OK, if size is OK now
2338 if RM_Size (Enumtype) >= Minsize then
2342 -- Try if we can get by with biasing
2345 UI_From_Int (Minimum_Size (Enumtype, Biased => True));
2347 -- Error message if even biasing does not work
2349 if RM_Size (Enumtype) < Minsize then
2350 Error_Msg_Uint_1 := RM_Size (Enumtype);
2351 Error_Msg_Uint_2 := Max;
2353 ("previously given size (^) is too small "
2354 & "for this value (^)", Max_Node);
2356 -- If biasing worked, indicate that we now have biased rep
2360 (Enumtype, Size_Clause (Enumtype), "size clause");
2365 Set_RM_Size (Enumtype, Minsize);
2366 Set_Enum_Esize (Enumtype);
2369 Set_RM_Size (Base_Type (Enumtype), RM_Size (Enumtype));
2370 Set_Esize (Base_Type (Enumtype), Esize (Enumtype));
2371 Set_Alignment (Base_Type (Enumtype), Alignment (Enumtype));
2375 -- We repeat the too late test in case it froze itself!
2377 if Rep_Item_Too_Late (Enumtype, N) then
2380 end Analyze_Enumeration_Representation_Clause;
2382 ----------------------------
2383 -- Analyze_Free_Statement --
2384 ----------------------------
2386 procedure Analyze_Free_Statement (N : Node_Id) is
2388 Analyze (Expression (N));
2389 end Analyze_Free_Statement;
2391 ---------------------------
2392 -- Analyze_Freeze_Entity --
2393 ---------------------------
2395 procedure Analyze_Freeze_Entity (N : Node_Id) is
2396 E : constant Entity_Id := Entity (N);
2399 -- Remember that we are processing a freezing entity. Required to
2400 -- ensure correct decoration of internal entities associated with
2401 -- interfaces (see New_Overloaded_Entity).
2403 Inside_Freezing_Actions := Inside_Freezing_Actions + 1;
2405 -- For tagged types covering interfaces add internal entities that link
2406 -- the primitives of the interfaces with the primitives that cover them.
2407 -- Note: These entities were originally generated only when generating
2408 -- code because their main purpose was to provide support to initialize
2409 -- the secondary dispatch tables. They are now generated also when
2410 -- compiling with no code generation to provide ASIS the relationship
2411 -- between interface primitives and tagged type primitives. They are
2412 -- also used to locate primitives covering interfaces when processing
2413 -- generics (see Derive_Subprograms).
2415 if Ada_Version >= Ada_05
2416 and then Ekind (E) = E_Record_Type
2417 and then Is_Tagged_Type (E)
2418 and then not Is_Interface (E)
2419 and then Has_Interfaces (E)
2421 -- This would be a good common place to call the routine that checks
2422 -- overriding of interface primitives (and thus factorize calls to
2423 -- Check_Abstract_Overriding located at different contexts in the
2424 -- compiler). However, this is not possible because it causes
2425 -- spurious errors in case of late overriding.
2427 Add_Internal_Interface_Entities (E);
2432 if Ekind (E) = E_Record_Type
2433 and then Is_CPP_Class (E)
2434 and then Is_Tagged_Type (E)
2435 and then Tagged_Type_Expansion
2436 and then Expander_Active
2438 if CPP_Num_Prims (E) = 0 then
2440 -- If the CPP type has user defined components then it must import
2441 -- primitives from C++. This is required because if the C++ class
2442 -- has no primitives then the C++ compiler does not added the _tag
2443 -- component to the type.
2445 pragma Assert (Chars (First_Entity (E)) = Name_uTag);
2447 if First_Entity (E) /= Last_Entity (E) then
2449 ("?'C'P'P type must import at least one primitive from C++",
2454 -- Check that all its primitives are abstract or imported from C++.
2455 -- Check also availability of the C++ constructor.
2458 Has_Constructors : constant Boolean := Has_CPP_Constructors (E);
2460 Error_Reported : Boolean := False;
2464 Elmt := First_Elmt (Primitive_Operations (E));
2465 while Present (Elmt) loop
2466 Prim := Node (Elmt);
2468 if Comes_From_Source (Prim) then
2469 if Is_Abstract_Subprogram (Prim) then
2472 elsif not Is_Imported (Prim)
2473 or else Convention (Prim) /= Convention_CPP
2476 ("?primitives of 'C'P'P types must be imported from C++"
2477 & " or abstract", Prim);
2479 elsif not Has_Constructors
2480 and then not Error_Reported
2482 Error_Msg_Name_1 := Chars (E);
2484 ("?'C'P'P constructor required for type %", Prim);
2485 Error_Reported := True;
2494 Inside_Freezing_Actions := Inside_Freezing_Actions - 1;
2495 end Analyze_Freeze_Entity;
2497 ------------------------------------------
2498 -- Analyze_Record_Representation_Clause --
2499 ------------------------------------------
2501 -- Note: we check as much as we can here, but we can't do any checks
2502 -- based on the position values (e.g. overlap checks) until freeze time
2503 -- because especially in Ada 2005 (machine scalar mode), the processing
2504 -- for non-standard bit order can substantially change the positions.
2505 -- See procedure Check_Record_Representation_Clause (called from Freeze)
2506 -- for the remainder of this processing.
2508 procedure Analyze_Record_Representation_Clause (N : Node_Id) is
2509 Ident : constant Node_Id := Identifier (N);
2514 Hbit : Uint := Uint_0;
2518 Rectype : Entity_Id;
2520 CR_Pragma : Node_Id := Empty;
2521 -- Points to N_Pragma node if Complete_Representation pragma present
2524 if Ignore_Rep_Clauses then
2529 Rectype := Entity (Ident);
2531 if Rectype = Any_Type
2532 or else Rep_Item_Too_Early (Rectype, N)
2536 Rectype := Underlying_Type (Rectype);
2539 -- First some basic error checks
2541 if not Is_Record_Type (Rectype) then
2543 ("record type required, found}", Ident, First_Subtype (Rectype));
2546 elsif Scope (Rectype) /= Current_Scope then
2547 Error_Msg_N ("type must be declared in this scope", N);
2550 elsif not Is_First_Subtype (Rectype) then
2551 Error_Msg_N ("cannot give record rep clause for subtype", N);
2554 elsif Has_Record_Rep_Clause (Rectype) then
2555 Error_Msg_N ("duplicate record rep clause ignored", N);
2558 elsif Rep_Item_Too_Late (Rectype, N) then
2562 if Present (Mod_Clause (N)) then
2564 Loc : constant Source_Ptr := Sloc (N);
2565 M : constant Node_Id := Mod_Clause (N);
2566 P : constant List_Id := Pragmas_Before (M);
2570 pragma Warnings (Off, Mod_Val);
2573 Check_Restriction (No_Obsolescent_Features, Mod_Clause (N));
2575 if Warn_On_Obsolescent_Feature then
2577 ("mod clause is an obsolescent feature (RM J.8)?", N);
2579 ("\use alignment attribute definition clause instead?", N);
2586 -- In ASIS_Mode mode, expansion is disabled, but we must convert
2587 -- the Mod clause into an alignment clause anyway, so that the
2588 -- back-end can compute and back-annotate properly the size and
2589 -- alignment of types that may include this record.
2591 -- This seems dubious, this destroys the source tree in a manner
2592 -- not detectable by ASIS ???
2594 if Operating_Mode = Check_Semantics
2598 Make_Attribute_Definition_Clause (Loc,
2599 Name => New_Reference_To (Base_Type (Rectype), Loc),
2600 Chars => Name_Alignment,
2601 Expression => Relocate_Node (Expression (M)));
2603 Set_From_At_Mod (AtM_Nod);
2604 Insert_After (N, AtM_Nod);
2605 Mod_Val := Get_Alignment_Value (Expression (AtM_Nod));
2606 Set_Mod_Clause (N, Empty);
2609 -- Get the alignment value to perform error checking
2611 Mod_Val := Get_Alignment_Value (Expression (M));
2616 -- For untagged types, clear any existing component clauses for the
2617 -- type. If the type is derived, this is what allows us to override
2618 -- a rep clause for the parent. For type extensions, the representation
2619 -- of the inherited components is inherited, so we want to keep previous
2620 -- component clauses for completeness.
2622 if not Is_Tagged_Type (Rectype) then
2623 Comp := First_Component_Or_Discriminant (Rectype);
2624 while Present (Comp) loop
2625 Set_Component_Clause (Comp, Empty);
2626 Next_Component_Or_Discriminant (Comp);
2630 -- All done if no component clauses
2632 CC := First (Component_Clauses (N));
2638 -- A representation like this applies to the base type
2640 Set_Has_Record_Rep_Clause (Base_Type (Rectype));
2641 Set_Has_Non_Standard_Rep (Base_Type (Rectype));
2642 Set_Has_Specified_Layout (Base_Type (Rectype));
2644 -- Process the component clauses
2646 while Present (CC) loop
2650 if Nkind (CC) = N_Pragma then
2653 -- The only pragma of interest is Complete_Representation
2655 if Pragma_Name (CC) = Name_Complete_Representation then
2659 -- Processing for real component clause
2662 Posit := Static_Integer (Position (CC));
2663 Fbit := Static_Integer (First_Bit (CC));
2664 Lbit := Static_Integer (Last_Bit (CC));
2667 and then Fbit /= No_Uint
2668 and then Lbit /= No_Uint
2672 ("position cannot be negative", Position (CC));
2676 ("first bit cannot be negative", First_Bit (CC));
2678 -- The Last_Bit specified in a component clause must not be
2679 -- less than the First_Bit minus one (RM-13.5.1(10)).
2681 elsif Lbit < Fbit - 1 then
2683 ("last bit cannot be less than first bit minus one",
2686 -- Values look OK, so find the corresponding record component
2687 -- Even though the syntax allows an attribute reference for
2688 -- implementation-defined components, GNAT does not allow the
2689 -- tag to get an explicit position.
2691 elsif Nkind (Component_Name (CC)) = N_Attribute_Reference then
2692 if Attribute_Name (Component_Name (CC)) = Name_Tag then
2693 Error_Msg_N ("position of tag cannot be specified", CC);
2695 Error_Msg_N ("illegal component name", CC);
2699 Comp := First_Entity (Rectype);
2700 while Present (Comp) loop
2701 exit when Chars (Comp) = Chars (Component_Name (CC));
2707 -- Maybe component of base type that is absent from
2708 -- statically constrained first subtype.
2710 Comp := First_Entity (Base_Type (Rectype));
2711 while Present (Comp) loop
2712 exit when Chars (Comp) = Chars (Component_Name (CC));
2719 ("component clause is for non-existent field", CC);
2721 -- Ada 2012 (AI05-0026): Any name that denotes a
2722 -- discriminant of an object of an unchecked union type
2723 -- shall not occur within a record_representation_clause.
2725 -- The general restriction of using record rep clauses on
2726 -- Unchecked_Union types has now been lifted. Since it is
2727 -- possible to introduce a record rep clause which mentions
2728 -- the discriminant of an Unchecked_Union in non-Ada 2012
2729 -- code, this check is applied to all versions of the
2732 elsif Ekind (Comp) = E_Discriminant
2733 and then Is_Unchecked_Union (Rectype)
2736 ("cannot reference discriminant of Unchecked_Union",
2737 Component_Name (CC));
2739 elsif Present (Component_Clause (Comp)) then
2741 -- Diagnose duplicate rep clause, or check consistency
2742 -- if this is an inherited component. In a double fault,
2743 -- there may be a duplicate inconsistent clause for an
2744 -- inherited component.
2746 if Scope (Original_Record_Component (Comp)) = Rectype
2747 or else Parent (Component_Clause (Comp)) = N
2749 Error_Msg_Sloc := Sloc (Component_Clause (Comp));
2750 Error_Msg_N ("component clause previously given#", CC);
2754 Rep1 : constant Node_Id := Component_Clause (Comp);
2756 if Intval (Position (Rep1)) /=
2757 Intval (Position (CC))
2758 or else Intval (First_Bit (Rep1)) /=
2759 Intval (First_Bit (CC))
2760 or else Intval (Last_Bit (Rep1)) /=
2761 Intval (Last_Bit (CC))
2763 Error_Msg_N ("component clause inconsistent "
2764 & "with representation of ancestor", CC);
2765 elsif Warn_On_Redundant_Constructs then
2766 Error_Msg_N ("?redundant component clause "
2767 & "for inherited component!", CC);
2772 -- Normal case where this is the first component clause we
2773 -- have seen for this entity, so set it up properly.
2776 -- Make reference for field in record rep clause and set
2777 -- appropriate entity field in the field identifier.
2780 (Comp, Component_Name (CC), Set_Ref => False);
2781 Set_Entity (Component_Name (CC), Comp);
2783 -- Update Fbit and Lbit to the actual bit number
2785 Fbit := Fbit + UI_From_Int (SSU) * Posit;
2786 Lbit := Lbit + UI_From_Int (SSU) * Posit;
2788 if Has_Size_Clause (Rectype)
2789 and then Esize (Rectype) <= Lbit
2792 ("bit number out of range of specified size",
2795 Set_Component_Clause (Comp, CC);
2796 Set_Component_Bit_Offset (Comp, Fbit);
2797 Set_Esize (Comp, 1 + (Lbit - Fbit));
2798 Set_Normalized_First_Bit (Comp, Fbit mod SSU);
2799 Set_Normalized_Position (Comp, Fbit / SSU);
2801 if Warn_On_Overridden_Size
2802 and then Has_Size_Clause (Etype (Comp))
2803 and then RM_Size (Etype (Comp)) /= Esize (Comp)
2806 ("?component size overrides size clause for&",
2807 Component_Name (CC), Etype (Comp));
2810 -- This information is also set in the corresponding
2811 -- component of the base type, found by accessing the
2812 -- Original_Record_Component link if it is present.
2814 Ocomp := Original_Record_Component (Comp);
2821 (Component_Name (CC),
2827 (Comp, First_Node (CC), "component clause", Biased);
2829 if Present (Ocomp) then
2830 Set_Component_Clause (Ocomp, CC);
2831 Set_Component_Bit_Offset (Ocomp, Fbit);
2832 Set_Normalized_First_Bit (Ocomp, Fbit mod SSU);
2833 Set_Normalized_Position (Ocomp, Fbit / SSU);
2834 Set_Esize (Ocomp, 1 + (Lbit - Fbit));
2836 Set_Normalized_Position_Max
2837 (Ocomp, Normalized_Position (Ocomp));
2839 -- Note: we don't use Set_Biased here, because we
2840 -- already gave a warning above if needed, and we
2841 -- would get a duplicate for the same name here.
2843 Set_Has_Biased_Representation
2844 (Ocomp, Has_Biased_Representation (Comp));
2847 if Esize (Comp) < 0 then
2848 Error_Msg_N ("component size is negative", CC);
2859 -- Check missing components if Complete_Representation pragma appeared
2861 if Present (CR_Pragma) then
2862 Comp := First_Component_Or_Discriminant (Rectype);
2863 while Present (Comp) loop
2864 if No (Component_Clause (Comp)) then
2866 ("missing component clause for &", CR_Pragma, Comp);
2869 Next_Component_Or_Discriminant (Comp);
2872 -- If no Complete_Representation pragma, warn if missing components
2874 elsif Warn_On_Unrepped_Components then
2876 Num_Repped_Components : Nat := 0;
2877 Num_Unrepped_Components : Nat := 0;
2880 -- First count number of repped and unrepped components
2882 Comp := First_Component_Or_Discriminant (Rectype);
2883 while Present (Comp) loop
2884 if Present (Component_Clause (Comp)) then
2885 Num_Repped_Components := Num_Repped_Components + 1;
2887 Num_Unrepped_Components := Num_Unrepped_Components + 1;
2890 Next_Component_Or_Discriminant (Comp);
2893 -- We are only interested in the case where there is at least one
2894 -- unrepped component, and at least half the components have rep
2895 -- clauses. We figure that if less than half have them, then the
2896 -- partial rep clause is really intentional. If the component
2897 -- type has no underlying type set at this point (as for a generic
2898 -- formal type), we don't know enough to give a warning on the
2901 if Num_Unrepped_Components > 0
2902 and then Num_Unrepped_Components < Num_Repped_Components
2904 Comp := First_Component_Or_Discriminant (Rectype);
2905 while Present (Comp) loop
2906 if No (Component_Clause (Comp))
2907 and then Comes_From_Source (Comp)
2908 and then Present (Underlying_Type (Etype (Comp)))
2909 and then (Is_Scalar_Type (Underlying_Type (Etype (Comp)))
2910 or else Size_Known_At_Compile_Time
2911 (Underlying_Type (Etype (Comp))))
2912 and then not Has_Warnings_Off (Rectype)
2914 Error_Msg_Sloc := Sloc (Comp);
2916 ("?no component clause given for & declared #",
2920 Next_Component_Or_Discriminant (Comp);
2925 end Analyze_Record_Representation_Clause;
2927 -----------------------------------
2928 -- Check_Constant_Address_Clause --
2929 -----------------------------------
2931 procedure Check_Constant_Address_Clause
2935 procedure Check_At_Constant_Address (Nod : Node_Id);
2936 -- Checks that the given node N represents a name whose 'Address is
2937 -- constant (in the same sense as OK_Constant_Address_Clause, i.e. the
2938 -- address value is the same at the point of declaration of U_Ent and at
2939 -- the time of elaboration of the address clause.
2941 procedure Check_Expr_Constants (Nod : Node_Id);
2942 -- Checks that Nod meets the requirements for a constant address clause
2943 -- in the sense of the enclosing procedure.
2945 procedure Check_List_Constants (Lst : List_Id);
2946 -- Check that all elements of list Lst meet the requirements for a
2947 -- constant address clause in the sense of the enclosing procedure.
2949 -------------------------------
2950 -- Check_At_Constant_Address --
2951 -------------------------------
2953 procedure Check_At_Constant_Address (Nod : Node_Id) is
2955 if Is_Entity_Name (Nod) then
2956 if Present (Address_Clause (Entity ((Nod)))) then
2958 ("invalid address clause for initialized object &!",
2961 ("address for& cannot" &
2962 " depend on another address clause! (RM 13.1(22))!",
2965 elsif In_Same_Source_Unit (Entity (Nod), U_Ent)
2966 and then Sloc (U_Ent) < Sloc (Entity (Nod))
2969 ("invalid address clause for initialized object &!",
2971 Error_Msg_Node_2 := U_Ent;
2973 ("\& must be defined before & (RM 13.1(22))!",
2977 elsif Nkind (Nod) = N_Selected_Component then
2979 T : constant Entity_Id := Etype (Prefix (Nod));
2982 if (Is_Record_Type (T)
2983 and then Has_Discriminants (T))
2986 and then Is_Record_Type (Designated_Type (T))
2987 and then Has_Discriminants (Designated_Type (T)))
2990 ("invalid address clause for initialized object &!",
2993 ("\address cannot depend on component" &
2994 " of discriminated record (RM 13.1(22))!",
2997 Check_At_Constant_Address (Prefix (Nod));
3001 elsif Nkind (Nod) = N_Indexed_Component then
3002 Check_At_Constant_Address (Prefix (Nod));
3003 Check_List_Constants (Expressions (Nod));
3006 Check_Expr_Constants (Nod);
3008 end Check_At_Constant_Address;
3010 --------------------------
3011 -- Check_Expr_Constants --
3012 --------------------------
3014 procedure Check_Expr_Constants (Nod : Node_Id) is
3015 Loc_U_Ent : constant Source_Ptr := Sloc (U_Ent);
3016 Ent : Entity_Id := Empty;
3019 if Nkind (Nod) in N_Has_Etype
3020 and then Etype (Nod) = Any_Type
3026 when N_Empty | N_Error =>
3029 when N_Identifier | N_Expanded_Name =>
3030 Ent := Entity (Nod);
3032 -- We need to look at the original node if it is different
3033 -- from the node, since we may have rewritten things and
3034 -- substituted an identifier representing the rewrite.
3036 if Original_Node (Nod) /= Nod then
3037 Check_Expr_Constants (Original_Node (Nod));
3039 -- If the node is an object declaration without initial
3040 -- value, some code has been expanded, and the expression
3041 -- is not constant, even if the constituents might be
3042 -- acceptable, as in A'Address + offset.
3044 if Ekind (Ent) = E_Variable
3046 Nkind (Declaration_Node (Ent)) = N_Object_Declaration
3048 No (Expression (Declaration_Node (Ent)))
3051 ("invalid address clause for initialized object &!",
3054 -- If entity is constant, it may be the result of expanding
3055 -- a check. We must verify that its declaration appears
3056 -- before the object in question, else we also reject the
3059 elsif Ekind (Ent) = E_Constant
3060 and then In_Same_Source_Unit (Ent, U_Ent)
3061 and then Sloc (Ent) > Loc_U_Ent
3064 ("invalid address clause for initialized object &!",
3071 -- Otherwise look at the identifier and see if it is OK
3073 if Ekind_In (Ent, E_Named_Integer, E_Named_Real)
3074 or else Is_Type (Ent)
3079 Ekind (Ent) = E_Constant
3081 Ekind (Ent) = E_In_Parameter
3083 -- This is the case where we must have Ent defined before
3084 -- U_Ent. Clearly if they are in different units this
3085 -- requirement is met since the unit containing Ent is
3086 -- already processed.
3088 if not In_Same_Source_Unit (Ent, U_Ent) then
3091 -- Otherwise location of Ent must be before the location
3092 -- of U_Ent, that's what prior defined means.
3094 elsif Sloc (Ent) < Loc_U_Ent then
3099 ("invalid address clause for initialized object &!",
3101 Error_Msg_Node_2 := U_Ent;
3103 ("\& must be defined before & (RM 13.1(22))!",
3107 elsif Nkind (Original_Node (Nod)) = N_Function_Call then
3108 Check_Expr_Constants (Original_Node (Nod));
3112 ("invalid address clause for initialized object &!",
3115 if Comes_From_Source (Ent) then
3117 ("\reference to variable& not allowed"
3118 & " (RM 13.1(22))!", Nod, Ent);
3121 ("non-static expression not allowed"
3122 & " (RM 13.1(22))!", Nod);
3126 when N_Integer_Literal =>
3128 -- If this is a rewritten unchecked conversion, in a system
3129 -- where Address is an integer type, always use the base type
3130 -- for a literal value. This is user-friendly and prevents
3131 -- order-of-elaboration issues with instances of unchecked
3134 if Nkind (Original_Node (Nod)) = N_Function_Call then
3135 Set_Etype (Nod, Base_Type (Etype (Nod)));
3138 when N_Real_Literal |
3140 N_Character_Literal =>
3144 Check_Expr_Constants (Low_Bound (Nod));
3145 Check_Expr_Constants (High_Bound (Nod));
3147 when N_Explicit_Dereference =>
3148 Check_Expr_Constants (Prefix (Nod));
3150 when N_Indexed_Component =>
3151 Check_Expr_Constants (Prefix (Nod));
3152 Check_List_Constants (Expressions (Nod));
3155 Check_Expr_Constants (Prefix (Nod));
3156 Check_Expr_Constants (Discrete_Range (Nod));
3158 when N_Selected_Component =>
3159 Check_Expr_Constants (Prefix (Nod));
3161 when N_Attribute_Reference =>
3162 if Attribute_Name (Nod) = Name_Address
3164 Attribute_Name (Nod) = Name_Access
3166 Attribute_Name (Nod) = Name_Unchecked_Access
3168 Attribute_Name (Nod) = Name_Unrestricted_Access
3170 Check_At_Constant_Address (Prefix (Nod));
3173 Check_Expr_Constants (Prefix (Nod));
3174 Check_List_Constants (Expressions (Nod));
3178 Check_List_Constants (Component_Associations (Nod));
3179 Check_List_Constants (Expressions (Nod));
3181 when N_Component_Association =>
3182 Check_Expr_Constants (Expression (Nod));
3184 when N_Extension_Aggregate =>
3185 Check_Expr_Constants (Ancestor_Part (Nod));
3186 Check_List_Constants (Component_Associations (Nod));
3187 Check_List_Constants (Expressions (Nod));
3192 when N_Binary_Op | N_Short_Circuit | N_Membership_Test =>
3193 Check_Expr_Constants (Left_Opnd (Nod));
3194 Check_Expr_Constants (Right_Opnd (Nod));
3197 Check_Expr_Constants (Right_Opnd (Nod));
3199 when N_Type_Conversion |
3200 N_Qualified_Expression |
3202 Check_Expr_Constants (Expression (Nod));
3204 when N_Unchecked_Type_Conversion =>
3205 Check_Expr_Constants (Expression (Nod));
3207 -- If this is a rewritten unchecked conversion, subtypes in
3208 -- this node are those created within the instance. To avoid
3209 -- order of elaboration issues, replace them with their base
3210 -- types. Note that address clauses can cause order of
3211 -- elaboration problems because they are elaborated by the
3212 -- back-end at the point of definition, and may mention
3213 -- entities declared in between (as long as everything is
3214 -- static). It is user-friendly to allow unchecked conversions
3217 if Nkind (Original_Node (Nod)) = N_Function_Call then
3218 Set_Etype (Expression (Nod),
3219 Base_Type (Etype (Expression (Nod))));
3220 Set_Etype (Nod, Base_Type (Etype (Nod)));
3223 when N_Function_Call =>
3224 if not Is_Pure (Entity (Name (Nod))) then
3226 ("invalid address clause for initialized object &!",
3230 ("\function & is not pure (RM 13.1(22))!",
3231 Nod, Entity (Name (Nod)));
3234 Check_List_Constants (Parameter_Associations (Nod));
3237 when N_Parameter_Association =>
3238 Check_Expr_Constants (Explicit_Actual_Parameter (Nod));
3242 ("invalid address clause for initialized object &!",
3245 ("\must be constant defined before& (RM 13.1(22))!",
3248 end Check_Expr_Constants;
3250 --------------------------
3251 -- Check_List_Constants --
3252 --------------------------
3254 procedure Check_List_Constants (Lst : List_Id) is
3258 if Present (Lst) then
3259 Nod1 := First (Lst);
3260 while Present (Nod1) loop
3261 Check_Expr_Constants (Nod1);
3265 end Check_List_Constants;
3267 -- Start of processing for Check_Constant_Address_Clause
3270 -- If rep_clauses are to be ignored, no need for legality checks. In
3271 -- particular, no need to pester user about rep clauses that violate
3272 -- the rule on constant addresses, given that these clauses will be
3273 -- removed by Freeze before they reach the back end.
3275 if not Ignore_Rep_Clauses then
3276 Check_Expr_Constants (Expr);
3278 end Check_Constant_Address_Clause;
3280 ----------------------------------------
3281 -- Check_Record_Representation_Clause --
3282 ----------------------------------------
3284 procedure Check_Record_Representation_Clause (N : Node_Id) is
3285 Loc : constant Source_Ptr := Sloc (N);
3286 Ident : constant Node_Id := Identifier (N);
3287 Rectype : Entity_Id;
3292 Hbit : Uint := Uint_0;
3296 Max_Bit_So_Far : Uint;
3297 -- Records the maximum bit position so far. If all field positions
3298 -- are monotonically increasing, then we can skip the circuit for
3299 -- checking for overlap, since no overlap is possible.
3301 Tagged_Parent : Entity_Id := Empty;
3302 -- This is set in the case of a derived tagged type for which we have
3303 -- Is_Fully_Repped_Tagged_Type True (indicating that all components are
3304 -- positioned by record representation clauses). In this case we must
3305 -- check for overlap between components of this tagged type, and the
3306 -- components of its parent. Tagged_Parent will point to this parent
3307 -- type. For all other cases Tagged_Parent is left set to Empty.
3309 Parent_Last_Bit : Uint;
3310 -- Relevant only if Tagged_Parent is set, Parent_Last_Bit indicates the
3311 -- last bit position for any field in the parent type. We only need to
3312 -- check overlap for fields starting below this point.
3314 Overlap_Check_Required : Boolean;
3315 -- Used to keep track of whether or not an overlap check is required
3317 Overlap_Detected : Boolean := False;
3318 -- Set True if an overlap is detected
3320 Ccount : Natural := 0;
3321 -- Number of component clauses in record rep clause
3323 procedure Check_Component_Overlap (C1_Ent, C2_Ent : Entity_Id);
3324 -- Given two entities for record components or discriminants, checks
3325 -- if they have overlapping component clauses and issues errors if so.
3327 procedure Find_Component;
3328 -- Finds component entity corresponding to current component clause (in
3329 -- CC), and sets Comp to the entity, and Fbit/Lbit to the zero origin
3330 -- start/stop bits for the field. If there is no matching component or
3331 -- if the matching component does not have a component clause, then
3332 -- that's an error and Comp is set to Empty, but no error message is
3333 -- issued, since the message was already given. Comp is also set to
3334 -- Empty if the current "component clause" is in fact a pragma.
3336 -----------------------------
3337 -- Check_Component_Overlap --
3338 -----------------------------
3340 procedure Check_Component_Overlap (C1_Ent, C2_Ent : Entity_Id) is
3341 CC1 : constant Node_Id := Component_Clause (C1_Ent);
3342 CC2 : constant Node_Id := Component_Clause (C2_Ent);
3345 if Present (CC1) and then Present (CC2) then
3347 -- Exclude odd case where we have two tag fields in the same
3348 -- record, both at location zero. This seems a bit strange, but
3349 -- it seems to happen in some circumstances, perhaps on an error.
3351 if Chars (C1_Ent) = Name_uTag
3353 Chars (C2_Ent) = Name_uTag
3358 -- Here we check if the two fields overlap
3361 S1 : constant Uint := Component_Bit_Offset (C1_Ent);
3362 S2 : constant Uint := Component_Bit_Offset (C2_Ent);
3363 E1 : constant Uint := S1 + Esize (C1_Ent);
3364 E2 : constant Uint := S2 + Esize (C2_Ent);
3367 if E2 <= S1 or else E1 <= S2 then
3370 Error_Msg_Node_2 := Component_Name (CC2);
3371 Error_Msg_Sloc := Sloc (Error_Msg_Node_2);
3372 Error_Msg_Node_1 := Component_Name (CC1);
3374 ("component& overlaps & #", Component_Name (CC1));
3375 Overlap_Detected := True;
3379 end Check_Component_Overlap;
3381 --------------------
3382 -- Find_Component --
3383 --------------------
3385 procedure Find_Component is
3387 procedure Search_Component (R : Entity_Id);
3388 -- Search components of R for a match. If found, Comp is set.
3390 ----------------------
3391 -- Search_Component --
3392 ----------------------
3394 procedure Search_Component (R : Entity_Id) is
3396 Comp := First_Component_Or_Discriminant (R);
3397 while Present (Comp) loop
3399 -- Ignore error of attribute name for component name (we
3400 -- already gave an error message for this, so no need to
3403 if Nkind (Component_Name (CC)) = N_Attribute_Reference then
3406 exit when Chars (Comp) = Chars (Component_Name (CC));
3409 Next_Component_Or_Discriminant (Comp);
3411 end Search_Component;
3413 -- Start of processing for Find_Component
3416 -- Return with Comp set to Empty if we have a pragma
3418 if Nkind (CC) = N_Pragma then
3423 -- Search current record for matching component
3425 Search_Component (Rectype);
3427 -- If not found, maybe component of base type that is absent from
3428 -- statically constrained first subtype.
3431 Search_Component (Base_Type (Rectype));
3434 -- If no component, or the component does not reference the component
3435 -- clause in question, then there was some previous error for which
3436 -- we already gave a message, so just return with Comp Empty.
3439 or else Component_Clause (Comp) /= CC
3443 -- Normal case where we have a component clause
3446 Fbit := Component_Bit_Offset (Comp);
3447 Lbit := Fbit + Esize (Comp) - 1;
3451 -- Start of processing for Check_Record_Representation_Clause
3455 Rectype := Entity (Ident);
3457 if Rectype = Any_Type then
3460 Rectype := Underlying_Type (Rectype);
3463 -- See if we have a fully repped derived tagged type
3466 PS : constant Entity_Id := Parent_Subtype (Rectype);
3469 if Present (PS) and then Is_Fully_Repped_Tagged_Type (PS) then
3470 Tagged_Parent := PS;
3472 -- Find maximum bit of any component of the parent type
3474 Parent_Last_Bit := UI_From_Int (System_Address_Size - 1);
3475 Pcomp := First_Entity (Tagged_Parent);
3476 while Present (Pcomp) loop
3477 if Ekind_In (Pcomp, E_Discriminant, E_Component) then
3478 if Component_Bit_Offset (Pcomp) /= No_Uint
3479 and then Known_Static_Esize (Pcomp)
3484 Component_Bit_Offset (Pcomp) + Esize (Pcomp) - 1);
3487 Next_Entity (Pcomp);
3493 -- All done if no component clauses
3495 CC := First (Component_Clauses (N));
3501 -- If a tag is present, then create a component clause that places it
3502 -- at the start of the record (otherwise gigi may place it after other
3503 -- fields that have rep clauses).
3505 Fent := First_Entity (Rectype);
3507 if Nkind (Fent) = N_Defining_Identifier
3508 and then Chars (Fent) = Name_uTag
3510 Set_Component_Bit_Offset (Fent, Uint_0);
3511 Set_Normalized_Position (Fent, Uint_0);
3512 Set_Normalized_First_Bit (Fent, Uint_0);
3513 Set_Normalized_Position_Max (Fent, Uint_0);
3514 Init_Esize (Fent, System_Address_Size);
3516 Set_Component_Clause (Fent,
3517 Make_Component_Clause (Loc,
3519 Make_Identifier (Loc,
3520 Chars => Name_uTag),
3523 Make_Integer_Literal (Loc,
3527 Make_Integer_Literal (Loc,
3531 Make_Integer_Literal (Loc,
3532 UI_From_Int (System_Address_Size))));
3534 Ccount := Ccount + 1;
3537 Max_Bit_So_Far := Uint_Minus_1;
3538 Overlap_Check_Required := False;
3540 -- Process the component clauses
3542 while Present (CC) loop
3545 if Present (Comp) then
3546 Ccount := Ccount + 1;
3548 -- We need a full overlap check if record positions non-monotonic
3550 if Fbit <= Max_Bit_So_Far then
3551 Overlap_Check_Required := True;
3554 Max_Bit_So_Far := Lbit;
3556 -- Check bit position out of range of specified size
3558 if Has_Size_Clause (Rectype)
3559 and then Esize (Rectype) <= Lbit
3562 ("bit number out of range of specified size",
3565 -- Check for overlap with tag field
3568 if Is_Tagged_Type (Rectype)
3569 and then Fbit < System_Address_Size
3572 ("component overlaps tag field of&",
3573 Component_Name (CC), Rectype);
3574 Overlap_Detected := True;
3582 -- Check parent overlap if component might overlap parent field
3584 if Present (Tagged_Parent)
3585 and then Fbit <= Parent_Last_Bit
3587 Pcomp := First_Component_Or_Discriminant (Tagged_Parent);
3588 while Present (Pcomp) loop
3589 if not Is_Tag (Pcomp)
3590 and then Chars (Pcomp) /= Name_uParent
3592 Check_Component_Overlap (Comp, Pcomp);
3595 Next_Component_Or_Discriminant (Pcomp);
3603 -- Now that we have processed all the component clauses, check for
3604 -- overlap. We have to leave this till last, since the components can
3605 -- appear in any arbitrary order in the representation clause.
3607 -- We do not need this check if all specified ranges were monotonic,
3608 -- as recorded by Overlap_Check_Required being False at this stage.
3610 -- This first section checks if there are any overlapping entries at
3611 -- all. It does this by sorting all entries and then seeing if there are
3612 -- any overlaps. If there are none, then that is decisive, but if there
3613 -- are overlaps, they may still be OK (they may result from fields in
3614 -- different variants).
3616 if Overlap_Check_Required then
3617 Overlap_Check1 : declare
3619 OC_Fbit : array (0 .. Ccount) of Uint;
3620 -- First-bit values for component clauses, the value is the offset
3621 -- of the first bit of the field from start of record. The zero
3622 -- entry is for use in sorting.
3624 OC_Lbit : array (0 .. Ccount) of Uint;
3625 -- Last-bit values for component clauses, the value is the offset
3626 -- of the last bit of the field from start of record. The zero
3627 -- entry is for use in sorting.
3629 OC_Count : Natural := 0;
3630 -- Count of entries in OC_Fbit and OC_Lbit
3632 function OC_Lt (Op1, Op2 : Natural) return Boolean;
3633 -- Compare routine for Sort
3635 procedure OC_Move (From : Natural; To : Natural);
3636 -- Move routine for Sort
3638 package Sorting is new GNAT.Heap_Sort_G (OC_Move, OC_Lt);
3644 function OC_Lt (Op1, Op2 : Natural) return Boolean is
3646 return OC_Fbit (Op1) < OC_Fbit (Op2);
3653 procedure OC_Move (From : Natural; To : Natural) is
3655 OC_Fbit (To) := OC_Fbit (From);
3656 OC_Lbit (To) := OC_Lbit (From);
3659 -- Start of processing for Overlap_Check
3662 CC := First (Component_Clauses (N));
3663 while Present (CC) loop
3665 -- Exclude component clause already marked in error
3667 if not Error_Posted (CC) then
3670 if Present (Comp) then
3671 OC_Count := OC_Count + 1;
3672 OC_Fbit (OC_Count) := Fbit;
3673 OC_Lbit (OC_Count) := Lbit;
3680 Sorting.Sort (OC_Count);
3682 Overlap_Check_Required := False;
3683 for J in 1 .. OC_Count - 1 loop
3684 if OC_Lbit (J) >= OC_Fbit (J + 1) then
3685 Overlap_Check_Required := True;
3692 -- If Overlap_Check_Required is still True, then we have to do the full
3693 -- scale overlap check, since we have at least two fields that do
3694 -- overlap, and we need to know if that is OK since they are in
3695 -- different variant, or whether we have a definite problem.
3697 if Overlap_Check_Required then
3698 Overlap_Check2 : declare
3699 C1_Ent, C2_Ent : Entity_Id;
3700 -- Entities of components being checked for overlap
3703 -- Component_List node whose Component_Items are being checked
3706 -- Component declaration for component being checked
3709 C1_Ent := First_Entity (Base_Type (Rectype));
3711 -- Loop through all components in record. For each component check
3712 -- for overlap with any of the preceding elements on the component
3713 -- list containing the component and also, if the component is in
3714 -- a variant, check against components outside the case structure.
3715 -- This latter test is repeated recursively up the variant tree.
3717 Main_Component_Loop : while Present (C1_Ent) loop
3718 if not Ekind_In (C1_Ent, E_Component, E_Discriminant) then
3719 goto Continue_Main_Component_Loop;
3722 -- Skip overlap check if entity has no declaration node. This
3723 -- happens with discriminants in constrained derived types.
3724 -- Possibly we are missing some checks as a result, but that
3725 -- does not seem terribly serious.
3727 if No (Declaration_Node (C1_Ent)) then
3728 goto Continue_Main_Component_Loop;
3731 Clist := Parent (List_Containing (Declaration_Node (C1_Ent)));
3733 -- Loop through component lists that need checking. Check the
3734 -- current component list and all lists in variants above us.
3736 Component_List_Loop : loop
3738 -- If derived type definition, go to full declaration
3739 -- If at outer level, check discriminants if there are any.
3741 if Nkind (Clist) = N_Derived_Type_Definition then
3742 Clist := Parent (Clist);
3745 -- Outer level of record definition, check discriminants
3747 if Nkind_In (Clist, N_Full_Type_Declaration,
3748 N_Private_Type_Declaration)
3750 if Has_Discriminants (Defining_Identifier (Clist)) then
3752 First_Discriminant (Defining_Identifier (Clist));
3753 while Present (C2_Ent) loop
3754 exit when C1_Ent = C2_Ent;
3755 Check_Component_Overlap (C1_Ent, C2_Ent);
3756 Next_Discriminant (C2_Ent);
3760 -- Record extension case
3762 elsif Nkind (Clist) = N_Derived_Type_Definition then
3765 -- Otherwise check one component list
3768 Citem := First (Component_Items (Clist));
3769 while Present (Citem) loop
3770 if Nkind (Citem) = N_Component_Declaration then
3771 C2_Ent := Defining_Identifier (Citem);
3772 exit when C1_Ent = C2_Ent;
3773 Check_Component_Overlap (C1_Ent, C2_Ent);
3780 -- Check for variants above us (the parent of the Clist can
3781 -- be a variant, in which case its parent is a variant part,
3782 -- and the parent of the variant part is a component list
3783 -- whose components must all be checked against the current
3784 -- component for overlap).
3786 if Nkind (Parent (Clist)) = N_Variant then
3787 Clist := Parent (Parent (Parent (Clist)));
3789 -- Check for possible discriminant part in record, this
3790 -- is treated essentially as another level in the
3791 -- recursion. For this case the parent of the component
3792 -- list is the record definition, and its parent is the
3793 -- full type declaration containing the discriminant
3796 elsif Nkind (Parent (Clist)) = N_Record_Definition then
3797 Clist := Parent (Parent ((Clist)));
3799 -- If neither of these two cases, we are at the top of
3803 exit Component_List_Loop;
3805 end loop Component_List_Loop;
3807 <<Continue_Main_Component_Loop>>
3808 Next_Entity (C1_Ent);
3810 end loop Main_Component_Loop;
3814 -- The following circuit deals with warning on record holes (gaps). We
3815 -- skip this check if overlap was detected, since it makes sense for the
3816 -- programmer to fix this illegality before worrying about warnings.
3818 if not Overlap_Detected and Warn_On_Record_Holes then
3819 Record_Hole_Check : declare
3820 Decl : constant Node_Id := Declaration_Node (Base_Type (Rectype));
3821 -- Full declaration of record type
3823 procedure Check_Component_List
3827 -- Check component list CL for holes. The starting bit should be
3828 -- Sbit. which is zero for the main record component list and set
3829 -- appropriately for recursive calls for variants. DS is set to
3830 -- a list of discriminant specifications to be included in the
3831 -- consideration of components. It is No_List if none to consider.
3833 --------------------------
3834 -- Check_Component_List --
3835 --------------------------
3837 procedure Check_Component_List
3845 Compl := Integer (List_Length (Component_Items (CL)));
3847 if DS /= No_List then
3848 Compl := Compl + Integer (List_Length (DS));
3852 Comps : array (Natural range 0 .. Compl) of Entity_Id;
3853 -- Gather components (zero entry is for sort routine)
3855 Ncomps : Natural := 0;
3856 -- Number of entries stored in Comps (starting at Comps (1))
3859 -- One component item or discriminant specification
3862 -- Starting bit for next component
3870 function Lt (Op1, Op2 : Natural) return Boolean;
3871 -- Compare routine for Sort
3873 procedure Move (From : Natural; To : Natural);
3874 -- Move routine for Sort
3876 package Sorting is new GNAT.Heap_Sort_G (Move, Lt);
3882 function Lt (Op1, Op2 : Natural) return Boolean is
3884 return Component_Bit_Offset (Comps (Op1))
3886 Component_Bit_Offset (Comps (Op2));
3893 procedure Move (From : Natural; To : Natural) is
3895 Comps (To) := Comps (From);
3899 -- Gather discriminants into Comp
3901 if DS /= No_List then
3902 Citem := First (DS);
3903 while Present (Citem) loop
3904 if Nkind (Citem) = N_Discriminant_Specification then
3906 Ent : constant Entity_Id :=
3907 Defining_Identifier (Citem);
3909 if Ekind (Ent) = E_Discriminant then
3910 Ncomps := Ncomps + 1;
3911 Comps (Ncomps) := Ent;
3920 -- Gather component entities into Comp
3922 Citem := First (Component_Items (CL));
3923 while Present (Citem) loop
3924 if Nkind (Citem) = N_Component_Declaration then
3925 Ncomps := Ncomps + 1;
3926 Comps (Ncomps) := Defining_Identifier (Citem);
3932 -- Now sort the component entities based on the first bit.
3933 -- Note we already know there are no overlapping components.
3935 Sorting.Sort (Ncomps);
3937 -- Loop through entries checking for holes
3940 for J in 1 .. Ncomps loop
3942 Error_Msg_Uint_1 := Component_Bit_Offset (CEnt) - Nbit;
3944 if Error_Msg_Uint_1 > 0 then
3946 ("?^-bit gap before component&",
3947 Component_Name (Component_Clause (CEnt)), CEnt);
3950 Nbit := Component_Bit_Offset (CEnt) + Esize (CEnt);
3953 -- Process variant parts recursively if present
3955 if Present (Variant_Part (CL)) then
3956 Variant := First (Variants (Variant_Part (CL)));
3957 while Present (Variant) loop
3958 Check_Component_List
3959 (Component_List (Variant), Nbit, No_List);
3964 end Check_Component_List;
3966 -- Start of processing for Record_Hole_Check
3973 if Is_Tagged_Type (Rectype) then
3974 Sbit := UI_From_Int (System_Address_Size);
3979 if Nkind (Decl) = N_Full_Type_Declaration
3980 and then Nkind (Type_Definition (Decl)) = N_Record_Definition
3982 Check_Component_List
3983 (Component_List (Type_Definition (Decl)),
3985 Discriminant_Specifications (Decl));
3988 end Record_Hole_Check;
3991 -- For records that have component clauses for all components, and whose
3992 -- size is less than or equal to 32, we need to know the size in the
3993 -- front end to activate possible packed array processing where the
3994 -- component type is a record.
3996 -- At this stage Hbit + 1 represents the first unused bit from all the
3997 -- component clauses processed, so if the component clauses are
3998 -- complete, then this is the length of the record.
4000 -- For records longer than System.Storage_Unit, and for those where not
4001 -- all components have component clauses, the back end determines the
4002 -- length (it may for example be appropriate to round up the size
4003 -- to some convenient boundary, based on alignment considerations, etc).
4005 if Unknown_RM_Size (Rectype) and then Hbit + 1 <= 32 then
4007 -- Nothing to do if at least one component has no component clause
4009 Comp := First_Component_Or_Discriminant (Rectype);
4010 while Present (Comp) loop
4011 exit when No (Component_Clause (Comp));
4012 Next_Component_Or_Discriminant (Comp);
4015 -- If we fall out of loop, all components have component clauses
4016 -- and so we can set the size to the maximum value.
4019 Set_RM_Size (Rectype, Hbit + 1);
4022 end Check_Record_Representation_Clause;
4028 procedure Check_Size
4032 Biased : out Boolean)
4034 UT : constant Entity_Id := Underlying_Type (T);
4040 -- Dismiss cases for generic types or types with previous errors
4043 or else UT = Any_Type
4044 or else Is_Generic_Type (UT)
4045 or else Is_Generic_Type (Root_Type (UT))
4049 -- Check case of bit packed array
4051 elsif Is_Array_Type (UT)
4052 and then Known_Static_Component_Size (UT)
4053 and then Is_Bit_Packed_Array (UT)
4061 Asiz := Component_Size (UT);
4062 Indx := First_Index (UT);
4064 Ityp := Etype (Indx);
4066 -- If non-static bound, then we are not in the business of
4067 -- trying to check the length, and indeed an error will be
4068 -- issued elsewhere, since sizes of non-static array types
4069 -- cannot be set implicitly or explicitly.
4071 if not Is_Static_Subtype (Ityp) then
4075 -- Otherwise accumulate next dimension
4077 Asiz := Asiz * (Expr_Value (Type_High_Bound (Ityp)) -
4078 Expr_Value (Type_Low_Bound (Ityp)) +
4082 exit when No (Indx);
4088 Error_Msg_Uint_1 := Asiz;
4090 ("size for& too small, minimum allowed is ^", N, T);
4091 Set_Esize (T, Asiz);
4092 Set_RM_Size (T, Asiz);
4096 -- All other composite types are ignored
4098 elsif Is_Composite_Type (UT) then
4101 -- For fixed-point types, don't check minimum if type is not frozen,
4102 -- since we don't know all the characteristics of the type that can
4103 -- affect the size (e.g. a specified small) till freeze time.
4105 elsif Is_Fixed_Point_Type (UT)
4106 and then not Is_Frozen (UT)
4110 -- Cases for which a minimum check is required
4113 -- Ignore if specified size is correct for the type
4115 if Known_Esize (UT) and then Siz = Esize (UT) then
4119 -- Otherwise get minimum size
4121 M := UI_From_Int (Minimum_Size (UT));
4125 -- Size is less than minimum size, but one possibility remains
4126 -- that we can manage with the new size if we bias the type.
4128 M := UI_From_Int (Minimum_Size (UT, Biased => True));
4131 Error_Msg_Uint_1 := M;
4133 ("size for& too small, minimum allowed is ^", N, T);
4143 -------------------------
4144 -- Get_Alignment_Value --
4145 -------------------------
4147 function Get_Alignment_Value (Expr : Node_Id) return Uint is
4148 Align : constant Uint := Static_Integer (Expr);
4151 if Align = No_Uint then
4154 elsif Align <= 0 then
4155 Error_Msg_N ("alignment value must be positive", Expr);
4159 for J in Int range 0 .. 64 loop
4161 M : constant Uint := Uint_2 ** J;
4164 exit when M = Align;
4168 ("alignment value must be power of 2", Expr);
4176 end Get_Alignment_Value;
4182 procedure Initialize is
4184 Unchecked_Conversions.Init;
4187 -------------------------
4188 -- Is_Operational_Item --
4189 -------------------------
4191 function Is_Operational_Item (N : Node_Id) return Boolean is
4193 if Nkind (N) /= N_Attribute_Definition_Clause then
4197 Id : constant Attribute_Id := Get_Attribute_Id (Chars (N));
4199 return Id = Attribute_Input
4200 or else Id = Attribute_Output
4201 or else Id = Attribute_Read
4202 or else Id = Attribute_Write
4203 or else Id = Attribute_External_Tag;
4206 end Is_Operational_Item;
4212 function Minimum_Size
4214 Biased : Boolean := False) return Nat
4216 Lo : Uint := No_Uint;
4217 Hi : Uint := No_Uint;
4218 LoR : Ureal := No_Ureal;
4219 HiR : Ureal := No_Ureal;
4220 LoSet : Boolean := False;
4221 HiSet : Boolean := False;
4225 R_Typ : constant Entity_Id := Root_Type (T);
4228 -- If bad type, return 0
4230 if T = Any_Type then
4233 -- For generic types, just return zero. There cannot be any legitimate
4234 -- need to know such a size, but this routine may be called with a
4235 -- generic type as part of normal processing.
4237 elsif Is_Generic_Type (R_Typ)
4238 or else R_Typ = Any_Type
4242 -- Access types. Normally an access type cannot have a size smaller
4243 -- than the size of System.Address. The exception is on VMS, where
4244 -- we have short and long addresses, and it is possible for an access
4245 -- type to have a short address size (and thus be less than the size
4246 -- of System.Address itself). We simply skip the check for VMS, and
4247 -- leave it to the back end to do the check.
4249 elsif Is_Access_Type (T) then
4250 if OpenVMS_On_Target then
4253 return System_Address_Size;
4256 -- Floating-point types
4258 elsif Is_Floating_Point_Type (T) then
4259 return UI_To_Int (Esize (R_Typ));
4263 elsif Is_Discrete_Type (T) then
4265 -- The following loop is looking for the nearest compile time known
4266 -- bounds following the ancestor subtype chain. The idea is to find
4267 -- the most restrictive known bounds information.
4271 if Ancest = Any_Type or else Etype (Ancest) = Any_Type then
4276 if Compile_Time_Known_Value (Type_Low_Bound (Ancest)) then
4277 Lo := Expr_Rep_Value (Type_Low_Bound (Ancest));
4284 if Compile_Time_Known_Value (Type_High_Bound (Ancest)) then
4285 Hi := Expr_Rep_Value (Type_High_Bound (Ancest));
4291 Ancest := Ancestor_Subtype (Ancest);
4294 Ancest := Base_Type (T);
4296 if Is_Generic_Type (Ancest) then
4302 -- Fixed-point types. We can't simply use Expr_Value to get the
4303 -- Corresponding_Integer_Value values of the bounds, since these do not
4304 -- get set till the type is frozen, and this routine can be called
4305 -- before the type is frozen. Similarly the test for bounds being static
4306 -- needs to include the case where we have unanalyzed real literals for
4309 elsif Is_Fixed_Point_Type (T) then
4311 -- The following loop is looking for the nearest compile time known
4312 -- bounds following the ancestor subtype chain. The idea is to find
4313 -- the most restrictive known bounds information.
4317 if Ancest = Any_Type or else Etype (Ancest) = Any_Type then
4321 -- Note: In the following two tests for LoSet and HiSet, it may
4322 -- seem redundant to test for N_Real_Literal here since normally
4323 -- one would assume that the test for the value being known at
4324 -- compile time includes this case. However, there is a glitch.
4325 -- If the real literal comes from folding a non-static expression,
4326 -- then we don't consider any non- static expression to be known
4327 -- at compile time if we are in configurable run time mode (needed
4328 -- in some cases to give a clearer definition of what is and what
4329 -- is not accepted). So the test is indeed needed. Without it, we
4330 -- would set neither Lo_Set nor Hi_Set and get an infinite loop.
4333 if Nkind (Type_Low_Bound (Ancest)) = N_Real_Literal
4334 or else Compile_Time_Known_Value (Type_Low_Bound (Ancest))
4336 LoR := Expr_Value_R (Type_Low_Bound (Ancest));
4343 if Nkind (Type_High_Bound (Ancest)) = N_Real_Literal
4344 or else Compile_Time_Known_Value (Type_High_Bound (Ancest))
4346 HiR := Expr_Value_R (Type_High_Bound (Ancest));
4352 Ancest := Ancestor_Subtype (Ancest);
4355 Ancest := Base_Type (T);
4357 if Is_Generic_Type (Ancest) then
4363 Lo := UR_To_Uint (LoR / Small_Value (T));
4364 Hi := UR_To_Uint (HiR / Small_Value (T));
4366 -- No other types allowed
4369 raise Program_Error;
4372 -- Fall through with Hi and Lo set. Deal with biased case
4375 and then not Is_Fixed_Point_Type (T)
4376 and then not (Is_Enumeration_Type (T)
4377 and then Has_Non_Standard_Rep (T)))
4378 or else Has_Biased_Representation (T)
4384 -- Signed case. Note that we consider types like range 1 .. -1 to be
4385 -- signed for the purpose of computing the size, since the bounds have
4386 -- to be accommodated in the base type.
4388 if Lo < 0 or else Hi < 0 then
4392 -- S = size, B = 2 ** (size - 1) (can accommodate -B .. +(B - 1))
4393 -- Note that we accommodate the case where the bounds cross. This
4394 -- can happen either because of the way the bounds are declared
4395 -- or because of the algorithm in Freeze_Fixed_Point_Type.
4409 -- If both bounds are positive, make sure that both are represen-
4410 -- table in the case where the bounds are crossed. This can happen
4411 -- either because of the way the bounds are declared, or because of
4412 -- the algorithm in Freeze_Fixed_Point_Type.
4418 -- S = size, (can accommodate 0 .. (2**size - 1))
4421 while Hi >= Uint_2 ** S loop
4429 ---------------------------
4430 -- New_Stream_Subprogram --
4431 ---------------------------
4433 procedure New_Stream_Subprogram
4437 Nam : TSS_Name_Type)
4439 Loc : constant Source_Ptr := Sloc (N);
4440 Sname : constant Name_Id := Make_TSS_Name (Base_Type (Ent), Nam);
4441 Subp_Id : Entity_Id;
4442 Subp_Decl : Node_Id;
4446 Defer_Declaration : constant Boolean :=
4447 Is_Tagged_Type (Ent) or else Is_Private_Type (Ent);
4448 -- For a tagged type, there is a declaration for each stream attribute
4449 -- at the freeze point, and we must generate only a completion of this
4450 -- declaration. We do the same for private types, because the full view
4451 -- might be tagged. Otherwise we generate a declaration at the point of
4452 -- the attribute definition clause.
4454 function Build_Spec return Node_Id;
4455 -- Used for declaration and renaming declaration, so that this is
4456 -- treated as a renaming_as_body.
4462 function Build_Spec return Node_Id is
4463 Out_P : constant Boolean := (Nam = TSS_Stream_Read);
4466 T_Ref : constant Node_Id := New_Reference_To (Etyp, Loc);
4469 Subp_Id := Make_Defining_Identifier (Loc, Sname);
4471 -- S : access Root_Stream_Type'Class
4473 Formals := New_List (
4474 Make_Parameter_Specification (Loc,
4475 Defining_Identifier =>
4476 Make_Defining_Identifier (Loc, Name_S),
4478 Make_Access_Definition (Loc,
4481 Designated_Type (Etype (F)), Loc))));
4483 if Nam = TSS_Stream_Input then
4484 Spec := Make_Function_Specification (Loc,
4485 Defining_Unit_Name => Subp_Id,
4486 Parameter_Specifications => Formals,
4487 Result_Definition => T_Ref);
4492 Make_Parameter_Specification (Loc,
4493 Defining_Identifier => Make_Defining_Identifier (Loc, Name_V),
4494 Out_Present => Out_P,
4495 Parameter_Type => T_Ref));
4498 Make_Procedure_Specification (Loc,
4499 Defining_Unit_Name => Subp_Id,
4500 Parameter_Specifications => Formals);
4506 -- Start of processing for New_Stream_Subprogram
4509 F := First_Formal (Subp);
4511 if Ekind (Subp) = E_Procedure then
4512 Etyp := Etype (Next_Formal (F));
4514 Etyp := Etype (Subp);
4517 -- Prepare subprogram declaration and insert it as an action on the
4518 -- clause node. The visibility for this entity is used to test for
4519 -- visibility of the attribute definition clause (in the sense of
4520 -- 8.3(23) as amended by AI-195).
4522 if not Defer_Declaration then
4524 Make_Subprogram_Declaration (Loc,
4525 Specification => Build_Spec);
4527 -- For a tagged type, there is always a visible declaration for each
4528 -- stream TSS (it is a predefined primitive operation), and the
4529 -- completion of this declaration occurs at the freeze point, which is
4530 -- not always visible at places where the attribute definition clause is
4531 -- visible. So, we create a dummy entity here for the purpose of
4532 -- tracking the visibility of the attribute definition clause itself.
4536 Make_Defining_Identifier (Loc,
4537 Chars => New_External_Name (Sname, 'V'));
4539 Make_Object_Declaration (Loc,
4540 Defining_Identifier => Subp_Id,
4541 Object_Definition => New_Occurrence_Of (Standard_Boolean, Loc));
4544 Insert_Action (N, Subp_Decl);
4545 Set_Entity (N, Subp_Id);
4548 Make_Subprogram_Renaming_Declaration (Loc,
4549 Specification => Build_Spec,
4550 Name => New_Reference_To (Subp, Loc));
4552 if Defer_Declaration then
4553 Set_TSS (Base_Type (Ent), Subp_Id);
4555 Insert_Action (N, Subp_Decl);
4556 Copy_TSS (Subp_Id, Base_Type (Ent));
4558 end New_Stream_Subprogram;
4560 ------------------------
4561 -- Rep_Item_Too_Early --
4562 ------------------------
4564 function Rep_Item_Too_Early (T : Entity_Id; N : Node_Id) return Boolean is
4566 -- Cannot apply non-operational rep items to generic types
4568 if Is_Operational_Item (N) then
4572 and then Is_Generic_Type (Root_Type (T))
4574 Error_Msg_N ("representation item not allowed for generic type", N);
4578 -- Otherwise check for incomplete type
4580 if Is_Incomplete_Or_Private_Type (T)
4581 and then No (Underlying_Type (T))
4584 ("representation item must be after full type declaration", N);
4587 -- If the type has incomplete components, a representation clause is
4588 -- illegal but stream attributes and Convention pragmas are correct.
4590 elsif Has_Private_Component (T) then
4591 if Nkind (N) = N_Pragma then
4595 ("representation item must appear after type is fully defined",
4602 end Rep_Item_Too_Early;
4604 -----------------------
4605 -- Rep_Item_Too_Late --
4606 -----------------------
4608 function Rep_Item_Too_Late
4611 FOnly : Boolean := False) return Boolean
4614 Parent_Type : Entity_Id;
4617 -- Output the too late message. Note that this is not considered a
4618 -- serious error, since the effect is simply that we ignore the
4619 -- representation clause in this case.
4625 procedure Too_Late is
4627 Error_Msg_N ("|representation item appears too late!", N);
4630 -- Start of processing for Rep_Item_Too_Late
4633 -- First make sure entity is not frozen (RM 13.1(9)). Exclude imported
4634 -- types, which may be frozen if they appear in a representation clause
4635 -- for a local type.
4638 and then not From_With_Type (T)
4641 S := First_Subtype (T);
4643 if Present (Freeze_Node (S)) then
4645 ("?no more representation items for }", Freeze_Node (S), S);
4650 -- Check for case of non-tagged derived type whose parent either has
4651 -- primitive operations, or is a by reference type (RM 13.1(10)).
4655 and then Is_Derived_Type (T)
4656 and then not Is_Tagged_Type (T)
4658 Parent_Type := Etype (Base_Type (T));
4660 if Has_Primitive_Operations (Parent_Type) then
4663 ("primitive operations already defined for&!", N, Parent_Type);
4666 elsif Is_By_Reference_Type (Parent_Type) then
4669 ("parent type & is a by reference type!", N, Parent_Type);
4674 -- No error, link item into head of chain of rep items for the entity,
4675 -- but avoid chaining if we have an overloadable entity, and the pragma
4676 -- is one that can apply to multiple overloaded entities.
4678 if Is_Overloadable (T)
4679 and then Nkind (N) = N_Pragma
4682 Pname : constant Name_Id := Pragma_Name (N);
4684 if Pname = Name_Convention or else
4685 Pname = Name_Import or else
4686 Pname = Name_Export or else
4687 Pname = Name_External or else
4688 Pname = Name_Interface
4695 Record_Rep_Item (T, N);
4697 end Rep_Item_Too_Late;
4699 -------------------------
4700 -- Same_Representation --
4701 -------------------------
4703 function Same_Representation (Typ1, Typ2 : Entity_Id) return Boolean is
4704 T1 : constant Entity_Id := Underlying_Type (Typ1);
4705 T2 : constant Entity_Id := Underlying_Type (Typ2);
4708 -- A quick check, if base types are the same, then we definitely have
4709 -- the same representation, because the subtype specific representation
4710 -- attributes (Size and Alignment) do not affect representation from
4711 -- the point of view of this test.
4713 if Base_Type (T1) = Base_Type (T2) then
4716 elsif Is_Private_Type (Base_Type (T2))
4717 and then Base_Type (T1) = Full_View (Base_Type (T2))
4722 -- Tagged types never have differing representations
4724 if Is_Tagged_Type (T1) then
4728 -- Representations are definitely different if conventions differ
4730 if Convention (T1) /= Convention (T2) then
4734 -- Representations are different if component alignments differ
4736 if (Is_Record_Type (T1) or else Is_Array_Type (T1))
4738 (Is_Record_Type (T2) or else Is_Array_Type (T2))
4739 and then Component_Alignment (T1) /= Component_Alignment (T2)
4744 -- For arrays, the only real issue is component size. If we know the
4745 -- component size for both arrays, and it is the same, then that's
4746 -- good enough to know we don't have a change of representation.
4748 if Is_Array_Type (T1) then
4749 if Known_Component_Size (T1)
4750 and then Known_Component_Size (T2)
4751 and then Component_Size (T1) = Component_Size (T2)
4757 -- Types definitely have same representation if neither has non-standard
4758 -- representation since default representations are always consistent.
4759 -- If only one has non-standard representation, and the other does not,
4760 -- then we consider that they do not have the same representation. They
4761 -- might, but there is no way of telling early enough.
4763 if Has_Non_Standard_Rep (T1) then
4764 if not Has_Non_Standard_Rep (T2) then
4768 return not Has_Non_Standard_Rep (T2);
4771 -- Here the two types both have non-standard representation, and we need
4772 -- to determine if they have the same non-standard representation.
4774 -- For arrays, we simply need to test if the component sizes are the
4775 -- same. Pragma Pack is reflected in modified component sizes, so this
4776 -- check also deals with pragma Pack.
4778 if Is_Array_Type (T1) then
4779 return Component_Size (T1) = Component_Size (T2);
4781 -- Tagged types always have the same representation, because it is not
4782 -- possible to specify different representations for common fields.
4784 elsif Is_Tagged_Type (T1) then
4787 -- Case of record types
4789 elsif Is_Record_Type (T1) then
4791 -- Packed status must conform
4793 if Is_Packed (T1) /= Is_Packed (T2) then
4796 -- Otherwise we must check components. Typ2 maybe a constrained
4797 -- subtype with fewer components, so we compare the components
4798 -- of the base types.
4801 Record_Case : declare
4802 CD1, CD2 : Entity_Id;
4804 function Same_Rep return Boolean;
4805 -- CD1 and CD2 are either components or discriminants. This
4806 -- function tests whether the two have the same representation
4812 function Same_Rep return Boolean is
4814 if No (Component_Clause (CD1)) then
4815 return No (Component_Clause (CD2));
4819 Present (Component_Clause (CD2))
4821 Component_Bit_Offset (CD1) = Component_Bit_Offset (CD2)
4823 Esize (CD1) = Esize (CD2);
4827 -- Start of processing for Record_Case
4830 if Has_Discriminants (T1) then
4831 CD1 := First_Discriminant (T1);
4832 CD2 := First_Discriminant (T2);
4834 -- The number of discriminants may be different if the
4835 -- derived type has fewer (constrained by values). The
4836 -- invisible discriminants retain the representation of
4837 -- the original, so the discrepancy does not per se
4838 -- indicate a different representation.
4841 and then Present (CD2)
4843 if not Same_Rep then
4846 Next_Discriminant (CD1);
4847 Next_Discriminant (CD2);
4852 CD1 := First_Component (Underlying_Type (Base_Type (T1)));
4853 CD2 := First_Component (Underlying_Type (Base_Type (T2)));
4855 while Present (CD1) loop
4856 if not Same_Rep then
4859 Next_Component (CD1);
4860 Next_Component (CD2);
4868 -- For enumeration types, we must check each literal to see if the
4869 -- representation is the same. Note that we do not permit enumeration
4870 -- representation clauses for Character and Wide_Character, so these
4871 -- cases were already dealt with.
4873 elsif Is_Enumeration_Type (T1) then
4874 Enumeration_Case : declare
4878 L1 := First_Literal (T1);
4879 L2 := First_Literal (T2);
4881 while Present (L1) loop
4882 if Enumeration_Rep (L1) /= Enumeration_Rep (L2) then
4892 end Enumeration_Case;
4894 -- Any other types have the same representation for these purposes
4899 end Same_Representation;
4905 procedure Set_Biased
4909 Biased : Boolean := True)
4913 Set_Has_Biased_Representation (E);
4915 if Warn_On_Biased_Representation then
4917 ("?" & Msg & " forces biased representation for&", N, E);
4922 --------------------
4923 -- Set_Enum_Esize --
4924 --------------------
4926 procedure Set_Enum_Esize (T : Entity_Id) is
4934 -- Find the minimum standard size (8,16,32,64) that fits
4936 Lo := Enumeration_Rep (Entity (Type_Low_Bound (T)));
4937 Hi := Enumeration_Rep (Entity (Type_High_Bound (T)));
4940 if Lo >= -Uint_2**07 and then Hi < Uint_2**07 then
4941 Sz := Standard_Character_Size; -- May be > 8 on some targets
4943 elsif Lo >= -Uint_2**15 and then Hi < Uint_2**15 then
4946 elsif Lo >= -Uint_2**31 and then Hi < Uint_2**31 then
4949 else pragma Assert (Lo >= -Uint_2**63 and then Hi < Uint_2**63);
4954 if Hi < Uint_2**08 then
4955 Sz := Standard_Character_Size; -- May be > 8 on some targets
4957 elsif Hi < Uint_2**16 then
4960 elsif Hi < Uint_2**32 then
4963 else pragma Assert (Hi < Uint_2**63);
4968 -- That minimum is the proper size unless we have a foreign convention
4969 -- and the size required is 32 or less, in which case we bump the size
4970 -- up to 32. This is required for C and C++ and seems reasonable for
4971 -- all other foreign conventions.
4973 if Has_Foreign_Convention (T)
4974 and then Esize (T) < Standard_Integer_Size
4976 Init_Esize (T, Standard_Integer_Size);
4982 ------------------------------
4983 -- Validate_Address_Clauses --
4984 ------------------------------
4986 procedure Validate_Address_Clauses is
4988 for J in Address_Clause_Checks.First .. Address_Clause_Checks.Last loop
4990 ACCR : Address_Clause_Check_Record
4991 renames Address_Clause_Checks.Table (J);
5002 -- Skip processing of this entry if warning already posted
5004 if not Address_Warning_Posted (ACCR.N) then
5006 Expr := Original_Node (Expression (ACCR.N));
5010 X_Alignment := Alignment (ACCR.X);
5011 Y_Alignment := Alignment (ACCR.Y);
5013 -- Similarly obtain sizes
5015 X_Size := Esize (ACCR.X);
5016 Y_Size := Esize (ACCR.Y);
5018 -- Check for large object overlaying smaller one
5021 and then X_Size > Uint_0
5022 and then X_Size > Y_Size
5025 ("?& overlays smaller object", ACCR.N, ACCR.X);
5027 ("\?program execution may be erroneous", ACCR.N);
5028 Error_Msg_Uint_1 := X_Size;
5030 ("\?size of & is ^", ACCR.N, ACCR.X);
5031 Error_Msg_Uint_1 := Y_Size;
5033 ("\?size of & is ^", ACCR.N, ACCR.Y);
5035 -- Check for inadequate alignment, both of the base object
5036 -- and of the offset, if any.
5038 -- Note: we do not check the alignment if we gave a size
5039 -- warning, since it would likely be redundant.
5041 elsif Y_Alignment /= Uint_0
5042 and then (Y_Alignment < X_Alignment
5045 Nkind (Expr) = N_Attribute_Reference
5047 Attribute_Name (Expr) = Name_Address
5049 Has_Compatible_Alignment
5050 (ACCR.X, Prefix (Expr))
5051 /= Known_Compatible))
5054 ("?specified address for& may be inconsistent "
5058 ("\?program execution may be erroneous (RM 13.3(27))",
5060 Error_Msg_Uint_1 := X_Alignment;
5062 ("\?alignment of & is ^",
5064 Error_Msg_Uint_1 := Y_Alignment;
5066 ("\?alignment of & is ^",
5068 if Y_Alignment >= X_Alignment then
5070 ("\?but offset is not multiple of alignment",
5077 end Validate_Address_Clauses;
5079 -----------------------------------
5080 -- Validate_Unchecked_Conversion --
5081 -----------------------------------
5083 procedure Validate_Unchecked_Conversion
5085 Act_Unit : Entity_Id)
5092 -- Obtain source and target types. Note that we call Ancestor_Subtype
5093 -- here because the processing for generic instantiation always makes
5094 -- subtypes, and we want the original frozen actual types.
5096 -- If we are dealing with private types, then do the check on their
5097 -- fully declared counterparts if the full declarations have been
5098 -- encountered (they don't have to be visible, but they must exist!)
5100 Source := Ancestor_Subtype (Etype (First_Formal (Act_Unit)));
5102 if Is_Private_Type (Source)
5103 and then Present (Underlying_Type (Source))
5105 Source := Underlying_Type (Source);
5108 Target := Ancestor_Subtype (Etype (Act_Unit));
5110 -- If either type is generic, the instantiation happens within a generic
5111 -- unit, and there is nothing to check. The proper check
5112 -- will happen when the enclosing generic is instantiated.
5114 if Is_Generic_Type (Source) or else Is_Generic_Type (Target) then
5118 if Is_Private_Type (Target)
5119 and then Present (Underlying_Type (Target))
5121 Target := Underlying_Type (Target);
5124 -- Source may be unconstrained array, but not target
5126 if Is_Array_Type (Target)
5127 and then not Is_Constrained (Target)
5130 ("unchecked conversion to unconstrained array not allowed", N);
5134 -- Warn if conversion between two different convention pointers
5136 if Is_Access_Type (Target)
5137 and then Is_Access_Type (Source)
5138 and then Convention (Target) /= Convention (Source)
5139 and then Warn_On_Unchecked_Conversion
5141 -- Give warnings for subprogram pointers only on most targets. The
5142 -- exception is VMS, where data pointers can have different lengths
5143 -- depending on the pointer convention.
5145 if Is_Access_Subprogram_Type (Target)
5146 or else Is_Access_Subprogram_Type (Source)
5147 or else OpenVMS_On_Target
5150 ("?conversion between pointers with different conventions!", N);
5154 -- Warn if one of the operands is Ada.Calendar.Time. Do not emit a
5155 -- warning when compiling GNAT-related sources.
5157 if Warn_On_Unchecked_Conversion
5158 and then not In_Predefined_Unit (N)
5159 and then RTU_Loaded (Ada_Calendar)
5161 (Chars (Source) = Name_Time
5163 Chars (Target) = Name_Time)
5165 -- If Ada.Calendar is loaded and the name of one of the operands is
5166 -- Time, there is a good chance that this is Ada.Calendar.Time.
5169 Calendar_Time : constant Entity_Id :=
5170 Full_View (RTE (RO_CA_Time));
5172 pragma Assert (Present (Calendar_Time));
5174 if Source = Calendar_Time
5175 or else Target = Calendar_Time
5178 ("?representation of 'Time values may change between " &
5179 "'G'N'A'T versions", N);
5184 -- Make entry in unchecked conversion table for later processing by
5185 -- Validate_Unchecked_Conversions, which will check sizes and alignments
5186 -- (using values set by the back-end where possible). This is only done
5187 -- if the appropriate warning is active.
5189 if Warn_On_Unchecked_Conversion then
5190 Unchecked_Conversions.Append
5191 (New_Val => UC_Entry'
5196 -- If both sizes are known statically now, then back end annotation
5197 -- is not required to do a proper check but if either size is not
5198 -- known statically, then we need the annotation.
5200 if Known_Static_RM_Size (Source)
5201 and then Known_Static_RM_Size (Target)
5205 Back_Annotate_Rep_Info := True;
5209 -- If unchecked conversion to access type, and access type is declared
5210 -- in the same unit as the unchecked conversion, then set the
5211 -- No_Strict_Aliasing flag (no strict aliasing is implicit in this
5214 if Is_Access_Type (Target) and then
5215 In_Same_Source_Unit (Target, N)
5217 Set_No_Strict_Aliasing (Implementation_Base_Type (Target));
5220 -- Generate N_Validate_Unchecked_Conversion node for back end in
5221 -- case the back end needs to perform special validation checks.
5223 -- Shouldn't this be in Exp_Ch13, since the check only gets done
5224 -- if we have full expansion and the back end is called ???
5227 Make_Validate_Unchecked_Conversion (Sloc (N));
5228 Set_Source_Type (Vnode, Source);
5229 Set_Target_Type (Vnode, Target);
5231 -- If the unchecked conversion node is in a list, just insert before it.
5232 -- If not we have some strange case, not worth bothering about.
5234 if Is_List_Member (N) then
5235 Insert_After (N, Vnode);
5237 end Validate_Unchecked_Conversion;
5239 ------------------------------------
5240 -- Validate_Unchecked_Conversions --
5241 ------------------------------------
5243 procedure Validate_Unchecked_Conversions is
5245 for N in Unchecked_Conversions.First .. Unchecked_Conversions.Last loop
5247 T : UC_Entry renames Unchecked_Conversions.Table (N);
5249 Eloc : constant Source_Ptr := T.Eloc;
5250 Source : constant Entity_Id := T.Source;
5251 Target : constant Entity_Id := T.Target;
5257 -- This validation check, which warns if we have unequal sizes for
5258 -- unchecked conversion, and thus potentially implementation
5259 -- dependent semantics, is one of the few occasions on which we
5260 -- use the official RM size instead of Esize. See description in
5261 -- Einfo "Handling of Type'Size Values" for details.
5263 if Serious_Errors_Detected = 0
5264 and then Known_Static_RM_Size (Source)
5265 and then Known_Static_RM_Size (Target)
5267 -- Don't do the check if warnings off for either type, note the
5268 -- deliberate use of OR here instead of OR ELSE to get the flag
5269 -- Warnings_Off_Used set for both types if appropriate.
5271 and then not (Has_Warnings_Off (Source)
5273 Has_Warnings_Off (Target))
5275 Source_Siz := RM_Size (Source);
5276 Target_Siz := RM_Size (Target);
5278 if Source_Siz /= Target_Siz then
5280 ("?types for unchecked conversion have different sizes!",
5283 if All_Errors_Mode then
5284 Error_Msg_Name_1 := Chars (Source);
5285 Error_Msg_Uint_1 := Source_Siz;
5286 Error_Msg_Name_2 := Chars (Target);
5287 Error_Msg_Uint_2 := Target_Siz;
5288 Error_Msg ("\size of % is ^, size of % is ^?", Eloc);
5290 Error_Msg_Uint_1 := UI_Abs (Source_Siz - Target_Siz);
5292 if Is_Discrete_Type (Source)
5293 and then Is_Discrete_Type (Target)
5295 if Source_Siz > Target_Siz then
5297 ("\?^ high order bits of source will be ignored!",
5300 elsif Is_Unsigned_Type (Source) then
5302 ("\?source will be extended with ^ high order " &
5303 "zero bits?!", Eloc);
5307 ("\?source will be extended with ^ high order " &
5312 elsif Source_Siz < Target_Siz then
5313 if Is_Discrete_Type (Target) then
5314 if Bytes_Big_Endian then
5316 ("\?target value will include ^ undefined " &
5321 ("\?target value will include ^ undefined " &
5328 ("\?^ trailing bits of target value will be " &
5329 "undefined!", Eloc);
5332 else pragma Assert (Source_Siz > Target_Siz);
5334 ("\?^ trailing bits of source will be ignored!",
5341 -- If both types are access types, we need to check the alignment.
5342 -- If the alignment of both is specified, we can do it here.
5344 if Serious_Errors_Detected = 0
5345 and then Ekind (Source) in Access_Kind
5346 and then Ekind (Target) in Access_Kind
5347 and then Target_Strict_Alignment
5348 and then Present (Designated_Type (Source))
5349 and then Present (Designated_Type (Target))
5352 D_Source : constant Entity_Id := Designated_Type (Source);
5353 D_Target : constant Entity_Id := Designated_Type (Target);
5356 if Known_Alignment (D_Source)
5357 and then Known_Alignment (D_Target)
5360 Source_Align : constant Uint := Alignment (D_Source);
5361 Target_Align : constant Uint := Alignment (D_Target);
5364 if Source_Align < Target_Align
5365 and then not Is_Tagged_Type (D_Source)
5367 -- Suppress warning if warnings suppressed on either
5368 -- type or either designated type. Note the use of
5369 -- OR here instead of OR ELSE. That is intentional,
5370 -- we would like to set flag Warnings_Off_Used in
5371 -- all types for which warnings are suppressed.
5373 and then not (Has_Warnings_Off (D_Source)
5375 Has_Warnings_Off (D_Target)
5377 Has_Warnings_Off (Source)
5379 Has_Warnings_Off (Target))
5381 Error_Msg_Uint_1 := Target_Align;
5382 Error_Msg_Uint_2 := Source_Align;
5383 Error_Msg_Node_1 := D_Target;
5384 Error_Msg_Node_2 := D_Source;
5386 ("?alignment of & (^) is stricter than " &
5387 "alignment of & (^)!", Eloc);
5389 ("\?resulting access value may have invalid " &
5390 "alignment!", Eloc);
5398 end Validate_Unchecked_Conversions;