1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2002, 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 2, 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 COPYING. If not, write --
19 -- to the Free Software Foundation, 59 Temple Place - Suite 330, Boston, --
20 -- MA 02111-1307, USA. --
22 -- GNAT was originally developed by the GNAT team at New York University. --
23 -- Extensive contributions were provided by Ada Core Technologies Inc. --
25 ------------------------------------------------------------------------------
27 with Atree; use Atree;
28 with Checks; use Checks;
29 with Einfo; use Einfo;
30 with Errout; use Errout;
31 with Exp_Tss; use Exp_Tss;
32 with Exp_Util; use Exp_Util;
33 with Hostparm; use Hostparm;
35 with Nlists; use Nlists;
36 with Nmake; use Nmake;
38 with Rtsfind; use Rtsfind;
40 with Sem_Ch8; use Sem_Ch8;
41 with Sem_Eval; use Sem_Eval;
42 with Sem_Res; use Sem_Res;
43 with Sem_Type; use Sem_Type;
44 with Sem_Util; use Sem_Util;
45 with Snames; use Snames;
46 with Stand; use Stand;
47 with Sinfo; use Sinfo;
49 with Ttypes; use Ttypes;
50 with Tbuild; use Tbuild;
51 with Urealp; use Urealp;
53 with GNAT.Heap_Sort_A; use GNAT.Heap_Sort_A;
55 package body Sem_Ch13 is
57 SSU : constant Pos := System_Storage_Unit;
58 -- Convenient short hand for commonly used constant
60 -----------------------
61 -- Local Subprograms --
62 -----------------------
64 procedure Alignment_Check_For_Esize_Change (Typ : Entity_Id);
65 -- This routine is called after setting the Esize of type entity Typ.
66 -- The purpose is to deal with the situation where an aligment has been
67 -- inherited from a derived type that is no longer appropriate for the
68 -- new Esize value. In this case, we reset the Alignment to unknown.
70 procedure Check_Component_Overlap (C1_Ent, C2_Ent : Entity_Id);
71 -- Given two entities for record components or discriminants, checks
72 -- if they hav overlapping component clauses and issues errors if so.
74 function Get_Alignment_Value (Expr : Node_Id) return Uint;
75 -- Given the expression for an alignment value, returns the corresponding
76 -- Uint value. If the value is inappropriate, then error messages are
77 -- posted as required, and a value of No_Uint is returned.
79 function Is_Operational_Item (N : Node_Id) return Boolean;
80 -- A specification for a stream attribute is allowed before the full
81 -- type is declared, as explained in AI-00137 and the corrigendum.
82 -- Attributes that do not specify a representation characteristic are
83 -- operational attributes.
85 procedure New_Stream_Function
90 -- Create a function renaming of a given stream attribute to the
91 -- designated subprogram and then in the tagged case, provide this as
92 -- a primitive operation, or in the non-tagged case make an appropriate
93 -- TSS entry. Used for Input. This is more properly an expansion activity
94 -- than just semantics, but the presence of user-defined stream functions
95 -- for limited types is a legality check, which is why this takes place
96 -- here rather than in exp_ch13, where it was previously.
98 -- To avoid elaboration anomalies with freeze nodes, for untagged types
99 -- we generate both a subprogram declaration and a subprogram renaming
100 -- declaration, so that the attribute specification is handled as a
101 -- renaming_as_body. For tagged types, the specification is one of the
104 procedure New_Stream_Procedure
109 Out_P : Boolean := False);
110 -- Create a procedure renaming of a given stream attribute to the
111 -- designated subprogram and then in the tagged case, provide this as
112 -- a primitive operation, or in the non-tagged case make an appropriate
113 -- TSS entry. Used for Read, Output, Write.
115 procedure Check_Constant_Address_Clause (Expr : Node_Id; U_Ent : Entity_Id);
116 -- Expr is an expression for an address clause. This procedure checks
117 -- that the expression is constant, in the limited sense that it is safe
118 -- to evaluate it at the point the object U_Ent is declared, rather than
119 -- at the point of the address clause. The condition for this to be true
120 -- is that the expression has no variables, no constants declared after
121 -- U_Ent, and no calls to non-pure functions. If this condition is not
122 -- met, then an appropriate error message is posted.
124 procedure Warn_Overlay
128 -- Expr is the expression for an address clause for entity Nam whose type
129 -- is Typ. If Typ has a default initialization, check whether the address
130 -- clause might overlay two entities, and emit a warning on the side effect
131 -- that the initialization will cause.
133 ----------------------------------------------
134 -- Table for Validate_Unchecked_Conversions --
135 ----------------------------------------------
137 -- The following table collects unchecked conversions for validation.
138 -- Entries are made by Validate_Unchecked_Conversion and then the
139 -- call to Validate_Unchecked_Conversions does the actual error
140 -- checking and posting of warnings. The reason for this delayed
141 -- processing is to take advantage of back-annotations of size and
142 -- alignment values peformed by the back end.
144 type UC_Entry is record
145 Enode : Node_Id; -- node used for posting warnings
146 Source : Entity_Id; -- source type for unchecked conversion
147 Target : Entity_Id; -- target type for unchecked conversion
150 package Unchecked_Conversions is new Table.Table (
151 Table_Component_Type => UC_Entry,
152 Table_Index_Type => Int,
153 Table_Low_Bound => 1,
155 Table_Increment => 200,
156 Table_Name => "Unchecked_Conversions");
158 --------------------------------------
159 -- Alignment_Check_For_Esize_Change --
160 --------------------------------------
162 procedure Alignment_Check_For_Esize_Change (Typ : Entity_Id) is
164 -- If the alignment is known, and not set by a rep clause, and is
165 -- inconsistent with the size being set, then reset it to unknown,
166 -- we assume in this case that the size overrides the inherited
167 -- alignment, and that the alignment must be recomputed.
169 if Known_Alignment (Typ)
170 and then not Has_Alignment_Clause (Typ)
171 and then Esize (Typ) mod (Alignment (Typ) * SSU) /= 0
173 Init_Alignment (Typ);
175 end Alignment_Check_For_Esize_Change;
177 -----------------------
178 -- Analyze_At_Clause --
179 -----------------------
181 -- An at clause is replaced by the corresponding Address attribute
182 -- definition clause that is the preferred approach in Ada 95.
184 procedure Analyze_At_Clause (N : Node_Id) is
187 Make_Attribute_Definition_Clause (Sloc (N),
188 Name => Identifier (N),
189 Chars => Name_Address,
190 Expression => Expression (N)));
191 Analyze_Attribute_Definition_Clause (N);
192 end Analyze_At_Clause;
194 -----------------------------------------
195 -- Analyze_Attribute_Definition_Clause --
196 -----------------------------------------
198 procedure Analyze_Attribute_Definition_Clause (N : Node_Id) is
199 Loc : constant Source_Ptr := Sloc (N);
200 Nam : constant Node_Id := Name (N);
201 Attr : constant Name_Id := Chars (N);
202 Expr : constant Node_Id := Expression (N);
203 Id : constant Attribute_Id := Get_Attribute_Id (Attr);
207 FOnly : Boolean := False;
208 -- Reset to True for subtype specific attribute (Alignment, Size)
209 -- and for stream attributes, i.e. those cases where in the call
210 -- to Rep_Item_Too_Late, FOnly is set True so that only the freezing
211 -- rules are checked. Note that the case of stream attributes is not
212 -- clear from the RM, but see AI95-00137. Also, the RM seems to
213 -- disallow Storage_Size for derived task types, but that is also
214 -- clearly unintentional.
220 if Rep_Item_Too_Early (Ent, N) then
224 -- Rep clause applies to full view of incomplete type or private type
225 -- if we have one (if not, this is a premature use of the type).
226 -- However, certain semantic checks need to be done on the specified
227 -- entity (i.e. the private view), so we save it in Ent.
229 if Is_Private_Type (Ent)
230 and then Is_Derived_Type (Ent)
231 and then not Is_Tagged_Type (Ent)
232 and then No (Full_View (Ent))
234 -- If this is a private type whose completion is a derivation
235 -- from another private type, there is no full view, and the
236 -- attribute belongs to the type itself, not its underlying parent.
240 elsif Ekind (Ent) = E_Incomplete_Type then
241 Ent := Underlying_Type (Ent);
244 U_Ent := Underlying_Type (Ent);
247 -- Complete other routine error checks
249 if Etype (Nam) = Any_Type then
252 elsif Scope (Ent) /= Current_Scope then
253 Error_Msg_N ("entity must be declared in this scope", Nam);
256 elsif No (U_Ent) then
259 elsif Is_Type (U_Ent)
260 and then not Is_First_Subtype (U_Ent)
261 and then Id /= Attribute_Object_Size
262 and then Id /= Attribute_Value_Size
263 and then not From_At_Mod (N)
265 Error_Msg_N ("cannot specify attribute for subtype", Nam);
270 -- Switch on particular attribute
278 -- Address attribute definition clause
280 when Attribute_Address => Address : begin
281 Analyze_And_Resolve (Expr, RTE (RE_Address));
283 if Present (Address_Clause (U_Ent)) then
284 Error_Msg_N ("address already given for &", Nam);
286 -- Case of address clause for subprogram
288 elsif Is_Subprogram (U_Ent) then
290 if Has_Homonym (U_Ent) then
292 ("address clause cannot be given " &
293 "for overloaded subprogram",
297 -- For subprograms, all address clauses are permitted,
298 -- and we mark the subprogram as having a deferred freeze
299 -- so that Gigi will not elaborate it too soon.
301 -- Above needs more comments, what is too soon about???
303 Set_Has_Delayed_Freeze (U_Ent);
305 -- Case of address clause for entry
307 elsif Ekind (U_Ent) = E_Entry then
309 if Nkind (Parent (N)) = N_Task_Body then
311 ("entry address must be specified in task spec", Nam);
314 -- For entries, we require a constant address
316 Check_Constant_Address_Clause (Expr, U_Ent);
318 if Is_Task_Type (Scope (U_Ent))
319 and then Comes_From_Source (Scope (U_Ent))
322 ("?entry address declared for entry in task type", N);
324 ("\?only one task can be declared of this type", N);
327 -- Case of address clause for an object
330 Ekind (U_Ent) = E_Variable
332 Ekind (U_Ent) = E_Constant
335 Decl : constant Node_Id := Declaration_Node (U_Ent);
336 Expr : constant Node_Id := Expression (N);
337 Typ : constant Entity_Id := Etype (U_Ent);
340 -- Exported variables cannot have an address clause,
341 -- because this cancels the effect of the pragma Export
343 if Is_Exported (U_Ent) then
345 ("cannot export object with address clause", Nam);
347 -- Imported variables can have an address clause, but then
348 -- the import is pretty meaningless except to suppress
349 -- initializations, so we do not need such variables to
350 -- be statically allocated (and in fact it causes trouble
351 -- if the address clause is a local value).
353 elsif Is_Imported (U_Ent) then
354 Set_Is_Statically_Allocated (U_Ent, False);
357 -- We mark a possible modification of a variable with an
358 -- address clause, since it is likely aliasing is occurring.
360 Note_Possible_Modification (Nam);
362 -- If we have no initialization of any kind, then we can
363 -- safely defer the elaboration of the variable to its
364 -- freezing point, so that the address clause will be
365 -- computed at the proper point.
367 -- The same processing applies to all initialized scalar
368 -- types and all access types. Packed bit arrays of size
369 -- up to 64 are represented using a modular type with an
370 -- initialization (to zero) and can be processed like
371 -- other initialized scalar types.
373 if (No (Expression (Decl))
374 and then not Has_Non_Null_Base_Init_Proc (Typ))
377 (Present (Expression (Decl))
378 and then Is_Scalar_Type (Typ))
384 (Is_Bit_Packed_Array (Base_Type (Typ))
386 Is_Modular_Integer_Type (Packed_Array_Type (Typ)))
388 Set_Has_Delayed_Freeze (U_Ent);
390 -- Otherwise, we require the address clause to be constant
393 Check_Constant_Address_Clause (Expr, U_Ent);
396 if Is_Exported (U_Ent) then
398 ("& cannot be exported if an address clause is given",
401 ("\define and export a variable " &
402 "that holds its address instead",
406 if not Error_Posted (Expr) then
407 Warn_Overlay (Expr, Typ, Nam);
410 -- If entity has delayed freeze then we will generate
411 -- an alignment check at the freeze point. If there is
412 -- no delayed freeze we can do it right now.
414 if not Has_Delayed_Freeze (U_Ent) then
415 Apply_Alignment_Check (U_Ent, N);
418 -- Kill the size check code, since we are not allocating
419 -- the variable, it is somewhere else.
421 Kill_Size_Check_Code (U_Ent);
424 -- Not a valid entity for an address clause
427 Error_Msg_N ("address cannot be given for &", Nam);
435 -- Alignment attribute definition clause
437 when Attribute_Alignment => Alignment_Block : declare
438 Align : Uint := Get_Alignment_Value (Expr);
443 if not Is_Type (U_Ent)
444 and then Ekind (U_Ent) /= E_Variable
445 and then Ekind (U_Ent) /= E_Constant
447 Error_Msg_N ("alignment cannot be given for &", Nam);
449 elsif Has_Alignment_Clause (U_Ent) then
450 Error_Msg_Sloc := Sloc (Alignment_Clause (U_Ent));
451 Error_Msg_N ("alignment clause previously given#", N);
453 elsif Align /= No_Uint then
454 Set_Has_Alignment_Clause (U_Ent);
455 Set_Alignment (U_Ent, Align);
463 -- Bit_Order attribute definition clause
465 when Attribute_Bit_Order => Bit_Order : declare
467 if not Is_Record_Type (U_Ent) then
469 ("Bit_Order can only be defined for record type", Nam);
472 Analyze_And_Resolve (Expr, RTE (RE_Bit_Order));
474 if Etype (Expr) = Any_Type then
477 elsif not Is_Static_Expression (Expr) then
478 Error_Msg_N ("Bit_Order requires static expression", Expr);
481 if (Expr_Value (Expr) = 0) /= Bytes_Big_Endian then
482 Set_Reverse_Bit_Order (U_Ent, True);
492 -- Component_Size attribute definition clause
494 when Attribute_Component_Size => Component_Size_Case : declare
495 Csize : constant Uint := Static_Integer (Expr);
498 New_Ctyp : Entity_Id;
502 if not Is_Array_Type (U_Ent) then
503 Error_Msg_N ("component size requires array type", Nam);
507 Btype := Base_Type (U_Ent);
509 if Has_Component_Size_Clause (Btype) then
511 ("component size clase for& previously given", Nam);
513 elsif Csize /= No_Uint then
514 Check_Size (Expr, Component_Type (Btype), Csize, Biased);
516 if Has_Aliased_Components (Btype)
522 ("component size incorrect for aliased components", N);
526 -- For the biased case, build a declaration for a subtype
527 -- that will be used to represent the biased subtype that
528 -- reflects the biased representation of components. We need
529 -- this subtype to get proper conversions on referencing
530 -- elements of the array.
534 Make_Defining_Identifier (Loc,
535 Chars => New_External_Name (Chars (U_Ent), 'C', 0, 'T'));
538 Make_Subtype_Declaration (Loc,
539 Defining_Identifier => New_Ctyp,
540 Subtype_Indication =>
541 New_Occurrence_Of (Component_Type (Btype), Loc));
543 Set_Parent (Decl, N);
544 Analyze (Decl, Suppress => All_Checks);
546 Set_Has_Delayed_Freeze (New_Ctyp, False);
547 Set_Esize (New_Ctyp, Csize);
548 Set_RM_Size (New_Ctyp, Csize);
549 Init_Alignment (New_Ctyp);
550 Set_Has_Biased_Representation (New_Ctyp, True);
551 Set_Is_Itype (New_Ctyp, True);
552 Set_Associated_Node_For_Itype (New_Ctyp, U_Ent);
554 Set_Component_Type (Btype, New_Ctyp);
557 Set_Component_Size (Btype, Csize);
558 Set_Has_Component_Size_Clause (Btype, True);
559 Set_Has_Non_Standard_Rep (Btype, True);
561 end Component_Size_Case;
567 when Attribute_External_Tag => External_Tag :
569 if not Is_Tagged_Type (U_Ent) then
570 Error_Msg_N ("should be a tagged type", Nam);
573 Analyze_And_Resolve (Expr, Standard_String);
575 if not Is_Static_Expression (Expr) then
576 Error_Msg_N ("must be a static string", Nam);
579 Set_Has_External_Tag_Rep_Clause (U_Ent);
586 when Attribute_Input => Input : declare
587 Subp : Entity_Id := Empty;
592 function Has_Good_Profile (Subp : Entity_Id) return Boolean;
593 -- Return true if the entity is a function with an appropriate
594 -- profile for the Input attribute.
596 function Has_Good_Profile (Subp : Entity_Id) return Boolean is
598 Ok : Boolean := False;
601 if Ekind (Subp) = E_Function then
602 F := First_Formal (Subp);
604 if Present (F) and then No (Next_Formal (F)) then
605 if Ekind (Etype (F)) = E_Anonymous_Access_Type
607 Designated_Type (Etype (F)) =
608 Class_Wide_Type (RTE (RE_Root_Stream_Type))
610 Ok := Base_Type (Etype (Subp)) = Base_Type (Ent);
616 end Has_Good_Profile;
618 -- Start of processing for Input attribute definition
623 if not Is_Type (U_Ent) then
624 Error_Msg_N ("local name must be a subtype", Nam);
628 Pnam := TSS (Base_Type (U_Ent), Name_uInput);
631 and then Base_Type (Etype (Pnam)) = Base_Type (U_Ent)
633 Error_Msg_Sloc := Sloc (Pnam);
634 Error_Msg_N ("input attribute already defined #", Nam);
641 if Is_Entity_Name (Expr) then
642 if not Is_Overloaded (Expr) then
643 if Has_Good_Profile (Entity (Expr)) then
644 Subp := Entity (Expr);
648 Get_First_Interp (Expr, I, It);
650 while Present (It.Nam) loop
651 if Has_Good_Profile (It.Nam) then
656 Get_Next_Interp (I, It);
661 if Present (Subp) then
662 Set_Entity (Expr, Subp);
663 Set_Etype (Expr, Etype (Subp));
664 New_Stream_Function (N, U_Ent, Subp, Name_uInput);
666 Error_Msg_N ("incorrect expression for input attribute", Expr);
675 -- Machine radix attribute definition clause
677 when Attribute_Machine_Radix => Machine_Radix : declare
678 Radix : constant Uint := Static_Integer (Expr);
681 if not Is_Decimal_Fixed_Point_Type (U_Ent) then
682 Error_Msg_N ("decimal fixed-point type expected for &", Nam);
684 elsif Has_Machine_Radix_Clause (U_Ent) then
685 Error_Msg_Sloc := Sloc (Alignment_Clause (U_Ent));
686 Error_Msg_N ("machine radix clause previously given#", N);
688 elsif Radix /= No_Uint then
689 Set_Has_Machine_Radix_Clause (U_Ent);
690 Set_Has_Non_Standard_Rep (Base_Type (U_Ent));
694 elsif Radix = 10 then
695 Set_Machine_Radix_10 (U_Ent);
697 Error_Msg_N ("machine radix value must be 2 or 10", Expr);
706 -- Object_Size attribute definition clause
708 when Attribute_Object_Size => Object_Size : declare
709 Size : constant Uint := Static_Integer (Expr);
713 if not Is_Type (U_Ent) then
714 Error_Msg_N ("Object_Size cannot be given for &", Nam);
716 elsif Has_Object_Size_Clause (U_Ent) then
717 Error_Msg_N ("Object_Size already given for &", Nam);
720 Check_Size (Expr, U_Ent, Size, Biased);
728 UI_Mod (Size, 64) /= 0
731 ("Object_Size must be 8, 16, 32, or multiple of 64",
735 Set_Esize (U_Ent, Size);
736 Set_Has_Object_Size_Clause (U_Ent);
737 Alignment_Check_For_Esize_Change (U_Ent);
745 when Attribute_Output => Output : declare
746 Subp : Entity_Id := Empty;
751 function Has_Good_Profile (Subp : Entity_Id) return Boolean;
752 -- Return true if the entity is a procedure with an
753 -- appropriate profile for the output attribute.
755 function Has_Good_Profile (Subp : Entity_Id) return Boolean is
757 Ok : Boolean := False;
760 if Ekind (Subp) = E_Procedure then
761 F := First_Formal (Subp);
764 if Ekind (Etype (F)) = E_Anonymous_Access_Type
766 Designated_Type (Etype (F)) =
767 Class_Wide_Type (RTE (RE_Root_Stream_Type))
771 and then Parameter_Mode (F) = E_In_Parameter
772 and then Base_Type (Etype (F)) = Base_Type (Ent)
773 and then No (Next_Formal (F));
779 end Has_Good_Profile;
784 if not Is_Type (U_Ent) then
785 Error_Msg_N ("local name must be a subtype", Nam);
789 Pnam := TSS (Base_Type (U_Ent), Name_uOutput);
793 Base_Type (Etype (Next_Formal (First_Formal (Pnam))))
796 Error_Msg_Sloc := Sloc (Pnam);
797 Error_Msg_N ("output attribute already defined #", Nam);
804 if Is_Entity_Name (Expr) then
805 if not Is_Overloaded (Expr) then
806 if Has_Good_Profile (Entity (Expr)) then
807 Subp := Entity (Expr);
811 Get_First_Interp (Expr, I, It);
813 while Present (It.Nam) loop
814 if Has_Good_Profile (It.Nam) then
819 Get_Next_Interp (I, It);
824 if Present (Subp) then
825 Set_Entity (Expr, Subp);
826 Set_Etype (Expr, Etype (Subp));
827 New_Stream_Procedure (N, U_Ent, Subp, Name_uOutput);
829 Error_Msg_N ("incorrect expression for output attribute", Expr);
838 when Attribute_Read => Read : declare
839 Subp : Entity_Id := Empty;
844 function Has_Good_Profile (Subp : Entity_Id) return Boolean;
845 -- Return true if the entity is a procedure with an appropriate
846 -- profile for the Read attribute.
848 function Has_Good_Profile (Subp : Entity_Id) return Boolean is
850 Ok : Boolean := False;
853 if Ekind (Subp) = E_Procedure then
854 F := First_Formal (Subp);
857 if Ekind (Etype (F)) = E_Anonymous_Access_Type
859 Designated_Type (Etype (F)) =
860 Class_Wide_Type (RTE (RE_Root_Stream_Type))
864 and then Parameter_Mode (F) = E_Out_Parameter
865 and then Base_Type (Etype (F)) = Base_Type (Ent)
866 and then No (Next_Formal (F));
872 end Has_Good_Profile;
874 -- Start of processing for Read attribute definition
879 if not Is_Type (U_Ent) then
880 Error_Msg_N ("local name must be a subtype", Nam);
884 Pnam := TSS (Base_Type (U_Ent), Name_uRead);
887 and then Base_Type (Etype (Next_Formal (First_Formal (Pnam))))
890 Error_Msg_Sloc := Sloc (Pnam);
891 Error_Msg_N ("read attribute already defined #", Nam);
898 if Is_Entity_Name (Expr) then
899 if not Is_Overloaded (Expr) then
900 if Has_Good_Profile (Entity (Expr)) then
901 Subp := Entity (Expr);
905 Get_First_Interp (Expr, I, It);
907 while Present (It.Nam) loop
908 if Has_Good_Profile (It.Nam) then
913 Get_Next_Interp (I, It);
918 if Present (Subp) then
919 Set_Entity (Expr, Subp);
920 Set_Etype (Expr, Etype (Subp));
921 New_Stream_Procedure (N, U_Ent, Subp, Name_uRead, True);
923 Error_Msg_N ("incorrect expression for read attribute", Expr);
932 -- Size attribute definition clause
934 when Attribute_Size => Size : declare
935 Size : constant Uint := Static_Integer (Expr);
942 if Has_Size_Clause (U_Ent) then
943 Error_Msg_N ("size already given for &", Nam);
945 elsif not Is_Type (U_Ent)
946 and then Ekind (U_Ent) /= E_Variable
947 and then Ekind (U_Ent) /= E_Constant
949 Error_Msg_N ("size cannot be given for &", Nam);
951 elsif Is_Array_Type (U_Ent)
952 and then not Is_Constrained (U_Ent)
955 ("size cannot be given for unconstrained array", Nam);
957 elsif Size /= No_Uint then
959 if Is_Type (U_Ent) then
962 Etyp := Etype (U_Ent);
965 -- Check size, note that Gigi is in charge of checking
966 -- that the size of an array or record type is OK. Also
967 -- we do not check the size in the ordinary fixed-point
968 -- case, since it is too early to do so (there may be a
969 -- subsequent small clause that affects the size). We can
970 -- check the size if a small clause has already been given.
972 if not Is_Ordinary_Fixed_Point_Type (U_Ent)
973 or else Has_Small_Clause (U_Ent)
975 Check_Size (Expr, Etyp, Size, Biased);
976 Set_Has_Biased_Representation (U_Ent, Biased);
979 -- For types set RM_Size and Esize if possible
981 if Is_Type (U_Ent) then
982 Set_RM_Size (U_Ent, Size);
984 -- For scalar types, increase Object_Size to power of 2,
985 -- but not less than a storage unit in any case (i.e.,
986 -- normally this means it will be byte addressable).
988 if Is_Scalar_Type (U_Ent) then
989 if Size <= System_Storage_Unit then
990 Init_Esize (U_Ent, System_Storage_Unit);
991 elsif Size <= 16 then
992 Init_Esize (U_Ent, 16);
993 elsif Size <= 32 then
994 Init_Esize (U_Ent, 32);
996 Set_Esize (U_Ent, (Size + 63) / 64 * 64);
999 -- For all other types, object size = value size. The
1000 -- backend will adjust as needed.
1003 Set_Esize (U_Ent, Size);
1006 Alignment_Check_For_Esize_Change (U_Ent);
1008 -- For objects, set Esize only
1011 Set_Esize (U_Ent, Size);
1014 Set_Has_Size_Clause (U_Ent);
1022 -- Small attribute definition clause
1024 when Attribute_Small => Small : declare
1025 Implicit_Base : constant Entity_Id := Base_Type (U_Ent);
1029 Analyze_And_Resolve (Expr, Any_Real);
1031 if Etype (Expr) = Any_Type then
1034 elsif not Is_Static_Expression (Expr) then
1035 Error_Msg_N ("small requires static expression", Expr);
1039 Small := Expr_Value_R (Expr);
1041 if Small <= Ureal_0 then
1042 Error_Msg_N ("small value must be greater than zero", Expr);
1048 if not Is_Ordinary_Fixed_Point_Type (U_Ent) then
1050 ("small requires an ordinary fixed point type", Nam);
1052 elsif Has_Small_Clause (U_Ent) then
1053 Error_Msg_N ("small already given for &", Nam);
1055 elsif Small > Delta_Value (U_Ent) then
1057 ("small value must not be greater then delta value", Nam);
1060 Set_Small_Value (U_Ent, Small);
1061 Set_Small_Value (Implicit_Base, Small);
1062 Set_Has_Small_Clause (U_Ent);
1063 Set_Has_Small_Clause (Implicit_Base);
1064 Set_Has_Non_Standard_Rep (Implicit_Base);
1072 -- Storage_Size attribute definition clause
1074 when Attribute_Storage_Size => Storage_Size : declare
1075 Btype : constant Entity_Id := Base_Type (U_Ent);
1079 if Is_Task_Type (U_Ent) then
1083 if not Is_Access_Type (U_Ent)
1084 and then Ekind (U_Ent) /= E_Task_Type
1086 Error_Msg_N ("storage size cannot be given for &", Nam);
1088 elsif Is_Access_Type (U_Ent) and Is_Derived_Type (U_Ent) then
1090 ("storage size cannot be given for a derived access type",
1093 elsif Has_Storage_Size_Clause (Btype) then
1094 Error_Msg_N ("storage size already given for &", Nam);
1097 Analyze_And_Resolve (Expr, Any_Integer);
1099 if Is_Access_Type (U_Ent) then
1101 if Present (Associated_Storage_Pool (U_Ent)) then
1102 Error_Msg_N ("storage pool already given for &", Nam);
1106 if Compile_Time_Known_Value (Expr)
1107 and then Expr_Value (Expr) = 0
1109 Set_No_Pool_Assigned (Btype);
1112 else -- Is_Task_Type (U_Ent)
1113 Sprag := Get_Rep_Pragma (Btype, Name_Storage_Size);
1115 if Present (Sprag) then
1116 Error_Msg_Sloc := Sloc (Sprag);
1118 ("Storage_Size already specified#", Nam);
1123 Set_Has_Storage_Size_Clause (Btype);
1131 -- Storage_Pool attribute definition clause
1133 when Attribute_Storage_Pool => Storage_Pool : declare
1137 if Ekind (U_Ent) /= E_Access_Type
1138 and then Ekind (U_Ent) /= E_General_Access_Type
1141 "storage pool can only be given for access types", Nam);
1144 elsif Is_Derived_Type (U_Ent) then
1146 ("storage pool cannot be given for a derived access type",
1149 elsif Has_Storage_Size_Clause (U_Ent) then
1150 Error_Msg_N ("storage size already given for &", Nam);
1153 elsif Present (Associated_Storage_Pool (U_Ent)) then
1154 Error_Msg_N ("storage pool already given for &", Nam);
1159 (Expr, Class_Wide_Type (RTE (RE_Root_Storage_Pool)));
1161 -- If the argument is a name that is not an entity name, then
1162 -- we construct a renaming operation to define an entity of
1163 -- type storage pool.
1165 if not Is_Entity_Name (Expr)
1166 and then Is_Object_Reference (Expr)
1169 Make_Defining_Identifier (Loc,
1170 Chars => New_Internal_Name ('P'));
1173 Rnode : constant Node_Id :=
1174 Make_Object_Renaming_Declaration (Loc,
1175 Defining_Identifier => Pool,
1177 New_Occurrence_Of (Etype (Expr), Loc),
1181 Insert_Before (N, Rnode);
1183 Set_Associated_Storage_Pool (U_Ent, Pool);
1186 elsif Is_Entity_Name (Expr) then
1187 Pool := Entity (Expr);
1189 -- If pool is a renamed object, get original one. This can
1190 -- happen with an explicit renaming, and within instances.
1192 while Present (Renamed_Object (Pool))
1193 and then Is_Entity_Name (Renamed_Object (Pool))
1195 Pool := Entity (Renamed_Object (Pool));
1198 if Present (Renamed_Object (Pool))
1199 and then Nkind (Renamed_Object (Pool)) = N_Type_Conversion
1200 and then Is_Entity_Name (Expression (Renamed_Object (Pool)))
1202 Pool := Entity (Expression (Renamed_Object (Pool)));
1205 if Present (Etype (Pool))
1206 and then Etype (Pool) /= RTE (RE_Stack_Bounded_Pool)
1207 and then Etype (Pool) /= RTE (RE_Unbounded_Reclaim_Pool)
1209 Set_Associated_Storage_Pool (U_Ent, Pool);
1211 Error_Msg_N ("Non sharable GNAT Pool", Expr);
1214 -- The pool may be specified as the Storage_Pool of some other
1215 -- type. It is rewritten as a class_wide conversion of the
1216 -- corresponding pool entity.
1218 elsif Nkind (Expr) = N_Type_Conversion
1219 and then Is_Entity_Name (Expression (Expr))
1220 and then Nkind (Original_Node (Expr)) = N_Attribute_Reference
1222 Pool := Entity (Expression (Expr));
1224 if Present (Etype (Pool))
1225 and then Etype (Pool) /= RTE (RE_Stack_Bounded_Pool)
1226 and then Etype (Pool) /= RTE (RE_Unbounded_Reclaim_Pool)
1228 Set_Associated_Storage_Pool (U_Ent, Pool);
1230 Error_Msg_N ("Non sharable GNAT Pool", Expr);
1234 Error_Msg_N ("incorrect reference to a Storage Pool", Expr);
1243 -- Value_Size attribute definition clause
1245 when Attribute_Value_Size => Value_Size : declare
1246 Size : constant Uint := Static_Integer (Expr);
1250 if not Is_Type (U_Ent) then
1251 Error_Msg_N ("Value_Size cannot be given for &", Nam);
1254 (Get_Attribute_Definition_Clause
1255 (U_Ent, Attribute_Value_Size))
1257 Error_Msg_N ("Value_Size already given for &", Nam);
1260 if Is_Elementary_Type (U_Ent) then
1261 Check_Size (Expr, U_Ent, Size, Biased);
1262 Set_Has_Biased_Representation (U_Ent, Biased);
1265 Set_RM_Size (U_Ent, Size);
1273 -- Write attribute definition clause
1274 -- check for class-wide case will be performed later
1276 when Attribute_Write => Write : declare
1277 Subp : Entity_Id := Empty;
1282 function Has_Good_Profile (Subp : Entity_Id) return Boolean;
1283 -- Return true if the entity is a procedure with an
1284 -- appropriate profile for the write attribute.
1286 function Has_Good_Profile (Subp : Entity_Id) return Boolean is
1288 Ok : Boolean := False;
1291 if Ekind (Subp) = E_Procedure then
1292 F := First_Formal (Subp);
1295 if Ekind (Etype (F)) = E_Anonymous_Access_Type
1297 Designated_Type (Etype (F)) =
1298 Class_Wide_Type (RTE (RE_Root_Stream_Type))
1302 and then Parameter_Mode (F) = E_In_Parameter
1303 and then Base_Type (Etype (F)) = Base_Type (Ent)
1304 and then No (Next_Formal (F));
1310 end Has_Good_Profile;
1312 -- Start of processing for Write attribute definition
1317 if not Is_Type (U_Ent) then
1318 Error_Msg_N ("local name must be a subtype", Nam);
1322 Pnam := TSS (Base_Type (U_Ent), Name_uWrite);
1325 and then Base_Type (Etype (Next_Formal (First_Formal (Pnam))))
1328 Error_Msg_Sloc := Sloc (Pnam);
1329 Error_Msg_N ("write attribute already defined #", Nam);
1335 if Is_Entity_Name (Expr) then
1336 if not Is_Overloaded (Expr) then
1337 if Has_Good_Profile (Entity (Expr)) then
1338 Subp := Entity (Expr);
1342 Get_First_Interp (Expr, I, It);
1344 while Present (It.Nam) loop
1345 if Has_Good_Profile (It.Nam) then
1350 Get_Next_Interp (I, It);
1355 if Present (Subp) then
1356 Set_Entity (Expr, Subp);
1357 Set_Etype (Expr, Etype (Subp));
1358 New_Stream_Procedure (N, U_Ent, Subp, Name_uWrite);
1360 Error_Msg_N ("incorrect expression for write attribute", Expr);
1365 -- All other attributes cannot be set
1369 ("attribute& cannot be set with definition clause", N);
1373 -- The test for the type being frozen must be performed after
1374 -- any expression the clause has been analyzed since the expression
1375 -- itself might cause freezing that makes the clause illegal.
1377 if Rep_Item_Too_Late (U_Ent, N, FOnly) then
1380 end Analyze_Attribute_Definition_Clause;
1382 ----------------------------
1383 -- Analyze_Code_Statement --
1384 ----------------------------
1386 procedure Analyze_Code_Statement (N : Node_Id) is
1387 HSS : constant Node_Id := Parent (N);
1388 SBody : constant Node_Id := Parent (HSS);
1389 Subp : constant Entity_Id := Current_Scope;
1396 -- Analyze and check we get right type, note that this implements the
1397 -- requirement (RM 13.8(1)) that Machine_Code be with'ed, since that
1398 -- is the only way that Asm_Insn could possibly be visible.
1400 Analyze_And_Resolve (Expression (N));
1402 if Etype (Expression (N)) = Any_Type then
1404 elsif Etype (Expression (N)) /= RTE (RE_Asm_Insn) then
1405 Error_Msg_N ("incorrect type for code statement", N);
1409 -- Make sure we appear in the handled statement sequence of a
1410 -- subprogram (RM 13.8(3)).
1412 if Nkind (HSS) /= N_Handled_Sequence_Of_Statements
1413 or else Nkind (SBody) /= N_Subprogram_Body
1416 ("code statement can only appear in body of subprogram", N);
1420 -- Do remaining checks (RM 13.8(3)) if not already done
1422 if not Is_Machine_Code_Subprogram (Subp) then
1423 Set_Is_Machine_Code_Subprogram (Subp);
1425 -- No exception handlers allowed
1427 if Present (Exception_Handlers (HSS)) then
1429 ("exception handlers not permitted in machine code subprogram",
1430 First (Exception_Handlers (HSS)));
1433 -- No declarations other than use clauses and pragmas (we allow
1434 -- certain internally generated declarations as well).
1436 Decl := First (Declarations (SBody));
1437 while Present (Decl) loop
1438 DeclO := Original_Node (Decl);
1439 if Comes_From_Source (DeclO)
1440 and then Nkind (DeclO) /= N_Pragma
1441 and then Nkind (DeclO) /= N_Use_Package_Clause
1442 and then Nkind (DeclO) /= N_Use_Type_Clause
1443 and then Nkind (DeclO) /= N_Implicit_Label_Declaration
1446 ("this declaration not allowed in machine code subprogram",
1453 -- No statements other than code statements, pragmas, and labels.
1454 -- Again we allow certain internally generated statements.
1456 Stmt := First (Statements (HSS));
1457 while Present (Stmt) loop
1458 StmtO := Original_Node (Stmt);
1459 if Comes_From_Source (StmtO)
1460 and then Nkind (StmtO) /= N_Pragma
1461 and then Nkind (StmtO) /= N_Label
1462 and then Nkind (StmtO) /= N_Code_Statement
1465 ("this statement is not allowed in machine code subprogram",
1473 end Analyze_Code_Statement;
1475 -----------------------------------------------
1476 -- Analyze_Enumeration_Representation_Clause --
1477 -----------------------------------------------
1479 procedure Analyze_Enumeration_Representation_Clause (N : Node_Id) is
1480 Ident : constant Node_Id := Identifier (N);
1481 Aggr : constant Node_Id := Array_Aggregate (N);
1482 Enumtype : Entity_Id;
1488 Err : Boolean := False;
1490 Lo : constant Uint := Expr_Value (Type_Low_Bound (Universal_Integer));
1491 Hi : constant Uint := Expr_Value (Type_High_Bound (Universal_Integer));
1496 -- First some basic error checks
1499 Enumtype := Entity (Ident);
1501 if Enumtype = Any_Type
1502 or else Rep_Item_Too_Early (Enumtype, N)
1506 Enumtype := Underlying_Type (Enumtype);
1509 if not Is_Enumeration_Type (Enumtype) then
1511 ("enumeration type required, found}",
1512 Ident, First_Subtype (Enumtype));
1516 if Scope (Enumtype) /= Current_Scope then
1517 Error_Msg_N ("type must be declared in this scope", Ident);
1520 elsif not Is_First_Subtype (Enumtype) then
1521 Error_Msg_N ("cannot give enumeration rep clause for subtype", N);
1524 elsif Has_Enumeration_Rep_Clause (Enumtype) then
1525 Error_Msg_N ("duplicate enumeration rep clause ignored", N);
1528 elsif Root_Type (Enumtype) = Standard_Character
1529 or else Root_Type (Enumtype) = Standard_Wide_Character
1531 Error_Msg_N ("enumeration rep clause not allowed for this type", N);
1534 Set_Has_Enumeration_Rep_Clause (Enumtype);
1535 Set_Has_Enumeration_Rep_Clause (Base_Type (Enumtype));
1538 -- Now we process the aggregate. Note that we don't use the normal
1539 -- aggregate code for this purpose, because we don't want any of the
1540 -- normal expansion activities, and a number of special semantic
1541 -- rules apply (including the component type being any integer type)
1543 -- Badent signals that we found some incorrect entries processing
1544 -- the list. The final checks for completeness and ordering are
1545 -- skipped in this case.
1547 Elit := First_Literal (Enumtype);
1549 -- First the positional entries if any
1551 if Present (Expressions (Aggr)) then
1552 Expr := First (Expressions (Aggr));
1553 while Present (Expr) loop
1555 Error_Msg_N ("too many entries in aggregate", Expr);
1559 Val := Static_Integer (Expr);
1561 if Val = No_Uint then
1564 elsif Val < Lo or else Hi < Val then
1565 Error_Msg_N ("value outside permitted range", Expr);
1569 Set_Enumeration_Rep (Elit, Val);
1570 Set_Enumeration_Rep_Expr (Elit, Expr);
1576 -- Now process the named entries if present
1578 if Present (Component_Associations (Aggr)) then
1579 Assoc := First (Component_Associations (Aggr));
1580 while Present (Assoc) loop
1581 Choice := First (Choices (Assoc));
1583 if Present (Next (Choice)) then
1585 ("multiple choice not allowed here", Next (Choice));
1589 if Nkind (Choice) = N_Others_Choice then
1590 Error_Msg_N ("others choice not allowed here", Choice);
1593 elsif Nkind (Choice) = N_Range then
1594 -- ??? should allow zero/one element range here
1595 Error_Msg_N ("range not allowed here", Choice);
1599 Analyze_And_Resolve (Choice, Enumtype);
1601 if Is_Entity_Name (Choice)
1602 and then Is_Type (Entity (Choice))
1604 Error_Msg_N ("subtype name not allowed here", Choice);
1606 -- ??? should allow static subtype with zero/one entry
1608 elsif Etype (Choice) = Base_Type (Enumtype) then
1609 if not Is_Static_Expression (Choice) then
1611 ("non-static expression used for choice", Choice);
1615 Elit := Expr_Value_E (Choice);
1617 if Present (Enumeration_Rep_Expr (Elit)) then
1618 Error_Msg_Sloc := Sloc (Enumeration_Rep_Expr (Elit));
1620 ("representation for& previously given#",
1625 Set_Enumeration_Rep_Expr (Elit, Choice);
1627 Expr := Expression (Assoc);
1628 Val := Static_Integer (Expr);
1630 if Val = No_Uint then
1633 elsif Val < Lo or else Hi < Val then
1634 Error_Msg_N ("value outside permitted range", Expr);
1638 Set_Enumeration_Rep (Elit, Val);
1647 -- Aggregate is fully processed. Now we check that a full set of
1648 -- representations was given, and that they are in range and in order.
1649 -- These checks are only done if no other errors occurred.
1655 Elit := First_Literal (Enumtype);
1656 while Present (Elit) loop
1657 if No (Enumeration_Rep_Expr (Elit)) then
1658 Error_Msg_NE ("missing representation for&!", N, Elit);
1661 Val := Enumeration_Rep (Elit);
1663 if Min = No_Uint then
1667 if Val /= No_Uint then
1668 if Max /= No_Uint and then Val <= Max then
1670 ("enumeration value for& not ordered!",
1671 Enumeration_Rep_Expr (Elit), Elit);
1677 -- If there is at least one literal whose representation
1678 -- is not equal to the Pos value, then note that this
1679 -- enumeration type has a non-standard representation.
1681 if Val /= Enumeration_Pos (Elit) then
1682 Set_Has_Non_Standard_Rep (Base_Type (Enumtype));
1689 -- Now set proper size information
1692 Minsize : Uint := UI_From_Int (Minimum_Size (Enumtype));
1695 if Has_Size_Clause (Enumtype) then
1696 if Esize (Enumtype) >= Minsize then
1701 UI_From_Int (Minimum_Size (Enumtype, Biased => True));
1703 if Esize (Enumtype) < Minsize then
1704 Error_Msg_N ("previously given size is too small", N);
1707 Set_Has_Biased_Representation (Enumtype);
1712 Set_RM_Size (Enumtype, Minsize);
1713 Set_Enum_Esize (Enumtype);
1716 Set_RM_Size (Base_Type (Enumtype), RM_Size (Enumtype));
1717 Set_Esize (Base_Type (Enumtype), Esize (Enumtype));
1718 Set_Alignment (Base_Type (Enumtype), Alignment (Enumtype));
1722 -- We repeat the too late test in case it froze itself!
1724 if Rep_Item_Too_Late (Enumtype, N) then
1728 end Analyze_Enumeration_Representation_Clause;
1730 ----------------------------
1731 -- Analyze_Free_Statement --
1732 ----------------------------
1734 procedure Analyze_Free_Statement (N : Node_Id) is
1736 Analyze (Expression (N));
1737 end Analyze_Free_Statement;
1739 ------------------------------------------
1740 -- Analyze_Record_Representation_Clause --
1741 ------------------------------------------
1743 procedure Analyze_Record_Representation_Clause (N : Node_Id) is
1744 Loc : constant Source_Ptr := Sloc (N);
1745 Ident : constant Node_Id := Identifier (N);
1746 Rectype : Entity_Id;
1752 Hbit : Uint := Uint_0;
1757 Max_Bit_So_Far : Uint;
1758 -- Records the maximum bit position so far. If all field positoins
1759 -- are monotonically increasing, then we can skip the circuit for
1760 -- checking for overlap, since no overlap is possible.
1762 Overlap_Check_Required : Boolean;
1763 -- Used to keep track of whether or not an overlap check is required
1765 Ccount : Natural := 0;
1766 -- Number of component clauses in record rep clause
1770 Rectype := Entity (Ident);
1772 if Rectype = Any_Type
1773 or else Rep_Item_Too_Early (Rectype, N)
1777 Rectype := Underlying_Type (Rectype);
1780 -- First some basic error checks
1782 if not Is_Record_Type (Rectype) then
1784 ("record type required, found}", Ident, First_Subtype (Rectype));
1787 elsif Is_Unchecked_Union (Rectype) then
1789 ("record rep clause not allowed for Unchecked_Union", N);
1791 elsif Scope (Rectype) /= Current_Scope then
1792 Error_Msg_N ("type must be declared in this scope", N);
1795 elsif not Is_First_Subtype (Rectype) then
1796 Error_Msg_N ("cannot give record rep clause for subtype", N);
1799 elsif Has_Record_Rep_Clause (Rectype) then
1800 Error_Msg_N ("duplicate record rep clause ignored", N);
1803 elsif Rep_Item_Too_Late (Rectype, N) then
1807 if Present (Mod_Clause (N)) then
1809 Loc : constant Source_Ptr := Sloc (N);
1810 M : constant Node_Id := Mod_Clause (N);
1811 P : constant List_Id := Pragmas_Before (M);
1820 -- In Tree_Output mode, expansion is disabled, but we must
1821 -- convert the Mod clause into an alignment clause anyway, so
1822 -- that the back-end can compute and back-annotate properly the
1823 -- size and alignment of types that may include this record.
1825 if Operating_Mode = Check_Semantics
1826 and then Tree_Output
1829 Make_Attribute_Definition_Clause (Loc,
1830 Name => New_Reference_To (Base_Type (Rectype), Loc),
1831 Chars => Name_Alignment,
1832 Expression => Relocate_Node (Expression (M)));
1834 Set_From_At_Mod (AtM_Nod);
1835 Insert_After (N, AtM_Nod);
1836 Mod_Val := Get_Alignment_Value (Expression (AtM_Nod));
1837 Set_Mod_Clause (N, Empty);
1840 -- Get the alignment value to perform error checking
1842 Mod_Val := Get_Alignment_Value (Expression (M));
1848 -- Clear any existing component clauses for the type (this happens
1849 -- with derived types, where we are now overriding the original)
1851 Fent := First_Entity (Rectype);
1854 while Present (Comp) loop
1855 if Ekind (Comp) = E_Component
1856 or else Ekind (Comp) = E_Discriminant
1858 Set_Component_Clause (Comp, Empty);
1864 -- All done if no component clauses
1866 CC := First (Component_Clauses (N));
1872 -- If a tag is present, then create a component clause that places
1873 -- it at the start of the record (otherwise gigi may place it after
1874 -- other fields that have rep clauses).
1876 if Nkind (Fent) = N_Defining_Identifier
1877 and then Chars (Fent) = Name_uTag
1879 Set_Component_Bit_Offset (Fent, Uint_0);
1880 Set_Normalized_Position (Fent, Uint_0);
1881 Set_Normalized_First_Bit (Fent, Uint_0);
1882 Set_Normalized_Position_Max (Fent, Uint_0);
1883 Init_Esize (Fent, System_Address_Size);
1885 Set_Component_Clause (Fent,
1886 Make_Component_Clause (Loc,
1888 Make_Identifier (Loc,
1889 Chars => Name_uTag),
1892 Make_Integer_Literal (Loc,
1896 Make_Integer_Literal (Loc,
1900 Make_Integer_Literal (Loc,
1901 UI_From_Int (System_Address_Size))));
1903 Ccount := Ccount + 1;
1906 -- A representation like this applies to the base type
1908 Set_Has_Record_Rep_Clause (Base_Type (Rectype));
1909 Set_Has_Non_Standard_Rep (Base_Type (Rectype));
1910 Set_Has_Specified_Layout (Base_Type (Rectype));
1912 Max_Bit_So_Far := Uint_Minus_1;
1913 Overlap_Check_Required := False;
1915 -- Process the component clauses
1917 while Present (CC) loop
1919 -- If pragma, just analyze it
1921 if Nkind (CC) = N_Pragma then
1924 -- Processing for real component clause
1927 Ccount := Ccount + 1;
1928 Posit := Static_Integer (Position (CC));
1929 Fbit := Static_Integer (First_Bit (CC));
1930 Lbit := Static_Integer (Last_Bit (CC));
1933 and then Fbit /= No_Uint
1934 and then Lbit /= No_Uint
1938 ("position cannot be negative", Position (CC));
1942 ("first bit cannot be negative", First_Bit (CC));
1944 -- Values look OK, so find the corresponding record component
1945 -- Even though the syntax allows an attribute reference for
1946 -- implementation-defined components, GNAT does not allow the
1947 -- tag to get an explicit position.
1949 elsif Nkind (Component_Name (CC)) = N_Attribute_Reference then
1951 if Attribute_Name (Component_Name (CC)) = Name_Tag then
1952 Error_Msg_N ("position of tag cannot be specified", CC);
1954 Error_Msg_N ("illegal component name", CC);
1958 Comp := First_Entity (Rectype);
1959 while Present (Comp) loop
1960 exit when Chars (Comp) = Chars (Component_Name (CC));
1966 -- Maybe component of base type that is absent from
1967 -- statically constrained first subtype.
1969 Comp := First_Entity (Base_Type (Rectype));
1970 while Present (Comp) loop
1971 exit when Chars (Comp) = Chars (Component_Name (CC));
1978 ("component clause is for non-existent field", CC);
1980 elsif Present (Component_Clause (Comp)) then
1981 Error_Msg_Sloc := Sloc (Component_Clause (Comp));
1983 ("component clause previously given#", CC);
1986 -- Update Fbit and Lbit to the actual bit number.
1988 Fbit := Fbit + UI_From_Int (SSU) * Posit;
1989 Lbit := Lbit + UI_From_Int (SSU) * Posit;
1991 if Fbit <= Max_Bit_So_Far then
1992 Overlap_Check_Required := True;
1994 Max_Bit_So_Far := Lbit;
1997 if Has_Size_Clause (Rectype)
1998 and then Esize (Rectype) <= Lbit
2001 ("bit number out of range of specified size",
2004 Set_Component_Clause (Comp, CC);
2005 Set_Component_Bit_Offset (Comp, Fbit);
2006 Set_Esize (Comp, 1 + (Lbit - Fbit));
2007 Set_Normalized_First_Bit (Comp, Fbit mod SSU);
2008 Set_Normalized_Position (Comp, Fbit / SSU);
2010 Set_Normalized_Position_Max
2011 (Fent, Normalized_Position (Fent));
2013 if Is_Tagged_Type (Rectype)
2014 and then Fbit < System_Address_Size
2017 ("component overlaps tag field of&",
2021 -- Test for large object that is not on a byte
2022 -- boundary, defined as a large packed array not
2023 -- represented by a modular type, or an object for
2024 -- which a size of greater than 64 bits is specified.
2026 if Fbit mod SSU /= 0 then
2027 if (Is_Packed_Array_Type (Etype (Comp))
2028 and then Is_Array_Type
2029 (Packed_Array_Type (Etype (Comp))))
2030 or else Esize (Etype (Comp)) > 64
2033 ("large component must be on byte boundary",
2038 -- This information is also set in the
2039 -- corresponding component of the base type,
2040 -- found by accessing the Original_Record_Component
2041 -- link if it is present.
2043 Ocomp := Original_Record_Component (Comp);
2050 (Component_Name (CC),
2055 Set_Has_Biased_Representation (Comp, Biased);
2057 if Present (Ocomp) then
2058 Set_Component_Clause (Ocomp, CC);
2059 Set_Component_Bit_Offset (Ocomp, Fbit);
2060 Set_Normalized_First_Bit (Ocomp, Fbit mod SSU);
2061 Set_Normalized_Position (Ocomp, Fbit / SSU);
2062 Set_Esize (Ocomp, 1 + (Lbit - Fbit));
2064 Set_Normalized_Position_Max
2065 (Ocomp, Normalized_Position (Ocomp));
2067 Set_Has_Biased_Representation
2068 (Ocomp, Has_Biased_Representation (Comp));
2071 if Esize (Comp) < 0 then
2072 Error_Msg_N ("component size is negative", CC);
2083 -- Now that we have processed all the component clauses, check for
2084 -- overlap. We have to leave this till last, since the components
2085 -- can appear in any arbitrary order in the representation clause.
2087 -- We do not need this check if all specified ranges were monotonic,
2088 -- as recorded by Overlap_Check_Required being False at this stage.
2090 -- This first section checks if there are any overlapping entries
2091 -- at all. It does this by sorting all entries and then seeing if
2092 -- there are any overlaps. If there are none, then that is decisive,
2093 -- but if there are overlaps, they may still be OK (they may result
2094 -- from fields in different variants).
2096 if Overlap_Check_Required then
2097 Overlap_Check1 : declare
2099 OC_Fbit : array (0 .. Ccount) of Uint;
2100 -- First-bit values for component clauses, the value is the
2101 -- offset of the first bit of the field from start of record.
2102 -- The zero entry is for use in sorting.
2104 OC_Lbit : array (0 .. Ccount) of Uint;
2105 -- Last-bit values for component clauses, the value is the
2106 -- offset of the last bit of the field from start of record.
2107 -- The zero entry is for use in sorting.
2109 OC_Count : Natural := 0;
2110 -- Count of entries in OC_Fbit and OC_Lbit
2112 function OC_Lt (Op1, Op2 : Natural) return Boolean;
2113 -- Compare routine for Sort (See GNAT.Heap_Sort_A)
2115 procedure OC_Move (From : Natural; To : Natural);
2116 -- Move routine for Sort (see GNAT.Heap_Sort_A)
2118 function OC_Lt (Op1, Op2 : Natural) return Boolean is
2120 return OC_Fbit (Op1) < OC_Fbit (Op2);
2123 procedure OC_Move (From : Natural; To : Natural) is
2125 OC_Fbit (To) := OC_Fbit (From);
2126 OC_Lbit (To) := OC_Lbit (From);
2130 CC := First (Component_Clauses (N));
2131 while Present (CC) loop
2132 if Nkind (CC) /= N_Pragma then
2133 Posit := Static_Integer (Position (CC));
2134 Fbit := Static_Integer (First_Bit (CC));
2135 Lbit := Static_Integer (Last_Bit (CC));
2138 and then Fbit /= No_Uint
2139 and then Lbit /= No_Uint
2141 OC_Count := OC_Count + 1;
2142 Posit := Posit * SSU;
2143 OC_Fbit (OC_Count) := Fbit + Posit;
2144 OC_Lbit (OC_Count) := Lbit + Posit;
2153 OC_Move'Unrestricted_Access,
2154 OC_Lt'Unrestricted_Access);
2156 Overlap_Check_Required := False;
2157 for J in 1 .. OC_Count - 1 loop
2158 if OC_Lbit (J) >= OC_Fbit (J + 1) then
2159 Overlap_Check_Required := True;
2166 -- If Overlap_Check_Required is still True, then we have to do
2167 -- the full scale overlap check, since we have at least two fields
2168 -- that do overlap, and we need to know if that is OK since they
2169 -- are in the same variant, or whether we have a definite problem
2171 if Overlap_Check_Required then
2172 Overlap_Check2 : declare
2173 C1_Ent, C2_Ent : Entity_Id;
2174 -- Entities of components being checked for overlap
2177 -- Component_List node whose Component_Items are being checked
2180 -- Component declaration for component being checked
2183 C1_Ent := First_Entity (Base_Type (Rectype));
2185 -- Loop through all components in record. For each component check
2186 -- for overlap with any of the preceding elements on the component
2187 -- list containing the component, and also, if the component is in
2188 -- a variant, check against components outside the case structure.
2189 -- This latter test is repeated recursively up the variant tree.
2191 Main_Component_Loop : while Present (C1_Ent) loop
2192 if Ekind (C1_Ent) /= E_Component
2193 and then Ekind (C1_Ent) /= E_Discriminant
2195 goto Continue_Main_Component_Loop;
2198 -- Skip overlap check if entity has no declaration node. This
2199 -- happens with discriminants in constrained derived types.
2200 -- Probably we are missing some checks as a result, but that
2201 -- does not seem terribly serious ???
2203 if No (Declaration_Node (C1_Ent)) then
2204 goto Continue_Main_Component_Loop;
2207 Clist := Parent (List_Containing (Declaration_Node (C1_Ent)));
2209 -- Loop through component lists that need checking. Check the
2210 -- current component list and all lists in variants above us.
2212 Component_List_Loop : loop
2214 -- If derived type definition, go to full declaration
2215 -- If at outer level, check discriminants if there are any
2217 if Nkind (Clist) = N_Derived_Type_Definition then
2218 Clist := Parent (Clist);
2221 -- Outer level of record definition, check discriminants
2223 if Nkind (Clist) = N_Full_Type_Declaration
2224 or else Nkind (Clist) = N_Private_Type_Declaration
2226 if Has_Discriminants (Defining_Identifier (Clist)) then
2228 First_Discriminant (Defining_Identifier (Clist));
2230 while Present (C2_Ent) loop
2231 exit when C1_Ent = C2_Ent;
2232 Check_Component_Overlap (C1_Ent, C2_Ent);
2233 Next_Discriminant (C2_Ent);
2237 -- Record extension case
2239 elsif Nkind (Clist) = N_Derived_Type_Definition then
2242 -- Otherwise check one component list
2245 Citem := First (Component_Items (Clist));
2247 while Present (Citem) loop
2248 if Nkind (Citem) = N_Component_Declaration then
2249 C2_Ent := Defining_Identifier (Citem);
2250 exit when C1_Ent = C2_Ent;
2251 Check_Component_Overlap (C1_Ent, C2_Ent);
2258 -- Check for variants above us (the parent of the Clist can
2259 -- be a variant, in which case its parent is a variant part,
2260 -- and the parent of the variant part is a component list
2261 -- whose components must all be checked against the current
2262 -- component for overlap.
2264 if Nkind (Parent (Clist)) = N_Variant then
2265 Clist := Parent (Parent (Parent (Clist)));
2267 -- Check for possible discriminant part in record, this is
2268 -- treated essentially as another level in the recursion.
2269 -- For this case we have the parent of the component list
2270 -- is the record definition, and its parent is the full
2271 -- type declaration which contains the discriminant
2274 elsif Nkind (Parent (Clist)) = N_Record_Definition then
2275 Clist := Parent (Parent ((Clist)));
2277 -- If neither of these two cases, we are at the top of
2281 exit Component_List_Loop;
2283 end loop Component_List_Loop;
2285 <<Continue_Main_Component_Loop>>
2286 Next_Entity (C1_Ent);
2288 end loop Main_Component_Loop;
2292 -- For records that have component clauses for all components, and
2293 -- whose size is less than or equal to 32, we need to know the size
2294 -- in the front end to activate possible packed array processing
2295 -- where the component type is a record.
2297 -- At this stage Hbit + 1 represents the first unused bit from all
2298 -- the component clauses processed, so if the component clauses are
2299 -- complete, then this is the length of the record.
2301 -- For records longer than System.Storage_Unit, and for those where
2302 -- not all components have component clauses, the back end determines
2303 -- the length (it may for example be appopriate to round up the size
2304 -- to some convenient boundary, based on alignment considerations etc).
2306 if Unknown_RM_Size (Rectype)
2307 and then Hbit + 1 <= 32
2309 -- Nothing to do if at least one component with no component clause
2311 Comp := First_Entity (Rectype);
2312 while Present (Comp) loop
2313 if Ekind (Comp) = E_Component
2314 or else Ekind (Comp) = E_Discriminant
2316 if No (Component_Clause (Comp)) then
2324 -- If we fall out of loop, all components have component clauses
2325 -- and so we can set the size to the maximum value.
2327 Set_RM_Size (Rectype, Hbit + 1);
2330 end Analyze_Record_Representation_Clause;
2332 -----------------------------
2333 -- Check_Component_Overlap --
2334 -----------------------------
2336 procedure Check_Component_Overlap (C1_Ent, C2_Ent : Entity_Id) is
2338 if Present (Component_Clause (C1_Ent))
2339 and then Present (Component_Clause (C2_Ent))
2341 -- Exclude odd case where we have two tag fields in the same
2342 -- record, both at location zero. This seems a bit strange,
2343 -- but it seems to happen in some circumstances ???
2345 if Chars (C1_Ent) = Name_uTag
2346 and then Chars (C2_Ent) = Name_uTag
2351 -- Here we check if the two fields overlap
2354 S1 : constant Uint := Component_Bit_Offset (C1_Ent);
2355 S2 : constant Uint := Component_Bit_Offset (C2_Ent);
2356 E1 : constant Uint := S1 + Esize (C1_Ent);
2357 E2 : constant Uint := S2 + Esize (C2_Ent);
2360 if E2 <= S1 or else E1 <= S2 then
2364 Component_Name (Component_Clause (C2_Ent));
2365 Error_Msg_Sloc := Sloc (Error_Msg_Node_2);
2367 Component_Name (Component_Clause (C1_Ent));
2369 ("component& overlaps & #",
2370 Component_Name (Component_Clause (C1_Ent)));
2374 end Check_Component_Overlap;
2376 -----------------------------------
2377 -- Check_Constant_Address_Clause --
2378 -----------------------------------
2380 procedure Check_Constant_Address_Clause
2384 procedure Check_At_Constant_Address (Nod : Node_Id);
2385 -- Checks that the given node N represents a name whose 'Address
2386 -- is constant (in the same sense as OK_Constant_Address_Clause,
2387 -- i.e. the address value is the same at the point of declaration
2388 -- of U_Ent and at the time of elaboration of the address clause.
2390 procedure Check_Expr_Constants (Nod : Node_Id);
2391 -- Checks that Nod meets the requirements for a constant address
2392 -- clause in the sense of the enclosing procedure.
2394 procedure Check_List_Constants (Lst : List_Id);
2395 -- Check that all elements of list Lst meet the requirements for a
2396 -- constant address clause in the sense of the enclosing procedure.
2398 -------------------------------
2399 -- Check_At_Constant_Address --
2400 -------------------------------
2402 procedure Check_At_Constant_Address (Nod : Node_Id) is
2404 if Is_Entity_Name (Nod) then
2405 if Present (Address_Clause (Entity ((Nod)))) then
2407 ("invalid address clause for initialized object &!",
2410 ("address for& cannot" &
2411 " depend on another address clause! ('R'M 13.1(22))!",
2414 elsif In_Same_Source_Unit (Entity (Nod), U_Ent)
2415 and then Sloc (U_Ent) < Sloc (Entity (Nod))
2418 ("invalid address clause for initialized object &!",
2420 Error_Msg_Name_1 := Chars (Entity (Nod));
2421 Error_Msg_Name_2 := Chars (U_Ent);
2423 ("\% must be defined before % ('R'M 13.1(22))!",
2427 elsif Nkind (Nod) = N_Selected_Component then
2429 T : constant Entity_Id := Etype (Prefix (Nod));
2432 if (Is_Record_Type (T)
2433 and then Has_Discriminants (T))
2436 and then Is_Record_Type (Designated_Type (T))
2437 and then Has_Discriminants (Designated_Type (T)))
2440 ("invalid address clause for initialized object &!",
2443 ("\address cannot depend on component" &
2444 " of discriminated record ('R'M 13.1(22))!",
2447 Check_At_Constant_Address (Prefix (Nod));
2451 elsif Nkind (Nod) = N_Indexed_Component then
2452 Check_At_Constant_Address (Prefix (Nod));
2453 Check_List_Constants (Expressions (Nod));
2456 Check_Expr_Constants (Nod);
2458 end Check_At_Constant_Address;
2460 --------------------------
2461 -- Check_Expr_Constants --
2462 --------------------------
2464 procedure Check_Expr_Constants (Nod : Node_Id) is
2466 if Nkind (Nod) in N_Has_Etype
2467 and then Etype (Nod) = Any_Type
2473 when N_Empty | N_Error =>
2476 when N_Identifier | N_Expanded_Name =>
2478 Ent : constant Entity_Id := Entity (Nod);
2479 Loc_Ent : constant Source_Ptr := Sloc (Ent);
2480 Loc_U_Ent : constant Source_Ptr := Sloc (U_Ent);
2483 if Ekind (Ent) = E_Named_Integer
2485 Ekind (Ent) = E_Named_Real
2492 Ekind (Ent) = E_Constant
2494 Ekind (Ent) = E_In_Parameter
2496 -- This is the case where we must have Ent defined
2497 -- before U_Ent. Clearly if they are in different
2498 -- units this requirement is met since the unit
2499 -- containing Ent is already processed.
2501 if not In_Same_Source_Unit (Ent, U_Ent) then
2504 -- Otherwise location of Ent must be before the
2505 -- location of U_Ent, that's what prior defined means.
2507 elsif Loc_Ent < Loc_U_Ent then
2512 ("invalid address clause for initialized object &!",
2514 Error_Msg_Name_1 := Chars (Ent);
2515 Error_Msg_Name_2 := Chars (U_Ent);
2517 ("\% must be defined before % ('R'M 13.1(22))!",
2521 elsif Nkind (Original_Node (Nod)) = N_Function_Call then
2522 Check_Expr_Constants (Original_Node (Nod));
2526 ("invalid address clause for initialized object &!",
2528 Error_Msg_Name_1 := Chars (Ent);
2530 ("\reference to variable% not allowed ('R'M 13.1(22))!",
2535 when N_Integer_Literal |
2538 N_Character_Literal =>
2542 Check_Expr_Constants (Low_Bound (Nod));
2543 Check_Expr_Constants (High_Bound (Nod));
2545 when N_Explicit_Dereference =>
2546 Check_Expr_Constants (Prefix (Nod));
2548 when N_Indexed_Component =>
2549 Check_Expr_Constants (Prefix (Nod));
2550 Check_List_Constants (Expressions (Nod));
2553 Check_Expr_Constants (Prefix (Nod));
2554 Check_Expr_Constants (Discrete_Range (Nod));
2556 when N_Selected_Component =>
2557 Check_Expr_Constants (Prefix (Nod));
2559 when N_Attribute_Reference =>
2561 if (Attribute_Name (Nod) = Name_Address
2563 Attribute_Name (Nod) = Name_Access
2565 Attribute_Name (Nod) = Name_Unchecked_Access
2567 Attribute_Name (Nod) = Name_Unrestricted_Access)
2569 Check_At_Constant_Address (Prefix (Nod));
2572 Check_Expr_Constants (Prefix (Nod));
2573 Check_List_Constants (Expressions (Nod));
2577 Check_List_Constants (Component_Associations (Nod));
2578 Check_List_Constants (Expressions (Nod));
2580 when N_Component_Association =>
2581 Check_Expr_Constants (Expression (Nod));
2583 when N_Extension_Aggregate =>
2584 Check_Expr_Constants (Ancestor_Part (Nod));
2585 Check_List_Constants (Component_Associations (Nod));
2586 Check_List_Constants (Expressions (Nod));
2591 when N_Binary_Op | N_And_Then | N_Or_Else | N_In | N_Not_In =>
2592 Check_Expr_Constants (Left_Opnd (Nod));
2593 Check_Expr_Constants (Right_Opnd (Nod));
2596 Check_Expr_Constants (Right_Opnd (Nod));
2598 when N_Type_Conversion |
2599 N_Qualified_Expression |
2601 Check_Expr_Constants (Expression (Nod));
2603 when N_Unchecked_Type_Conversion =>
2604 Check_Expr_Constants (Expression (Nod));
2606 -- If this is a rewritten unchecked conversion, subtypes
2607 -- in this node are those created within the instance.
2608 -- To avoid order of elaboration issues, replace them
2609 -- with their base types. Note that address clauses can
2610 -- cause order of elaboration problems because they are
2611 -- elaborated by the back-end at the point of definition,
2612 -- and may mention entities declared in between (as long
2613 -- as everything is static). It is user-friendly to allow
2614 -- unchecked conversions in this context.
2616 if Nkind (Original_Node (Nod)) = N_Function_Call then
2617 Set_Etype (Expression (Nod),
2618 Base_Type (Etype (Expression (Nod))));
2619 Set_Etype (Nod, Base_Type (Etype (Nod)));
2622 when N_Function_Call =>
2623 if not Is_Pure (Entity (Name (Nod))) then
2625 ("invalid address clause for initialized object &!",
2629 ("\function & is not pure ('R'M 13.1(22))!",
2630 Nod, Entity (Name (Nod)));
2633 Check_List_Constants (Parameter_Associations (Nod));
2636 when N_Parameter_Association =>
2637 Check_Expr_Constants (Explicit_Actual_Parameter (Nod));
2641 ("invalid address clause for initialized object &!",
2644 ("\must be constant defined before& ('R'M 13.1(22))!",
2647 end Check_Expr_Constants;
2649 --------------------------
2650 -- Check_List_Constants --
2651 --------------------------
2653 procedure Check_List_Constants (Lst : List_Id) is
2657 if Present (Lst) then
2658 Nod1 := First (Lst);
2659 while Present (Nod1) loop
2660 Check_Expr_Constants (Nod1);
2664 end Check_List_Constants;
2666 -- Start of processing for Check_Constant_Address_Clause
2669 Check_Expr_Constants (Expr);
2670 end Check_Constant_Address_Clause;
2676 procedure Check_Size
2680 Biased : out Boolean)
2682 UT : constant Entity_Id := Underlying_Type (T);
2688 -- Immediate return if size is same as standard size or if composite
2689 -- item, or generic type, or type with previous errors.
2692 or else UT = Any_Type
2693 or else Is_Generic_Type (UT)
2694 or else Is_Generic_Type (Root_Type (UT))
2695 or else Is_Composite_Type (UT)
2696 or else (Known_Esize (UT) and then Siz = Esize (UT))
2700 -- For fixed-point types, don't check minimum if type is not frozen,
2701 -- since type is not known till then
2704 elsif Is_Fixed_Point_Type (UT)
2705 and then not Is_Frozen (UT)
2709 -- Cases for which a minimum check is required
2712 M := UI_From_Int (Minimum_Size (UT));
2716 -- Size is less than minimum size, but one possibility remains
2717 -- that we can manage with the new size if we bias the type
2719 M := UI_From_Int (Minimum_Size (UT, Biased => True));
2722 Error_Msg_Uint_1 := M;
2724 ("size for& too small, minimum allowed is ^", N, T);
2732 -------------------------
2733 -- Get_Alignment_Value --
2734 -------------------------
2736 function Get_Alignment_Value (Expr : Node_Id) return Uint is
2737 Align : constant Uint := Static_Integer (Expr);
2740 if Align = No_Uint then
2743 elsif Align <= 0 then
2744 Error_Msg_N ("alignment value must be positive", Expr);
2748 for J in Int range 0 .. 64 loop
2750 M : constant Uint := Uint_2 ** J;
2753 exit when M = Align;
2757 ("alignment value must be power of 2", Expr);
2765 end Get_Alignment_Value;
2771 procedure Initialize is
2773 Unchecked_Conversions.Init;
2776 -------------------------
2777 -- Is_Operational_Item --
2778 -------------------------
2780 function Is_Operational_Item (N : Node_Id) return Boolean is
2782 if Nkind (N) /= N_Attribute_Definition_Clause then
2786 Id : constant Attribute_Id := Get_Attribute_Id (Chars (N));
2789 return Id = Attribute_Input
2790 or else Id = Attribute_Output
2791 or else Id = Attribute_Read
2792 or else Id = Attribute_Write
2793 or else Id = Attribute_External_Tag;
2796 end Is_Operational_Item;
2802 function Minimum_Size
2804 Biased : Boolean := False)
2807 Lo : Uint := No_Uint;
2808 Hi : Uint := No_Uint;
2809 LoR : Ureal := No_Ureal;
2810 HiR : Ureal := No_Ureal;
2811 LoSet : Boolean := False;
2812 HiSet : Boolean := False;
2816 R_Typ : constant Entity_Id := Root_Type (T);
2819 -- If bad type, return 0
2821 if T = Any_Type then
2824 -- For generic types, just return zero. There cannot be any legitimate
2825 -- need to know such a size, but this routine may be called with a
2826 -- generic type as part of normal processing.
2828 elsif Is_Generic_Type (R_Typ)
2829 or else R_Typ = Any_Type
2835 elsif Is_Access_Type (T) then
2836 return System_Address_Size;
2838 -- Floating-point types
2840 elsif Is_Floating_Point_Type (T) then
2841 return UI_To_Int (Esize (R_Typ));
2845 elsif Is_Discrete_Type (T) then
2847 -- The following loop is looking for the nearest compile time
2848 -- known bounds following the ancestor subtype chain. The idea
2849 -- is to find the most restrictive known bounds information.
2853 if Ancest = Any_Type or else Etype (Ancest) = Any_Type then
2858 if Compile_Time_Known_Value (Type_Low_Bound (Ancest)) then
2859 Lo := Expr_Rep_Value (Type_Low_Bound (Ancest));
2866 if Compile_Time_Known_Value (Type_High_Bound (Ancest)) then
2867 Hi := Expr_Rep_Value (Type_High_Bound (Ancest));
2873 Ancest := Ancestor_Subtype (Ancest);
2876 Ancest := Base_Type (T);
2878 if Is_Generic_Type (Ancest) then
2884 -- Fixed-point types. We can't simply use Expr_Value to get the
2885 -- Corresponding_Integer_Value values of the bounds, since these
2886 -- do not get set till the type is frozen, and this routine can
2887 -- be called before the type is frozen. Similarly the test for
2888 -- bounds being static needs to include the case where we have
2889 -- unanalyzed real literals for the same reason.
2891 elsif Is_Fixed_Point_Type (T) then
2893 -- The following loop is looking for the nearest compile time
2894 -- known bounds following the ancestor subtype chain. The idea
2895 -- is to find the most restrictive known bounds information.
2899 if Ancest = Any_Type or else Etype (Ancest) = Any_Type then
2904 if Nkind (Type_Low_Bound (Ancest)) = N_Real_Literal
2905 or else Compile_Time_Known_Value (Type_Low_Bound (Ancest))
2907 LoR := Expr_Value_R (Type_Low_Bound (Ancest));
2914 if Nkind (Type_High_Bound (Ancest)) = N_Real_Literal
2915 or else Compile_Time_Known_Value (Type_High_Bound (Ancest))
2917 HiR := Expr_Value_R (Type_High_Bound (Ancest));
2923 Ancest := Ancestor_Subtype (Ancest);
2926 Ancest := Base_Type (T);
2928 if Is_Generic_Type (Ancest) then
2934 Lo := UR_To_Uint (LoR / Small_Value (T));
2935 Hi := UR_To_Uint (HiR / Small_Value (T));
2937 -- No other types allowed
2940 raise Program_Error;
2943 -- Fall through with Hi and Lo set. Deal with biased case.
2945 if (Biased and then not Is_Fixed_Point_Type (T))
2946 or else Has_Biased_Representation (T)
2952 -- Signed case. Note that we consider types like range 1 .. -1 to be
2953 -- signed for the purpose of computing the size, since the bounds
2954 -- have to be accomodated in the base type.
2956 if Lo < 0 or else Hi < 0 then
2960 -- S = size, B = 2 ** (size - 1) (can accommodate -B .. +(B - 1))
2961 -- Note that we accommodate the case where the bounds cross. This
2962 -- can happen either because of the way the bounds are declared
2963 -- or because of the algorithm in Freeze_Fixed_Point_Type.
2977 -- If both bounds are positive, make sure that both are represen-
2978 -- table in the case where the bounds are crossed. This can happen
2979 -- either because of the way the bounds are declared, or because of
2980 -- the algorithm in Freeze_Fixed_Point_Type.
2986 -- S = size, (can accommodate 0 .. (2**size - 1))
2989 while Hi >= Uint_2 ** S loop
2997 -------------------------
2998 -- New_Stream_Function --
2999 -------------------------
3001 procedure New_Stream_Function
3007 Loc : constant Source_Ptr := Sloc (N);
3008 Subp_Id : Entity_Id;
3009 Subp_Decl : Node_Id;
3013 function Build_Spec return Node_Id;
3014 -- Used for declaration and renaming declaration, so that this is
3015 -- treated as a renaming_as_body.
3021 function Build_Spec return Node_Id is
3023 Subp_Id := Make_Defining_Identifier (Loc, Nam);
3026 Make_Function_Specification (Loc,
3027 Defining_Unit_Name => Subp_Id,
3028 Parameter_Specifications =>
3030 Make_Parameter_Specification (Loc,
3031 Defining_Identifier =>
3032 Make_Defining_Identifier (Loc, Name_S),
3034 Make_Access_Definition (Loc,
3037 Designated_Type (Etype (F)), Loc)))),
3040 New_Reference_To (Etyp, Loc));
3043 -- Start of processing for New_Stream_Function
3046 F := First_Formal (Subp);
3047 Etyp := Etype (Subp);
3049 if not Is_Tagged_Type (Ent) then
3051 Make_Subprogram_Declaration (Loc,
3052 Specification => Build_Spec);
3053 Insert_Action (N, Subp_Decl);
3057 Make_Subprogram_Renaming_Declaration (Loc,
3058 Specification => Build_Spec,
3059 Name => New_Reference_To (Subp, Loc));
3061 if Is_Tagged_Type (Ent) and then not Is_Limited_Type (Ent) then
3062 Set_TSS (Base_Type (Ent), Subp_Id);
3064 Insert_Action (N, Subp_Decl);
3065 Copy_TSS (Subp_Id, Base_Type (Ent));
3068 end New_Stream_Function;
3070 --------------------------
3071 -- New_Stream_Procedure --
3072 --------------------------
3074 procedure New_Stream_Procedure
3079 Out_P : Boolean := False)
3081 Loc : constant Source_Ptr := Sloc (N);
3082 Subp_Id : Entity_Id;
3083 Subp_Decl : Node_Id;
3087 function Build_Spec return Node_Id;
3088 -- Used for declaration and renaming declaration, so that this is
3089 -- treated as a renaming_as_body.
3091 function Build_Spec return Node_Id is
3093 Subp_Id := Make_Defining_Identifier (Loc, Nam);
3096 Make_Procedure_Specification (Loc,
3097 Defining_Unit_Name => Subp_Id,
3098 Parameter_Specifications =>
3100 Make_Parameter_Specification (Loc,
3101 Defining_Identifier =>
3102 Make_Defining_Identifier (Loc, Name_S),
3104 Make_Access_Definition (Loc,
3107 Designated_Type (Etype (F)), Loc))),
3109 Make_Parameter_Specification (Loc,
3110 Defining_Identifier =>
3111 Make_Defining_Identifier (Loc, Name_V),
3112 Out_Present => Out_P,
3114 New_Reference_To (Etyp, Loc))));
3117 -- Start of processing for New_Stream_Function
3120 F := First_Formal (Subp);
3121 Etyp := Etype (Next_Formal (F));
3123 if not Is_Tagged_Type (Ent) then
3125 Make_Subprogram_Declaration (Loc,
3126 Specification => Build_Spec);
3127 Insert_Action (N, Subp_Decl);
3131 Make_Subprogram_Renaming_Declaration (Loc,
3132 Specification => Build_Spec,
3133 Name => New_Reference_To (Subp, Loc));
3135 if Is_Tagged_Type (Ent) and then not Is_Limited_Type (Ent) then
3136 Set_TSS (Base_Type (Ent), Subp_Id);
3138 Insert_Action (N, Subp_Decl);
3139 Copy_TSS (Subp_Id, Base_Type (Ent));
3142 end New_Stream_Procedure;
3144 ---------------------
3145 -- Record_Rep_Item --
3146 ---------------------
3148 procedure Record_Rep_Item (T : Entity_Id; N : Node_Id) is
3150 Set_Next_Rep_Item (N, First_Rep_Item (T));
3151 Set_First_Rep_Item (T, N);
3152 end Record_Rep_Item;
3154 ------------------------
3155 -- Rep_Item_Too_Early --
3156 ------------------------
3158 function Rep_Item_Too_Early
3164 -- Cannot apply rep items that are not operational items
3167 if Is_Operational_Item (N) then
3171 and then Is_Generic_Type (Root_Type (T))
3174 ("representation item not allowed for generic type", N);
3178 -- Otherwise check for incompleted type
3180 if Is_Incomplete_Or_Private_Type (T)
3181 and then No (Underlying_Type (T))
3184 ("representation item must be after full type declaration", N);
3187 -- If the type has incompleted components, a representation clause is
3188 -- illegal but stream attributes and Convention pragmas are correct.
3190 elsif Has_Private_Component (T) then
3191 if Nkind (N) = N_Pragma then
3195 ("representation item must appear after type is fully defined",
3202 end Rep_Item_Too_Early;
3204 -----------------------
3205 -- Rep_Item_Too_Late --
3206 -----------------------
3208 function Rep_Item_Too_Late
3211 FOnly : Boolean := False)
3215 Parent_Type : Entity_Id;
3218 -- Output the too late message
3220 procedure Too_Late is
3222 Error_Msg_N ("representation item appears too late!", N);
3225 -- Start of processing for Rep_Item_Too_Late
3228 -- First make sure entity is not frozen (RM 13.1(9)). Exclude imported
3229 -- types, which may be frozen if they appear in a representation clause
3230 -- for a local type.
3233 and then not From_With_Type (T)
3236 S := First_Subtype (T);
3238 if Present (Freeze_Node (S)) then
3240 ("?no more representation items for }!", Freeze_Node (S), S);
3245 -- Check for case of non-tagged derived type whose parent either has
3246 -- primitive operations, or is a by reference type (RM 13.1(10)).
3250 and then Is_Derived_Type (T)
3251 and then not Is_Tagged_Type (T)
3253 Parent_Type := Etype (Base_Type (T));
3255 if Has_Primitive_Operations (Parent_Type) then
3258 ("primitive operations already defined for&!", N, Parent_Type);
3261 elsif Is_By_Reference_Type (Parent_Type) then
3264 ("parent type & is a by reference type!", N, Parent_Type);
3269 -- No error, link item into head of chain of rep items for the entity
3271 Record_Rep_Item (T, N);
3273 end Rep_Item_Too_Late;
3275 -------------------------
3276 -- Same_Representation --
3277 -------------------------
3279 function Same_Representation (Typ1, Typ2 : Entity_Id) return Boolean is
3280 T1 : constant Entity_Id := Underlying_Type (Typ1);
3281 T2 : constant Entity_Id := Underlying_Type (Typ2);
3284 -- A quick check, if base types are the same, then we definitely have
3285 -- the same representation, because the subtype specific representation
3286 -- attributes (Size and Alignment) do not affect representation from
3287 -- the point of view of this test.
3289 if Base_Type (T1) = Base_Type (T2) then
3292 elsif Is_Private_Type (Base_Type (T2))
3293 and then Base_Type (T1) = Full_View (Base_Type (T2))
3298 -- Tagged types never have differing representations
3300 if Is_Tagged_Type (T1) then
3304 -- Representations are definitely different if conventions differ
3306 if Convention (T1) /= Convention (T2) then
3310 -- Representations are different if component alignments differ
3312 if (Is_Record_Type (T1) or else Is_Array_Type (T1))
3314 (Is_Record_Type (T2) or else Is_Array_Type (T2))
3315 and then Component_Alignment (T1) /= Component_Alignment (T2)
3320 -- For arrays, the only real issue is component size. If we know the
3321 -- component size for both arrays, and it is the same, then that's
3322 -- good enough to know we don't have a change of representation.
3324 if Is_Array_Type (T1) then
3325 if Known_Component_Size (T1)
3326 and then Known_Component_Size (T2)
3327 and then Component_Size (T1) = Component_Size (T2)
3333 -- Types definitely have same representation if neither has non-standard
3334 -- representation since default representations are always consistent.
3335 -- If only one has non-standard representation, and the other does not,
3336 -- then we consider that they do not have the same representation. They
3337 -- might, but there is no way of telling early enough.
3339 if Has_Non_Standard_Rep (T1) then
3340 if not Has_Non_Standard_Rep (T2) then
3344 return not Has_Non_Standard_Rep (T2);
3347 -- Here the two types both have non-standard representation, and we
3348 -- need to determine if they have the same non-standard representation
3350 -- For arrays, we simply need to test if the component sizes are the
3351 -- same. Pragma Pack is reflected in modified component sizes, so this
3352 -- check also deals with pragma Pack.
3354 if Is_Array_Type (T1) then
3355 return Component_Size (T1) = Component_Size (T2);
3357 -- Tagged types always have the same representation, because it is not
3358 -- possible to specify different representations for common fields.
3360 elsif Is_Tagged_Type (T1) then
3363 -- Case of record types
3365 elsif Is_Record_Type (T1) then
3367 -- Packed status must conform
3369 if Is_Packed (T1) /= Is_Packed (T2) then
3372 -- Otherwise we must check components. Typ2 maybe a constrained
3373 -- subtype with fewer components, so we compare the components
3374 -- of the base types.
3377 Record_Case : declare
3378 CD1, CD2 : Entity_Id;
3380 function Same_Rep return Boolean;
3381 -- CD1 and CD2 are either components or discriminants. This
3382 -- function tests whether the two have the same representation
3384 function Same_Rep return Boolean is
3386 if No (Component_Clause (CD1)) then
3387 return No (Component_Clause (CD2));
3391 Present (Component_Clause (CD2))
3393 Component_Bit_Offset (CD1) = Component_Bit_Offset (CD2)
3395 Esize (CD1) = Esize (CD2);
3399 -- Start processing for Record_Case
3402 if Has_Discriminants (T1) then
3403 CD1 := First_Discriminant (T1);
3404 CD2 := First_Discriminant (T2);
3406 while Present (CD1) loop
3407 if not Same_Rep then
3410 Next_Discriminant (CD1);
3411 Next_Discriminant (CD2);
3416 CD1 := First_Component (Underlying_Type (Base_Type (T1)));
3417 CD2 := First_Component (Underlying_Type (Base_Type (T2)));
3419 while Present (CD1) loop
3420 if not Same_Rep then
3423 Next_Component (CD1);
3424 Next_Component (CD2);
3432 -- For enumeration types, we must check each literal to see if the
3433 -- representation is the same. Note that we do not permit enumeration
3434 -- representation clauses for Character and Wide_Character, so these
3435 -- cases were already dealt with.
3437 elsif Is_Enumeration_Type (T1) then
3439 Enumeration_Case : declare
3443 L1 := First_Literal (T1);
3444 L2 := First_Literal (T2);
3446 while Present (L1) loop
3447 if Enumeration_Rep (L1) /= Enumeration_Rep (L2) then
3457 end Enumeration_Case;
3459 -- Any other types have the same representation for these purposes
3465 end Same_Representation;
3467 --------------------
3468 -- Set_Enum_Esize --
3469 --------------------
3471 procedure Set_Enum_Esize (T : Entity_Id) is
3479 -- Find the minimum standard size (8,16,32,64) that fits
3481 Lo := Enumeration_Rep (Entity (Type_Low_Bound (T)));
3482 Hi := Enumeration_Rep (Entity (Type_High_Bound (T)));
3485 if Lo >= -Uint_2**07 and then Hi < Uint_2**07 then
3486 Sz := Standard_Character_Size; -- May be > 8 on some targets
3488 elsif Lo >= -Uint_2**15 and then Hi < Uint_2**15 then
3491 elsif Lo >= -Uint_2**31 and then Hi < Uint_2**31 then
3494 else pragma Assert (Lo >= -Uint_2**63 and then Hi < Uint_2**63);
3499 if Hi < Uint_2**08 then
3500 Sz := Standard_Character_Size; -- May be > 8 on some targets
3502 elsif Hi < Uint_2**16 then
3505 elsif Hi < Uint_2**32 then
3508 else pragma Assert (Hi < Uint_2**63);
3513 -- That minimum is the proper size unless we have a foreign convention
3514 -- and the size required is 32 or less, in which case we bump the size
3515 -- up to 32. This is required for C and C++ and seems reasonable for
3516 -- all other foreign conventions.
3518 if Has_Foreign_Convention (T)
3519 and then Esize (T) < Standard_Integer_Size
3521 Init_Esize (T, Standard_Integer_Size);
3529 -----------------------------------
3530 -- Validate_Unchecked_Conversion --
3531 -----------------------------------
3533 procedure Validate_Unchecked_Conversion
3535 Act_Unit : Entity_Id)
3542 -- Obtain source and target types. Note that we call Ancestor_Subtype
3543 -- here because the processing for generic instantiation always makes
3544 -- subtypes, and we want the original frozen actual types.
3546 -- If we are dealing with private types, then do the check on their
3547 -- fully declared counterparts if the full declarations have been
3548 -- encountered (they don't have to be visible, but they must exist!)
3550 Source := Ancestor_Subtype (Etype (First_Formal (Act_Unit)));
3552 if Is_Private_Type (Source)
3553 and then Present (Underlying_Type (Source))
3555 Source := Underlying_Type (Source);
3558 Target := Ancestor_Subtype (Etype (Act_Unit));
3560 -- If either type is generic, the instantiation happens within a
3561 -- generic unit, and there is nothing to check. The proper check
3562 -- will happen when the enclosing generic is instantiated.
3564 if Is_Generic_Type (Source) or else Is_Generic_Type (Target) then
3568 if Is_Private_Type (Target)
3569 and then Present (Underlying_Type (Target))
3571 Target := Underlying_Type (Target);
3574 -- Source may be unconstrained array, but not target
3576 if Is_Array_Type (Target)
3577 and then not Is_Constrained (Target)
3580 ("unchecked conversion to unconstrained array not allowed", N);
3584 -- Make entry in unchecked conversion table for later processing
3585 -- by Validate_Unchecked_Conversions, which will check sizes and
3586 -- alignments (using values set by the back-end where possible).
3588 Unchecked_Conversions.Append
3589 (New_Val => UC_Entry'
3594 -- Generate N_Validate_Unchecked_Conversion node for back end if
3595 -- the back end needs to perform special validation checks. At the
3596 -- current time, only the JVM version requires such checks.
3600 Make_Validate_Unchecked_Conversion (Sloc (N));
3601 Set_Source_Type (Vnode, Source);
3602 Set_Target_Type (Vnode, Target);
3603 Insert_After (N, Vnode);
3605 end Validate_Unchecked_Conversion;
3607 ------------------------------------
3608 -- Validate_Unchecked_Conversions --
3609 ------------------------------------
3611 procedure Validate_Unchecked_Conversions is
3613 for N in Unchecked_Conversions.First .. Unchecked_Conversions.Last loop
3615 T : UC_Entry renames Unchecked_Conversions.Table (N);
3617 Enode : constant Node_Id := T.Enode;
3618 Source : constant Entity_Id := T.Source;
3619 Target : constant Entity_Id := T.Target;
3625 -- This validation check, which warns if we have unequal sizes
3626 -- for unchecked conversion, and thus potentially implementation
3627 -- dependent semantics, is one of the few occasions on which we
3628 -- use the official RM size instead of Esize. See description
3629 -- in Einfo "Handling of Type'Size Values" for details.
3631 if Serious_Errors_Detected = 0
3632 and then Known_Static_RM_Size (Source)
3633 and then Known_Static_RM_Size (Target)
3635 Source_Siz := RM_Size (Source);
3636 Target_Siz := RM_Size (Target);
3638 if Source_Siz /= Target_Siz then
3639 Warn_On_Instance := True;
3641 ("types for unchecked conversion have different sizes?",
3644 if All_Errors_Mode then
3645 Error_Msg_Name_1 := Chars (Source);
3646 Error_Msg_Uint_1 := Source_Siz;
3647 Error_Msg_Name_2 := Chars (Target);
3648 Error_Msg_Uint_2 := Target_Siz;
3650 ("\size of % is ^, size of % is ^?", Enode);
3652 Error_Msg_Uint_1 := UI_Abs (Source_Siz - Target_Siz);
3654 if Is_Discrete_Type (Source)
3655 and then Is_Discrete_Type (Target)
3657 if Source_Siz > Target_Siz then
3659 ("\^ high order bits of source will be ignored?",
3662 elsif Is_Modular_Integer_Type (Source) then
3664 ("\source will be extended with ^ high order " &
3665 "zero bits?", Enode);
3669 ("\source will be extended with ^ high order " &
3674 elsif Source_Siz < Target_Siz then
3675 if Is_Discrete_Type (Target) then
3676 if Bytes_Big_Endian then
3678 ("\target value will include ^ undefined " &
3683 ("\target value will include ^ undefined " &
3690 ("\^ trailing bits of target value will be " &
3691 "undefined?", Enode);
3694 else pragma Assert (Source_Siz > Target_Siz);
3696 ("\^ trailing bits of source will be ignored?",
3701 Warn_On_Instance := False;
3705 -- If both types are access types, we need to check the alignment.
3706 -- If the alignment of both is specified, we can do it here.
3708 if Serious_Errors_Detected = 0
3709 and then Ekind (Source) in Access_Kind
3710 and then Ekind (Target) in Access_Kind
3711 and then Target_Strict_Alignment
3712 and then Present (Designated_Type (Source))
3713 and then Present (Designated_Type (Target))
3716 D_Source : constant Entity_Id := Designated_Type (Source);
3717 D_Target : constant Entity_Id := Designated_Type (Target);
3720 if Known_Alignment (D_Source)
3721 and then Known_Alignment (D_Target)
3724 Source_Align : constant Uint := Alignment (D_Source);
3725 Target_Align : constant Uint := Alignment (D_Target);
3728 if Source_Align < Target_Align
3729 and then not Is_Tagged_Type (D_Source)
3731 Warn_On_Instance := True;
3732 Error_Msg_Uint_1 := Target_Align;
3733 Error_Msg_Uint_2 := Source_Align;
3734 Error_Msg_Node_2 := D_Source;
3736 ("alignment of & (^) is stricter than " &
3737 "alignment of & (^)?", Enode, D_Target);
3739 if All_Errors_Mode then
3741 ("\resulting access value may have invalid " &
3742 "alignment?", Enode);
3745 Warn_On_Instance := False;
3753 end Validate_Unchecked_Conversions;
3759 procedure Warn_Overlay
3764 Old : Entity_Id := Empty;
3768 if not Address_Clause_Overlay_Warnings then
3773 and then (Has_Non_Null_Base_Init_Proc (Typ)
3774 or else Is_Access_Type (Typ))
3775 and then not Is_Imported (Entity (Nam))
3777 if Nkind (Expr) = N_Attribute_Reference
3778 and then Is_Entity_Name (Prefix (Expr))
3780 Old := Entity (Prefix (Expr));
3782 elsif Is_Entity_Name (Expr)
3783 and then Ekind (Entity (Expr)) = E_Constant
3785 Decl := Declaration_Node (Entity (Expr));
3787 if Nkind (Decl) = N_Object_Declaration
3788 and then Present (Expression (Decl))
3789 and then Nkind (Expression (Decl)) = N_Attribute_Reference
3790 and then Is_Entity_Name (Prefix (Expression (Decl)))
3792 Old := Entity (Prefix (Expression (Decl)));
3794 elsif Nkind (Expr) = N_Function_Call then
3798 -- A function call (most likely to To_Address) is probably not
3799 -- an overlay, so skip warning. Ditto if the function call was
3800 -- inlined and transformed into an entity.
3802 elsif Nkind (Original_Node (Expr)) = N_Function_Call then
3806 Decl := Next (Parent (Expr));
3808 -- If a pragma Import follows, we assume that it is for the current
3809 -- target of the address clause, and skip the warning.
3812 and then Nkind (Decl) = N_Pragma
3813 and then Chars (Decl) = Name_Import
3818 if Present (Old) then
3819 Error_Msg_Node_2 := Old;
3821 ("default initialization of & may modify &?",
3825 ("default initialization of & may modify overlaid storage?",
3829 -- Add friendly warning if initialization comes from a packed array
3832 if Is_Record_Type (Typ) then
3837 Comp := First_Component (Typ);
3839 while Present (Comp) loop
3840 if Nkind (Parent (Comp)) = N_Component_Declaration
3841 and then Present (Expression (Parent (Comp)))
3844 elsif Is_Array_Type (Etype (Comp))
3845 and then Present (Packed_Array_Type (Etype (Comp)))
3848 ("packed array component& will be initialized to zero?",
3852 Next_Component (Comp);
3859 ("use pragma Import for & to " &
3860 "suppress initialization ('R'M B.1(24))?",