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
109 ----------------------------------------------
110 -- Table for Validate_Unchecked_Conversions --
111 ----------------------------------------------
113 -- The following table collects unchecked conversions for validation.
114 -- Entries are made by Validate_Unchecked_Conversion and then the
115 -- call to Validate_Unchecked_Conversions does the actual error
116 -- checking and posting of warnings. The reason for this delayed
117 -- processing is to take advantage of back-annotations of size and
118 -- alignment values performed by the back end.
120 -- Note: the reason we store a Source_Ptr value instead of a Node_Id
121 -- is that by the time Validate_Unchecked_Conversions is called, Sprint
122 -- will already have modified all Sloc values if the -gnatD option is set.
124 type UC_Entry is record
125 Eloc : Source_Ptr; -- node used for posting warnings
126 Source : Entity_Id; -- source type for unchecked conversion
127 Target : Entity_Id; -- target type for unchecked conversion
130 package Unchecked_Conversions is new Table.Table (
131 Table_Component_Type => UC_Entry,
132 Table_Index_Type => Int,
133 Table_Low_Bound => 1,
135 Table_Increment => 200,
136 Table_Name => "Unchecked_Conversions");
138 ----------------------------------------
139 -- Table for Validate_Address_Clauses --
140 ----------------------------------------
142 -- If an address clause has the form
144 -- for X'Address use Expr
146 -- where Expr is of the form Y'Address or recursively is a reference
147 -- to a constant of either of these forms, and X and Y are entities of
148 -- objects, then if Y has a smaller alignment than X, that merits a
149 -- warning about possible bad alignment. The following table collects
150 -- address clauses of this kind. We put these in a table so that they
151 -- can be checked after the back end has completed annotation of the
152 -- alignments of objects, since we can catch more cases that way.
154 type Address_Clause_Check_Record is record
156 -- The address clause
159 -- The entity of the object overlaying Y
162 -- The entity of the object being overlaid
165 -- Whether the address is offseted within Y
168 package Address_Clause_Checks is new Table.Table (
169 Table_Component_Type => Address_Clause_Check_Record,
170 Table_Index_Type => Int,
171 Table_Low_Bound => 1,
173 Table_Increment => 200,
174 Table_Name => "Address_Clause_Checks");
176 -----------------------------------------
177 -- Adjust_Record_For_Reverse_Bit_Order --
178 -----------------------------------------
180 procedure Adjust_Record_For_Reverse_Bit_Order (R : Entity_Id) is
185 -- Processing depends on version of Ada
187 -- For Ada 95, we just renumber bits within a storage unit. We do the
188 -- same for Ada 83 mode, since we recognize pragma Bit_Order in Ada 83,
189 -- and are free to add this extension.
191 if Ada_Version < Ada_2005 then
192 Comp := First_Component_Or_Discriminant (R);
193 while Present (Comp) loop
194 CC := Component_Clause (Comp);
196 -- If component clause is present, then deal with the non-default
197 -- bit order case for Ada 95 mode.
199 -- We only do this processing for the base type, and in fact that
200 -- is important, since otherwise if there are record subtypes, we
201 -- could reverse the bits once for each subtype, which is wrong.
204 and then Ekind (R) = E_Record_Type
207 CFB : constant Uint := Component_Bit_Offset (Comp);
208 CSZ : constant Uint := Esize (Comp);
209 CLC : constant Node_Id := Component_Clause (Comp);
210 Pos : constant Node_Id := Position (CLC);
211 FB : constant Node_Id := First_Bit (CLC);
213 Storage_Unit_Offset : constant Uint :=
214 CFB / System_Storage_Unit;
216 Start_Bit : constant Uint :=
217 CFB mod System_Storage_Unit;
220 -- Cases where field goes over storage unit boundary
222 if Start_Bit + CSZ > System_Storage_Unit then
224 -- Allow multi-byte field but generate warning
226 if Start_Bit mod System_Storage_Unit = 0
227 and then CSZ mod System_Storage_Unit = 0
230 ("multi-byte field specified with non-standard"
231 & " Bit_Order?", CLC);
233 if Bytes_Big_Endian then
235 ("bytes are not reversed "
236 & "(component is big-endian)?", CLC);
239 ("bytes are not reversed "
240 & "(component is little-endian)?", CLC);
243 -- Do not allow non-contiguous field
247 ("attempt to specify non-contiguous field "
248 & "not permitted", CLC);
250 ("\caused by non-standard Bit_Order "
253 ("\consider possibility of using "
254 & "Ada 2005 mode here", CLC);
257 -- Case where field fits in one storage unit
260 -- Give warning if suspicious component clause
262 if Intval (FB) >= System_Storage_Unit
263 and then Warn_On_Reverse_Bit_Order
266 ("?Bit_Order clause does not affect " &
267 "byte ordering", Pos);
269 Intval (Pos) + Intval (FB) /
272 ("?position normalized to ^ before bit " &
273 "order interpreted", Pos);
276 -- Here is where we fix up the Component_Bit_Offset value
277 -- to account for the reverse bit order. Some examples of
278 -- what needs to be done are:
280 -- First_Bit .. Last_Bit Component_Bit_Offset
292 -- The rule is that the first bit is is obtained by
293 -- subtracting the old ending bit from storage_unit - 1.
295 Set_Component_Bit_Offset
297 (Storage_Unit_Offset * System_Storage_Unit) +
298 (System_Storage_Unit - 1) -
299 (Start_Bit + CSZ - 1));
301 Set_Normalized_First_Bit
303 Component_Bit_Offset (Comp) mod
304 System_Storage_Unit);
309 Next_Component_Or_Discriminant (Comp);
312 -- For Ada 2005, we do machine scalar processing, as fully described In
313 -- AI-133. This involves gathering all components which start at the
314 -- same byte offset and processing them together. Same approach is still
315 -- valid in later versions including Ada 2012.
319 Max_Machine_Scalar_Size : constant Uint :=
321 (Standard_Long_Long_Integer_Size);
322 -- We use this as the maximum machine scalar size
325 SSU : constant Uint := UI_From_Int (System_Storage_Unit);
328 -- This first loop through components does two things. First it
329 -- deals with the case of components with component clauses whose
330 -- length is greater than the maximum machine scalar size (either
331 -- accepting them or rejecting as needed). Second, it counts the
332 -- number of components with component clauses whose length does
333 -- not exceed this maximum for later processing.
336 Comp := First_Component_Or_Discriminant (R);
337 while Present (Comp) loop
338 CC := Component_Clause (Comp);
342 Fbit : constant Uint :=
343 Static_Integer (First_Bit (CC));
346 -- Case of component with size > max machine scalar
348 if Esize (Comp) > Max_Machine_Scalar_Size then
350 -- Must begin on byte boundary
352 if Fbit mod SSU /= 0 then
354 ("illegal first bit value for "
355 & "reverse bit order",
357 Error_Msg_Uint_1 := SSU;
358 Error_Msg_Uint_2 := Max_Machine_Scalar_Size;
361 ("\must be a multiple of ^ "
362 & "if size greater than ^",
365 -- Must end on byte boundary
367 elsif Esize (Comp) mod SSU /= 0 then
369 ("illegal last bit value for "
370 & "reverse bit order",
372 Error_Msg_Uint_1 := SSU;
373 Error_Msg_Uint_2 := Max_Machine_Scalar_Size;
376 ("\must be a multiple of ^ if size "
380 -- OK, give warning if enabled
382 elsif Warn_On_Reverse_Bit_Order then
384 ("multi-byte field specified with "
385 & " non-standard Bit_Order?", CC);
387 if Bytes_Big_Endian then
389 ("\bytes are not reversed "
390 & "(component is big-endian)?", CC);
393 ("\bytes are not reversed "
394 & "(component is little-endian)?", CC);
398 -- Case where size is not greater than max machine
399 -- scalar. For now, we just count these.
402 Num_CC := Num_CC + 1;
407 Next_Component_Or_Discriminant (Comp);
410 -- We need to sort the component clauses on the basis of the
411 -- Position values in the clause, so we can group clauses with
412 -- the same Position. together to determine the relevant machine
416 Comps : array (0 .. Num_CC) of Entity_Id;
417 -- Array to collect component and discriminant entities. The
418 -- data starts at index 1, the 0'th entry is for the sort
421 function CP_Lt (Op1, Op2 : Natural) return Boolean;
422 -- Compare routine for Sort
424 procedure CP_Move (From : Natural; To : Natural);
425 -- Move routine for Sort
427 package Sorting is new GNAT.Heap_Sort_G (CP_Move, CP_Lt);
431 -- Start and stop positions in the component list of the set of
432 -- components with the same starting position (that constitute
433 -- components in a single machine scalar).
436 -- Maximum last bit value of any component in this set
439 -- Corresponding machine scalar size
445 function CP_Lt (Op1, Op2 : Natural) return Boolean is
447 return Position (Component_Clause (Comps (Op1))) <
448 Position (Component_Clause (Comps (Op2)));
455 procedure CP_Move (From : Natural; To : Natural) is
457 Comps (To) := Comps (From);
460 -- Start of processing for Sort_CC
463 -- Collect the component clauses
466 Comp := First_Component_Or_Discriminant (R);
467 while Present (Comp) loop
468 if Present (Component_Clause (Comp))
469 and then Esize (Comp) <= Max_Machine_Scalar_Size
471 Num_CC := Num_CC + 1;
472 Comps (Num_CC) := Comp;
475 Next_Component_Or_Discriminant (Comp);
478 -- Sort by ascending position number
480 Sorting.Sort (Num_CC);
482 -- We now have all the components whose size does not exceed
483 -- the max machine scalar value, sorted by starting position.
484 -- In this loop we gather groups of clauses starting at the
485 -- same position, to process them in accordance with AI-133.
488 while Stop < Num_CC loop
493 (Last_Bit (Component_Clause (Comps (Start))));
494 while Stop < Num_CC loop
496 (Position (Component_Clause (Comps (Stop + 1)))) =
498 (Position (Component_Clause (Comps (Stop))))
506 (Component_Clause (Comps (Stop)))));
512 -- Now we have a group of component clauses from Start to
513 -- Stop whose positions are identical, and MaxL is the
514 -- maximum last bit value of any of these components.
516 -- We need to determine the corresponding machine scalar
517 -- size. This loop assumes that machine scalar sizes are
518 -- even, and that each possible machine scalar has twice
519 -- as many bits as the next smaller one.
521 MSS := Max_Machine_Scalar_Size;
523 and then (MSS / 2) >= SSU
524 and then (MSS / 2) > MaxL
529 -- Here is where we fix up the Component_Bit_Offset value
530 -- to account for the reverse bit order. Some examples of
531 -- what needs to be done for the case of a machine scalar
534 -- First_Bit .. Last_Bit Component_Bit_Offset
546 -- The rule is that the first bit is obtained by subtracting
547 -- the old ending bit from machine scalar size - 1.
549 for C in Start .. Stop loop
551 Comp : constant Entity_Id := Comps (C);
552 CC : constant Node_Id :=
553 Component_Clause (Comp);
554 LB : constant Uint :=
555 Static_Integer (Last_Bit (CC));
556 NFB : constant Uint := MSS - Uint_1 - LB;
557 NLB : constant Uint := NFB + Esize (Comp) - 1;
558 Pos : constant Uint :=
559 Static_Integer (Position (CC));
562 if Warn_On_Reverse_Bit_Order then
563 Error_Msg_Uint_1 := MSS;
565 ("info: reverse bit order in machine " &
566 "scalar of length^?", First_Bit (CC));
567 Error_Msg_Uint_1 := NFB;
568 Error_Msg_Uint_2 := NLB;
570 if Bytes_Big_Endian then
572 ("?\info: big-endian range for "
573 & "component & is ^ .. ^",
574 First_Bit (CC), Comp);
577 ("?\info: little-endian range "
578 & "for component & is ^ .. ^",
579 First_Bit (CC), Comp);
583 Set_Component_Bit_Offset (Comp, Pos * SSU + NFB);
584 Set_Normalized_First_Bit (Comp, NFB mod SSU);
591 end Adjust_Record_For_Reverse_Bit_Order;
593 --------------------------------------
594 -- Alignment_Check_For_Esize_Change --
595 --------------------------------------
597 procedure Alignment_Check_For_Esize_Change (Typ : Entity_Id) is
599 -- If the alignment is known, and not set by a rep clause, and is
600 -- inconsistent with the size being set, then reset it to unknown,
601 -- we assume in this case that the size overrides the inherited
602 -- alignment, and that the alignment must be recomputed.
604 if Known_Alignment (Typ)
605 and then not Has_Alignment_Clause (Typ)
606 and then Esize (Typ) mod (Alignment (Typ) * SSU) /= 0
608 Init_Alignment (Typ);
610 end Alignment_Check_For_Esize_Change;
612 -----------------------
613 -- Analyze_At_Clause --
614 -----------------------
616 -- An at clause is replaced by the corresponding Address attribute
617 -- definition clause that is the preferred approach in Ada 95.
619 procedure Analyze_At_Clause (N : Node_Id) is
620 CS : constant Boolean := Comes_From_Source (N);
623 -- This is an obsolescent feature
625 Check_Restriction (No_Obsolescent_Features, N);
627 if Warn_On_Obsolescent_Feature then
629 ("at clause is an obsolescent feature (RM J.7(2))?", N);
631 ("\use address attribute definition clause instead?", N);
634 -- Rewrite as address clause
637 Make_Attribute_Definition_Clause (Sloc (N),
638 Name => Identifier (N),
639 Chars => Name_Address,
640 Expression => Expression (N)));
642 -- We preserve Comes_From_Source, since logically the clause still
643 -- comes from the source program even though it is changed in form.
645 Set_Comes_From_Source (N, CS);
647 -- Analyze rewritten clause
649 Analyze_Attribute_Definition_Clause (N);
650 end Analyze_At_Clause;
652 -----------------------------------------
653 -- Analyze_Attribute_Definition_Clause --
654 -----------------------------------------
656 procedure Analyze_Attribute_Definition_Clause (N : Node_Id) is
657 Loc : constant Source_Ptr := Sloc (N);
658 Nam : constant Node_Id := Name (N);
659 Attr : constant Name_Id := Chars (N);
660 Expr : constant Node_Id := Expression (N);
661 Id : constant Attribute_Id := Get_Attribute_Id (Attr);
665 FOnly : Boolean := False;
666 -- Reset to True for subtype specific attribute (Alignment, Size)
667 -- and for stream attributes, i.e. those cases where in the call
668 -- to Rep_Item_Too_Late, FOnly is set True so that only the freezing
669 -- rules are checked. Note that the case of stream attributes is not
670 -- clear from the RM, but see AI95-00137. Also, the RM seems to
671 -- disallow Storage_Size for derived task types, but that is also
672 -- clearly unintentional.
674 procedure Analyze_Stream_TSS_Definition (TSS_Nam : TSS_Name_Type);
675 -- Common processing for 'Read, 'Write, 'Input and 'Output attribute
676 -- definition clauses.
678 -----------------------------------
679 -- Analyze_Stream_TSS_Definition --
680 -----------------------------------
682 procedure Analyze_Stream_TSS_Definition (TSS_Nam : TSS_Name_Type) is
683 Subp : Entity_Id := Empty;
688 Is_Read : constant Boolean := (TSS_Nam = TSS_Stream_Read);
690 function Has_Good_Profile (Subp : Entity_Id) return Boolean;
691 -- Return true if the entity is a subprogram with an appropriate
692 -- profile for the attribute being defined.
694 ----------------------
695 -- Has_Good_Profile --
696 ----------------------
698 function Has_Good_Profile (Subp : Entity_Id) return Boolean is
700 Is_Function : constant Boolean := (TSS_Nam = TSS_Stream_Input);
701 Expected_Ekind : constant array (Boolean) of Entity_Kind :=
702 (False => E_Procedure, True => E_Function);
706 if Ekind (Subp) /= Expected_Ekind (Is_Function) then
710 F := First_Formal (Subp);
713 or else Ekind (Etype (F)) /= E_Anonymous_Access_Type
714 or else Designated_Type (Etype (F)) /=
715 Class_Wide_Type (RTE (RE_Root_Stream_Type))
720 if not Is_Function then
724 Expected_Mode : constant array (Boolean) of Entity_Kind :=
725 (False => E_In_Parameter,
726 True => E_Out_Parameter);
728 if Parameter_Mode (F) /= Expected_Mode (Is_Read) then
739 return Base_Type (Typ) = Base_Type (Ent)
740 and then No (Next_Formal (F));
741 end Has_Good_Profile;
743 -- Start of processing for Analyze_Stream_TSS_Definition
748 if not Is_Type (U_Ent) then
749 Error_Msg_N ("local name must be a subtype", Nam);
753 Pnam := TSS (Base_Type (U_Ent), TSS_Nam);
755 -- If Pnam is present, it can be either inherited from an ancestor
756 -- type (in which case it is legal to redefine it for this type), or
757 -- be a previous definition of the attribute for the same type (in
758 -- which case it is illegal).
760 -- In the first case, it will have been analyzed already, and we
761 -- can check that its profile does not match the expected profile
762 -- for a stream attribute of U_Ent. In the second case, either Pnam
763 -- has been analyzed (and has the expected profile), or it has not
764 -- been analyzed yet (case of a type that has not been frozen yet
765 -- and for which the stream attribute has been set using Set_TSS).
768 and then (No (First_Entity (Pnam)) or else Has_Good_Profile (Pnam))
770 Error_Msg_Sloc := Sloc (Pnam);
771 Error_Msg_Name_1 := Attr;
772 Error_Msg_N ("% attribute already defined #", Nam);
778 if Is_Entity_Name (Expr) then
779 if not Is_Overloaded (Expr) then
780 if Has_Good_Profile (Entity (Expr)) then
781 Subp := Entity (Expr);
785 Get_First_Interp (Expr, I, It);
786 while Present (It.Nam) loop
787 if Has_Good_Profile (It.Nam) then
792 Get_Next_Interp (I, It);
797 if Present (Subp) then
798 if Is_Abstract_Subprogram (Subp) then
799 Error_Msg_N ("stream subprogram must not be abstract", Expr);
803 Set_Entity (Expr, Subp);
804 Set_Etype (Expr, Etype (Subp));
806 New_Stream_Subprogram (N, U_Ent, Subp, TSS_Nam);
809 Error_Msg_Name_1 := Attr;
810 Error_Msg_N ("incorrect expression for% attribute", Expr);
812 end Analyze_Stream_TSS_Definition;
814 -- Start of processing for Analyze_Attribute_Definition_Clause
817 -- Process Ignore_Rep_Clauses option
819 if Ignore_Rep_Clauses then
822 -- The following should be ignored. They do not affect legality
823 -- and may be target dependent. The basic idea of -gnatI is to
824 -- ignore any rep clauses that may be target dependent but do not
825 -- affect legality (except possibly to be rejected because they
826 -- are incompatible with the compilation target).
828 when Attribute_Alignment |
829 Attribute_Bit_Order |
830 Attribute_Component_Size |
831 Attribute_Machine_Radix |
832 Attribute_Object_Size |
835 Attribute_Stream_Size |
836 Attribute_Value_Size =>
838 Rewrite (N, Make_Null_Statement (Sloc (N)));
841 -- The following should not be ignored, because in the first place
842 -- they are reasonably portable, and should not cause problems in
843 -- compiling code from another target, and also they do affect
844 -- legality, e.g. failing to provide a stream attribute for a
845 -- type may make a program illegal.
847 when Attribute_External_Tag |
851 Attribute_Storage_Pool |
852 Attribute_Storage_Size |
856 -- Other cases are errors ("attribute& cannot be set with
857 -- definition clause"), which will be caught below.
867 if Rep_Item_Too_Early (Ent, N) then
871 -- Rep clause applies to full view of incomplete type or private type if
872 -- we have one (if not, this is a premature use of the type). However,
873 -- certain semantic checks need to be done on the specified entity (i.e.
874 -- the private view), so we save it in Ent.
876 if Is_Private_Type (Ent)
877 and then Is_Derived_Type (Ent)
878 and then not Is_Tagged_Type (Ent)
879 and then No (Full_View (Ent))
881 -- If this is a private type whose completion is a derivation from
882 -- another private type, there is no full view, and the attribute
883 -- belongs to the type itself, not its underlying parent.
887 elsif Ekind (Ent) = E_Incomplete_Type then
889 -- The attribute applies to the full view, set the entity of the
890 -- attribute definition accordingly.
892 Ent := Underlying_Type (Ent);
894 Set_Entity (Nam, Ent);
897 U_Ent := Underlying_Type (Ent);
900 -- Complete other routine error checks
902 if Etype (Nam) = Any_Type then
905 elsif Scope (Ent) /= Current_Scope then
906 Error_Msg_N ("entity must be declared in this scope", Nam);
909 elsif No (U_Ent) then
912 elsif Is_Type (U_Ent)
913 and then not Is_First_Subtype (U_Ent)
914 and then Id /= Attribute_Object_Size
915 and then Id /= Attribute_Value_Size
916 and then not From_At_Mod (N)
918 Error_Msg_N ("cannot specify attribute for subtype", Nam);
922 -- Switch on particular attribute
930 -- Address attribute definition clause
932 when Attribute_Address => Address : begin
934 -- A little error check, catch for X'Address use X'Address;
936 if Nkind (Nam) = N_Identifier
937 and then Nkind (Expr) = N_Attribute_Reference
938 and then Attribute_Name (Expr) = Name_Address
939 and then Nkind (Prefix (Expr)) = N_Identifier
940 and then Chars (Nam) = Chars (Prefix (Expr))
943 ("address for & is self-referencing", Prefix (Expr), Ent);
947 -- Not that special case, carry on with analysis of expression
949 Analyze_And_Resolve (Expr, RTE (RE_Address));
951 -- Even when ignoring rep clauses we need to indicate that the
952 -- entity has an address clause and thus it is legal to declare
955 if Ignore_Rep_Clauses then
956 if Ekind_In (U_Ent, E_Variable, E_Constant) then
957 Record_Rep_Item (U_Ent, N);
963 if Present (Address_Clause (U_Ent)) then
964 Error_Msg_N ("address already given for &", Nam);
966 -- Case of address clause for subprogram
968 elsif Is_Subprogram (U_Ent) then
969 if Has_Homonym (U_Ent) then
971 ("address clause cannot be given " &
972 "for overloaded subprogram",
977 -- For subprograms, all address clauses are permitted, and we
978 -- mark the subprogram as having a deferred freeze so that Gigi
979 -- will not elaborate it too soon.
981 -- Above needs more comments, what is too soon about???
983 Set_Has_Delayed_Freeze (U_Ent);
985 -- Case of address clause for entry
987 elsif Ekind (U_Ent) = E_Entry then
988 if Nkind (Parent (N)) = N_Task_Body then
990 ("entry address must be specified in task spec", Nam);
994 -- For entries, we require a constant address
996 Check_Constant_Address_Clause (Expr, U_Ent);
998 -- Special checks for task types
1000 if Is_Task_Type (Scope (U_Ent))
1001 and then Comes_From_Source (Scope (U_Ent))
1004 ("?entry address declared for entry in task type", N);
1006 ("\?only one task can be declared of this type", N);
1009 -- Entry address clauses are obsolescent
1011 Check_Restriction (No_Obsolescent_Features, N);
1013 if Warn_On_Obsolescent_Feature then
1015 ("attaching interrupt to task entry is an " &
1016 "obsolescent feature (RM J.7.1)?", N);
1018 ("\use interrupt procedure instead?", N);
1021 -- Case of an address clause for a controlled object which we
1022 -- consider to be erroneous.
1024 elsif Is_Controlled (Etype (U_Ent))
1025 or else Has_Controlled_Component (Etype (U_Ent))
1028 ("?controlled object& must not be overlaid", Nam, U_Ent);
1030 ("\?Program_Error will be raised at run time", Nam);
1031 Insert_Action (Declaration_Node (U_Ent),
1032 Make_Raise_Program_Error (Loc,
1033 Reason => PE_Overlaid_Controlled_Object));
1036 -- Case of address clause for a (non-controlled) object
1039 Ekind (U_Ent) = E_Variable
1041 Ekind (U_Ent) = E_Constant
1044 Expr : constant Node_Id := Expression (N);
1049 -- Exported variables cannot have an address clause, because
1050 -- this cancels the effect of the pragma Export.
1052 if Is_Exported (U_Ent) then
1054 ("cannot export object with address clause", Nam);
1058 Find_Overlaid_Entity (N, O_Ent, Off);
1060 -- Overlaying controlled objects is erroneous
1063 and then (Has_Controlled_Component (Etype (O_Ent))
1064 or else Is_Controlled (Etype (O_Ent)))
1067 ("?cannot overlay with controlled object", Expr);
1069 ("\?Program_Error will be raised at run time", Expr);
1070 Insert_Action (Declaration_Node (U_Ent),
1071 Make_Raise_Program_Error (Loc,
1072 Reason => PE_Overlaid_Controlled_Object));
1075 elsif Present (O_Ent)
1076 and then Ekind (U_Ent) = E_Constant
1077 and then not Is_Constant_Object (O_Ent)
1079 Error_Msg_N ("constant overlays a variable?", Expr);
1081 elsif Present (Renamed_Object (U_Ent)) then
1083 ("address clause not allowed"
1084 & " for a renaming declaration (RM 13.1(6))", Nam);
1087 -- Imported variables can have an address clause, but then
1088 -- the import is pretty meaningless except to suppress
1089 -- initializations, so we do not need such variables to
1090 -- be statically allocated (and in fact it causes trouble
1091 -- if the address clause is a local value).
1093 elsif Is_Imported (U_Ent) then
1094 Set_Is_Statically_Allocated (U_Ent, False);
1097 -- We mark a possible modification of a variable with an
1098 -- address clause, since it is likely aliasing is occurring.
1100 Note_Possible_Modification (Nam, Sure => False);
1102 -- Here we are checking for explicit overlap of one variable
1103 -- by another, and if we find this then mark the overlapped
1104 -- variable as also being volatile to prevent unwanted
1105 -- optimizations. This is a significant pessimization so
1106 -- avoid it when there is an offset, i.e. when the object
1107 -- is composite; they cannot be optimized easily anyway.
1110 and then Is_Object (O_Ent)
1113 Set_Treat_As_Volatile (O_Ent);
1116 -- Legality checks on the address clause for initialized
1117 -- objects is deferred until the freeze point, because
1118 -- a subsequent pragma might indicate that the object is
1119 -- imported and thus not initialized.
1121 Set_Has_Delayed_Freeze (U_Ent);
1123 -- If an initialization call has been generated for this
1124 -- object, it needs to be deferred to after the freeze node
1125 -- we have just now added, otherwise GIGI will see a
1126 -- reference to the variable (as actual to the IP call)
1127 -- before its definition.
1130 Init_Call : constant Node_Id := Find_Init_Call (U_Ent, N);
1132 if Present (Init_Call) then
1134 Append_Freeze_Action (U_Ent, Init_Call);
1138 if Is_Exported (U_Ent) then
1140 ("& cannot be exported if an address clause is given",
1143 ("\define and export a variable " &
1144 "that holds its address instead",
1148 -- Entity has delayed freeze, so we will generate an
1149 -- alignment check at the freeze point unless suppressed.
1151 if not Range_Checks_Suppressed (U_Ent)
1152 and then not Alignment_Checks_Suppressed (U_Ent)
1154 Set_Check_Address_Alignment (N);
1157 -- Kill the size check code, since we are not allocating
1158 -- the variable, it is somewhere else.
1160 Kill_Size_Check_Code (U_Ent);
1162 -- If the address clause is of the form:
1164 -- for Y'Address use X'Address
1168 -- Const : constant Address := X'Address;
1170 -- for Y'Address use Const;
1172 -- then we make an entry in the table for checking the size
1173 -- and alignment of the overlaying variable. We defer this
1174 -- check till after code generation to take full advantage
1175 -- of the annotation done by the back end. This entry is
1176 -- only made if the address clause comes from source.
1177 -- If the entity has a generic type, the check will be
1178 -- performed in the instance if the actual type justifies
1179 -- it, and we do not insert the clause in the table to
1180 -- prevent spurious warnings.
1182 if Address_Clause_Overlay_Warnings
1183 and then Comes_From_Source (N)
1184 and then Present (O_Ent)
1185 and then Is_Object (O_Ent)
1187 if not Is_Generic_Type (Etype (U_Ent)) then
1188 Address_Clause_Checks.Append ((N, U_Ent, O_Ent, Off));
1191 -- If variable overlays a constant view, and we are
1192 -- warning on overlays, then mark the variable as
1193 -- overlaying a constant (we will give warnings later
1194 -- if this variable is assigned).
1196 if Is_Constant_Object (O_Ent)
1197 and then Ekind (U_Ent) = E_Variable
1199 Set_Overlays_Constant (U_Ent);
1204 -- Not a valid entity for an address clause
1207 Error_Msg_N ("address cannot be given for &", Nam);
1215 -- Alignment attribute definition clause
1217 when Attribute_Alignment => Alignment : declare
1218 Align : constant Uint := Get_Alignment_Value (Expr);
1223 if not Is_Type (U_Ent)
1224 and then Ekind (U_Ent) /= E_Variable
1225 and then Ekind (U_Ent) /= E_Constant
1227 Error_Msg_N ("alignment cannot be given for &", Nam);
1229 elsif Has_Alignment_Clause (U_Ent) then
1230 Error_Msg_Sloc := Sloc (Alignment_Clause (U_Ent));
1231 Error_Msg_N ("alignment clause previously given#", N);
1233 elsif Align /= No_Uint then
1234 Set_Has_Alignment_Clause (U_Ent);
1235 Set_Alignment (U_Ent, Align);
1237 -- For an array type, U_Ent is the first subtype. In that case,
1238 -- also set the alignment of the anonymous base type so that
1239 -- other subtypes (such as the itypes for aggregates of the
1240 -- type) also receive the expected alignment.
1242 if Is_Array_Type (U_Ent) then
1243 Set_Alignment (Base_Type (U_Ent), Align);
1252 -- Bit_Order attribute definition clause
1254 when Attribute_Bit_Order => Bit_Order : declare
1256 if not Is_Record_Type (U_Ent) then
1258 ("Bit_Order can only be defined for record type", Nam);
1261 Analyze_And_Resolve (Expr, RTE (RE_Bit_Order));
1263 if Etype (Expr) = Any_Type then
1266 elsif not Is_Static_Expression (Expr) then
1267 Flag_Non_Static_Expr
1268 ("Bit_Order requires static expression!", Expr);
1271 if (Expr_Value (Expr) = 0) /= Bytes_Big_Endian then
1272 Set_Reverse_Bit_Order (U_Ent, True);
1278 --------------------
1279 -- Component_Size --
1280 --------------------
1282 -- Component_Size attribute definition clause
1284 when Attribute_Component_Size => Component_Size_Case : declare
1285 Csize : constant Uint := Static_Integer (Expr);
1288 New_Ctyp : Entity_Id;
1292 if not Is_Array_Type (U_Ent) then
1293 Error_Msg_N ("component size requires array type", Nam);
1297 Btype := Base_Type (U_Ent);
1299 if Has_Component_Size_Clause (Btype) then
1301 ("component size clause for& previously given", Nam);
1303 elsif Csize /= No_Uint then
1304 Check_Size (Expr, Component_Type (Btype), Csize, Biased);
1306 if Has_Aliased_Components (Btype)
1309 and then Csize /= 16
1312 ("component size incorrect for aliased components", N);
1316 -- For the biased case, build a declaration for a subtype
1317 -- that will be used to represent the biased subtype that
1318 -- reflects the biased representation of components. We need
1319 -- this subtype to get proper conversions on referencing
1320 -- elements of the array. Note that component size clauses
1321 -- are ignored in VM mode.
1323 if VM_Target = No_VM then
1326 Make_Defining_Identifier (Loc,
1328 New_External_Name (Chars (U_Ent), 'C', 0, 'T'));
1331 Make_Subtype_Declaration (Loc,
1332 Defining_Identifier => New_Ctyp,
1333 Subtype_Indication =>
1334 New_Occurrence_Of (Component_Type (Btype), Loc));
1336 Set_Parent (Decl, N);
1337 Analyze (Decl, Suppress => All_Checks);
1339 Set_Has_Delayed_Freeze (New_Ctyp, False);
1340 Set_Esize (New_Ctyp, Csize);
1341 Set_RM_Size (New_Ctyp, Csize);
1342 Init_Alignment (New_Ctyp);
1343 Set_Has_Biased_Representation (New_Ctyp, True);
1344 Set_Is_Itype (New_Ctyp, True);
1345 Set_Associated_Node_For_Itype (New_Ctyp, U_Ent);
1347 Set_Component_Type (Btype, New_Ctyp);
1349 if Warn_On_Biased_Representation then
1351 ("?component size clause forces biased "
1352 & "representation", N);
1356 Set_Component_Size (Btype, Csize);
1358 -- For VM case, we ignore component size clauses
1361 -- Give a warning unless we are in GNAT mode, in which case
1362 -- the warning is suppressed since it is not useful.
1364 if not GNAT_Mode then
1366 ("?component size ignored in this configuration", N);
1370 Set_Has_Component_Size_Clause (Btype, True);
1371 Set_Has_Non_Standard_Rep (Btype, True);
1373 end Component_Size_Case;
1379 when Attribute_External_Tag => External_Tag :
1381 if not Is_Tagged_Type (U_Ent) then
1382 Error_Msg_N ("should be a tagged type", Nam);
1385 Analyze_And_Resolve (Expr, Standard_String);
1387 if not Is_Static_Expression (Expr) then
1388 Flag_Non_Static_Expr
1389 ("static string required for tag name!", Nam);
1392 if VM_Target = No_VM then
1393 Set_Has_External_Tag_Rep_Clause (U_Ent);
1395 Error_Msg_Name_1 := Attr;
1397 ("% attribute unsupported in this configuration", Nam);
1400 if not Is_Library_Level_Entity (U_Ent) then
1402 ("?non-unique external tag supplied for &", N, U_Ent);
1404 ("?\same external tag applies to all subprogram calls", N);
1406 ("?\corresponding internal tag cannot be obtained", N);
1414 when Attribute_Input =>
1415 Analyze_Stream_TSS_Definition (TSS_Stream_Input);
1416 Set_Has_Specified_Stream_Input (Ent);
1422 -- Machine radix attribute definition clause
1424 when Attribute_Machine_Radix => Machine_Radix : declare
1425 Radix : constant Uint := Static_Integer (Expr);
1428 if not Is_Decimal_Fixed_Point_Type (U_Ent) then
1429 Error_Msg_N ("decimal fixed-point type expected for &", Nam);
1431 elsif Has_Machine_Radix_Clause (U_Ent) then
1432 Error_Msg_Sloc := Sloc (Alignment_Clause (U_Ent));
1433 Error_Msg_N ("machine radix clause previously given#", N);
1435 elsif Radix /= No_Uint then
1436 Set_Has_Machine_Radix_Clause (U_Ent);
1437 Set_Has_Non_Standard_Rep (Base_Type (U_Ent));
1441 elsif Radix = 10 then
1442 Set_Machine_Radix_10 (U_Ent);
1444 Error_Msg_N ("machine radix value must be 2 or 10", Expr);
1453 -- Object_Size attribute definition clause
1455 when Attribute_Object_Size => Object_Size : declare
1456 Size : constant Uint := Static_Integer (Expr);
1459 pragma Warnings (Off, Biased);
1462 if not Is_Type (U_Ent) then
1463 Error_Msg_N ("Object_Size cannot be given for &", Nam);
1465 elsif Has_Object_Size_Clause (U_Ent) then
1466 Error_Msg_N ("Object_Size already given for &", Nam);
1469 Check_Size (Expr, U_Ent, Size, Biased);
1477 UI_Mod (Size, 64) /= 0
1480 ("Object_Size must be 8, 16, 32, or multiple of 64",
1484 Set_Esize (U_Ent, Size);
1485 Set_Has_Object_Size_Clause (U_Ent);
1486 Alignment_Check_For_Esize_Change (U_Ent);
1494 when Attribute_Output =>
1495 Analyze_Stream_TSS_Definition (TSS_Stream_Output);
1496 Set_Has_Specified_Stream_Output (Ent);
1502 when Attribute_Read =>
1503 Analyze_Stream_TSS_Definition (TSS_Stream_Read);
1504 Set_Has_Specified_Stream_Read (Ent);
1510 -- Size attribute definition clause
1512 when Attribute_Size => Size : declare
1513 Size : constant Uint := Static_Integer (Expr);
1520 if Has_Size_Clause (U_Ent) then
1521 Error_Msg_N ("size already given for &", Nam);
1523 elsif not Is_Type (U_Ent)
1524 and then Ekind (U_Ent) /= E_Variable
1525 and then Ekind (U_Ent) /= E_Constant
1527 Error_Msg_N ("size cannot be given for &", Nam);
1529 elsif Is_Array_Type (U_Ent)
1530 and then not Is_Constrained (U_Ent)
1533 ("size cannot be given for unconstrained array", Nam);
1535 elsif Size /= No_Uint then
1536 if Is_Type (U_Ent) then
1539 Etyp := Etype (U_Ent);
1542 -- Check size, note that Gigi is in charge of checking that the
1543 -- size of an array or record type is OK. Also we do not check
1544 -- the size in the ordinary fixed-point case, since it is too
1545 -- early to do so (there may be subsequent small clause that
1546 -- affects the size). We can check the size if a small clause
1547 -- has already been given.
1549 if not Is_Ordinary_Fixed_Point_Type (U_Ent)
1550 or else Has_Small_Clause (U_Ent)
1552 Check_Size (Expr, Etyp, Size, Biased);
1553 Set_Has_Biased_Representation (U_Ent, Biased);
1555 if Biased and Warn_On_Biased_Representation then
1557 ("?size clause forces biased representation", N);
1561 -- For types set RM_Size and Esize if possible
1563 if Is_Type (U_Ent) then
1564 Set_RM_Size (U_Ent, Size);
1566 -- For scalar types, increase Object_Size to power of 2, but
1567 -- not less than a storage unit in any case (i.e., normally
1568 -- this means it will be byte addressable).
1570 if Is_Scalar_Type (U_Ent) then
1571 if Size <= System_Storage_Unit then
1572 Init_Esize (U_Ent, System_Storage_Unit);
1573 elsif Size <= 16 then
1574 Init_Esize (U_Ent, 16);
1575 elsif Size <= 32 then
1576 Init_Esize (U_Ent, 32);
1578 Set_Esize (U_Ent, (Size + 63) / 64 * 64);
1581 -- For all other types, object size = value size. The
1582 -- backend will adjust as needed.
1585 Set_Esize (U_Ent, Size);
1588 Alignment_Check_For_Esize_Change (U_Ent);
1590 -- For objects, set Esize only
1593 if Is_Elementary_Type (Etyp) then
1594 if Size /= System_Storage_Unit
1596 Size /= System_Storage_Unit * 2
1598 Size /= System_Storage_Unit * 4
1600 Size /= System_Storage_Unit * 8
1602 Error_Msg_Uint_1 := UI_From_Int (System_Storage_Unit);
1603 Error_Msg_Uint_2 := Error_Msg_Uint_1 * 8;
1605 ("size for primitive object must be a power of 2"
1606 & " in the range ^-^", N);
1610 Set_Esize (U_Ent, Size);
1613 Set_Has_Size_Clause (U_Ent);
1621 -- Small attribute definition clause
1623 when Attribute_Small => Small : declare
1624 Implicit_Base : constant Entity_Id := Base_Type (U_Ent);
1628 Analyze_And_Resolve (Expr, Any_Real);
1630 if Etype (Expr) = Any_Type then
1633 elsif not Is_Static_Expression (Expr) then
1634 Flag_Non_Static_Expr
1635 ("small requires static expression!", Expr);
1639 Small := Expr_Value_R (Expr);
1641 if Small <= Ureal_0 then
1642 Error_Msg_N ("small value must be greater than zero", Expr);
1648 if not Is_Ordinary_Fixed_Point_Type (U_Ent) then
1650 ("small requires an ordinary fixed point type", Nam);
1652 elsif Has_Small_Clause (U_Ent) then
1653 Error_Msg_N ("small already given for &", Nam);
1655 elsif Small > Delta_Value (U_Ent) then
1657 ("small value must not be greater then delta value", Nam);
1660 Set_Small_Value (U_Ent, Small);
1661 Set_Small_Value (Implicit_Base, Small);
1662 Set_Has_Small_Clause (U_Ent);
1663 Set_Has_Small_Clause (Implicit_Base);
1664 Set_Has_Non_Standard_Rep (Implicit_Base);
1672 -- Storage_Pool attribute definition clause
1674 when Attribute_Storage_Pool => Storage_Pool : declare
1679 if Ekind (U_Ent) = E_Access_Subprogram_Type then
1681 ("storage pool cannot be given for access-to-subprogram type",
1686 Ekind_In (U_Ent, E_Access_Type, E_General_Access_Type)
1689 ("storage pool can only be given for access types", Nam);
1692 elsif Is_Derived_Type (U_Ent) then
1694 ("storage pool cannot be given for a derived access type",
1697 elsif Has_Storage_Size_Clause (U_Ent) then
1698 Error_Msg_N ("storage size already given for &", Nam);
1701 elsif Present (Associated_Storage_Pool (U_Ent)) then
1702 Error_Msg_N ("storage pool already given for &", Nam);
1707 (Expr, Class_Wide_Type (RTE (RE_Root_Storage_Pool)));
1709 if not Denotes_Variable (Expr) then
1710 Error_Msg_N ("storage pool must be a variable", Expr);
1714 if Nkind (Expr) = N_Type_Conversion then
1715 T := Etype (Expression (Expr));
1720 -- The Stack_Bounded_Pool is used internally for implementing
1721 -- access types with a Storage_Size. Since it only work
1722 -- properly when used on one specific type, we need to check
1723 -- that it is not hijacked improperly:
1724 -- type T is access Integer;
1725 -- for T'Storage_Size use n;
1726 -- type Q is access Float;
1727 -- for Q'Storage_Size use T'Storage_Size; -- incorrect
1729 if RTE_Available (RE_Stack_Bounded_Pool)
1730 and then Base_Type (T) = RTE (RE_Stack_Bounded_Pool)
1732 Error_Msg_N ("non-shareable internal Pool", Expr);
1736 -- If the argument is a name that is not an entity name, then
1737 -- we construct a renaming operation to define an entity of
1738 -- type storage pool.
1740 if not Is_Entity_Name (Expr)
1741 and then Is_Object_Reference (Expr)
1743 Pool := Make_Temporary (Loc, 'P', Expr);
1746 Rnode : constant Node_Id :=
1747 Make_Object_Renaming_Declaration (Loc,
1748 Defining_Identifier => Pool,
1750 New_Occurrence_Of (Etype (Expr), Loc),
1754 Insert_Before (N, Rnode);
1756 Set_Associated_Storage_Pool (U_Ent, Pool);
1759 elsif Is_Entity_Name (Expr) then
1760 Pool := Entity (Expr);
1762 -- If pool is a renamed object, get original one. This can
1763 -- happen with an explicit renaming, and within instances.
1765 while Present (Renamed_Object (Pool))
1766 and then Is_Entity_Name (Renamed_Object (Pool))
1768 Pool := Entity (Renamed_Object (Pool));
1771 if Present (Renamed_Object (Pool))
1772 and then Nkind (Renamed_Object (Pool)) = N_Type_Conversion
1773 and then Is_Entity_Name (Expression (Renamed_Object (Pool)))
1775 Pool := Entity (Expression (Renamed_Object (Pool)));
1778 Set_Associated_Storage_Pool (U_Ent, Pool);
1780 elsif Nkind (Expr) = N_Type_Conversion
1781 and then Is_Entity_Name (Expression (Expr))
1782 and then Nkind (Original_Node (Expr)) = N_Attribute_Reference
1784 Pool := Entity (Expression (Expr));
1785 Set_Associated_Storage_Pool (U_Ent, Pool);
1788 Error_Msg_N ("incorrect reference to a Storage Pool", Expr);
1797 -- Storage_Size attribute definition clause
1799 when Attribute_Storage_Size => Storage_Size : declare
1800 Btype : constant Entity_Id := Base_Type (U_Ent);
1804 if Is_Task_Type (U_Ent) then
1805 Check_Restriction (No_Obsolescent_Features, N);
1807 if Warn_On_Obsolescent_Feature then
1809 ("storage size clause for task is an " &
1810 "obsolescent feature (RM J.9)?", N);
1811 Error_Msg_N ("\use Storage_Size pragma instead?", N);
1817 if not Is_Access_Type (U_Ent)
1818 and then Ekind (U_Ent) /= E_Task_Type
1820 Error_Msg_N ("storage size cannot be given for &", Nam);
1822 elsif Is_Access_Type (U_Ent) and Is_Derived_Type (U_Ent) then
1824 ("storage size cannot be given for a derived access type",
1827 elsif Has_Storage_Size_Clause (Btype) then
1828 Error_Msg_N ("storage size already given for &", Nam);
1831 Analyze_And_Resolve (Expr, Any_Integer);
1833 if Is_Access_Type (U_Ent) then
1834 if Present (Associated_Storage_Pool (U_Ent)) then
1835 Error_Msg_N ("storage pool already given for &", Nam);
1839 if Compile_Time_Known_Value (Expr)
1840 and then Expr_Value (Expr) = 0
1842 Set_No_Pool_Assigned (Btype);
1845 else -- Is_Task_Type (U_Ent)
1846 Sprag := Get_Rep_Pragma (Btype, Name_Storage_Size);
1848 if Present (Sprag) then
1849 Error_Msg_Sloc := Sloc (Sprag);
1851 ("Storage_Size already specified#", Nam);
1856 Set_Has_Storage_Size_Clause (Btype);
1864 when Attribute_Stream_Size => Stream_Size : declare
1865 Size : constant Uint := Static_Integer (Expr);
1868 if Ada_Version <= Ada_95 then
1869 Check_Restriction (No_Implementation_Attributes, N);
1872 if Has_Stream_Size_Clause (U_Ent) then
1873 Error_Msg_N ("Stream_Size already given for &", Nam);
1875 elsif Is_Elementary_Type (U_Ent) then
1876 if Size /= System_Storage_Unit
1878 Size /= System_Storage_Unit * 2
1880 Size /= System_Storage_Unit * 4
1882 Size /= System_Storage_Unit * 8
1884 Error_Msg_Uint_1 := UI_From_Int (System_Storage_Unit);
1886 ("stream size for elementary type must be a"
1887 & " power of 2 and at least ^", N);
1889 elsif RM_Size (U_Ent) > Size then
1890 Error_Msg_Uint_1 := RM_Size (U_Ent);
1892 ("stream size for elementary type must be a"
1893 & " power of 2 and at least ^", N);
1896 Set_Has_Stream_Size_Clause (U_Ent);
1899 Error_Msg_N ("Stream_Size cannot be given for &", Nam);
1907 -- Value_Size attribute definition clause
1909 when Attribute_Value_Size => Value_Size : declare
1910 Size : constant Uint := Static_Integer (Expr);
1914 if not Is_Type (U_Ent) then
1915 Error_Msg_N ("Value_Size cannot be given for &", Nam);
1918 (Get_Attribute_Definition_Clause
1919 (U_Ent, Attribute_Value_Size))
1921 Error_Msg_N ("Value_Size already given for &", Nam);
1923 elsif Is_Array_Type (U_Ent)
1924 and then not Is_Constrained (U_Ent)
1927 ("Value_Size cannot be given for unconstrained array", Nam);
1930 if Is_Elementary_Type (U_Ent) then
1931 Check_Size (Expr, U_Ent, Size, Biased);
1932 Set_Has_Biased_Representation (U_Ent, Biased);
1934 if Biased and Warn_On_Biased_Representation then
1936 ("?value size clause forces biased representation", N);
1940 Set_RM_Size (U_Ent, Size);
1948 when Attribute_Write =>
1949 Analyze_Stream_TSS_Definition (TSS_Stream_Write);
1950 Set_Has_Specified_Stream_Write (Ent);
1952 -- All other attributes cannot be set
1956 ("attribute& cannot be set with definition clause", N);
1959 -- The test for the type being frozen must be performed after
1960 -- any expression the clause has been analyzed since the expression
1961 -- itself might cause freezing that makes the clause illegal.
1963 if Rep_Item_Too_Late (U_Ent, N, FOnly) then
1966 end Analyze_Attribute_Definition_Clause;
1968 ----------------------------
1969 -- Analyze_Code_Statement --
1970 ----------------------------
1972 procedure Analyze_Code_Statement (N : Node_Id) is
1973 HSS : constant Node_Id := Parent (N);
1974 SBody : constant Node_Id := Parent (HSS);
1975 Subp : constant Entity_Id := Current_Scope;
1982 -- Analyze and check we get right type, note that this implements the
1983 -- requirement (RM 13.8(1)) that Machine_Code be with'ed, since that
1984 -- is the only way that Asm_Insn could possibly be visible.
1986 Analyze_And_Resolve (Expression (N));
1988 if Etype (Expression (N)) = Any_Type then
1990 elsif Etype (Expression (N)) /= RTE (RE_Asm_Insn) then
1991 Error_Msg_N ("incorrect type for code statement", N);
1995 Check_Code_Statement (N);
1997 -- Make sure we appear in the handled statement sequence of a
1998 -- subprogram (RM 13.8(3)).
2000 if Nkind (HSS) /= N_Handled_Sequence_Of_Statements
2001 or else Nkind (SBody) /= N_Subprogram_Body
2004 ("code statement can only appear in body of subprogram", N);
2008 -- Do remaining checks (RM 13.8(3)) if not already done
2010 if not Is_Machine_Code_Subprogram (Subp) then
2011 Set_Is_Machine_Code_Subprogram (Subp);
2013 -- No exception handlers allowed
2015 if Present (Exception_Handlers (HSS)) then
2017 ("exception handlers not permitted in machine code subprogram",
2018 First (Exception_Handlers (HSS)));
2021 -- No declarations other than use clauses and pragmas (we allow
2022 -- certain internally generated declarations as well).
2024 Decl := First (Declarations (SBody));
2025 while Present (Decl) loop
2026 DeclO := Original_Node (Decl);
2027 if Comes_From_Source (DeclO)
2028 and not Nkind_In (DeclO, N_Pragma,
2029 N_Use_Package_Clause,
2031 N_Implicit_Label_Declaration)
2034 ("this declaration not allowed in machine code subprogram",
2041 -- No statements other than code statements, pragmas, and labels.
2042 -- Again we allow certain internally generated statements.
2044 Stmt := First (Statements (HSS));
2045 while Present (Stmt) loop
2046 StmtO := Original_Node (Stmt);
2047 if Comes_From_Source (StmtO)
2048 and then not Nkind_In (StmtO, N_Pragma,
2053 ("this statement is not allowed in machine code subprogram",
2060 end Analyze_Code_Statement;
2062 -----------------------------------------------
2063 -- Analyze_Enumeration_Representation_Clause --
2064 -----------------------------------------------
2066 procedure Analyze_Enumeration_Representation_Clause (N : Node_Id) is
2067 Ident : constant Node_Id := Identifier (N);
2068 Aggr : constant Node_Id := Array_Aggregate (N);
2069 Enumtype : Entity_Id;
2075 Err : Boolean := False;
2077 Lo : constant Uint := Expr_Value (Type_Low_Bound (Universal_Integer));
2078 Hi : constant Uint := Expr_Value (Type_High_Bound (Universal_Integer));
2083 if Ignore_Rep_Clauses then
2087 -- First some basic error checks
2090 Enumtype := Entity (Ident);
2092 if Enumtype = Any_Type
2093 or else Rep_Item_Too_Early (Enumtype, N)
2097 Enumtype := Underlying_Type (Enumtype);
2100 if not Is_Enumeration_Type (Enumtype) then
2102 ("enumeration type required, found}",
2103 Ident, First_Subtype (Enumtype));
2107 -- Ignore rep clause on generic actual type. This will already have
2108 -- been flagged on the template as an error, and this is the safest
2109 -- way to ensure we don't get a junk cascaded message in the instance.
2111 if Is_Generic_Actual_Type (Enumtype) then
2114 -- Type must be in current scope
2116 elsif Scope (Enumtype) /= Current_Scope then
2117 Error_Msg_N ("type must be declared in this scope", Ident);
2120 -- Type must be a first subtype
2122 elsif not Is_First_Subtype (Enumtype) then
2123 Error_Msg_N ("cannot give enumeration rep clause for subtype", N);
2126 -- Ignore duplicate rep clause
2128 elsif Has_Enumeration_Rep_Clause (Enumtype) then
2129 Error_Msg_N ("duplicate enumeration rep clause ignored", N);
2132 -- Don't allow rep clause for standard [wide_[wide_]]character
2134 elsif Is_Standard_Character_Type (Enumtype) then
2135 Error_Msg_N ("enumeration rep clause not allowed for this type", N);
2138 -- Check that the expression is a proper aggregate (no parentheses)
2140 elsif Paren_Count (Aggr) /= 0 then
2142 ("extra parentheses surrounding aggregate not allowed",
2146 -- All tests passed, so set rep clause in place
2149 Set_Has_Enumeration_Rep_Clause (Enumtype);
2150 Set_Has_Enumeration_Rep_Clause (Base_Type (Enumtype));
2153 -- Now we process the aggregate. Note that we don't use the normal
2154 -- aggregate code for this purpose, because we don't want any of the
2155 -- normal expansion activities, and a number of special semantic
2156 -- rules apply (including the component type being any integer type)
2158 Elit := First_Literal (Enumtype);
2160 -- First the positional entries if any
2162 if Present (Expressions (Aggr)) then
2163 Expr := First (Expressions (Aggr));
2164 while Present (Expr) loop
2166 Error_Msg_N ("too many entries in aggregate", Expr);
2170 Val := Static_Integer (Expr);
2172 -- Err signals that we found some incorrect entries processing
2173 -- the list. The final checks for completeness and ordering are
2174 -- skipped in this case.
2176 if Val = No_Uint then
2178 elsif Val < Lo or else Hi < Val then
2179 Error_Msg_N ("value outside permitted range", Expr);
2183 Set_Enumeration_Rep (Elit, Val);
2184 Set_Enumeration_Rep_Expr (Elit, Expr);
2190 -- Now process the named entries if present
2192 if Present (Component_Associations (Aggr)) then
2193 Assoc := First (Component_Associations (Aggr));
2194 while Present (Assoc) loop
2195 Choice := First (Choices (Assoc));
2197 if Present (Next (Choice)) then
2199 ("multiple choice not allowed here", Next (Choice));
2203 if Nkind (Choice) = N_Others_Choice then
2204 Error_Msg_N ("others choice not allowed here", Choice);
2207 elsif Nkind (Choice) = N_Range then
2208 -- ??? should allow zero/one element range here
2209 Error_Msg_N ("range not allowed here", Choice);
2213 Analyze_And_Resolve (Choice, Enumtype);
2215 if Is_Entity_Name (Choice)
2216 and then Is_Type (Entity (Choice))
2218 Error_Msg_N ("subtype name not allowed here", Choice);
2220 -- ??? should allow static subtype with zero/one entry
2222 elsif Etype (Choice) = Base_Type (Enumtype) then
2223 if not Is_Static_Expression (Choice) then
2224 Flag_Non_Static_Expr
2225 ("non-static expression used for choice!", Choice);
2229 Elit := Expr_Value_E (Choice);
2231 if Present (Enumeration_Rep_Expr (Elit)) then
2232 Error_Msg_Sloc := Sloc (Enumeration_Rep_Expr (Elit));
2234 ("representation for& previously given#",
2239 Set_Enumeration_Rep_Expr (Elit, Choice);
2241 Expr := Expression (Assoc);
2242 Val := Static_Integer (Expr);
2244 if Val = No_Uint then
2247 elsif Val < Lo or else Hi < Val then
2248 Error_Msg_N ("value outside permitted range", Expr);
2252 Set_Enumeration_Rep (Elit, Val);
2261 -- Aggregate is fully processed. Now we check that a full set of
2262 -- representations was given, and that they are in range and in order.
2263 -- These checks are only done if no other errors occurred.
2269 Elit := First_Literal (Enumtype);
2270 while Present (Elit) loop
2271 if No (Enumeration_Rep_Expr (Elit)) then
2272 Error_Msg_NE ("missing representation for&!", N, Elit);
2275 Val := Enumeration_Rep (Elit);
2277 if Min = No_Uint then
2281 if Val /= No_Uint then
2282 if Max /= No_Uint and then Val <= Max then
2284 ("enumeration value for& not ordered!",
2285 Enumeration_Rep_Expr (Elit), Elit);
2291 -- If there is at least one literal whose representation
2292 -- is not equal to the Pos value, then note that this
2293 -- enumeration type has a non-standard representation.
2295 if Val /= Enumeration_Pos (Elit) then
2296 Set_Has_Non_Standard_Rep (Base_Type (Enumtype));
2303 -- Now set proper size information
2306 Minsize : Uint := UI_From_Int (Minimum_Size (Enumtype));
2309 if Has_Size_Clause (Enumtype) then
2310 if Esize (Enumtype) >= Minsize then
2315 UI_From_Int (Minimum_Size (Enumtype, Biased => True));
2317 if Esize (Enumtype) < Minsize then
2318 Error_Msg_N ("previously given size is too small", N);
2321 Set_Has_Biased_Representation (Enumtype);
2326 Set_RM_Size (Enumtype, Minsize);
2327 Set_Enum_Esize (Enumtype);
2330 Set_RM_Size (Base_Type (Enumtype), RM_Size (Enumtype));
2331 Set_Esize (Base_Type (Enumtype), Esize (Enumtype));
2332 Set_Alignment (Base_Type (Enumtype), Alignment (Enumtype));
2336 -- We repeat the too late test in case it froze itself!
2338 if Rep_Item_Too_Late (Enumtype, N) then
2341 end Analyze_Enumeration_Representation_Clause;
2343 ----------------------------
2344 -- Analyze_Free_Statement --
2345 ----------------------------
2347 procedure Analyze_Free_Statement (N : Node_Id) is
2349 Analyze (Expression (N));
2350 end Analyze_Free_Statement;
2352 ---------------------------
2353 -- Analyze_Freeze_Entity --
2354 ---------------------------
2356 procedure Analyze_Freeze_Entity (N : Node_Id) is
2357 E : constant Entity_Id := Entity (N);
2360 -- For tagged types covering interfaces add internal entities that link
2361 -- the primitives of the interfaces with the primitives that cover them.
2363 -- Note: These entities were originally generated only when generating
2364 -- code because their main purpose was to provide support to initialize
2365 -- the secondary dispatch tables. They are now generated also when
2366 -- compiling with no code generation to provide ASIS the relationship
2367 -- between interface primitives and tagged type primitives. They are
2368 -- also used to locate primitives covering interfaces when processing
2369 -- generics (see Derive_Subprograms).
2371 if Ada_Version >= Ada_05
2372 and then Ekind (E) = E_Record_Type
2373 and then Is_Tagged_Type (E)
2374 and then not Is_Interface (E)
2375 and then Has_Interfaces (E)
2377 -- This would be a good common place to call the routine that checks
2378 -- overriding of interface primitives (and thus factorize calls to
2379 -- Check_Abstract_Overriding located at different contexts in the
2380 -- compiler). However, this is not possible because it causes
2381 -- spurious errors in case of late overriding.
2383 Add_Internal_Interface_Entities (E);
2388 if Ekind (E) = E_Record_Type
2389 and then Is_CPP_Class (E)
2390 and then Is_Tagged_Type (E)
2391 and then Tagged_Type_Expansion
2392 and then Expander_Active
2394 if CPP_Num_Prims (E) = 0 then
2396 -- If the CPP type has user defined components then it must import
2397 -- primitives from C++. This is required because if the C++ class
2398 -- has no primitives then the C++ compiler does not added the _tag
2399 -- component to the type.
2401 pragma Assert (Chars (First_Entity (E)) = Name_uTag);
2403 if First_Entity (E) /= Last_Entity (E) then
2405 ("?'C'P'P type must import at least one primitive from C++",
2410 -- Check that all its primitives are abstract or imported from C++.
2411 -- Check also availability of the C++ constructor.
2414 Has_Constructors : constant Boolean := Has_CPP_Constructors (E);
2416 Error_Reported : Boolean := False;
2420 Elmt := First_Elmt (Primitive_Operations (E));
2421 while Present (Elmt) loop
2422 Prim := Node (Elmt);
2424 if Comes_From_Source (Prim) then
2425 if Is_Abstract_Subprogram (Prim) then
2428 elsif not Is_Imported (Prim)
2429 or else Convention (Prim) /= Convention_CPP
2432 ("?primitives of 'C'P'P types must be imported from C++"
2433 & " or abstract", Prim);
2435 elsif not Has_Constructors
2436 and then not Error_Reported
2438 Error_Msg_Name_1 := Chars (E);
2440 ("?'C'P'P constructor required for type %", Prim);
2441 Error_Reported := True;
2449 end Analyze_Freeze_Entity;
2451 ------------------------------------------
2452 -- Analyze_Record_Representation_Clause --
2453 ------------------------------------------
2455 -- Note: we check as much as we can here, but we can't do any checks
2456 -- based on the position values (e.g. overlap checks) until freeze time
2457 -- because especially in Ada 2005 (machine scalar mode), the processing
2458 -- for non-standard bit order can substantially change the positions.
2459 -- See procedure Check_Record_Representation_Clause (called from Freeze)
2460 -- for the remainder of this processing.
2462 procedure Analyze_Record_Representation_Clause (N : Node_Id) is
2463 Ident : constant Node_Id := Identifier (N);
2464 Rectype : Entity_Id;
2469 Hbit : Uint := Uint_0;
2474 CR_Pragma : Node_Id := Empty;
2475 -- Points to N_Pragma node if Complete_Representation pragma present
2478 if Ignore_Rep_Clauses then
2483 Rectype := Entity (Ident);
2485 if Rectype = Any_Type
2486 or else Rep_Item_Too_Early (Rectype, N)
2490 Rectype := Underlying_Type (Rectype);
2493 -- First some basic error checks
2495 if not Is_Record_Type (Rectype) then
2497 ("record type required, found}", Ident, First_Subtype (Rectype));
2500 elsif Is_Unchecked_Union (Rectype) then
2502 ("record rep clause not allowed for Unchecked_Union", N);
2504 elsif Scope (Rectype) /= Current_Scope then
2505 Error_Msg_N ("type must be declared in this scope", N);
2508 elsif not Is_First_Subtype (Rectype) then
2509 Error_Msg_N ("cannot give record rep clause for subtype", N);
2512 elsif Has_Record_Rep_Clause (Rectype) then
2513 Error_Msg_N ("duplicate record rep clause ignored", N);
2516 elsif Rep_Item_Too_Late (Rectype, N) then
2520 if Present (Mod_Clause (N)) then
2522 Loc : constant Source_Ptr := Sloc (N);
2523 M : constant Node_Id := Mod_Clause (N);
2524 P : constant List_Id := Pragmas_Before (M);
2528 pragma Warnings (Off, Mod_Val);
2531 Check_Restriction (No_Obsolescent_Features, Mod_Clause (N));
2533 if Warn_On_Obsolescent_Feature then
2535 ("mod clause is an obsolescent feature (RM J.8)?", N);
2537 ("\use alignment attribute definition clause instead?", N);
2544 -- In ASIS_Mode mode, expansion is disabled, but we must convert
2545 -- the Mod clause into an alignment clause anyway, so that the
2546 -- back-end can compute and back-annotate properly the size and
2547 -- alignment of types that may include this record.
2549 -- This seems dubious, this destroys the source tree in a manner
2550 -- not detectable by ASIS ???
2552 if Operating_Mode = Check_Semantics
2556 Make_Attribute_Definition_Clause (Loc,
2557 Name => New_Reference_To (Base_Type (Rectype), Loc),
2558 Chars => Name_Alignment,
2559 Expression => Relocate_Node (Expression (M)));
2561 Set_From_At_Mod (AtM_Nod);
2562 Insert_After (N, AtM_Nod);
2563 Mod_Val := Get_Alignment_Value (Expression (AtM_Nod));
2564 Set_Mod_Clause (N, Empty);
2567 -- Get the alignment value to perform error checking
2569 Mod_Val := Get_Alignment_Value (Expression (M));
2574 -- For untagged types, clear any existing component clauses for the
2575 -- type. If the type is derived, this is what allows us to override
2576 -- a rep clause for the parent. For type extensions, the representation
2577 -- of the inherited components is inherited, so we want to keep previous
2578 -- component clauses for completeness.
2580 if not Is_Tagged_Type (Rectype) then
2581 Comp := First_Component_Or_Discriminant (Rectype);
2582 while Present (Comp) loop
2583 Set_Component_Clause (Comp, Empty);
2584 Next_Component_Or_Discriminant (Comp);
2588 -- All done if no component clauses
2590 CC := First (Component_Clauses (N));
2596 -- A representation like this applies to the base type
2598 Set_Has_Record_Rep_Clause (Base_Type (Rectype));
2599 Set_Has_Non_Standard_Rep (Base_Type (Rectype));
2600 Set_Has_Specified_Layout (Base_Type (Rectype));
2602 -- Process the component clauses
2604 while Present (CC) loop
2608 if Nkind (CC) = N_Pragma then
2611 -- The only pragma of interest is Complete_Representation
2613 if Pragma_Name (CC) = Name_Complete_Representation then
2617 -- Processing for real component clause
2620 Posit := Static_Integer (Position (CC));
2621 Fbit := Static_Integer (First_Bit (CC));
2622 Lbit := Static_Integer (Last_Bit (CC));
2625 and then Fbit /= No_Uint
2626 and then Lbit /= No_Uint
2630 ("position cannot be negative", Position (CC));
2634 ("first bit cannot be negative", First_Bit (CC));
2636 -- The Last_Bit specified in a component clause must not be
2637 -- less than the First_Bit minus one (RM-13.5.1(10)).
2639 elsif Lbit < Fbit - 1 then
2641 ("last bit cannot be less than first bit minus one",
2644 -- Values look OK, so find the corresponding record component
2645 -- Even though the syntax allows an attribute reference for
2646 -- implementation-defined components, GNAT does not allow the
2647 -- tag to get an explicit position.
2649 elsif Nkind (Component_Name (CC)) = N_Attribute_Reference then
2650 if Attribute_Name (Component_Name (CC)) = Name_Tag then
2651 Error_Msg_N ("position of tag cannot be specified", CC);
2653 Error_Msg_N ("illegal component name", CC);
2657 Comp := First_Entity (Rectype);
2658 while Present (Comp) loop
2659 exit when Chars (Comp) = Chars (Component_Name (CC));
2665 -- Maybe component of base type that is absent from
2666 -- statically constrained first subtype.
2668 Comp := First_Entity (Base_Type (Rectype));
2669 while Present (Comp) loop
2670 exit when Chars (Comp) = Chars (Component_Name (CC));
2677 ("component clause is for non-existent field", CC);
2679 elsif Present (Component_Clause (Comp)) then
2681 -- Diagnose duplicate rep clause, or check consistency
2682 -- if this is an inherited component. In a double fault,
2683 -- there may be a duplicate inconsistent clause for an
2684 -- inherited component.
2686 if Scope (Original_Record_Component (Comp)) = Rectype
2687 or else Parent (Component_Clause (Comp)) = N
2689 Error_Msg_Sloc := Sloc (Component_Clause (Comp));
2690 Error_Msg_N ("component clause previously given#", CC);
2694 Rep1 : constant Node_Id := Component_Clause (Comp);
2696 if Intval (Position (Rep1)) /=
2697 Intval (Position (CC))
2698 or else Intval (First_Bit (Rep1)) /=
2699 Intval (First_Bit (CC))
2700 or else Intval (Last_Bit (Rep1)) /=
2701 Intval (Last_Bit (CC))
2703 Error_Msg_N ("component clause inconsistent "
2704 & "with representation of ancestor", CC);
2705 elsif Warn_On_Redundant_Constructs then
2706 Error_Msg_N ("?redundant component clause "
2707 & "for inherited component!", CC);
2712 -- Normal case where this is the first component clause we
2713 -- have seen for this entity, so set it up properly.
2716 -- Make reference for field in record rep clause and set
2717 -- appropriate entity field in the field identifier.
2720 (Comp, Component_Name (CC), Set_Ref => False);
2721 Set_Entity (Component_Name (CC), Comp);
2723 -- Update Fbit and Lbit to the actual bit number
2725 Fbit := Fbit + UI_From_Int (SSU) * Posit;
2726 Lbit := Lbit + UI_From_Int (SSU) * Posit;
2728 if Has_Size_Clause (Rectype)
2729 and then Esize (Rectype) <= Lbit
2732 ("bit number out of range of specified size",
2735 Set_Component_Clause (Comp, CC);
2736 Set_Component_Bit_Offset (Comp, Fbit);
2737 Set_Esize (Comp, 1 + (Lbit - Fbit));
2738 Set_Normalized_First_Bit (Comp, Fbit mod SSU);
2739 Set_Normalized_Position (Comp, Fbit / SSU);
2741 -- This information is also set in the corresponding
2742 -- component of the base type, found by accessing the
2743 -- Original_Record_Component link if it is present.
2745 Ocomp := Original_Record_Component (Comp);
2752 (Component_Name (CC),
2757 Set_Has_Biased_Representation (Comp, Biased);
2759 if Biased and Warn_On_Biased_Representation then
2761 ("?component clause forces biased "
2762 & "representation", CC);
2765 if Present (Ocomp) then
2766 Set_Component_Clause (Ocomp, CC);
2767 Set_Component_Bit_Offset (Ocomp, Fbit);
2768 Set_Normalized_First_Bit (Ocomp, Fbit mod SSU);
2769 Set_Normalized_Position (Ocomp, Fbit / SSU);
2770 Set_Esize (Ocomp, 1 + (Lbit - Fbit));
2772 Set_Normalized_Position_Max
2773 (Ocomp, Normalized_Position (Ocomp));
2775 Set_Has_Biased_Representation
2776 (Ocomp, Has_Biased_Representation (Comp));
2779 if Esize (Comp) < 0 then
2780 Error_Msg_N ("component size is negative", CC);
2791 -- Check missing components if Complete_Representation pragma appeared
2793 if Present (CR_Pragma) then
2794 Comp := First_Component_Or_Discriminant (Rectype);
2795 while Present (Comp) loop
2796 if No (Component_Clause (Comp)) then
2798 ("missing component clause for &", CR_Pragma, Comp);
2801 Next_Component_Or_Discriminant (Comp);
2804 -- If no Complete_Representation pragma, warn if missing components
2806 elsif Warn_On_Unrepped_Components then
2808 Num_Repped_Components : Nat := 0;
2809 Num_Unrepped_Components : Nat := 0;
2812 -- First count number of repped and unrepped components
2814 Comp := First_Component_Or_Discriminant (Rectype);
2815 while Present (Comp) loop
2816 if Present (Component_Clause (Comp)) then
2817 Num_Repped_Components := Num_Repped_Components + 1;
2819 Num_Unrepped_Components := Num_Unrepped_Components + 1;
2822 Next_Component_Or_Discriminant (Comp);
2825 -- We are only interested in the case where there is at least one
2826 -- unrepped component, and at least half the components have rep
2827 -- clauses. We figure that if less than half have them, then the
2828 -- partial rep clause is really intentional. If the component
2829 -- type has no underlying type set at this point (as for a generic
2830 -- formal type), we don't know enough to give a warning on the
2833 if Num_Unrepped_Components > 0
2834 and then Num_Unrepped_Components < Num_Repped_Components
2836 Comp := First_Component_Or_Discriminant (Rectype);
2837 while Present (Comp) loop
2838 if No (Component_Clause (Comp))
2839 and then Comes_From_Source (Comp)
2840 and then Present (Underlying_Type (Etype (Comp)))
2841 and then (Is_Scalar_Type (Underlying_Type (Etype (Comp)))
2842 or else Size_Known_At_Compile_Time
2843 (Underlying_Type (Etype (Comp))))
2844 and then not Has_Warnings_Off (Rectype)
2846 Error_Msg_Sloc := Sloc (Comp);
2848 ("?no component clause given for & declared #",
2852 Next_Component_Or_Discriminant (Comp);
2857 end Analyze_Record_Representation_Clause;
2859 -----------------------------------
2860 -- Check_Constant_Address_Clause --
2861 -----------------------------------
2863 procedure Check_Constant_Address_Clause
2867 procedure Check_At_Constant_Address (Nod : Node_Id);
2868 -- Checks that the given node N represents a name whose 'Address is
2869 -- constant (in the same sense as OK_Constant_Address_Clause, i.e. the
2870 -- address value is the same at the point of declaration of U_Ent and at
2871 -- the time of elaboration of the address clause.
2873 procedure Check_Expr_Constants (Nod : Node_Id);
2874 -- Checks that Nod meets the requirements for a constant address clause
2875 -- in the sense of the enclosing procedure.
2877 procedure Check_List_Constants (Lst : List_Id);
2878 -- Check that all elements of list Lst meet the requirements for a
2879 -- constant address clause in the sense of the enclosing procedure.
2881 -------------------------------
2882 -- Check_At_Constant_Address --
2883 -------------------------------
2885 procedure Check_At_Constant_Address (Nod : Node_Id) is
2887 if Is_Entity_Name (Nod) then
2888 if Present (Address_Clause (Entity ((Nod)))) then
2890 ("invalid address clause for initialized object &!",
2893 ("address for& cannot" &
2894 " depend on another address clause! (RM 13.1(22))!",
2897 elsif In_Same_Source_Unit (Entity (Nod), U_Ent)
2898 and then Sloc (U_Ent) < Sloc (Entity (Nod))
2901 ("invalid address clause for initialized object &!",
2903 Error_Msg_Node_2 := U_Ent;
2905 ("\& must be defined before & (RM 13.1(22))!",
2909 elsif Nkind (Nod) = N_Selected_Component then
2911 T : constant Entity_Id := Etype (Prefix (Nod));
2914 if (Is_Record_Type (T)
2915 and then Has_Discriminants (T))
2918 and then Is_Record_Type (Designated_Type (T))
2919 and then Has_Discriminants (Designated_Type (T)))
2922 ("invalid address clause for initialized object &!",
2925 ("\address cannot depend on component" &
2926 " of discriminated record (RM 13.1(22))!",
2929 Check_At_Constant_Address (Prefix (Nod));
2933 elsif Nkind (Nod) = N_Indexed_Component then
2934 Check_At_Constant_Address (Prefix (Nod));
2935 Check_List_Constants (Expressions (Nod));
2938 Check_Expr_Constants (Nod);
2940 end Check_At_Constant_Address;
2942 --------------------------
2943 -- Check_Expr_Constants --
2944 --------------------------
2946 procedure Check_Expr_Constants (Nod : Node_Id) is
2947 Loc_U_Ent : constant Source_Ptr := Sloc (U_Ent);
2948 Ent : Entity_Id := Empty;
2951 if Nkind (Nod) in N_Has_Etype
2952 and then Etype (Nod) = Any_Type
2958 when N_Empty | N_Error =>
2961 when N_Identifier | N_Expanded_Name =>
2962 Ent := Entity (Nod);
2964 -- We need to look at the original node if it is different
2965 -- from the node, since we may have rewritten things and
2966 -- substituted an identifier representing the rewrite.
2968 if Original_Node (Nod) /= Nod then
2969 Check_Expr_Constants (Original_Node (Nod));
2971 -- If the node is an object declaration without initial
2972 -- value, some code has been expanded, and the expression
2973 -- is not constant, even if the constituents might be
2974 -- acceptable, as in A'Address + offset.
2976 if Ekind (Ent) = E_Variable
2978 Nkind (Declaration_Node (Ent)) = N_Object_Declaration
2980 No (Expression (Declaration_Node (Ent)))
2983 ("invalid address clause for initialized object &!",
2986 -- If entity is constant, it may be the result of expanding
2987 -- a check. We must verify that its declaration appears
2988 -- before the object in question, else we also reject the
2991 elsif Ekind (Ent) = E_Constant
2992 and then In_Same_Source_Unit (Ent, U_Ent)
2993 and then Sloc (Ent) > Loc_U_Ent
2996 ("invalid address clause for initialized object &!",
3003 -- Otherwise look at the identifier and see if it is OK
3005 if Ekind_In (Ent, E_Named_Integer, E_Named_Real)
3006 or else Is_Type (Ent)
3011 Ekind (Ent) = E_Constant
3013 Ekind (Ent) = E_In_Parameter
3015 -- This is the case where we must have Ent defined before
3016 -- U_Ent. Clearly if they are in different units this
3017 -- requirement is met since the unit containing Ent is
3018 -- already processed.
3020 if not In_Same_Source_Unit (Ent, U_Ent) then
3023 -- Otherwise location of Ent must be before the location
3024 -- of U_Ent, that's what prior defined means.
3026 elsif Sloc (Ent) < Loc_U_Ent then
3031 ("invalid address clause for initialized object &!",
3033 Error_Msg_Node_2 := U_Ent;
3035 ("\& must be defined before & (RM 13.1(22))!",
3039 elsif Nkind (Original_Node (Nod)) = N_Function_Call then
3040 Check_Expr_Constants (Original_Node (Nod));
3044 ("invalid address clause for initialized object &!",
3047 if Comes_From_Source (Ent) then
3049 ("\reference to variable& not allowed"
3050 & " (RM 13.1(22))!", Nod, Ent);
3053 ("non-static expression not allowed"
3054 & " (RM 13.1(22))!", Nod);
3058 when N_Integer_Literal =>
3060 -- If this is a rewritten unchecked conversion, in a system
3061 -- where Address is an integer type, always use the base type
3062 -- for a literal value. This is user-friendly and prevents
3063 -- order-of-elaboration issues with instances of unchecked
3066 if Nkind (Original_Node (Nod)) = N_Function_Call then
3067 Set_Etype (Nod, Base_Type (Etype (Nod)));
3070 when N_Real_Literal |
3072 N_Character_Literal =>
3076 Check_Expr_Constants (Low_Bound (Nod));
3077 Check_Expr_Constants (High_Bound (Nod));
3079 when N_Explicit_Dereference =>
3080 Check_Expr_Constants (Prefix (Nod));
3082 when N_Indexed_Component =>
3083 Check_Expr_Constants (Prefix (Nod));
3084 Check_List_Constants (Expressions (Nod));
3087 Check_Expr_Constants (Prefix (Nod));
3088 Check_Expr_Constants (Discrete_Range (Nod));
3090 when N_Selected_Component =>
3091 Check_Expr_Constants (Prefix (Nod));
3093 when N_Attribute_Reference =>
3094 if Attribute_Name (Nod) = Name_Address
3096 Attribute_Name (Nod) = Name_Access
3098 Attribute_Name (Nod) = Name_Unchecked_Access
3100 Attribute_Name (Nod) = Name_Unrestricted_Access
3102 Check_At_Constant_Address (Prefix (Nod));
3105 Check_Expr_Constants (Prefix (Nod));
3106 Check_List_Constants (Expressions (Nod));
3110 Check_List_Constants (Component_Associations (Nod));
3111 Check_List_Constants (Expressions (Nod));
3113 when N_Component_Association =>
3114 Check_Expr_Constants (Expression (Nod));
3116 when N_Extension_Aggregate =>
3117 Check_Expr_Constants (Ancestor_Part (Nod));
3118 Check_List_Constants (Component_Associations (Nod));
3119 Check_List_Constants (Expressions (Nod));
3124 when N_Binary_Op | N_Short_Circuit | N_Membership_Test =>
3125 Check_Expr_Constants (Left_Opnd (Nod));
3126 Check_Expr_Constants (Right_Opnd (Nod));
3129 Check_Expr_Constants (Right_Opnd (Nod));
3131 when N_Type_Conversion |
3132 N_Qualified_Expression |
3134 Check_Expr_Constants (Expression (Nod));
3136 when N_Unchecked_Type_Conversion =>
3137 Check_Expr_Constants (Expression (Nod));
3139 -- If this is a rewritten unchecked conversion, subtypes in
3140 -- this node are those created within the instance. To avoid
3141 -- order of elaboration issues, replace them with their base
3142 -- types. Note that address clauses can cause order of
3143 -- elaboration problems because they are elaborated by the
3144 -- back-end at the point of definition, and may mention
3145 -- entities declared in between (as long as everything is
3146 -- static). It is user-friendly to allow unchecked conversions
3149 if Nkind (Original_Node (Nod)) = N_Function_Call then
3150 Set_Etype (Expression (Nod),
3151 Base_Type (Etype (Expression (Nod))));
3152 Set_Etype (Nod, Base_Type (Etype (Nod)));
3155 when N_Function_Call =>
3156 if not Is_Pure (Entity (Name (Nod))) then
3158 ("invalid address clause for initialized object &!",
3162 ("\function & is not pure (RM 13.1(22))!",
3163 Nod, Entity (Name (Nod)));
3166 Check_List_Constants (Parameter_Associations (Nod));
3169 when N_Parameter_Association =>
3170 Check_Expr_Constants (Explicit_Actual_Parameter (Nod));
3174 ("invalid address clause for initialized object &!",
3177 ("\must be constant defined before& (RM 13.1(22))!",
3180 end Check_Expr_Constants;
3182 --------------------------
3183 -- Check_List_Constants --
3184 --------------------------
3186 procedure Check_List_Constants (Lst : List_Id) is
3190 if Present (Lst) then
3191 Nod1 := First (Lst);
3192 while Present (Nod1) loop
3193 Check_Expr_Constants (Nod1);
3197 end Check_List_Constants;
3199 -- Start of processing for Check_Constant_Address_Clause
3202 -- If rep_clauses are to be ignored, no need for legality checks. In
3203 -- particular, no need to pester user about rep clauses that violate
3204 -- the rule on constant addresses, given that these clauses will be
3205 -- removed by Freeze before they reach the back end.
3207 if not Ignore_Rep_Clauses then
3208 Check_Expr_Constants (Expr);
3210 end Check_Constant_Address_Clause;
3212 ----------------------------------------
3213 -- Check_Record_Representation_Clause --
3214 ----------------------------------------
3216 procedure Check_Record_Representation_Clause (N : Node_Id) is
3217 Loc : constant Source_Ptr := Sloc (N);
3218 Ident : constant Node_Id := Identifier (N);
3219 Rectype : Entity_Id;
3224 Hbit : Uint := Uint_0;
3228 Max_Bit_So_Far : Uint;
3229 -- Records the maximum bit position so far. If all field positions
3230 -- are monotonically increasing, then we can skip the circuit for
3231 -- checking for overlap, since no overlap is possible.
3233 Tagged_Parent : Entity_Id := Empty;
3234 -- This is set in the case of a derived tagged type for which we have
3235 -- Is_Fully_Repped_Tagged_Type True (indicating that all components are
3236 -- positioned by record representation clauses). In this case we must
3237 -- check for overlap between components of this tagged type, and the
3238 -- components of its parent. Tagged_Parent will point to this parent
3239 -- type. For all other cases Tagged_Parent is left set to Empty.
3241 Parent_Last_Bit : Uint;
3242 -- Relevant only if Tagged_Parent is set, Parent_Last_Bit indicates the
3243 -- last bit position for any field in the parent type. We only need to
3244 -- check overlap for fields starting below this point.
3246 Overlap_Check_Required : Boolean;
3247 -- Used to keep track of whether or not an overlap check is required
3249 Ccount : Natural := 0;
3250 -- Number of component clauses in record rep clause
3252 procedure Check_Component_Overlap (C1_Ent, C2_Ent : Entity_Id);
3253 -- Given two entities for record components or discriminants, checks
3254 -- if they have overlapping component clauses and issues errors if so.
3256 procedure Find_Component;
3257 -- Finds component entity corresponding to current component clause (in
3258 -- CC), and sets Comp to the entity, and Fbit/Lbit to the zero origin
3259 -- start/stop bits for the field. If there is no matching component or
3260 -- if the matching component does not have a component clause, then
3261 -- that's an error and Comp is set to Empty, but no error message is
3262 -- issued, since the message was already given. Comp is also set to
3263 -- Empty if the current "component clause" is in fact a pragma.
3265 -----------------------------
3266 -- Check_Component_Overlap --
3267 -----------------------------
3269 procedure Check_Component_Overlap (C1_Ent, C2_Ent : Entity_Id) is
3270 CC1 : constant Node_Id := Component_Clause (C1_Ent);
3271 CC2 : constant Node_Id := Component_Clause (C2_Ent);
3273 if Present (CC1) and then Present (CC2) then
3275 -- Exclude odd case where we have two tag fields in the same
3276 -- record, both at location zero. This seems a bit strange, but
3277 -- it seems to happen in some circumstances, perhaps on an error.
3279 if Chars (C1_Ent) = Name_uTag
3281 Chars (C2_Ent) = Name_uTag
3286 -- Here we check if the two fields overlap
3289 S1 : constant Uint := Component_Bit_Offset (C1_Ent);
3290 S2 : constant Uint := Component_Bit_Offset (C2_Ent);
3291 E1 : constant Uint := S1 + Esize (C1_Ent);
3292 E2 : constant Uint := S2 + Esize (C2_Ent);
3295 if E2 <= S1 or else E1 <= S2 then
3298 Error_Msg_Node_2 := Component_Name (CC2);
3299 Error_Msg_Sloc := Sloc (Error_Msg_Node_2);
3300 Error_Msg_Node_1 := Component_Name (CC1);
3302 ("component& overlaps & #", Component_Name (CC1));
3306 end Check_Component_Overlap;
3308 --------------------
3309 -- Find_Component --
3310 --------------------
3312 procedure Find_Component is
3314 procedure Search_Component (R : Entity_Id);
3315 -- Search components of R for a match. If found, Comp is set.
3317 ----------------------
3318 -- Search_Component --
3319 ----------------------
3321 procedure Search_Component (R : Entity_Id) is
3323 Comp := First_Component_Or_Discriminant (R);
3324 while Present (Comp) loop
3326 -- Ignore error of attribute name for component name (we
3327 -- already gave an error message for this, so no need to
3330 if Nkind (Component_Name (CC)) = N_Attribute_Reference then
3333 exit when Chars (Comp) = Chars (Component_Name (CC));
3336 Next_Component_Or_Discriminant (Comp);
3338 end Search_Component;
3340 -- Start of processing for Find_Component
3343 -- Return with Comp set to Empty if we have a pragma
3345 if Nkind (CC) = N_Pragma then
3350 -- Search current record for matching component
3352 Search_Component (Rectype);
3354 -- If not found, maybe component of base type that is absent from
3355 -- statically constrained first subtype.
3358 Search_Component (Base_Type (Rectype));
3361 -- If no component, or the component does not reference the component
3362 -- clause in question, then there was some previous error for which
3363 -- we already gave a message, so just return with Comp Empty.
3366 or else Component_Clause (Comp) /= CC
3370 -- Normal case where we have a component clause
3373 Fbit := Component_Bit_Offset (Comp);
3374 Lbit := Fbit + Esize (Comp) - 1;
3378 -- Start of processing for Check_Record_Representation_Clause
3382 Rectype := Entity (Ident);
3384 if Rectype = Any_Type then
3387 Rectype := Underlying_Type (Rectype);
3390 -- See if we have a fully repped derived tagged type
3393 PS : constant Entity_Id := Parent_Subtype (Rectype);
3396 if Present (PS) and then Is_Fully_Repped_Tagged_Type (PS) then
3397 Tagged_Parent := PS;
3399 -- Find maximum bit of any component of the parent type
3401 Parent_Last_Bit := UI_From_Int (System_Address_Size - 1);
3402 Pcomp := First_Entity (Tagged_Parent);
3403 while Present (Pcomp) loop
3404 if Ekind_In (Pcomp, E_Discriminant, E_Component) then
3405 if Component_Bit_Offset (Pcomp) /= No_Uint
3406 and then Known_Static_Esize (Pcomp)
3411 Component_Bit_Offset (Pcomp) + Esize (Pcomp) - 1);
3414 Next_Entity (Pcomp);
3420 -- All done if no component clauses
3422 CC := First (Component_Clauses (N));
3428 -- If a tag is present, then create a component clause that places it
3429 -- at the start of the record (otherwise gigi may place it after other
3430 -- fields that have rep clauses).
3432 Fent := First_Entity (Rectype);
3434 if Nkind (Fent) = N_Defining_Identifier
3435 and then Chars (Fent) = Name_uTag
3437 Set_Component_Bit_Offset (Fent, Uint_0);
3438 Set_Normalized_Position (Fent, Uint_0);
3439 Set_Normalized_First_Bit (Fent, Uint_0);
3440 Set_Normalized_Position_Max (Fent, Uint_0);
3441 Init_Esize (Fent, System_Address_Size);
3443 Set_Component_Clause (Fent,
3444 Make_Component_Clause (Loc,
3446 Make_Identifier (Loc,
3447 Chars => Name_uTag),
3450 Make_Integer_Literal (Loc,
3454 Make_Integer_Literal (Loc,
3458 Make_Integer_Literal (Loc,
3459 UI_From_Int (System_Address_Size))));
3461 Ccount := Ccount + 1;
3464 Max_Bit_So_Far := Uint_Minus_1;
3465 Overlap_Check_Required := False;
3467 -- Process the component clauses
3469 while Present (CC) loop
3472 if Present (Comp) then
3473 Ccount := Ccount + 1;
3475 if Fbit <= Max_Bit_So_Far then
3476 Overlap_Check_Required := True;
3478 Max_Bit_So_Far := Lbit;
3481 -- Check bit position out of range of specified size
3483 if Has_Size_Clause (Rectype)
3484 and then Esize (Rectype) <= Lbit
3487 ("bit number out of range of specified size",
3490 -- Check for overlap with tag field
3493 if Is_Tagged_Type (Rectype)
3494 and then Fbit < System_Address_Size
3497 ("component overlaps tag field of&",
3498 Component_Name (CC), Rectype);
3506 -- Check parent overlap if component might overlap parent field
3508 if Present (Tagged_Parent)
3509 and then Fbit <= Parent_Last_Bit
3511 Pcomp := First_Component_Or_Discriminant (Tagged_Parent);
3512 while Present (Pcomp) loop
3513 if not Is_Tag (Pcomp)
3514 and then Chars (Pcomp) /= Name_uParent
3516 Check_Component_Overlap (Comp, Pcomp);
3519 Next_Component_Or_Discriminant (Pcomp);
3527 -- Now that we have processed all the component clauses, check for
3528 -- overlap. We have to leave this till last, since the components can
3529 -- appear in any arbitrary order in the representation clause.
3531 -- We do not need this check if all specified ranges were monotonic,
3532 -- as recorded by Overlap_Check_Required being False at this stage.
3534 -- This first section checks if there are any overlapping entries at
3535 -- all. It does this by sorting all entries and then seeing if there are
3536 -- any overlaps. If there are none, then that is decisive, but if there
3537 -- are overlaps, they may still be OK (they may result from fields in
3538 -- different variants).
3540 if Overlap_Check_Required then
3541 Overlap_Check1 : declare
3543 OC_Fbit : array (0 .. Ccount) of Uint;
3544 -- First-bit values for component clauses, the value is the offset
3545 -- of the first bit of the field from start of record. The zero
3546 -- entry is for use in sorting.
3548 OC_Lbit : array (0 .. Ccount) of Uint;
3549 -- Last-bit values for component clauses, the value is the offset
3550 -- of the last bit of the field from start of record. The zero
3551 -- entry is for use in sorting.
3553 OC_Count : Natural := 0;
3554 -- Count of entries in OC_Fbit and OC_Lbit
3556 function OC_Lt (Op1, Op2 : Natural) return Boolean;
3557 -- Compare routine for Sort
3559 procedure OC_Move (From : Natural; To : Natural);
3560 -- Move routine for Sort
3562 package Sorting is new GNAT.Heap_Sort_G (OC_Move, OC_Lt);
3568 function OC_Lt (Op1, Op2 : Natural) return Boolean is
3570 return OC_Fbit (Op1) < OC_Fbit (Op2);
3577 procedure OC_Move (From : Natural; To : Natural) is
3579 OC_Fbit (To) := OC_Fbit (From);
3580 OC_Lbit (To) := OC_Lbit (From);
3583 -- Start of processing for Overlap_Check
3586 CC := First (Component_Clauses (N));
3587 while Present (CC) loop
3589 -- Exclude component clause already marked in error
3591 if not Error_Posted (CC) then
3594 if Present (Comp) then
3595 OC_Count := OC_Count + 1;
3596 OC_Fbit (OC_Count) := Fbit;
3597 OC_Lbit (OC_Count) := Lbit;
3604 Sorting.Sort (OC_Count);
3606 Overlap_Check_Required := False;
3607 for J in 1 .. OC_Count - 1 loop
3608 if OC_Lbit (J) >= OC_Fbit (J + 1) then
3609 Overlap_Check_Required := True;
3616 -- If Overlap_Check_Required is still True, then we have to do the full
3617 -- scale overlap check, since we have at least two fields that do
3618 -- overlap, and we need to know if that is OK since they are in
3619 -- different variant, or whether we have a definite problem.
3621 if Overlap_Check_Required then
3622 Overlap_Check2 : declare
3623 C1_Ent, C2_Ent : Entity_Id;
3624 -- Entities of components being checked for overlap
3627 -- Component_List node whose Component_Items are being checked
3630 -- Component declaration for component being checked
3633 C1_Ent := First_Entity (Base_Type (Rectype));
3635 -- Loop through all components in record. For each component check
3636 -- for overlap with any of the preceding elements on the component
3637 -- list containing the component and also, if the component is in
3638 -- a variant, check against components outside the case structure.
3639 -- This latter test is repeated recursively up the variant tree.
3641 Main_Component_Loop : while Present (C1_Ent) loop
3642 if not Ekind_In (C1_Ent, E_Component, E_Discriminant) then
3643 goto Continue_Main_Component_Loop;
3646 -- Skip overlap check if entity has no declaration node. This
3647 -- happens with discriminants in constrained derived types.
3648 -- Probably we are missing some checks as a result, but that
3649 -- does not seem terribly serious ???
3651 if No (Declaration_Node (C1_Ent)) then
3652 goto Continue_Main_Component_Loop;
3655 Clist := Parent (List_Containing (Declaration_Node (C1_Ent)));
3657 -- Loop through component lists that need checking. Check the
3658 -- current component list and all lists in variants above us.
3660 Component_List_Loop : loop
3662 -- If derived type definition, go to full declaration
3663 -- If at outer level, check discriminants if there are any.
3665 if Nkind (Clist) = N_Derived_Type_Definition then
3666 Clist := Parent (Clist);
3669 -- Outer level of record definition, check discriminants
3671 if Nkind_In (Clist, N_Full_Type_Declaration,
3672 N_Private_Type_Declaration)
3674 if Has_Discriminants (Defining_Identifier (Clist)) then
3676 First_Discriminant (Defining_Identifier (Clist));
3677 while Present (C2_Ent) loop
3678 exit when C1_Ent = C2_Ent;
3679 Check_Component_Overlap (C1_Ent, C2_Ent);
3680 Next_Discriminant (C2_Ent);
3684 -- Record extension case
3686 elsif Nkind (Clist) = N_Derived_Type_Definition then
3689 -- Otherwise check one component list
3692 Citem := First (Component_Items (Clist));
3694 while Present (Citem) loop
3695 if Nkind (Citem) = N_Component_Declaration then
3696 C2_Ent := Defining_Identifier (Citem);
3697 exit when C1_Ent = C2_Ent;
3698 Check_Component_Overlap (C1_Ent, C2_Ent);
3705 -- Check for variants above us (the parent of the Clist can
3706 -- be a variant, in which case its parent is a variant part,
3707 -- and the parent of the variant part is a component list
3708 -- whose components must all be checked against the current
3709 -- component for overlap).
3711 if Nkind (Parent (Clist)) = N_Variant then
3712 Clist := Parent (Parent (Parent (Clist)));
3714 -- Check for possible discriminant part in record, this
3715 -- is treated essentially as another level in the
3716 -- recursion. For this case the parent of the component
3717 -- list is the record definition, and its parent is the
3718 -- full type declaration containing the discriminant
3721 elsif Nkind (Parent (Clist)) = N_Record_Definition then
3722 Clist := Parent (Parent ((Clist)));
3724 -- If neither of these two cases, we are at the top of
3728 exit Component_List_Loop;
3730 end loop Component_List_Loop;
3732 <<Continue_Main_Component_Loop>>
3733 Next_Entity (C1_Ent);
3735 end loop Main_Component_Loop;
3739 -- For records that have component clauses for all components, and whose
3740 -- size is less than or equal to 32, we need to know the size in the
3741 -- front end to activate possible packed array processing where the
3742 -- component type is a record.
3744 -- At this stage Hbit + 1 represents the first unused bit from all the
3745 -- component clauses processed, so if the component clauses are
3746 -- complete, then this is the length of the record.
3748 -- For records longer than System.Storage_Unit, and for those where not
3749 -- all components have component clauses, the back end determines the
3750 -- length (it may for example be appropriate to round up the size
3751 -- to some convenient boundary, based on alignment considerations, etc).
3753 if Unknown_RM_Size (Rectype) and then Hbit + 1 <= 32 then
3755 -- Nothing to do if at least one component has no component clause
3757 Comp := First_Component_Or_Discriminant (Rectype);
3758 while Present (Comp) loop
3759 exit when No (Component_Clause (Comp));
3760 Next_Component_Or_Discriminant (Comp);
3763 -- If we fall out of loop, all components have component clauses
3764 -- and so we can set the size to the maximum value.
3767 Set_RM_Size (Rectype, Hbit + 1);
3770 end Check_Record_Representation_Clause;
3776 procedure Check_Size
3780 Biased : out Boolean)
3782 UT : constant Entity_Id := Underlying_Type (T);
3788 -- Dismiss cases for generic types or types with previous errors
3791 or else UT = Any_Type
3792 or else Is_Generic_Type (UT)
3793 or else Is_Generic_Type (Root_Type (UT))
3797 -- Check case of bit packed array
3799 elsif Is_Array_Type (UT)
3800 and then Known_Static_Component_Size (UT)
3801 and then Is_Bit_Packed_Array (UT)
3809 Asiz := Component_Size (UT);
3810 Indx := First_Index (UT);
3812 Ityp := Etype (Indx);
3814 -- If non-static bound, then we are not in the business of
3815 -- trying to check the length, and indeed an error will be
3816 -- issued elsewhere, since sizes of non-static array types
3817 -- cannot be set implicitly or explicitly.
3819 if not Is_Static_Subtype (Ityp) then
3823 -- Otherwise accumulate next dimension
3825 Asiz := Asiz * (Expr_Value (Type_High_Bound (Ityp)) -
3826 Expr_Value (Type_Low_Bound (Ityp)) +
3830 exit when No (Indx);
3836 Error_Msg_Uint_1 := Asiz;
3838 ("size for& too small, minimum allowed is ^", N, T);
3839 Set_Esize (T, Asiz);
3840 Set_RM_Size (T, Asiz);
3844 -- All other composite types are ignored
3846 elsif Is_Composite_Type (UT) then
3849 -- For fixed-point types, don't check minimum if type is not frozen,
3850 -- since we don't know all the characteristics of the type that can
3851 -- affect the size (e.g. a specified small) till freeze time.
3853 elsif Is_Fixed_Point_Type (UT)
3854 and then not Is_Frozen (UT)
3858 -- Cases for which a minimum check is required
3861 -- Ignore if specified size is correct for the type
3863 if Known_Esize (UT) and then Siz = Esize (UT) then
3867 -- Otherwise get minimum size
3869 M := UI_From_Int (Minimum_Size (UT));
3873 -- Size is less than minimum size, but one possibility remains
3874 -- that we can manage with the new size if we bias the type.
3876 M := UI_From_Int (Minimum_Size (UT, Biased => True));
3879 Error_Msg_Uint_1 := M;
3881 ("size for& too small, minimum allowed is ^", N, T);
3891 -------------------------
3892 -- Get_Alignment_Value --
3893 -------------------------
3895 function Get_Alignment_Value (Expr : Node_Id) return Uint is
3896 Align : constant Uint := Static_Integer (Expr);
3899 if Align = No_Uint then
3902 elsif Align <= 0 then
3903 Error_Msg_N ("alignment value must be positive", Expr);
3907 for J in Int range 0 .. 64 loop
3909 M : constant Uint := Uint_2 ** J;
3912 exit when M = Align;
3916 ("alignment value must be power of 2", Expr);
3924 end Get_Alignment_Value;
3930 procedure Initialize is
3932 Unchecked_Conversions.Init;
3935 -------------------------
3936 -- Is_Operational_Item --
3937 -------------------------
3939 function Is_Operational_Item (N : Node_Id) return Boolean is
3941 if Nkind (N) /= N_Attribute_Definition_Clause then
3945 Id : constant Attribute_Id := Get_Attribute_Id (Chars (N));
3947 return Id = Attribute_Input
3948 or else Id = Attribute_Output
3949 or else Id = Attribute_Read
3950 or else Id = Attribute_Write
3951 or else Id = Attribute_External_Tag;
3954 end Is_Operational_Item;
3960 function Minimum_Size
3962 Biased : Boolean := False) return Nat
3964 Lo : Uint := No_Uint;
3965 Hi : Uint := No_Uint;
3966 LoR : Ureal := No_Ureal;
3967 HiR : Ureal := No_Ureal;
3968 LoSet : Boolean := False;
3969 HiSet : Boolean := False;
3973 R_Typ : constant Entity_Id := Root_Type (T);
3976 -- If bad type, return 0
3978 if T = Any_Type then
3981 -- For generic types, just return zero. There cannot be any legitimate
3982 -- need to know such a size, but this routine may be called with a
3983 -- generic type as part of normal processing.
3985 elsif Is_Generic_Type (R_Typ)
3986 or else R_Typ = Any_Type
3990 -- Access types. Normally an access type cannot have a size smaller
3991 -- than the size of System.Address. The exception is on VMS, where
3992 -- we have short and long addresses, and it is possible for an access
3993 -- type to have a short address size (and thus be less than the size
3994 -- of System.Address itself). We simply skip the check for VMS, and
3995 -- leave it to the back end to do the check.
3997 elsif Is_Access_Type (T) then
3998 if OpenVMS_On_Target then
4001 return System_Address_Size;
4004 -- Floating-point types
4006 elsif Is_Floating_Point_Type (T) then
4007 return UI_To_Int (Esize (R_Typ));
4011 elsif Is_Discrete_Type (T) then
4013 -- The following loop is looking for the nearest compile time known
4014 -- bounds following the ancestor subtype chain. The idea is to find
4015 -- the most restrictive known bounds information.
4019 if Ancest = Any_Type or else Etype (Ancest) = Any_Type then
4024 if Compile_Time_Known_Value (Type_Low_Bound (Ancest)) then
4025 Lo := Expr_Rep_Value (Type_Low_Bound (Ancest));
4032 if Compile_Time_Known_Value (Type_High_Bound (Ancest)) then
4033 Hi := Expr_Rep_Value (Type_High_Bound (Ancest));
4039 Ancest := Ancestor_Subtype (Ancest);
4042 Ancest := Base_Type (T);
4044 if Is_Generic_Type (Ancest) then
4050 -- Fixed-point types. We can't simply use Expr_Value to get the
4051 -- Corresponding_Integer_Value values of the bounds, since these do not
4052 -- get set till the type is frozen, and this routine can be called
4053 -- before the type is frozen. Similarly the test for bounds being static
4054 -- needs to include the case where we have unanalyzed real literals for
4057 elsif Is_Fixed_Point_Type (T) then
4059 -- The following loop is looking for the nearest compile time known
4060 -- bounds following the ancestor subtype chain. The idea is to find
4061 -- the most restrictive known bounds information.
4065 if Ancest = Any_Type or else Etype (Ancest) = Any_Type then
4069 -- Note: In the following two tests for LoSet and HiSet, it may
4070 -- seem redundant to test for N_Real_Literal here since normally
4071 -- one would assume that the test for the value being known at
4072 -- compile time includes this case. However, there is a glitch.
4073 -- If the real literal comes from folding a non-static expression,
4074 -- then we don't consider any non- static expression to be known
4075 -- at compile time if we are in configurable run time mode (needed
4076 -- in some cases to give a clearer definition of what is and what
4077 -- is not accepted). So the test is indeed needed. Without it, we
4078 -- would set neither Lo_Set nor Hi_Set and get an infinite loop.
4081 if Nkind (Type_Low_Bound (Ancest)) = N_Real_Literal
4082 or else Compile_Time_Known_Value (Type_Low_Bound (Ancest))
4084 LoR := Expr_Value_R (Type_Low_Bound (Ancest));
4091 if Nkind (Type_High_Bound (Ancest)) = N_Real_Literal
4092 or else Compile_Time_Known_Value (Type_High_Bound (Ancest))
4094 HiR := Expr_Value_R (Type_High_Bound (Ancest));
4100 Ancest := Ancestor_Subtype (Ancest);
4103 Ancest := Base_Type (T);
4105 if Is_Generic_Type (Ancest) then
4111 Lo := UR_To_Uint (LoR / Small_Value (T));
4112 Hi := UR_To_Uint (HiR / Small_Value (T));
4114 -- No other types allowed
4117 raise Program_Error;
4120 -- Fall through with Hi and Lo set. Deal with biased case
4123 and then not Is_Fixed_Point_Type (T)
4124 and then not (Is_Enumeration_Type (T)
4125 and then Has_Non_Standard_Rep (T)))
4126 or else Has_Biased_Representation (T)
4132 -- Signed case. Note that we consider types like range 1 .. -1 to be
4133 -- signed for the purpose of computing the size, since the bounds have
4134 -- to be accommodated in the base type.
4136 if Lo < 0 or else Hi < 0 then
4140 -- S = size, B = 2 ** (size - 1) (can accommodate -B .. +(B - 1))
4141 -- Note that we accommodate the case where the bounds cross. This
4142 -- can happen either because of the way the bounds are declared
4143 -- or because of the algorithm in Freeze_Fixed_Point_Type.
4157 -- If both bounds are positive, make sure that both are represen-
4158 -- table in the case where the bounds are crossed. This can happen
4159 -- either because of the way the bounds are declared, or because of
4160 -- the algorithm in Freeze_Fixed_Point_Type.
4166 -- S = size, (can accommodate 0 .. (2**size - 1))
4169 while Hi >= Uint_2 ** S loop
4177 ---------------------------
4178 -- New_Stream_Subprogram --
4179 ---------------------------
4181 procedure New_Stream_Subprogram
4185 Nam : TSS_Name_Type)
4187 Loc : constant Source_Ptr := Sloc (N);
4188 Sname : constant Name_Id := Make_TSS_Name (Base_Type (Ent), Nam);
4189 Subp_Id : Entity_Id;
4190 Subp_Decl : Node_Id;
4194 Defer_Declaration : constant Boolean :=
4195 Is_Tagged_Type (Ent) or else Is_Private_Type (Ent);
4196 -- For a tagged type, there is a declaration for each stream attribute
4197 -- at the freeze point, and we must generate only a completion of this
4198 -- declaration. We do the same for private types, because the full view
4199 -- might be tagged. Otherwise we generate a declaration at the point of
4200 -- the attribute definition clause.
4202 function Build_Spec return Node_Id;
4203 -- Used for declaration and renaming declaration, so that this is
4204 -- treated as a renaming_as_body.
4210 function Build_Spec return Node_Id is
4211 Out_P : constant Boolean := (Nam = TSS_Stream_Read);
4214 T_Ref : constant Node_Id := New_Reference_To (Etyp, Loc);
4217 Subp_Id := Make_Defining_Identifier (Loc, Sname);
4219 -- S : access Root_Stream_Type'Class
4221 Formals := New_List (
4222 Make_Parameter_Specification (Loc,
4223 Defining_Identifier =>
4224 Make_Defining_Identifier (Loc, Name_S),
4226 Make_Access_Definition (Loc,
4229 Designated_Type (Etype (F)), Loc))));
4231 if Nam = TSS_Stream_Input then
4232 Spec := Make_Function_Specification (Loc,
4233 Defining_Unit_Name => Subp_Id,
4234 Parameter_Specifications => Formals,
4235 Result_Definition => T_Ref);
4240 Make_Parameter_Specification (Loc,
4241 Defining_Identifier => Make_Defining_Identifier (Loc, Name_V),
4242 Out_Present => Out_P,
4243 Parameter_Type => T_Ref));
4246 Make_Procedure_Specification (Loc,
4247 Defining_Unit_Name => Subp_Id,
4248 Parameter_Specifications => Formals);
4254 -- Start of processing for New_Stream_Subprogram
4257 F := First_Formal (Subp);
4259 if Ekind (Subp) = E_Procedure then
4260 Etyp := Etype (Next_Formal (F));
4262 Etyp := Etype (Subp);
4265 -- Prepare subprogram declaration and insert it as an action on the
4266 -- clause node. The visibility for this entity is used to test for
4267 -- visibility of the attribute definition clause (in the sense of
4268 -- 8.3(23) as amended by AI-195).
4270 if not Defer_Declaration then
4272 Make_Subprogram_Declaration (Loc,
4273 Specification => Build_Spec);
4275 -- For a tagged type, there is always a visible declaration for each
4276 -- stream TSS (it is a predefined primitive operation), and the
4277 -- completion of this declaration occurs at the freeze point, which is
4278 -- not always visible at places where the attribute definition clause is
4279 -- visible. So, we create a dummy entity here for the purpose of
4280 -- tracking the visibility of the attribute definition clause itself.
4284 Make_Defining_Identifier (Loc,
4285 Chars => New_External_Name (Sname, 'V'));
4287 Make_Object_Declaration (Loc,
4288 Defining_Identifier => Subp_Id,
4289 Object_Definition => New_Occurrence_Of (Standard_Boolean, Loc));
4292 Insert_Action (N, Subp_Decl);
4293 Set_Entity (N, Subp_Id);
4296 Make_Subprogram_Renaming_Declaration (Loc,
4297 Specification => Build_Spec,
4298 Name => New_Reference_To (Subp, Loc));
4300 if Defer_Declaration then
4301 Set_TSS (Base_Type (Ent), Subp_Id);
4303 Insert_Action (N, Subp_Decl);
4304 Copy_TSS (Subp_Id, Base_Type (Ent));
4306 end New_Stream_Subprogram;
4308 ------------------------
4309 -- Rep_Item_Too_Early --
4310 ------------------------
4312 function Rep_Item_Too_Early (T : Entity_Id; N : Node_Id) return Boolean is
4314 -- Cannot apply non-operational rep items to generic types
4316 if Is_Operational_Item (N) then
4320 and then Is_Generic_Type (Root_Type (T))
4322 Error_Msg_N ("representation item not allowed for generic type", N);
4326 -- Otherwise check for incomplete type
4328 if Is_Incomplete_Or_Private_Type (T)
4329 and then No (Underlying_Type (T))
4332 ("representation item must be after full type declaration", N);
4335 -- If the type has incomplete components, a representation clause is
4336 -- illegal but stream attributes and Convention pragmas are correct.
4338 elsif Has_Private_Component (T) then
4339 if Nkind (N) = N_Pragma then
4343 ("representation item must appear after type is fully defined",
4350 end Rep_Item_Too_Early;
4352 -----------------------
4353 -- Rep_Item_Too_Late --
4354 -----------------------
4356 function Rep_Item_Too_Late
4359 FOnly : Boolean := False) return Boolean
4362 Parent_Type : Entity_Id;
4365 -- Output the too late message. Note that this is not considered a
4366 -- serious error, since the effect is simply that we ignore the
4367 -- representation clause in this case.
4373 procedure Too_Late is
4375 Error_Msg_N ("|representation item appears too late!", N);
4378 -- Start of processing for Rep_Item_Too_Late
4381 -- First make sure entity is not frozen (RM 13.1(9)). Exclude imported
4382 -- types, which may be frozen if they appear in a representation clause
4383 -- for a local type.
4386 and then not From_With_Type (T)
4389 S := First_Subtype (T);
4391 if Present (Freeze_Node (S)) then
4393 ("?no more representation items for }", Freeze_Node (S), S);
4398 -- Check for case of non-tagged derived type whose parent either has
4399 -- primitive operations, or is a by reference type (RM 13.1(10)).
4403 and then Is_Derived_Type (T)
4404 and then not Is_Tagged_Type (T)
4406 Parent_Type := Etype (Base_Type (T));
4408 if Has_Primitive_Operations (Parent_Type) then
4411 ("primitive operations already defined for&!", N, Parent_Type);
4414 elsif Is_By_Reference_Type (Parent_Type) then
4417 ("parent type & is a by reference type!", N, Parent_Type);
4422 -- No error, link item into head of chain of rep items for the entity,
4423 -- but avoid chaining if we have an overloadable entity, and the pragma
4424 -- is one that can apply to multiple overloaded entities.
4426 if Is_Overloadable (T)
4427 and then Nkind (N) = N_Pragma
4430 Pname : constant Name_Id := Pragma_Name (N);
4432 if Pname = Name_Convention or else
4433 Pname = Name_Import or else
4434 Pname = Name_Export or else
4435 Pname = Name_External or else
4436 Pname = Name_Interface
4443 Record_Rep_Item (T, N);
4445 end Rep_Item_Too_Late;
4447 -------------------------
4448 -- Same_Representation --
4449 -------------------------
4451 function Same_Representation (Typ1, Typ2 : Entity_Id) return Boolean is
4452 T1 : constant Entity_Id := Underlying_Type (Typ1);
4453 T2 : constant Entity_Id := Underlying_Type (Typ2);
4456 -- A quick check, if base types are the same, then we definitely have
4457 -- the same representation, because the subtype specific representation
4458 -- attributes (Size and Alignment) do not affect representation from
4459 -- the point of view of this test.
4461 if Base_Type (T1) = Base_Type (T2) then
4464 elsif Is_Private_Type (Base_Type (T2))
4465 and then Base_Type (T1) = Full_View (Base_Type (T2))
4470 -- Tagged types never have differing representations
4472 if Is_Tagged_Type (T1) then
4476 -- Representations are definitely different if conventions differ
4478 if Convention (T1) /= Convention (T2) then
4482 -- Representations are different if component alignments differ
4484 if (Is_Record_Type (T1) or else Is_Array_Type (T1))
4486 (Is_Record_Type (T2) or else Is_Array_Type (T2))
4487 and then Component_Alignment (T1) /= Component_Alignment (T2)
4492 -- For arrays, the only real issue is component size. If we know the
4493 -- component size for both arrays, and it is the same, then that's
4494 -- good enough to know we don't have a change of representation.
4496 if Is_Array_Type (T1) then
4497 if Known_Component_Size (T1)
4498 and then Known_Component_Size (T2)
4499 and then Component_Size (T1) = Component_Size (T2)
4505 -- Types definitely have same representation if neither has non-standard
4506 -- representation since default representations are always consistent.
4507 -- If only one has non-standard representation, and the other does not,
4508 -- then we consider that they do not have the same representation. They
4509 -- might, but there is no way of telling early enough.
4511 if Has_Non_Standard_Rep (T1) then
4512 if not Has_Non_Standard_Rep (T2) then
4516 return not Has_Non_Standard_Rep (T2);
4519 -- Here the two types both have non-standard representation, and we need
4520 -- to determine if they have the same non-standard representation.
4522 -- For arrays, we simply need to test if the component sizes are the
4523 -- same. Pragma Pack is reflected in modified component sizes, so this
4524 -- check also deals with pragma Pack.
4526 if Is_Array_Type (T1) then
4527 return Component_Size (T1) = Component_Size (T2);
4529 -- Tagged types always have the same representation, because it is not
4530 -- possible to specify different representations for common fields.
4532 elsif Is_Tagged_Type (T1) then
4535 -- Case of record types
4537 elsif Is_Record_Type (T1) then
4539 -- Packed status must conform
4541 if Is_Packed (T1) /= Is_Packed (T2) then
4544 -- Otherwise we must check components. Typ2 maybe a constrained
4545 -- subtype with fewer components, so we compare the components
4546 -- of the base types.
4549 Record_Case : declare
4550 CD1, CD2 : Entity_Id;
4552 function Same_Rep return Boolean;
4553 -- CD1 and CD2 are either components or discriminants. This
4554 -- function tests whether the two have the same representation
4560 function Same_Rep return Boolean is
4562 if No (Component_Clause (CD1)) then
4563 return No (Component_Clause (CD2));
4567 Present (Component_Clause (CD2))
4569 Component_Bit_Offset (CD1) = Component_Bit_Offset (CD2)
4571 Esize (CD1) = Esize (CD2);
4575 -- Start of processing for Record_Case
4578 if Has_Discriminants (T1) then
4579 CD1 := First_Discriminant (T1);
4580 CD2 := First_Discriminant (T2);
4582 -- The number of discriminants may be different if the
4583 -- derived type has fewer (constrained by values). The
4584 -- invisible discriminants retain the representation of
4585 -- the original, so the discrepancy does not per se
4586 -- indicate a different representation.
4589 and then Present (CD2)
4591 if not Same_Rep then
4594 Next_Discriminant (CD1);
4595 Next_Discriminant (CD2);
4600 CD1 := First_Component (Underlying_Type (Base_Type (T1)));
4601 CD2 := First_Component (Underlying_Type (Base_Type (T2)));
4603 while Present (CD1) loop
4604 if not Same_Rep then
4607 Next_Component (CD1);
4608 Next_Component (CD2);
4616 -- For enumeration types, we must check each literal to see if the
4617 -- representation is the same. Note that we do not permit enumeration
4618 -- representation clauses for Character and Wide_Character, so these
4619 -- cases were already dealt with.
4621 elsif Is_Enumeration_Type (T1) then
4623 Enumeration_Case : declare
4627 L1 := First_Literal (T1);
4628 L2 := First_Literal (T2);
4630 while Present (L1) loop
4631 if Enumeration_Rep (L1) /= Enumeration_Rep (L2) then
4641 end Enumeration_Case;
4643 -- Any other types have the same representation for these purposes
4648 end Same_Representation;
4650 --------------------
4651 -- Set_Enum_Esize --
4652 --------------------
4654 procedure Set_Enum_Esize (T : Entity_Id) is
4662 -- Find the minimum standard size (8,16,32,64) that fits
4664 Lo := Enumeration_Rep (Entity (Type_Low_Bound (T)));
4665 Hi := Enumeration_Rep (Entity (Type_High_Bound (T)));
4668 if Lo >= -Uint_2**07 and then Hi < Uint_2**07 then
4669 Sz := Standard_Character_Size; -- May be > 8 on some targets
4671 elsif Lo >= -Uint_2**15 and then Hi < Uint_2**15 then
4674 elsif Lo >= -Uint_2**31 and then Hi < Uint_2**31 then
4677 else pragma Assert (Lo >= -Uint_2**63 and then Hi < Uint_2**63);
4682 if Hi < Uint_2**08 then
4683 Sz := Standard_Character_Size; -- May be > 8 on some targets
4685 elsif Hi < Uint_2**16 then
4688 elsif Hi < Uint_2**32 then
4691 else pragma Assert (Hi < Uint_2**63);
4696 -- That minimum is the proper size unless we have a foreign convention
4697 -- and the size required is 32 or less, in which case we bump the size
4698 -- up to 32. This is required for C and C++ and seems reasonable for
4699 -- all other foreign conventions.
4701 if Has_Foreign_Convention (T)
4702 and then Esize (T) < Standard_Integer_Size
4704 Init_Esize (T, Standard_Integer_Size);
4710 ------------------------------
4711 -- Validate_Address_Clauses --
4712 ------------------------------
4714 procedure Validate_Address_Clauses is
4716 for J in Address_Clause_Checks.First .. Address_Clause_Checks.Last loop
4718 ACCR : Address_Clause_Check_Record
4719 renames Address_Clause_Checks.Table (J);
4730 -- Skip processing of this entry if warning already posted
4732 if not Address_Warning_Posted (ACCR.N) then
4734 Expr := Original_Node (Expression (ACCR.N));
4738 X_Alignment := Alignment (ACCR.X);
4739 Y_Alignment := Alignment (ACCR.Y);
4741 -- Similarly obtain sizes
4743 X_Size := Esize (ACCR.X);
4744 Y_Size := Esize (ACCR.Y);
4746 -- Check for large object overlaying smaller one
4749 and then X_Size > Uint_0
4750 and then X_Size > Y_Size
4753 ("?& overlays smaller object", ACCR.N, ACCR.X);
4755 ("\?program execution may be erroneous", ACCR.N);
4756 Error_Msg_Uint_1 := X_Size;
4758 ("\?size of & is ^", ACCR.N, ACCR.X);
4759 Error_Msg_Uint_1 := Y_Size;
4761 ("\?size of & is ^", ACCR.N, ACCR.Y);
4763 -- Check for inadequate alignment, both of the base object
4764 -- and of the offset, if any.
4766 -- Note: we do not check the alignment if we gave a size
4767 -- warning, since it would likely be redundant.
4769 elsif Y_Alignment /= Uint_0
4770 and then (Y_Alignment < X_Alignment
4773 Nkind (Expr) = N_Attribute_Reference
4775 Attribute_Name (Expr) = Name_Address
4777 Has_Compatible_Alignment
4778 (ACCR.X, Prefix (Expr))
4779 /= Known_Compatible))
4782 ("?specified address for& may be inconsistent "
4786 ("\?program execution may be erroneous (RM 13.3(27))",
4788 Error_Msg_Uint_1 := X_Alignment;
4790 ("\?alignment of & is ^",
4792 Error_Msg_Uint_1 := Y_Alignment;
4794 ("\?alignment of & is ^",
4796 if Y_Alignment >= X_Alignment then
4798 ("\?but offset is not multiple of alignment",
4805 end Validate_Address_Clauses;
4807 -----------------------------------
4808 -- Validate_Unchecked_Conversion --
4809 -----------------------------------
4811 procedure Validate_Unchecked_Conversion
4813 Act_Unit : Entity_Id)
4820 -- Obtain source and target types. Note that we call Ancestor_Subtype
4821 -- here because the processing for generic instantiation always makes
4822 -- subtypes, and we want the original frozen actual types.
4824 -- If we are dealing with private types, then do the check on their
4825 -- fully declared counterparts if the full declarations have been
4826 -- encountered (they don't have to be visible, but they must exist!)
4828 Source := Ancestor_Subtype (Etype (First_Formal (Act_Unit)));
4830 if Is_Private_Type (Source)
4831 and then Present (Underlying_Type (Source))
4833 Source := Underlying_Type (Source);
4836 Target := Ancestor_Subtype (Etype (Act_Unit));
4838 -- If either type is generic, the instantiation happens within a generic
4839 -- unit, and there is nothing to check. The proper check
4840 -- will happen when the enclosing generic is instantiated.
4842 if Is_Generic_Type (Source) or else Is_Generic_Type (Target) then
4846 if Is_Private_Type (Target)
4847 and then Present (Underlying_Type (Target))
4849 Target := Underlying_Type (Target);
4852 -- Source may be unconstrained array, but not target
4854 if Is_Array_Type (Target)
4855 and then not Is_Constrained (Target)
4858 ("unchecked conversion to unconstrained array not allowed", N);
4862 -- Warn if conversion between two different convention pointers
4864 if Is_Access_Type (Target)
4865 and then Is_Access_Type (Source)
4866 and then Convention (Target) /= Convention (Source)
4867 and then Warn_On_Unchecked_Conversion
4869 -- Give warnings for subprogram pointers only on most targets. The
4870 -- exception is VMS, where data pointers can have different lengths
4871 -- depending on the pointer convention.
4873 if Is_Access_Subprogram_Type (Target)
4874 or else Is_Access_Subprogram_Type (Source)
4875 or else OpenVMS_On_Target
4878 ("?conversion between pointers with different conventions!", N);
4882 -- Warn if one of the operands is Ada.Calendar.Time. Do not emit a
4883 -- warning when compiling GNAT-related sources.
4885 if Warn_On_Unchecked_Conversion
4886 and then not In_Predefined_Unit (N)
4887 and then RTU_Loaded (Ada_Calendar)
4889 (Chars (Source) = Name_Time
4891 Chars (Target) = Name_Time)
4893 -- If Ada.Calendar is loaded and the name of one of the operands is
4894 -- Time, there is a good chance that this is Ada.Calendar.Time.
4897 Calendar_Time : constant Entity_Id :=
4898 Full_View (RTE (RO_CA_Time));
4900 pragma Assert (Present (Calendar_Time));
4902 if Source = Calendar_Time
4903 or else Target = Calendar_Time
4906 ("?representation of 'Time values may change between " &
4907 "'G'N'A'T versions", N);
4912 -- Make entry in unchecked conversion table for later processing by
4913 -- Validate_Unchecked_Conversions, which will check sizes and alignments
4914 -- (using values set by the back-end where possible). This is only done
4915 -- if the appropriate warning is active.
4917 if Warn_On_Unchecked_Conversion then
4918 Unchecked_Conversions.Append
4919 (New_Val => UC_Entry'
4924 -- If both sizes are known statically now, then back end annotation
4925 -- is not required to do a proper check but if either size is not
4926 -- known statically, then we need the annotation.
4928 if Known_Static_RM_Size (Source)
4929 and then Known_Static_RM_Size (Target)
4933 Back_Annotate_Rep_Info := True;
4937 -- If unchecked conversion to access type, and access type is declared
4938 -- in the same unit as the unchecked conversion, then set the
4939 -- No_Strict_Aliasing flag (no strict aliasing is implicit in this
4942 if Is_Access_Type (Target) and then
4943 In_Same_Source_Unit (Target, N)
4945 Set_No_Strict_Aliasing (Implementation_Base_Type (Target));
4948 -- Generate N_Validate_Unchecked_Conversion node for back end in
4949 -- case the back end needs to perform special validation checks.
4951 -- Shouldn't this be in Exp_Ch13, since the check only gets done
4952 -- if we have full expansion and the back end is called ???
4955 Make_Validate_Unchecked_Conversion (Sloc (N));
4956 Set_Source_Type (Vnode, Source);
4957 Set_Target_Type (Vnode, Target);
4959 -- If the unchecked conversion node is in a list, just insert before it.
4960 -- If not we have some strange case, not worth bothering about.
4962 if Is_List_Member (N) then
4963 Insert_After (N, Vnode);
4965 end Validate_Unchecked_Conversion;
4967 ------------------------------------
4968 -- Validate_Unchecked_Conversions --
4969 ------------------------------------
4971 procedure Validate_Unchecked_Conversions is
4973 for N in Unchecked_Conversions.First .. Unchecked_Conversions.Last loop
4975 T : UC_Entry renames Unchecked_Conversions.Table (N);
4977 Eloc : constant Source_Ptr := T.Eloc;
4978 Source : constant Entity_Id := T.Source;
4979 Target : constant Entity_Id := T.Target;
4985 -- This validation check, which warns if we have unequal sizes for
4986 -- unchecked conversion, and thus potentially implementation
4987 -- dependent semantics, is one of the few occasions on which we
4988 -- use the official RM size instead of Esize. See description in
4989 -- Einfo "Handling of Type'Size Values" for details.
4991 if Serious_Errors_Detected = 0
4992 and then Known_Static_RM_Size (Source)
4993 and then Known_Static_RM_Size (Target)
4995 -- Don't do the check if warnings off for either type, note the
4996 -- deliberate use of OR here instead of OR ELSE to get the flag
4997 -- Warnings_Off_Used set for both types if appropriate.
4999 and then not (Has_Warnings_Off (Source)
5001 Has_Warnings_Off (Target))
5003 Source_Siz := RM_Size (Source);
5004 Target_Siz := RM_Size (Target);
5006 if Source_Siz /= Target_Siz then
5008 ("?types for unchecked conversion have different sizes!",
5011 if All_Errors_Mode then
5012 Error_Msg_Name_1 := Chars (Source);
5013 Error_Msg_Uint_1 := Source_Siz;
5014 Error_Msg_Name_2 := Chars (Target);
5015 Error_Msg_Uint_2 := Target_Siz;
5016 Error_Msg ("\size of % is ^, size of % is ^?", Eloc);
5018 Error_Msg_Uint_1 := UI_Abs (Source_Siz - Target_Siz);
5020 if Is_Discrete_Type (Source)
5021 and then Is_Discrete_Type (Target)
5023 if Source_Siz > Target_Siz then
5025 ("\?^ high order bits of source will be ignored!",
5028 elsif Is_Unsigned_Type (Source) then
5030 ("\?source will be extended with ^ high order " &
5031 "zero bits?!", Eloc);
5035 ("\?source will be extended with ^ high order " &
5040 elsif Source_Siz < Target_Siz then
5041 if Is_Discrete_Type (Target) then
5042 if Bytes_Big_Endian then
5044 ("\?target value will include ^ undefined " &
5049 ("\?target value will include ^ undefined " &
5056 ("\?^ trailing bits of target value will be " &
5057 "undefined!", Eloc);
5060 else pragma Assert (Source_Siz > Target_Siz);
5062 ("\?^ trailing bits of source will be ignored!",
5069 -- If both types are access types, we need to check the alignment.
5070 -- If the alignment of both is specified, we can do it here.
5072 if Serious_Errors_Detected = 0
5073 and then Ekind (Source) in Access_Kind
5074 and then Ekind (Target) in Access_Kind
5075 and then Target_Strict_Alignment
5076 and then Present (Designated_Type (Source))
5077 and then Present (Designated_Type (Target))
5080 D_Source : constant Entity_Id := Designated_Type (Source);
5081 D_Target : constant Entity_Id := Designated_Type (Target);
5084 if Known_Alignment (D_Source)
5085 and then Known_Alignment (D_Target)
5088 Source_Align : constant Uint := Alignment (D_Source);
5089 Target_Align : constant Uint := Alignment (D_Target);
5092 if Source_Align < Target_Align
5093 and then not Is_Tagged_Type (D_Source)
5095 -- Suppress warning if warnings suppressed on either
5096 -- type or either designated type. Note the use of
5097 -- OR here instead of OR ELSE. That is intentional,
5098 -- we would like to set flag Warnings_Off_Used in
5099 -- all types for which warnings are suppressed.
5101 and then not (Has_Warnings_Off (D_Source)
5103 Has_Warnings_Off (D_Target)
5105 Has_Warnings_Off (Source)
5107 Has_Warnings_Off (Target))
5109 Error_Msg_Uint_1 := Target_Align;
5110 Error_Msg_Uint_2 := Source_Align;
5111 Error_Msg_Node_1 := D_Target;
5112 Error_Msg_Node_2 := D_Source;
5114 ("?alignment of & (^) is stricter than " &
5115 "alignment of & (^)!", Eloc);
5117 ("\?resulting access value may have invalid " &
5118 "alignment!", Eloc);
5126 end Validate_Unchecked_Conversions;