1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
11 -- Copyright (C) 1992-2001, Free Software Foundation, Inc. --
13 -- GNAT is free software; you can redistribute it and/or modify it under --
14 -- terms of the GNU General Public License as published by the Free Soft- --
15 -- ware Foundation; either version 2, or (at your option) any later ver- --
16 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
17 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
18 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
19 -- for more details. You should have received a copy of the GNU General --
20 -- Public License distributed with GNAT; see file COPYING. If not, write --
21 -- to the Free Software Foundation, 59 Temple Place - Suite 330, Boston, --
22 -- MA 02111-1307, USA. --
24 -- GNAT was originally developed by the GNAT team at New York University. --
25 -- It is now maintained by Ada Core Technologies Inc (http://www.gnat.com). --
27 ------------------------------------------------------------------------------
29 with Atree; use Atree;
30 with Checks; use Checks;
31 with Einfo; use Einfo;
32 with Errout; use Errout;
33 with Exp_Tss; use Exp_Tss;
34 with Exp_Util; use Exp_Util;
35 with Hostparm; use Hostparm;
37 with Nlists; use Nlists;
38 with Nmake; use Nmake;
40 with Rtsfind; use Rtsfind;
42 with Sem_Ch8; use Sem_Ch8;
43 with Sem_Eval; use Sem_Eval;
44 with Sem_Res; use Sem_Res;
45 with Sem_Type; use Sem_Type;
46 with Sem_Util; use Sem_Util;
47 with Stand; use Stand;
48 with Sinfo; use Sinfo;
49 with Snames; use Snames;
51 with Ttypes; use Ttypes;
52 with Tbuild; use Tbuild;
53 with Urealp; use Urealp;
55 with GNAT.Heap_Sort_A; use GNAT.Heap_Sort_A;
57 package body Sem_Ch13 is
59 SSU : constant Pos := System_Storage_Unit;
60 -- Convenient short hand for commonly used constant
62 -----------------------
63 -- Local Subprograms --
64 -----------------------
66 procedure Alignment_Check_For_Esize_Change (Typ : Entity_Id);
67 -- This routine is called after setting the Esize of type entity Typ.
68 -- The purpose is to deal with the situation where an aligment has been
69 -- inherited from a derived type that is no longer appropriate for the
70 -- new Esize value. In this case, we reset the Alignment to unknown.
72 procedure Check_Component_Overlap (C1_Ent, C2_Ent : Entity_Id);
73 -- Given two entities for record components or discriminants, checks
74 -- if they hav overlapping component clauses and issues errors if so.
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_Function
92 -- Create a function renaming of a given stream attribute to the
93 -- designated subprogram and then in the tagged case, provide this as
94 -- a primitive operation, or in the non-tagged case make an appropriate
95 -- TSS entry. Used for Input. This is more properly an expansion activity
96 -- than just semantics, but the presence of user-defined stream functions
97 -- for limited types is a legality check, which is why this takes place
98 -- here rather than in exp_ch13, where it was previously.
100 procedure New_Stream_Procedure
105 Out_P : Boolean := False);
106 -- Create a procedure renaming of a given stream attribute to the
107 -- designated subprogram and then in the tagged case, provide this as
108 -- a primitive operation, or in the non-tagged case make an appropriate
109 -- TSS entry. Used for Read, Output, Write.
111 procedure Check_Constant_Address_Clause (Expr : Node_Id; U_Ent : Entity_Id);
112 -- Expr is an expression for an address clause. This procedure checks
113 -- that the expression is constant, in the limited sense that it is safe
114 -- to evaluate it at the point the object U_Ent is declared, rather than
115 -- at the point of the address clause. The condition for this to be true
116 -- is that the expression has no variables, no constants declared after
117 -- U_Ent, and no calls to non-pure functions. If this condition is not
118 -- met, then an appropriate error message is posted.
120 procedure Warn_Overlay
124 -- Expr is the expression for an address clause for entity Nam whose type
125 -- is Typ. If Typ has a default initialization, check whether the address
126 -- clause might overlay two entities, and emit a warning on the side effect
127 -- that the initialization will cause.
129 ----------------------------------------------
130 -- Table for Validate_Unchecked_Conversions --
131 ----------------------------------------------
133 -- The following table collects unchecked conversions for validation.
134 -- Entries are made by Validate_Unchecked_Conversion and then the
135 -- call to Validate_Unchecked_Conversions does the actual error
136 -- checking and posting of warnings. The reason for this delayed
137 -- processing is to take advantage of back-annotations of size and
138 -- alignment values peformed by the back end.
140 type UC_Entry is record
141 Enode : Node_Id; -- node used for posting warnings
142 Source : Entity_Id; -- source type for unchecked conversion
143 Target : Entity_Id; -- target type for unchecked conversion
146 package Unchecked_Conversions is new Table.Table (
147 Table_Component_Type => UC_Entry,
148 Table_Index_Type => Int,
149 Table_Low_Bound => 1,
151 Table_Increment => 200,
152 Table_Name => "Unchecked_Conversions");
154 --------------------------------------
155 -- Alignment_Check_For_Esize_Change --
156 --------------------------------------
158 procedure Alignment_Check_For_Esize_Change (Typ : Entity_Id) is
160 -- If the alignment is known, and not set by a rep clause, and is
161 -- inconsistent with the size being set, then reset it to unknown,
162 -- we assume in this case that the size overrides the inherited
163 -- alignment, and that the alignment must be recomputed.
165 if Known_Alignment (Typ)
166 and then not Has_Alignment_Clause (Typ)
167 and then Esize (Typ) mod (Alignment (Typ) * SSU) /= 0
169 Init_Alignment (Typ);
171 end Alignment_Check_For_Esize_Change;
173 -----------------------
174 -- Analyze_At_Clause --
175 -----------------------
177 -- An at clause is replaced by the corresponding Address attribute
178 -- definition clause that is the preferred approach in Ada 95.
180 procedure Analyze_At_Clause (N : Node_Id) is
183 Make_Attribute_Definition_Clause (Sloc (N),
184 Name => Identifier (N),
185 Chars => Name_Address,
186 Expression => Expression (N)));
187 Analyze_Attribute_Definition_Clause (N);
188 end Analyze_At_Clause;
190 -----------------------------------------
191 -- Analyze_Attribute_Definition_Clause --
192 -----------------------------------------
194 procedure Analyze_Attribute_Definition_Clause (N : Node_Id) is
195 Loc : constant Source_Ptr := Sloc (N);
196 Nam : constant Node_Id := Name (N);
197 Attr : constant Name_Id := Chars (N);
198 Expr : constant Node_Id := Expression (N);
199 Id : constant Attribute_Id := Get_Attribute_Id (Attr);
203 FOnly : Boolean := False;
204 -- Reset to True for subtype specific attribute (Alignment, Size)
205 -- and for stream attributes, i.e. those cases where in the call
206 -- to Rep_Item_Too_Late, FOnly is set True so that only the freezing
207 -- rules are checked. Note that the case of stream attributes is not
208 -- clear from the RM, but see AI95-00137. Also, the RM seems to
209 -- disallow Storage_Size for derived task types, but that is also
210 -- clearly unintentional.
216 if Rep_Item_Too_Early (Ent, N) then
220 -- Rep clause applies to full view of incomplete type or private type
221 -- if we have one (if not, this is a premature use of the type).
222 -- However, certain semantic checks need to be done on the specified
223 -- entity (i.e. the private view), so we save it in Ent.
225 if Is_Private_Type (Ent)
226 and then Is_Derived_Type (Ent)
227 and then not Is_Tagged_Type (Ent)
228 and then No (Full_View (Ent))
230 -- If this is a private type whose completion is a derivation
231 -- from another private type, there is no full view, and the
232 -- attribute belongs to the type itself, not its underlying parent.
236 elsif Ekind (Ent) = E_Incomplete_Type then
237 Ent := Underlying_Type (Ent);
240 U_Ent := Underlying_Type (Ent);
243 -- Complete other routine error checks
245 if Etype (Nam) = Any_Type then
248 elsif Scope (Ent) /= Current_Scope then
249 Error_Msg_N ("entity must be declared in this scope", Nam);
252 elsif Is_Type (U_Ent)
253 and then not Is_First_Subtype (U_Ent)
254 and then Id /= Attribute_Object_Size
255 and then Id /= Attribute_Value_Size
256 and then not From_At_Mod (N)
258 Error_Msg_N ("cannot specify attribute for subtype", Nam);
263 -- Switch on particular attribute
271 -- Address attribute definition clause
273 when Attribute_Address => Address : begin
274 Analyze_And_Resolve (Expr, RTE (RE_Address));
276 if Present (Address_Clause (U_Ent)) then
277 Error_Msg_N ("address already given for &", Nam);
279 -- Case of address clause for subprogram
281 elsif Is_Subprogram (U_Ent) then
283 if Has_Homonym (U_Ent) then
285 ("address clause cannot be given " &
286 "for overloaded subprogram",
290 -- For subprograms, all address clauses are permitted,
291 -- and we mark the subprogram as having a deferred freeze
292 -- so that Gigi will not elaborate it too soon.
294 -- Above needs more comments, what is too soon about???
296 Set_Has_Delayed_Freeze (U_Ent);
298 -- Case of address clause for entry
300 elsif Ekind (U_Ent) = E_Entry then
302 if Nkind (Parent (N)) = N_Task_Body then
304 ("entry address must be specified in task spec", Nam);
307 -- For entries, we require a constant address
309 Check_Constant_Address_Clause (Expr, U_Ent);
311 -- Case of address clause for an object
314 Ekind (U_Ent) = E_Variable
316 Ekind (U_Ent) = E_Constant
319 Decl : constant Node_Id := Declaration_Node (U_Ent);
320 Expr : constant Node_Id := Expression (N);
321 Typ : constant Entity_Id := Etype (U_Ent);
324 -- Exported variables cannot have an address clause,
325 -- because this cancels the effect of the pragma Export
327 if Is_Exported (U_Ent) then
329 ("cannot export object with address clause", Nam);
331 -- Imported variables can have an address clause, but then
332 -- the import is pretty meaningless except to suppress
333 -- initializations, so we do not need such variables to
334 -- be statically allocated (and in fact it causes trouble
335 -- if the address clause is a local value).
337 elsif Is_Imported (U_Ent) then
338 Set_Is_Statically_Allocated (U_Ent, False);
341 -- We mark a possible modification of a variable with an
342 -- address clause, since it is likely aliasing is occurring.
344 Note_Possible_Modification (Nam);
346 -- If we have no initialization of any kind, then we can
347 -- safely defer the elaboration of the variable to its
348 -- freezing point, so that the address clause will be
349 -- computed at the proper point.
351 -- The same processing applies to all initialized scalar
352 -- types and all access types. Packed bit arrays of size
353 -- up to 64 are represented using a modular type with an
354 -- initialization (to zero) and can be processed like
355 -- other initialized scalar types.
357 if (No (Expression (Decl))
358 and then not Has_Non_Null_Base_Init_Proc (Typ))
361 (Present (Expression (Decl))
362 and then Is_Scalar_Type (Typ))
368 (Is_Bit_Packed_Array (Base_Type (Typ))
370 Is_Modular_Integer_Type (Packed_Array_Type (Typ)))
372 Set_Has_Delayed_Freeze (U_Ent);
374 -- Otherwise, we require the address clause to be constant
377 Check_Constant_Address_Clause (Expr, U_Ent);
380 if Is_Exported (U_Ent) then
382 ("& cannot be exported if an address clause is given",
385 ("\define and export a variable " &
386 "that holds its address instead",
390 if not Error_Posted (Expr) then
391 Warn_Overlay (Expr, Typ, Nam);
394 -- If entity has delayed freeze then we will generate
395 -- an alignment check at the freeze point. If there is
396 -- no delayed freeze we can do it right now.
398 if not Has_Delayed_Freeze (U_Ent) then
399 Apply_Alignment_Check (U_Ent, N);
402 -- Kill the size check code, since we are not allocating
403 -- the variable, it is somewhere else.
405 Kill_Size_Check_Code (U_Ent);
408 -- Not a valid entity for an address clause
411 Error_Msg_N ("address cannot be given for &", Nam);
419 -- Alignment attribute definition clause
421 when Attribute_Alignment => Alignment_Block : declare
422 Align : Uint := Get_Alignment_Value (Expr);
427 if not Is_Type (U_Ent)
428 and then Ekind (U_Ent) /= E_Variable
429 and then Ekind (U_Ent) /= E_Constant
431 Error_Msg_N ("alignment cannot be given for &", Nam);
433 elsif Has_Alignment_Clause (U_Ent) then
434 Error_Msg_Sloc := Sloc (Alignment_Clause (U_Ent));
435 Error_Msg_N ("alignment clause previously given#", N);
437 elsif Align /= No_Uint then
438 Set_Has_Alignment_Clause (U_Ent);
439 Set_Alignment (U_Ent, Align);
447 -- Bit_Order attribute definition clause
449 when Attribute_Bit_Order => Bit_Order : declare
451 if not Is_Record_Type (U_Ent) then
453 ("Bit_Order can only be defined for record type", Nam);
456 Analyze_And_Resolve (Expr, RTE (RE_Bit_Order));
458 if Etype (Expr) = Any_Type then
461 elsif not Is_Static_Expression (Expr) then
462 Error_Msg_N ("Bit_Order requires static expression", Expr);
465 if (Expr_Value (Expr) = 0) /= Bytes_Big_Endian then
466 Set_Reverse_Bit_Order (U_Ent, True);
476 -- Component_Size attribute definition clause
478 when Attribute_Component_Size => Component_Size_Case : declare
479 Csize : constant Uint := Static_Integer (Expr);
482 New_Ctyp : Entity_Id;
486 if not Is_Array_Type (U_Ent) then
487 Error_Msg_N ("component size requires array type", Nam);
491 Btype := Base_Type (U_Ent);
493 if Has_Component_Size_Clause (Btype) then
495 ("component size clase for& previously given", Nam);
497 elsif Csize /= No_Uint then
498 Check_Size (Expr, Component_Type (Btype), Csize, Biased);
500 if Has_Aliased_Components (Btype)
506 ("component size incorrect for aliased components", N);
510 -- For the biased case, build a declaration for a subtype
511 -- that will be used to represent the biased subtype that
512 -- reflects the biased representation of components. We need
513 -- this subtype to get proper conversions on referencing
514 -- elements of the array.
518 Make_Defining_Identifier (Loc,
519 Chars => New_External_Name (Chars (U_Ent), 'C', 0, 'T'));
522 Make_Subtype_Declaration (Loc,
523 Defining_Identifier => New_Ctyp,
524 Subtype_Indication =>
525 New_Occurrence_Of (Component_Type (Btype), Loc));
527 Set_Parent (Decl, N);
528 Analyze (Decl, Suppress => All_Checks);
530 Set_Has_Delayed_Freeze (New_Ctyp, False);
531 Set_Esize (New_Ctyp, Csize);
532 Set_RM_Size (New_Ctyp, Csize);
533 Init_Alignment (New_Ctyp);
534 Set_Has_Biased_Representation (New_Ctyp, True);
535 Set_Is_Itype (New_Ctyp, True);
536 Set_Associated_Node_For_Itype (New_Ctyp, U_Ent);
538 Set_Component_Type (Btype, New_Ctyp);
541 Set_Component_Size (Btype, Csize);
542 Set_Has_Component_Size_Clause (Btype, True);
543 Set_Has_Non_Standard_Rep (Btype, True);
545 end Component_Size_Case;
551 when Attribute_External_Tag => External_Tag :
553 if not Is_Tagged_Type (U_Ent) then
554 Error_Msg_N ("should be a tagged type", Nam);
557 Analyze_And_Resolve (Expr, Standard_String);
559 if not Is_Static_Expression (Expr) then
560 Error_Msg_N ("must be a static string", Nam);
563 Set_Has_External_Tag_Rep_Clause (U_Ent);
570 when Attribute_Input => Input : declare
571 Subp : Entity_Id := Empty;
576 function Has_Good_Profile (Subp : Entity_Id) return Boolean;
577 -- Return true if the entity is a function with an appropriate
578 -- profile for the Input attribute.
580 function Has_Good_Profile (Subp : Entity_Id) return Boolean is
582 Ok : Boolean := False;
585 if Ekind (Subp) = E_Function then
586 F := First_Formal (Subp);
588 if Present (F) and then No (Next_Formal (F)) then
589 if Ekind (Etype (F)) = E_Anonymous_Access_Type
591 Designated_Type (Etype (F)) =
592 Class_Wide_Type (RTE (RE_Root_Stream_Type))
594 Ok := Base_Type (Etype (Subp)) = Base_Type (Ent);
600 end Has_Good_Profile;
602 -- Start of processing for Input attribute definition
607 if not Is_Type (U_Ent) then
608 Error_Msg_N ("local name must be a subtype", Nam);
612 Pnam := TSS (Base_Type (U_Ent), Name_uInput);
615 and then Base_Type (Etype (Pnam)) = Base_Type (U_Ent)
617 Error_Msg_Sloc := Sloc (Pnam);
618 Error_Msg_N ("input attribute already defined #", Nam);
625 if Is_Entity_Name (Expr) then
626 if not Is_Overloaded (Expr) then
627 if Has_Good_Profile (Entity (Expr)) then
628 Subp := Entity (Expr);
632 Get_First_Interp (Expr, I, It);
634 while Present (It.Nam) loop
635 if Has_Good_Profile (It.Nam) then
640 Get_Next_Interp (I, It);
645 if Present (Subp) then
646 Set_Entity (Expr, Subp);
647 Set_Etype (Expr, Etype (Subp));
648 New_Stream_Function (N, U_Ent, Subp, Name_uInput);
650 Error_Msg_N ("incorrect expression for input attribute", Expr);
659 -- Machine radix attribute definition clause
661 when Attribute_Machine_Radix => Machine_Radix : declare
662 Radix : constant Uint := Static_Integer (Expr);
665 if not Is_Decimal_Fixed_Point_Type (U_Ent) then
666 Error_Msg_N ("decimal fixed-point type expected for &", Nam);
668 elsif Has_Machine_Radix_Clause (U_Ent) then
669 Error_Msg_Sloc := Sloc (Alignment_Clause (U_Ent));
670 Error_Msg_N ("machine radix clause previously given#", N);
672 elsif Radix /= No_Uint then
673 Set_Has_Machine_Radix_Clause (U_Ent);
674 Set_Has_Non_Standard_Rep (Base_Type (U_Ent));
678 elsif Radix = 10 then
679 Set_Machine_Radix_10 (U_Ent);
681 Error_Msg_N ("machine radix value must be 2 or 10", Expr);
690 -- Object_Size attribute definition clause
692 when Attribute_Object_Size => Object_Size : declare
693 Size : constant Uint := Static_Integer (Expr);
697 if not Is_Type (U_Ent) then
698 Error_Msg_N ("Object_Size cannot be given for &", Nam);
700 elsif Has_Object_Size_Clause (U_Ent) then
701 Error_Msg_N ("Object_Size already given for &", Nam);
704 Check_Size (Expr, U_Ent, Size, Biased);
712 UI_Mod (Size, 64) /= 0
715 ("Object_Size must be 8, 16, 32, or multiple of 64",
719 Set_Esize (U_Ent, Size);
720 Set_Has_Object_Size_Clause (U_Ent);
721 Alignment_Check_For_Esize_Change (U_Ent);
729 when Attribute_Output => Output : declare
730 Subp : Entity_Id := Empty;
735 function Has_Good_Profile (Subp : Entity_Id) return Boolean;
736 -- Return true if the entity is a procedure with an
737 -- appropriate profile for the output attribute.
739 function Has_Good_Profile (Subp : Entity_Id) return Boolean is
741 Ok : Boolean := False;
744 if Ekind (Subp) = E_Procedure then
745 F := First_Formal (Subp);
748 if Ekind (Etype (F)) = E_Anonymous_Access_Type
750 Designated_Type (Etype (F)) =
751 Class_Wide_Type (RTE (RE_Root_Stream_Type))
755 and then Parameter_Mode (F) = E_In_Parameter
756 and then Base_Type (Etype (F)) = Base_Type (Ent)
757 and then No (Next_Formal (F));
763 end Has_Good_Profile;
768 if not Is_Type (U_Ent) then
769 Error_Msg_N ("local name must be a subtype", Nam);
773 Pnam := TSS (Base_Type (U_Ent), Name_uOutput);
777 Base_Type (Etype (Next_Formal (First_Formal (Pnam))))
780 Error_Msg_Sloc := Sloc (Pnam);
781 Error_Msg_N ("output attribute already defined #", Nam);
788 if Is_Entity_Name (Expr) then
789 if not Is_Overloaded (Expr) then
790 if Has_Good_Profile (Entity (Expr)) then
791 Subp := Entity (Expr);
795 Get_First_Interp (Expr, I, It);
797 while Present (It.Nam) loop
798 if Has_Good_Profile (It.Nam) then
803 Get_Next_Interp (I, It);
808 if Present (Subp) then
809 Set_Entity (Expr, Subp);
810 Set_Etype (Expr, Etype (Subp));
811 New_Stream_Procedure (N, U_Ent, Subp, Name_uOutput);
813 Error_Msg_N ("incorrect expression for output attribute", Expr);
822 when Attribute_Read => Read : declare
823 Subp : Entity_Id := Empty;
828 function Has_Good_Profile (Subp : Entity_Id) return Boolean;
829 -- Return true if the entity is a procedure with an appropriate
830 -- profile for the Read attribute.
832 function Has_Good_Profile (Subp : Entity_Id) return Boolean is
834 Ok : Boolean := False;
837 if Ekind (Subp) = E_Procedure then
838 F := First_Formal (Subp);
841 if Ekind (Etype (F)) = E_Anonymous_Access_Type
843 Designated_Type (Etype (F)) =
844 Class_Wide_Type (RTE (RE_Root_Stream_Type))
848 and then Parameter_Mode (F) = E_Out_Parameter
849 and then Base_Type (Etype (F)) = Base_Type (Ent)
850 and then No (Next_Formal (F));
856 end Has_Good_Profile;
858 -- Start of processing for Read attribute definition
863 if not Is_Type (U_Ent) then
864 Error_Msg_N ("local name must be a subtype", Nam);
868 Pnam := TSS (Base_Type (U_Ent), Name_uRead);
871 and then Base_Type (Etype (Next_Formal (First_Formal (Pnam))))
874 Error_Msg_Sloc := Sloc (Pnam);
875 Error_Msg_N ("read attribute already defined #", Nam);
882 if Is_Entity_Name (Expr) then
883 if not Is_Overloaded (Expr) then
884 if Has_Good_Profile (Entity (Expr)) then
885 Subp := Entity (Expr);
889 Get_First_Interp (Expr, I, It);
891 while Present (It.Nam) loop
892 if Has_Good_Profile (It.Nam) then
897 Get_Next_Interp (I, It);
902 if Present (Subp) then
903 Set_Entity (Expr, Subp);
904 Set_Etype (Expr, Etype (Subp));
905 New_Stream_Procedure (N, U_Ent, Subp, Name_uRead, True);
907 Error_Msg_N ("incorrect expression for read attribute", Expr);
916 -- Size attribute definition clause
918 when Attribute_Size => Size : declare
919 Size : constant Uint := Static_Integer (Expr);
926 if Has_Size_Clause (U_Ent) then
927 Error_Msg_N ("size already given for &", Nam);
929 elsif not Is_Type (U_Ent)
930 and then Ekind (U_Ent) /= E_Variable
931 and then Ekind (U_Ent) /= E_Constant
933 Error_Msg_N ("size cannot be given for &", Nam);
935 elsif Is_Array_Type (U_Ent)
936 and then not Is_Constrained (U_Ent)
939 ("size cannot be given for unconstrained array", Nam);
941 elsif Size /= No_Uint then
943 if Is_Type (U_Ent) then
946 Etyp := Etype (U_Ent);
949 -- Check size, note that Gigi is in charge of checking
950 -- that the size of an array or record type is OK. Also
951 -- we do not check the size in the ordinary fixed-point
952 -- case, since it is too early to do so (there may be a
953 -- subsequent small clause that affects the size). We can
954 -- check the size if a small clause has already been given.
956 if not Is_Ordinary_Fixed_Point_Type (U_Ent)
957 or else Has_Small_Clause (U_Ent)
959 Check_Size (Expr, Etyp, Size, Biased);
960 Set_Has_Biased_Representation (U_Ent, Biased);
963 -- For types set RM_Size and Esize if possible
965 if Is_Type (U_Ent) then
966 Set_RM_Size (U_Ent, Size);
968 -- For scalar types, increase Object_Size to power of 2,
969 -- but not less than 8 in any case, i.e. byte addressable.
971 if Is_Scalar_Type (U_Ent) then
973 Init_Esize (U_Ent, 8);
974 elsif Size <= 16 then
975 Init_Esize (U_Ent, 16);
976 elsif Size <= 32 then
977 Init_Esize (U_Ent, 32);
979 Set_Esize (U_Ent, (Size + 63) / 64 * 64);
982 -- For all other types, object size = value size. The
983 -- backend will adjust as needed.
986 Set_Esize (U_Ent, Size);
989 Alignment_Check_For_Esize_Change (U_Ent);
991 -- For objects, set Esize only
994 Set_Esize (U_Ent, Size);
997 Set_Has_Size_Clause (U_Ent);
1005 -- Small attribute definition clause
1007 when Attribute_Small => Small : declare
1008 Implicit_Base : constant Entity_Id := Base_Type (U_Ent);
1012 Analyze_And_Resolve (Expr, Any_Real);
1014 if Etype (Expr) = Any_Type then
1017 elsif not Is_Static_Expression (Expr) then
1018 Error_Msg_N ("small requires static expression", Expr);
1022 Small := Expr_Value_R (Expr);
1024 if Small <= Ureal_0 then
1025 Error_Msg_N ("small value must be greater than zero", Expr);
1031 if not Is_Ordinary_Fixed_Point_Type (U_Ent) then
1033 ("small requires an ordinary fixed point type", Nam);
1035 elsif Has_Small_Clause (U_Ent) then
1036 Error_Msg_N ("small already given for &", Nam);
1038 elsif Small > Delta_Value (U_Ent) then
1040 ("small value must not be greater then delta value", Nam);
1043 Set_Small_Value (U_Ent, Small);
1044 Set_Small_Value (Implicit_Base, Small);
1045 Set_Has_Small_Clause (U_Ent);
1046 Set_Has_Small_Clause (Implicit_Base);
1047 Set_Has_Non_Standard_Rep (Implicit_Base);
1055 -- Storage_Size attribute definition clause
1057 when Attribute_Storage_Size => Storage_Size : declare
1058 Btype : constant Entity_Id := Base_Type (U_Ent);
1062 if Is_Task_Type (U_Ent) then
1066 if not Is_Access_Type (U_Ent)
1067 and then Ekind (U_Ent) /= E_Task_Type
1069 Error_Msg_N ("storage size cannot be given for &", Nam);
1071 elsif Is_Access_Type (U_Ent) and Is_Derived_Type (U_Ent) then
1073 ("storage size cannot be given for a derived access type",
1076 elsif Has_Storage_Size_Clause (Btype) then
1077 Error_Msg_N ("storage size already given for &", Nam);
1080 Analyze_And_Resolve (Expr, Any_Integer);
1082 if Is_Access_Type (U_Ent) then
1084 if Present (Associated_Storage_Pool (U_Ent)) then
1085 Error_Msg_N ("storage pool already given for &", Nam);
1089 if Compile_Time_Known_Value (Expr)
1090 and then Expr_Value (Expr) = 0
1092 Set_No_Pool_Assigned (Btype);
1095 else -- Is_Task_Type (U_Ent)
1096 Sprag := Get_Rep_Pragma (Btype, Name_Storage_Size);
1098 if Present (Sprag) then
1099 Error_Msg_Sloc := Sloc (Sprag);
1101 ("Storage_Size already specified#", Nam);
1106 Set_Has_Storage_Size_Clause (Btype);
1114 -- Storage_Pool attribute definition clause
1116 when Attribute_Storage_Pool => Storage_Pool : declare
1120 if Ekind (U_Ent) /= E_Access_Type
1121 and then Ekind (U_Ent) /= E_General_Access_Type
1124 "storage pool can only be given for access types", Nam);
1127 elsif Is_Derived_Type (U_Ent) then
1129 ("storage pool cannot be given for a derived access type",
1132 elsif Has_Storage_Size_Clause (U_Ent) then
1133 Error_Msg_N ("storage size already given for &", Nam);
1136 elsif Present (Associated_Storage_Pool (U_Ent)) then
1137 Error_Msg_N ("storage pool already given for &", Nam);
1142 (Expr, Class_Wide_Type (RTE (RE_Root_Storage_Pool)));
1144 -- If the argument is a name that is not an entity name, then
1145 -- we construct a renaming operation to define an entity of
1146 -- type storage pool.
1148 if not Is_Entity_Name (Expr)
1149 and then Is_Object_Reference (Expr)
1152 Make_Defining_Identifier (Loc,
1153 Chars => New_Internal_Name ('P'));
1156 Rnode : constant Node_Id :=
1157 Make_Object_Renaming_Declaration (Loc,
1158 Defining_Identifier => Pool,
1160 New_Occurrence_Of (Etype (Expr), Loc),
1164 Insert_Before (N, Rnode);
1166 Set_Associated_Storage_Pool (U_Ent, Pool);
1169 elsif Is_Entity_Name (Expr) then
1170 Pool := Entity (Expr);
1172 -- If pool is a renamed object, get original one. This can
1173 -- happen with an explicit renaming, and within instances.
1175 while Present (Renamed_Object (Pool))
1176 and then Is_Entity_Name (Renamed_Object (Pool))
1178 Pool := Entity (Renamed_Object (Pool));
1181 if Present (Renamed_Object (Pool))
1182 and then Nkind (Renamed_Object (Pool)) = N_Type_Conversion
1183 and then Is_Entity_Name (Expression (Renamed_Object (Pool)))
1185 Pool := Entity (Expression (Renamed_Object (Pool)));
1188 if Present (Etype (Pool))
1189 and then Etype (Pool) /= RTE (RE_Stack_Bounded_Pool)
1190 and then Etype (Pool) /= RTE (RE_Unbounded_Reclaim_Pool)
1192 Set_Associated_Storage_Pool (U_Ent, Pool);
1194 Error_Msg_N ("Non sharable GNAT Pool", Expr);
1197 -- The pool may be specified as the Storage_Pool of some other
1198 -- type. It is rewritten as a class_wide conversion of the
1199 -- corresponding pool entity.
1201 elsif Nkind (Expr) = N_Type_Conversion
1202 and then Is_Entity_Name (Expression (Expr))
1203 and then Nkind (Original_Node (Expr)) = N_Attribute_Reference
1205 Pool := Entity (Expression (Expr));
1207 if Present (Etype (Pool))
1208 and then Etype (Pool) /= RTE (RE_Stack_Bounded_Pool)
1209 and then Etype (Pool) /= RTE (RE_Unbounded_Reclaim_Pool)
1211 Set_Associated_Storage_Pool (U_Ent, Pool);
1213 Error_Msg_N ("Non sharable GNAT Pool", Expr);
1217 Error_Msg_N ("incorrect reference to a Storage Pool", Expr);
1226 -- Value_Size attribute definition clause
1228 when Attribute_Value_Size => Value_Size : declare
1229 Size : constant Uint := Static_Integer (Expr);
1233 if not Is_Type (U_Ent) then
1234 Error_Msg_N ("Value_Size cannot be given for &", Nam);
1237 (Get_Attribute_Definition_Clause
1238 (U_Ent, Attribute_Value_Size))
1240 Error_Msg_N ("Value_Size already given for &", Nam);
1243 if Is_Elementary_Type (U_Ent) then
1244 Check_Size (Expr, U_Ent, Size, Biased);
1245 Set_Has_Biased_Representation (U_Ent, Biased);
1248 Set_RM_Size (U_Ent, Size);
1256 -- Write attribute definition clause
1257 -- check for class-wide case will be performed later
1259 when Attribute_Write => Write : declare
1260 Subp : Entity_Id := Empty;
1265 function Has_Good_Profile (Subp : Entity_Id) return Boolean;
1266 -- Return true if the entity is a procedure with an
1267 -- appropriate profile for the write attribute.
1269 function Has_Good_Profile (Subp : Entity_Id) return Boolean is
1271 Ok : Boolean := False;
1274 if Ekind (Subp) = E_Procedure then
1275 F := First_Formal (Subp);
1278 if Ekind (Etype (F)) = E_Anonymous_Access_Type
1280 Designated_Type (Etype (F)) =
1281 Class_Wide_Type (RTE (RE_Root_Stream_Type))
1285 and then Parameter_Mode (F) = E_In_Parameter
1286 and then Base_Type (Etype (F)) = Base_Type (Ent)
1287 and then No (Next_Formal (F));
1293 end Has_Good_Profile;
1295 -- Start of processing for Write attribute definition
1300 if not Is_Type (U_Ent) then
1301 Error_Msg_N ("local name must be a subtype", Nam);
1305 Pnam := TSS (Base_Type (U_Ent), Name_uWrite);
1308 and then Base_Type (Etype (Next_Formal (First_Formal (Pnam))))
1311 Error_Msg_Sloc := Sloc (Pnam);
1312 Error_Msg_N ("write attribute already defined #", Nam);
1318 if Is_Entity_Name (Expr) then
1319 if not Is_Overloaded (Expr) then
1320 if Has_Good_Profile (Entity (Expr)) then
1321 Subp := Entity (Expr);
1325 Get_First_Interp (Expr, I, It);
1327 while Present (It.Nam) loop
1328 if Has_Good_Profile (It.Nam) then
1333 Get_Next_Interp (I, It);
1338 if Present (Subp) then
1339 Set_Entity (Expr, Subp);
1340 Set_Etype (Expr, Etype (Subp));
1341 New_Stream_Procedure (N, U_Ent, Subp, Name_uWrite);
1343 Error_Msg_N ("incorrect expression for write attribute", Expr);
1348 -- All other attributes cannot be set
1352 ("attribute& cannot be set with definition clause", N);
1356 -- The test for the type being frozen must be performed after
1357 -- any expression the clause has been analyzed since the expression
1358 -- itself might cause freezing that makes the clause illegal.
1360 if Rep_Item_Too_Late (U_Ent, N, FOnly) then
1363 end Analyze_Attribute_Definition_Clause;
1365 ----------------------------
1366 -- Analyze_Code_Statement --
1367 ----------------------------
1369 procedure Analyze_Code_Statement (N : Node_Id) is
1370 HSS : constant Node_Id := Parent (N);
1371 SBody : constant Node_Id := Parent (HSS);
1372 Subp : constant Entity_Id := Current_Scope;
1379 -- Analyze and check we get right type, note that this implements the
1380 -- requirement (RM 13.8(1)) that Machine_Code be with'ed, since that
1381 -- is the only way that Asm_Insn could possibly be visible.
1383 Analyze_And_Resolve (Expression (N));
1385 if Etype (Expression (N)) = Any_Type then
1387 elsif Etype (Expression (N)) /= RTE (RE_Asm_Insn) then
1388 Error_Msg_N ("incorrect type for code statement", N);
1392 -- Make sure we appear in the handled statement sequence of a
1393 -- subprogram (RM 13.8(3)).
1395 if Nkind (HSS) /= N_Handled_Sequence_Of_Statements
1396 or else Nkind (SBody) /= N_Subprogram_Body
1399 ("code statement can only appear in body of subprogram", N);
1403 -- Do remaining checks (RM 13.8(3)) if not already done
1405 if not Is_Machine_Code_Subprogram (Subp) then
1406 Set_Is_Machine_Code_Subprogram (Subp);
1408 -- No exception handlers allowed
1410 if Present (Exception_Handlers (HSS)) then
1412 ("exception handlers not permitted in machine code subprogram",
1413 First (Exception_Handlers (HSS)));
1416 -- No declarations other than use clauses and pragmas (we allow
1417 -- certain internally generated declarations as well).
1419 Decl := First (Declarations (SBody));
1420 while Present (Decl) loop
1421 DeclO := Original_Node (Decl);
1422 if Comes_From_Source (DeclO)
1423 and then Nkind (DeclO) /= N_Pragma
1424 and then Nkind (DeclO) /= N_Use_Package_Clause
1425 and then Nkind (DeclO) /= N_Use_Type_Clause
1426 and then Nkind (DeclO) /= N_Implicit_Label_Declaration
1429 ("this declaration not allowed in machine code subprogram",
1436 -- No statements other than code statements, pragmas, and labels.
1437 -- Again we allow certain internally generated statements.
1439 Stmt := First (Statements (HSS));
1440 while Present (Stmt) loop
1441 StmtO := Original_Node (Stmt);
1442 if Comes_From_Source (StmtO)
1443 and then Nkind (StmtO) /= N_Pragma
1444 and then Nkind (StmtO) /= N_Label
1445 and then Nkind (StmtO) /= N_Code_Statement
1448 ("this statement is not allowed in machine code subprogram",
1456 end Analyze_Code_Statement;
1458 -----------------------------------------------
1459 -- Analyze_Enumeration_Representation_Clause --
1460 -----------------------------------------------
1462 procedure Analyze_Enumeration_Representation_Clause (N : Node_Id) is
1463 Ident : constant Node_Id := Identifier (N);
1464 Aggr : constant Node_Id := Array_Aggregate (N);
1465 Enumtype : Entity_Id;
1471 Err : Boolean := False;
1473 Lo : constant Uint := Expr_Value (Type_Low_Bound (Universal_Integer));
1474 Hi : constant Uint := Expr_Value (Type_High_Bound (Universal_Integer));
1479 -- First some basic error checks
1482 Enumtype := Entity (Ident);
1484 if Enumtype = Any_Type
1485 or else Rep_Item_Too_Early (Enumtype, N)
1489 Enumtype := Underlying_Type (Enumtype);
1492 if not Is_Enumeration_Type (Enumtype) then
1494 ("enumeration type required, found}",
1495 Ident, First_Subtype (Enumtype));
1499 if Scope (Enumtype) /= Current_Scope then
1500 Error_Msg_N ("type must be declared in this scope", Ident);
1503 elsif not Is_First_Subtype (Enumtype) then
1504 Error_Msg_N ("cannot give enumeration rep clause for subtype", N);
1507 elsif Has_Enumeration_Rep_Clause (Enumtype) then
1508 Error_Msg_N ("duplicate enumeration rep clause ignored", N);
1511 elsif Root_Type (Enumtype) = Standard_Character
1512 or else Root_Type (Enumtype) = Standard_Wide_Character
1514 Error_Msg_N ("enumeration rep clause not allowed for this type", N);
1517 Set_Has_Enumeration_Rep_Clause (Enumtype);
1518 Set_Has_Enumeration_Rep_Clause (Base_Type (Enumtype));
1521 -- Now we process the aggregate. Note that we don't use the normal
1522 -- aggregate code for this purpose, because we don't want any of the
1523 -- normal expansion activities, and a number of special semantic
1524 -- rules apply (including the component type being any integer type)
1526 -- Badent signals that we found some incorrect entries processing
1527 -- the list. The final checks for completeness and ordering are
1528 -- skipped in this case.
1530 Elit := First_Literal (Enumtype);
1532 -- First the positional entries if any
1534 if Present (Expressions (Aggr)) then
1535 Expr := First (Expressions (Aggr));
1536 while Present (Expr) loop
1538 Error_Msg_N ("too many entries in aggregate", Expr);
1542 Val := Static_Integer (Expr);
1544 if Val = No_Uint then
1547 elsif Val < Lo or else Hi < Val then
1548 Error_Msg_N ("value outside permitted range", Expr);
1552 Set_Enumeration_Rep (Elit, Val);
1553 Set_Enumeration_Rep_Expr (Elit, Expr);
1559 -- Now process the named entries if present
1561 if Present (Component_Associations (Aggr)) then
1562 Assoc := First (Component_Associations (Aggr));
1563 while Present (Assoc) loop
1564 Choice := First (Choices (Assoc));
1566 if Present (Next (Choice)) then
1568 ("multiple choice not allowed here", Next (Choice));
1572 if Nkind (Choice) = N_Others_Choice then
1573 Error_Msg_N ("others choice not allowed here", Choice);
1576 elsif Nkind (Choice) = N_Range then
1577 -- ??? should allow zero/one element range here
1578 Error_Msg_N ("range not allowed here", Choice);
1582 Analyze_And_Resolve (Choice, Enumtype);
1584 if Is_Entity_Name (Choice)
1585 and then Is_Type (Entity (Choice))
1587 Error_Msg_N ("subtype name not allowed here", Choice);
1589 -- ??? should allow static subtype with zero/one entry
1591 elsif Etype (Choice) = Base_Type (Enumtype) then
1592 if not Is_Static_Expression (Choice) then
1594 ("non-static expression used for choice", Choice);
1598 Elit := Expr_Value_E (Choice);
1600 if Present (Enumeration_Rep_Expr (Elit)) then
1601 Error_Msg_Sloc := Sloc (Enumeration_Rep_Expr (Elit));
1603 ("representation for& previously given#",
1608 Set_Enumeration_Rep_Expr (Elit, Choice);
1610 Expr := Expression (Assoc);
1611 Val := Static_Integer (Expr);
1613 if Val = No_Uint then
1616 elsif Val < Lo or else Hi < Val then
1617 Error_Msg_N ("value outside permitted range", Expr);
1621 Set_Enumeration_Rep (Elit, Val);
1630 -- Aggregate is fully processed. Now we check that a full set of
1631 -- representations was given, and that they are in range and in order.
1632 -- These checks are only done if no other errors occurred.
1638 Elit := First_Literal (Enumtype);
1639 while Present (Elit) loop
1640 if No (Enumeration_Rep_Expr (Elit)) then
1641 Error_Msg_NE ("missing representation for&!", N, Elit);
1644 Val := Enumeration_Rep (Elit);
1646 if Min = No_Uint then
1650 if Val /= No_Uint then
1651 if Max /= No_Uint and then Val <= Max then
1653 ("enumeration value for& not ordered!",
1654 Enumeration_Rep_Expr (Elit), Elit);
1660 -- If there is at least one literal whose representation
1661 -- is not equal to the Pos value, then note that this
1662 -- enumeration type has a non-standard representation.
1664 if Val /= Enumeration_Pos (Elit) then
1665 Set_Has_Non_Standard_Rep (Base_Type (Enumtype));
1672 -- Now set proper size information
1675 Minsize : Uint := UI_From_Int (Minimum_Size (Enumtype));
1678 if Has_Size_Clause (Enumtype) then
1679 if Esize (Enumtype) >= Minsize then
1684 UI_From_Int (Minimum_Size (Enumtype, Biased => True));
1686 if Esize (Enumtype) < Minsize then
1687 Error_Msg_N ("previously given size is too small", N);
1690 Set_Has_Biased_Representation (Enumtype);
1695 Set_RM_Size (Enumtype, Minsize);
1696 Set_Enum_Esize (Enumtype);
1699 Set_RM_Size (Base_Type (Enumtype), RM_Size (Enumtype));
1700 Set_Esize (Base_Type (Enumtype), Esize (Enumtype));
1701 Set_Alignment (Base_Type (Enumtype), Alignment (Enumtype));
1705 -- We repeat the too late test in case it froze itself!
1707 if Rep_Item_Too_Late (Enumtype, N) then
1711 end Analyze_Enumeration_Representation_Clause;
1713 ----------------------------
1714 -- Analyze_Free_Statement --
1715 ----------------------------
1717 procedure Analyze_Free_Statement (N : Node_Id) is
1719 Analyze (Expression (N));
1720 end Analyze_Free_Statement;
1722 ------------------------------------------
1723 -- Analyze_Record_Representation_Clause --
1724 ------------------------------------------
1726 procedure Analyze_Record_Representation_Clause (N : Node_Id) is
1727 Loc : constant Source_Ptr := Sloc (N);
1728 Ident : constant Node_Id := Identifier (N);
1729 Rectype : Entity_Id;
1735 Hbit : Uint := Uint_0;
1740 Max_Bit_So_Far : Uint;
1741 -- Records the maximum bit position so far. If all field positoins
1742 -- are monotonically increasing, then we can skip the circuit for
1743 -- checking for overlap, since no overlap is possible.
1745 Overlap_Check_Required : Boolean;
1746 -- Used to keep track of whether or not an overlap check is required
1748 Ccount : Natural := 0;
1749 -- Number of component clauses in record rep clause
1753 Rectype := Entity (Ident);
1755 if Rectype = Any_Type
1756 or else Rep_Item_Too_Early (Rectype, N)
1760 Rectype := Underlying_Type (Rectype);
1763 -- First some basic error checks
1765 if not Is_Record_Type (Rectype) then
1767 ("record type required, found}", Ident, First_Subtype (Rectype));
1770 elsif Is_Unchecked_Union (Rectype) then
1772 ("record rep clause not allowed for Unchecked_Union", N);
1774 elsif Scope (Rectype) /= Current_Scope then
1775 Error_Msg_N ("type must be declared in this scope", N);
1778 elsif not Is_First_Subtype (Rectype) then
1779 Error_Msg_N ("cannot give record rep clause for subtype", N);
1782 elsif Has_Record_Rep_Clause (Rectype) then
1783 Error_Msg_N ("duplicate record rep clause ignored", N);
1786 elsif Rep_Item_Too_Late (Rectype, N) then
1790 if Present (Mod_Clause (N)) then
1792 Loc : constant Source_Ptr := Sloc (N);
1793 M : constant Node_Id := Mod_Clause (N);
1794 P : constant List_Id := Pragmas_Before (M);
1803 -- In Tree_Output mode, expansion is disabled, but we must
1804 -- convert the Mod clause into an alignment clause anyway, so
1805 -- that the back-end can compute and back-annotate properly the
1806 -- size and alignment of types that may include this record.
1808 if Operating_Mode = Check_Semantics
1809 and then Tree_Output
1812 Make_Attribute_Definition_Clause (Loc,
1813 Name => New_Reference_To (Base_Type (Rectype), Loc),
1814 Chars => Name_Alignment,
1815 Expression => Relocate_Node (Expression (M)));
1817 Set_From_At_Mod (AtM_Nod);
1818 Insert_After (N, AtM_Nod);
1819 Mod_Val := Get_Alignment_Value (Expression (AtM_Nod));
1820 Set_Mod_Clause (N, Empty);
1823 -- Get the alignment value to perform error checking
1825 Mod_Val := Get_Alignment_Value (Expression (M));
1831 -- Clear any existing component clauses for the type (this happens
1832 -- with derived types, where we are now overriding the original)
1834 Fent := First_Entity (Rectype);
1837 while Present (Comp) loop
1838 if Ekind (Comp) = E_Component
1839 or else Ekind (Comp) = E_Discriminant
1841 Set_Component_Clause (Comp, Empty);
1847 -- All done if no component clauses
1849 CC := First (Component_Clauses (N));
1855 -- If a tag is present, then create a component clause that places
1856 -- it at the start of the record (otherwise gigi may place it after
1857 -- other fields that have rep clauses).
1859 if Nkind (Fent) = N_Defining_Identifier
1860 and then Chars (Fent) = Name_uTag
1862 Set_Component_Bit_Offset (Fent, Uint_0);
1863 Set_Normalized_Position (Fent, Uint_0);
1864 Set_Normalized_First_Bit (Fent, Uint_0);
1865 Set_Normalized_Position_Max (Fent, Uint_0);
1866 Init_Esize (Fent, System_Address_Size);
1868 Set_Component_Clause (Fent,
1869 Make_Component_Clause (Loc,
1871 Make_Identifier (Loc,
1872 Chars => Name_uTag),
1875 Make_Integer_Literal (Loc,
1879 Make_Integer_Literal (Loc,
1883 Make_Integer_Literal (Loc,
1884 UI_From_Int (System_Address_Size))));
1886 Ccount := Ccount + 1;
1889 Set_Has_Record_Rep_Clause (Rectype);
1890 Set_Has_Specified_Layout (Rectype);
1892 -- A representation like this applies to the base type as well
1894 Set_Has_Record_Rep_Clause (Base_Type (Rectype));
1895 Set_Has_Non_Standard_Rep (Base_Type (Rectype));
1896 Set_Has_Specified_Layout (Base_Type (Rectype));
1898 Max_Bit_So_Far := Uint_Minus_1;
1899 Overlap_Check_Required := False;
1901 -- Process the component clauses
1903 while Present (CC) loop
1905 -- If pragma, just analyze it
1907 if Nkind (CC) = N_Pragma then
1910 -- Processing for real component clause
1913 Ccount := Ccount + 1;
1914 Posit := Static_Integer (Position (CC));
1915 Fbit := Static_Integer (First_Bit (CC));
1916 Lbit := Static_Integer (Last_Bit (CC));
1919 and then Fbit /= No_Uint
1920 and then Lbit /= No_Uint
1924 ("position cannot be negative", Position (CC));
1928 ("first bit cannot be negative", First_Bit (CC));
1930 -- Values look OK, so find the corresponding record component
1931 -- Even though the syntax allows an attribute reference for
1932 -- implementation-defined components, GNAT does not allow the
1933 -- tag to get an explicit position.
1935 elsif Nkind (Component_Name (CC)) = N_Attribute_Reference then
1937 if Attribute_Name (Component_Name (CC)) = Name_Tag then
1938 Error_Msg_N ("position of tag cannot be specified", CC);
1940 Error_Msg_N ("illegal component name", CC);
1944 Comp := First_Entity (Rectype);
1945 while Present (Comp) loop
1946 exit when Chars (Comp) = Chars (Component_Name (CC));
1952 -- Maybe component of base type that is absent from
1953 -- statically constrained first subtype.
1955 Comp := First_Entity (Base_Type (Rectype));
1956 while Present (Comp) loop
1957 exit when Chars (Comp) = Chars (Component_Name (CC));
1964 ("component clause is for non-existent field", CC);
1966 elsif Present (Component_Clause (Comp)) then
1967 Error_Msg_Sloc := Sloc (Component_Clause (Comp));
1969 ("component clause previously given#", CC);
1972 -- Update Fbit and Lbit to the actual bit number.
1974 Fbit := Fbit + UI_From_Int (SSU) * Posit;
1975 Lbit := Lbit + UI_From_Int (SSU) * Posit;
1977 if Fbit <= Max_Bit_So_Far then
1978 Overlap_Check_Required := True;
1980 Max_Bit_So_Far := Lbit;
1983 if Has_Size_Clause (Rectype)
1984 and then Esize (Rectype) <= Lbit
1987 ("bit number out of range of specified size",
1990 Set_Component_Clause (Comp, CC);
1991 Set_Component_Bit_Offset (Comp, Fbit);
1992 Set_Esize (Comp, 1 + (Lbit - Fbit));
1993 Set_Normalized_First_Bit (Comp, Fbit mod SSU);
1994 Set_Normalized_Position (Comp, Fbit / SSU);
1996 Set_Normalized_Position_Max
1997 (Fent, Normalized_Position (Fent));
1999 if Is_Tagged_Type (Rectype)
2000 and then Fbit < System_Address_Size
2003 ("component overlaps tag field of&",
2007 -- Test for large object that is not on a byte
2008 -- boundary, defined as a large packed array not
2009 -- represented by a modular type, or an object for
2010 -- which a size of greater than 64 bits is specified.
2012 if Fbit mod SSU /= 0 then
2013 if (Is_Packed_Array_Type (Etype (Comp))
2014 and then Is_Array_Type
2015 (Packed_Array_Type (Etype (Comp))))
2016 or else Esize (Etype (Comp)) > 64
2019 ("large component must be on byte boundary",
2024 -- This information is also set in the
2025 -- corresponding component of the base type,
2026 -- found by accessing the Original_Record_Component
2027 -- link if it is present.
2029 Ocomp := Original_Record_Component (Comp);
2036 (Component_Name (CC),
2041 Set_Has_Biased_Representation (Comp, Biased);
2043 if Present (Ocomp) then
2044 Set_Component_Clause (Ocomp, CC);
2045 Set_Component_Bit_Offset (Ocomp, Fbit);
2046 Set_Normalized_First_Bit (Ocomp, Fbit mod SSU);
2047 Set_Normalized_Position (Ocomp, Fbit / SSU);
2048 Set_Esize (Ocomp, 1 + (Lbit - Fbit));
2050 Set_Normalized_Position_Max
2051 (Ocomp, Normalized_Position (Ocomp));
2053 Set_Has_Biased_Representation
2054 (Ocomp, Has_Biased_Representation (Comp));
2057 if Esize (Comp) < 0 then
2058 Error_Msg_N ("component size is negative", CC);
2069 -- Now that we have processed all the component clauses, check for
2070 -- overlap. We have to leave this till last, since the components
2071 -- can appear in any arbitrary order in the representation clause.
2073 -- We do not need this check if all specified ranges were monotonic,
2074 -- as recorded by Overlap_Check_Required being False at this stage.
2076 -- This first section checks if there are any overlapping entries
2077 -- at all. It does this by sorting all entries and then seeing if
2078 -- there are any overlaps. If there are none, then that is decisive,
2079 -- but if there are overlaps, they may still be OK (they may result
2080 -- from fields in different variants).
2082 if Overlap_Check_Required then
2083 Overlap_Check1 : declare
2085 OC_Fbit : array (0 .. Ccount) of Uint;
2086 -- First-bit values for component clauses, the value is the
2087 -- offset of the first bit of the field from start of record.
2088 -- The zero entry is for use in sorting.
2090 OC_Lbit : array (0 .. Ccount) of Uint;
2091 -- Last-bit values for component clauses, the value is the
2092 -- offset of the last bit of the field from start of record.
2093 -- The zero entry is for use in sorting.
2095 OC_Count : Natural := 0;
2096 -- Count of entries in OC_Fbit and OC_Lbit
2098 function OC_Lt (Op1, Op2 : Natural) return Boolean;
2099 -- Compare routine for Sort (See GNAT.Heap_Sort_A)
2101 procedure OC_Move (From : Natural; To : Natural);
2102 -- Move routine for Sort (see GNAT.Heap_Sort_A)
2104 function OC_Lt (Op1, Op2 : Natural) return Boolean is
2106 return OC_Fbit (Op1) < OC_Fbit (Op2);
2109 procedure OC_Move (From : Natural; To : Natural) is
2111 OC_Fbit (To) := OC_Fbit (From);
2112 OC_Lbit (To) := OC_Lbit (From);
2116 CC := First (Component_Clauses (N));
2117 while Present (CC) loop
2118 if Nkind (CC) /= N_Pragma then
2119 Posit := Static_Integer (Position (CC));
2120 Fbit := Static_Integer (First_Bit (CC));
2121 Lbit := Static_Integer (Last_Bit (CC));
2124 and then Fbit /= No_Uint
2125 and then Lbit /= No_Uint
2127 OC_Count := OC_Count + 1;
2128 Posit := Posit * SSU;
2129 OC_Fbit (OC_Count) := Fbit + Posit;
2130 OC_Lbit (OC_Count) := Lbit + Posit;
2139 OC_Move'Unrestricted_Access,
2140 OC_Lt'Unrestricted_Access);
2142 Overlap_Check_Required := False;
2143 for J in 1 .. OC_Count - 1 loop
2144 if OC_Lbit (J) >= OC_Fbit (J + 1) then
2145 Overlap_Check_Required := True;
2152 -- If Overlap_Check_Required is still True, then we have to do
2153 -- the full scale overlap check, since we have at least two fields
2154 -- that do overlap, and we need to know if that is OK since they
2155 -- are in the same variant, or whether we have a definite problem
2157 if Overlap_Check_Required then
2158 Overlap_Check2 : declare
2159 C1_Ent, C2_Ent : Entity_Id;
2160 -- Entities of components being checked for overlap
2163 -- Component_List node whose Component_Items are being checked
2166 -- Component declaration for component being checked
2169 C1_Ent := First_Entity (Base_Type (Rectype));
2171 -- Loop through all components in record. For each component check
2172 -- for overlap with any of the preceding elements on the component
2173 -- list containing the component, and also, if the component is in
2174 -- a variant, check against components outside the case structure.
2175 -- This latter test is repeated recursively up the variant tree.
2177 Main_Component_Loop : while Present (C1_Ent) loop
2178 if Ekind (C1_Ent) /= E_Component
2179 and then Ekind (C1_Ent) /= E_Discriminant
2181 goto Continue_Main_Component_Loop;
2184 -- Skip overlap check if entity has no declaration node. This
2185 -- happens with discriminants in constrained derived types.
2186 -- Probably we are missing some checks as a result, but that
2187 -- does not seem terribly serious ???
2189 if No (Declaration_Node (C1_Ent)) then
2190 goto Continue_Main_Component_Loop;
2193 Clist := Parent (List_Containing (Declaration_Node (C1_Ent)));
2195 -- Loop through component lists that need checking. Check the
2196 -- current component list and all lists in variants above us.
2198 Component_List_Loop : loop
2200 -- If derived type definition, go to full declaration
2201 -- If at outer level, check discriminants if there are any
2203 if Nkind (Clist) = N_Derived_Type_Definition then
2204 Clist := Parent (Clist);
2207 -- Outer level of record definition, check discriminants
2209 if Nkind (Clist) = N_Full_Type_Declaration
2210 or else Nkind (Clist) = N_Private_Type_Declaration
2212 if Has_Discriminants (Defining_Identifier (Clist)) then
2214 First_Discriminant (Defining_Identifier (Clist));
2216 while Present (C2_Ent) loop
2217 exit when C1_Ent = C2_Ent;
2218 Check_Component_Overlap (C1_Ent, C2_Ent);
2219 Next_Discriminant (C2_Ent);
2223 -- Record extension case
2225 elsif Nkind (Clist) = N_Derived_Type_Definition then
2228 -- Otherwise check one component list
2231 Citem := First (Component_Items (Clist));
2233 while Present (Citem) loop
2234 if Nkind (Citem) = N_Component_Declaration then
2235 C2_Ent := Defining_Identifier (Citem);
2236 exit when C1_Ent = C2_Ent;
2237 Check_Component_Overlap (C1_Ent, C2_Ent);
2244 -- Check for variants above us (the parent of the Clist can
2245 -- be a variant, in which case its parent is a variant part,
2246 -- and the parent of the variant part is a component list
2247 -- whose components must all be checked against the current
2248 -- component for overlap.
2250 if Nkind (Parent (Clist)) = N_Variant then
2251 Clist := Parent (Parent (Parent (Clist)));
2253 -- Check for possible discriminant part in record, this is
2254 -- treated essentially as another level in the recursion.
2255 -- For this case we have the parent of the component list
2256 -- is the record definition, and its parent is the full
2257 -- type declaration which contains the discriminant
2260 elsif Nkind (Parent (Clist)) = N_Record_Definition then
2261 Clist := Parent (Parent ((Clist)));
2263 -- If neither of these two cases, we are at the top of
2267 exit Component_List_Loop;
2269 end loop Component_List_Loop;
2271 <<Continue_Main_Component_Loop>>
2272 Next_Entity (C1_Ent);
2274 end loop Main_Component_Loop;
2278 -- For records that have component clauses for all components, and
2279 -- whose size is less than or equal to 32, we need to know the size
2280 -- in the front end to activate possible packed array processing
2281 -- where the component type is a record.
2283 -- At this stage Hbit + 1 represents the first unused bit from all
2284 -- the component clauses processed, so if the component clauses are
2285 -- complete, then this is the length of the record.
2287 -- For records longer than System.Storage_Unit, and for those where
2288 -- not all components have component clauses, the back end determines
2289 -- the length (it may for example be appopriate to round up the size
2290 -- to some convenient boundary, based on alignment considerations etc).
2292 if Unknown_RM_Size (Rectype)
2293 and then Hbit + 1 <= 32
2295 -- Nothing to do if at least one component with no component clause
2297 Comp := First_Entity (Rectype);
2298 while Present (Comp) loop
2299 if Ekind (Comp) = E_Component
2300 or else Ekind (Comp) = E_Discriminant
2302 if No (Component_Clause (Comp)) then
2310 -- If we fall out of loop, all components have component clauses
2311 -- and so we can set the size to the maximum value.
2313 Set_RM_Size (Rectype, Hbit + 1);
2316 end Analyze_Record_Representation_Clause;
2318 -----------------------------
2319 -- Check_Component_Overlap --
2320 -----------------------------
2322 procedure Check_Component_Overlap (C1_Ent, C2_Ent : Entity_Id) is
2324 if Present (Component_Clause (C1_Ent))
2325 and then Present (Component_Clause (C2_Ent))
2327 -- Exclude odd case where we have two tag fields in the same
2328 -- record, both at location zero. This seems a bit strange,
2329 -- but it seems to happen in some circumstances ???
2331 if Chars (C1_Ent) = Name_uTag
2332 and then Chars (C2_Ent) = Name_uTag
2337 -- Here we check if the two fields overlap
2340 S1 : constant Uint := Component_Bit_Offset (C1_Ent);
2341 S2 : constant Uint := Component_Bit_Offset (C2_Ent);
2342 E1 : constant Uint := S1 + Esize (C1_Ent);
2343 E2 : constant Uint := S2 + Esize (C2_Ent);
2346 if E2 <= S1 or else E1 <= S2 then
2350 Component_Name (Component_Clause (C2_Ent));
2351 Error_Msg_Sloc := Sloc (Error_Msg_Node_2);
2353 Component_Name (Component_Clause (C1_Ent));
2355 ("component& overlaps & #",
2356 Component_Name (Component_Clause (C1_Ent)));
2360 end Check_Component_Overlap;
2362 -----------------------------------
2363 -- Check_Constant_Address_Clause --
2364 -----------------------------------
2366 procedure Check_Constant_Address_Clause
2370 procedure Check_At_Constant_Address (Nod : Node_Id);
2371 -- Checks that the given node N represents a name whose 'Address
2372 -- is constant (in the same sense as OK_Constant_Address_Clause,
2373 -- i.e. the address value is the same at the point of declaration
2374 -- of U_Ent and at the time of elaboration of the address clause.
2376 procedure Check_Expr_Constants (Nod : Node_Id);
2377 -- Checks that Nod meets the requirements for a constant address
2378 -- clause in the sense of the enclosing procedure.
2380 procedure Check_List_Constants (Lst : List_Id);
2381 -- Check that all elements of list Lst meet the requirements for a
2382 -- constant address clause in the sense of the enclosing procedure.
2384 -------------------------------
2385 -- Check_At_Constant_Address --
2386 -------------------------------
2388 procedure Check_At_Constant_Address (Nod : Node_Id) is
2390 if Is_Entity_Name (Nod) then
2391 if Present (Address_Clause (Entity ((Nod)))) then
2393 ("invalid address clause for initialized object &!",
2396 ("address for& cannot" &
2397 " depend on another address clause! ('R'M 13.1(22))!",
2400 elsif In_Same_Source_Unit (Entity (Nod), U_Ent)
2401 and then Sloc (U_Ent) < Sloc (Entity (Nod))
2404 ("invalid address clause for initialized object &!",
2406 Error_Msg_Name_1 := Chars (Entity (Nod));
2407 Error_Msg_Name_2 := Chars (U_Ent);
2409 ("\% must be defined before % ('R'M 13.1(22))!",
2413 elsif Nkind (Nod) = N_Selected_Component then
2415 T : constant Entity_Id := Etype (Prefix (Nod));
2418 if (Is_Record_Type (T)
2419 and then Has_Discriminants (T))
2422 and then Is_Record_Type (Designated_Type (T))
2423 and then Has_Discriminants (Designated_Type (T)))
2426 ("invalid address clause for initialized object &!",
2429 ("\address cannot depend on component" &
2430 " of discriminated record ('R'M 13.1(22))!",
2433 Check_At_Constant_Address (Prefix (Nod));
2437 elsif Nkind (Nod) = N_Indexed_Component then
2438 Check_At_Constant_Address (Prefix (Nod));
2439 Check_List_Constants (Expressions (Nod));
2442 Check_Expr_Constants (Nod);
2444 end Check_At_Constant_Address;
2446 --------------------------
2447 -- Check_Expr_Constants --
2448 --------------------------
2450 procedure Check_Expr_Constants (Nod : Node_Id) is
2452 if Nkind (Nod) in N_Has_Etype
2453 and then Etype (Nod) = Any_Type
2459 when N_Empty | N_Error =>
2462 when N_Identifier | N_Expanded_Name =>
2464 Ent : constant Entity_Id := Entity (Nod);
2465 Loc_Ent : constant Source_Ptr := Sloc (Ent);
2466 Loc_U_Ent : constant Source_Ptr := Sloc (U_Ent);
2469 if Ekind (Ent) = E_Named_Integer
2471 Ekind (Ent) = E_Named_Real
2478 Ekind (Ent) = E_Constant
2480 Ekind (Ent) = E_In_Parameter
2482 -- This is the case where we must have Ent defined
2483 -- before U_Ent. Clearly if they are in different
2484 -- units this requirement is met since the unit
2485 -- containing Ent is already processed.
2487 if not In_Same_Source_Unit (Ent, U_Ent) then
2490 -- Otherwise location of Ent must be before the
2491 -- location of U_Ent, that's what prior defined means.
2493 elsif Loc_Ent < Loc_U_Ent then
2498 ("invalid address clause for initialized object &!",
2500 Error_Msg_Name_1 := Chars (Ent);
2501 Error_Msg_Name_2 := Chars (U_Ent);
2503 ("\% must be defined before % ('R'M 13.1(22))!",
2507 elsif Nkind (Original_Node (Nod)) = N_Function_Call then
2508 Check_Expr_Constants (Original_Node (Nod));
2512 ("invalid address clause for initialized object &!",
2514 Error_Msg_Name_1 := Chars (Ent);
2516 ("\reference to variable% not allowed ('R'M 13.1(22))!",
2521 when N_Integer_Literal |
2524 N_Character_Literal =>
2528 Check_Expr_Constants (Low_Bound (Nod));
2529 Check_Expr_Constants (High_Bound (Nod));
2531 when N_Explicit_Dereference =>
2532 Check_Expr_Constants (Prefix (Nod));
2534 when N_Indexed_Component =>
2535 Check_Expr_Constants (Prefix (Nod));
2536 Check_List_Constants (Expressions (Nod));
2539 Check_Expr_Constants (Prefix (Nod));
2540 Check_Expr_Constants (Discrete_Range (Nod));
2542 when N_Selected_Component =>
2543 Check_Expr_Constants (Prefix (Nod));
2545 when N_Attribute_Reference =>
2547 if (Attribute_Name (Nod) = Name_Address
2549 Attribute_Name (Nod) = Name_Access
2551 Attribute_Name (Nod) = Name_Unchecked_Access
2553 Attribute_Name (Nod) = Name_Unrestricted_Access)
2555 Check_At_Constant_Address (Prefix (Nod));
2558 Check_Expr_Constants (Prefix (Nod));
2559 Check_List_Constants (Expressions (Nod));
2563 Check_List_Constants (Component_Associations (Nod));
2564 Check_List_Constants (Expressions (Nod));
2566 when N_Component_Association =>
2567 Check_Expr_Constants (Expression (Nod));
2569 when N_Extension_Aggregate =>
2570 Check_Expr_Constants (Ancestor_Part (Nod));
2571 Check_List_Constants (Component_Associations (Nod));
2572 Check_List_Constants (Expressions (Nod));
2577 when N_Binary_Op | N_And_Then | N_Or_Else | N_In | N_Not_In =>
2578 Check_Expr_Constants (Left_Opnd (Nod));
2579 Check_Expr_Constants (Right_Opnd (Nod));
2582 Check_Expr_Constants (Right_Opnd (Nod));
2584 when N_Type_Conversion |
2585 N_Qualified_Expression |
2587 Check_Expr_Constants (Expression (Nod));
2589 when N_Unchecked_Type_Conversion =>
2590 Check_Expr_Constants (Expression (Nod));
2592 -- If this is a rewritten unchecked conversion, subtypes
2593 -- in this node are those created within the instance.
2594 -- To avoid order of elaboration issues, replace them
2595 -- with their base types. Note that address clauses can
2596 -- cause order of elaboration problems because they are
2597 -- elaborated by the back-end at the point of definition,
2598 -- and may mention entities declared in between (as long
2599 -- as everything is static). It is user-friendly to allow
2600 -- unchecked conversions in this context.
2602 if Nkind (Original_Node (Nod)) = N_Function_Call then
2603 Set_Etype (Expression (Nod),
2604 Base_Type (Etype (Expression (Nod))));
2605 Set_Etype (Nod, Base_Type (Etype (Nod)));
2608 when N_Function_Call =>
2609 if not Is_Pure (Entity (Name (Nod))) then
2611 ("invalid address clause for initialized object &!",
2615 ("\function & is not pure ('R'M 13.1(22))!",
2616 Nod, Entity (Name (Nod)));
2619 Check_List_Constants (Parameter_Associations (Nod));
2622 when N_Parameter_Association =>
2623 Check_Expr_Constants (Explicit_Actual_Parameter (Nod));
2627 ("invalid address clause for initialized object &!",
2630 ("\must be constant defined before& ('R'M 13.1(22))!",
2633 end Check_Expr_Constants;
2635 --------------------------
2636 -- Check_List_Constants --
2637 --------------------------
2639 procedure Check_List_Constants (Lst : List_Id) is
2643 if Present (Lst) then
2644 Nod1 := First (Lst);
2645 while Present (Nod1) loop
2646 Check_Expr_Constants (Nod1);
2650 end Check_List_Constants;
2652 -- Start of processing for Check_Constant_Address_Clause
2655 Check_Expr_Constants (Expr);
2656 end Check_Constant_Address_Clause;
2662 procedure Check_Size
2666 Biased : out Boolean)
2668 UT : constant Entity_Id := Underlying_Type (T);
2674 -- Immediate return if size is same as standard size or if composite
2675 -- item, or generic type, or type with previous errors.
2678 or else UT = Any_Type
2679 or else Is_Generic_Type (UT)
2680 or else Is_Generic_Type (Root_Type (UT))
2681 or else Is_Composite_Type (UT)
2682 or else (Known_Esize (UT) and then Siz = Esize (UT))
2686 -- For fixed-point types, don't check minimum if type is not frozen,
2687 -- since type is not known till then
2690 elsif Is_Fixed_Point_Type (UT)
2691 and then not Is_Frozen (UT)
2695 -- Cases for which a minimum check is required
2698 M := UI_From_Int (Minimum_Size (UT));
2702 -- Size is less than minimum size, but one possibility remains
2703 -- that we can manage with the new size if we bias the type
2705 M := UI_From_Int (Minimum_Size (UT, Biased => True));
2708 Error_Msg_Uint_1 := M;
2710 ("size for& too small, minimum allowed is ^", N, T);
2718 -------------------------
2719 -- Get_Alignment_Value --
2720 -------------------------
2722 function Get_Alignment_Value (Expr : Node_Id) return Uint is
2723 Align : constant Uint := Static_Integer (Expr);
2726 if Align = No_Uint then
2729 elsif Align <= 0 then
2730 Error_Msg_N ("alignment value must be positive", Expr);
2734 for J in Int range 0 .. 64 loop
2736 M : constant Uint := Uint_2 ** J;
2739 exit when M = Align;
2743 ("alignment value must be power of 2", Expr);
2751 end Get_Alignment_Value;
2753 -------------------------------------
2754 -- Get_Attribute_Definition_Clause --
2755 -------------------------------------
2757 function Get_Attribute_Definition_Clause
2765 N := First_Rep_Item (E);
2766 while Present (N) loop
2767 if Nkind (N) = N_Attribute_Definition_Clause
2768 and then Get_Attribute_Id (Chars (N)) = Id
2777 end Get_Attribute_Definition_Clause;
2779 --------------------
2780 -- Get_Rep_Pragma --
2781 --------------------
2783 function Get_Rep_Pragma (E : Entity_Id; Nam : Name_Id) return Node_Id is
2788 N := First_Rep_Item (E);
2790 while Present (N) loop
2791 if Nkind (N) = N_Pragma and then Chars (N) = Nam then
2793 if Nam = Name_Stream_Convert then
2795 -- For tagged types this pragma is not inherited, so we
2796 -- must verify that it is defined for the given type and
2799 Typ := Entity (Expression
2800 (First (Pragma_Argument_Associations (N))));
2802 if not Is_Tagged_Type (E)
2804 or else (Is_Private_Type (Typ)
2805 and then E = Full_View (Typ))
2827 procedure Initialize is
2829 Unchecked_Conversions.Init;
2832 -------------------------
2833 -- Is_Operational_Item --
2834 -------------------------
2836 function Is_Operational_Item (N : Node_Id) return Boolean is
2838 if Nkind (N) /= N_Attribute_Definition_Clause then
2842 Id : constant Attribute_Id := Get_Attribute_Id (Chars (N));
2845 return Id = Attribute_Input
2846 or else Id = Attribute_Output
2847 or else Id = Attribute_Read
2848 or else Id = Attribute_Write;
2851 end Is_Operational_Item;
2857 function Minimum_Size
2859 Biased : Boolean := False)
2862 Lo : Uint := No_Uint;
2863 Hi : Uint := No_Uint;
2864 LoR : Ureal := No_Ureal;
2865 HiR : Ureal := No_Ureal;
2866 LoSet : Boolean := False;
2867 HiSet : Boolean := False;
2873 -- If bad type, return 0
2875 if T = Any_Type then
2878 -- For generic types, just return zero. There cannot be any legitimate
2879 -- need to know such a size, but this routine may be called with a
2880 -- generic type as part of normal processing.
2882 elsif Is_Generic_Type (Root_Type (T)) then
2887 elsif Is_Access_Type (T) then
2888 return System_Address_Size;
2890 -- Floating-point types
2892 elsif Is_Floating_Point_Type (T) then
2893 return UI_To_Int (Esize (Root_Type (T)));
2897 elsif Is_Discrete_Type (T) then
2899 -- The following loop is looking for the nearest compile time
2900 -- known bounds following the ancestor subtype chain. The idea
2901 -- is to find the most restrictive known bounds information.
2905 if Ancest = Any_Type or else Etype (Ancest) = Any_Type then
2910 if Compile_Time_Known_Value (Type_Low_Bound (Ancest)) then
2911 Lo := Expr_Rep_Value (Type_Low_Bound (Ancest));
2918 if Compile_Time_Known_Value (Type_High_Bound (Ancest)) then
2919 Hi := Expr_Rep_Value (Type_High_Bound (Ancest));
2925 Ancest := Ancestor_Subtype (Ancest);
2928 Ancest := Base_Type (T);
2930 if Is_Generic_Type (Ancest) then
2936 -- Fixed-point types. We can't simply use Expr_Value to get the
2937 -- Corresponding_Integer_Value values of the bounds, since these
2938 -- do not get set till the type is frozen, and this routine can
2939 -- be called before the type is frozen. Similarly the test for
2940 -- bounds being static needs to include the case where we have
2941 -- unanalyzed real literals for the same reason.
2943 elsif Is_Fixed_Point_Type (T) then
2945 -- The following loop is looking for the nearest compile time
2946 -- known bounds following the ancestor subtype chain. The idea
2947 -- is to find the most restrictive known bounds information.
2951 if Ancest = Any_Type or else Etype (Ancest) = Any_Type then
2956 if Nkind (Type_Low_Bound (Ancest)) = N_Real_Literal
2957 or else Compile_Time_Known_Value (Type_Low_Bound (Ancest))
2959 LoR := Expr_Value_R (Type_Low_Bound (Ancest));
2966 if Nkind (Type_High_Bound (Ancest)) = N_Real_Literal
2967 or else Compile_Time_Known_Value (Type_High_Bound (Ancest))
2969 HiR := Expr_Value_R (Type_High_Bound (Ancest));
2975 Ancest := Ancestor_Subtype (Ancest);
2978 Ancest := Base_Type (T);
2980 if Is_Generic_Type (Ancest) then
2986 Lo := UR_To_Uint (LoR / Small_Value (T));
2987 Hi := UR_To_Uint (HiR / Small_Value (T));
2989 -- No other types allowed
2992 raise Program_Error;
2995 -- Fall through with Hi and Lo set. Deal with biased case.
2997 if (Biased and then not Is_Fixed_Point_Type (T))
2998 or else Has_Biased_Representation (T)
3004 -- Signed case. Note that we consider types like range 1 .. -1 to be
3005 -- signed for the purpose of computing the size, since the bounds
3006 -- have to be accomodated in the base type.
3008 if Lo < 0 or else Hi < 0 then
3012 -- S = size, B = 2 ** (size - 1) (can accommodate -B .. +(B - 1))
3013 -- Note that we accommodate the case where the bounds cross. This
3014 -- can happen either because of the way the bounds are declared
3015 -- or because of the algorithm in Freeze_Fixed_Point_Type.
3029 -- If both bounds are positive, make sure that both are represen-
3030 -- table in the case where the bounds are crossed. This can happen
3031 -- either because of the way the bounds are declared, or because of
3032 -- the algorithm in Freeze_Fixed_Point_Type.
3038 -- S = size, (can accommodate 0 .. (2**size - 1))
3041 while Hi >= Uint_2 ** S loop
3049 -------------------------
3050 -- New_Stream_Function --
3051 -------------------------
3053 procedure New_Stream_Function
3059 Loc : constant Source_Ptr := Sloc (N);
3060 Subp_Id : Entity_Id := Make_Defining_Identifier (Loc, Nam);
3061 Subp_Decl : Node_Id;
3066 F := First_Formal (Subp);
3067 Etyp := Etype (Subp);
3070 Make_Subprogram_Renaming_Declaration (Loc,
3073 Make_Function_Specification (Loc,
3074 Defining_Unit_Name => Subp_Id,
3075 Parameter_Specifications =>
3077 Make_Parameter_Specification (Loc,
3078 Defining_Identifier =>
3079 Make_Defining_Identifier (Loc, Name_S),
3081 Make_Access_Definition (Loc,
3084 Designated_Type (Etype (F)), Loc)))),
3087 New_Reference_To (Etyp, Loc)),
3089 Name => New_Reference_To (Subp, Loc));
3091 if Is_Tagged_Type (Ent) and then not Is_Limited_Type (Ent) then
3092 Set_TSS (Base_Type (Ent), Subp_Id);
3094 Insert_Action (N, Subp_Decl);
3095 Copy_TSS (Subp_Id, Base_Type (Ent));
3098 end New_Stream_Function;
3100 --------------------------
3101 -- New_Stream_Procedure --
3102 --------------------------
3104 procedure New_Stream_Procedure
3109 Out_P : Boolean := False)
3111 Loc : constant Source_Ptr := Sloc (N);
3112 Subp_Id : Entity_Id := Make_Defining_Identifier (Loc, Nam);
3113 Subp_Decl : Node_Id;
3118 F := First_Formal (Subp);
3119 Etyp := Etype (Next_Formal (F));
3122 Make_Subprogram_Renaming_Declaration (Loc,
3125 Make_Procedure_Specification (Loc,
3126 Defining_Unit_Name => Subp_Id,
3127 Parameter_Specifications =>
3129 Make_Parameter_Specification (Loc,
3130 Defining_Identifier =>
3131 Make_Defining_Identifier (Loc, Name_S),
3133 Make_Access_Definition (Loc,
3136 Designated_Type (Etype (F)), Loc))),
3138 Make_Parameter_Specification (Loc,
3139 Defining_Identifier =>
3140 Make_Defining_Identifier (Loc, Name_V),
3141 Out_Present => Out_P,
3143 New_Reference_To (Etyp, Loc)))),
3144 Name => New_Reference_To (Subp, Loc));
3146 if Is_Tagged_Type (Ent) and then not Is_Limited_Type (Ent) then
3147 Set_TSS (Base_Type (Ent), Subp_Id);
3149 Insert_Action (N, Subp_Decl);
3150 Copy_TSS (Subp_Id, Base_Type (Ent));
3153 end New_Stream_Procedure;
3155 ---------------------
3156 -- Record_Rep_Item --
3157 ---------------------
3159 procedure Record_Rep_Item (T : Entity_Id; N : Node_Id) is
3161 Set_Next_Rep_Item (N, First_Rep_Item (T));
3162 Set_First_Rep_Item (T, N);
3163 end Record_Rep_Item;
3165 ------------------------
3166 -- Rep_Item_Too_Early --
3167 ------------------------
3169 function Rep_Item_Too_Early
3175 -- Cannot apply rep items to generic types
3178 and then Is_Generic_Type (Root_Type (T))
3181 ("representation item not allowed for generic type", N);
3185 -- Otherwise check for incompleted type
3187 if Is_Incomplete_Or_Private_Type (T)
3188 and then No (Underlying_Type (T))
3191 ("representation item must be after full type declaration", N);
3194 -- If the type has incompleted components, a representation clause is
3195 -- illegal but stream attributes and Convention pragmas are correct.
3197 elsif Has_Private_Component (T) then
3198 if (Nkind (N) = N_Pragma or else Is_Operational_Item (N)) then
3202 ("representation item must appear after type is fully defined",
3209 end Rep_Item_Too_Early;
3211 -----------------------
3212 -- Rep_Item_Too_Late --
3213 -----------------------
3215 function Rep_Item_Too_Late
3218 FOnly : Boolean := False)
3222 Parent_Type : Entity_Id;
3225 -- Output the too late message
3227 procedure Too_Late is
3229 Error_Msg_N ("representation item appears too late!", N);
3232 -- Start of processing for Rep_Item_Too_Late
3235 -- First make sure entity is not frozen (RM 13.1(9)). Exclude imported
3236 -- types, which may be frozen if they appear in a representation clause
3237 -- for a local type.
3240 and then not From_With_Type (T)
3243 S := First_Subtype (T);
3245 if Present (Freeze_Node (S)) then
3247 ("?no more representation items for }!", Freeze_Node (S), S);
3252 -- Check for case of non-tagged derived type whose parent either has
3253 -- primitive operations, or is a by reference type (RM 13.1(10)).
3257 and then Is_Derived_Type (T)
3258 and then not Is_Tagged_Type (T)
3260 Parent_Type := Etype (Base_Type (T));
3262 if Has_Primitive_Operations (Parent_Type) then
3265 ("primitive operations already defined for&!", N, Parent_Type);
3268 elsif Is_By_Reference_Type (Parent_Type) then
3271 ("parent type & is a by reference type!", N, Parent_Type);
3276 -- No error, link item into head of chain of rep items for the entity
3278 Record_Rep_Item (T, N);
3280 end Rep_Item_Too_Late;
3282 -------------------------
3283 -- Same_Representation --
3284 -------------------------
3286 function Same_Representation (Typ1, Typ2 : Entity_Id) return Boolean is
3287 T1 : constant Entity_Id := Underlying_Type (Typ1);
3288 T2 : constant Entity_Id := Underlying_Type (Typ2);
3291 -- A quick check, if base types are the same, then we definitely have
3292 -- the same representation, because the subtype specific representation
3293 -- attributes (Size and Alignment) do not affect representation from
3294 -- the point of view of this test.
3296 if Base_Type (T1) = Base_Type (T2) then
3299 elsif Is_Private_Type (Base_Type (T2))
3300 and then Base_Type (T1) = Full_View (Base_Type (T2))
3305 -- Tagged types never have differing representations
3307 if Is_Tagged_Type (T1) then
3311 -- Representations are definitely different if conventions differ
3313 if Convention (T1) /= Convention (T2) then
3317 -- Representations are different if component alignments differ
3319 if (Is_Record_Type (T1) or else Is_Array_Type (T1))
3321 (Is_Record_Type (T2) or else Is_Array_Type (T2))
3322 and then Component_Alignment (T1) /= Component_Alignment (T2)
3327 -- For arrays, the only real issue is component size. If we know the
3328 -- component size for both arrays, and it is the same, then that's
3329 -- good enough to know we don't have a change of representation.
3331 if Is_Array_Type (T1) then
3332 if Known_Component_Size (T1)
3333 and then Known_Component_Size (T2)
3334 and then Component_Size (T1) = Component_Size (T2)
3340 -- Types definitely have same representation if neither has non-standard
3341 -- representation since default representations are always consistent.
3342 -- If only one has non-standard representation, and the other does not,
3343 -- then we consider that they do not have the same representation. They
3344 -- might, but there is no way of telling early enough.
3346 if Has_Non_Standard_Rep (T1) then
3347 if not Has_Non_Standard_Rep (T2) then
3351 return not Has_Non_Standard_Rep (T2);
3354 -- Here the two types both have non-standard representation, and we
3355 -- need to determine if they have the same non-standard representation
3357 -- For arrays, we simply need to test if the component sizes are the
3358 -- same. Pragma Pack is reflected in modified component sizes, so this
3359 -- check also deals with pragma Pack.
3361 if Is_Array_Type (T1) then
3362 return Component_Size (T1) = Component_Size (T2);
3364 -- Tagged types always have the same representation, because it is not
3365 -- possible to specify different representations for common fields.
3367 elsif Is_Tagged_Type (T1) then
3370 -- Case of record types
3372 elsif Is_Record_Type (T1) then
3374 -- Packed status must conform
3376 if Is_Packed (T1) /= Is_Packed (T2) then
3379 -- Otherwise we must check components. Typ2 maybe a constrained
3380 -- subtype with fewer components, so we compare the components
3381 -- of the base types.
3384 Record_Case : declare
3385 CD1, CD2 : Entity_Id;
3387 function Same_Rep return Boolean;
3388 -- CD1 and CD2 are either components or discriminants. This
3389 -- function tests whether the two have the same representation
3391 function Same_Rep return Boolean is
3393 if No (Component_Clause (CD1)) then
3394 return No (Component_Clause (CD2));
3398 Present (Component_Clause (CD2))
3400 Component_Bit_Offset (CD1) = Component_Bit_Offset (CD2)
3402 Esize (CD1) = Esize (CD2);
3406 -- Start processing for Record_Case
3409 if Has_Discriminants (T1) then
3410 CD1 := First_Discriminant (T1);
3411 CD2 := First_Discriminant (T2);
3413 while Present (CD1) loop
3414 if not Same_Rep then
3417 Next_Discriminant (CD1);
3418 Next_Discriminant (CD2);
3423 CD1 := First_Component (Underlying_Type (Base_Type (T1)));
3424 CD2 := First_Component (Underlying_Type (Base_Type (T2)));
3426 while Present (CD1) loop
3427 if not Same_Rep then
3430 Next_Component (CD1);
3431 Next_Component (CD2);
3439 -- For enumeration types, we must check each literal to see if the
3440 -- representation is the same. Note that we do not permit enumeration
3441 -- representation clauses for Character and Wide_Character, so these
3442 -- cases were already dealt with.
3444 elsif Is_Enumeration_Type (T1) then
3446 Enumeration_Case : declare
3450 L1 := First_Literal (T1);
3451 L2 := First_Literal (T2);
3453 while Present (L1) loop
3454 if Enumeration_Rep (L1) /= Enumeration_Rep (L2) then
3464 end Enumeration_Case;
3466 -- Any other types have the same representation for these purposes
3472 end Same_Representation;
3474 --------------------
3475 -- Set_Enum_Esize --
3476 --------------------
3478 procedure Set_Enum_Esize (T : Entity_Id) is
3486 -- Find the minimum standard size (8,16,32,64) that fits
3488 Lo := Enumeration_Rep (Entity (Type_Low_Bound (T)));
3489 Hi := Enumeration_Rep (Entity (Type_High_Bound (T)));
3492 if Lo >= -Uint_2**07 and then Hi < Uint_2**07 then
3495 elsif Lo >= -Uint_2**15 and then Hi < Uint_2**15 then
3498 elsif Lo >= -Uint_2**31 and then Hi < Uint_2**31 then
3501 else pragma Assert (Lo >= -Uint_2**63 and then Hi < Uint_2**63);
3506 if Hi < Uint_2**08 then
3509 elsif Hi < Uint_2**16 then
3512 elsif Hi < Uint_2**32 then
3515 else pragma Assert (Hi < Uint_2**63);
3520 -- That minimum is the proper size unless we have a foreign convention
3521 -- and the size required is 32 or less, in which case we bump the size
3522 -- up to 32. This is required for C and C++ and seems reasonable for
3523 -- all other foreign conventions.
3525 if Has_Foreign_Convention (T)
3526 and then Esize (T) < Standard_Integer_Size
3528 Init_Esize (T, Standard_Integer_Size);
3536 -----------------------------------
3537 -- Validate_Unchecked_Conversion --
3538 -----------------------------------
3540 procedure Validate_Unchecked_Conversion
3542 Act_Unit : Entity_Id)
3549 -- Obtain source and target types. Note that we call Ancestor_Subtype
3550 -- here because the processing for generic instantiation always makes
3551 -- subtypes, and we want the original frozen actual types.
3553 -- If we are dealing with private types, then do the check on their
3554 -- fully declared counterparts if the full declarations have been
3555 -- encountered (they don't have to be visible, but they must exist!)
3557 Source := Ancestor_Subtype (Etype (First_Formal (Act_Unit)));
3559 if Is_Private_Type (Source)
3560 and then Present (Underlying_Type (Source))
3562 Source := Underlying_Type (Source);
3565 Target := Ancestor_Subtype (Etype (Act_Unit));
3567 -- If either type is generic, the instantiation happens within a
3568 -- generic unit, and there is nothing to check. The proper check
3569 -- will happen when the enclosing generic is instantiated.
3571 if Is_Generic_Type (Source) or else Is_Generic_Type (Target) then
3575 if Is_Private_Type (Target)
3576 and then Present (Underlying_Type (Target))
3578 Target := Underlying_Type (Target);
3581 -- Source may be unconstrained array, but not target
3583 if Is_Array_Type (Target)
3584 and then not Is_Constrained (Target)
3587 ("unchecked conversion to unconstrained array not allowed", N);
3591 -- Make entry in unchecked conversion table for later processing
3592 -- by Validate_Unchecked_Conversions, which will check sizes and
3593 -- alignments (using values set by the back-end where possible).
3595 Unchecked_Conversions.Append
3596 (New_Val => UC_Entry'
3601 -- Generate N_Validate_Unchecked_Conversion node for back end if
3602 -- the back end needs to perform special validation checks. At the
3603 -- current time, only the JVM version requires such checks.
3607 Make_Validate_Unchecked_Conversion (Sloc (N));
3608 Set_Source_Type (Vnode, Source);
3609 Set_Target_Type (Vnode, Target);
3610 Insert_After (N, Vnode);
3612 end Validate_Unchecked_Conversion;
3614 ------------------------------------
3615 -- Validate_Unchecked_Conversions --
3616 ------------------------------------
3618 procedure Validate_Unchecked_Conversions is
3620 for N in Unchecked_Conversions.First .. Unchecked_Conversions.Last loop
3622 T : UC_Entry renames Unchecked_Conversions.Table (N);
3624 Enode : constant Node_Id := T.Enode;
3625 Source : constant Entity_Id := T.Source;
3626 Target : constant Entity_Id := T.Target;
3632 -- This validation check, which warns if we have unequal sizes
3633 -- for unchecked conversion, and thus potentially implementation
3634 -- dependent semantics, is one of the few occasions on which we
3635 -- use the official RM size instead of Esize. See description
3636 -- in Einfo "Handling of Type'Size Values" for details.
3638 if Errors_Detected = 0
3639 and then Known_Static_RM_Size (Source)
3640 and then Known_Static_RM_Size (Target)
3642 Source_Siz := RM_Size (Source);
3643 Target_Siz := RM_Size (Target);
3645 if Source_Siz /= Target_Siz then
3646 Warn_On_Instance := True;
3648 ("types for unchecked conversion have different sizes?",
3651 if All_Errors_Mode then
3652 Error_Msg_Name_1 := Chars (Source);
3653 Error_Msg_Uint_1 := Source_Siz;
3654 Error_Msg_Name_2 := Chars (Target);
3655 Error_Msg_Uint_2 := Target_Siz;
3657 ("\size of % is ^, size of % is ^?", Enode);
3659 Error_Msg_Uint_1 := UI_Abs (Source_Siz - Target_Siz);
3661 if Is_Discrete_Type (Source)
3662 and then Is_Discrete_Type (Target)
3664 if Source_Siz > Target_Siz then
3666 ("\^ high order bits of source will be ignored?",
3669 elsif Is_Modular_Integer_Type (Source) then
3671 ("\source will be extended with ^ high order " &
3672 "zero bits?", Enode);
3676 ("\source will be extended with ^ high order " &
3681 elsif Source_Siz < Target_Siz then
3682 if Is_Discrete_Type (Target) then
3683 if Bytes_Big_Endian then
3685 ("\target value will include ^ undefined " &
3690 ("\target value will include ^ undefined " &
3697 ("\^ trailing bits of target value will be " &
3698 "undefined?", Enode);
3701 else pragma Assert (Source_Siz > Target_Siz);
3703 ("\^ trailing bits of source will be ignored?",
3708 Warn_On_Instance := False;
3712 -- If both types are access types, we need to check the alignment.
3713 -- If the alignment of both is specified, we can do it here.
3715 if Errors_Detected = 0
3716 and then Ekind (Source) in Access_Kind
3717 and then Ekind (Target) in Access_Kind
3718 and then Target_Strict_Alignment
3719 and then Present (Designated_Type (Source))
3720 and then Present (Designated_Type (Target))
3723 D_Source : constant Entity_Id := Designated_Type (Source);
3724 D_Target : constant Entity_Id := Designated_Type (Target);
3727 if Known_Alignment (D_Source)
3728 and then Known_Alignment (D_Target)
3731 Source_Align : constant Uint := Alignment (D_Source);
3732 Target_Align : constant Uint := Alignment (D_Target);
3735 if Source_Align < Target_Align
3736 and then not Is_Tagged_Type (D_Source)
3738 Warn_On_Instance := True;
3739 Error_Msg_Uint_1 := Target_Align;
3740 Error_Msg_Uint_2 := Source_Align;
3741 Error_Msg_Node_2 := D_Source;
3743 ("alignment of & (^) is stricter than " &
3744 "alignment of & (^)?", Enode, D_Target);
3746 if All_Errors_Mode then
3748 ("\resulting access value may have invalid " &
3749 "alignment?", Enode);
3752 Warn_On_Instance := False;
3760 end Validate_Unchecked_Conversions;
3766 procedure Warn_Overlay
3771 Old : Entity_Id := Empty;
3775 if not Address_Clause_Overlay_Warnings then
3780 and then (Has_Non_Null_Base_Init_Proc (Typ)
3781 or else Is_Access_Type (Typ))
3782 and then not Is_Imported (Entity (Nam))
3784 if Nkind (Expr) = N_Attribute_Reference
3785 and then Is_Entity_Name (Prefix (Expr))
3787 Old := Entity (Prefix (Expr));
3789 elsif Is_Entity_Name (Expr)
3790 and then Ekind (Entity (Expr)) = E_Constant
3792 Decl := Declaration_Node (Entity (Expr));
3794 if Nkind (Decl) = N_Object_Declaration
3795 and then Present (Expression (Decl))
3796 and then Nkind (Expression (Decl)) = N_Attribute_Reference
3797 and then Is_Entity_Name (Prefix (Expression (Decl)))
3799 Old := Entity (Prefix (Expression (Decl)));
3801 elsif Nkind (Expr) = N_Function_Call then
3805 -- A function call (most likely to To_Address) is probably not
3806 -- an overlay, so skip warning. Ditto if the function call was
3807 -- inlined and transformed into an entity.
3809 elsif Nkind (Original_Node (Expr)) = N_Function_Call then
3813 Decl := Next (Parent (Expr));
3815 -- If a pragma Import follows, we assume that it is for the current
3816 -- target of the address clause, and skip the warning.
3819 and then Nkind (Decl) = N_Pragma
3820 and then Chars (Decl) = Name_Import
3825 if Present (Old) then
3826 Error_Msg_Node_2 := Old;
3828 ("default initialization of & may modify &?",
3832 ("default initialization of & may modify overlaid storage?",
3836 -- Add friendly warning if initialization comes from a packed array
3839 if Is_Record_Type (Typ) then
3844 Comp := First_Component (Typ);
3846 while Present (Comp) loop
3847 if Nkind (Parent (Comp)) = N_Component_Declaration
3848 and then Present (Expression (Parent (Comp)))
3851 elsif Is_Array_Type (Etype (Comp))
3852 and then Present (Packed_Array_Type (Etype (Comp)))
3855 ("packed array component& will be initialized to zero?",
3859 Next_Component (Comp);
3866 ("use pragma Import for & to " &
3867 "suppress initialization ('R'M B.1(24))?",