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 Errout; use Errout;
30 with Exp_Tss; use Exp_Tss;
31 with Exp_Util; use Exp_Util;
33 with Lib.Xref; use Lib.Xref;
34 with Namet; use Namet;
35 with Nlists; use Nlists;
36 with Nmake; use Nmake;
38 with Restrict; use Restrict;
39 with Rident; use Rident;
40 with Rtsfind; use Rtsfind;
42 with Sem_Aux; use Sem_Aux;
43 with Sem_Ch3; use Sem_Ch3;
44 with Sem_Ch8; use Sem_Ch8;
45 with Sem_Eval; use Sem_Eval;
46 with Sem_Res; use Sem_Res;
47 with Sem_Type; use Sem_Type;
48 with Sem_Util; use Sem_Util;
49 with Sem_Warn; use Sem_Warn;
50 with Snames; use Snames;
51 with Stand; use Stand;
52 with Sinfo; use Sinfo;
54 with Targparm; use Targparm;
55 with Ttypes; use Ttypes;
56 with Tbuild; use Tbuild;
57 with Urealp; use Urealp;
59 with GNAT.Heap_Sort_G;
61 package body Sem_Ch13 is
63 SSU : constant Pos := System_Storage_Unit;
64 -- Convenient short hand for commonly used constant
66 -----------------------
67 -- Local Subprograms --
68 -----------------------
70 procedure Alignment_Check_For_Esize_Change (Typ : Entity_Id);
71 -- This routine is called after setting the Esize of type entity Typ.
72 -- The purpose is to deal with the situation where an alignment has been
73 -- inherited from a derived type that is no longer appropriate for the
74 -- new Esize value. In this case, we reset the Alignment to unknown.
76 function Get_Alignment_Value (Expr : Node_Id) return Uint;
77 -- Given the expression for an alignment value, returns the corresponding
78 -- Uint value. If the value is inappropriate, then error messages are
79 -- posted as required, and a value of No_Uint is returned.
81 function Is_Operational_Item (N : Node_Id) return Boolean;
82 -- A specification for a stream attribute is allowed before the full
83 -- type is declared, as explained in AI-00137 and the corrigendum.
84 -- Attributes that do not specify a representation characteristic are
85 -- operational attributes.
87 procedure New_Stream_Subprogram
92 -- Create a subprogram renaming of a given stream attribute to the
93 -- designated subprogram and then in the tagged case, provide this as a
94 -- primitive operation, or in the non-tagged case make an appropriate TSS
95 -- entry. This is more properly an expansion activity than just semantics,
96 -- but the presence of user-defined stream functions for limited types is a
97 -- legality check, which is why this takes place here rather than in
98 -- exp_ch13, where it was previously. Nam indicates the name of the TSS
99 -- function to be generated.
101 -- To avoid elaboration anomalies with freeze nodes, for untagged types
102 -- we generate both a subprogram declaration and a subprogram renaming
103 -- declaration, so that the attribute specification is handled as a
104 -- renaming_as_body. For tagged types, the specification is one of the
107 ----------------------------------------------
108 -- Table for Validate_Unchecked_Conversions --
109 ----------------------------------------------
111 -- The following table collects unchecked conversions for validation.
112 -- Entries are made by Validate_Unchecked_Conversion and then the
113 -- call to Validate_Unchecked_Conversions does the actual error
114 -- checking and posting of warnings. The reason for this delayed
115 -- processing is to take advantage of back-annotations of size and
116 -- alignment values performed by the back end.
118 -- Note: the reason we store a Source_Ptr value instead of a Node_Id
119 -- is that by the time Validate_Unchecked_Conversions is called, Sprint
120 -- will already have modified all Sloc values if the -gnatD option is set.
122 type UC_Entry is record
123 Eloc : Source_Ptr; -- node used for posting warnings
124 Source : Entity_Id; -- source type for unchecked conversion
125 Target : Entity_Id; -- target type for unchecked conversion
128 package Unchecked_Conversions is new Table.Table (
129 Table_Component_Type => UC_Entry,
130 Table_Index_Type => Int,
131 Table_Low_Bound => 1,
133 Table_Increment => 200,
134 Table_Name => "Unchecked_Conversions");
136 ----------------------------------------
137 -- Table for Validate_Address_Clauses --
138 ----------------------------------------
140 -- If an address clause has the form
142 -- for X'Address use Expr
144 -- where Expr is of the form Y'Address or recursively is a reference
145 -- to a constant of either of these forms, and X and Y are entities of
146 -- objects, then if Y has a smaller alignment than X, that merits a
147 -- warning about possible bad alignment. The following table collects
148 -- address clauses of this kind. We put these in a table so that they
149 -- can be checked after the back end has completed annotation of the
150 -- alignments of objects, since we can catch more cases that way.
152 type Address_Clause_Check_Record is record
154 -- The address clause
157 -- The entity of the object overlaying Y
160 -- The entity of the object being overlaid
163 -- Whether the address is offseted within Y
166 package Address_Clause_Checks is new Table.Table (
167 Table_Component_Type => Address_Clause_Check_Record,
168 Table_Index_Type => Int,
169 Table_Low_Bound => 1,
171 Table_Increment => 200,
172 Table_Name => "Address_Clause_Checks");
174 -----------------------------------------
175 -- Adjust_Record_For_Reverse_Bit_Order --
176 -----------------------------------------
178 procedure Adjust_Record_For_Reverse_Bit_Order (R : Entity_Id) is
183 -- Processing depends on version of Ada
187 -- For Ada 95, we just renumber bits within a storage unit. We do
188 -- the same for Ada 83 mode, since we recognize pragma Bit_Order
189 -- in Ada 83, and are free to add this extension.
191 when Ada_83 | Ada_95 =>
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-
197 -- default bit order case for Ada 95 mode.
199 -- We only do this processing for the base type, and in
200 -- fact that's important, since otherwise if there are
201 -- record subtypes, we could reverse the bits once for
202 -- each subtype, which would be incorrect.
205 and then Ekind (R) = E_Record_Type
208 CFB : constant Uint := Component_Bit_Offset (Comp);
209 CSZ : constant Uint := Esize (Comp);
210 CLC : constant Node_Id := Component_Clause (Comp);
211 Pos : constant Node_Id := Position (CLC);
212 FB : constant Node_Id := First_Bit (CLC);
214 Storage_Unit_Offset : constant Uint :=
215 CFB / System_Storage_Unit;
217 Start_Bit : constant Uint :=
218 CFB mod System_Storage_Unit;
221 -- Cases where field goes over storage unit boundary
223 if Start_Bit + CSZ > System_Storage_Unit then
225 -- Allow multi-byte field but generate warning
227 if Start_Bit mod System_Storage_Unit = 0
228 and then CSZ mod System_Storage_Unit = 0
231 ("multi-byte field specified with non-standard"
232 & " Bit_Order?", CLC);
234 if Bytes_Big_Endian then
236 ("bytes are not reversed "
237 & "(component is big-endian)?", CLC);
240 ("bytes are not reversed "
241 & "(component is little-endian)?", CLC);
244 -- Do not allow non-contiguous field
248 ("attempt to specify non-contiguous field "
249 & "not permitted", CLC);
251 ("\caused by non-standard Bit_Order "
254 ("\consider possibility of using "
255 & "Ada 2005 mode here", CLC);
258 -- Case where field fits in one storage unit
261 -- Give warning if suspicious component clause
263 if Intval (FB) >= System_Storage_Unit
264 and then Warn_On_Reverse_Bit_Order
267 ("?Bit_Order clause does not affect " &
268 "byte ordering", Pos);
270 Intval (Pos) + Intval (FB) /
273 ("?position normalized to ^ before bit " &
274 "order interpreted", Pos);
277 -- Here is where we fix up the Component_Bit_Offset
278 -- value to account for the reverse bit order.
279 -- Some examples of what needs to be done are:
281 -- First_Bit .. Last_Bit Component_Bit_Offset
293 -- The general rule is that the first bit is
294 -- is obtained by subtracting the old ending bit
295 -- from storage_unit - 1.
297 Set_Component_Bit_Offset
299 (Storage_Unit_Offset * System_Storage_Unit) +
300 (System_Storage_Unit - 1) -
301 (Start_Bit + CSZ - 1));
303 Set_Normalized_First_Bit
305 Component_Bit_Offset (Comp) mod
306 System_Storage_Unit);
311 Next_Component_Or_Discriminant (Comp);
314 -- For Ada 2005, we do machine scalar processing, as fully described
315 -- In AI-133. This involves gathering all components which start at
316 -- the same byte offset and processing them together
320 Max_Machine_Scalar_Size : constant Uint :=
322 (Standard_Long_Long_Integer_Size);
323 -- We use this as the maximum machine scalar size
326 SSU : constant Uint := UI_From_Int (System_Storage_Unit);
329 -- This first loop through components does two things. First it
330 -- deals with the case of components with component clauses
331 -- whose length is greater than the maximum machine scalar size
332 -- (either accepting them or rejecting as needed). Second, it
333 -- counts the number of components with component clauses whose
334 -- length does not exceed this maximum for later processing.
337 Comp := First_Component_Or_Discriminant (R);
338 while Present (Comp) loop
339 CC := Component_Clause (Comp);
343 Fbit : constant Uint :=
344 Static_Integer (First_Bit (CC));
347 -- Case of component with size > max machine scalar
349 if Esize (Comp) > Max_Machine_Scalar_Size then
351 -- Must begin on byte boundary
353 if Fbit mod SSU /= 0 then
355 ("illegal first bit value for "
356 & "reverse bit order",
358 Error_Msg_Uint_1 := SSU;
359 Error_Msg_Uint_2 := Max_Machine_Scalar_Size;
362 ("\must be a multiple of ^ "
363 & "if size greater than ^",
366 -- Must end on byte boundary
368 elsif Esize (Comp) mod SSU /= 0 then
370 ("illegal last bit value for "
371 & "reverse bit order",
373 Error_Msg_Uint_1 := SSU;
374 Error_Msg_Uint_2 := Max_Machine_Scalar_Size;
377 ("\must be a multiple of ^ if size "
381 -- OK, give warning if enabled
383 elsif Warn_On_Reverse_Bit_Order then
385 ("multi-byte field specified with "
386 & " non-standard Bit_Order?", CC);
388 if Bytes_Big_Endian then
390 ("\bytes are not reversed "
391 & "(component is big-endian)?", CC);
394 ("\bytes are not reversed "
395 & "(component is little-endian)?", CC);
399 -- Case where size is not greater than max machine
400 -- scalar. For now, we just count these.
403 Num_CC := Num_CC + 1;
408 Next_Component_Or_Discriminant (Comp);
411 -- We need to sort the component clauses on the basis of the
412 -- Position values in the clause, so we can group clauses with
413 -- the same Position. together to determine the relevant
414 -- machine scalar size.
417 Comps : array (0 .. Num_CC) of Entity_Id;
418 -- Array to collect component and discriminant entities. The
419 -- data starts at index 1, the 0'th entry is for the sort
422 function CP_Lt (Op1, Op2 : Natural) return Boolean;
423 -- Compare routine for Sort
425 procedure CP_Move (From : Natural; To : Natural);
426 -- Move routine for Sort
428 package Sorting is new GNAT.Heap_Sort_G (CP_Move, CP_Lt);
432 -- Start and stop positions in component list of set of
433 -- components with the same starting position (that
434 -- constitute components in a single machine scalar).
437 -- Maximum last bit value of any component in this set
440 -- Corresponding machine scalar size
446 function CP_Lt (Op1, Op2 : Natural) return Boolean is
448 return Position (Component_Clause (Comps (Op1))) <
449 Position (Component_Clause (Comps (Op2)));
456 procedure CP_Move (From : Natural; To : Natural) is
458 Comps (To) := Comps (From);
461 -- Start of processing for Sort_CC
464 -- Collect the component clauses
467 Comp := First_Component_Or_Discriminant (R);
468 while Present (Comp) loop
469 if Present (Component_Clause (Comp))
470 and then Esize (Comp) <= Max_Machine_Scalar_Size
472 Num_CC := Num_CC + 1;
473 Comps (Num_CC) := Comp;
476 Next_Component_Or_Discriminant (Comp);
479 -- Sort by ascending position number
481 Sorting.Sort (Num_CC);
483 -- We now have all the components whose size does not exceed
484 -- the max machine scalar value, sorted by starting
485 -- position. In this loop we gather groups of clauses
486 -- starting at the same position, to process them in
487 -- accordance with Ada 2005 AI-133.
490 while Stop < Num_CC loop
495 (Last_Bit (Component_Clause (Comps (Start))));
496 while Stop < Num_CC loop
498 (Position (Component_Clause (Comps (Stop + 1)))) =
500 (Position (Component_Clause (Comps (Stop))))
508 (Component_Clause (Comps (Stop)))));
514 -- Now we have a group of component clauses from Start to
515 -- Stop whose positions are identical, and MaxL is the
516 -- maximum last bit value of any of these components.
518 -- We need to determine the corresponding machine scalar
519 -- size. This loop assumes that machine scalar sizes are
520 -- even, and that each possible machine scalar has twice
521 -- as many bits as the next smaller one.
523 MSS := Max_Machine_Scalar_Size;
525 and then (MSS / 2) >= SSU
526 and then (MSS / 2) > MaxL
531 -- Here is where we fix up the Component_Bit_Offset value
532 -- to account for the reverse bit order. Some examples of
533 -- what needs to be done for the case of a machine scalar
536 -- First_Bit .. Last_Bit Component_Bit_Offset
548 -- The general rule is that the first bit is obtained by
549 -- subtracting the old ending bit from machine scalar
552 for C in Start .. Stop loop
554 Comp : constant Entity_Id := Comps (C);
555 CC : constant Node_Id :=
556 Component_Clause (Comp);
557 LB : constant Uint :=
558 Static_Integer (Last_Bit (CC));
559 NFB : constant Uint := MSS - Uint_1 - LB;
560 NLB : constant Uint := NFB + Esize (Comp) - 1;
561 Pos : constant Uint :=
562 Static_Integer (Position (CC));
565 if Warn_On_Reverse_Bit_Order then
566 Error_Msg_Uint_1 := MSS;
568 ("info: reverse bit order in machine " &
569 "scalar of length^?", First_Bit (CC));
570 Error_Msg_Uint_1 := NFB;
571 Error_Msg_Uint_2 := NLB;
573 if Bytes_Big_Endian then
575 ("?\info: big-endian range for "
576 & "component & is ^ .. ^",
577 First_Bit (CC), Comp);
580 ("?\info: little-endian range "
581 & "for component & is ^ .. ^",
582 First_Bit (CC), Comp);
586 Set_Component_Bit_Offset (Comp, Pos * SSU + NFB);
587 Set_Normalized_First_Bit (Comp, NFB mod SSU);
594 end Adjust_Record_For_Reverse_Bit_Order;
596 --------------------------------------
597 -- Alignment_Check_For_Esize_Change --
598 --------------------------------------
600 procedure Alignment_Check_For_Esize_Change (Typ : Entity_Id) is
602 -- If the alignment is known, and not set by a rep clause, and is
603 -- inconsistent with the size being set, then reset it to unknown,
604 -- we assume in this case that the size overrides the inherited
605 -- alignment, and that the alignment must be recomputed.
607 if Known_Alignment (Typ)
608 and then not Has_Alignment_Clause (Typ)
609 and then Esize (Typ) mod (Alignment (Typ) * SSU) /= 0
611 Init_Alignment (Typ);
613 end Alignment_Check_For_Esize_Change;
615 -----------------------
616 -- Analyze_At_Clause --
617 -----------------------
619 -- An at clause is replaced by the corresponding Address attribute
620 -- definition clause that is the preferred approach in Ada 95.
622 procedure Analyze_At_Clause (N : Node_Id) is
623 CS : constant Boolean := Comes_From_Source (N);
626 -- This is an obsolescent feature
628 Check_Restriction (No_Obsolescent_Features, N);
630 if Warn_On_Obsolescent_Feature then
632 ("at clause is an obsolescent feature (RM J.7(2))?", N);
634 ("\use address attribute definition clause instead?", N);
637 -- Rewrite as address clause
640 Make_Attribute_Definition_Clause (Sloc (N),
641 Name => Identifier (N),
642 Chars => Name_Address,
643 Expression => Expression (N)));
645 -- We preserve Comes_From_Source, since logically the clause still
646 -- comes from the source program even though it is changed in form.
648 Set_Comes_From_Source (N, CS);
650 -- Analyze rewritten clause
652 Analyze_Attribute_Definition_Clause (N);
653 end Analyze_At_Clause;
655 -----------------------------------------
656 -- Analyze_Attribute_Definition_Clause --
657 -----------------------------------------
659 procedure Analyze_Attribute_Definition_Clause (N : Node_Id) is
660 Loc : constant Source_Ptr := Sloc (N);
661 Nam : constant Node_Id := Name (N);
662 Attr : constant Name_Id := Chars (N);
663 Expr : constant Node_Id := Expression (N);
664 Id : constant Attribute_Id := Get_Attribute_Id (Attr);
668 FOnly : Boolean := False;
669 -- Reset to True for subtype specific attribute (Alignment, Size)
670 -- and for stream attributes, i.e. those cases where in the call
671 -- to Rep_Item_Too_Late, FOnly is set True so that only the freezing
672 -- rules are checked. Note that the case of stream attributes is not
673 -- clear from the RM, but see AI95-00137. Also, the RM seems to
674 -- disallow Storage_Size for derived task types, but that is also
675 -- clearly unintentional.
677 procedure Analyze_Stream_TSS_Definition (TSS_Nam : TSS_Name_Type);
678 -- Common processing for 'Read, 'Write, 'Input and 'Output attribute
679 -- definition clauses.
681 -----------------------------------
682 -- Analyze_Stream_TSS_Definition --
683 -----------------------------------
685 procedure Analyze_Stream_TSS_Definition (TSS_Nam : TSS_Name_Type) is
686 Subp : Entity_Id := Empty;
691 Is_Read : constant Boolean := (TSS_Nam = TSS_Stream_Read);
693 function Has_Good_Profile (Subp : Entity_Id) return Boolean;
694 -- Return true if the entity is a subprogram with an appropriate
695 -- profile for the attribute being defined.
697 ----------------------
698 -- Has_Good_Profile --
699 ----------------------
701 function Has_Good_Profile (Subp : Entity_Id) return Boolean is
703 Is_Function : constant Boolean := (TSS_Nam = TSS_Stream_Input);
704 Expected_Ekind : constant array (Boolean) of Entity_Kind :=
705 (False => E_Procedure, True => E_Function);
709 if Ekind (Subp) /= Expected_Ekind (Is_Function) then
713 F := First_Formal (Subp);
716 or else Ekind (Etype (F)) /= E_Anonymous_Access_Type
717 or else Designated_Type (Etype (F)) /=
718 Class_Wide_Type (RTE (RE_Root_Stream_Type))
723 if not Is_Function then
727 Expected_Mode : constant array (Boolean) of Entity_Kind :=
728 (False => E_In_Parameter,
729 True => E_Out_Parameter);
731 if Parameter_Mode (F) /= Expected_Mode (Is_Read) then
742 return Base_Type (Typ) = Base_Type (Ent)
743 and then No (Next_Formal (F));
744 end Has_Good_Profile;
746 -- Start of processing for Analyze_Stream_TSS_Definition
751 if not Is_Type (U_Ent) then
752 Error_Msg_N ("local name must be a subtype", Nam);
756 Pnam := TSS (Base_Type (U_Ent), TSS_Nam);
758 -- If Pnam is present, it can be either inherited from an ancestor
759 -- type (in which case it is legal to redefine it for this type), or
760 -- be a previous definition of the attribute for the same type (in
761 -- which case it is illegal).
763 -- In the first case, it will have been analyzed already, and we
764 -- can check that its profile does not match the expected profile
765 -- for a stream attribute of U_Ent. In the second case, either Pnam
766 -- has been analyzed (and has the expected profile), or it has not
767 -- been analyzed yet (case of a type that has not been frozen yet
768 -- and for which the stream attribute has been set using Set_TSS).
771 and then (No (First_Entity (Pnam)) or else Has_Good_Profile (Pnam))
773 Error_Msg_Sloc := Sloc (Pnam);
774 Error_Msg_Name_1 := Attr;
775 Error_Msg_N ("% attribute already defined #", Nam);
781 if Is_Entity_Name (Expr) then
782 if not Is_Overloaded (Expr) then
783 if Has_Good_Profile (Entity (Expr)) then
784 Subp := Entity (Expr);
788 Get_First_Interp (Expr, I, It);
789 while Present (It.Nam) loop
790 if Has_Good_Profile (It.Nam) then
795 Get_Next_Interp (I, It);
800 if Present (Subp) then
801 if Is_Abstract_Subprogram (Subp) then
802 Error_Msg_N ("stream subprogram must not be abstract", Expr);
806 Set_Entity (Expr, Subp);
807 Set_Etype (Expr, Etype (Subp));
809 New_Stream_Subprogram (N, U_Ent, Subp, TSS_Nam);
812 Error_Msg_Name_1 := Attr;
813 Error_Msg_N ("incorrect expression for% attribute", Expr);
815 end Analyze_Stream_TSS_Definition;
817 -- Start of processing for Analyze_Attribute_Definition_Clause
820 -- Process Ignore_Rep_Clauses option
822 if Ignore_Rep_Clauses then
825 -- The following should be ignored. They do not affect legality
826 -- and may be target dependent. The basic idea of -gnatI is to
827 -- ignore any rep clauses that may be target dependent but do not
828 -- affect legality (except possibly to be rejected because they
829 -- are incompatible with the compilation target).
831 when Attribute_Alignment |
832 Attribute_Bit_Order |
833 Attribute_Component_Size |
834 Attribute_Machine_Radix |
835 Attribute_Object_Size |
838 Attribute_Stream_Size |
839 Attribute_Value_Size =>
841 Rewrite (N, Make_Null_Statement (Sloc (N)));
844 -- The following should not be ignored, because in the first place
845 -- they are reasonably portable, and should not cause problems in
846 -- compiling code from another target, and also they do affect
847 -- legality, e.g. failing to provide a stream attribute for a
848 -- type may make a program illegal.
850 when Attribute_External_Tag |
854 Attribute_Storage_Pool |
855 Attribute_Storage_Size |
859 -- Other cases are errors, which will be caught below
869 if Rep_Item_Too_Early (Ent, N) then
873 -- Rep clause applies to full view of incomplete type or private type if
874 -- we have one (if not, this is a premature use of the type). However,
875 -- certain semantic checks need to be done on the specified entity (i.e.
876 -- the private view), so we save it in Ent.
878 if Is_Private_Type (Ent)
879 and then Is_Derived_Type (Ent)
880 and then not Is_Tagged_Type (Ent)
881 and then No (Full_View (Ent))
883 -- If this is a private type whose completion is a derivation from
884 -- another private type, there is no full view, and the attribute
885 -- belongs to the type itself, not its underlying parent.
889 elsif Ekind (Ent) = E_Incomplete_Type then
891 -- The attribute applies to the full view, set the entity of the
892 -- attribute definition accordingly.
894 Ent := Underlying_Type (Ent);
896 Set_Entity (Nam, Ent);
899 U_Ent := Underlying_Type (Ent);
902 -- Complete other routine error checks
904 if Etype (Nam) = Any_Type then
907 elsif Scope (Ent) /= Current_Scope then
908 Error_Msg_N ("entity must be declared in this scope", Nam);
911 elsif No (U_Ent) then
914 elsif Is_Type (U_Ent)
915 and then not Is_First_Subtype (U_Ent)
916 and then Id /= Attribute_Object_Size
917 and then Id /= Attribute_Value_Size
918 and then not From_At_Mod (N)
920 Error_Msg_N ("cannot specify attribute for subtype", Nam);
924 -- Switch on particular attribute
932 -- Address attribute definition clause
934 when Attribute_Address => Address : begin
936 -- A little error check, catch for X'Address use X'Address;
938 if Nkind (Nam) = N_Identifier
939 and then Nkind (Expr) = N_Attribute_Reference
940 and then Attribute_Name (Expr) = Name_Address
941 and then Nkind (Prefix (Expr)) = N_Identifier
942 and then Chars (Nam) = Chars (Prefix (Expr))
945 ("address for & is self-referencing", Prefix (Expr), Ent);
949 -- Not that special case, carry on with analysis of expression
951 Analyze_And_Resolve (Expr, RTE (RE_Address));
953 -- Even when ignoring rep clauses we need to indicate that the
954 -- entity has an address clause and thus it is legal to declare
957 if Ignore_Rep_Clauses then
958 if Ekind_In (U_Ent, E_Variable, E_Constant) then
959 Record_Rep_Item (U_Ent, N);
965 if Present (Address_Clause (U_Ent)) then
966 Error_Msg_N ("address already given for &", Nam);
968 -- Case of address clause for subprogram
970 elsif Is_Subprogram (U_Ent) then
971 if Has_Homonym (U_Ent) then
973 ("address clause cannot be given " &
974 "for overloaded subprogram",
979 -- For subprograms, all address clauses are permitted, and we
980 -- mark the subprogram as having a deferred freeze so that Gigi
981 -- will not elaborate it too soon.
983 -- Above needs more comments, what is too soon about???
985 Set_Has_Delayed_Freeze (U_Ent);
987 -- Case of address clause for entry
989 elsif Ekind (U_Ent) = E_Entry then
990 if Nkind (Parent (N)) = N_Task_Body then
992 ("entry address must be specified in task spec", Nam);
996 -- For entries, we require a constant address
998 Check_Constant_Address_Clause (Expr, U_Ent);
1000 -- Special checks for task types
1002 if Is_Task_Type (Scope (U_Ent))
1003 and then Comes_From_Source (Scope (U_Ent))
1006 ("?entry address declared for entry in task type", N);
1008 ("\?only one task can be declared of this type", N);
1011 -- Entry address clauses are obsolescent
1013 Check_Restriction (No_Obsolescent_Features, N);
1015 if Warn_On_Obsolescent_Feature then
1017 ("attaching interrupt to task entry is an " &
1018 "obsolescent feature (RM J.7.1)?", N);
1020 ("\use interrupt procedure instead?", N);
1023 -- Case of an address clause for a controlled object which we
1024 -- consider to be erroneous.
1026 elsif Is_Controlled (Etype (U_Ent))
1027 or else Has_Controlled_Component (Etype (U_Ent))
1030 ("?controlled object& must not be overlaid", Nam, U_Ent);
1032 ("\?Program_Error will be raised at run time", Nam);
1033 Insert_Action (Declaration_Node (U_Ent),
1034 Make_Raise_Program_Error (Loc,
1035 Reason => PE_Overlaid_Controlled_Object));
1038 -- Case of address clause for a (non-controlled) object
1041 Ekind (U_Ent) = E_Variable
1043 Ekind (U_Ent) = E_Constant
1046 Expr : constant Node_Id := Expression (N);
1051 -- Exported variables cannot have an address clause, because
1052 -- this cancels the effect of the pragma Export.
1054 if Is_Exported (U_Ent) then
1056 ("cannot export object with address clause", Nam);
1060 Find_Overlaid_Entity (N, O_Ent, Off);
1062 -- Overlaying controlled objects is erroneous
1065 and then (Has_Controlled_Component (Etype (O_Ent))
1066 or else Is_Controlled (Etype (O_Ent)))
1069 ("?cannot overlay with controlled object", Expr);
1071 ("\?Program_Error will be raised at run time", Expr);
1072 Insert_Action (Declaration_Node (U_Ent),
1073 Make_Raise_Program_Error (Loc,
1074 Reason => PE_Overlaid_Controlled_Object));
1077 elsif Present (O_Ent)
1078 and then Ekind (U_Ent) = E_Constant
1079 and then not Is_Constant_Object (O_Ent)
1081 Error_Msg_N ("constant overlays a variable?", Expr);
1083 elsif Present (Renamed_Object (U_Ent)) then
1085 ("address clause not allowed"
1086 & " for a renaming declaration (RM 13.1(6))", Nam);
1089 -- Imported variables can have an address clause, but then
1090 -- the import is pretty meaningless except to suppress
1091 -- initializations, so we do not need such variables to
1092 -- be statically allocated (and in fact it causes trouble
1093 -- if the address clause is a local value).
1095 elsif Is_Imported (U_Ent) then
1096 Set_Is_Statically_Allocated (U_Ent, False);
1099 -- We mark a possible modification of a variable with an
1100 -- address clause, since it is likely aliasing is occurring.
1102 Note_Possible_Modification (Nam, Sure => False);
1104 -- Here we are checking for explicit overlap of one variable
1105 -- by another, and if we find this then mark the overlapped
1106 -- variable as also being volatile to prevent unwanted
1107 -- optimizations. This is a significant pessimization so
1108 -- avoid it when there is an offset, i.e. when the object
1109 -- is composite; they cannot be optimized easily anyway.
1112 and then Is_Object (O_Ent)
1115 Set_Treat_As_Volatile (O_Ent);
1118 -- Legality checks on the address clause for initialized
1119 -- objects is deferred until the freeze point, because
1120 -- a subsequent pragma might indicate that the object is
1121 -- imported and thus not initialized.
1123 Set_Has_Delayed_Freeze (U_Ent);
1125 -- If an initialization call has been generated for this
1126 -- object, it needs to be deferred to after the freeze node
1127 -- we have just now added, otherwise GIGI will see a
1128 -- reference to the variable (as actual to the IP call)
1129 -- before its definition.
1132 Init_Call : constant Node_Id := Find_Init_Call (U_Ent, N);
1134 if Present (Init_Call) then
1136 Append_Freeze_Action (U_Ent, Init_Call);
1140 if Is_Exported (U_Ent) then
1142 ("& cannot be exported if an address clause is given",
1145 ("\define and export a variable " &
1146 "that holds its address instead",
1150 -- Entity has delayed freeze, so we will generate an
1151 -- alignment check at the freeze point unless suppressed.
1153 if not Range_Checks_Suppressed (U_Ent)
1154 and then not Alignment_Checks_Suppressed (U_Ent)
1156 Set_Check_Address_Alignment (N);
1159 -- Kill the size check code, since we are not allocating
1160 -- the variable, it is somewhere else.
1162 Kill_Size_Check_Code (U_Ent);
1164 -- If the address clause is of the form:
1166 -- for Y'Address use X'Address
1170 -- Const : constant Address := X'Address;
1172 -- for Y'Address use Const;
1174 -- then we make an entry in the table for checking the size
1175 -- and alignment of the overlaying variable. We defer this
1176 -- check till after code generation to take full advantage
1177 -- of the annotation done by the back end. This entry is
1178 -- only made if the address clause comes from source.
1179 -- If the entity has a generic type, the check will be
1180 -- performed in the instance if the actual type justifies
1181 -- it, and we do not insert the clause in the table to
1182 -- prevent spurious warnings.
1184 if Address_Clause_Overlay_Warnings
1185 and then Comes_From_Source (N)
1186 and then Present (O_Ent)
1187 and then Is_Object (O_Ent)
1189 if not Is_Generic_Type (Etype (U_Ent)) then
1190 Address_Clause_Checks.Append ((N, U_Ent, O_Ent, Off));
1193 -- If variable overlays a constant view, and we are
1194 -- warning on overlays, then mark the variable as
1195 -- overlaying a constant (we will give warnings later
1196 -- if this variable is assigned).
1198 if Is_Constant_Object (O_Ent)
1199 and then Ekind (U_Ent) = E_Variable
1201 Set_Overlays_Constant (U_Ent);
1206 -- Not a valid entity for an address clause
1209 Error_Msg_N ("address cannot be given for &", Nam);
1217 -- Alignment attribute definition clause
1219 when Attribute_Alignment => Alignment : declare
1220 Align : constant Uint := Get_Alignment_Value (Expr);
1225 if not Is_Type (U_Ent)
1226 and then Ekind (U_Ent) /= E_Variable
1227 and then Ekind (U_Ent) /= E_Constant
1229 Error_Msg_N ("alignment cannot be given for &", Nam);
1231 elsif Has_Alignment_Clause (U_Ent) then
1232 Error_Msg_Sloc := Sloc (Alignment_Clause (U_Ent));
1233 Error_Msg_N ("alignment clause previously given#", N);
1235 elsif Align /= No_Uint then
1236 Set_Has_Alignment_Clause (U_Ent);
1237 Set_Alignment (U_Ent, Align);
1239 -- For an array type, U_Ent is the first subtype. In that case,
1240 -- also set the alignment of the anonymous base type so that
1241 -- other subtypes (such as the itypes for aggregates of the
1242 -- type) also receive the expected alignment.
1244 if Is_Array_Type (U_Ent) then
1245 Set_Alignment (Base_Type (U_Ent), Align);
1254 -- Bit_Order attribute definition clause
1256 when Attribute_Bit_Order => Bit_Order : declare
1258 if not Is_Record_Type (U_Ent) then
1260 ("Bit_Order can only be defined for record type", Nam);
1263 Analyze_And_Resolve (Expr, RTE (RE_Bit_Order));
1265 if Etype (Expr) = Any_Type then
1268 elsif not Is_Static_Expression (Expr) then
1269 Flag_Non_Static_Expr
1270 ("Bit_Order requires static expression!", Expr);
1273 if (Expr_Value (Expr) = 0) /= Bytes_Big_Endian then
1274 Set_Reverse_Bit_Order (U_Ent, True);
1280 --------------------
1281 -- Component_Size --
1282 --------------------
1284 -- Component_Size attribute definition clause
1286 when Attribute_Component_Size => Component_Size_Case : declare
1287 Csize : constant Uint := Static_Integer (Expr);
1290 New_Ctyp : Entity_Id;
1294 if not Is_Array_Type (U_Ent) then
1295 Error_Msg_N ("component size requires array type", Nam);
1299 Btype := Base_Type (U_Ent);
1301 if Has_Component_Size_Clause (Btype) then
1303 ("component size clause for& previously given", Nam);
1305 elsif Csize /= No_Uint then
1306 Check_Size (Expr, Component_Type (Btype), Csize, Biased);
1308 if Has_Aliased_Components (Btype)
1311 and then Csize /= 16
1314 ("component size incorrect for aliased components", N);
1318 -- For the biased case, build a declaration for a subtype
1319 -- that will be used to represent the biased subtype that
1320 -- reflects the biased representation of components. We need
1321 -- this subtype to get proper conversions on referencing
1322 -- elements of the array. Note that component size clauses
1323 -- are ignored in VM mode.
1325 if VM_Target = No_VM then
1328 Make_Defining_Identifier (Loc,
1330 New_External_Name (Chars (U_Ent), 'C', 0, 'T'));
1333 Make_Subtype_Declaration (Loc,
1334 Defining_Identifier => New_Ctyp,
1335 Subtype_Indication =>
1336 New_Occurrence_Of (Component_Type (Btype), Loc));
1338 Set_Parent (Decl, N);
1339 Analyze (Decl, Suppress => All_Checks);
1341 Set_Has_Delayed_Freeze (New_Ctyp, False);
1342 Set_Esize (New_Ctyp, Csize);
1343 Set_RM_Size (New_Ctyp, Csize);
1344 Init_Alignment (New_Ctyp);
1345 Set_Has_Biased_Representation (New_Ctyp, True);
1346 Set_Is_Itype (New_Ctyp, True);
1347 Set_Associated_Node_For_Itype (New_Ctyp, U_Ent);
1349 Set_Component_Type (Btype, New_Ctyp);
1351 if Warn_On_Biased_Representation then
1353 ("?component size clause forces biased "
1354 & "representation", N);
1358 Set_Component_Size (Btype, Csize);
1360 -- For VM case, we ignore component size clauses
1363 -- Give a warning unless we are in GNAT mode, in which case
1364 -- the warning is suppressed since it is not useful.
1366 if not GNAT_Mode then
1368 ("?component size ignored in this configuration", N);
1372 Set_Has_Component_Size_Clause (Btype, True);
1373 Set_Has_Non_Standard_Rep (Btype, True);
1375 end Component_Size_Case;
1381 when Attribute_External_Tag => External_Tag :
1383 if not Is_Tagged_Type (U_Ent) then
1384 Error_Msg_N ("should be a tagged type", Nam);
1387 Analyze_And_Resolve (Expr, Standard_String);
1389 if not Is_Static_Expression (Expr) then
1390 Flag_Non_Static_Expr
1391 ("static string required for tag name!", Nam);
1394 if VM_Target = No_VM then
1395 Set_Has_External_Tag_Rep_Clause (U_Ent);
1397 Error_Msg_Name_1 := Attr;
1399 ("% attribute unsupported in this configuration", Nam);
1402 if not Is_Library_Level_Entity (U_Ent) then
1404 ("?non-unique external tag supplied for &", N, U_Ent);
1406 ("?\same external tag applies to all subprogram calls", N);
1408 ("?\corresponding internal tag cannot be obtained", N);
1416 when Attribute_Input =>
1417 Analyze_Stream_TSS_Definition (TSS_Stream_Input);
1418 Set_Has_Specified_Stream_Input (Ent);
1424 -- Machine radix attribute definition clause
1426 when Attribute_Machine_Radix => Machine_Radix : declare
1427 Radix : constant Uint := Static_Integer (Expr);
1430 if not Is_Decimal_Fixed_Point_Type (U_Ent) then
1431 Error_Msg_N ("decimal fixed-point type expected for &", Nam);
1433 elsif Has_Machine_Radix_Clause (U_Ent) then
1434 Error_Msg_Sloc := Sloc (Alignment_Clause (U_Ent));
1435 Error_Msg_N ("machine radix clause previously given#", N);
1437 elsif Radix /= No_Uint then
1438 Set_Has_Machine_Radix_Clause (U_Ent);
1439 Set_Has_Non_Standard_Rep (Base_Type (U_Ent));
1443 elsif Radix = 10 then
1444 Set_Machine_Radix_10 (U_Ent);
1446 Error_Msg_N ("machine radix value must be 2 or 10", Expr);
1455 -- Object_Size attribute definition clause
1457 when Attribute_Object_Size => Object_Size : declare
1458 Size : constant Uint := Static_Integer (Expr);
1461 pragma Warnings (Off, Biased);
1464 if not Is_Type (U_Ent) then
1465 Error_Msg_N ("Object_Size cannot be given for &", Nam);
1467 elsif Has_Object_Size_Clause (U_Ent) then
1468 Error_Msg_N ("Object_Size already given for &", Nam);
1471 Check_Size (Expr, U_Ent, Size, Biased);
1479 UI_Mod (Size, 64) /= 0
1482 ("Object_Size must be 8, 16, 32, or multiple of 64",
1486 Set_Esize (U_Ent, Size);
1487 Set_Has_Object_Size_Clause (U_Ent);
1488 Alignment_Check_For_Esize_Change (U_Ent);
1496 when Attribute_Output =>
1497 Analyze_Stream_TSS_Definition (TSS_Stream_Output);
1498 Set_Has_Specified_Stream_Output (Ent);
1504 when Attribute_Read =>
1505 Analyze_Stream_TSS_Definition (TSS_Stream_Read);
1506 Set_Has_Specified_Stream_Read (Ent);
1512 -- Size attribute definition clause
1514 when Attribute_Size => Size : declare
1515 Size : constant Uint := Static_Integer (Expr);
1522 if Has_Size_Clause (U_Ent) then
1523 Error_Msg_N ("size already given for &", Nam);
1525 elsif not Is_Type (U_Ent)
1526 and then Ekind (U_Ent) /= E_Variable
1527 and then Ekind (U_Ent) /= E_Constant
1529 Error_Msg_N ("size cannot be given for &", Nam);
1531 elsif Is_Array_Type (U_Ent)
1532 and then not Is_Constrained (U_Ent)
1535 ("size cannot be given for unconstrained array", Nam);
1537 elsif Size /= No_Uint then
1538 if Is_Type (U_Ent) then
1541 Etyp := Etype (U_Ent);
1544 -- Check size, note that Gigi is in charge of checking that the
1545 -- size of an array or record type is OK. Also we do not check
1546 -- the size in the ordinary fixed-point case, since it is too
1547 -- early to do so (there may be subsequent small clause that
1548 -- affects the size). We can check the size if a small clause
1549 -- has already been given.
1551 if not Is_Ordinary_Fixed_Point_Type (U_Ent)
1552 or else Has_Small_Clause (U_Ent)
1554 Check_Size (Expr, Etyp, Size, Biased);
1555 Set_Has_Biased_Representation (U_Ent, Biased);
1557 if Biased and Warn_On_Biased_Representation then
1559 ("?size clause forces biased representation", N);
1563 -- For types set RM_Size and Esize if possible
1565 if Is_Type (U_Ent) then
1566 Set_RM_Size (U_Ent, Size);
1568 -- For scalar types, increase Object_Size to power of 2, but
1569 -- not less than a storage unit in any case (i.e., normally
1570 -- this means it will be byte addressable).
1572 if Is_Scalar_Type (U_Ent) then
1573 if Size <= System_Storage_Unit then
1574 Init_Esize (U_Ent, System_Storage_Unit);
1575 elsif Size <= 16 then
1576 Init_Esize (U_Ent, 16);
1577 elsif Size <= 32 then
1578 Init_Esize (U_Ent, 32);
1580 Set_Esize (U_Ent, (Size + 63) / 64 * 64);
1583 -- For all other types, object size = value size. The
1584 -- backend will adjust as needed.
1587 Set_Esize (U_Ent, Size);
1590 Alignment_Check_For_Esize_Change (U_Ent);
1592 -- For objects, set Esize only
1595 if Is_Elementary_Type (Etyp) then
1596 if Size /= System_Storage_Unit
1598 Size /= System_Storage_Unit * 2
1600 Size /= System_Storage_Unit * 4
1602 Size /= System_Storage_Unit * 8
1604 Error_Msg_Uint_1 := UI_From_Int (System_Storage_Unit);
1605 Error_Msg_Uint_2 := Error_Msg_Uint_1 * 8;
1607 ("size for primitive object must be a power of 2"
1608 & " in the range ^-^", N);
1612 Set_Esize (U_Ent, Size);
1615 Set_Has_Size_Clause (U_Ent);
1623 -- Small attribute definition clause
1625 when Attribute_Small => Small : declare
1626 Implicit_Base : constant Entity_Id := Base_Type (U_Ent);
1630 Analyze_And_Resolve (Expr, Any_Real);
1632 if Etype (Expr) = Any_Type then
1635 elsif not Is_Static_Expression (Expr) then
1636 Flag_Non_Static_Expr
1637 ("small requires static expression!", Expr);
1641 Small := Expr_Value_R (Expr);
1643 if Small <= Ureal_0 then
1644 Error_Msg_N ("small value must be greater than zero", Expr);
1650 if not Is_Ordinary_Fixed_Point_Type (U_Ent) then
1652 ("small requires an ordinary fixed point type", Nam);
1654 elsif Has_Small_Clause (U_Ent) then
1655 Error_Msg_N ("small already given for &", Nam);
1657 elsif Small > Delta_Value (U_Ent) then
1659 ("small value must not be greater then delta value", Nam);
1662 Set_Small_Value (U_Ent, Small);
1663 Set_Small_Value (Implicit_Base, Small);
1664 Set_Has_Small_Clause (U_Ent);
1665 Set_Has_Small_Clause (Implicit_Base);
1666 Set_Has_Non_Standard_Rep (Implicit_Base);
1674 -- Storage_Pool attribute definition clause
1676 when Attribute_Storage_Pool => Storage_Pool : declare
1681 if Ekind (U_Ent) = E_Access_Subprogram_Type then
1683 ("storage pool cannot be given for access-to-subprogram type",
1688 Ekind_In (U_Ent, E_Access_Type, E_General_Access_Type)
1691 ("storage pool can only be given for access types", Nam);
1694 elsif Is_Derived_Type (U_Ent) then
1696 ("storage pool cannot be given for a derived access type",
1699 elsif Has_Storage_Size_Clause (U_Ent) then
1700 Error_Msg_N ("storage size already given for &", Nam);
1703 elsif Present (Associated_Storage_Pool (U_Ent)) then
1704 Error_Msg_N ("storage pool already given for &", Nam);
1709 (Expr, Class_Wide_Type (RTE (RE_Root_Storage_Pool)));
1711 if not Denotes_Variable (Expr) then
1712 Error_Msg_N ("storage pool must be a variable", Expr);
1716 if Nkind (Expr) = N_Type_Conversion then
1717 T := Etype (Expression (Expr));
1722 -- The Stack_Bounded_Pool is used internally for implementing
1723 -- access types with a Storage_Size. Since it only work
1724 -- properly when used on one specific type, we need to check
1725 -- that it is not hijacked improperly:
1726 -- type T is access Integer;
1727 -- for T'Storage_Size use n;
1728 -- type Q is access Float;
1729 -- for Q'Storage_Size use T'Storage_Size; -- incorrect
1731 if RTE_Available (RE_Stack_Bounded_Pool)
1732 and then Base_Type (T) = RTE (RE_Stack_Bounded_Pool)
1734 Error_Msg_N ("non-shareable internal Pool", Expr);
1738 -- If the argument is a name that is not an entity name, then
1739 -- we construct a renaming operation to define an entity of
1740 -- type storage pool.
1742 if not Is_Entity_Name (Expr)
1743 and then Is_Object_Reference (Expr)
1745 Pool := Make_Temporary (Loc, 'P', Expr);
1748 Rnode : constant Node_Id :=
1749 Make_Object_Renaming_Declaration (Loc,
1750 Defining_Identifier => Pool,
1752 New_Occurrence_Of (Etype (Expr), Loc),
1756 Insert_Before (N, Rnode);
1758 Set_Associated_Storage_Pool (U_Ent, Pool);
1761 elsif Is_Entity_Name (Expr) then
1762 Pool := Entity (Expr);
1764 -- If pool is a renamed object, get original one. This can
1765 -- happen with an explicit renaming, and within instances.
1767 while Present (Renamed_Object (Pool))
1768 and then Is_Entity_Name (Renamed_Object (Pool))
1770 Pool := Entity (Renamed_Object (Pool));
1773 if Present (Renamed_Object (Pool))
1774 and then Nkind (Renamed_Object (Pool)) = N_Type_Conversion
1775 and then Is_Entity_Name (Expression (Renamed_Object (Pool)))
1777 Pool := Entity (Expression (Renamed_Object (Pool)));
1780 Set_Associated_Storage_Pool (U_Ent, Pool);
1782 elsif Nkind (Expr) = N_Type_Conversion
1783 and then Is_Entity_Name (Expression (Expr))
1784 and then Nkind (Original_Node (Expr)) = N_Attribute_Reference
1786 Pool := Entity (Expression (Expr));
1787 Set_Associated_Storage_Pool (U_Ent, Pool);
1790 Error_Msg_N ("incorrect reference to a Storage Pool", Expr);
1799 -- Storage_Size attribute definition clause
1801 when Attribute_Storage_Size => Storage_Size : declare
1802 Btype : constant Entity_Id := Base_Type (U_Ent);
1806 if Is_Task_Type (U_Ent) then
1807 Check_Restriction (No_Obsolescent_Features, N);
1809 if Warn_On_Obsolescent_Feature then
1811 ("storage size clause for task is an " &
1812 "obsolescent feature (RM J.9)?", N);
1813 Error_Msg_N ("\use Storage_Size pragma instead?", N);
1819 if not Is_Access_Type (U_Ent)
1820 and then Ekind (U_Ent) /= E_Task_Type
1822 Error_Msg_N ("storage size cannot be given for &", Nam);
1824 elsif Is_Access_Type (U_Ent) and Is_Derived_Type (U_Ent) then
1826 ("storage size cannot be given for a derived access type",
1829 elsif Has_Storage_Size_Clause (Btype) then
1830 Error_Msg_N ("storage size already given for &", Nam);
1833 Analyze_And_Resolve (Expr, Any_Integer);
1835 if Is_Access_Type (U_Ent) then
1836 if Present (Associated_Storage_Pool (U_Ent)) then
1837 Error_Msg_N ("storage pool already given for &", Nam);
1841 if Compile_Time_Known_Value (Expr)
1842 and then Expr_Value (Expr) = 0
1844 Set_No_Pool_Assigned (Btype);
1847 else -- Is_Task_Type (U_Ent)
1848 Sprag := Get_Rep_Pragma (Btype, Name_Storage_Size);
1850 if Present (Sprag) then
1851 Error_Msg_Sloc := Sloc (Sprag);
1853 ("Storage_Size already specified#", Nam);
1858 Set_Has_Storage_Size_Clause (Btype);
1866 when Attribute_Stream_Size => Stream_Size : declare
1867 Size : constant Uint := Static_Integer (Expr);
1870 if Ada_Version <= Ada_95 then
1871 Check_Restriction (No_Implementation_Attributes, N);
1874 if Has_Stream_Size_Clause (U_Ent) then
1875 Error_Msg_N ("Stream_Size already given for &", Nam);
1877 elsif Is_Elementary_Type (U_Ent) then
1878 if Size /= System_Storage_Unit
1880 Size /= System_Storage_Unit * 2
1882 Size /= System_Storage_Unit * 4
1884 Size /= System_Storage_Unit * 8
1886 Error_Msg_Uint_1 := UI_From_Int (System_Storage_Unit);
1888 ("stream size for elementary type must be a"
1889 & " power of 2 and at least ^", N);
1891 elsif RM_Size (U_Ent) > Size then
1892 Error_Msg_Uint_1 := RM_Size (U_Ent);
1894 ("stream size for elementary type must be a"
1895 & " power of 2 and at least ^", N);
1898 Set_Has_Stream_Size_Clause (U_Ent);
1901 Error_Msg_N ("Stream_Size cannot be given for &", Nam);
1909 -- Value_Size attribute definition clause
1911 when Attribute_Value_Size => Value_Size : declare
1912 Size : constant Uint := Static_Integer (Expr);
1916 if not Is_Type (U_Ent) then
1917 Error_Msg_N ("Value_Size cannot be given for &", Nam);
1920 (Get_Attribute_Definition_Clause
1921 (U_Ent, Attribute_Value_Size))
1923 Error_Msg_N ("Value_Size already given for &", Nam);
1925 elsif Is_Array_Type (U_Ent)
1926 and then not Is_Constrained (U_Ent)
1929 ("Value_Size cannot be given for unconstrained array", Nam);
1932 if Is_Elementary_Type (U_Ent) then
1933 Check_Size (Expr, U_Ent, Size, Biased);
1934 Set_Has_Biased_Representation (U_Ent, Biased);
1936 if Biased and Warn_On_Biased_Representation then
1938 ("?value size clause forces biased representation", N);
1942 Set_RM_Size (U_Ent, Size);
1950 when Attribute_Write =>
1951 Analyze_Stream_TSS_Definition (TSS_Stream_Write);
1952 Set_Has_Specified_Stream_Write (Ent);
1954 -- All other attributes cannot be set
1958 ("attribute& cannot be set with definition clause", N);
1961 -- The test for the type being frozen must be performed after
1962 -- any expression the clause has been analyzed since the expression
1963 -- itself might cause freezing that makes the clause illegal.
1965 if Rep_Item_Too_Late (U_Ent, N, FOnly) then
1968 end Analyze_Attribute_Definition_Clause;
1970 ----------------------------
1971 -- Analyze_Code_Statement --
1972 ----------------------------
1974 procedure Analyze_Code_Statement (N : Node_Id) is
1975 HSS : constant Node_Id := Parent (N);
1976 SBody : constant Node_Id := Parent (HSS);
1977 Subp : constant Entity_Id := Current_Scope;
1984 -- Analyze and check we get right type, note that this implements the
1985 -- requirement (RM 13.8(1)) that Machine_Code be with'ed, since that
1986 -- is the only way that Asm_Insn could possibly be visible.
1988 Analyze_And_Resolve (Expression (N));
1990 if Etype (Expression (N)) = Any_Type then
1992 elsif Etype (Expression (N)) /= RTE (RE_Asm_Insn) then
1993 Error_Msg_N ("incorrect type for code statement", N);
1997 Check_Code_Statement (N);
1999 -- Make sure we appear in the handled statement sequence of a
2000 -- subprogram (RM 13.8(3)).
2002 if Nkind (HSS) /= N_Handled_Sequence_Of_Statements
2003 or else Nkind (SBody) /= N_Subprogram_Body
2006 ("code statement can only appear in body of subprogram", N);
2010 -- Do remaining checks (RM 13.8(3)) if not already done
2012 if not Is_Machine_Code_Subprogram (Subp) then
2013 Set_Is_Machine_Code_Subprogram (Subp);
2015 -- No exception handlers allowed
2017 if Present (Exception_Handlers (HSS)) then
2019 ("exception handlers not permitted in machine code subprogram",
2020 First (Exception_Handlers (HSS)));
2023 -- No declarations other than use clauses and pragmas (we allow
2024 -- certain internally generated declarations as well).
2026 Decl := First (Declarations (SBody));
2027 while Present (Decl) loop
2028 DeclO := Original_Node (Decl);
2029 if Comes_From_Source (DeclO)
2030 and not Nkind_In (DeclO, N_Pragma,
2031 N_Use_Package_Clause,
2033 N_Implicit_Label_Declaration)
2036 ("this declaration not allowed in machine code subprogram",
2043 -- No statements other than code statements, pragmas, and labels.
2044 -- Again we allow certain internally generated statements.
2046 Stmt := First (Statements (HSS));
2047 while Present (Stmt) loop
2048 StmtO := Original_Node (Stmt);
2049 if Comes_From_Source (StmtO)
2050 and then not Nkind_In (StmtO, N_Pragma,
2055 ("this statement is not allowed in machine code subprogram",
2062 end Analyze_Code_Statement;
2064 -----------------------------------------------
2065 -- Analyze_Enumeration_Representation_Clause --
2066 -----------------------------------------------
2068 procedure Analyze_Enumeration_Representation_Clause (N : Node_Id) is
2069 Ident : constant Node_Id := Identifier (N);
2070 Aggr : constant Node_Id := Array_Aggregate (N);
2071 Enumtype : Entity_Id;
2077 Err : Boolean := False;
2079 Lo : constant Uint := Expr_Value (Type_Low_Bound (Universal_Integer));
2080 Hi : constant Uint := Expr_Value (Type_High_Bound (Universal_Integer));
2085 if Ignore_Rep_Clauses then
2089 -- First some basic error checks
2092 Enumtype := Entity (Ident);
2094 if Enumtype = Any_Type
2095 or else Rep_Item_Too_Early (Enumtype, N)
2099 Enumtype := Underlying_Type (Enumtype);
2102 if not Is_Enumeration_Type (Enumtype) then
2104 ("enumeration type required, found}",
2105 Ident, First_Subtype (Enumtype));
2109 -- Ignore rep clause on generic actual type. This will already have
2110 -- been flagged on the template as an error, and this is the safest
2111 -- way to ensure we don't get a junk cascaded message in the instance.
2113 if Is_Generic_Actual_Type (Enumtype) then
2116 -- Type must be in current scope
2118 elsif Scope (Enumtype) /= Current_Scope then
2119 Error_Msg_N ("type must be declared in this scope", Ident);
2122 -- Type must be a first subtype
2124 elsif not Is_First_Subtype (Enumtype) then
2125 Error_Msg_N ("cannot give enumeration rep clause for subtype", N);
2128 -- Ignore duplicate rep clause
2130 elsif Has_Enumeration_Rep_Clause (Enumtype) then
2131 Error_Msg_N ("duplicate enumeration rep clause ignored", N);
2134 -- Don't allow rep clause for standard [wide_[wide_]]character
2136 elsif Is_Standard_Character_Type (Enumtype) then
2137 Error_Msg_N ("enumeration rep clause not allowed for this type", N);
2140 -- Check that the expression is a proper aggregate (no parentheses)
2142 elsif Paren_Count (Aggr) /= 0 then
2144 ("extra parentheses surrounding aggregate not allowed",
2148 -- All tests passed, so set rep clause in place
2151 Set_Has_Enumeration_Rep_Clause (Enumtype);
2152 Set_Has_Enumeration_Rep_Clause (Base_Type (Enumtype));
2155 -- Now we process the aggregate. Note that we don't use the normal
2156 -- aggregate code for this purpose, because we don't want any of the
2157 -- normal expansion activities, and a number of special semantic
2158 -- rules apply (including the component type being any integer type)
2160 Elit := First_Literal (Enumtype);
2162 -- First the positional entries if any
2164 if Present (Expressions (Aggr)) then
2165 Expr := First (Expressions (Aggr));
2166 while Present (Expr) loop
2168 Error_Msg_N ("too many entries in aggregate", Expr);
2172 Val := Static_Integer (Expr);
2174 -- Err signals that we found some incorrect entries processing
2175 -- the list. The final checks for completeness and ordering are
2176 -- skipped in this case.
2178 if Val = No_Uint then
2180 elsif Val < Lo or else Hi < Val then
2181 Error_Msg_N ("value outside permitted range", Expr);
2185 Set_Enumeration_Rep (Elit, Val);
2186 Set_Enumeration_Rep_Expr (Elit, Expr);
2192 -- Now process the named entries if present
2194 if Present (Component_Associations (Aggr)) then
2195 Assoc := First (Component_Associations (Aggr));
2196 while Present (Assoc) loop
2197 Choice := First (Choices (Assoc));
2199 if Present (Next (Choice)) then
2201 ("multiple choice not allowed here", Next (Choice));
2205 if Nkind (Choice) = N_Others_Choice then
2206 Error_Msg_N ("others choice not allowed here", Choice);
2209 elsif Nkind (Choice) = N_Range then
2210 -- ??? should allow zero/one element range here
2211 Error_Msg_N ("range not allowed here", Choice);
2215 Analyze_And_Resolve (Choice, Enumtype);
2217 if Is_Entity_Name (Choice)
2218 and then Is_Type (Entity (Choice))
2220 Error_Msg_N ("subtype name not allowed here", Choice);
2222 -- ??? should allow static subtype with zero/one entry
2224 elsif Etype (Choice) = Base_Type (Enumtype) then
2225 if not Is_Static_Expression (Choice) then
2226 Flag_Non_Static_Expr
2227 ("non-static expression used for choice!", Choice);
2231 Elit := Expr_Value_E (Choice);
2233 if Present (Enumeration_Rep_Expr (Elit)) then
2234 Error_Msg_Sloc := Sloc (Enumeration_Rep_Expr (Elit));
2236 ("representation for& previously given#",
2241 Set_Enumeration_Rep_Expr (Elit, Choice);
2243 Expr := Expression (Assoc);
2244 Val := Static_Integer (Expr);
2246 if Val = No_Uint then
2249 elsif Val < Lo or else Hi < Val then
2250 Error_Msg_N ("value outside permitted range", Expr);
2254 Set_Enumeration_Rep (Elit, Val);
2263 -- Aggregate is fully processed. Now we check that a full set of
2264 -- representations was given, and that they are in range and in order.
2265 -- These checks are only done if no other errors occurred.
2271 Elit := First_Literal (Enumtype);
2272 while Present (Elit) loop
2273 if No (Enumeration_Rep_Expr (Elit)) then
2274 Error_Msg_NE ("missing representation for&!", N, Elit);
2277 Val := Enumeration_Rep (Elit);
2279 if Min = No_Uint then
2283 if Val /= No_Uint then
2284 if Max /= No_Uint and then Val <= Max then
2286 ("enumeration value for& not ordered!",
2287 Enumeration_Rep_Expr (Elit), Elit);
2293 -- If there is at least one literal whose representation
2294 -- is not equal to the Pos value, then note that this
2295 -- enumeration type has a non-standard representation.
2297 if Val /= Enumeration_Pos (Elit) then
2298 Set_Has_Non_Standard_Rep (Base_Type (Enumtype));
2305 -- Now set proper size information
2308 Minsize : Uint := UI_From_Int (Minimum_Size (Enumtype));
2311 if Has_Size_Clause (Enumtype) then
2312 if Esize (Enumtype) >= Minsize then
2317 UI_From_Int (Minimum_Size (Enumtype, Biased => True));
2319 if Esize (Enumtype) < Minsize then
2320 Error_Msg_N ("previously given size is too small", N);
2323 Set_Has_Biased_Representation (Enumtype);
2328 Set_RM_Size (Enumtype, Minsize);
2329 Set_Enum_Esize (Enumtype);
2332 Set_RM_Size (Base_Type (Enumtype), RM_Size (Enumtype));
2333 Set_Esize (Base_Type (Enumtype), Esize (Enumtype));
2334 Set_Alignment (Base_Type (Enumtype), Alignment (Enumtype));
2338 -- We repeat the too late test in case it froze itself!
2340 if Rep_Item_Too_Late (Enumtype, N) then
2343 end Analyze_Enumeration_Representation_Clause;
2345 ----------------------------
2346 -- Analyze_Free_Statement --
2347 ----------------------------
2349 procedure Analyze_Free_Statement (N : Node_Id) is
2351 Analyze (Expression (N));
2352 end Analyze_Free_Statement;
2354 ---------------------------
2355 -- Analyze_Freeze_Entity --
2356 ---------------------------
2358 procedure Analyze_Freeze_Entity (N : Node_Id) is
2359 E : constant Entity_Id := Entity (N);
2362 -- For tagged types covering interfaces add internal entities that link
2363 -- the primitives of the interfaces with the primitives that cover them.
2365 -- Note: These entities were originally generated only when generating
2366 -- code because their main purpose was to provide support to initialize
2367 -- the secondary dispatch tables. They are now generated also when
2368 -- compiling with no code generation to provide ASIS the relationship
2369 -- between interface primitives and tagged type primitives.
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 Add_Internal_Interface_Entities (E);
2379 end Analyze_Freeze_Entity;
2381 ------------------------------------------
2382 -- Analyze_Record_Representation_Clause --
2383 ------------------------------------------
2385 -- Note: we check as much as we can here, but we can't do any checks
2386 -- based on the position values (e.g. overlap checks) until freeze time
2387 -- because especially in Ada 2005 (machine scalar mode), the processing
2388 -- for non-standard bit order can substantially change the positions.
2389 -- See procedure Check_Record_Representation_Clause (called from Freeze)
2390 -- for the remainder of this processing.
2392 procedure Analyze_Record_Representation_Clause (N : Node_Id) is
2393 Ident : constant Node_Id := Identifier (N);
2394 Rectype : Entity_Id;
2399 Hbit : Uint := Uint_0;
2404 CR_Pragma : Node_Id := Empty;
2405 -- Points to N_Pragma node if Complete_Representation pragma present
2408 if Ignore_Rep_Clauses then
2413 Rectype := Entity (Ident);
2415 if Rectype = Any_Type
2416 or else Rep_Item_Too_Early (Rectype, N)
2420 Rectype := Underlying_Type (Rectype);
2423 -- First some basic error checks
2425 if not Is_Record_Type (Rectype) then
2427 ("record type required, found}", Ident, First_Subtype (Rectype));
2430 elsif Is_Unchecked_Union (Rectype) then
2432 ("record rep clause not allowed for Unchecked_Union", N);
2434 elsif Scope (Rectype) /= Current_Scope then
2435 Error_Msg_N ("type must be declared in this scope", N);
2438 elsif not Is_First_Subtype (Rectype) then
2439 Error_Msg_N ("cannot give record rep clause for subtype", N);
2442 elsif Has_Record_Rep_Clause (Rectype) then
2443 Error_Msg_N ("duplicate record rep clause ignored", N);
2446 elsif Rep_Item_Too_Late (Rectype, N) then
2450 if Present (Mod_Clause (N)) then
2452 Loc : constant Source_Ptr := Sloc (N);
2453 M : constant Node_Id := Mod_Clause (N);
2454 P : constant List_Id := Pragmas_Before (M);
2458 pragma Warnings (Off, Mod_Val);
2461 Check_Restriction (No_Obsolescent_Features, Mod_Clause (N));
2463 if Warn_On_Obsolescent_Feature then
2465 ("mod clause is an obsolescent feature (RM J.8)?", N);
2467 ("\use alignment attribute definition clause instead?", N);
2474 -- In ASIS_Mode mode, expansion is disabled, but we must convert
2475 -- the Mod clause into an alignment clause anyway, so that the
2476 -- back-end can compute and back-annotate properly the size and
2477 -- alignment of types that may include this record.
2479 -- This seems dubious, this destroys the source tree in a manner
2480 -- not detectable by ASIS ???
2482 if Operating_Mode = Check_Semantics
2486 Make_Attribute_Definition_Clause (Loc,
2487 Name => New_Reference_To (Base_Type (Rectype), Loc),
2488 Chars => Name_Alignment,
2489 Expression => Relocate_Node (Expression (M)));
2491 Set_From_At_Mod (AtM_Nod);
2492 Insert_After (N, AtM_Nod);
2493 Mod_Val := Get_Alignment_Value (Expression (AtM_Nod));
2494 Set_Mod_Clause (N, Empty);
2497 -- Get the alignment value to perform error checking
2499 Mod_Val := Get_Alignment_Value (Expression (M));
2504 -- For untagged types, clear any existing component clauses for the
2505 -- type. If the type is derived, this is what allows us to override
2506 -- a rep clause for the parent. For type extensions, the representation
2507 -- of the inherited components is inherited, so we want to keep previous
2508 -- component clauses for completeness.
2510 if not Is_Tagged_Type (Rectype) then
2511 Comp := First_Component_Or_Discriminant (Rectype);
2512 while Present (Comp) loop
2513 Set_Component_Clause (Comp, Empty);
2514 Next_Component_Or_Discriminant (Comp);
2518 -- All done if no component clauses
2520 CC := First (Component_Clauses (N));
2526 -- A representation like this applies to the base type
2528 Set_Has_Record_Rep_Clause (Base_Type (Rectype));
2529 Set_Has_Non_Standard_Rep (Base_Type (Rectype));
2530 Set_Has_Specified_Layout (Base_Type (Rectype));
2532 -- Process the component clauses
2534 while Present (CC) loop
2538 if Nkind (CC) = N_Pragma then
2541 -- The only pragma of interest is Complete_Representation
2543 if Pragma_Name (CC) = Name_Complete_Representation then
2547 -- Processing for real component clause
2550 Posit := Static_Integer (Position (CC));
2551 Fbit := Static_Integer (First_Bit (CC));
2552 Lbit := Static_Integer (Last_Bit (CC));
2555 and then Fbit /= No_Uint
2556 and then Lbit /= No_Uint
2560 ("position cannot be negative", Position (CC));
2564 ("first bit cannot be negative", First_Bit (CC));
2566 -- The Last_Bit specified in a component clause must not be
2567 -- less than the First_Bit minus one (RM-13.5.1(10)).
2569 elsif Lbit < Fbit - 1 then
2571 ("last bit cannot be less than first bit minus one",
2574 -- Values look OK, so find the corresponding record component
2575 -- Even though the syntax allows an attribute reference for
2576 -- implementation-defined components, GNAT does not allow the
2577 -- tag to get an explicit position.
2579 elsif Nkind (Component_Name (CC)) = N_Attribute_Reference then
2580 if Attribute_Name (Component_Name (CC)) = Name_Tag then
2581 Error_Msg_N ("position of tag cannot be specified", CC);
2583 Error_Msg_N ("illegal component name", CC);
2587 Comp := First_Entity (Rectype);
2588 while Present (Comp) loop
2589 exit when Chars (Comp) = Chars (Component_Name (CC));
2595 -- Maybe component of base type that is absent from
2596 -- statically constrained first subtype.
2598 Comp := First_Entity (Base_Type (Rectype));
2599 while Present (Comp) loop
2600 exit when Chars (Comp) = Chars (Component_Name (CC));
2607 ("component clause is for non-existent field", CC);
2609 elsif Present (Component_Clause (Comp)) then
2611 -- Diagnose duplicate rep clause, or check consistency
2612 -- if this is an inherited component. In a double fault,
2613 -- there may be a duplicate inconsistent clause for an
2614 -- inherited component.
2616 if Scope (Original_Record_Component (Comp)) = Rectype
2617 or else Parent (Component_Clause (Comp)) = N
2619 Error_Msg_Sloc := Sloc (Component_Clause (Comp));
2620 Error_Msg_N ("component clause previously given#", CC);
2624 Rep1 : constant Node_Id := Component_Clause (Comp);
2626 if Intval (Position (Rep1)) /=
2627 Intval (Position (CC))
2628 or else Intval (First_Bit (Rep1)) /=
2629 Intval (First_Bit (CC))
2630 or else Intval (Last_Bit (Rep1)) /=
2631 Intval (Last_Bit (CC))
2633 Error_Msg_N ("component clause inconsistent "
2634 & "with representation of ancestor", CC);
2635 elsif Warn_On_Redundant_Constructs then
2636 Error_Msg_N ("?redundant component clause "
2637 & "for inherited component!", CC);
2642 -- Normal case where this is the first component clause we
2643 -- have seen for this entity, so set it up properly.
2646 -- Make reference for field in record rep clause and set
2647 -- appropriate entity field in the field identifier.
2650 (Comp, Component_Name (CC), Set_Ref => False);
2651 Set_Entity (Component_Name (CC), Comp);
2653 -- Update Fbit and Lbit to the actual bit number
2655 Fbit := Fbit + UI_From_Int (SSU) * Posit;
2656 Lbit := Lbit + UI_From_Int (SSU) * Posit;
2658 if Has_Size_Clause (Rectype)
2659 and then Esize (Rectype) <= Lbit
2662 ("bit number out of range of specified size",
2665 Set_Component_Clause (Comp, CC);
2666 Set_Component_Bit_Offset (Comp, Fbit);
2667 Set_Esize (Comp, 1 + (Lbit - Fbit));
2668 Set_Normalized_First_Bit (Comp, Fbit mod SSU);
2669 Set_Normalized_Position (Comp, Fbit / SSU);
2671 -- This information is also set in the corresponding
2672 -- component of the base type, found by accessing the
2673 -- Original_Record_Component link if it is present.
2675 Ocomp := Original_Record_Component (Comp);
2682 (Component_Name (CC),
2687 Set_Has_Biased_Representation (Comp, Biased);
2689 if Biased and Warn_On_Biased_Representation then
2691 ("?component clause forces biased "
2692 & "representation", CC);
2695 if Present (Ocomp) then
2696 Set_Component_Clause (Ocomp, CC);
2697 Set_Component_Bit_Offset (Ocomp, Fbit);
2698 Set_Normalized_First_Bit (Ocomp, Fbit mod SSU);
2699 Set_Normalized_Position (Ocomp, Fbit / SSU);
2700 Set_Esize (Ocomp, 1 + (Lbit - Fbit));
2702 Set_Normalized_Position_Max
2703 (Ocomp, Normalized_Position (Ocomp));
2705 Set_Has_Biased_Representation
2706 (Ocomp, Has_Biased_Representation (Comp));
2709 if Esize (Comp) < 0 then
2710 Error_Msg_N ("component size is negative", CC);
2721 -- Check missing components if Complete_Representation pragma appeared
2723 if Present (CR_Pragma) then
2724 Comp := First_Component_Or_Discriminant (Rectype);
2725 while Present (Comp) loop
2726 if No (Component_Clause (Comp)) then
2728 ("missing component clause for &", CR_Pragma, Comp);
2731 Next_Component_Or_Discriminant (Comp);
2734 -- If no Complete_Representation pragma, warn if missing components
2736 elsif Warn_On_Unrepped_Components then
2738 Num_Repped_Components : Nat := 0;
2739 Num_Unrepped_Components : Nat := 0;
2742 -- First count number of repped and unrepped components
2744 Comp := First_Component_Or_Discriminant (Rectype);
2745 while Present (Comp) loop
2746 if Present (Component_Clause (Comp)) then
2747 Num_Repped_Components := Num_Repped_Components + 1;
2749 Num_Unrepped_Components := Num_Unrepped_Components + 1;
2752 Next_Component_Or_Discriminant (Comp);
2755 -- We are only interested in the case where there is at least one
2756 -- unrepped component, and at least half the components have rep
2757 -- clauses. We figure that if less than half have them, then the
2758 -- partial rep clause is really intentional. If the component
2759 -- type has no underlying type set at this point (as for a generic
2760 -- formal type), we don't know enough to give a warning on the
2763 if Num_Unrepped_Components > 0
2764 and then Num_Unrepped_Components < Num_Repped_Components
2766 Comp := First_Component_Or_Discriminant (Rectype);
2767 while Present (Comp) loop
2768 if No (Component_Clause (Comp))
2769 and then Comes_From_Source (Comp)
2770 and then Present (Underlying_Type (Etype (Comp)))
2771 and then (Is_Scalar_Type (Underlying_Type (Etype (Comp)))
2772 or else Size_Known_At_Compile_Time
2773 (Underlying_Type (Etype (Comp))))
2774 and then not Has_Warnings_Off (Rectype)
2776 Error_Msg_Sloc := Sloc (Comp);
2778 ("?no component clause given for & declared #",
2782 Next_Component_Or_Discriminant (Comp);
2787 end Analyze_Record_Representation_Clause;
2789 -----------------------------------
2790 -- Check_Constant_Address_Clause --
2791 -----------------------------------
2793 procedure Check_Constant_Address_Clause
2797 procedure Check_At_Constant_Address (Nod : Node_Id);
2798 -- Checks that the given node N represents a name whose 'Address is
2799 -- constant (in the same sense as OK_Constant_Address_Clause, i.e. the
2800 -- address value is the same at the point of declaration of U_Ent and at
2801 -- the time of elaboration of the address clause.
2803 procedure Check_Expr_Constants (Nod : Node_Id);
2804 -- Checks that Nod meets the requirements for a constant address clause
2805 -- in the sense of the enclosing procedure.
2807 procedure Check_List_Constants (Lst : List_Id);
2808 -- Check that all elements of list Lst meet the requirements for a
2809 -- constant address clause in the sense of the enclosing procedure.
2811 -------------------------------
2812 -- Check_At_Constant_Address --
2813 -------------------------------
2815 procedure Check_At_Constant_Address (Nod : Node_Id) is
2817 if Is_Entity_Name (Nod) then
2818 if Present (Address_Clause (Entity ((Nod)))) then
2820 ("invalid address clause for initialized object &!",
2823 ("address for& cannot" &
2824 " depend on another address clause! (RM 13.1(22))!",
2827 elsif In_Same_Source_Unit (Entity (Nod), U_Ent)
2828 and then Sloc (U_Ent) < Sloc (Entity (Nod))
2831 ("invalid address clause for initialized object &!",
2833 Error_Msg_Node_2 := U_Ent;
2835 ("\& must be defined before & (RM 13.1(22))!",
2839 elsif Nkind (Nod) = N_Selected_Component then
2841 T : constant Entity_Id := Etype (Prefix (Nod));
2844 if (Is_Record_Type (T)
2845 and then Has_Discriminants (T))
2848 and then Is_Record_Type (Designated_Type (T))
2849 and then Has_Discriminants (Designated_Type (T)))
2852 ("invalid address clause for initialized object &!",
2855 ("\address cannot depend on component" &
2856 " of discriminated record (RM 13.1(22))!",
2859 Check_At_Constant_Address (Prefix (Nod));
2863 elsif Nkind (Nod) = N_Indexed_Component then
2864 Check_At_Constant_Address (Prefix (Nod));
2865 Check_List_Constants (Expressions (Nod));
2868 Check_Expr_Constants (Nod);
2870 end Check_At_Constant_Address;
2872 --------------------------
2873 -- Check_Expr_Constants --
2874 --------------------------
2876 procedure Check_Expr_Constants (Nod : Node_Id) is
2877 Loc_U_Ent : constant Source_Ptr := Sloc (U_Ent);
2878 Ent : Entity_Id := Empty;
2881 if Nkind (Nod) in N_Has_Etype
2882 and then Etype (Nod) = Any_Type
2888 when N_Empty | N_Error =>
2891 when N_Identifier | N_Expanded_Name =>
2892 Ent := Entity (Nod);
2894 -- We need to look at the original node if it is different
2895 -- from the node, since we may have rewritten things and
2896 -- substituted an identifier representing the rewrite.
2898 if Original_Node (Nod) /= Nod then
2899 Check_Expr_Constants (Original_Node (Nod));
2901 -- If the node is an object declaration without initial
2902 -- value, some code has been expanded, and the expression
2903 -- is not constant, even if the constituents might be
2904 -- acceptable, as in A'Address + offset.
2906 if Ekind (Ent) = E_Variable
2908 Nkind (Declaration_Node (Ent)) = N_Object_Declaration
2910 No (Expression (Declaration_Node (Ent)))
2913 ("invalid address clause for initialized object &!",
2916 -- If entity is constant, it may be the result of expanding
2917 -- a check. We must verify that its declaration appears
2918 -- before the object in question, else we also reject the
2921 elsif Ekind (Ent) = E_Constant
2922 and then In_Same_Source_Unit (Ent, U_Ent)
2923 and then Sloc (Ent) > Loc_U_Ent
2926 ("invalid address clause for initialized object &!",
2933 -- Otherwise look at the identifier and see if it is OK
2935 if Ekind_In (Ent, E_Named_Integer, E_Named_Real)
2936 or else Is_Type (Ent)
2941 Ekind (Ent) = E_Constant
2943 Ekind (Ent) = E_In_Parameter
2945 -- This is the case where we must have Ent defined before
2946 -- U_Ent. Clearly if they are in different units this
2947 -- requirement is met since the unit containing Ent is
2948 -- already processed.
2950 if not In_Same_Source_Unit (Ent, U_Ent) then
2953 -- Otherwise location of Ent must be before the location
2954 -- of U_Ent, that's what prior defined means.
2956 elsif Sloc (Ent) < Loc_U_Ent then
2961 ("invalid address clause for initialized object &!",
2963 Error_Msg_Node_2 := U_Ent;
2965 ("\& must be defined before & (RM 13.1(22))!",
2969 elsif Nkind (Original_Node (Nod)) = N_Function_Call then
2970 Check_Expr_Constants (Original_Node (Nod));
2974 ("invalid address clause for initialized object &!",
2977 if Comes_From_Source (Ent) then
2979 ("\reference to variable& not allowed"
2980 & " (RM 13.1(22))!", Nod, Ent);
2983 ("non-static expression not allowed"
2984 & " (RM 13.1(22))!", Nod);
2988 when N_Integer_Literal =>
2990 -- If this is a rewritten unchecked conversion, in a system
2991 -- where Address is an integer type, always use the base type
2992 -- for a literal value. This is user-friendly and prevents
2993 -- order-of-elaboration issues with instances of unchecked
2996 if Nkind (Original_Node (Nod)) = N_Function_Call then
2997 Set_Etype (Nod, Base_Type (Etype (Nod)));
3000 when N_Real_Literal |
3002 N_Character_Literal =>
3006 Check_Expr_Constants (Low_Bound (Nod));
3007 Check_Expr_Constants (High_Bound (Nod));
3009 when N_Explicit_Dereference =>
3010 Check_Expr_Constants (Prefix (Nod));
3012 when N_Indexed_Component =>
3013 Check_Expr_Constants (Prefix (Nod));
3014 Check_List_Constants (Expressions (Nod));
3017 Check_Expr_Constants (Prefix (Nod));
3018 Check_Expr_Constants (Discrete_Range (Nod));
3020 when N_Selected_Component =>
3021 Check_Expr_Constants (Prefix (Nod));
3023 when N_Attribute_Reference =>
3024 if Attribute_Name (Nod) = Name_Address
3026 Attribute_Name (Nod) = Name_Access
3028 Attribute_Name (Nod) = Name_Unchecked_Access
3030 Attribute_Name (Nod) = Name_Unrestricted_Access
3032 Check_At_Constant_Address (Prefix (Nod));
3035 Check_Expr_Constants (Prefix (Nod));
3036 Check_List_Constants (Expressions (Nod));
3040 Check_List_Constants (Component_Associations (Nod));
3041 Check_List_Constants (Expressions (Nod));
3043 when N_Component_Association =>
3044 Check_Expr_Constants (Expression (Nod));
3046 when N_Extension_Aggregate =>
3047 Check_Expr_Constants (Ancestor_Part (Nod));
3048 Check_List_Constants (Component_Associations (Nod));
3049 Check_List_Constants (Expressions (Nod));
3054 when N_Binary_Op | N_Short_Circuit | N_Membership_Test =>
3055 Check_Expr_Constants (Left_Opnd (Nod));
3056 Check_Expr_Constants (Right_Opnd (Nod));
3059 Check_Expr_Constants (Right_Opnd (Nod));
3061 when N_Type_Conversion |
3062 N_Qualified_Expression |
3064 Check_Expr_Constants (Expression (Nod));
3066 when N_Unchecked_Type_Conversion =>
3067 Check_Expr_Constants (Expression (Nod));
3069 -- If this is a rewritten unchecked conversion, subtypes in
3070 -- this node are those created within the instance. To avoid
3071 -- order of elaboration issues, replace them with their base
3072 -- types. Note that address clauses can cause order of
3073 -- elaboration problems because they are elaborated by the
3074 -- back-end at the point of definition, and may mention
3075 -- entities declared in between (as long as everything is
3076 -- static). It is user-friendly to allow unchecked conversions
3079 if Nkind (Original_Node (Nod)) = N_Function_Call then
3080 Set_Etype (Expression (Nod),
3081 Base_Type (Etype (Expression (Nod))));
3082 Set_Etype (Nod, Base_Type (Etype (Nod)));
3085 when N_Function_Call =>
3086 if not Is_Pure (Entity (Name (Nod))) then
3088 ("invalid address clause for initialized object &!",
3092 ("\function & is not pure (RM 13.1(22))!",
3093 Nod, Entity (Name (Nod)));
3096 Check_List_Constants (Parameter_Associations (Nod));
3099 when N_Parameter_Association =>
3100 Check_Expr_Constants (Explicit_Actual_Parameter (Nod));
3104 ("invalid address clause for initialized object &!",
3107 ("\must be constant defined before& (RM 13.1(22))!",
3110 end Check_Expr_Constants;
3112 --------------------------
3113 -- Check_List_Constants --
3114 --------------------------
3116 procedure Check_List_Constants (Lst : List_Id) is
3120 if Present (Lst) then
3121 Nod1 := First (Lst);
3122 while Present (Nod1) loop
3123 Check_Expr_Constants (Nod1);
3127 end Check_List_Constants;
3129 -- Start of processing for Check_Constant_Address_Clause
3132 Check_Expr_Constants (Expr);
3133 end Check_Constant_Address_Clause;
3135 ----------------------------------------
3136 -- Check_Record_Representation_Clause --
3137 ----------------------------------------
3139 procedure Check_Record_Representation_Clause (N : Node_Id) is
3140 Loc : constant Source_Ptr := Sloc (N);
3141 Ident : constant Node_Id := Identifier (N);
3142 Rectype : Entity_Id;
3147 Hbit : Uint := Uint_0;
3151 Max_Bit_So_Far : Uint;
3152 -- Records the maximum bit position so far. If all field positions
3153 -- are monotonically increasing, then we can skip the circuit for
3154 -- checking for overlap, since no overlap is possible.
3156 Tagged_Parent : Entity_Id := Empty;
3157 -- This is set in the case of a derived tagged type for which we have
3158 -- Is_Fully_Repped_Tagged_Type True (indicating that all components are
3159 -- positioned by record representation clauses). In this case we must
3160 -- check for overlap between components of this tagged type, and the
3161 -- components of its parent. Tagged_Parent will point to this parent
3162 -- type. For all other cases Tagged_Parent is left set to Empty.
3164 Parent_Last_Bit : Uint;
3165 -- Relevant only if Tagged_Parent is set, Parent_Last_Bit indicates the
3166 -- last bit position for any field in the parent type. We only need to
3167 -- check overlap for fields starting below this point.
3169 Overlap_Check_Required : Boolean;
3170 -- Used to keep track of whether or not an overlap check is required
3172 Ccount : Natural := 0;
3173 -- Number of component clauses in record rep clause
3175 procedure Check_Component_Overlap (C1_Ent, C2_Ent : Entity_Id);
3176 -- Given two entities for record components or discriminants, checks
3177 -- if they have overlapping component clauses and issues errors if so.
3179 procedure Find_Component;
3180 -- Finds component entity corresponding to current component clause (in
3181 -- CC), and sets Comp to the entity, and Fbit/Lbit to the zero origin
3182 -- start/stop bits for the field. If there is no matching component or
3183 -- if the matching component does not have a component clause, then
3184 -- that's an error and Comp is set to Empty, but no error message is
3185 -- issued, since the message was already given. Comp is also set to
3186 -- Empty if the current "component clause" is in fact a pragma.
3188 -----------------------------
3189 -- Check_Component_Overlap --
3190 -----------------------------
3192 procedure Check_Component_Overlap (C1_Ent, C2_Ent : Entity_Id) is
3193 CC1 : constant Node_Id := Component_Clause (C1_Ent);
3194 CC2 : constant Node_Id := Component_Clause (C2_Ent);
3196 if Present (CC1) and then Present (CC2) then
3198 -- Exclude odd case where we have two tag fields in the same
3199 -- record, both at location zero. This seems a bit strange, but
3200 -- it seems to happen in some circumstances, perhaps on an error.
3202 if Chars (C1_Ent) = Name_uTag
3204 Chars (C2_Ent) = Name_uTag
3209 -- Here we check if the two fields overlap
3212 S1 : constant Uint := Component_Bit_Offset (C1_Ent);
3213 S2 : constant Uint := Component_Bit_Offset (C2_Ent);
3214 E1 : constant Uint := S1 + Esize (C1_Ent);
3215 E2 : constant Uint := S2 + Esize (C2_Ent);
3218 if E2 <= S1 or else E1 <= S2 then
3221 Error_Msg_Node_2 := Component_Name (CC2);
3222 Error_Msg_Sloc := Sloc (Error_Msg_Node_2);
3223 Error_Msg_Node_1 := Component_Name (CC1);
3225 ("component& overlaps & #", Component_Name (CC1));
3229 end Check_Component_Overlap;
3231 --------------------
3232 -- Find_Component --
3233 --------------------
3235 procedure Find_Component is
3237 procedure Search_Component (R : Entity_Id);
3238 -- Search components of R for a match. If found, Comp is set.
3240 ----------------------
3241 -- Search_Component --
3242 ----------------------
3244 procedure Search_Component (R : Entity_Id) is
3246 Comp := First_Component_Or_Discriminant (R);
3247 while Present (Comp) loop
3249 -- Ignore error of attribute name for component name (we
3250 -- already gave an error message for this, so no need to
3253 if Nkind (Component_Name (CC)) = N_Attribute_Reference then
3256 exit when Chars (Comp) = Chars (Component_Name (CC));
3259 Next_Component_Or_Discriminant (Comp);
3261 end Search_Component;
3263 -- Start of processing for Find_Component
3266 -- Return with Comp set to Empty if we have a pragma
3268 if Nkind (CC) = N_Pragma then
3273 -- Search current record for matching component
3275 Search_Component (Rectype);
3277 -- If not found, maybe component of base type that is absent from
3278 -- statically constrained first subtype.
3281 Search_Component (Base_Type (Rectype));
3284 -- If no component, or the component does not reference the component
3285 -- clause in question, then there was some previous error for which
3286 -- we already gave a message, so just return with Comp Empty.
3289 or else Component_Clause (Comp) /= CC
3293 -- Normal case where we have a component clause
3296 Fbit := Component_Bit_Offset (Comp);
3297 Lbit := Fbit + Esize (Comp) - 1;
3301 -- Start of processing for Check_Record_Representation_Clause
3305 Rectype := Entity (Ident);
3307 if Rectype = Any_Type then
3310 Rectype := Underlying_Type (Rectype);
3313 -- See if we have a fully repped derived tagged type
3316 PS : constant Entity_Id := Parent_Subtype (Rectype);
3319 if Present (PS) and then Is_Fully_Repped_Tagged_Type (PS) then
3320 Tagged_Parent := PS;
3322 -- Find maximum bit of any component of the parent type
3324 Parent_Last_Bit := UI_From_Int (System_Address_Size - 1);
3325 Pcomp := First_Entity (Tagged_Parent);
3326 while Present (Pcomp) loop
3327 if Ekind_In (Pcomp, E_Discriminant, E_Component) then
3328 if Component_Bit_Offset (Pcomp) /= No_Uint
3329 and then Known_Static_Esize (Pcomp)
3334 Component_Bit_Offset (Pcomp) + Esize (Pcomp) - 1);
3337 Next_Entity (Pcomp);
3343 -- All done if no component clauses
3345 CC := First (Component_Clauses (N));
3351 -- If a tag is present, then create a component clause that places it
3352 -- at the start of the record (otherwise gigi may place it after other
3353 -- fields that have rep clauses).
3355 Fent := First_Entity (Rectype);
3357 if Nkind (Fent) = N_Defining_Identifier
3358 and then Chars (Fent) = Name_uTag
3360 Set_Component_Bit_Offset (Fent, Uint_0);
3361 Set_Normalized_Position (Fent, Uint_0);
3362 Set_Normalized_First_Bit (Fent, Uint_0);
3363 Set_Normalized_Position_Max (Fent, Uint_0);
3364 Init_Esize (Fent, System_Address_Size);
3366 Set_Component_Clause (Fent,
3367 Make_Component_Clause (Loc,
3369 Make_Identifier (Loc,
3370 Chars => Name_uTag),
3373 Make_Integer_Literal (Loc,
3377 Make_Integer_Literal (Loc,
3381 Make_Integer_Literal (Loc,
3382 UI_From_Int (System_Address_Size))));
3384 Ccount := Ccount + 1;
3387 Max_Bit_So_Far := Uint_Minus_1;
3388 Overlap_Check_Required := False;
3390 -- Process the component clauses
3392 while Present (CC) loop
3395 if Present (Comp) then
3396 Ccount := Ccount + 1;
3398 if Fbit <= Max_Bit_So_Far then
3399 Overlap_Check_Required := True;
3401 Max_Bit_So_Far := Lbit;
3404 -- Check bit position out of range of specified size
3406 if Has_Size_Clause (Rectype)
3407 and then Esize (Rectype) <= Lbit
3410 ("bit number out of range of specified size",
3413 -- Check for overlap with tag field
3416 if Is_Tagged_Type (Rectype)
3417 and then Fbit < System_Address_Size
3420 ("component overlaps tag field of&",
3421 Component_Name (CC), Rectype);
3429 -- Check parent overlap if component might overlap parent field
3431 if Present (Tagged_Parent)
3432 and then Fbit <= Parent_Last_Bit
3434 Pcomp := First_Component_Or_Discriminant (Tagged_Parent);
3435 while Present (Pcomp) loop
3436 if not Is_Tag (Pcomp)
3437 and then Chars (Pcomp) /= Name_uParent
3439 Check_Component_Overlap (Comp, Pcomp);
3442 Next_Component_Or_Discriminant (Pcomp);
3450 -- Now that we have processed all the component clauses, check for
3451 -- overlap. We have to leave this till last, since the components can
3452 -- appear in any arbitrary order in the representation clause.
3454 -- We do not need this check if all specified ranges were monotonic,
3455 -- as recorded by Overlap_Check_Required being False at this stage.
3457 -- This first section checks if there are any overlapping entries at
3458 -- all. It does this by sorting all entries and then seeing if there are
3459 -- any overlaps. If there are none, then that is decisive, but if there
3460 -- are overlaps, they may still be OK (they may result from fields in
3461 -- different variants).
3463 if Overlap_Check_Required then
3464 Overlap_Check1 : declare
3466 OC_Fbit : array (0 .. Ccount) of Uint;
3467 -- First-bit values for component clauses, the value is the offset
3468 -- of the first bit of the field from start of record. The zero
3469 -- entry is for use in sorting.
3471 OC_Lbit : array (0 .. Ccount) of Uint;
3472 -- Last-bit values for component clauses, the value is the offset
3473 -- of the last bit of the field from start of record. The zero
3474 -- entry is for use in sorting.
3476 OC_Count : Natural := 0;
3477 -- Count of entries in OC_Fbit and OC_Lbit
3479 function OC_Lt (Op1, Op2 : Natural) return Boolean;
3480 -- Compare routine for Sort
3482 procedure OC_Move (From : Natural; To : Natural);
3483 -- Move routine for Sort
3485 package Sorting is new GNAT.Heap_Sort_G (OC_Move, OC_Lt);
3491 function OC_Lt (Op1, Op2 : Natural) return Boolean is
3493 return OC_Fbit (Op1) < OC_Fbit (Op2);
3500 procedure OC_Move (From : Natural; To : Natural) is
3502 OC_Fbit (To) := OC_Fbit (From);
3503 OC_Lbit (To) := OC_Lbit (From);
3506 -- Start of processing for Overlap_Check
3509 CC := First (Component_Clauses (N));
3510 while Present (CC) loop
3512 -- Exclude component clause already marked in error
3514 if not Error_Posted (CC) then
3517 if Present (Comp) then
3518 OC_Count := OC_Count + 1;
3519 OC_Fbit (OC_Count) := Fbit;
3520 OC_Lbit (OC_Count) := Lbit;
3527 Sorting.Sort (OC_Count);
3529 Overlap_Check_Required := False;
3530 for J in 1 .. OC_Count - 1 loop
3531 if OC_Lbit (J) >= OC_Fbit (J + 1) then
3532 Overlap_Check_Required := True;
3539 -- If Overlap_Check_Required is still True, then we have to do the full
3540 -- scale overlap check, since we have at least two fields that do
3541 -- overlap, and we need to know if that is OK since they are in
3542 -- different variant, or whether we have a definite problem.
3544 if Overlap_Check_Required then
3545 Overlap_Check2 : declare
3546 C1_Ent, C2_Ent : Entity_Id;
3547 -- Entities of components being checked for overlap
3550 -- Component_List node whose Component_Items are being checked
3553 -- Component declaration for component being checked
3556 C1_Ent := First_Entity (Base_Type (Rectype));
3558 -- Loop through all components in record. For each component check
3559 -- for overlap with any of the preceding elements on the component
3560 -- list containing the component and also, if the component is in
3561 -- a variant, check against components outside the case structure.
3562 -- This latter test is repeated recursively up the variant tree.
3564 Main_Component_Loop : while Present (C1_Ent) loop
3565 if not Ekind_In (C1_Ent, E_Component, E_Discriminant) then
3566 goto Continue_Main_Component_Loop;
3569 -- Skip overlap check if entity has no declaration node. This
3570 -- happens with discriminants in constrained derived types.
3571 -- Probably we are missing some checks as a result, but that
3572 -- does not seem terribly serious ???
3574 if No (Declaration_Node (C1_Ent)) then
3575 goto Continue_Main_Component_Loop;
3578 Clist := Parent (List_Containing (Declaration_Node (C1_Ent)));
3580 -- Loop through component lists that need checking. Check the
3581 -- current component list and all lists in variants above us.
3583 Component_List_Loop : loop
3585 -- If derived type definition, go to full declaration
3586 -- If at outer level, check discriminants if there are any.
3588 if Nkind (Clist) = N_Derived_Type_Definition then
3589 Clist := Parent (Clist);
3592 -- Outer level of record definition, check discriminants
3594 if Nkind_In (Clist, N_Full_Type_Declaration,
3595 N_Private_Type_Declaration)
3597 if Has_Discriminants (Defining_Identifier (Clist)) then
3599 First_Discriminant (Defining_Identifier (Clist));
3600 while Present (C2_Ent) loop
3601 exit when C1_Ent = C2_Ent;
3602 Check_Component_Overlap (C1_Ent, C2_Ent);
3603 Next_Discriminant (C2_Ent);
3607 -- Record extension case
3609 elsif Nkind (Clist) = N_Derived_Type_Definition then
3612 -- Otherwise check one component list
3615 Citem := First (Component_Items (Clist));
3617 while Present (Citem) loop
3618 if Nkind (Citem) = N_Component_Declaration then
3619 C2_Ent := Defining_Identifier (Citem);
3620 exit when C1_Ent = C2_Ent;
3621 Check_Component_Overlap (C1_Ent, C2_Ent);
3628 -- Check for variants above us (the parent of the Clist can
3629 -- be a variant, in which case its parent is a variant part,
3630 -- and the parent of the variant part is a component list
3631 -- whose components must all be checked against the current
3632 -- component for overlap).
3634 if Nkind (Parent (Clist)) = N_Variant then
3635 Clist := Parent (Parent (Parent (Clist)));
3637 -- Check for possible discriminant part in record, this
3638 -- is treated essentially as another level in the
3639 -- recursion. For this case the parent of the component
3640 -- list is the record definition, and its parent is the
3641 -- full type declaration containing the discriminant
3644 elsif Nkind (Parent (Clist)) = N_Record_Definition then
3645 Clist := Parent (Parent ((Clist)));
3647 -- If neither of these two cases, we are at the top of
3651 exit Component_List_Loop;
3653 end loop Component_List_Loop;
3655 <<Continue_Main_Component_Loop>>
3656 Next_Entity (C1_Ent);
3658 end loop Main_Component_Loop;
3662 -- For records that have component clauses for all components, and whose
3663 -- size is less than or equal to 32, we need to know the size in the
3664 -- front end to activate possible packed array processing where the
3665 -- component type is a record.
3667 -- At this stage Hbit + 1 represents the first unused bit from all the
3668 -- component clauses processed, so if the component clauses are
3669 -- complete, then this is the length of the record.
3671 -- For records longer than System.Storage_Unit, and for those where not
3672 -- all components have component clauses, the back end determines the
3673 -- length (it may for example be appropriate to round up the size
3674 -- to some convenient boundary, based on alignment considerations, etc).
3676 if Unknown_RM_Size (Rectype) and then Hbit + 1 <= 32 then
3678 -- Nothing to do if at least one component has no component clause
3680 Comp := First_Component_Or_Discriminant (Rectype);
3681 while Present (Comp) loop
3682 exit when No (Component_Clause (Comp));
3683 Next_Component_Or_Discriminant (Comp);
3686 -- If we fall out of loop, all components have component clauses
3687 -- and so we can set the size to the maximum value.
3690 Set_RM_Size (Rectype, Hbit + 1);
3693 end Check_Record_Representation_Clause;
3699 procedure Check_Size
3703 Biased : out Boolean)
3705 UT : constant Entity_Id := Underlying_Type (T);
3711 -- Dismiss cases for generic types or types with previous errors
3714 or else UT = Any_Type
3715 or else Is_Generic_Type (UT)
3716 or else Is_Generic_Type (Root_Type (UT))
3720 -- Check case of bit packed array
3722 elsif Is_Array_Type (UT)
3723 and then Known_Static_Component_Size (UT)
3724 and then Is_Bit_Packed_Array (UT)
3732 Asiz := Component_Size (UT);
3733 Indx := First_Index (UT);
3735 Ityp := Etype (Indx);
3737 -- If non-static bound, then we are not in the business of
3738 -- trying to check the length, and indeed an error will be
3739 -- issued elsewhere, since sizes of non-static array types
3740 -- cannot be set implicitly or explicitly.
3742 if not Is_Static_Subtype (Ityp) then
3746 -- Otherwise accumulate next dimension
3748 Asiz := Asiz * (Expr_Value (Type_High_Bound (Ityp)) -
3749 Expr_Value (Type_Low_Bound (Ityp)) +
3753 exit when No (Indx);
3759 Error_Msg_Uint_1 := Asiz;
3761 ("size for& too small, minimum allowed is ^", N, T);
3762 Set_Esize (T, Asiz);
3763 Set_RM_Size (T, Asiz);
3767 -- All other composite types are ignored
3769 elsif Is_Composite_Type (UT) then
3772 -- For fixed-point types, don't check minimum if type is not frozen,
3773 -- since we don't know all the characteristics of the type that can
3774 -- affect the size (e.g. a specified small) till freeze time.
3776 elsif Is_Fixed_Point_Type (UT)
3777 and then not Is_Frozen (UT)
3781 -- Cases for which a minimum check is required
3784 -- Ignore if specified size is correct for the type
3786 if Known_Esize (UT) and then Siz = Esize (UT) then
3790 -- Otherwise get minimum size
3792 M := UI_From_Int (Minimum_Size (UT));
3796 -- Size is less than minimum size, but one possibility remains
3797 -- that we can manage with the new size if we bias the type.
3799 M := UI_From_Int (Minimum_Size (UT, Biased => True));
3802 Error_Msg_Uint_1 := M;
3804 ("size for& too small, minimum allowed is ^", N, T);
3814 -------------------------
3815 -- Get_Alignment_Value --
3816 -------------------------
3818 function Get_Alignment_Value (Expr : Node_Id) return Uint is
3819 Align : constant Uint := Static_Integer (Expr);
3822 if Align = No_Uint then
3825 elsif Align <= 0 then
3826 Error_Msg_N ("alignment value must be positive", Expr);
3830 for J in Int range 0 .. 64 loop
3832 M : constant Uint := Uint_2 ** J;
3835 exit when M = Align;
3839 ("alignment value must be power of 2", Expr);
3847 end Get_Alignment_Value;
3853 procedure Initialize is
3855 Unchecked_Conversions.Init;
3858 -------------------------
3859 -- Is_Operational_Item --
3860 -------------------------
3862 function Is_Operational_Item (N : Node_Id) return Boolean is
3864 if Nkind (N) /= N_Attribute_Definition_Clause then
3868 Id : constant Attribute_Id := Get_Attribute_Id (Chars (N));
3870 return Id = Attribute_Input
3871 or else Id = Attribute_Output
3872 or else Id = Attribute_Read
3873 or else Id = Attribute_Write
3874 or else Id = Attribute_External_Tag;
3877 end Is_Operational_Item;
3883 function Minimum_Size
3885 Biased : Boolean := False) return Nat
3887 Lo : Uint := No_Uint;
3888 Hi : Uint := No_Uint;
3889 LoR : Ureal := No_Ureal;
3890 HiR : Ureal := No_Ureal;
3891 LoSet : Boolean := False;
3892 HiSet : Boolean := False;
3896 R_Typ : constant Entity_Id := Root_Type (T);
3899 -- If bad type, return 0
3901 if T = Any_Type then
3904 -- For generic types, just return zero. There cannot be any legitimate
3905 -- need to know such a size, but this routine may be called with a
3906 -- generic type as part of normal processing.
3908 elsif Is_Generic_Type (R_Typ)
3909 or else R_Typ = Any_Type
3913 -- Access types. Normally an access type cannot have a size smaller
3914 -- than the size of System.Address. The exception is on VMS, where
3915 -- we have short and long addresses, and it is possible for an access
3916 -- type to have a short address size (and thus be less than the size
3917 -- of System.Address itself). We simply skip the check for VMS, and
3918 -- leave it to the back end to do the check.
3920 elsif Is_Access_Type (T) then
3921 if OpenVMS_On_Target then
3924 return System_Address_Size;
3927 -- Floating-point types
3929 elsif Is_Floating_Point_Type (T) then
3930 return UI_To_Int (Esize (R_Typ));
3934 elsif Is_Discrete_Type (T) then
3936 -- The following loop is looking for the nearest compile time known
3937 -- bounds following the ancestor subtype chain. The idea is to find
3938 -- the most restrictive known bounds information.
3942 if Ancest = Any_Type or else Etype (Ancest) = Any_Type then
3947 if Compile_Time_Known_Value (Type_Low_Bound (Ancest)) then
3948 Lo := Expr_Rep_Value (Type_Low_Bound (Ancest));
3955 if Compile_Time_Known_Value (Type_High_Bound (Ancest)) then
3956 Hi := Expr_Rep_Value (Type_High_Bound (Ancest));
3962 Ancest := Ancestor_Subtype (Ancest);
3965 Ancest := Base_Type (T);
3967 if Is_Generic_Type (Ancest) then
3973 -- Fixed-point types. We can't simply use Expr_Value to get the
3974 -- Corresponding_Integer_Value values of the bounds, since these do not
3975 -- get set till the type is frozen, and this routine can be called
3976 -- before the type is frozen. Similarly the test for bounds being static
3977 -- needs to include the case where we have unanalyzed real literals for
3980 elsif Is_Fixed_Point_Type (T) then
3982 -- The following loop is looking for the nearest compile time known
3983 -- bounds following the ancestor subtype chain. The idea is to find
3984 -- the most restrictive known bounds information.
3988 if Ancest = Any_Type or else Etype (Ancest) = Any_Type then
3992 -- Note: In the following two tests for LoSet and HiSet, it may
3993 -- seem redundant to test for N_Real_Literal here since normally
3994 -- one would assume that the test for the value being known at
3995 -- compile time includes this case. However, there is a glitch.
3996 -- If the real literal comes from folding a non-static expression,
3997 -- then we don't consider any non- static expression to be known
3998 -- at compile time if we are in configurable run time mode (needed
3999 -- in some cases to give a clearer definition of what is and what
4000 -- is not accepted). So the test is indeed needed. Without it, we
4001 -- would set neither Lo_Set nor Hi_Set and get an infinite loop.
4004 if Nkind (Type_Low_Bound (Ancest)) = N_Real_Literal
4005 or else Compile_Time_Known_Value (Type_Low_Bound (Ancest))
4007 LoR := Expr_Value_R (Type_Low_Bound (Ancest));
4014 if Nkind (Type_High_Bound (Ancest)) = N_Real_Literal
4015 or else Compile_Time_Known_Value (Type_High_Bound (Ancest))
4017 HiR := Expr_Value_R (Type_High_Bound (Ancest));
4023 Ancest := Ancestor_Subtype (Ancest);
4026 Ancest := Base_Type (T);
4028 if Is_Generic_Type (Ancest) then
4034 Lo := UR_To_Uint (LoR / Small_Value (T));
4035 Hi := UR_To_Uint (HiR / Small_Value (T));
4037 -- No other types allowed
4040 raise Program_Error;
4043 -- Fall through with Hi and Lo set. Deal with biased case
4046 and then not Is_Fixed_Point_Type (T)
4047 and then not (Is_Enumeration_Type (T)
4048 and then Has_Non_Standard_Rep (T)))
4049 or else Has_Biased_Representation (T)
4055 -- Signed case. Note that we consider types like range 1 .. -1 to be
4056 -- signed for the purpose of computing the size, since the bounds have
4057 -- to be accommodated in the base type.
4059 if Lo < 0 or else Hi < 0 then
4063 -- S = size, B = 2 ** (size - 1) (can accommodate -B .. +(B - 1))
4064 -- Note that we accommodate the case where the bounds cross. This
4065 -- can happen either because of the way the bounds are declared
4066 -- or because of the algorithm in Freeze_Fixed_Point_Type.
4080 -- If both bounds are positive, make sure that both are represen-
4081 -- table in the case where the bounds are crossed. This can happen
4082 -- either because of the way the bounds are declared, or because of
4083 -- the algorithm in Freeze_Fixed_Point_Type.
4089 -- S = size, (can accommodate 0 .. (2**size - 1))
4092 while Hi >= Uint_2 ** S loop
4100 ---------------------------
4101 -- New_Stream_Subprogram --
4102 ---------------------------
4104 procedure New_Stream_Subprogram
4108 Nam : TSS_Name_Type)
4110 Loc : constant Source_Ptr := Sloc (N);
4111 Sname : constant Name_Id := Make_TSS_Name (Base_Type (Ent), Nam);
4112 Subp_Id : Entity_Id;
4113 Subp_Decl : Node_Id;
4117 Defer_Declaration : constant Boolean :=
4118 Is_Tagged_Type (Ent) or else Is_Private_Type (Ent);
4119 -- For a tagged type, there is a declaration for each stream attribute
4120 -- at the freeze point, and we must generate only a completion of this
4121 -- declaration. We do the same for private types, because the full view
4122 -- might be tagged. Otherwise we generate a declaration at the point of
4123 -- the attribute definition clause.
4125 function Build_Spec return Node_Id;
4126 -- Used for declaration and renaming declaration, so that this is
4127 -- treated as a renaming_as_body.
4133 function Build_Spec return Node_Id is
4134 Out_P : constant Boolean := (Nam = TSS_Stream_Read);
4137 T_Ref : constant Node_Id := New_Reference_To (Etyp, Loc);
4140 Subp_Id := Make_Defining_Identifier (Loc, Sname);
4142 -- S : access Root_Stream_Type'Class
4144 Formals := New_List (
4145 Make_Parameter_Specification (Loc,
4146 Defining_Identifier =>
4147 Make_Defining_Identifier (Loc, Name_S),
4149 Make_Access_Definition (Loc,
4152 Designated_Type (Etype (F)), Loc))));
4154 if Nam = TSS_Stream_Input then
4155 Spec := Make_Function_Specification (Loc,
4156 Defining_Unit_Name => Subp_Id,
4157 Parameter_Specifications => Formals,
4158 Result_Definition => T_Ref);
4163 Make_Parameter_Specification (Loc,
4164 Defining_Identifier => Make_Defining_Identifier (Loc, Name_V),
4165 Out_Present => Out_P,
4166 Parameter_Type => T_Ref));
4169 Make_Procedure_Specification (Loc,
4170 Defining_Unit_Name => Subp_Id,
4171 Parameter_Specifications => Formals);
4177 -- Start of processing for New_Stream_Subprogram
4180 F := First_Formal (Subp);
4182 if Ekind (Subp) = E_Procedure then
4183 Etyp := Etype (Next_Formal (F));
4185 Etyp := Etype (Subp);
4188 -- Prepare subprogram declaration and insert it as an action on the
4189 -- clause node. The visibility for this entity is used to test for
4190 -- visibility of the attribute definition clause (in the sense of
4191 -- 8.3(23) as amended by AI-195).
4193 if not Defer_Declaration then
4195 Make_Subprogram_Declaration (Loc,
4196 Specification => Build_Spec);
4198 -- For a tagged type, there is always a visible declaration for each
4199 -- stream TSS (it is a predefined primitive operation), and the
4200 -- completion of this declaration occurs at the freeze point, which is
4201 -- not always visible at places where the attribute definition clause is
4202 -- visible. So, we create a dummy entity here for the purpose of
4203 -- tracking the visibility of the attribute definition clause itself.
4207 Make_Defining_Identifier (Loc,
4208 Chars => New_External_Name (Sname, 'V'));
4210 Make_Object_Declaration (Loc,
4211 Defining_Identifier => Subp_Id,
4212 Object_Definition => New_Occurrence_Of (Standard_Boolean, Loc));
4215 Insert_Action (N, Subp_Decl);
4216 Set_Entity (N, Subp_Id);
4219 Make_Subprogram_Renaming_Declaration (Loc,
4220 Specification => Build_Spec,
4221 Name => New_Reference_To (Subp, Loc));
4223 if Defer_Declaration then
4224 Set_TSS (Base_Type (Ent), Subp_Id);
4226 Insert_Action (N, Subp_Decl);
4227 Copy_TSS (Subp_Id, Base_Type (Ent));
4229 end New_Stream_Subprogram;
4231 ------------------------
4232 -- Rep_Item_Too_Early --
4233 ------------------------
4235 function Rep_Item_Too_Early (T : Entity_Id; N : Node_Id) return Boolean is
4237 -- Cannot apply non-operational rep items to generic types
4239 if Is_Operational_Item (N) then
4243 and then Is_Generic_Type (Root_Type (T))
4245 Error_Msg_N ("representation item not allowed for generic type", N);
4249 -- Otherwise check for incomplete type
4251 if Is_Incomplete_Or_Private_Type (T)
4252 and then No (Underlying_Type (T))
4255 ("representation item must be after full type declaration", N);
4258 -- If the type has incomplete components, a representation clause is
4259 -- illegal but stream attributes and Convention pragmas are correct.
4261 elsif Has_Private_Component (T) then
4262 if Nkind (N) = N_Pragma then
4266 ("representation item must appear after type is fully defined",
4273 end Rep_Item_Too_Early;
4275 -----------------------
4276 -- Rep_Item_Too_Late --
4277 -----------------------
4279 function Rep_Item_Too_Late
4282 FOnly : Boolean := False) return Boolean
4285 Parent_Type : Entity_Id;
4288 -- Output the too late message. Note that this is not considered a
4289 -- serious error, since the effect is simply that we ignore the
4290 -- representation clause in this case.
4296 procedure Too_Late is
4298 Error_Msg_N ("|representation item appears too late!", N);
4301 -- Start of processing for Rep_Item_Too_Late
4304 -- First make sure entity is not frozen (RM 13.1(9)). Exclude imported
4305 -- types, which may be frozen if they appear in a representation clause
4306 -- for a local type.
4309 and then not From_With_Type (T)
4312 S := First_Subtype (T);
4314 if Present (Freeze_Node (S)) then
4316 ("?no more representation items for }", Freeze_Node (S), S);
4321 -- Check for case of non-tagged derived type whose parent either has
4322 -- primitive operations, or is a by reference type (RM 13.1(10)).
4326 and then Is_Derived_Type (T)
4327 and then not Is_Tagged_Type (T)
4329 Parent_Type := Etype (Base_Type (T));
4331 if Has_Primitive_Operations (Parent_Type) then
4334 ("primitive operations already defined for&!", N, Parent_Type);
4337 elsif Is_By_Reference_Type (Parent_Type) then
4340 ("parent type & is a by reference type!", N, Parent_Type);
4345 -- No error, link item into head of chain of rep items for the entity,
4346 -- but avoid chaining if we have an overloadable entity, and the pragma
4347 -- is one that can apply to multiple overloaded entities.
4349 if Is_Overloadable (T)
4350 and then Nkind (N) = N_Pragma
4353 Pname : constant Name_Id := Pragma_Name (N);
4355 if Pname = Name_Convention or else
4356 Pname = Name_Import or else
4357 Pname = Name_Export or else
4358 Pname = Name_External or else
4359 Pname = Name_Interface
4366 Record_Rep_Item (T, N);
4368 end Rep_Item_Too_Late;
4370 -------------------------
4371 -- Same_Representation --
4372 -------------------------
4374 function Same_Representation (Typ1, Typ2 : Entity_Id) return Boolean is
4375 T1 : constant Entity_Id := Underlying_Type (Typ1);
4376 T2 : constant Entity_Id := Underlying_Type (Typ2);
4379 -- A quick check, if base types are the same, then we definitely have
4380 -- the same representation, because the subtype specific representation
4381 -- attributes (Size and Alignment) do not affect representation from
4382 -- the point of view of this test.
4384 if Base_Type (T1) = Base_Type (T2) then
4387 elsif Is_Private_Type (Base_Type (T2))
4388 and then Base_Type (T1) = Full_View (Base_Type (T2))
4393 -- Tagged types never have differing representations
4395 if Is_Tagged_Type (T1) then
4399 -- Representations are definitely different if conventions differ
4401 if Convention (T1) /= Convention (T2) then
4405 -- Representations are different if component alignments differ
4407 if (Is_Record_Type (T1) or else Is_Array_Type (T1))
4409 (Is_Record_Type (T2) or else Is_Array_Type (T2))
4410 and then Component_Alignment (T1) /= Component_Alignment (T2)
4415 -- For arrays, the only real issue is component size. If we know the
4416 -- component size for both arrays, and it is the same, then that's
4417 -- good enough to know we don't have a change of representation.
4419 if Is_Array_Type (T1) then
4420 if Known_Component_Size (T1)
4421 and then Known_Component_Size (T2)
4422 and then Component_Size (T1) = Component_Size (T2)
4428 -- Types definitely have same representation if neither has non-standard
4429 -- representation since default representations are always consistent.
4430 -- If only one has non-standard representation, and the other does not,
4431 -- then we consider that they do not have the same representation. They
4432 -- might, but there is no way of telling early enough.
4434 if Has_Non_Standard_Rep (T1) then
4435 if not Has_Non_Standard_Rep (T2) then
4439 return not Has_Non_Standard_Rep (T2);
4442 -- Here the two types both have non-standard representation, and we need
4443 -- to determine if they have the same non-standard representation.
4445 -- For arrays, we simply need to test if the component sizes are the
4446 -- same. Pragma Pack is reflected in modified component sizes, so this
4447 -- check also deals with pragma Pack.
4449 if Is_Array_Type (T1) then
4450 return Component_Size (T1) = Component_Size (T2);
4452 -- Tagged types always have the same representation, because it is not
4453 -- possible to specify different representations for common fields.
4455 elsif Is_Tagged_Type (T1) then
4458 -- Case of record types
4460 elsif Is_Record_Type (T1) then
4462 -- Packed status must conform
4464 if Is_Packed (T1) /= Is_Packed (T2) then
4467 -- Otherwise we must check components. Typ2 maybe a constrained
4468 -- subtype with fewer components, so we compare the components
4469 -- of the base types.
4472 Record_Case : declare
4473 CD1, CD2 : Entity_Id;
4475 function Same_Rep return Boolean;
4476 -- CD1 and CD2 are either components or discriminants. This
4477 -- function tests whether the two have the same representation
4483 function Same_Rep return Boolean is
4485 if No (Component_Clause (CD1)) then
4486 return No (Component_Clause (CD2));
4490 Present (Component_Clause (CD2))
4492 Component_Bit_Offset (CD1) = Component_Bit_Offset (CD2)
4494 Esize (CD1) = Esize (CD2);
4498 -- Start of processing for Record_Case
4501 if Has_Discriminants (T1) then
4502 CD1 := First_Discriminant (T1);
4503 CD2 := First_Discriminant (T2);
4505 -- The number of discriminants may be different if the
4506 -- derived type has fewer (constrained by values). The
4507 -- invisible discriminants retain the representation of
4508 -- the original, so the discrepancy does not per se
4509 -- indicate a different representation.
4512 and then Present (CD2)
4514 if not Same_Rep then
4517 Next_Discriminant (CD1);
4518 Next_Discriminant (CD2);
4523 CD1 := First_Component (Underlying_Type (Base_Type (T1)));
4524 CD2 := First_Component (Underlying_Type (Base_Type (T2)));
4526 while Present (CD1) loop
4527 if not Same_Rep then
4530 Next_Component (CD1);
4531 Next_Component (CD2);
4539 -- For enumeration types, we must check each literal to see if the
4540 -- representation is the same. Note that we do not permit enumeration
4541 -- representation clauses for Character and Wide_Character, so these
4542 -- cases were already dealt with.
4544 elsif Is_Enumeration_Type (T1) then
4546 Enumeration_Case : declare
4550 L1 := First_Literal (T1);
4551 L2 := First_Literal (T2);
4553 while Present (L1) loop
4554 if Enumeration_Rep (L1) /= Enumeration_Rep (L2) then
4564 end Enumeration_Case;
4566 -- Any other types have the same representation for these purposes
4571 end Same_Representation;
4573 --------------------
4574 -- Set_Enum_Esize --
4575 --------------------
4577 procedure Set_Enum_Esize (T : Entity_Id) is
4585 -- Find the minimum standard size (8,16,32,64) that fits
4587 Lo := Enumeration_Rep (Entity (Type_Low_Bound (T)));
4588 Hi := Enumeration_Rep (Entity (Type_High_Bound (T)));
4591 if Lo >= -Uint_2**07 and then Hi < Uint_2**07 then
4592 Sz := Standard_Character_Size; -- May be > 8 on some targets
4594 elsif Lo >= -Uint_2**15 and then Hi < Uint_2**15 then
4597 elsif Lo >= -Uint_2**31 and then Hi < Uint_2**31 then
4600 else pragma Assert (Lo >= -Uint_2**63 and then Hi < Uint_2**63);
4605 if Hi < Uint_2**08 then
4606 Sz := Standard_Character_Size; -- May be > 8 on some targets
4608 elsif Hi < Uint_2**16 then
4611 elsif Hi < Uint_2**32 then
4614 else pragma Assert (Hi < Uint_2**63);
4619 -- That minimum is the proper size unless we have a foreign convention
4620 -- and the size required is 32 or less, in which case we bump the size
4621 -- up to 32. This is required for C and C++ and seems reasonable for
4622 -- all other foreign conventions.
4624 if Has_Foreign_Convention (T)
4625 and then Esize (T) < Standard_Integer_Size
4627 Init_Esize (T, Standard_Integer_Size);
4633 ------------------------------
4634 -- Validate_Address_Clauses --
4635 ------------------------------
4637 procedure Validate_Address_Clauses is
4639 for J in Address_Clause_Checks.First .. Address_Clause_Checks.Last loop
4641 ACCR : Address_Clause_Check_Record
4642 renames Address_Clause_Checks.Table (J);
4653 -- Skip processing of this entry if warning already posted
4655 if not Address_Warning_Posted (ACCR.N) then
4657 Expr := Original_Node (Expression (ACCR.N));
4661 X_Alignment := Alignment (ACCR.X);
4662 Y_Alignment := Alignment (ACCR.Y);
4664 -- Similarly obtain sizes
4666 X_Size := Esize (ACCR.X);
4667 Y_Size := Esize (ACCR.Y);
4669 -- Check for large object overlaying smaller one
4672 and then X_Size > Uint_0
4673 and then X_Size > Y_Size
4676 ("?& overlays smaller object", ACCR.N, ACCR.X);
4678 ("\?program execution may be erroneous", ACCR.N);
4679 Error_Msg_Uint_1 := X_Size;
4681 ("\?size of & is ^", ACCR.N, ACCR.X);
4682 Error_Msg_Uint_1 := Y_Size;
4684 ("\?size of & is ^", ACCR.N, ACCR.Y);
4686 -- Check for inadequate alignment, both of the base object
4687 -- and of the offset, if any.
4689 -- Note: we do not check the alignment if we gave a size
4690 -- warning, since it would likely be redundant.
4692 elsif Y_Alignment /= Uint_0
4693 and then (Y_Alignment < X_Alignment
4696 Nkind (Expr) = N_Attribute_Reference
4698 Attribute_Name (Expr) = Name_Address
4700 Has_Compatible_Alignment
4701 (ACCR.X, Prefix (Expr))
4702 /= Known_Compatible))
4705 ("?specified address for& may be inconsistent "
4709 ("\?program execution may be erroneous (RM 13.3(27))",
4711 Error_Msg_Uint_1 := X_Alignment;
4713 ("\?alignment of & is ^",
4715 Error_Msg_Uint_1 := Y_Alignment;
4717 ("\?alignment of & is ^",
4719 if Y_Alignment >= X_Alignment then
4721 ("\?but offset is not multiple of alignment",
4728 end Validate_Address_Clauses;
4730 -----------------------------------
4731 -- Validate_Unchecked_Conversion --
4732 -----------------------------------
4734 procedure Validate_Unchecked_Conversion
4736 Act_Unit : Entity_Id)
4743 -- Obtain source and target types. Note that we call Ancestor_Subtype
4744 -- here because the processing for generic instantiation always makes
4745 -- subtypes, and we want the original frozen actual types.
4747 -- If we are dealing with private types, then do the check on their
4748 -- fully declared counterparts if the full declarations have been
4749 -- encountered (they don't have to be visible, but they must exist!)
4751 Source := Ancestor_Subtype (Etype (First_Formal (Act_Unit)));
4753 if Is_Private_Type (Source)
4754 and then Present (Underlying_Type (Source))
4756 Source := Underlying_Type (Source);
4759 Target := Ancestor_Subtype (Etype (Act_Unit));
4761 -- If either type is generic, the instantiation happens within a generic
4762 -- unit, and there is nothing to check. The proper check
4763 -- will happen when the enclosing generic is instantiated.
4765 if Is_Generic_Type (Source) or else Is_Generic_Type (Target) then
4769 if Is_Private_Type (Target)
4770 and then Present (Underlying_Type (Target))
4772 Target := Underlying_Type (Target);
4775 -- Source may be unconstrained array, but not target
4777 if Is_Array_Type (Target)
4778 and then not Is_Constrained (Target)
4781 ("unchecked conversion to unconstrained array not allowed", N);
4785 -- Warn if conversion between two different convention pointers
4787 if Is_Access_Type (Target)
4788 and then Is_Access_Type (Source)
4789 and then Convention (Target) /= Convention (Source)
4790 and then Warn_On_Unchecked_Conversion
4792 -- Give warnings for subprogram pointers only on most targets. The
4793 -- exception is VMS, where data pointers can have different lengths
4794 -- depending on the pointer convention.
4796 if Is_Access_Subprogram_Type (Target)
4797 or else Is_Access_Subprogram_Type (Source)
4798 or else OpenVMS_On_Target
4801 ("?conversion between pointers with different conventions!", N);
4805 -- Warn if one of the operands is Ada.Calendar.Time. Do not emit a
4806 -- warning when compiling GNAT-related sources.
4808 if Warn_On_Unchecked_Conversion
4809 and then not In_Predefined_Unit (N)
4810 and then RTU_Loaded (Ada_Calendar)
4812 (Chars (Source) = Name_Time
4814 Chars (Target) = Name_Time)
4816 -- If Ada.Calendar is loaded and the name of one of the operands is
4817 -- Time, there is a good chance that this is Ada.Calendar.Time.
4820 Calendar_Time : constant Entity_Id :=
4821 Full_View (RTE (RO_CA_Time));
4823 pragma Assert (Present (Calendar_Time));
4825 if Source = Calendar_Time
4826 or else Target = Calendar_Time
4829 ("?representation of 'Time values may change between " &
4830 "'G'N'A'T versions", N);
4835 -- Make entry in unchecked conversion table for later processing by
4836 -- Validate_Unchecked_Conversions, which will check sizes and alignments
4837 -- (using values set by the back-end where possible). This is only done
4838 -- if the appropriate warning is active.
4840 if Warn_On_Unchecked_Conversion then
4841 Unchecked_Conversions.Append
4842 (New_Val => UC_Entry'
4847 -- If both sizes are known statically now, then back end annotation
4848 -- is not required to do a proper check but if either size is not
4849 -- known statically, then we need the annotation.
4851 if Known_Static_RM_Size (Source)
4852 and then Known_Static_RM_Size (Target)
4856 Back_Annotate_Rep_Info := True;
4860 -- If unchecked conversion to access type, and access type is declared
4861 -- in the same unit as the unchecked conversion, then set the
4862 -- No_Strict_Aliasing flag (no strict aliasing is implicit in this
4865 if Is_Access_Type (Target) and then
4866 In_Same_Source_Unit (Target, N)
4868 Set_No_Strict_Aliasing (Implementation_Base_Type (Target));
4871 -- Generate N_Validate_Unchecked_Conversion node for back end in
4872 -- case the back end needs to perform special validation checks.
4874 -- Shouldn't this be in Exp_Ch13, since the check only gets done
4875 -- if we have full expansion and the back end is called ???
4878 Make_Validate_Unchecked_Conversion (Sloc (N));
4879 Set_Source_Type (Vnode, Source);
4880 Set_Target_Type (Vnode, Target);
4882 -- If the unchecked conversion node is in a list, just insert before it.
4883 -- If not we have some strange case, not worth bothering about.
4885 if Is_List_Member (N) then
4886 Insert_After (N, Vnode);
4888 end Validate_Unchecked_Conversion;
4890 ------------------------------------
4891 -- Validate_Unchecked_Conversions --
4892 ------------------------------------
4894 procedure Validate_Unchecked_Conversions is
4896 for N in Unchecked_Conversions.First .. Unchecked_Conversions.Last loop
4898 T : UC_Entry renames Unchecked_Conversions.Table (N);
4900 Eloc : constant Source_Ptr := T.Eloc;
4901 Source : constant Entity_Id := T.Source;
4902 Target : constant Entity_Id := T.Target;
4908 -- This validation check, which warns if we have unequal sizes for
4909 -- unchecked conversion, and thus potentially implementation
4910 -- dependent semantics, is one of the few occasions on which we
4911 -- use the official RM size instead of Esize. See description in
4912 -- Einfo "Handling of Type'Size Values" for details.
4914 if Serious_Errors_Detected = 0
4915 and then Known_Static_RM_Size (Source)
4916 and then Known_Static_RM_Size (Target)
4918 -- Don't do the check if warnings off for either type, note the
4919 -- deliberate use of OR here instead of OR ELSE to get the flag
4920 -- Warnings_Off_Used set for both types if appropriate.
4922 and then not (Has_Warnings_Off (Source)
4924 Has_Warnings_Off (Target))
4926 Source_Siz := RM_Size (Source);
4927 Target_Siz := RM_Size (Target);
4929 if Source_Siz /= Target_Siz then
4931 ("?types for unchecked conversion have different sizes!",
4934 if All_Errors_Mode then
4935 Error_Msg_Name_1 := Chars (Source);
4936 Error_Msg_Uint_1 := Source_Siz;
4937 Error_Msg_Name_2 := Chars (Target);
4938 Error_Msg_Uint_2 := Target_Siz;
4939 Error_Msg ("\size of % is ^, size of % is ^?", Eloc);
4941 Error_Msg_Uint_1 := UI_Abs (Source_Siz - Target_Siz);
4943 if Is_Discrete_Type (Source)
4944 and then Is_Discrete_Type (Target)
4946 if Source_Siz > Target_Siz then
4948 ("\?^ high order bits of source will be ignored!",
4951 elsif Is_Unsigned_Type (Source) then
4953 ("\?source will be extended with ^ high order " &
4954 "zero bits?!", Eloc);
4958 ("\?source will be extended with ^ high order " &
4963 elsif Source_Siz < Target_Siz then
4964 if Is_Discrete_Type (Target) then
4965 if Bytes_Big_Endian then
4967 ("\?target value will include ^ undefined " &
4972 ("\?target value will include ^ undefined " &
4979 ("\?^ trailing bits of target value will be " &
4980 "undefined!", Eloc);
4983 else pragma Assert (Source_Siz > Target_Siz);
4985 ("\?^ trailing bits of source will be ignored!",
4992 -- If both types are access types, we need to check the alignment.
4993 -- If the alignment of both is specified, we can do it here.
4995 if Serious_Errors_Detected = 0
4996 and then Ekind (Source) in Access_Kind
4997 and then Ekind (Target) in Access_Kind
4998 and then Target_Strict_Alignment
4999 and then Present (Designated_Type (Source))
5000 and then Present (Designated_Type (Target))
5003 D_Source : constant Entity_Id := Designated_Type (Source);
5004 D_Target : constant Entity_Id := Designated_Type (Target);
5007 if Known_Alignment (D_Source)
5008 and then Known_Alignment (D_Target)
5011 Source_Align : constant Uint := Alignment (D_Source);
5012 Target_Align : constant Uint := Alignment (D_Target);
5015 if Source_Align < Target_Align
5016 and then not Is_Tagged_Type (D_Source)
5018 -- Suppress warning if warnings suppressed on either
5019 -- type or either designated type. Note the use of
5020 -- OR here instead of OR ELSE. That is intentional,
5021 -- we would like to set flag Warnings_Off_Used in
5022 -- all types for which warnings are suppressed.
5024 and then not (Has_Warnings_Off (D_Source)
5026 Has_Warnings_Off (D_Target)
5028 Has_Warnings_Off (Source)
5030 Has_Warnings_Off (Target))
5032 Error_Msg_Uint_1 := Target_Align;
5033 Error_Msg_Uint_2 := Source_Align;
5034 Error_Msg_Node_1 := D_Target;
5035 Error_Msg_Node_2 := D_Source;
5037 ("?alignment of & (^) is stricter than " &
5038 "alignment of & (^)!", Eloc);
5040 ("\?resulting access value may have invalid " &
5041 "alignment!", Eloc);
5049 end Validate_Unchecked_Conversions;