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 Einfo; use Einfo;
31 with Errout; use Errout;
32 with Exp_Tss; use Exp_Tss;
33 with Exp_Util; use Exp_Util;
34 with Hostparm; use Hostparm;
36 with Nlists; use Nlists;
37 with Nmake; use Nmake;
39 with Rtsfind; use Rtsfind;
41 with Sem_Ch8; use Sem_Ch8;
42 with Sem_Eval; use Sem_Eval;
43 with Sem_Res; use Sem_Res;
44 with Sem_Type; use Sem_Type;
45 with Sem_Util; use Sem_Util;
46 with Stand; use Stand;
47 with Sinfo; use Sinfo;
48 with Snames; use Snames;
50 with Ttypes; use Ttypes;
51 with Tbuild; use Tbuild;
52 with Urealp; use Urealp;
54 with GNAT.Heap_Sort_A; use GNAT.Heap_Sort_A;
56 package body Sem_Ch13 is
58 SSU : constant Pos := System_Storage_Unit;
59 -- Convenient short hand for commonly used constant
61 -----------------------
62 -- Local Subprograms --
63 -----------------------
65 procedure Alignment_Check_For_Esize_Change (Typ : Entity_Id);
66 -- This routine is called after setting the Esize of type entity Typ.
67 -- The purpose is to deal with the situation where an aligment has been
68 -- inherited from a derived type that is no longer appropriate for the
69 -- new Esize value. In this case, we reset the Alignment to unknown.
71 procedure Check_Address_Alignment (E : Entity_Id; Expr : Node_Id);
72 -- Given an object entity E, for which the alignment is known, checks
73 -- to see if Expr (the expression from an Address clause) is a known
74 -- at compile time value, and if so posts a warning if the value is
75 -- not consistent with the known alignment requirement. This is not
76 -- an error, but rather leads to erroneous behavior, but we certainly
77 -- may as well give a warning if we detect this situation.
79 procedure Check_Component_Overlap (C1_Ent, C2_Ent : Entity_Id);
80 -- Given two entities for record components or discriminants, checks
81 -- if they hav overlapping component clauses and issues errors if so.
83 function Get_Alignment_Value (Expr : Node_Id) return Uint;
84 -- Given the expression for an alignment value, returns the corresponding
85 -- Uint value. If the value is inappropriate, then error messages are
86 -- posted as required, and a value of No_Uint is returned.
88 function Is_Operational_Item (N : Node_Id) return Boolean;
89 -- A specification for a stream attribute is allowed before the full
90 -- type is declared, as explained in AI-00137 and the corrigendum.
91 -- Attributes that do not specify a representation characteristic are
92 -- operational attributes.
94 procedure New_Stream_Function
99 -- Create a function renaming of a given stream attribute to the
100 -- designated subprogram and then in the tagged case, provide this as
101 -- a primitive operation, or in the non-tagged case make an appropriate
102 -- TSS entry. Used for Input. This is more properly an expansion activity
103 -- than just semantics, but the presence of user-defined stream functions
104 -- for limited types is a legality check, which is why this takes place
105 -- here rather than in exp_ch13, where it was previously.
107 procedure New_Stream_Procedure
112 Out_P : Boolean := False);
113 -- Create a procedure renaming of a given stream attribute to the
114 -- designated subprogram and then in the tagged case, provide this as
115 -- a primitive operation, or in the non-tagged case make an appropriate
116 -- TSS entry. Used for Read, Output, Write.
118 procedure Check_Constant_Address_Clause (Expr : Node_Id; U_Ent : Entity_Id);
119 -- Expr is an expression for an address clause. This procedure checks
120 -- that the expression is constant, in the limited sense that it is safe
121 -- to evaluate it at the point the object U_Ent is declared, rather than
122 -- at the point of the address clause. The condition for this to be true
123 -- is that the expression has no variables, no constants declared after
124 -- U_Ent, and no calls to non-pure functions. If this condition is not
125 -- met, then an appropriate error message is posted.
127 procedure Warn_Overlay
131 -- Expr is the expression for an address clause for entity Nam whose type
132 -- is Typ. If Typ has a default initialization, check whether the address
133 -- clause might overlay two entities, and emit a warning on the side effect
134 -- that the initialization will cause.
136 ----------------------------------------------
137 -- Table for Validate_Unchecked_Conversions --
138 ----------------------------------------------
140 -- The following table collects unchecked conversions for validation.
141 -- Entries are made by Validate_Unchecked_Conversion and then the
142 -- call to Validate_Unchecked_Conversions does the actual error
143 -- checking and posting of warnings. The reason for this delayed
144 -- processing is to take advantage of back-annotations of size and
145 -- alignment values peformed by the back end.
147 type UC_Entry is record
148 Enode : Node_Id; -- node used for posting warnings
149 Source : Entity_Id; -- source type for unchecked conversion
150 Target : Entity_Id; -- target type for unchecked conversion
153 package Unchecked_Conversions is new Table.Table (
154 Table_Component_Type => UC_Entry,
155 Table_Index_Type => Int,
156 Table_Low_Bound => 1,
158 Table_Increment => 200,
159 Table_Name => "Unchecked_Conversions");
161 --------------------------------------
162 -- Alignment_Check_For_Esize_Change --
163 --------------------------------------
165 procedure Alignment_Check_For_Esize_Change (Typ : Entity_Id) is
167 -- If the alignment is known, and not set by a rep clause, and is
168 -- inconsistent with the size being set, then reset it to unknown,
169 -- we assume in this case that the size overrides the inherited
170 -- alignment, and that the alignment must be recomputed.
172 if Known_Alignment (Typ)
173 and then not Has_Alignment_Clause (Typ)
174 and then Esize (Typ) mod (Alignment (Typ) * SSU) /= 0
176 Init_Alignment (Typ);
178 end Alignment_Check_For_Esize_Change;
180 -----------------------
181 -- Analyze_At_Clause --
182 -----------------------
184 -- An at clause is replaced by the corresponding Address attribute
185 -- definition clause that is the preferred approach in Ada 95.
187 procedure Analyze_At_Clause (N : Node_Id) is
190 Make_Attribute_Definition_Clause (Sloc (N),
191 Name => Identifier (N),
192 Chars => Name_Address,
193 Expression => Expression (N)));
194 Analyze_Attribute_Definition_Clause (N);
195 end Analyze_At_Clause;
197 -----------------------------------------
198 -- Analyze_Attribute_Definition_Clause --
199 -----------------------------------------
201 procedure Analyze_Attribute_Definition_Clause (N : Node_Id) is
202 Loc : constant Source_Ptr := Sloc (N);
203 Nam : constant Node_Id := Name (N);
204 Attr : constant Name_Id := Chars (N);
205 Expr : constant Node_Id := Expression (N);
206 Id : constant Attribute_Id := Get_Attribute_Id (Attr);
210 FOnly : Boolean := False;
211 -- Reset to True for subtype specific attribute (Alignment, Size)
212 -- and for stream attributes, i.e. those cases where in the call
213 -- to Rep_Item_Too_Late, FOnly is set True so that only the freezing
214 -- rules are checked. Note that the case of stream attributes is not
215 -- clear from the RM, but see AI95-00137. Also, the RM seems to
216 -- disallow Storage_Size for derived task types, but that is also
217 -- clearly unintentional.
223 if Rep_Item_Too_Early (Ent, N) then
227 -- Rep clause applies to full view of incomplete type or private type
228 -- if we have one (if not, this is a premature use of the type).
229 -- However, certain semantic checks need to be done on the specified
230 -- entity (i.e. the private view), so we save it in Ent.
232 if Is_Private_Type (Ent)
233 and then Is_Derived_Type (Ent)
234 and then not Is_Tagged_Type (Ent)
235 and then No (Full_View (Ent))
237 -- If this is a private type whose completion is a derivation
238 -- from another private type, there is no full view, and the
239 -- attribute belongs to the type itself, not its underlying parent.
243 elsif Ekind (Ent) = E_Incomplete_Type then
244 Ent := Underlying_Type (Ent);
247 U_Ent := Underlying_Type (Ent);
250 -- Complete other routine error checks
252 if Etype (Nam) = Any_Type then
255 elsif Scope (Ent) /= Current_Scope then
256 Error_Msg_N ("entity must be declared in this scope", Nam);
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 -- Case of address clause for variable or constant
321 Ekind (U_Ent) = E_Variable
323 Ekind (U_Ent) = E_Constant
326 Decl : constant Node_Id := Declaration_Node (U_Ent);
327 Expr : constant Node_Id := Expression (N);
328 Typ : constant Entity_Id := Etype (U_Ent);
331 -- Exported variables cannot have an address clause,
332 -- because this cancels the effect of the pragma Export
334 if Is_Exported (U_Ent) then
336 ("cannot export object with address clause", Nam);
338 -- Imported variables can have an address clause, but then
339 -- the import is pretty meaningless except to suppress
340 -- initializations, so we do not need such variables to
341 -- be statically allocated (and in fact it causes trouble
342 -- if the address clause is a local value).
344 elsif Is_Imported (U_Ent) then
345 Set_Is_Statically_Allocated (U_Ent, False);
348 -- We mark a possible modification of a variable with an
349 -- address clause, since it is likely aliasing is occurring.
351 Note_Possible_Modification (Nam);
353 -- If we have no initialization of any kind, then we can
354 -- safely defer the elaboration of the variable to its
355 -- freezing point, so that the address clause will be
356 -- computed at the proper point.
358 -- The same processing applies to all initialized scalar
359 -- types and all access types. Packed bit arrays of size
360 -- up to 64 are represented using a modular type with an
361 -- initialization (to zero) and can be processed like
362 -- other initialized scalar types.
364 if (No (Expression (Decl))
365 and then not Has_Non_Null_Base_Init_Proc (Typ))
368 (Present (Expression (Decl))
369 and then Is_Scalar_Type (Typ))
375 (Is_Bit_Packed_Array (Base_Type (Typ))
377 Is_Modular_Integer_Type (Packed_Array_Type (Typ)))
379 Set_Has_Delayed_Freeze (U_Ent);
381 -- Otherwise, we require the address clause to be constant
384 Check_Constant_Address_Clause (Expr, U_Ent);
387 if Is_Exported (U_Ent) then
389 ("& cannot be exported if an address clause is given",
392 ("\define and export a variable " &
393 "that holds its address instead",
397 if not Error_Posted (Expr) then
398 Warn_Overlay (Expr, Typ, Nam);
401 -- Check for bad alignment
403 if Known_Alignment (U_Ent) then
404 Check_Address_Alignment (U_Ent, Expr);
407 -- Kill the size check code, since we are not allocating
408 -- the variable, it is somewhere else.
410 Kill_Size_Check_Code (U_Ent);
413 -- Not a valid entity for an address clause
416 Error_Msg_N ("address cannot be given for &", Nam);
424 -- Alignment attribute definition clause
426 when Attribute_Alignment => Alignment_Block : declare
427 Align : Uint := Get_Alignment_Value (Expr);
432 if not Is_Type (U_Ent)
433 and then Ekind (U_Ent) /= E_Variable
434 and then Ekind (U_Ent) /= E_Constant
436 Error_Msg_N ("alignment cannot be given for &", Nam);
438 elsif Has_Alignment_Clause (U_Ent) then
439 Error_Msg_Sloc := Sloc (Alignment_Clause (U_Ent));
440 Error_Msg_N ("alignment clause previously given#", N);
442 elsif Align /= No_Uint then
443 Set_Has_Alignment_Clause (U_Ent);
444 Set_Alignment (U_Ent, Align);
452 -- Bit_Order attribute definition clause
454 when Attribute_Bit_Order => Bit_Order : declare
456 if not Is_Record_Type (U_Ent) then
458 ("Bit_Order can only be defined for record type", Nam);
461 Analyze_And_Resolve (Expr, RTE (RE_Bit_Order));
463 if Etype (Expr) = Any_Type then
466 elsif not Is_Static_Expression (Expr) then
467 Error_Msg_N ("Bit_Order requires static expression", Expr);
470 if (Expr_Value (Expr) = 0) /= Bytes_Big_Endian then
471 Set_Reverse_Bit_Order (U_Ent, True);
481 -- Component_Size attribute definition clause
483 when Attribute_Component_Size => Component_Size_Case : declare
484 Csize : constant Uint := Static_Integer (Expr);
487 New_Ctyp : Entity_Id;
491 if not Is_Array_Type (U_Ent) then
492 Error_Msg_N ("component size requires array type", Nam);
496 Btype := Base_Type (U_Ent);
498 if Has_Component_Size_Clause (Btype) then
500 ("component size clase for& previously given", Nam);
502 elsif Csize /= No_Uint then
503 Check_Size (Expr, Component_Type (Btype), Csize, Biased);
505 if Has_Aliased_Components (Btype)
511 ("component size incorrect for aliased components", N);
515 -- For the biased case, build a declaration for a subtype
516 -- that will be used to represent the biased subtype that
517 -- reflects the biased representation of components. We need
518 -- this subtype to get proper conversions on referencing
519 -- elements of the array.
523 Make_Defining_Identifier (Loc,
524 Chars => New_External_Name (Chars (U_Ent), 'C', 0, 'T'));
527 Make_Subtype_Declaration (Loc,
528 Defining_Identifier => New_Ctyp,
529 Subtype_Indication =>
530 New_Occurrence_Of (Component_Type (Btype), Loc));
532 Set_Parent (Decl, N);
533 Analyze (Decl, Suppress => All_Checks);
535 Set_Has_Delayed_Freeze (New_Ctyp, False);
536 Set_Esize (New_Ctyp, Csize);
537 Set_RM_Size (New_Ctyp, Csize);
538 Init_Alignment (New_Ctyp);
539 Set_Has_Biased_Representation (New_Ctyp, True);
540 Set_Is_Itype (New_Ctyp, True);
541 Set_Associated_Node_For_Itype (New_Ctyp, U_Ent);
543 Set_Component_Type (Btype, New_Ctyp);
546 Set_Component_Size (Btype, Csize);
547 Set_Has_Component_Size_Clause (Btype, True);
548 Set_Has_Non_Standard_Rep (Btype, True);
550 end Component_Size_Case;
556 when Attribute_External_Tag => External_Tag :
558 if not Is_Tagged_Type (U_Ent) then
559 Error_Msg_N ("should be a tagged type", Nam);
562 Analyze_And_Resolve (Expr, Standard_String);
564 if not Is_Static_Expression (Expr) then
565 Error_Msg_N ("must be a static string", Nam);
568 Set_Has_External_Tag_Rep_Clause (U_Ent);
575 when Attribute_Input => Input : declare
576 Subp : Entity_Id := Empty;
581 function Has_Good_Profile (Subp : Entity_Id) return Boolean;
582 -- Return true if the entity is a function with an appropriate
583 -- profile for the Input attribute.
585 function Has_Good_Profile (Subp : Entity_Id) return Boolean is
587 Ok : Boolean := False;
590 if Ekind (Subp) = E_Function then
591 F := First_Formal (Subp);
593 if Present (F) and then No (Next_Formal (F)) then
594 if Ekind (Etype (F)) = E_Anonymous_Access_Type
596 Designated_Type (Etype (F)) =
597 Class_Wide_Type (RTE (RE_Root_Stream_Type))
599 Ok := Base_Type (Etype (Subp)) = Base_Type (Ent);
605 end Has_Good_Profile;
607 -- Start of processing for Input attribute definition
612 if not Is_Type (U_Ent) then
613 Error_Msg_N ("local name must be a subtype", Nam);
617 Pnam := TSS (Base_Type (U_Ent), Name_uInput);
620 and then Base_Type (Etype (Pnam)) = Base_Type (U_Ent)
622 Error_Msg_Sloc := Sloc (Pnam);
623 Error_Msg_N ("input attribute already defined #", Nam);
630 if Is_Entity_Name (Expr) then
631 if not Is_Overloaded (Expr) then
632 if Has_Good_Profile (Entity (Expr)) then
633 Subp := Entity (Expr);
637 Get_First_Interp (Expr, I, It);
639 while Present (It.Nam) loop
640 if Has_Good_Profile (It.Nam) then
645 Get_Next_Interp (I, It);
650 if Present (Subp) then
651 Set_Entity (Expr, Subp);
652 Set_Etype (Expr, Etype (Subp));
653 New_Stream_Function (N, U_Ent, Subp, Name_uInput);
655 Error_Msg_N ("incorrect expression for input attribute", Expr);
664 -- Machine radix attribute definition clause
666 when Attribute_Machine_Radix => Machine_Radix : declare
667 Radix : constant Uint := Static_Integer (Expr);
670 if not Is_Decimal_Fixed_Point_Type (U_Ent) then
671 Error_Msg_N ("decimal fixed-point type expected for &", Nam);
673 elsif Has_Machine_Radix_Clause (U_Ent) then
674 Error_Msg_Sloc := Sloc (Alignment_Clause (U_Ent));
675 Error_Msg_N ("machine radix clause previously given#", N);
677 elsif Radix /= No_Uint then
678 Set_Has_Machine_Radix_Clause (U_Ent);
679 Set_Has_Non_Standard_Rep (Base_Type (U_Ent));
683 elsif Radix = 10 then
684 Set_Machine_Radix_10 (U_Ent);
686 Error_Msg_N ("machine radix value must be 2 or 10", Expr);
695 -- Object_Size attribute definition clause
697 when Attribute_Object_Size => Object_Size : declare
698 Size : constant Uint := Static_Integer (Expr);
702 if not Is_Type (U_Ent) then
703 Error_Msg_N ("Object_Size cannot be given for &", Nam);
705 elsif Has_Object_Size_Clause (U_Ent) then
706 Error_Msg_N ("Object_Size already given for &", Nam);
709 Check_Size (Expr, U_Ent, Size, Biased);
717 UI_Mod (Size, 64) /= 0
720 ("Object_Size must be 8, 16, 32, or multiple of 64",
724 Set_Esize (U_Ent, Size);
725 Set_Has_Object_Size_Clause (U_Ent);
726 Alignment_Check_For_Esize_Change (U_Ent);
734 when Attribute_Output => Output : declare
735 Subp : Entity_Id := Empty;
740 function Has_Good_Profile (Subp : Entity_Id) return Boolean;
741 -- Return true if the entity is a procedure with an
742 -- appropriate profile for the output attribute.
744 function Has_Good_Profile (Subp : Entity_Id) return Boolean is
746 Ok : Boolean := False;
749 if Ekind (Subp) = E_Procedure then
750 F := First_Formal (Subp);
753 if Ekind (Etype (F)) = E_Anonymous_Access_Type
755 Designated_Type (Etype (F)) =
756 Class_Wide_Type (RTE (RE_Root_Stream_Type))
760 and then Parameter_Mode (F) = E_In_Parameter
761 and then Base_Type (Etype (F)) = Base_Type (Ent)
762 and then No (Next_Formal (F));
768 end Has_Good_Profile;
773 if not Is_Type (U_Ent) then
774 Error_Msg_N ("local name must be a subtype", Nam);
778 Pnam := TSS (Base_Type (U_Ent), Name_uOutput);
782 Base_Type (Etype (Next_Formal (First_Formal (Pnam))))
785 Error_Msg_Sloc := Sloc (Pnam);
786 Error_Msg_N ("output attribute already defined #", Nam);
793 if Is_Entity_Name (Expr) then
794 if not Is_Overloaded (Expr) then
795 if Has_Good_Profile (Entity (Expr)) then
796 Subp := Entity (Expr);
800 Get_First_Interp (Expr, I, It);
802 while Present (It.Nam) loop
803 if Has_Good_Profile (It.Nam) then
808 Get_Next_Interp (I, It);
813 if Present (Subp) then
814 Set_Entity (Expr, Subp);
815 Set_Etype (Expr, Etype (Subp));
816 New_Stream_Procedure (N, U_Ent, Subp, Name_uOutput);
818 Error_Msg_N ("incorrect expression for output attribute", Expr);
827 when Attribute_Read => Read : declare
828 Subp : Entity_Id := Empty;
833 function Has_Good_Profile (Subp : Entity_Id) return Boolean;
834 -- Return true if the entity is a procedure with an appropriate
835 -- profile for the Read attribute.
837 function Has_Good_Profile (Subp : Entity_Id) return Boolean is
839 Ok : Boolean := False;
842 if Ekind (Subp) = E_Procedure then
843 F := First_Formal (Subp);
846 if Ekind (Etype (F)) = E_Anonymous_Access_Type
848 Designated_Type (Etype (F)) =
849 Class_Wide_Type (RTE (RE_Root_Stream_Type))
853 and then Parameter_Mode (F) = E_Out_Parameter
854 and then Base_Type (Etype (F)) = Base_Type (Ent)
855 and then No (Next_Formal (F));
861 end Has_Good_Profile;
863 -- Start of processing for Read attribute definition
868 if not Is_Type (U_Ent) then
869 Error_Msg_N ("local name must be a subtype", Nam);
873 Pnam := TSS (Base_Type (U_Ent), Name_uRead);
876 and then Base_Type (Etype (Next_Formal (First_Formal (Pnam))))
879 Error_Msg_Sloc := Sloc (Pnam);
880 Error_Msg_N ("read attribute already defined #", Nam);
887 if Is_Entity_Name (Expr) then
888 if not Is_Overloaded (Expr) then
889 if Has_Good_Profile (Entity (Expr)) then
890 Subp := Entity (Expr);
894 Get_First_Interp (Expr, I, It);
896 while Present (It.Nam) loop
897 if Has_Good_Profile (It.Nam) then
902 Get_Next_Interp (I, It);
907 if Present (Subp) then
908 Set_Entity (Expr, Subp);
909 Set_Etype (Expr, Etype (Subp));
910 New_Stream_Procedure (N, U_Ent, Subp, Name_uRead, True);
912 Error_Msg_N ("incorrect expression for read attribute", Expr);
921 -- Size attribute definition clause
923 when Attribute_Size => Size : declare
924 Size : constant Uint := Static_Integer (Expr);
931 if Has_Size_Clause (U_Ent) then
932 Error_Msg_N ("size already given for &", Nam);
934 elsif not Is_Type (U_Ent)
935 and then Ekind (U_Ent) /= E_Variable
936 and then Ekind (U_Ent) /= E_Constant
938 Error_Msg_N ("size cannot be given for &", Nam);
940 elsif Is_Array_Type (U_Ent)
941 and then not Is_Constrained (U_Ent)
944 ("size cannot be given for unconstrained array", Nam);
946 elsif Size /= No_Uint then
948 if Is_Type (U_Ent) then
951 Etyp := Etype (U_Ent);
954 -- Check size, note that Gigi is in charge of checking
955 -- that the size of an array or record type is OK. Also
956 -- we do not check the size in the ordinary fixed-point
957 -- case, since it is too early to do so (there may be a
958 -- subsequent small clause that affects the size). We can
959 -- check the size if a small clause has already been given.
961 if not Is_Ordinary_Fixed_Point_Type (U_Ent)
962 or else Has_Small_Clause (U_Ent)
964 Check_Size (Expr, Etyp, Size, Biased);
965 Set_Has_Biased_Representation (U_Ent, Biased);
968 -- For types set RM_Size and Esize if possible
970 if Is_Type (U_Ent) then
971 Set_RM_Size (U_Ent, Size);
973 -- For scalar types, increase Object_Size to power of 2,
974 -- but not less than 8 in any case, i.e. byte addressable.
976 if Is_Scalar_Type (U_Ent) then
978 Init_Esize (U_Ent, 8);
979 elsif Size <= 16 then
980 Init_Esize (U_Ent, 16);
981 elsif Size <= 32 then
982 Init_Esize (U_Ent, 32);
984 Set_Esize (U_Ent, (Size + 63) / 64 * 64);
987 -- For all other types, object size = value size. The
988 -- backend will adjust as needed.
991 Set_Esize (U_Ent, Size);
994 Alignment_Check_For_Esize_Change (U_Ent);
996 -- For objects, set Esize only
999 Set_Esize (U_Ent, Size);
1002 Set_Has_Size_Clause (U_Ent);
1010 -- Small attribute definition clause
1012 when Attribute_Small => Small : declare
1013 Implicit_Base : constant Entity_Id := Base_Type (U_Ent);
1017 Analyze_And_Resolve (Expr, Any_Real);
1019 if Etype (Expr) = Any_Type then
1022 elsif not Is_Static_Expression (Expr) then
1023 Error_Msg_N ("small requires static expression", Expr);
1027 Small := Expr_Value_R (Expr);
1029 if Small <= Ureal_0 then
1030 Error_Msg_N ("small value must be greater than zero", Expr);
1036 if not Is_Ordinary_Fixed_Point_Type (U_Ent) then
1038 ("small requires an ordinary fixed point type", Nam);
1040 elsif Has_Small_Clause (U_Ent) then
1041 Error_Msg_N ("small already given for &", Nam);
1043 elsif Small > Delta_Value (U_Ent) then
1045 ("small value must not be greater then delta value", Nam);
1048 Set_Small_Value (U_Ent, Small);
1049 Set_Small_Value (Implicit_Base, Small);
1050 Set_Has_Small_Clause (U_Ent);
1051 Set_Has_Small_Clause (Implicit_Base);
1052 Set_Has_Non_Standard_Rep (Implicit_Base);
1060 -- Storage_Size attribute definition clause
1062 when Attribute_Storage_Size => Storage_Size : declare
1063 Btype : constant Entity_Id := Base_Type (U_Ent);
1067 if Is_Task_Type (U_Ent) then
1071 if not Is_Access_Type (U_Ent)
1072 and then Ekind (U_Ent) /= E_Task_Type
1074 Error_Msg_N ("storage size cannot be given for &", Nam);
1076 elsif Is_Access_Type (U_Ent) and Is_Derived_Type (U_Ent) then
1078 ("storage size cannot be given for a derived access type",
1081 elsif Has_Storage_Size_Clause (Btype) then
1082 Error_Msg_N ("storage size already given for &", Nam);
1085 Analyze_And_Resolve (Expr, Any_Integer);
1087 if Is_Access_Type (U_Ent) then
1089 if Present (Associated_Storage_Pool (U_Ent)) then
1090 Error_Msg_N ("storage pool already given for &", Nam);
1094 if Compile_Time_Known_Value (Expr)
1095 and then Expr_Value (Expr) = 0
1097 Set_No_Pool_Assigned (Btype);
1100 else -- Is_Task_Type (U_Ent)
1101 Sprag := Get_Rep_Pragma (Btype, Name_Storage_Size);
1103 if Present (Sprag) then
1104 Error_Msg_Sloc := Sloc (Sprag);
1106 ("Storage_Size already specified#", Nam);
1111 Set_Has_Storage_Size_Clause (Btype);
1119 -- Storage_Pool attribute definition clause
1121 when Attribute_Storage_Pool => Storage_Pool : declare
1125 if Ekind (U_Ent) /= E_Access_Type
1126 and then Ekind (U_Ent) /= E_General_Access_Type
1129 "storage pool can only be given for access types", Nam);
1132 elsif Is_Derived_Type (U_Ent) then
1134 ("storage pool cannot be given for a derived access type",
1137 elsif Has_Storage_Size_Clause (U_Ent) then
1138 Error_Msg_N ("storage size already given for &", Nam);
1141 elsif Present (Associated_Storage_Pool (U_Ent)) then
1142 Error_Msg_N ("storage pool already given for &", Nam);
1147 (Expr, Class_Wide_Type (RTE (RE_Root_Storage_Pool)));
1149 -- If the argument is a name that is not an entity name, then
1150 -- we construct a renaming operation to define an entity of
1151 -- type storage pool.
1153 if not Is_Entity_Name (Expr)
1154 and then Is_Object_Reference (Expr)
1157 Make_Defining_Identifier (Loc,
1158 Chars => New_Internal_Name ('P'));
1161 Rnode : constant Node_Id :=
1162 Make_Object_Renaming_Declaration (Loc,
1163 Defining_Identifier => Pool,
1165 New_Occurrence_Of (Etype (Expr), Loc),
1169 Insert_Before (N, Rnode);
1171 Set_Associated_Storage_Pool (U_Ent, Pool);
1174 elsif Is_Entity_Name (Expr) then
1175 Pool := Entity (Expr);
1177 -- If pool is a renamed object, get original one. This can
1178 -- happen with an explicit renaming, and within instances.
1180 while Present (Renamed_Object (Pool))
1181 and then Is_Entity_Name (Renamed_Object (Pool))
1183 Pool := Entity (Renamed_Object (Pool));
1186 if Present (Renamed_Object (Pool))
1187 and then Nkind (Renamed_Object (Pool)) = N_Type_Conversion
1188 and then Is_Entity_Name (Expression (Renamed_Object (Pool)))
1190 Pool := Entity (Expression (Renamed_Object (Pool)));
1193 if Present (Etype (Pool))
1194 and then Etype (Pool) /= RTE (RE_Stack_Bounded_Pool)
1195 and then Etype (Pool) /= RTE (RE_Unbounded_Reclaim_Pool)
1197 Set_Associated_Storage_Pool (U_Ent, Pool);
1199 Error_Msg_N ("Non sharable GNAT Pool", Expr);
1202 -- The pool may be specified as the Storage_Pool of some other
1203 -- type. It is rewritten as a class_wide conversion of the
1204 -- corresponding pool entity.
1206 elsif Nkind (Expr) = N_Type_Conversion
1207 and then Is_Entity_Name (Expression (Expr))
1208 and then Nkind (Original_Node (Expr)) = N_Attribute_Reference
1210 Pool := Entity (Expression (Expr));
1212 if Present (Etype (Pool))
1213 and then Etype (Pool) /= RTE (RE_Stack_Bounded_Pool)
1214 and then Etype (Pool) /= RTE (RE_Unbounded_Reclaim_Pool)
1216 Set_Associated_Storage_Pool (U_Ent, Pool);
1218 Error_Msg_N ("Non sharable GNAT Pool", Expr);
1222 Error_Msg_N ("incorrect reference to a Storage Pool", Expr);
1231 -- Value_Size attribute definition clause
1233 when Attribute_Value_Size => Value_Size : declare
1234 Size : constant Uint := Static_Integer (Expr);
1238 if not Is_Type (U_Ent) then
1239 Error_Msg_N ("Value_Size cannot be given for &", Nam);
1242 (Get_Attribute_Definition_Clause
1243 (U_Ent, Attribute_Value_Size))
1245 Error_Msg_N ("Value_Size already given for &", Nam);
1248 if Is_Elementary_Type (U_Ent) then
1249 Check_Size (Expr, U_Ent, Size, Biased);
1250 Set_Has_Biased_Representation (U_Ent, Biased);
1253 Set_RM_Size (U_Ent, Size);
1261 -- Write attribute definition clause
1262 -- check for class-wide case will be performed later
1264 when Attribute_Write => Write : declare
1265 Subp : Entity_Id := Empty;
1270 function Has_Good_Profile (Subp : Entity_Id) return Boolean;
1271 -- Return true if the entity is a procedure with an
1272 -- appropriate profile for the write attribute.
1274 function Has_Good_Profile (Subp : Entity_Id) return Boolean is
1276 Ok : Boolean := False;
1279 if Ekind (Subp) = E_Procedure then
1280 F := First_Formal (Subp);
1283 if Ekind (Etype (F)) = E_Anonymous_Access_Type
1285 Designated_Type (Etype (F)) =
1286 Class_Wide_Type (RTE (RE_Root_Stream_Type))
1290 and then Parameter_Mode (F) = E_In_Parameter
1291 and then Base_Type (Etype (F)) = Base_Type (Ent)
1292 and then No (Next_Formal (F));
1298 end Has_Good_Profile;
1300 -- Start of processing for Write attribute definition
1305 if not Is_Type (U_Ent) then
1306 Error_Msg_N ("local name must be a subtype", Nam);
1310 Pnam := TSS (Base_Type (U_Ent), Name_uWrite);
1313 and then Base_Type (Etype (Next_Formal (First_Formal (Pnam))))
1316 Error_Msg_Sloc := Sloc (Pnam);
1317 Error_Msg_N ("write attribute already defined #", Nam);
1323 if Is_Entity_Name (Expr) then
1324 if not Is_Overloaded (Expr) then
1325 if Has_Good_Profile (Entity (Expr)) then
1326 Subp := Entity (Expr);
1330 Get_First_Interp (Expr, I, It);
1332 while Present (It.Nam) loop
1333 if Has_Good_Profile (It.Nam) then
1338 Get_Next_Interp (I, It);
1343 if Present (Subp) then
1344 Set_Entity (Expr, Subp);
1345 Set_Etype (Expr, Etype (Subp));
1346 New_Stream_Procedure (N, U_Ent, Subp, Name_uWrite);
1348 Error_Msg_N ("incorrect expression for write attribute", Expr);
1353 -- All other attributes cannot be set
1357 ("attribute& cannot be set with definition clause", N);
1361 -- The test for the type being frozen must be performed after
1362 -- any expression the clause has been analyzed since the expression
1363 -- itself might cause freezing that makes the clause illegal.
1365 if Rep_Item_Too_Late (U_Ent, N, FOnly) then
1368 end Analyze_Attribute_Definition_Clause;
1370 ----------------------------
1371 -- Analyze_Code_Statement --
1372 ----------------------------
1374 procedure Analyze_Code_Statement (N : Node_Id) is
1375 HSS : constant Node_Id := Parent (N);
1376 SBody : constant Node_Id := Parent (HSS);
1377 Subp : constant Entity_Id := Current_Scope;
1384 -- Analyze and check we get right type, note that this implements the
1385 -- requirement (RM 13.8(1)) that Machine_Code be with'ed, since that
1386 -- is the only way that Asm_Insn could possibly be visible.
1388 Analyze_And_Resolve (Expression (N));
1390 if Etype (Expression (N)) = Any_Type then
1392 elsif Etype (Expression (N)) /= RTE (RE_Asm_Insn) then
1393 Error_Msg_N ("incorrect type for code statement", N);
1397 -- Make sure we appear in the handled statement sequence of a
1398 -- subprogram (RM 13.8(3)).
1400 if Nkind (HSS) /= N_Handled_Sequence_Of_Statements
1401 or else Nkind (SBody) /= N_Subprogram_Body
1404 ("code statement can only appear in body of subprogram", N);
1408 -- Do remaining checks (RM 13.8(3)) if not already done
1410 if not Is_Machine_Code_Subprogram (Subp) then
1411 Set_Is_Machine_Code_Subprogram (Subp);
1413 -- No exception handlers allowed
1415 if Present (Exception_Handlers (HSS)) then
1417 ("exception handlers not permitted in machine code subprogram",
1418 First (Exception_Handlers (HSS)));
1421 -- No declarations other than use clauses and pragmas (we allow
1422 -- certain internally generated declarations as well).
1424 Decl := First (Declarations (SBody));
1425 while Present (Decl) loop
1426 DeclO := Original_Node (Decl);
1427 if Comes_From_Source (DeclO)
1428 and then Nkind (DeclO) /= N_Pragma
1429 and then Nkind (DeclO) /= N_Use_Package_Clause
1430 and then Nkind (DeclO) /= N_Use_Type_Clause
1431 and then Nkind (DeclO) /= N_Implicit_Label_Declaration
1434 ("this declaration not allowed in machine code subprogram",
1441 -- No statements other than code statements, pragmas, and labels.
1442 -- Again we allow certain internally generated statements.
1444 Stmt := First (Statements (HSS));
1445 while Present (Stmt) loop
1446 StmtO := Original_Node (Stmt);
1447 if Comes_From_Source (StmtO)
1448 and then Nkind (StmtO) /= N_Pragma
1449 and then Nkind (StmtO) /= N_Label
1450 and then Nkind (StmtO) /= N_Code_Statement
1453 ("this statement is not allowed in machine code subprogram",
1461 end Analyze_Code_Statement;
1463 -----------------------------------------------
1464 -- Analyze_Enumeration_Representation_Clause --
1465 -----------------------------------------------
1467 procedure Analyze_Enumeration_Representation_Clause (N : Node_Id) is
1468 Ident : constant Node_Id := Identifier (N);
1469 Aggr : constant Node_Id := Array_Aggregate (N);
1470 Enumtype : Entity_Id;
1476 Err : Boolean := False;
1478 Lo : constant Uint := Expr_Value (Type_Low_Bound (Universal_Integer));
1479 Hi : constant Uint := Expr_Value (Type_High_Bound (Universal_Integer));
1484 -- First some basic error checks
1487 Enumtype := Entity (Ident);
1489 if Enumtype = Any_Type
1490 or else Rep_Item_Too_Early (Enumtype, N)
1494 Enumtype := Underlying_Type (Enumtype);
1497 if not Is_Enumeration_Type (Enumtype) then
1499 ("enumeration type required, found}",
1500 Ident, First_Subtype (Enumtype));
1504 if Scope (Enumtype) /= Current_Scope then
1505 Error_Msg_N ("type must be declared in this scope", Ident);
1508 elsif not Is_First_Subtype (Enumtype) then
1509 Error_Msg_N ("cannot give enumeration rep clause for subtype", N);
1512 elsif Has_Enumeration_Rep_Clause (Enumtype) then
1513 Error_Msg_N ("duplicate enumeration rep clause ignored", N);
1516 elsif Root_Type (Enumtype) = Standard_Character
1517 or else Root_Type (Enumtype) = Standard_Wide_Character
1519 Error_Msg_N ("enumeration rep clause not allowed for this type", N);
1522 Set_Has_Enumeration_Rep_Clause (Enumtype);
1523 Set_Has_Enumeration_Rep_Clause (Base_Type (Enumtype));
1526 -- Now we process the aggregate. Note that we don't use the normal
1527 -- aggregate code for this purpose, because we don't want any of the
1528 -- normal expansion activities, and a number of special semantic
1529 -- rules apply (including the component type being any integer type)
1531 -- Badent signals that we found some incorrect entries processing
1532 -- the list. The final checks for completeness and ordering are
1533 -- skipped in this case.
1535 Elit := First_Literal (Enumtype);
1537 -- First the positional entries if any
1539 if Present (Expressions (Aggr)) then
1540 Expr := First (Expressions (Aggr));
1541 while Present (Expr) loop
1543 Error_Msg_N ("too many entries in aggregate", Expr);
1547 Val := Static_Integer (Expr);
1549 if Val = No_Uint then
1552 elsif Val < Lo or else Hi < Val then
1553 Error_Msg_N ("value outside permitted range", Expr);
1557 Set_Enumeration_Rep (Elit, Val);
1558 Set_Enumeration_Rep_Expr (Elit, Expr);
1564 -- Now process the named entries if present
1566 if Present (Component_Associations (Aggr)) then
1567 Assoc := First (Component_Associations (Aggr));
1568 while Present (Assoc) loop
1569 Choice := First (Choices (Assoc));
1571 if Present (Next (Choice)) then
1573 ("multiple choice not allowed here", Next (Choice));
1577 if Nkind (Choice) = N_Others_Choice then
1578 Error_Msg_N ("others choice not allowed here", Choice);
1581 elsif Nkind (Choice) = N_Range then
1582 -- ??? should allow zero/one element range here
1583 Error_Msg_N ("range not allowed here", Choice);
1587 Analyze_And_Resolve (Choice, Enumtype);
1589 if Is_Entity_Name (Choice)
1590 and then Is_Type (Entity (Choice))
1592 Error_Msg_N ("subtype name not allowed here", Choice);
1594 -- ??? should allow static subtype with zero/one entry
1596 elsif Etype (Choice) = Base_Type (Enumtype) then
1597 if not Is_Static_Expression (Choice) then
1599 ("non-static expression used for choice", Choice);
1603 Elit := Expr_Value_E (Choice);
1605 if Present (Enumeration_Rep_Expr (Elit)) then
1606 Error_Msg_Sloc := Sloc (Enumeration_Rep_Expr (Elit));
1608 ("representation for& previously given#",
1613 Set_Enumeration_Rep_Expr (Elit, Choice);
1615 Expr := Expression (Assoc);
1616 Val := Static_Integer (Expr);
1618 if Val = No_Uint then
1621 elsif Val < Lo or else Hi < Val then
1622 Error_Msg_N ("value outside permitted range", Expr);
1626 Set_Enumeration_Rep (Elit, Val);
1635 -- Aggregate is fully processed. Now we check that a full set of
1636 -- representations was given, and that they are in range and in order.
1637 -- These checks are only done if no other errors occurred.
1643 Elit := First_Literal (Enumtype);
1644 while Present (Elit) loop
1645 if No (Enumeration_Rep_Expr (Elit)) then
1646 Error_Msg_NE ("missing representation for&!", N, Elit);
1649 Val := Enumeration_Rep (Elit);
1651 if Min = No_Uint then
1655 if Val /= No_Uint then
1656 if Max /= No_Uint and then Val <= Max then
1658 ("enumeration value for& not ordered!",
1659 Enumeration_Rep_Expr (Elit), Elit);
1665 -- If there is at least one literal whose representation
1666 -- is not equal to the Pos value, then note that this
1667 -- enumeration type has a non-standard representation.
1669 if Val /= Enumeration_Pos (Elit) then
1670 Set_Has_Non_Standard_Rep (Base_Type (Enumtype));
1677 -- Now set proper size information
1680 Minsize : Uint := UI_From_Int (Minimum_Size (Enumtype));
1683 if Has_Size_Clause (Enumtype) then
1684 if Esize (Enumtype) >= Minsize then
1689 UI_From_Int (Minimum_Size (Enumtype, Biased => True));
1691 if Esize (Enumtype) < Minsize then
1692 Error_Msg_N ("previously given size is too small", N);
1695 Set_Has_Biased_Representation (Enumtype);
1700 Set_RM_Size (Enumtype, Minsize);
1701 Set_Enum_Esize (Enumtype);
1704 Set_RM_Size (Base_Type (Enumtype), RM_Size (Enumtype));
1705 Set_Esize (Base_Type (Enumtype), Esize (Enumtype));
1706 Set_Alignment (Base_Type (Enumtype), Alignment (Enumtype));
1710 -- We repeat the too late test in case it froze itself!
1712 if Rep_Item_Too_Late (Enumtype, N) then
1716 end Analyze_Enumeration_Representation_Clause;
1718 ----------------------------
1719 -- Analyze_Free_Statement --
1720 ----------------------------
1722 procedure Analyze_Free_Statement (N : Node_Id) is
1724 Analyze (Expression (N));
1725 end Analyze_Free_Statement;
1727 ------------------------------------------
1728 -- Analyze_Record_Representation_Clause --
1729 ------------------------------------------
1731 procedure Analyze_Record_Representation_Clause (N : Node_Id) is
1732 Loc : constant Source_Ptr := Sloc (N);
1733 Ident : constant Node_Id := Identifier (N);
1734 Rectype : Entity_Id;
1740 Hbit : Uint := Uint_0;
1745 Max_Bit_So_Far : Uint;
1746 -- Records the maximum bit position so far. If all field positoins
1747 -- are monotonically increasing, then we can skip the circuit for
1748 -- checking for overlap, since no overlap is possible.
1750 Overlap_Check_Required : Boolean;
1751 -- Used to keep track of whether or not an overlap check is required
1753 Ccount : Natural := 0;
1754 -- Number of component clauses in record rep clause
1758 Rectype := Entity (Ident);
1760 if Rectype = Any_Type
1761 or else Rep_Item_Too_Early (Rectype, N)
1765 Rectype := Underlying_Type (Rectype);
1768 -- First some basic error checks
1770 if not Is_Record_Type (Rectype) then
1772 ("record type required, found}", Ident, First_Subtype (Rectype));
1775 elsif Is_Unchecked_Union (Rectype) then
1777 ("record rep clause not allowed for Unchecked_Union", N);
1779 elsif Scope (Rectype) /= Current_Scope then
1780 Error_Msg_N ("type must be declared in this scope", N);
1783 elsif not Is_First_Subtype (Rectype) then
1784 Error_Msg_N ("cannot give record rep clause for subtype", N);
1787 elsif Has_Record_Rep_Clause (Rectype) then
1788 Error_Msg_N ("duplicate record rep clause ignored", N);
1791 elsif Rep_Item_Too_Late (Rectype, N) then
1795 if Present (Mod_Clause (N)) then
1797 Loc : constant Source_Ptr := Sloc (N);
1798 M : constant Node_Id := Mod_Clause (N);
1799 P : constant List_Id := Pragmas_Before (M);
1808 -- In Tree_Output mode, expansion is disabled, but we must
1809 -- convert the Mod clause into an alignment clause anyway, so
1810 -- that the back-end can compute and back-annotate properly the
1811 -- size and alignment of types that may include this record.
1813 if Operating_Mode = Check_Semantics
1814 and then Tree_Output
1817 Make_Attribute_Definition_Clause (Loc,
1818 Name => New_Reference_To (Base_Type (Rectype), Loc),
1819 Chars => Name_Alignment,
1820 Expression => Relocate_Node (Expression (M)));
1822 Set_From_At_Mod (AtM_Nod);
1823 Insert_After (N, AtM_Nod);
1824 Mod_Val := Get_Alignment_Value (Expression (AtM_Nod));
1825 Set_Mod_Clause (N, Empty);
1828 -- Get the alignment value to perform error checking
1830 Mod_Val := Get_Alignment_Value (Expression (M));
1836 -- Clear any existing component clauses for the type (this happens
1837 -- with derived types, where we are now overriding the original)
1839 Fent := First_Entity (Rectype);
1842 while Present (Comp) loop
1843 if Ekind (Comp) = E_Component
1844 or else Ekind (Comp) = E_Discriminant
1846 Set_Component_Clause (Comp, Empty);
1852 -- All done if no component clauses
1854 CC := First (Component_Clauses (N));
1860 -- If a tag is present, then create a component clause that places
1861 -- it at the start of the record (otherwise gigi may place it after
1862 -- other fields that have rep clauses).
1864 if Nkind (Fent) = N_Defining_Identifier
1865 and then Chars (Fent) = Name_uTag
1867 Set_Component_Bit_Offset (Fent, Uint_0);
1868 Set_Normalized_Position (Fent, Uint_0);
1869 Set_Normalized_First_Bit (Fent, Uint_0);
1870 Set_Normalized_Position_Max (Fent, Uint_0);
1871 Init_Esize (Fent, System_Address_Size);
1873 Set_Component_Clause (Fent,
1874 Make_Component_Clause (Loc,
1876 Make_Identifier (Loc,
1877 Chars => Name_uTag),
1880 Make_Integer_Literal (Loc,
1884 Make_Integer_Literal (Loc,
1888 Make_Integer_Literal (Loc,
1889 UI_From_Int (System_Address_Size))));
1891 Ccount := Ccount + 1;
1894 Set_Has_Record_Rep_Clause (Rectype);
1895 Set_Has_Specified_Layout (Rectype);
1897 -- A representation like this applies to the base type as well
1899 Set_Has_Record_Rep_Clause (Base_Type (Rectype));
1900 Set_Has_Non_Standard_Rep (Base_Type (Rectype));
1901 Set_Has_Specified_Layout (Base_Type (Rectype));
1903 Max_Bit_So_Far := Uint_Minus_1;
1904 Overlap_Check_Required := False;
1906 -- Process the component clauses
1908 while Present (CC) loop
1910 -- If pragma, just analyze it
1912 if Nkind (CC) = N_Pragma then
1915 -- Processing for real component clause
1918 Ccount := Ccount + 1;
1919 Posit := Static_Integer (Position (CC));
1920 Fbit := Static_Integer (First_Bit (CC));
1921 Lbit := Static_Integer (Last_Bit (CC));
1924 and then Fbit /= No_Uint
1925 and then Lbit /= No_Uint
1929 ("position cannot be negative", Position (CC));
1933 ("first bit cannot be negative", First_Bit (CC));
1935 -- Values look OK, so find the corresponding record component
1936 -- Even though the syntax allows an attribute reference for
1937 -- implementation-defined components, GNAT does not allow the
1938 -- tag to get an explicit position.
1940 elsif Nkind (Component_Name (CC)) = N_Attribute_Reference then
1942 if Attribute_Name (Component_Name (CC)) = Name_Tag then
1943 Error_Msg_N ("position of tag cannot be specified", CC);
1945 Error_Msg_N ("illegal component name", CC);
1949 Comp := First_Entity (Rectype);
1950 while Present (Comp) loop
1951 exit when Chars (Comp) = Chars (Component_Name (CC));
1957 -- Maybe component of base type that is absent from
1958 -- statically constrained first subtype.
1960 Comp := First_Entity (Base_Type (Rectype));
1961 while Present (Comp) loop
1962 exit when Chars (Comp) = Chars (Component_Name (CC));
1969 ("component clause is for non-existent field", CC);
1971 elsif Present (Component_Clause (Comp)) then
1972 Error_Msg_Sloc := Sloc (Component_Clause (Comp));
1974 ("component clause previously given#", CC);
1977 -- Update Fbit and Lbit to the actual bit number.
1979 Fbit := Fbit + UI_From_Int (SSU) * Posit;
1980 Lbit := Lbit + UI_From_Int (SSU) * Posit;
1982 if Fbit <= Max_Bit_So_Far then
1983 Overlap_Check_Required := True;
1985 Max_Bit_So_Far := Lbit;
1988 if Has_Size_Clause (Rectype)
1989 and then Esize (Rectype) <= Lbit
1992 ("bit number out of range of specified size",
1995 Set_Component_Clause (Comp, CC);
1996 Set_Component_Bit_Offset (Comp, Fbit);
1997 Set_Esize (Comp, 1 + (Lbit - Fbit));
1998 Set_Normalized_First_Bit (Comp, Fbit mod SSU);
1999 Set_Normalized_Position (Comp, Fbit / SSU);
2001 Set_Normalized_Position_Max
2002 (Fent, Normalized_Position (Fent));
2004 if Is_Tagged_Type (Rectype)
2005 and then Fbit < System_Address_Size
2008 ("component overlaps tag field of&",
2012 -- Test for large object that is not on a byte
2013 -- boundary, defined as a large packed array not
2014 -- represented by a modular type, or an object for
2015 -- which a size of greater than 64 bits is specified.
2017 if Fbit mod SSU /= 0 then
2018 if (Is_Packed_Array_Type (Etype (Comp))
2019 and then Is_Array_Type
2020 (Packed_Array_Type (Etype (Comp))))
2021 or else Esize (Etype (Comp)) > 64
2024 ("large component must be on byte boundary",
2029 -- This information is also set in the
2030 -- corresponding component of the base type,
2031 -- found by accessing the Original_Record_Component
2032 -- link if it is present.
2034 Ocomp := Original_Record_Component (Comp);
2041 (Component_Name (CC),
2046 Set_Has_Biased_Representation (Comp, Biased);
2048 if Present (Ocomp) then
2049 Set_Component_Clause (Ocomp, CC);
2050 Set_Component_Bit_Offset (Ocomp, Fbit);
2051 Set_Normalized_First_Bit (Ocomp, Fbit mod SSU);
2052 Set_Normalized_Position (Ocomp, Fbit / SSU);
2053 Set_Esize (Ocomp, 1 + (Lbit - Fbit));
2055 Set_Normalized_Position_Max
2056 (Ocomp, Normalized_Position (Ocomp));
2058 Set_Has_Biased_Representation
2059 (Ocomp, Has_Biased_Representation (Comp));
2062 if Esize (Comp) < 0 then
2063 Error_Msg_N ("component size is negative", CC);
2074 -- Now that we have processed all the component clauses, check for
2075 -- overlap. We have to leave this till last, since the components
2076 -- can appear in any arbitrary order in the representation clause.
2078 -- We do not need this check if all specified ranges were monotonic,
2079 -- as recorded by Overlap_Check_Required being False at this stage.
2081 -- This first section checks if there are any overlapping entries
2082 -- at all. It does this by sorting all entries and then seeing if
2083 -- there are any overlaps. If there are none, then that is decisive,
2084 -- but if there are overlaps, they may still be OK (they may result
2085 -- from fields in different variants).
2087 if Overlap_Check_Required then
2088 Overlap_Check1 : declare
2090 OC_Fbit : array (0 .. Ccount) of Uint;
2091 -- First-bit values for component clauses, the value is the
2092 -- offset of the first bit of the field from start of record.
2093 -- The zero entry is for use in sorting.
2095 OC_Lbit : array (0 .. Ccount) of Uint;
2096 -- Last-bit values for component clauses, the value is the
2097 -- offset of the last bit of the field from start of record.
2098 -- The zero entry is for use in sorting.
2100 OC_Count : Natural := 0;
2101 -- Count of entries in OC_Fbit and OC_Lbit
2103 function OC_Lt (Op1, Op2 : Natural) return Boolean;
2104 -- Compare routine for Sort (See GNAT.Heap_Sort_A)
2106 procedure OC_Move (From : Natural; To : Natural);
2107 -- Move routine for Sort (see GNAT.Heap_Sort_A)
2109 function OC_Lt (Op1, Op2 : Natural) return Boolean is
2111 return OC_Fbit (Op1) < OC_Fbit (Op2);
2114 procedure OC_Move (From : Natural; To : Natural) is
2116 OC_Fbit (To) := OC_Fbit (From);
2117 OC_Lbit (To) := OC_Lbit (From);
2121 CC := First (Component_Clauses (N));
2122 while Present (CC) loop
2123 if Nkind (CC) /= N_Pragma then
2124 Posit := Static_Integer (Position (CC));
2125 Fbit := Static_Integer (First_Bit (CC));
2126 Lbit := Static_Integer (Last_Bit (CC));
2129 and then Fbit /= No_Uint
2130 and then Lbit /= No_Uint
2132 OC_Count := OC_Count + 1;
2133 Posit := Posit * SSU;
2134 OC_Fbit (OC_Count) := Fbit + Posit;
2135 OC_Lbit (OC_Count) := Lbit + Posit;
2144 OC_Move'Unrestricted_Access,
2145 OC_Lt'Unrestricted_Access);
2147 Overlap_Check_Required := False;
2148 for J in 1 .. OC_Count - 1 loop
2149 if OC_Lbit (J) >= OC_Fbit (J + 1) then
2150 Overlap_Check_Required := True;
2157 -- If Overlap_Check_Required is still True, then we have to do
2158 -- the full scale overlap check, since we have at least two fields
2159 -- that do overlap, and we need to know if that is OK since they
2160 -- are in the same variant, or whether we have a definite problem
2162 if Overlap_Check_Required then
2163 Overlap_Check2 : declare
2164 C1_Ent, C2_Ent : Entity_Id;
2165 -- Entities of components being checked for overlap
2168 -- Component_List node whose Component_Items are being checked
2171 -- Component declaration for component being checked
2174 C1_Ent := First_Entity (Base_Type (Rectype));
2176 -- Loop through all components in record. For each component check
2177 -- for overlap with any of the preceding elements on the component
2178 -- list containing the component, and also, if the component is in
2179 -- a variant, check against components outside the case structure.
2180 -- This latter test is repeated recursively up the variant tree.
2182 Main_Component_Loop : while Present (C1_Ent) loop
2183 if Ekind (C1_Ent) /= E_Component
2184 and then Ekind (C1_Ent) /= E_Discriminant
2186 goto Continue_Main_Component_Loop;
2189 -- Skip overlap check if entity has no declaration node. This
2190 -- happens with discriminants in constrained derived types.
2191 -- Probably we are missing some checks as a result, but that
2192 -- does not seem terribly serious ???
2194 if No (Declaration_Node (C1_Ent)) then
2195 goto Continue_Main_Component_Loop;
2198 Clist := Parent (List_Containing (Declaration_Node (C1_Ent)));
2200 -- Loop through component lists that need checking. Check the
2201 -- current component list and all lists in variants above us.
2203 Component_List_Loop : loop
2205 -- If derived type definition, go to full declaration
2206 -- If at outer level, check discriminants if there are any
2208 if Nkind (Clist) = N_Derived_Type_Definition then
2209 Clist := Parent (Clist);
2212 -- Outer level of record definition, check discriminants
2214 if Nkind (Clist) = N_Full_Type_Declaration
2215 or else Nkind (Clist) = N_Private_Type_Declaration
2217 if Has_Discriminants (Defining_Identifier (Clist)) then
2219 First_Discriminant (Defining_Identifier (Clist));
2221 while Present (C2_Ent) loop
2222 exit when C1_Ent = C2_Ent;
2223 Check_Component_Overlap (C1_Ent, C2_Ent);
2224 Next_Discriminant (C2_Ent);
2228 -- Record extension case
2230 elsif Nkind (Clist) = N_Derived_Type_Definition then
2233 -- Otherwise check one component list
2236 Citem := First (Component_Items (Clist));
2238 while Present (Citem) loop
2239 if Nkind (Citem) = N_Component_Declaration then
2240 C2_Ent := Defining_Identifier (Citem);
2241 exit when C1_Ent = C2_Ent;
2242 Check_Component_Overlap (C1_Ent, C2_Ent);
2249 -- Check for variants above us (the parent of the Clist can
2250 -- be a variant, in which case its parent is a variant part,
2251 -- and the parent of the variant part is a component list
2252 -- whose components must all be checked against the current
2253 -- component for overlap.
2255 if Nkind (Parent (Clist)) = N_Variant then
2256 Clist := Parent (Parent (Parent (Clist)));
2258 -- Check for possible discriminant part in record, this is
2259 -- treated essentially as another level in the recursion.
2260 -- For this case we have the parent of the component list
2261 -- is the record definition, and its parent is the full
2262 -- type declaration which contains the discriminant
2265 elsif Nkind (Parent (Clist)) = N_Record_Definition then
2266 Clist := Parent (Parent ((Clist)));
2268 -- If neither of these two cases, we are at the top of
2272 exit Component_List_Loop;
2274 end loop Component_List_Loop;
2276 <<Continue_Main_Component_Loop>>
2277 Next_Entity (C1_Ent);
2279 end loop Main_Component_Loop;
2283 -- For records that have component clauses for all components, and
2284 -- whose size is less than or equal to 32, we need to know the size
2285 -- in the front end to activate possible packed array processing
2286 -- where the component type is a record.
2288 -- At this stage Hbit + 1 represents the first unused bit from all
2289 -- the component clauses processed, so if the component clauses are
2290 -- complete, then this is the length of the record.
2292 -- For records longer than System.Storage_Unit, and for those where
2293 -- not all components have component clauses, the back end determines
2294 -- the length (it may for example be appopriate to round up the size
2295 -- to some convenient boundary, based on alignment considerations etc).
2297 if Unknown_RM_Size (Rectype)
2298 and then Hbit + 1 <= 32
2300 -- Nothing to do if at least one component with no component clause
2302 Comp := First_Entity (Rectype);
2303 while Present (Comp) loop
2304 if Ekind (Comp) = E_Component
2305 or else Ekind (Comp) = E_Discriminant
2307 if No (Component_Clause (Comp)) then
2315 -- If we fall out of loop, all components have component clauses
2316 -- and so we can set the size to the maximum value.
2318 Set_RM_Size (Rectype, Hbit + 1);
2321 end Analyze_Record_Representation_Clause;
2323 -----------------------------
2324 -- Check_Address_Alignment --
2325 -----------------------------
2327 procedure Check_Address_Alignment (E : Entity_Id; Expr : Node_Id) is
2331 if Nkind (Expr) = N_Unchecked_Type_Conversion then
2332 Arg := Expression (Expr);
2334 elsif Nkind (Expr) = N_Function_Call
2335 and then Is_RTE (Entity (Name (Expr)), RE_To_Address)
2337 Arg := First (Parameter_Associations (Expr));
2339 if Nkind (Arg) = N_Parameter_Association then
2340 Arg := Explicit_Actual_Parameter (Arg);
2347 -- Here Arg is the address value
2349 if Compile_Time_Known_Value (Arg) then
2350 if Expr_Value (Arg) mod Alignment (E) /= 0 then
2352 ("?specified address for& not consistent with alignment",
2356 end Check_Address_Alignment;
2358 -----------------------------
2359 -- Check_Component_Overlap --
2360 -----------------------------
2362 procedure Check_Component_Overlap (C1_Ent, C2_Ent : Entity_Id) is
2364 if Present (Component_Clause (C1_Ent))
2365 and then Present (Component_Clause (C2_Ent))
2367 -- Exclude odd case where we have two tag fields in the same
2368 -- record, both at location zero. This seems a bit strange,
2369 -- but it seems to happen in some circumstances ???
2371 if Chars (C1_Ent) = Name_uTag
2372 and then Chars (C2_Ent) = Name_uTag
2377 -- Here we check if the two fields overlap
2380 S1 : constant Uint := Component_Bit_Offset (C1_Ent);
2381 S2 : constant Uint := Component_Bit_Offset (C2_Ent);
2382 E1 : constant Uint := S1 + Esize (C1_Ent);
2383 E2 : constant Uint := S2 + Esize (C2_Ent);
2386 if E2 <= S1 or else E1 <= S2 then
2390 Component_Name (Component_Clause (C2_Ent));
2391 Error_Msg_Sloc := Sloc (Error_Msg_Node_2);
2393 Component_Name (Component_Clause (C1_Ent));
2395 ("component& overlaps & #",
2396 Component_Name (Component_Clause (C1_Ent)));
2400 end Check_Component_Overlap;
2402 -----------------------------------
2403 -- Check_Constant_Address_Clause --
2404 -----------------------------------
2406 procedure Check_Constant_Address_Clause
2410 procedure Check_At_Constant_Address (Nod : Node_Id);
2411 -- Checks that the given node N represents a name whose 'Address
2412 -- is constant (in the same sense as OK_Constant_Address_Clause,
2413 -- i.e. the address value is the same at the point of declaration
2414 -- of U_Ent and at the time of elaboration of the address clause.
2416 procedure Check_Expr_Constants (Nod : Node_Id);
2417 -- Checks that Nod meets the requirements for a constant address
2418 -- clause in the sense of the enclosing procedure.
2420 procedure Check_List_Constants (Lst : List_Id);
2421 -- Check that all elements of list Lst meet the requirements for a
2422 -- constant address clause in the sense of the enclosing procedure.
2424 -------------------------------
2425 -- Check_At_Constant_Address --
2426 -------------------------------
2428 procedure Check_At_Constant_Address (Nod : Node_Id) is
2430 if Is_Entity_Name (Nod) then
2431 if Present (Address_Clause (Entity ((Nod)))) then
2433 ("invalid address clause for initialized object &!",
2436 ("address for& cannot" &
2437 " depend on another address clause! ('R'M 13.1(22))!",
2440 elsif In_Same_Source_Unit (Entity (Nod), U_Ent)
2441 and then Sloc (U_Ent) < Sloc (Entity (Nod))
2444 ("invalid address clause for initialized object &!",
2446 Error_Msg_Name_1 := Chars (Entity (Nod));
2447 Error_Msg_Name_2 := Chars (U_Ent);
2449 ("\% must be defined before % ('R'M 13.1(22))!",
2453 elsif Nkind (Nod) = N_Selected_Component then
2455 T : constant Entity_Id := Etype (Prefix (Nod));
2458 if (Is_Record_Type (T)
2459 and then Has_Discriminants (T))
2462 and then Is_Record_Type (Designated_Type (T))
2463 and then Has_Discriminants (Designated_Type (T)))
2466 ("invalid address clause for initialized object &!",
2469 ("\address cannot depend on component" &
2470 " of discriminated record ('R'M 13.1(22))!",
2473 Check_At_Constant_Address (Prefix (Nod));
2477 elsif Nkind (Nod) = N_Indexed_Component then
2478 Check_At_Constant_Address (Prefix (Nod));
2479 Check_List_Constants (Expressions (Nod));
2482 Check_Expr_Constants (Nod);
2484 end Check_At_Constant_Address;
2486 --------------------------
2487 -- Check_Expr_Constants --
2488 --------------------------
2490 procedure Check_Expr_Constants (Nod : Node_Id) is
2492 if Nkind (Nod) in N_Has_Etype
2493 and then Etype (Nod) = Any_Type
2499 when N_Empty | N_Error =>
2502 when N_Identifier | N_Expanded_Name =>
2504 Ent : constant Entity_Id := Entity (Nod);
2505 Loc_Ent : constant Source_Ptr := Sloc (Ent);
2506 Loc_U_Ent : constant Source_Ptr := Sloc (U_Ent);
2509 if Ekind (Ent) = E_Named_Integer
2511 Ekind (Ent) = E_Named_Real
2518 Ekind (Ent) = E_Constant
2520 Ekind (Ent) = E_In_Parameter
2522 -- This is the case where we must have Ent defined
2523 -- before U_Ent. Clearly if they are in different
2524 -- units this requirement is met since the unit
2525 -- containing Ent is already processed.
2527 if not In_Same_Source_Unit (Ent, U_Ent) then
2530 -- Otherwise location of Ent must be before the
2531 -- location of U_Ent, that's what prior defined means.
2533 elsif Loc_Ent < Loc_U_Ent then
2538 ("invalid address clause for initialized object &!",
2540 Error_Msg_Name_1 := Chars (Ent);
2541 Error_Msg_Name_2 := Chars (U_Ent);
2543 ("\% must be defined before % ('R'M 13.1(22))!",
2547 elsif Nkind (Original_Node (Nod)) = N_Function_Call then
2548 Check_Expr_Constants (Original_Node (Nod));
2552 ("invalid address clause for initialized object &!",
2554 Error_Msg_Name_1 := Chars (Ent);
2556 ("\reference to variable% not allowed ('R'M 13.1(22))!",
2561 when N_Integer_Literal |
2564 N_Character_Literal =>
2568 Check_Expr_Constants (Low_Bound (Nod));
2569 Check_Expr_Constants (High_Bound (Nod));
2571 when N_Explicit_Dereference =>
2572 Check_Expr_Constants (Prefix (Nod));
2574 when N_Indexed_Component =>
2575 Check_Expr_Constants (Prefix (Nod));
2576 Check_List_Constants (Expressions (Nod));
2579 Check_Expr_Constants (Prefix (Nod));
2580 Check_Expr_Constants (Discrete_Range (Nod));
2582 when N_Selected_Component =>
2583 Check_Expr_Constants (Prefix (Nod));
2585 when N_Attribute_Reference =>
2587 if (Attribute_Name (Nod) = Name_Address
2589 Attribute_Name (Nod) = Name_Access
2591 Attribute_Name (Nod) = Name_Unchecked_Access
2593 Attribute_Name (Nod) = Name_Unrestricted_Access)
2595 Check_At_Constant_Address (Prefix (Nod));
2598 Check_Expr_Constants (Prefix (Nod));
2599 Check_List_Constants (Expressions (Nod));
2603 Check_List_Constants (Component_Associations (Nod));
2604 Check_List_Constants (Expressions (Nod));
2606 when N_Component_Association =>
2607 Check_Expr_Constants (Expression (Nod));
2609 when N_Extension_Aggregate =>
2610 Check_Expr_Constants (Ancestor_Part (Nod));
2611 Check_List_Constants (Component_Associations (Nod));
2612 Check_List_Constants (Expressions (Nod));
2617 when N_Binary_Op | N_And_Then | N_Or_Else | N_In | N_Not_In =>
2618 Check_Expr_Constants (Left_Opnd (Nod));
2619 Check_Expr_Constants (Right_Opnd (Nod));
2622 Check_Expr_Constants (Right_Opnd (Nod));
2624 when N_Type_Conversion |
2625 N_Qualified_Expression |
2627 Check_Expr_Constants (Expression (Nod));
2629 when N_Unchecked_Type_Conversion =>
2630 Check_Expr_Constants (Expression (Nod));
2632 -- If this is a rewritten unchecked conversion, subtypes
2633 -- in this node are those created within the instance.
2634 -- To avoid order of elaboration issues, replace them
2635 -- with their base types. Note that address clauses can
2636 -- cause order of elaboration problems because they are
2637 -- elaborated by the back-end at the point of definition,
2638 -- and may mention entities declared in between (as long
2639 -- as everything is static). It is user-friendly to allow
2640 -- unchecked conversions in this context.
2642 if Nkind (Original_Node (Nod)) = N_Function_Call then
2643 Set_Etype (Expression (Nod),
2644 Base_Type (Etype (Expression (Nod))));
2645 Set_Etype (Nod, Base_Type (Etype (Nod)));
2648 when N_Function_Call =>
2649 if not Is_Pure (Entity (Name (Nod))) then
2651 ("invalid address clause for initialized object &!",
2655 ("\function & is not pure ('R'M 13.1(22))!",
2656 Nod, Entity (Name (Nod)));
2659 Check_List_Constants (Parameter_Associations (Nod));
2662 when N_Parameter_Association =>
2663 Check_Expr_Constants (Explicit_Actual_Parameter (Nod));
2667 ("invalid address clause for initialized object &!",
2670 ("\must be constant defined before& ('R'M 13.1(22))!",
2673 end Check_Expr_Constants;
2675 --------------------------
2676 -- Check_List_Constants --
2677 --------------------------
2679 procedure Check_List_Constants (Lst : List_Id) is
2683 if Present (Lst) then
2684 Nod1 := First (Lst);
2685 while Present (Nod1) loop
2686 Check_Expr_Constants (Nod1);
2690 end Check_List_Constants;
2692 -- Start of processing for Check_Constant_Address_Clause
2695 Check_Expr_Constants (Expr);
2696 end Check_Constant_Address_Clause;
2702 procedure Check_Size
2706 Biased : out Boolean)
2708 UT : constant Entity_Id := Underlying_Type (T);
2714 -- Immediate return if size is same as standard size or if composite
2715 -- item, or generic type, or type with previous errors.
2718 or else UT = Any_Type
2719 or else Is_Generic_Type (UT)
2720 or else Is_Generic_Type (Root_Type (UT))
2721 or else Is_Composite_Type (UT)
2722 or else (Known_Esize (UT) and then Siz = Esize (UT))
2726 -- For fixed-point types, don't check minimum if type is not frozen,
2727 -- since type is not known till then
2730 elsif Is_Fixed_Point_Type (UT)
2731 and then not Is_Frozen (UT)
2735 -- Cases for which a minimum check is required
2738 M := UI_From_Int (Minimum_Size (UT));
2742 -- Size is less than minimum size, but one possibility remains
2743 -- that we can manage with the new size if we bias the type
2745 M := UI_From_Int (Minimum_Size (UT, Biased => True));
2748 Error_Msg_Uint_1 := M;
2750 ("size for& too small, minimum allowed is ^", N, T);
2758 -------------------------
2759 -- Get_Alignment_Value --
2760 -------------------------
2762 function Get_Alignment_Value (Expr : Node_Id) return Uint is
2763 Align : constant Uint := Static_Integer (Expr);
2766 if Align = No_Uint then
2769 elsif Align <= 0 then
2770 Error_Msg_N ("alignment value must be positive", Expr);
2774 for J in Int range 0 .. 64 loop
2776 M : constant Uint := Uint_2 ** J;
2779 exit when M = Align;
2783 ("alignment value must be power of 2", Expr);
2791 end Get_Alignment_Value;
2793 -------------------------------------
2794 -- Get_Attribute_Definition_Clause --
2795 -------------------------------------
2797 function Get_Attribute_Definition_Clause
2805 N := First_Rep_Item (E);
2806 while Present (N) loop
2807 if Nkind (N) = N_Attribute_Definition_Clause
2808 and then Get_Attribute_Id (Chars (N)) = Id
2817 end Get_Attribute_Definition_Clause;
2819 --------------------
2820 -- Get_Rep_Pragma --
2821 --------------------
2823 function Get_Rep_Pragma (E : Entity_Id; Nam : Name_Id) return Node_Id is
2828 N := First_Rep_Item (E);
2830 while Present (N) loop
2831 if Nkind (N) = N_Pragma and then Chars (N) = Nam then
2833 if Nam = Name_Stream_Convert then
2835 -- For tagged types this pragma is not inherited, so we
2836 -- must verify that it is defined for the given type and
2839 Typ := Entity (Expression
2840 (First (Pragma_Argument_Associations (N))));
2842 if not Is_Tagged_Type (E)
2844 or else (Is_Private_Type (Typ)
2845 and then E = Full_View (Typ))
2867 procedure Initialize is
2869 Unchecked_Conversions.Init;
2872 -------------------------
2873 -- Is_Operational_Item --
2874 -------------------------
2876 function Is_Operational_Item (N : Node_Id) return Boolean is
2878 if Nkind (N) /= N_Attribute_Definition_Clause then
2882 Id : constant Attribute_Id := Get_Attribute_Id (Chars (N));
2885 return Id = Attribute_Input
2886 or else Id = Attribute_Output
2887 or else Id = Attribute_Read
2888 or else Id = Attribute_Write;
2891 end Is_Operational_Item;
2897 function Minimum_Size
2899 Biased : Boolean := False)
2902 Lo : Uint := No_Uint;
2903 Hi : Uint := No_Uint;
2904 LoR : Ureal := No_Ureal;
2905 HiR : Ureal := No_Ureal;
2906 LoSet : Boolean := False;
2907 HiSet : Boolean := False;
2913 -- If bad type, return 0
2915 if T = Any_Type then
2918 -- For generic types, just return zero. There cannot be any legitimate
2919 -- need to know such a size, but this routine may be called with a
2920 -- generic type as part of normal processing.
2922 elsif Is_Generic_Type (Root_Type (T)) then
2927 elsif Is_Access_Type (T) then
2928 return System_Address_Size;
2930 -- Floating-point types
2932 elsif Is_Floating_Point_Type (T) then
2933 return UI_To_Int (Esize (Root_Type (T)));
2937 elsif Is_Discrete_Type (T) then
2939 -- The following loop is looking for the nearest compile time
2940 -- known bounds following the ancestor subtype chain. The idea
2941 -- is to find the most restrictive known bounds information.
2945 if Ancest = Any_Type or else Etype (Ancest) = Any_Type then
2950 if Compile_Time_Known_Value (Type_Low_Bound (Ancest)) then
2951 Lo := Expr_Rep_Value (Type_Low_Bound (Ancest));
2958 if Compile_Time_Known_Value (Type_High_Bound (Ancest)) then
2959 Hi := Expr_Rep_Value (Type_High_Bound (Ancest));
2965 Ancest := Ancestor_Subtype (Ancest);
2968 Ancest := Base_Type (T);
2970 if Is_Generic_Type (Ancest) then
2976 -- Fixed-point types. We can't simply use Expr_Value to get the
2977 -- Corresponding_Integer_Value values of the bounds, since these
2978 -- do not get set till the type is frozen, and this routine can
2979 -- be called before the type is frozen. Similarly the test for
2980 -- bounds being static needs to include the case where we have
2981 -- unanalyzed real literals for the same reason.
2983 elsif Is_Fixed_Point_Type (T) then
2985 -- The following loop is looking for the nearest compile time
2986 -- known bounds following the ancestor subtype chain. The idea
2987 -- is to find the most restrictive known bounds information.
2991 if Ancest = Any_Type or else Etype (Ancest) = Any_Type then
2996 if Nkind (Type_Low_Bound (Ancest)) = N_Real_Literal
2997 or else Compile_Time_Known_Value (Type_Low_Bound (Ancest))
2999 LoR := Expr_Value_R (Type_Low_Bound (Ancest));
3006 if Nkind (Type_High_Bound (Ancest)) = N_Real_Literal
3007 or else Compile_Time_Known_Value (Type_High_Bound (Ancest))
3009 HiR := Expr_Value_R (Type_High_Bound (Ancest));
3015 Ancest := Ancestor_Subtype (Ancest);
3018 Ancest := Base_Type (T);
3020 if Is_Generic_Type (Ancest) then
3026 Lo := UR_To_Uint (LoR / Small_Value (T));
3027 Hi := UR_To_Uint (HiR / Small_Value (T));
3029 -- No other types allowed
3032 raise Program_Error;
3035 -- Fall through with Hi and Lo set. Deal with biased case.
3037 if (Biased and then not Is_Fixed_Point_Type (T))
3038 or else Has_Biased_Representation (T)
3044 -- Signed case. Note that we consider types like range 1 .. -1 to be
3045 -- signed for the purpose of computing the size, since the bounds
3046 -- have to be accomodated in the base type.
3048 if Lo < 0 or else Hi < 0 then
3052 -- S = size, B = 2 ** (size - 1) (can accomodate -B .. +(B - 1))
3053 -- Note that we accomodate the case where the bounds cross. This
3054 -- can happen either because of the way the bounds are declared
3055 -- or because of the algorithm in Freeze_Fixed_Point_Type.
3069 -- If both bounds are positive, make sure that both are represen-
3070 -- table in the case where the bounds are crossed. This can happen
3071 -- either because of the way the bounds are declared, or because of
3072 -- the algorithm in Freeze_Fixed_Point_Type.
3078 -- S = size, (can accomodate 0 .. (2**size - 1))
3081 while Hi >= Uint_2 ** S loop
3089 -------------------------
3090 -- New_Stream_Function --
3091 -------------------------
3093 procedure New_Stream_Function
3099 Loc : constant Source_Ptr := Sloc (N);
3100 Subp_Id : Entity_Id := Make_Defining_Identifier (Loc, Nam);
3101 Subp_Decl : Node_Id;
3106 F := First_Formal (Subp);
3107 Etyp := Etype (Subp);
3110 Make_Subprogram_Renaming_Declaration (Loc,
3113 Make_Function_Specification (Loc,
3114 Defining_Unit_Name => Subp_Id,
3115 Parameter_Specifications =>
3117 Make_Parameter_Specification (Loc,
3118 Defining_Identifier =>
3119 Make_Defining_Identifier (Loc, Name_S),
3121 Make_Access_Definition (Loc,
3124 Designated_Type (Etype (F)), Loc)))),
3127 New_Reference_To (Etyp, Loc)),
3129 Name => New_Reference_To (Subp, Loc));
3131 if Is_Tagged_Type (Ent) and then not Is_Limited_Type (Ent) then
3132 Set_TSS (Base_Type (Ent), Subp_Id);
3134 Insert_Action (N, Subp_Decl);
3135 Copy_TSS (Subp_Id, Base_Type (Ent));
3138 end New_Stream_Function;
3140 --------------------------
3141 -- New_Stream_Procedure --
3142 --------------------------
3144 procedure New_Stream_Procedure
3149 Out_P : Boolean := False)
3151 Loc : constant Source_Ptr := Sloc (N);
3152 Subp_Id : Entity_Id := Make_Defining_Identifier (Loc, Nam);
3153 Subp_Decl : Node_Id;
3158 F := First_Formal (Subp);
3159 Etyp := Etype (Next_Formal (F));
3162 Make_Subprogram_Renaming_Declaration (Loc,
3165 Make_Procedure_Specification (Loc,
3166 Defining_Unit_Name => Subp_Id,
3167 Parameter_Specifications =>
3169 Make_Parameter_Specification (Loc,
3170 Defining_Identifier =>
3171 Make_Defining_Identifier (Loc, Name_S),
3173 Make_Access_Definition (Loc,
3176 Designated_Type (Etype (F)), Loc))),
3178 Make_Parameter_Specification (Loc,
3179 Defining_Identifier =>
3180 Make_Defining_Identifier (Loc, Name_V),
3181 Out_Present => Out_P,
3183 New_Reference_To (Etyp, Loc)))),
3184 Name => New_Reference_To (Subp, Loc));
3186 if Is_Tagged_Type (Ent) and then not Is_Limited_Type (Ent) then
3187 Set_TSS (Base_Type (Ent), Subp_Id);
3189 Insert_Action (N, Subp_Decl);
3190 Copy_TSS (Subp_Id, Base_Type (Ent));
3193 end New_Stream_Procedure;
3195 ---------------------
3196 -- Record_Rep_Item --
3197 ---------------------
3199 procedure Record_Rep_Item (T : Entity_Id; N : Node_Id) is
3201 Set_Next_Rep_Item (N, First_Rep_Item (T));
3202 Set_First_Rep_Item (T, N);
3203 end Record_Rep_Item;
3205 ------------------------
3206 -- Rep_Item_Too_Early --
3207 ------------------------
3209 function Rep_Item_Too_Early
3215 -- Cannot apply rep items to generic types
3218 and then Is_Generic_Type (Root_Type (T))
3221 ("representation item not allowed for generic type", N);
3225 -- Otherwise check for incompleted type
3227 if Is_Incomplete_Or_Private_Type (T)
3228 and then No (Underlying_Type (T))
3231 ("representation item must be after full type declaration", N);
3234 -- If the type has incompleted components, a representation clause is
3235 -- illegal but stream attributes and Convention pragmas are correct.
3237 elsif Has_Private_Component (T) then
3238 if (Nkind (N) = N_Pragma or else Is_Operational_Item (N)) then
3242 ("representation item must appear after type is fully defined",
3249 end Rep_Item_Too_Early;
3251 -----------------------
3252 -- Rep_Item_Too_Late --
3253 -----------------------
3255 function Rep_Item_Too_Late
3258 FOnly : Boolean := False)
3262 Parent_Type : Entity_Id;
3265 -- Output the too late message
3267 procedure Too_Late is
3269 Error_Msg_N ("representation item appears too late!", N);
3272 -- Start of processing for Rep_Item_Too_Late
3275 -- First make sure entity is not frozen (RM 13.1(9)). Exclude imported
3276 -- types, which may be frozen if they appear in a representation clause
3277 -- for a local type.
3280 and then not From_With_Type (T)
3283 S := First_Subtype (T);
3285 if Present (Freeze_Node (S)) then
3287 ("?no more representation items for }!", Freeze_Node (S), S);
3292 -- Check for case of non-tagged derived type whose parent either has
3293 -- primitive operations, or is a by reference type (RM 13.1(10)).
3297 and then Is_Derived_Type (T)
3298 and then not Is_Tagged_Type (T)
3300 Parent_Type := Etype (Base_Type (T));
3302 if Has_Primitive_Operations (Parent_Type) then
3305 ("primitive operations already defined for&!", N, Parent_Type);
3308 elsif Is_By_Reference_Type (Parent_Type) then
3311 ("parent type & is a by reference type!", N, Parent_Type);
3316 -- No error, link item into head of chain of rep items for the entity
3318 Record_Rep_Item (T, N);
3320 end Rep_Item_Too_Late;
3322 -------------------------
3323 -- Same_Representation --
3324 -------------------------
3326 function Same_Representation (Typ1, Typ2 : Entity_Id) return Boolean is
3327 T1 : constant Entity_Id := Underlying_Type (Typ1);
3328 T2 : constant Entity_Id := Underlying_Type (Typ2);
3331 -- A quick check, if base types are the same, then we definitely have
3332 -- the same representation, because the subtype specific representation
3333 -- attributes (Size and Alignment) do not affect representation from
3334 -- the point of view of this test.
3336 if Base_Type (T1) = Base_Type (T2) then
3339 elsif Is_Private_Type (Base_Type (T2))
3340 and then Base_Type (T1) = Full_View (Base_Type (T2))
3345 -- Tagged types never have differing representations
3347 if Is_Tagged_Type (T1) then
3351 -- Representations are definitely different if conventions differ
3353 if Convention (T1) /= Convention (T2) then
3357 -- Representations are different if component alignments differ
3359 if (Is_Record_Type (T1) or else Is_Array_Type (T1))
3361 (Is_Record_Type (T2) or else Is_Array_Type (T2))
3362 and then Component_Alignment (T1) /= Component_Alignment (T2)
3367 -- For arrays, the only real issue is component size. If we know the
3368 -- component size for both arrays, and it is the same, then that's
3369 -- good enough to know we don't have a change of representation.
3371 if Is_Array_Type (T1) then
3372 if Known_Component_Size (T1)
3373 and then Known_Component_Size (T2)
3374 and then Component_Size (T1) = Component_Size (T2)
3380 -- Types definitely have same representation if neither has non-standard
3381 -- representation since default representations are always consistent.
3382 -- If only one has non-standard representation, and the other does not,
3383 -- then we consider that they do not have the same representation. They
3384 -- might, but there is no way of telling early enough.
3386 if Has_Non_Standard_Rep (T1) then
3387 if not Has_Non_Standard_Rep (T2) then
3391 return not Has_Non_Standard_Rep (T2);
3394 -- Here the two types both have non-standard representation, and we
3395 -- need to determine if they have the same non-standard representation
3397 -- For arrays, we simply need to test if the component sizes are the
3398 -- same. Pragma Pack is reflected in modified component sizes, so this
3399 -- check also deals with pragma Pack.
3401 if Is_Array_Type (T1) then
3402 return Component_Size (T1) = Component_Size (T2);
3404 -- Tagged types always have the same representation, because it is not
3405 -- possible to specify different representations for common fields.
3407 elsif Is_Tagged_Type (T1) then
3410 -- Case of record types
3412 elsif Is_Record_Type (T1) then
3414 -- Packed status must conform
3416 if Is_Packed (T1) /= Is_Packed (T2) then
3419 -- Otherwise we must check components. Typ2 maybe a constrained
3420 -- subtype with fewer components, so we compare the components
3421 -- of the base types.
3424 Record_Case : declare
3425 CD1, CD2 : Entity_Id;
3427 function Same_Rep return Boolean;
3428 -- CD1 and CD2 are either components or discriminants. This
3429 -- function tests whether the two have the same representation
3431 function Same_Rep return Boolean is
3433 if No (Component_Clause (CD1)) then
3434 return No (Component_Clause (CD2));
3438 Present (Component_Clause (CD2))
3440 Component_Bit_Offset (CD1) = Component_Bit_Offset (CD2)
3442 Esize (CD1) = Esize (CD2);
3446 -- Start processing for Record_Case
3449 if Has_Discriminants (T1) then
3450 CD1 := First_Discriminant (T1);
3451 CD2 := First_Discriminant (T2);
3453 while Present (CD1) loop
3454 if not Same_Rep then
3457 Next_Discriminant (CD1);
3458 Next_Discriminant (CD2);
3463 CD1 := First_Component (Underlying_Type (Base_Type (T1)));
3464 CD2 := First_Component (Underlying_Type (Base_Type (T2)));
3466 while Present (CD1) loop
3467 if not Same_Rep then
3470 Next_Component (CD1);
3471 Next_Component (CD2);
3479 -- For enumeration types, we must check each literal to see if the
3480 -- representation is the same. Note that we do not permit enumeration
3481 -- reprsentation clauses for Character and Wide_Character, so these
3482 -- cases were already dealt with.
3484 elsif Is_Enumeration_Type (T1) then
3486 Enumeration_Case : declare
3490 L1 := First_Literal (T1);
3491 L2 := First_Literal (T2);
3493 while Present (L1) loop
3494 if Enumeration_Rep (L1) /= Enumeration_Rep (L2) then
3504 end Enumeration_Case;
3506 -- Any other types have the same representation for these purposes
3512 end Same_Representation;
3514 --------------------
3515 -- Set_Enum_Esize --
3516 --------------------
3518 procedure Set_Enum_Esize (T : Entity_Id) is
3526 -- Find the minimum standard size (8,16,32,64) that fits
3528 Lo := Enumeration_Rep (Entity (Type_Low_Bound (T)));
3529 Hi := Enumeration_Rep (Entity (Type_High_Bound (T)));
3532 if Lo >= -Uint_2**07 and then Hi < Uint_2**07 then
3535 elsif Lo >= -Uint_2**15 and then Hi < Uint_2**15 then
3538 elsif Lo >= -Uint_2**31 and then Hi < Uint_2**31 then
3541 else pragma Assert (Lo >= -Uint_2**63 and then Hi < Uint_2**63);
3546 if Hi < Uint_2**08 then
3549 elsif Hi < Uint_2**16 then
3552 elsif Hi < Uint_2**32 then
3555 else pragma Assert (Hi < Uint_2**63);
3560 -- That minimum is the proper size unless we have a foreign convention
3561 -- and the size required is 32 or less, in which case we bump the size
3562 -- up to 32. This is required for C and C++ and seems reasonable for
3563 -- all other foreign conventions.
3565 if Has_Foreign_Convention (T)
3566 and then Esize (T) < Standard_Integer_Size
3568 Init_Esize (T, Standard_Integer_Size);
3576 -----------------------------------
3577 -- Validate_Unchecked_Conversion --
3578 -----------------------------------
3580 procedure Validate_Unchecked_Conversion
3582 Act_Unit : Entity_Id)
3589 -- Obtain source and target types. Note that we call Ancestor_Subtype
3590 -- here because the processing for generic instantiation always makes
3591 -- subtypes, and we want the original frozen actual types.
3593 -- If we are dealing with private types, then do the check on their
3594 -- fully declared counterparts if the full declarations have been
3595 -- encountered (they don't have to be visible, but they must exist!)
3597 Source := Ancestor_Subtype (Etype (First_Formal (Act_Unit)));
3599 if Is_Private_Type (Source)
3600 and then Present (Underlying_Type (Source))
3602 Source := Underlying_Type (Source);
3605 Target := Ancestor_Subtype (Etype (Act_Unit));
3607 -- If either type is generic, the instantiation happens within a
3608 -- generic unit, and there is nothing to check. The proper check
3609 -- will happen when the enclosing generic is instantiated.
3611 if Is_Generic_Type (Source) or else Is_Generic_Type (Target) then
3615 if Is_Private_Type (Target)
3616 and then Present (Underlying_Type (Target))
3618 Target := Underlying_Type (Target);
3621 -- Source may be unconstrained array, but not target
3623 if Is_Array_Type (Target)
3624 and then not Is_Constrained (Target)
3627 ("unchecked conversion to unconstrained array not allowed", N);
3631 -- Make entry in unchecked conversion table for later processing
3632 -- by Validate_Unchecked_Conversions, which will check sizes and
3633 -- alignments (using values set by the back-end where possible).
3635 Unchecked_Conversions.Append
3636 (New_Val => UC_Entry'
3641 -- Generate N_Validate_Unchecked_Conversion node for back end if
3642 -- the back end needs to perform special validation checks. At the
3643 -- current time, only the JVM version requires such checks.
3647 Make_Validate_Unchecked_Conversion (Sloc (N));
3648 Set_Source_Type (Vnode, Source);
3649 Set_Target_Type (Vnode, Target);
3650 Insert_After (N, Vnode);
3652 end Validate_Unchecked_Conversion;
3654 ------------------------------------
3655 -- Validate_Unchecked_Conversions --
3656 ------------------------------------
3658 procedure Validate_Unchecked_Conversions is
3660 for N in Unchecked_Conversions.First .. Unchecked_Conversions.Last loop
3662 T : UC_Entry renames Unchecked_Conversions.Table (N);
3664 Enode : constant Node_Id := T.Enode;
3665 Source : constant Entity_Id := T.Source;
3666 Target : constant Entity_Id := T.Target;
3672 -- This validation check, which warns if we have unequal sizes
3673 -- for unchecked conversion, and thus potentially implementation
3674 -- dependent semantics, is one of the few occasions on which we
3675 -- use the official RM size instead of Esize. See description
3676 -- in Einfo "Handling of Type'Size Values" for details.
3678 if Errors_Detected = 0
3679 and then Known_Static_RM_Size (Source)
3680 and then Known_Static_RM_Size (Target)
3682 Source_Siz := RM_Size (Source);
3683 Target_Siz := RM_Size (Target);
3685 if Source_Siz /= Target_Siz then
3686 Warn_On_Instance := True;
3688 ("types for unchecked conversion have different sizes?",
3691 if All_Errors_Mode then
3692 Error_Msg_Name_1 := Chars (Source);
3693 Error_Msg_Uint_1 := Source_Siz;
3694 Error_Msg_Name_2 := Chars (Target);
3695 Error_Msg_Uint_2 := Target_Siz;
3697 ("\size of % is ^, size of % is ^?", Enode);
3699 Error_Msg_Uint_1 := UI_Abs (Source_Siz - Target_Siz);
3701 if Is_Discrete_Type (Source)
3702 and then Is_Discrete_Type (Target)
3704 if Source_Siz > Target_Siz then
3706 ("\^ high order bits of source will be ignored?",
3709 elsif Is_Modular_Integer_Type (Source) then
3711 ("\source will be extended with ^ high order " &
3712 "zero bits?", Enode);
3716 ("\source will be extended with ^ high order " &
3721 elsif Source_Siz < Target_Siz then
3722 if Is_Discrete_Type (Target) then
3723 if Bytes_Big_Endian then
3725 ("\target value will include ^ undefined " &
3730 ("\target value will include ^ undefined " &
3737 ("\^ trailing bits of target value will be " &
3738 "undefined?", Enode);
3741 else pragma Assert (Source_Siz > Target_Siz);
3743 ("\^ trailing bits of source will be ignored?",
3748 Warn_On_Instance := False;
3752 -- If both types are access types, we need to check the alignment.
3753 -- If the alignment of both is specified, we can do it here.
3755 if Errors_Detected = 0
3756 and then Ekind (Source) in Access_Kind
3757 and then Ekind (Target) in Access_Kind
3758 and then Target_Strict_Alignment
3759 and then Present (Designated_Type (Source))
3760 and then Present (Designated_Type (Target))
3763 D_Source : constant Entity_Id := Designated_Type (Source);
3764 D_Target : constant Entity_Id := Designated_Type (Target);
3767 if Known_Alignment (D_Source)
3768 and then Known_Alignment (D_Target)
3771 Source_Align : constant Uint := Alignment (D_Source);
3772 Target_Align : constant Uint := Alignment (D_Target);
3775 if Source_Align < Target_Align
3776 and then not Is_Tagged_Type (D_Source)
3778 Warn_On_Instance := True;
3779 Error_Msg_Uint_1 := Target_Align;
3780 Error_Msg_Uint_2 := Source_Align;
3781 Error_Msg_Node_2 := D_Source;
3783 ("alignment of & (^) is stricter than " &
3784 "alignment of & (^)?", Enode, D_Target);
3786 if All_Errors_Mode then
3788 ("\resulting access value may have invalid " &
3789 "alignment?", Enode);
3792 Warn_On_Instance := False;
3800 end Validate_Unchecked_Conversions;
3806 procedure Warn_Overlay
3811 Old : Entity_Id := Empty;
3815 if not Address_Clause_Overlay_Warnings then
3820 and then (Has_Non_Null_Base_Init_Proc (Typ)
3821 or else Is_Access_Type (Typ))
3822 and then not Is_Imported (Entity (Nam))
3824 if Nkind (Expr) = N_Attribute_Reference
3825 and then Is_Entity_Name (Prefix (Expr))
3827 Old := Entity (Prefix (Expr));
3829 elsif Is_Entity_Name (Expr)
3830 and then Ekind (Entity (Expr)) = E_Constant
3832 Decl := Declaration_Node (Entity (Expr));
3834 if Nkind (Decl) = N_Object_Declaration
3835 and then Present (Expression (Decl))
3836 and then Nkind (Expression (Decl)) = N_Attribute_Reference
3837 and then Is_Entity_Name (Prefix (Expression (Decl)))
3839 Old := Entity (Prefix (Expression (Decl)));
3841 elsif Nkind (Expr) = N_Function_Call then
3845 -- A function call (most likely to To_Address) is probably not
3846 -- an overlay, so skip warning. Ditto if the function call was
3847 -- inlined and transformed into an entity.
3849 elsif Nkind (Original_Node (Expr)) = N_Function_Call then
3853 Decl := Next (Parent (Expr));
3855 -- If a pragma Import follows, we assume that it is for the current
3856 -- target of the address clause, and skip the warning.
3859 and then Nkind (Decl) = N_Pragma
3860 and then Chars (Decl) = Name_Import
3865 if Present (Old) then
3866 Error_Msg_Node_2 := Old;
3868 ("default initialization of & may modify &?",
3872 ("default initialization of & may modify overlaid storage?",
3876 -- Add friendly warning if initialization comes from a packed array
3879 if Is_Record_Type (Typ) then
3884 Comp := First_Component (Typ);
3886 while Present (Comp) loop
3887 if Nkind (Parent (Comp)) = N_Component_Declaration
3888 and then Present (Expression (Parent (Comp)))
3891 elsif Is_Array_Type (Etype (Comp))
3892 and then Present (Packed_Array_Type (Etype (Comp)))
3895 ("packed array component& will be initialized to zero?",
3899 Next_Component (Comp);
3906 ("use pragma Import for & to " &
3907 "suppress initialization ('R'M B.1(24))?",