1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2004, 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;
34 with Nlists; use Nlists;
35 with Nmake; use Nmake;
37 with Rtsfind; use Rtsfind;
39 with Sem_Ch8; use Sem_Ch8;
40 with Sem_Eval; use Sem_Eval;
41 with Sem_Res; use Sem_Res;
42 with Sem_Type; use Sem_Type;
43 with Sem_Util; use Sem_Util;
44 with Snames; use Snames;
45 with Stand; use Stand;
46 with Sinfo; use Sinfo;
48 with Targparm; use Targparm;
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 function Address_Aliased_Entity (N : Node_Id) return Entity_Id;
86 -- If expression N is of the form E'Address, return E.
88 procedure Mark_Aliased_Address_As_Volatile (N : Node_Id);
89 -- This is used for processing of an address representation clause. If
90 -- the expression N is of the form of K'Address, then the entity that
91 -- is associated with K is marked as volatile.
93 procedure New_Stream_Function
98 -- Create a function renaming of a given stream attribute to the
99 -- designated subprogram and then in the tagged case, provide this as
100 -- a primitive operation, or in the non-tagged case make an appropriate
101 -- TSS entry. Used for Input. This is more properly an expansion activity
102 -- than just semantics, but the presence of user-defined stream functions
103 -- for limited types is a legality check, which is why this takes place
104 -- here rather than in exp_ch13, where it was previously. Nam indicates
105 -- the name of the TSS function to be generated.
107 -- To avoid elaboration anomalies with freeze nodes, for untagged types
108 -- we generate both a subprogram declaration and a subprogram renaming
109 -- declaration, so that the attribute specification is handled as a
110 -- renaming_as_body. For tagged types, the specification is one of the
113 procedure New_Stream_Procedure
118 Out_P : Boolean := False);
119 -- Create a procedure renaming of a given stream attribute to the
120 -- designated subprogram and then in the tagged case, provide this as
121 -- a primitive operation, or in the non-tagged case make an appropriate
122 -- TSS entry. Used for Read, Output, Write. Nam indicates the name of
123 -- the TSS procedure to be generated.
125 ----------------------------------------------
126 -- Table for Validate_Unchecked_Conversions --
127 ----------------------------------------------
129 -- The following table collects unchecked conversions for validation.
130 -- Entries are made by Validate_Unchecked_Conversion and then the
131 -- call to Validate_Unchecked_Conversions does the actual error
132 -- checking and posting of warnings. The reason for this delayed
133 -- processing is to take advantage of back-annotations of size and
134 -- alignment values peformed by the back end.
136 type UC_Entry is record
137 Enode : Node_Id; -- node used for posting warnings
138 Source : Entity_Id; -- source type for unchecked conversion
139 Target : Entity_Id; -- target type for unchecked conversion
142 package Unchecked_Conversions is new Table.Table (
143 Table_Component_Type => UC_Entry,
144 Table_Index_Type => Int,
145 Table_Low_Bound => 1,
147 Table_Increment => 200,
148 Table_Name => "Unchecked_Conversions");
150 ----------------------------
151 -- Address_Aliased_Entity --
152 ----------------------------
154 function Address_Aliased_Entity (N : Node_Id) return Entity_Id is
156 if Nkind (N) = N_Attribute_Reference
157 and then Attribute_Name (N) = Name_Address
160 Nam : Node_Id := Prefix (N);
163 or else Nkind (Nam) = N_Selected_Component
164 or else Nkind (Nam) = N_Indexed_Component
169 if Is_Entity_Name (Nam) then
176 end Address_Aliased_Entity;
178 --------------------------------------
179 -- Alignment_Check_For_Esize_Change --
180 --------------------------------------
182 procedure Alignment_Check_For_Esize_Change (Typ : Entity_Id) is
184 -- If the alignment is known, and not set by a rep clause, and is
185 -- inconsistent with the size being set, then reset it to unknown,
186 -- we assume in this case that the size overrides the inherited
187 -- alignment, and that the alignment must be recomputed.
189 if Known_Alignment (Typ)
190 and then not Has_Alignment_Clause (Typ)
191 and then Esize (Typ) mod (Alignment (Typ) * SSU) /= 0
193 Init_Alignment (Typ);
195 end Alignment_Check_For_Esize_Change;
197 -----------------------
198 -- Analyze_At_Clause --
199 -----------------------
201 -- An at clause is replaced by the corresponding Address attribute
202 -- definition clause that is the preferred approach in Ada 95.
204 procedure Analyze_At_Clause (N : Node_Id) is
206 if Warn_On_Obsolescent_Feature then
208 ("at clause is an obsolescent feature ('R'M 'J.7(2))?", N);
210 ("|use address attribute definition clause instead?", N);
214 Make_Attribute_Definition_Clause (Sloc (N),
215 Name => Identifier (N),
216 Chars => Name_Address,
217 Expression => Expression (N)));
218 Analyze_Attribute_Definition_Clause (N);
219 end Analyze_At_Clause;
221 -----------------------------------------
222 -- Analyze_Attribute_Definition_Clause --
223 -----------------------------------------
225 procedure Analyze_Attribute_Definition_Clause (N : Node_Id) is
226 Loc : constant Source_Ptr := Sloc (N);
227 Nam : constant Node_Id := Name (N);
228 Attr : constant Name_Id := Chars (N);
229 Expr : constant Node_Id := Expression (N);
230 Id : constant Attribute_Id := Get_Attribute_Id (Attr);
234 FOnly : Boolean := False;
235 -- Reset to True for subtype specific attribute (Alignment, Size)
236 -- and for stream attributes, i.e. those cases where in the call
237 -- to Rep_Item_Too_Late, FOnly is set True so that only the freezing
238 -- rules are checked. Note that the case of stream attributes is not
239 -- clear from the RM, but see AI95-00137. Also, the RM seems to
240 -- disallow Storage_Size for derived task types, but that is also
241 -- clearly unintentional.
247 if Rep_Item_Too_Early (Ent, N) then
251 -- Rep clause applies to full view of incomplete type or private type
252 -- if we have one (if not, this is a premature use of the type).
253 -- However, certain semantic checks need to be done on the specified
254 -- entity (i.e. the private view), so we save it in Ent.
256 if Is_Private_Type (Ent)
257 and then Is_Derived_Type (Ent)
258 and then not Is_Tagged_Type (Ent)
259 and then No (Full_View (Ent))
261 -- If this is a private type whose completion is a derivation
262 -- from another private type, there is no full view, and the
263 -- attribute belongs to the type itself, not its underlying parent.
267 elsif Ekind (Ent) = E_Incomplete_Type then
269 -- The attribute applies to the full view, set the entity
270 -- of the attribute definition accordingly.
272 Ent := Underlying_Type (Ent);
274 Set_Entity (Nam, Ent);
277 U_Ent := Underlying_Type (Ent);
280 -- Complete other routine error checks
282 if Etype (Nam) = Any_Type then
285 elsif Scope (Ent) /= Current_Scope then
286 Error_Msg_N ("entity must be declared in this scope", Nam);
289 elsif No (U_Ent) then
292 elsif Is_Type (U_Ent)
293 and then not Is_First_Subtype (U_Ent)
294 and then Id /= Attribute_Object_Size
295 and then Id /= Attribute_Value_Size
296 and then not From_At_Mod (N)
298 Error_Msg_N ("cannot specify attribute for subtype", Nam);
303 -- Switch on particular attribute
311 -- Address attribute definition clause
313 when Attribute_Address => Address : begin
314 Analyze_And_Resolve (Expr, RTE (RE_Address));
316 if Present (Address_Clause (U_Ent)) then
317 Error_Msg_N ("address already given for &", Nam);
319 -- Case of address clause for subprogram
321 elsif Is_Subprogram (U_Ent) then
322 if Has_Homonym (U_Ent) then
324 ("address clause cannot be given " &
325 "for overloaded subprogram",
329 -- For subprograms, all address clauses are permitted,
330 -- and we mark the subprogram as having a deferred freeze
331 -- so that Gigi will not elaborate it too soon.
333 -- Above needs more comments, what is too soon about???
335 Set_Has_Delayed_Freeze (U_Ent);
337 -- Case of address clause for entry
339 elsif Ekind (U_Ent) = E_Entry then
340 if Nkind (Parent (N)) = N_Task_Body then
342 ("entry address must be specified in task spec", Nam);
345 -- For entries, we require a constant address
347 Check_Constant_Address_Clause (Expr, U_Ent);
349 if Is_Task_Type (Scope (U_Ent))
350 and then Comes_From_Source (Scope (U_Ent))
353 ("?entry address declared for entry in task type", N);
355 ("\?only one task can be declared of this type", N);
358 if Warn_On_Obsolescent_Feature then
360 ("attaching interrupt to task entry is an " &
361 "obsolescent feature ('R'M 'J.7.1)?", N);
363 ("|use interrupt procedure instead?", N);
366 -- Case of an address clause for a controlled object:
367 -- erroneous execution.
369 elsif Is_Controlled (Etype (U_Ent)) then
371 ("?controlled object& must not be overlaid", Nam, U_Ent);
373 ("\?Program_Error will be raised at run time", Nam);
374 Insert_Action (Declaration_Node (U_Ent),
375 Make_Raise_Program_Error (Loc,
376 Reason => PE_Overlaid_Controlled_Object));
378 -- Case of address clause for a (non-controlled) object
381 Ekind (U_Ent) = E_Variable
383 Ekind (U_Ent) = E_Constant
386 Expr : constant Node_Id := Expression (N);
387 Aent : constant Entity_Id := Address_Aliased_Entity (Expr);
390 -- Exported variables cannot have an address clause,
391 -- because this cancels the effect of the pragma Export
393 if Is_Exported (U_Ent) then
395 ("cannot export object with address clause", Nam);
397 -- Overlaying controlled objects is erroneous
400 and then Is_Controlled (Etype (Aent))
403 ("?controlled object must not be overlaid", Expr);
405 ("\?Program_Error will be raised at run time", Expr);
406 Insert_Action (Declaration_Node (U_Ent),
407 Make_Raise_Program_Error (Loc,
408 Reason => PE_Overlaid_Controlled_Object));
411 and then Ekind (U_Ent) = E_Constant
412 and then Ekind (Aent) /= E_Constant
414 Error_Msg_N ("constant overlays a variable?", Expr);
416 elsif Present (Renamed_Object (U_Ent)) then
418 ("address clause not allowed"
419 & " for a renaming declaration ('R'M 13.1(6))", Nam);
421 -- Imported variables can have an address clause, but then
422 -- the import is pretty meaningless except to suppress
423 -- initializations, so we do not need such variables to
424 -- be statically allocated (and in fact it causes trouble
425 -- if the address clause is a local value).
427 elsif Is_Imported (U_Ent) then
428 Set_Is_Statically_Allocated (U_Ent, False);
431 -- We mark a possible modification of a variable with an
432 -- address clause, since it is likely aliasing is occurring.
434 Note_Possible_Modification (Nam);
436 -- Here we are checking for explicit overlap of one
437 -- variable by another, and if we find this, then we
438 -- mark the overlapped variable as also being aliased.
440 -- First case is where we have an explicit
442 -- for J'Address use K'Address;
444 -- In this case, we mark K as volatile
446 Mark_Aliased_Address_As_Volatile (Expr);
448 -- Second case is where we have a constant whose
449 -- definition is of the form of an adress as in:
451 -- A : constant Address := K'Address;
453 -- for B'Address use A;
455 -- In this case we also mark K as volatile
457 if Is_Entity_Name (Expr) then
459 Ent : constant Entity_Id := Entity (Expr);
460 Decl : constant Node_Id := Declaration_Node (Ent);
463 if Ekind (Ent) = E_Constant
464 and then Nkind (Decl) = N_Object_Declaration
465 and then Present (Expression (Decl))
467 Mark_Aliased_Address_As_Volatile
473 -- Legality checks on the address clause for initialized
474 -- objects is deferred until the freeze point, because
475 -- a subsequent pragma might indicate that the object is
476 -- imported and thus not initialized.
478 Set_Has_Delayed_Freeze (U_Ent);
480 if Is_Exported (U_Ent) then
482 ("& cannot be exported if an address clause is given",
485 ("\define and export a variable " &
486 "that holds its address instead",
490 -- Entity has delayed freeze, so we will generate
491 -- an alignment check at the freeze point.
493 Set_Check_Address_Alignment
494 (N, not Range_Checks_Suppressed (U_Ent));
496 -- Kill the size check code, since we are not allocating
497 -- the variable, it is somewhere else.
499 Kill_Size_Check_Code (U_Ent);
502 -- Not a valid entity for an address clause
505 Error_Msg_N ("address cannot be given for &", Nam);
513 -- Alignment attribute definition clause
515 when Attribute_Alignment => Alignment_Block : declare
516 Align : constant Uint := Get_Alignment_Value (Expr);
521 if not Is_Type (U_Ent)
522 and then Ekind (U_Ent) /= E_Variable
523 and then Ekind (U_Ent) /= E_Constant
525 Error_Msg_N ("alignment cannot be given for &", Nam);
527 elsif Has_Alignment_Clause (U_Ent) then
528 Error_Msg_Sloc := Sloc (Alignment_Clause (U_Ent));
529 Error_Msg_N ("alignment clause previously given#", N);
531 elsif Align /= No_Uint then
532 Set_Has_Alignment_Clause (U_Ent);
533 Set_Alignment (U_Ent, Align);
541 -- Bit_Order attribute definition clause
543 when Attribute_Bit_Order => Bit_Order : declare
545 if not Is_Record_Type (U_Ent) then
547 ("Bit_Order can only be defined for record type", Nam);
550 Analyze_And_Resolve (Expr, RTE (RE_Bit_Order));
552 if Etype (Expr) = Any_Type then
555 elsif not Is_Static_Expression (Expr) then
557 ("Bit_Order requires static expression!", Expr);
560 if (Expr_Value (Expr) = 0) /= Bytes_Big_Endian then
561 Set_Reverse_Bit_Order (U_Ent, True);
571 -- Component_Size attribute definition clause
573 when Attribute_Component_Size => Component_Size_Case : declare
574 Csize : constant Uint := Static_Integer (Expr);
577 New_Ctyp : Entity_Id;
581 if not Is_Array_Type (U_Ent) then
582 Error_Msg_N ("component size requires array type", Nam);
586 Btype := Base_Type (U_Ent);
588 if Has_Component_Size_Clause (Btype) then
590 ("component size clase for& previously given", Nam);
592 elsif Csize /= No_Uint then
593 Check_Size (Expr, Component_Type (Btype), Csize, Biased);
595 if Has_Aliased_Components (Btype)
601 ("component size incorrect for aliased components", N);
605 -- For the biased case, build a declaration for a subtype
606 -- that will be used to represent the biased subtype that
607 -- reflects the biased representation of components. We need
608 -- this subtype to get proper conversions on referencing
609 -- elements of the array.
613 Make_Defining_Identifier (Loc,
614 Chars => New_External_Name (Chars (U_Ent), 'C', 0, 'T'));
617 Make_Subtype_Declaration (Loc,
618 Defining_Identifier => New_Ctyp,
619 Subtype_Indication =>
620 New_Occurrence_Of (Component_Type (Btype), Loc));
622 Set_Parent (Decl, N);
623 Analyze (Decl, Suppress => All_Checks);
625 Set_Has_Delayed_Freeze (New_Ctyp, False);
626 Set_Esize (New_Ctyp, Csize);
627 Set_RM_Size (New_Ctyp, Csize);
628 Init_Alignment (New_Ctyp);
629 Set_Has_Biased_Representation (New_Ctyp, True);
630 Set_Is_Itype (New_Ctyp, True);
631 Set_Associated_Node_For_Itype (New_Ctyp, U_Ent);
633 Set_Component_Type (Btype, New_Ctyp);
636 Set_Component_Size (Btype, Csize);
637 Set_Has_Component_Size_Clause (Btype, True);
638 Set_Has_Non_Standard_Rep (Btype, True);
640 end Component_Size_Case;
646 when Attribute_External_Tag => External_Tag :
648 if not Is_Tagged_Type (U_Ent) then
649 Error_Msg_N ("should be a tagged type", Nam);
652 Analyze_And_Resolve (Expr, Standard_String);
654 if not Is_Static_Expression (Expr) then
656 ("static string required for tag name!", Nam);
659 Set_Has_External_Tag_Rep_Clause (U_Ent);
666 when Attribute_Input => Input : declare
667 Subp : Entity_Id := Empty;
672 function Has_Good_Profile (Subp : Entity_Id) return Boolean;
673 -- Return true if the entity is a function with an appropriate
674 -- profile for the Input attribute.
676 ----------------------
677 -- Has_Good_Profile --
678 ----------------------
680 function Has_Good_Profile (Subp : Entity_Id) return Boolean is
682 Ok : Boolean := False;
685 if Ekind (Subp) = E_Function then
686 F := First_Formal (Subp);
688 if Present (F) and then No (Next_Formal (F)) then
689 if Ekind (Etype (F)) = E_Anonymous_Access_Type
691 Designated_Type (Etype (F)) =
692 Class_Wide_Type (RTE (RE_Root_Stream_Type))
694 Ok := Base_Type (Etype (Subp)) = Base_Type (Ent);
700 end Has_Good_Profile;
702 -- Start of processing for Input attribute definition
707 if not Is_Type (U_Ent) then
708 Error_Msg_N ("local name must be a subtype", Nam);
712 Pnam := TSS (Base_Type (U_Ent), TSS_Stream_Input);
715 and then Base_Type (Etype (Pnam)) = Base_Type (U_Ent)
717 Error_Msg_Sloc := Sloc (Pnam);
718 Error_Msg_N ("input attribute already defined #", Nam);
725 if Is_Entity_Name (Expr) then
726 if not Is_Overloaded (Expr) then
727 if Has_Good_Profile (Entity (Expr)) then
728 Subp := Entity (Expr);
732 Get_First_Interp (Expr, I, It);
734 while Present (It.Nam) loop
735 if Has_Good_Profile (It.Nam) then
740 Get_Next_Interp (I, It);
745 if Present (Subp) then
746 Set_Entity (Expr, Subp);
747 Set_Etype (Expr, Etype (Subp));
748 New_Stream_Function (N, U_Ent, Subp, TSS_Stream_Input);
750 Error_Msg_N ("incorrect expression for input attribute", Expr);
759 -- Machine radix attribute definition clause
761 when Attribute_Machine_Radix => Machine_Radix : declare
762 Radix : constant Uint := Static_Integer (Expr);
765 if not Is_Decimal_Fixed_Point_Type (U_Ent) then
766 Error_Msg_N ("decimal fixed-point type expected for &", Nam);
768 elsif Has_Machine_Radix_Clause (U_Ent) then
769 Error_Msg_Sloc := Sloc (Alignment_Clause (U_Ent));
770 Error_Msg_N ("machine radix clause previously given#", N);
772 elsif Radix /= No_Uint then
773 Set_Has_Machine_Radix_Clause (U_Ent);
774 Set_Has_Non_Standard_Rep (Base_Type (U_Ent));
778 elsif Radix = 10 then
779 Set_Machine_Radix_10 (U_Ent);
781 Error_Msg_N ("machine radix value must be 2 or 10", Expr);
790 -- Object_Size attribute definition clause
792 when Attribute_Object_Size => Object_Size : declare
793 Size : constant Uint := Static_Integer (Expr);
797 if not Is_Type (U_Ent) then
798 Error_Msg_N ("Object_Size cannot be given for &", Nam);
800 elsif Has_Object_Size_Clause (U_Ent) then
801 Error_Msg_N ("Object_Size already given for &", Nam);
804 Check_Size (Expr, U_Ent, Size, Biased);
812 UI_Mod (Size, 64) /= 0
815 ("Object_Size must be 8, 16, 32, or multiple of 64",
819 Set_Esize (U_Ent, Size);
820 Set_Has_Object_Size_Clause (U_Ent);
821 Alignment_Check_For_Esize_Change (U_Ent);
829 when Attribute_Output => Output : declare
830 Subp : Entity_Id := Empty;
835 function Has_Good_Profile (Subp : Entity_Id) return Boolean;
836 -- Return true if the entity is a procedure with an
837 -- appropriate profile for the output attribute.
839 ----------------------
840 -- Has_Good_Profile --
841 ----------------------
843 function Has_Good_Profile (Subp : Entity_Id) return Boolean is
845 Ok : Boolean := False;
848 if Ekind (Subp) = E_Procedure then
849 F := First_Formal (Subp);
852 if Ekind (Etype (F)) = E_Anonymous_Access_Type
854 Designated_Type (Etype (F)) =
855 Class_Wide_Type (RTE (RE_Root_Stream_Type))
859 and then Parameter_Mode (F) = E_In_Parameter
860 and then Base_Type (Etype (F)) = Base_Type (Ent)
861 and then No (Next_Formal (F));
867 end Has_Good_Profile;
869 -- Start of processing for Output attribute definition
874 if not Is_Type (U_Ent) then
875 Error_Msg_N ("local name must be a subtype", Nam);
879 Pnam := TSS (Base_Type (U_Ent), TSS_Stream_Output);
883 Base_Type (Etype (Next_Formal (First_Formal (Pnam))))
886 Error_Msg_Sloc := Sloc (Pnam);
887 Error_Msg_N ("output attribute already defined #", Nam);
894 if Is_Entity_Name (Expr) then
895 if not Is_Overloaded (Expr) then
896 if Has_Good_Profile (Entity (Expr)) then
897 Subp := Entity (Expr);
901 Get_First_Interp (Expr, I, It);
903 while Present (It.Nam) loop
904 if Has_Good_Profile (It.Nam) then
909 Get_Next_Interp (I, It);
914 if Present (Subp) then
915 Set_Entity (Expr, Subp);
916 Set_Etype (Expr, Etype (Subp));
917 New_Stream_Procedure (N, U_Ent, Subp, TSS_Stream_Output);
919 Error_Msg_N ("incorrect expression for output attribute", Expr);
928 when Attribute_Read => Read : declare
929 Subp : Entity_Id := Empty;
934 function Has_Good_Profile (Subp : Entity_Id) return Boolean;
935 -- Return true if the entity is a procedure with an appropriate
936 -- profile for the Read attribute.
938 ----------------------
939 -- Has_Good_Profile --
940 ----------------------
942 function Has_Good_Profile (Subp : Entity_Id) return Boolean is
944 Ok : Boolean := False;
947 if Ekind (Subp) = E_Procedure then
948 F := First_Formal (Subp);
951 if Ekind (Etype (F)) = E_Anonymous_Access_Type
953 Designated_Type (Etype (F)) =
954 Class_Wide_Type (RTE (RE_Root_Stream_Type))
958 and then Parameter_Mode (F) = E_Out_Parameter
959 and then Base_Type (Etype (F)) = Base_Type (Ent)
960 and then No (Next_Formal (F));
966 end Has_Good_Profile;
968 -- Start of processing for Read attribute definition
973 if not Is_Type (U_Ent) then
974 Error_Msg_N ("local name must be a subtype", Nam);
978 Pnam := TSS (Base_Type (U_Ent), TSS_Stream_Read);
981 and then Base_Type (Etype (Next_Formal (First_Formal (Pnam))))
984 Error_Msg_Sloc := Sloc (Pnam);
985 Error_Msg_N ("read attribute already defined #", Nam);
992 if Is_Entity_Name (Expr) then
993 if not Is_Overloaded (Expr) then
994 if Has_Good_Profile (Entity (Expr)) then
995 Subp := Entity (Expr);
999 Get_First_Interp (Expr, I, It);
1001 while Present (It.Nam) loop
1002 if Has_Good_Profile (It.Nam) then
1007 Get_Next_Interp (I, It);
1012 if Present (Subp) then
1013 Set_Entity (Expr, Subp);
1014 Set_Etype (Expr, Etype (Subp));
1015 New_Stream_Procedure (N, U_Ent, Subp, TSS_Stream_Read, True);
1017 Error_Msg_N ("incorrect expression for read attribute", Expr);
1026 -- Size attribute definition clause
1028 when Attribute_Size => Size : declare
1029 Size : constant Uint := Static_Integer (Expr);
1036 if Has_Size_Clause (U_Ent) then
1037 Error_Msg_N ("size already given for &", Nam);
1039 elsif not Is_Type (U_Ent)
1040 and then Ekind (U_Ent) /= E_Variable
1041 and then Ekind (U_Ent) /= E_Constant
1043 Error_Msg_N ("size cannot be given for &", Nam);
1045 elsif Is_Array_Type (U_Ent)
1046 and then not Is_Constrained (U_Ent)
1049 ("size cannot be given for unconstrained array", Nam);
1051 elsif Size /= No_Uint then
1052 if Is_Type (U_Ent) then
1055 Etyp := Etype (U_Ent);
1058 -- Check size, note that Gigi is in charge of checking
1059 -- that the size of an array or record type is OK. Also
1060 -- we do not check the size in the ordinary fixed-point
1061 -- case, since it is too early to do so (there may be a
1062 -- subsequent small clause that affects the size). We can
1063 -- check the size if a small clause has already been given.
1065 if not Is_Ordinary_Fixed_Point_Type (U_Ent)
1066 or else Has_Small_Clause (U_Ent)
1068 Check_Size (Expr, Etyp, Size, Biased);
1069 Set_Has_Biased_Representation (U_Ent, Biased);
1072 -- For types set RM_Size and Esize if possible
1074 if Is_Type (U_Ent) then
1075 Set_RM_Size (U_Ent, Size);
1077 -- For scalar types, increase Object_Size to power of 2,
1078 -- but not less than a storage unit in any case (i.e.,
1079 -- normally this means it will be byte addressable).
1081 if Is_Scalar_Type (U_Ent) then
1082 if Size <= System_Storage_Unit then
1083 Init_Esize (U_Ent, System_Storage_Unit);
1084 elsif Size <= 16 then
1085 Init_Esize (U_Ent, 16);
1086 elsif Size <= 32 then
1087 Init_Esize (U_Ent, 32);
1089 Set_Esize (U_Ent, (Size + 63) / 64 * 64);
1092 -- For all other types, object size = value size. The
1093 -- backend will adjust as needed.
1096 Set_Esize (U_Ent, Size);
1099 Alignment_Check_For_Esize_Change (U_Ent);
1101 -- For objects, set Esize only
1104 if Is_Elementary_Type (Etyp) then
1105 if Size /= System_Storage_Unit
1107 Size /= System_Storage_Unit * 2
1109 Size /= System_Storage_Unit * 4
1111 Size /= System_Storage_Unit * 8
1114 ("size for primitive object must be power of 2", N);
1118 Set_Esize (U_Ent, Size);
1121 Set_Has_Size_Clause (U_Ent);
1129 -- Small attribute definition clause
1131 when Attribute_Small => Small : declare
1132 Implicit_Base : constant Entity_Id := Base_Type (U_Ent);
1136 Analyze_And_Resolve (Expr, Any_Real);
1138 if Etype (Expr) = Any_Type then
1141 elsif not Is_Static_Expression (Expr) then
1142 Flag_Non_Static_Expr
1143 ("small requires static expression!", Expr);
1147 Small := Expr_Value_R (Expr);
1149 if Small <= Ureal_0 then
1150 Error_Msg_N ("small value must be greater than zero", Expr);
1156 if not Is_Ordinary_Fixed_Point_Type (U_Ent) then
1158 ("small requires an ordinary fixed point type", Nam);
1160 elsif Has_Small_Clause (U_Ent) then
1161 Error_Msg_N ("small already given for &", Nam);
1163 elsif Small > Delta_Value (U_Ent) then
1165 ("small value must not be greater then delta value", Nam);
1168 Set_Small_Value (U_Ent, Small);
1169 Set_Small_Value (Implicit_Base, Small);
1170 Set_Has_Small_Clause (U_Ent);
1171 Set_Has_Small_Clause (Implicit_Base);
1172 Set_Has_Non_Standard_Rep (Implicit_Base);
1180 -- Storage_Size attribute definition clause
1182 when Attribute_Storage_Size => Storage_Size : declare
1183 Btype : constant Entity_Id := Base_Type (U_Ent);
1187 if Is_Task_Type (U_Ent) then
1188 if Warn_On_Obsolescent_Feature then
1190 ("storage size clause for task is an " &
1191 "obsolescent feature ('R'M 'J.9)?", N);
1193 ("|use Storage_Size pragma instead?", N);
1199 if not Is_Access_Type (U_Ent)
1200 and then Ekind (U_Ent) /= E_Task_Type
1202 Error_Msg_N ("storage size cannot be given for &", Nam);
1204 elsif Is_Access_Type (U_Ent) and Is_Derived_Type (U_Ent) then
1206 ("storage size cannot be given for a derived access type",
1209 elsif Has_Storage_Size_Clause (Btype) then
1210 Error_Msg_N ("storage size already given for &", Nam);
1213 Analyze_And_Resolve (Expr, Any_Integer);
1215 if Is_Access_Type (U_Ent) then
1217 if Present (Associated_Storage_Pool (U_Ent)) then
1218 Error_Msg_N ("storage pool already given for &", Nam);
1222 if Compile_Time_Known_Value (Expr)
1223 and then Expr_Value (Expr) = 0
1225 Set_No_Pool_Assigned (Btype);
1228 else -- Is_Task_Type (U_Ent)
1229 Sprag := Get_Rep_Pragma (Btype, Name_Storage_Size);
1231 if Present (Sprag) then
1232 Error_Msg_Sloc := Sloc (Sprag);
1234 ("Storage_Size already specified#", Nam);
1239 Set_Has_Storage_Size_Clause (Btype);
1247 -- Storage_Pool attribute definition clause
1249 when Attribute_Storage_Pool => Storage_Pool : declare
1253 if Ekind (U_Ent) /= E_Access_Type
1254 and then Ekind (U_Ent) /= E_General_Access_Type
1257 "storage pool can only be given for access types", Nam);
1260 elsif Is_Derived_Type (U_Ent) then
1262 ("storage pool cannot be given for a derived access type",
1265 elsif Has_Storage_Size_Clause (U_Ent) then
1266 Error_Msg_N ("storage size already given for &", Nam);
1269 elsif Present (Associated_Storage_Pool (U_Ent)) then
1270 Error_Msg_N ("storage pool already given for &", Nam);
1275 (Expr, Class_Wide_Type (RTE (RE_Root_Storage_Pool)));
1277 -- If the argument is a name that is not an entity name, then
1278 -- we construct a renaming operation to define an entity of
1279 -- type storage pool.
1281 if not Is_Entity_Name (Expr)
1282 and then Is_Object_Reference (Expr)
1285 Make_Defining_Identifier (Loc,
1286 Chars => New_Internal_Name ('P'));
1289 Rnode : constant Node_Id :=
1290 Make_Object_Renaming_Declaration (Loc,
1291 Defining_Identifier => Pool,
1293 New_Occurrence_Of (Etype (Expr), Loc),
1297 Insert_Before (N, Rnode);
1299 Set_Associated_Storage_Pool (U_Ent, Pool);
1302 elsif Is_Entity_Name (Expr) then
1303 Pool := Entity (Expr);
1305 -- If pool is a renamed object, get original one. This can
1306 -- happen with an explicit renaming, and within instances.
1308 while Present (Renamed_Object (Pool))
1309 and then Is_Entity_Name (Renamed_Object (Pool))
1311 Pool := Entity (Renamed_Object (Pool));
1314 if Present (Renamed_Object (Pool))
1315 and then Nkind (Renamed_Object (Pool)) = N_Type_Conversion
1316 and then Is_Entity_Name (Expression (Renamed_Object (Pool)))
1318 Pool := Entity (Expression (Renamed_Object (Pool)));
1321 if Present (Etype (Pool))
1322 and then Etype (Pool) /= RTE (RE_Stack_Bounded_Pool)
1323 and then Etype (Pool) /= RTE (RE_Unbounded_Reclaim_Pool)
1325 Set_Associated_Storage_Pool (U_Ent, Pool);
1327 Error_Msg_N ("Non sharable GNAT Pool", Expr);
1330 -- The pool may be specified as the Storage_Pool of some other
1331 -- type. It is rewritten as a class_wide conversion of the
1332 -- corresponding pool entity.
1334 elsif Nkind (Expr) = N_Type_Conversion
1335 and then Is_Entity_Name (Expression (Expr))
1336 and then Nkind (Original_Node (Expr)) = N_Attribute_Reference
1338 Pool := Entity (Expression (Expr));
1340 if Present (Etype (Pool))
1341 and then Etype (Pool) /= RTE (RE_Stack_Bounded_Pool)
1342 and then Etype (Pool) /= RTE (RE_Unbounded_Reclaim_Pool)
1344 Set_Associated_Storage_Pool (U_Ent, Pool);
1346 Error_Msg_N ("Non sharable GNAT Pool", Expr);
1350 Error_Msg_N ("incorrect reference to a Storage Pool", Expr);
1359 -- Value_Size attribute definition clause
1361 when Attribute_Value_Size => Value_Size : declare
1362 Size : constant Uint := Static_Integer (Expr);
1366 if not Is_Type (U_Ent) then
1367 Error_Msg_N ("Value_Size cannot be given for &", Nam);
1370 (Get_Attribute_Definition_Clause
1371 (U_Ent, Attribute_Value_Size))
1373 Error_Msg_N ("Value_Size already given for &", Nam);
1376 if Is_Elementary_Type (U_Ent) then
1377 Check_Size (Expr, U_Ent, Size, Biased);
1378 Set_Has_Biased_Representation (U_Ent, Biased);
1381 Set_RM_Size (U_Ent, Size);
1389 -- Write attribute definition clause
1390 -- check for class-wide case will be performed later
1392 when Attribute_Write => Write : declare
1393 Subp : Entity_Id := Empty;
1398 function Has_Good_Profile (Subp : Entity_Id) return Boolean;
1399 -- Return true if the entity is a procedure with an
1400 -- appropriate profile for the write attribute.
1402 ----------------------
1403 -- Has_Good_Profile --
1404 ----------------------
1406 function Has_Good_Profile (Subp : Entity_Id) return Boolean is
1408 Ok : Boolean := False;
1411 if Ekind (Subp) = E_Procedure then
1412 F := First_Formal (Subp);
1415 if Ekind (Etype (F)) = E_Anonymous_Access_Type
1417 Designated_Type (Etype (F)) =
1418 Class_Wide_Type (RTE (RE_Root_Stream_Type))
1422 and then Parameter_Mode (F) = E_In_Parameter
1423 and then Base_Type (Etype (F)) = Base_Type (Ent)
1424 and then No (Next_Formal (F));
1430 end Has_Good_Profile;
1432 -- Start of processing for Write attribute definition
1437 if not Is_Type (U_Ent) then
1438 Error_Msg_N ("local name must be a subtype", Nam);
1442 Pnam := TSS (Base_Type (U_Ent), TSS_Stream_Write);
1445 and then Base_Type (Etype (Next_Formal (First_Formal (Pnam))))
1448 Error_Msg_Sloc := Sloc (Pnam);
1449 Error_Msg_N ("write attribute already defined #", Nam);
1455 if Is_Entity_Name (Expr) then
1456 if not Is_Overloaded (Expr) then
1457 if Has_Good_Profile (Entity (Expr)) then
1458 Subp := Entity (Expr);
1462 Get_First_Interp (Expr, I, It);
1464 while Present (It.Nam) loop
1465 if Has_Good_Profile (It.Nam) then
1470 Get_Next_Interp (I, It);
1475 if Present (Subp) then
1476 Set_Entity (Expr, Subp);
1477 Set_Etype (Expr, Etype (Subp));
1478 New_Stream_Procedure (N, U_Ent, Subp, TSS_Stream_Write);
1480 Error_Msg_N ("incorrect expression for write attribute", Expr);
1485 -- All other attributes cannot be set
1489 ("attribute& cannot be set with definition clause", N);
1493 -- The test for the type being frozen must be performed after
1494 -- any expression the clause has been analyzed since the expression
1495 -- itself might cause freezing that makes the clause illegal.
1497 if Rep_Item_Too_Late (U_Ent, N, FOnly) then
1500 end Analyze_Attribute_Definition_Clause;
1502 ----------------------------
1503 -- Analyze_Code_Statement --
1504 ----------------------------
1506 procedure Analyze_Code_Statement (N : Node_Id) is
1507 HSS : constant Node_Id := Parent (N);
1508 SBody : constant Node_Id := Parent (HSS);
1509 Subp : constant Entity_Id := Current_Scope;
1516 -- Analyze and check we get right type, note that this implements the
1517 -- requirement (RM 13.8(1)) that Machine_Code be with'ed, since that
1518 -- is the only way that Asm_Insn could possibly be visible.
1520 Analyze_And_Resolve (Expression (N));
1522 if Etype (Expression (N)) = Any_Type then
1524 elsif Etype (Expression (N)) /= RTE (RE_Asm_Insn) then
1525 Error_Msg_N ("incorrect type for code statement", N);
1529 -- Make sure we appear in the handled statement sequence of a
1530 -- subprogram (RM 13.8(3)).
1532 if Nkind (HSS) /= N_Handled_Sequence_Of_Statements
1533 or else Nkind (SBody) /= N_Subprogram_Body
1536 ("code statement can only appear in body of subprogram", N);
1540 -- Do remaining checks (RM 13.8(3)) if not already done
1542 if not Is_Machine_Code_Subprogram (Subp) then
1543 Set_Is_Machine_Code_Subprogram (Subp);
1545 -- No exception handlers allowed
1547 if Present (Exception_Handlers (HSS)) then
1549 ("exception handlers not permitted in machine code subprogram",
1550 First (Exception_Handlers (HSS)));
1553 -- No declarations other than use clauses and pragmas (we allow
1554 -- certain internally generated declarations as well).
1556 Decl := First (Declarations (SBody));
1557 while Present (Decl) loop
1558 DeclO := Original_Node (Decl);
1559 if Comes_From_Source (DeclO)
1560 and then Nkind (DeclO) /= N_Pragma
1561 and then Nkind (DeclO) /= N_Use_Package_Clause
1562 and then Nkind (DeclO) /= N_Use_Type_Clause
1563 and then Nkind (DeclO) /= N_Implicit_Label_Declaration
1566 ("this declaration not allowed in machine code subprogram",
1573 -- No statements other than code statements, pragmas, and labels.
1574 -- Again we allow certain internally generated statements.
1576 Stmt := First (Statements (HSS));
1577 while Present (Stmt) loop
1578 StmtO := Original_Node (Stmt);
1579 if Comes_From_Source (StmtO)
1580 and then Nkind (StmtO) /= N_Pragma
1581 and then Nkind (StmtO) /= N_Label
1582 and then Nkind (StmtO) /= N_Code_Statement
1585 ("this statement is not allowed in machine code subprogram",
1592 end Analyze_Code_Statement;
1594 -----------------------------------------------
1595 -- Analyze_Enumeration_Representation_Clause --
1596 -----------------------------------------------
1598 procedure Analyze_Enumeration_Representation_Clause (N : Node_Id) is
1599 Ident : constant Node_Id := Identifier (N);
1600 Aggr : constant Node_Id := Array_Aggregate (N);
1601 Enumtype : Entity_Id;
1607 Err : Boolean := False;
1609 Lo : constant Uint := Expr_Value (Type_Low_Bound (Universal_Integer));
1610 Hi : constant Uint := Expr_Value (Type_High_Bound (Universal_Integer));
1615 -- First some basic error checks
1618 Enumtype := Entity (Ident);
1620 if Enumtype = Any_Type
1621 or else Rep_Item_Too_Early (Enumtype, N)
1625 Enumtype := Underlying_Type (Enumtype);
1628 if not Is_Enumeration_Type (Enumtype) then
1630 ("enumeration type required, found}",
1631 Ident, First_Subtype (Enumtype));
1635 -- Ignore rep clause on generic actual type. This will already have
1636 -- been flagged on the template as an error, and this is the safest
1637 -- way to ensure we don't get a junk cascaded message in the instance.
1639 if Is_Generic_Actual_Type (Enumtype) then
1642 -- Type must be in current scope
1644 elsif Scope (Enumtype) /= Current_Scope then
1645 Error_Msg_N ("type must be declared in this scope", Ident);
1648 -- Type must be a first subtype
1650 elsif not Is_First_Subtype (Enumtype) then
1651 Error_Msg_N ("cannot give enumeration rep clause for subtype", N);
1654 -- Ignore duplicate rep clause
1656 elsif Has_Enumeration_Rep_Clause (Enumtype) then
1657 Error_Msg_N ("duplicate enumeration rep clause ignored", N);
1660 -- Don't allow rep clause if root type is standard [wide_]character
1662 elsif Root_Type (Enumtype) = Standard_Character
1663 or else Root_Type (Enumtype) = Standard_Wide_Character
1665 Error_Msg_N ("enumeration rep clause not allowed for this type", N);
1668 -- All tests passed, so set rep clause in place
1671 Set_Has_Enumeration_Rep_Clause (Enumtype);
1672 Set_Has_Enumeration_Rep_Clause (Base_Type (Enumtype));
1675 -- Now we process the aggregate. Note that we don't use the normal
1676 -- aggregate code for this purpose, because we don't want any of the
1677 -- normal expansion activities, and a number of special semantic
1678 -- rules apply (including the component type being any integer type)
1680 -- Badent signals that we found some incorrect entries processing
1681 -- the list. The final checks for completeness and ordering are
1682 -- skipped in this case.
1684 Elit := First_Literal (Enumtype);
1686 -- First the positional entries if any
1688 if Present (Expressions (Aggr)) then
1689 Expr := First (Expressions (Aggr));
1690 while Present (Expr) loop
1692 Error_Msg_N ("too many entries in aggregate", Expr);
1696 Val := Static_Integer (Expr);
1698 if Val = No_Uint then
1701 elsif Val < Lo or else Hi < Val then
1702 Error_Msg_N ("value outside permitted range", Expr);
1706 Set_Enumeration_Rep (Elit, Val);
1707 Set_Enumeration_Rep_Expr (Elit, Expr);
1713 -- Now process the named entries if present
1715 if Present (Component_Associations (Aggr)) then
1716 Assoc := First (Component_Associations (Aggr));
1717 while Present (Assoc) loop
1718 Choice := First (Choices (Assoc));
1720 if Present (Next (Choice)) then
1722 ("multiple choice not allowed here", Next (Choice));
1726 if Nkind (Choice) = N_Others_Choice then
1727 Error_Msg_N ("others choice not allowed here", Choice);
1730 elsif Nkind (Choice) = N_Range then
1731 -- ??? should allow zero/one element range here
1732 Error_Msg_N ("range not allowed here", Choice);
1736 Analyze_And_Resolve (Choice, Enumtype);
1738 if Is_Entity_Name (Choice)
1739 and then Is_Type (Entity (Choice))
1741 Error_Msg_N ("subtype name not allowed here", Choice);
1743 -- ??? should allow static subtype with zero/one entry
1745 elsif Etype (Choice) = Base_Type (Enumtype) then
1746 if not Is_Static_Expression (Choice) then
1747 Flag_Non_Static_Expr
1748 ("non-static expression used for choice!", Choice);
1752 Elit := Expr_Value_E (Choice);
1754 if Present (Enumeration_Rep_Expr (Elit)) then
1755 Error_Msg_Sloc := Sloc (Enumeration_Rep_Expr (Elit));
1757 ("representation for& previously given#",
1762 Set_Enumeration_Rep_Expr (Elit, Choice);
1764 Expr := Expression (Assoc);
1765 Val := Static_Integer (Expr);
1767 if Val = No_Uint then
1770 elsif Val < Lo or else Hi < Val then
1771 Error_Msg_N ("value outside permitted range", Expr);
1775 Set_Enumeration_Rep (Elit, Val);
1784 -- Aggregate is fully processed. Now we check that a full set of
1785 -- representations was given, and that they are in range and in order.
1786 -- These checks are only done if no other errors occurred.
1792 Elit := First_Literal (Enumtype);
1793 while Present (Elit) loop
1794 if No (Enumeration_Rep_Expr (Elit)) then
1795 Error_Msg_NE ("missing representation for&!", N, Elit);
1798 Val := Enumeration_Rep (Elit);
1800 if Min = No_Uint then
1804 if Val /= No_Uint then
1805 if Max /= No_Uint and then Val <= Max then
1807 ("enumeration value for& not ordered!",
1808 Enumeration_Rep_Expr (Elit), Elit);
1814 -- If there is at least one literal whose representation
1815 -- is not equal to the Pos value, then note that this
1816 -- enumeration type has a non-standard representation.
1818 if Val /= Enumeration_Pos (Elit) then
1819 Set_Has_Non_Standard_Rep (Base_Type (Enumtype));
1826 -- Now set proper size information
1829 Minsize : Uint := UI_From_Int (Minimum_Size (Enumtype));
1832 if Has_Size_Clause (Enumtype) then
1833 if Esize (Enumtype) >= Minsize then
1838 UI_From_Int (Minimum_Size (Enumtype, Biased => True));
1840 if Esize (Enumtype) < Minsize then
1841 Error_Msg_N ("previously given size is too small", N);
1844 Set_Has_Biased_Representation (Enumtype);
1849 Set_RM_Size (Enumtype, Minsize);
1850 Set_Enum_Esize (Enumtype);
1853 Set_RM_Size (Base_Type (Enumtype), RM_Size (Enumtype));
1854 Set_Esize (Base_Type (Enumtype), Esize (Enumtype));
1855 Set_Alignment (Base_Type (Enumtype), Alignment (Enumtype));
1859 -- We repeat the too late test in case it froze itself!
1861 if Rep_Item_Too_Late (Enumtype, N) then
1864 end Analyze_Enumeration_Representation_Clause;
1866 ----------------------------
1867 -- Analyze_Free_Statement --
1868 ----------------------------
1870 procedure Analyze_Free_Statement (N : Node_Id) is
1872 Analyze (Expression (N));
1873 end Analyze_Free_Statement;
1875 ------------------------------------------
1876 -- Analyze_Record_Representation_Clause --
1877 ------------------------------------------
1879 procedure Analyze_Record_Representation_Clause (N : Node_Id) is
1880 Loc : constant Source_Ptr := Sloc (N);
1881 Ident : constant Node_Id := Identifier (N);
1882 Rectype : Entity_Id;
1888 Hbit : Uint := Uint_0;
1893 Max_Bit_So_Far : Uint;
1894 -- Records the maximum bit position so far. If all field positions
1895 -- are monotonically increasing, then we can skip the circuit for
1896 -- checking for overlap, since no overlap is possible.
1898 Overlap_Check_Required : Boolean;
1899 -- Used to keep track of whether or not an overlap check is required
1901 Ccount : Natural := 0;
1902 -- Number of component clauses in record rep clause
1906 Rectype := Entity (Ident);
1908 if Rectype = Any_Type
1909 or else Rep_Item_Too_Early (Rectype, N)
1913 Rectype := Underlying_Type (Rectype);
1916 -- First some basic error checks
1918 if not Is_Record_Type (Rectype) then
1920 ("record type required, found}", Ident, First_Subtype (Rectype));
1923 elsif Is_Unchecked_Union (Rectype) then
1925 ("record rep clause not allowed for Unchecked_Union", N);
1927 elsif Scope (Rectype) /= Current_Scope then
1928 Error_Msg_N ("type must be declared in this scope", N);
1931 elsif not Is_First_Subtype (Rectype) then
1932 Error_Msg_N ("cannot give record rep clause for subtype", N);
1935 elsif Has_Record_Rep_Clause (Rectype) then
1936 Error_Msg_N ("duplicate record rep clause ignored", N);
1939 elsif Rep_Item_Too_Late (Rectype, N) then
1943 if Present (Mod_Clause (N)) then
1945 Loc : constant Source_Ptr := Sloc (N);
1946 M : constant Node_Id := Mod_Clause (N);
1947 P : constant List_Id := Pragmas_Before (M);
1951 pragma Warnings (Off, Mod_Val);
1954 if Warn_On_Obsolescent_Feature then
1956 ("mod clause is an obsolescent feature ('R'M 'J.8)?", N);
1958 ("|use alignment attribute definition clause instead?", N);
1965 -- In ASIS_Mode mode, expansion is disabled, but we must
1966 -- convert the Mod clause into an alignment clause anyway, so
1967 -- that the back-end can compute and back-annotate properly the
1968 -- size and alignment of types that may include this record.
1970 if Operating_Mode = Check_Semantics
1974 Make_Attribute_Definition_Clause (Loc,
1975 Name => New_Reference_To (Base_Type (Rectype), Loc),
1976 Chars => Name_Alignment,
1977 Expression => Relocate_Node (Expression (M)));
1979 Set_From_At_Mod (AtM_Nod);
1980 Insert_After (N, AtM_Nod);
1981 Mod_Val := Get_Alignment_Value (Expression (AtM_Nod));
1982 Set_Mod_Clause (N, Empty);
1985 -- Get the alignment value to perform error checking
1987 Mod_Val := Get_Alignment_Value (Expression (M));
1993 -- Clear any existing component clauses for the type (this happens
1994 -- with derived types, where we are now overriding the original)
1996 Fent := First_Entity (Rectype);
1999 while Present (Comp) loop
2000 if Ekind (Comp) = E_Component
2001 or else Ekind (Comp) = E_Discriminant
2003 Set_Component_Clause (Comp, Empty);
2009 -- All done if no component clauses
2011 CC := First (Component_Clauses (N));
2017 -- If a tag is present, then create a component clause that places
2018 -- it at the start of the record (otherwise gigi may place it after
2019 -- other fields that have rep clauses).
2021 if Nkind (Fent) = N_Defining_Identifier
2022 and then Chars (Fent) = Name_uTag
2024 Set_Component_Bit_Offset (Fent, Uint_0);
2025 Set_Normalized_Position (Fent, Uint_0);
2026 Set_Normalized_First_Bit (Fent, Uint_0);
2027 Set_Normalized_Position_Max (Fent, Uint_0);
2028 Init_Esize (Fent, System_Address_Size);
2030 Set_Component_Clause (Fent,
2031 Make_Component_Clause (Loc,
2033 Make_Identifier (Loc,
2034 Chars => Name_uTag),
2037 Make_Integer_Literal (Loc,
2041 Make_Integer_Literal (Loc,
2045 Make_Integer_Literal (Loc,
2046 UI_From_Int (System_Address_Size))));
2048 Ccount := Ccount + 1;
2051 -- A representation like this applies to the base type
2053 Set_Has_Record_Rep_Clause (Base_Type (Rectype));
2054 Set_Has_Non_Standard_Rep (Base_Type (Rectype));
2055 Set_Has_Specified_Layout (Base_Type (Rectype));
2057 Max_Bit_So_Far := Uint_Minus_1;
2058 Overlap_Check_Required := False;
2060 -- Process the component clauses
2062 while Present (CC) loop
2064 -- If pragma, just analyze it
2066 if Nkind (CC) = N_Pragma then
2069 -- Processing for real component clause
2072 Ccount := Ccount + 1;
2073 Posit := Static_Integer (Position (CC));
2074 Fbit := Static_Integer (First_Bit (CC));
2075 Lbit := Static_Integer (Last_Bit (CC));
2078 and then Fbit /= No_Uint
2079 and then Lbit /= No_Uint
2083 ("position cannot be negative", Position (CC));
2087 ("first bit cannot be negative", First_Bit (CC));
2089 -- Values look OK, so find the corresponding record component
2090 -- Even though the syntax allows an attribute reference for
2091 -- implementation-defined components, GNAT does not allow the
2092 -- tag to get an explicit position.
2094 elsif Nkind (Component_Name (CC)) = N_Attribute_Reference then
2096 if Attribute_Name (Component_Name (CC)) = Name_Tag then
2097 Error_Msg_N ("position of tag cannot be specified", CC);
2099 Error_Msg_N ("illegal component name", CC);
2103 Comp := First_Entity (Rectype);
2104 while Present (Comp) loop
2105 exit when Chars (Comp) = Chars (Component_Name (CC));
2111 -- Maybe component of base type that is absent from
2112 -- statically constrained first subtype.
2114 Comp := First_Entity (Base_Type (Rectype));
2115 while Present (Comp) loop
2116 exit when Chars (Comp) = Chars (Component_Name (CC));
2123 ("component clause is for non-existent field", CC);
2125 elsif Present (Component_Clause (Comp)) then
2126 Error_Msg_Sloc := Sloc (Component_Clause (Comp));
2128 ("component clause previously given#", CC);
2131 -- Update Fbit and Lbit to the actual bit number.
2133 Fbit := Fbit + UI_From_Int (SSU) * Posit;
2134 Lbit := Lbit + UI_From_Int (SSU) * Posit;
2136 if Fbit <= Max_Bit_So_Far then
2137 Overlap_Check_Required := True;
2139 Max_Bit_So_Far := Lbit;
2142 if Has_Size_Clause (Rectype)
2143 and then Esize (Rectype) <= Lbit
2146 ("bit number out of range of specified size",
2149 Set_Component_Clause (Comp, CC);
2150 Set_Component_Bit_Offset (Comp, Fbit);
2151 Set_Esize (Comp, 1 + (Lbit - Fbit));
2152 Set_Normalized_First_Bit (Comp, Fbit mod SSU);
2153 Set_Normalized_Position (Comp, Fbit / SSU);
2155 Set_Normalized_Position_Max
2156 (Fent, Normalized_Position (Fent));
2158 if Is_Tagged_Type (Rectype)
2159 and then Fbit < System_Address_Size
2162 ("component overlaps tag field of&",
2166 -- This information is also set in the corresponding
2167 -- component of the base type, found by accessing the
2168 -- Original_Record_Component link if it is present.
2170 Ocomp := Original_Record_Component (Comp);
2177 (Component_Name (CC),
2182 Set_Has_Biased_Representation (Comp, Biased);
2184 if Present (Ocomp) then
2185 Set_Component_Clause (Ocomp, CC);
2186 Set_Component_Bit_Offset (Ocomp, Fbit);
2187 Set_Normalized_First_Bit (Ocomp, Fbit mod SSU);
2188 Set_Normalized_Position (Ocomp, Fbit / SSU);
2189 Set_Esize (Ocomp, 1 + (Lbit - Fbit));
2191 Set_Normalized_Position_Max
2192 (Ocomp, Normalized_Position (Ocomp));
2194 Set_Has_Biased_Representation
2195 (Ocomp, Has_Biased_Representation (Comp));
2198 if Esize (Comp) < 0 then
2199 Error_Msg_N ("component size is negative", CC);
2210 -- Now that we have processed all the component clauses, check for
2211 -- overlap. We have to leave this till last, since the components
2212 -- can appear in any arbitrary order in the representation clause.
2214 -- We do not need this check if all specified ranges were monotonic,
2215 -- as recorded by Overlap_Check_Required being False at this stage.
2217 -- This first section checks if there are any overlapping entries
2218 -- at all. It does this by sorting all entries and then seeing if
2219 -- there are any overlaps. If there are none, then that is decisive,
2220 -- but if there are overlaps, they may still be OK (they may result
2221 -- from fields in different variants).
2223 if Overlap_Check_Required then
2224 Overlap_Check1 : declare
2226 OC_Fbit : array (0 .. Ccount) of Uint;
2227 -- First-bit values for component clauses, the value is the
2228 -- offset of the first bit of the field from start of record.
2229 -- The zero entry is for use in sorting.
2231 OC_Lbit : array (0 .. Ccount) of Uint;
2232 -- Last-bit values for component clauses, the value is the
2233 -- offset of the last bit of the field from start of record.
2234 -- The zero entry is for use in sorting.
2236 OC_Count : Natural := 0;
2237 -- Count of entries in OC_Fbit and OC_Lbit
2239 function OC_Lt (Op1, Op2 : Natural) return Boolean;
2240 -- Compare routine for Sort (See GNAT.Heap_Sort_A)
2242 procedure OC_Move (From : Natural; To : Natural);
2243 -- Move routine for Sort (see GNAT.Heap_Sort_A)
2245 function OC_Lt (Op1, Op2 : Natural) return Boolean is
2247 return OC_Fbit (Op1) < OC_Fbit (Op2);
2250 procedure OC_Move (From : Natural; To : Natural) is
2252 OC_Fbit (To) := OC_Fbit (From);
2253 OC_Lbit (To) := OC_Lbit (From);
2257 CC := First (Component_Clauses (N));
2258 while Present (CC) loop
2259 if Nkind (CC) /= N_Pragma then
2260 Posit := Static_Integer (Position (CC));
2261 Fbit := Static_Integer (First_Bit (CC));
2262 Lbit := Static_Integer (Last_Bit (CC));
2265 and then Fbit /= No_Uint
2266 and then Lbit /= No_Uint
2268 OC_Count := OC_Count + 1;
2269 Posit := Posit * SSU;
2270 OC_Fbit (OC_Count) := Fbit + Posit;
2271 OC_Lbit (OC_Count) := Lbit + Posit;
2280 OC_Move'Unrestricted_Access,
2281 OC_Lt'Unrestricted_Access);
2283 Overlap_Check_Required := False;
2284 for J in 1 .. OC_Count - 1 loop
2285 if OC_Lbit (J) >= OC_Fbit (J + 1) then
2286 Overlap_Check_Required := True;
2293 -- If Overlap_Check_Required is still True, then we have to do
2294 -- the full scale overlap check, since we have at least two fields
2295 -- that do overlap, and we need to know if that is OK since they
2296 -- are in the same variant, or whether we have a definite problem
2298 if Overlap_Check_Required then
2299 Overlap_Check2 : declare
2300 C1_Ent, C2_Ent : Entity_Id;
2301 -- Entities of components being checked for overlap
2304 -- Component_List node whose Component_Items are being checked
2307 -- Component declaration for component being checked
2310 C1_Ent := First_Entity (Base_Type (Rectype));
2312 -- Loop through all components in record. For each component check
2313 -- for overlap with any of the preceding elements on the component
2314 -- list containing the component, and also, if the component is in
2315 -- a variant, check against components outside the case structure.
2316 -- This latter test is repeated recursively up the variant tree.
2318 Main_Component_Loop : while Present (C1_Ent) loop
2319 if Ekind (C1_Ent) /= E_Component
2320 and then Ekind (C1_Ent) /= E_Discriminant
2322 goto Continue_Main_Component_Loop;
2325 -- Skip overlap check if entity has no declaration node. This
2326 -- happens with discriminants in constrained derived types.
2327 -- Probably we are missing some checks as a result, but that
2328 -- does not seem terribly serious ???
2330 if No (Declaration_Node (C1_Ent)) then
2331 goto Continue_Main_Component_Loop;
2334 Clist := Parent (List_Containing (Declaration_Node (C1_Ent)));
2336 -- Loop through component lists that need checking. Check the
2337 -- current component list and all lists in variants above us.
2339 Component_List_Loop : loop
2341 -- If derived type definition, go to full declaration
2342 -- If at outer level, check discriminants if there are any
2344 if Nkind (Clist) = N_Derived_Type_Definition then
2345 Clist := Parent (Clist);
2348 -- Outer level of record definition, check discriminants
2350 if Nkind (Clist) = N_Full_Type_Declaration
2351 or else Nkind (Clist) = N_Private_Type_Declaration
2353 if Has_Discriminants (Defining_Identifier (Clist)) then
2355 First_Discriminant (Defining_Identifier (Clist));
2357 while Present (C2_Ent) loop
2358 exit when C1_Ent = C2_Ent;
2359 Check_Component_Overlap (C1_Ent, C2_Ent);
2360 Next_Discriminant (C2_Ent);
2364 -- Record extension case
2366 elsif Nkind (Clist) = N_Derived_Type_Definition then
2369 -- Otherwise check one component list
2372 Citem := First (Component_Items (Clist));
2374 while Present (Citem) loop
2375 if Nkind (Citem) = N_Component_Declaration then
2376 C2_Ent := Defining_Identifier (Citem);
2377 exit when C1_Ent = C2_Ent;
2378 Check_Component_Overlap (C1_Ent, C2_Ent);
2385 -- Check for variants above us (the parent of the Clist can
2386 -- be a variant, in which case its parent is a variant part,
2387 -- and the parent of the variant part is a component list
2388 -- whose components must all be checked against the current
2389 -- component for overlap.
2391 if Nkind (Parent (Clist)) = N_Variant then
2392 Clist := Parent (Parent (Parent (Clist)));
2394 -- Check for possible discriminant part in record, this is
2395 -- treated essentially as another level in the recursion.
2396 -- For this case we have the parent of the component list
2397 -- is the record definition, and its parent is the full
2398 -- type declaration which contains the discriminant
2401 elsif Nkind (Parent (Clist)) = N_Record_Definition then
2402 Clist := Parent (Parent ((Clist)));
2404 -- If neither of these two cases, we are at the top of
2408 exit Component_List_Loop;
2410 end loop Component_List_Loop;
2412 <<Continue_Main_Component_Loop>>
2413 Next_Entity (C1_Ent);
2415 end loop Main_Component_Loop;
2419 -- For records that have component clauses for all components, and
2420 -- whose size is less than or equal to 32, we need to know the size
2421 -- in the front end to activate possible packed array processing
2422 -- where the component type is a record.
2424 -- At this stage Hbit + 1 represents the first unused bit from all
2425 -- the component clauses processed, so if the component clauses are
2426 -- complete, then this is the length of the record.
2428 -- For records longer than System.Storage_Unit, and for those where
2429 -- not all components have component clauses, the back end determines
2430 -- the length (it may for example be appopriate to round up the size
2431 -- to some convenient boundary, based on alignment considerations etc).
2433 if Unknown_RM_Size (Rectype)
2434 and then Hbit + 1 <= 32
2436 -- Nothing to do if at least one component with no component clause
2438 Comp := First_Entity (Rectype);
2439 while Present (Comp) loop
2440 if Ekind (Comp) = E_Component
2441 or else Ekind (Comp) = E_Discriminant
2443 if No (Component_Clause (Comp)) then
2451 -- If we fall out of loop, all components have component clauses
2452 -- and so we can set the size to the maximum value.
2454 Set_RM_Size (Rectype, Hbit + 1);
2456 end Analyze_Record_Representation_Clause;
2458 -----------------------------
2459 -- Check_Component_Overlap --
2460 -----------------------------
2462 procedure Check_Component_Overlap (C1_Ent, C2_Ent : Entity_Id) is
2464 if Present (Component_Clause (C1_Ent))
2465 and then Present (Component_Clause (C2_Ent))
2467 -- Exclude odd case where we have two tag fields in the same
2468 -- record, both at location zero. This seems a bit strange,
2469 -- but it seems to happen in some circumstances ???
2471 if Chars (C1_Ent) = Name_uTag
2472 and then Chars (C2_Ent) = Name_uTag
2477 -- Here we check if the two fields overlap
2480 S1 : constant Uint := Component_Bit_Offset (C1_Ent);
2481 S2 : constant Uint := Component_Bit_Offset (C2_Ent);
2482 E1 : constant Uint := S1 + Esize (C1_Ent);
2483 E2 : constant Uint := S2 + Esize (C2_Ent);
2486 if E2 <= S1 or else E1 <= S2 then
2490 Component_Name (Component_Clause (C2_Ent));
2491 Error_Msg_Sloc := Sloc (Error_Msg_Node_2);
2493 Component_Name (Component_Clause (C1_Ent));
2495 ("component& overlaps & #",
2496 Component_Name (Component_Clause (C1_Ent)));
2500 end Check_Component_Overlap;
2502 -----------------------------------
2503 -- Check_Constant_Address_Clause --
2504 -----------------------------------
2506 procedure Check_Constant_Address_Clause
2510 procedure Check_At_Constant_Address (Nod : Node_Id);
2511 -- Checks that the given node N represents a name whose 'Address
2512 -- is constant (in the same sense as OK_Constant_Address_Clause,
2513 -- i.e. the address value is the same at the point of declaration
2514 -- of U_Ent and at the time of elaboration of the address clause.
2516 procedure Check_Expr_Constants (Nod : Node_Id);
2517 -- Checks that Nod meets the requirements for a constant address
2518 -- clause in the sense of the enclosing procedure.
2520 procedure Check_List_Constants (Lst : List_Id);
2521 -- Check that all elements of list Lst meet the requirements for a
2522 -- constant address clause in the sense of the enclosing procedure.
2524 -------------------------------
2525 -- Check_At_Constant_Address --
2526 -------------------------------
2528 procedure Check_At_Constant_Address (Nod : Node_Id) is
2530 if Is_Entity_Name (Nod) then
2531 if Present (Address_Clause (Entity ((Nod)))) then
2533 ("invalid address clause for initialized object &!",
2536 ("address for& cannot" &
2537 " depend on another address clause! ('R'M 13.1(22))!",
2540 elsif In_Same_Source_Unit (Entity (Nod), U_Ent)
2541 and then Sloc (U_Ent) < Sloc (Entity (Nod))
2544 ("invalid address clause for initialized object &!",
2546 Error_Msg_Name_1 := Chars (Entity (Nod));
2547 Error_Msg_Name_2 := Chars (U_Ent);
2549 ("\% must be defined before % ('R'M 13.1(22))!",
2553 elsif Nkind (Nod) = N_Selected_Component then
2555 T : constant Entity_Id := Etype (Prefix (Nod));
2558 if (Is_Record_Type (T)
2559 and then Has_Discriminants (T))
2562 and then Is_Record_Type (Designated_Type (T))
2563 and then Has_Discriminants (Designated_Type (T)))
2566 ("invalid address clause for initialized object &!",
2569 ("\address cannot depend on component" &
2570 " of discriminated record ('R'M 13.1(22))!",
2573 Check_At_Constant_Address (Prefix (Nod));
2577 elsif Nkind (Nod) = N_Indexed_Component then
2578 Check_At_Constant_Address (Prefix (Nod));
2579 Check_List_Constants (Expressions (Nod));
2582 Check_Expr_Constants (Nod);
2584 end Check_At_Constant_Address;
2586 --------------------------
2587 -- Check_Expr_Constants --
2588 --------------------------
2590 procedure Check_Expr_Constants (Nod : Node_Id) is
2591 Loc_U_Ent : constant Source_Ptr := Sloc (U_Ent);
2592 Ent : Entity_Id := Empty;
2595 if Nkind (Nod) in N_Has_Etype
2596 and then Etype (Nod) = Any_Type
2602 when N_Empty | N_Error =>
2605 when N_Identifier | N_Expanded_Name =>
2606 Ent := Entity (Nod);
2608 -- We need to look at the original node if it is different
2609 -- from the node, since we may have rewritten things and
2610 -- substituted an identifier representing the rewrite.
2612 if Original_Node (Nod) /= Nod then
2613 Check_Expr_Constants (Original_Node (Nod));
2615 -- If the node is an object declaration without initial
2616 -- value, some code has been expanded, and the expression
2617 -- is not constant, even if the constituents might be
2618 -- acceptable, as in A'Address + offset.
2620 if Ekind (Ent) = E_Variable
2621 and then Nkind (Declaration_Node (Ent))
2622 = N_Object_Declaration
2624 No (Expression (Declaration_Node (Ent)))
2627 ("invalid address clause for initialized object &!",
2630 -- If entity is constant, it may be the result of expanding
2631 -- a check. We must verify that its declaration appears
2632 -- before the object in question, else we also reject the
2635 elsif Ekind (Ent) = E_Constant
2636 and then In_Same_Source_Unit (Ent, U_Ent)
2637 and then Sloc (Ent) > Loc_U_Ent
2640 ("invalid address clause for initialized object &!",
2647 -- Otherwise look at the identifier and see if it is OK.
2649 if Ekind (Ent) = E_Named_Integer
2651 Ekind (Ent) = E_Named_Real
2658 Ekind (Ent) = E_Constant
2660 Ekind (Ent) = E_In_Parameter
2662 -- This is the case where we must have Ent defined
2663 -- before U_Ent. Clearly if they are in different
2664 -- units this requirement is met since the unit
2665 -- containing Ent is already processed.
2667 if not In_Same_Source_Unit (Ent, U_Ent) then
2670 -- Otherwise location of Ent must be before the
2671 -- location of U_Ent, that's what prior defined means.
2673 elsif Sloc (Ent) < Loc_U_Ent then
2678 ("invalid address clause for initialized object &!",
2680 Error_Msg_Name_1 := Chars (Ent);
2681 Error_Msg_Name_2 := Chars (U_Ent);
2683 ("\% must be defined before % ('R'M 13.1(22))!",
2687 elsif Nkind (Original_Node (Nod)) = N_Function_Call then
2688 Check_Expr_Constants (Original_Node (Nod));
2692 ("invalid address clause for initialized object &!",
2695 if Comes_From_Source (Ent) then
2696 Error_Msg_Name_1 := Chars (Ent);
2698 ("\reference to variable% not allowed"
2699 & " ('R'M 13.1(22))!", Nod);
2702 ("non-static expression not allowed"
2703 & " ('R'M 13.1(22))!", Nod);
2707 when N_Integer_Literal =>
2709 -- If this is a rewritten unchecked conversion, in a system
2710 -- where Address is an integer type, always use the base type
2711 -- for a literal value. This is user-friendly and prevents
2712 -- order-of-elaboration issues with instances of unchecked
2715 if Nkind (Original_Node (Nod)) = N_Function_Call then
2716 Set_Etype (Nod, Base_Type (Etype (Nod)));
2719 when N_Real_Literal |
2721 N_Character_Literal =>
2725 Check_Expr_Constants (Low_Bound (Nod));
2726 Check_Expr_Constants (High_Bound (Nod));
2728 when N_Explicit_Dereference =>
2729 Check_Expr_Constants (Prefix (Nod));
2731 when N_Indexed_Component =>
2732 Check_Expr_Constants (Prefix (Nod));
2733 Check_List_Constants (Expressions (Nod));
2736 Check_Expr_Constants (Prefix (Nod));
2737 Check_Expr_Constants (Discrete_Range (Nod));
2739 when N_Selected_Component =>
2740 Check_Expr_Constants (Prefix (Nod));
2742 when N_Attribute_Reference =>
2744 if Attribute_Name (Nod) = Name_Address
2746 Attribute_Name (Nod) = Name_Access
2748 Attribute_Name (Nod) = Name_Unchecked_Access
2750 Attribute_Name (Nod) = Name_Unrestricted_Access
2752 Check_At_Constant_Address (Prefix (Nod));
2755 Check_Expr_Constants (Prefix (Nod));
2756 Check_List_Constants (Expressions (Nod));
2760 Check_List_Constants (Component_Associations (Nod));
2761 Check_List_Constants (Expressions (Nod));
2763 when N_Component_Association =>
2764 Check_Expr_Constants (Expression (Nod));
2766 when N_Extension_Aggregate =>
2767 Check_Expr_Constants (Ancestor_Part (Nod));
2768 Check_List_Constants (Component_Associations (Nod));
2769 Check_List_Constants (Expressions (Nod));
2774 when N_Binary_Op | N_And_Then | N_Or_Else | N_In | N_Not_In =>
2775 Check_Expr_Constants (Left_Opnd (Nod));
2776 Check_Expr_Constants (Right_Opnd (Nod));
2779 Check_Expr_Constants (Right_Opnd (Nod));
2781 when N_Type_Conversion |
2782 N_Qualified_Expression |
2784 Check_Expr_Constants (Expression (Nod));
2786 when N_Unchecked_Type_Conversion =>
2787 Check_Expr_Constants (Expression (Nod));
2789 -- If this is a rewritten unchecked conversion, subtypes
2790 -- in this node are those created within the instance.
2791 -- To avoid order of elaboration issues, replace them
2792 -- with their base types. Note that address clauses can
2793 -- cause order of elaboration problems because they are
2794 -- elaborated by the back-end at the point of definition,
2795 -- and may mention entities declared in between (as long
2796 -- as everything is static). It is user-friendly to allow
2797 -- unchecked conversions in this context.
2799 if Nkind (Original_Node (Nod)) = N_Function_Call then
2800 Set_Etype (Expression (Nod),
2801 Base_Type (Etype (Expression (Nod))));
2802 Set_Etype (Nod, Base_Type (Etype (Nod)));
2805 when N_Function_Call =>
2806 if not Is_Pure (Entity (Name (Nod))) then
2808 ("invalid address clause for initialized object &!",
2812 ("\function & is not pure ('R'M 13.1(22))!",
2813 Nod, Entity (Name (Nod)));
2816 Check_List_Constants (Parameter_Associations (Nod));
2819 when N_Parameter_Association =>
2820 Check_Expr_Constants (Explicit_Actual_Parameter (Nod));
2824 ("invalid address clause for initialized object &!",
2827 ("\must be constant defined before& ('R'M 13.1(22))!",
2830 end Check_Expr_Constants;
2832 --------------------------
2833 -- Check_List_Constants --
2834 --------------------------
2836 procedure Check_List_Constants (Lst : List_Id) is
2840 if Present (Lst) then
2841 Nod1 := First (Lst);
2842 while Present (Nod1) loop
2843 Check_Expr_Constants (Nod1);
2847 end Check_List_Constants;
2849 -- Start of processing for Check_Constant_Address_Clause
2852 Check_Expr_Constants (Expr);
2853 end Check_Constant_Address_Clause;
2859 procedure Check_Size
2863 Biased : out Boolean)
2865 UT : constant Entity_Id := Underlying_Type (T);
2871 -- Dismiss cases for generic types or types with previous errors
2874 or else UT = Any_Type
2875 or else Is_Generic_Type (UT)
2876 or else Is_Generic_Type (Root_Type (UT))
2880 -- Check case of bit packed array
2882 elsif Is_Array_Type (UT)
2883 and then Known_Static_Component_Size (UT)
2884 and then Is_Bit_Packed_Array (UT)
2892 Asiz := Component_Size (UT);
2893 Indx := First_Index (UT);
2895 Ityp := Etype (Indx);
2897 -- If non-static bound, then we are not in the business of
2898 -- trying to check the length, and indeed an error will be
2899 -- issued elsewhere, since sizes of non-static array types
2900 -- cannot be set implicitly or explicitly.
2902 if not Is_Static_Subtype (Ityp) then
2906 -- Otherwise accumulate next dimension
2908 Asiz := Asiz * (Expr_Value (Type_High_Bound (Ityp)) -
2909 Expr_Value (Type_Low_Bound (Ityp)) +
2913 exit when No (Indx);
2919 Error_Msg_Uint_1 := Asiz;
2921 ("size for& too small, minimum allowed is ^", N, T);
2922 Set_Esize (T, Asiz);
2923 Set_RM_Size (T, Asiz);
2927 -- All other composite types are ignored
2929 elsif Is_Composite_Type (UT) then
2932 -- For fixed-point types, don't check minimum if type is not frozen,
2933 -- since we don't know all the characteristics of the type that can
2934 -- affect the size (e.g. a specified small) till freeze time.
2936 elsif Is_Fixed_Point_Type (UT)
2937 and then not Is_Frozen (UT)
2941 -- Cases for which a minimum check is required
2944 -- Ignore if specified size is correct for the type
2946 if Known_Esize (UT) and then Siz = Esize (UT) then
2950 -- Otherwise get minimum size
2952 M := UI_From_Int (Minimum_Size (UT));
2956 -- Size is less than minimum size, but one possibility remains
2957 -- that we can manage with the new size if we bias the type
2959 M := UI_From_Int (Minimum_Size (UT, Biased => True));
2962 Error_Msg_Uint_1 := M;
2964 ("size for& too small, minimum allowed is ^", N, T);
2974 -------------------------
2975 -- Get_Alignment_Value --
2976 -------------------------
2978 function Get_Alignment_Value (Expr : Node_Id) return Uint is
2979 Align : constant Uint := Static_Integer (Expr);
2982 if Align = No_Uint then
2985 elsif Align <= 0 then
2986 Error_Msg_N ("alignment value must be positive", Expr);
2990 for J in Int range 0 .. 64 loop
2992 M : constant Uint := Uint_2 ** J;
2995 exit when M = Align;
2999 ("alignment value must be power of 2", Expr);
3007 end Get_Alignment_Value;
3013 procedure Initialize is
3015 Unchecked_Conversions.Init;
3018 -------------------------
3019 -- Is_Operational_Item --
3020 -------------------------
3022 function Is_Operational_Item (N : Node_Id) return Boolean is
3024 if Nkind (N) /= N_Attribute_Definition_Clause then
3028 Id : constant Attribute_Id := Get_Attribute_Id (Chars (N));
3031 return Id = Attribute_Input
3032 or else Id = Attribute_Output
3033 or else Id = Attribute_Read
3034 or else Id = Attribute_Write
3035 or else Id = Attribute_External_Tag;
3038 end Is_Operational_Item;
3040 --------------------------------------
3041 -- Mark_Aliased_Address_As_Volatile --
3042 --------------------------------------
3044 procedure Mark_Aliased_Address_As_Volatile (N : Node_Id) is
3045 Ent : constant Entity_Id := Address_Aliased_Entity (N);
3048 if Present (Ent) then
3049 Set_Treat_As_Volatile (Ent);
3051 end Mark_Aliased_Address_As_Volatile;
3057 function Minimum_Size
3059 Biased : Boolean := False) return Nat
3061 Lo : Uint := No_Uint;
3062 Hi : Uint := No_Uint;
3063 LoR : Ureal := No_Ureal;
3064 HiR : Ureal := No_Ureal;
3065 LoSet : Boolean := False;
3066 HiSet : Boolean := False;
3070 R_Typ : constant Entity_Id := Root_Type (T);
3073 -- If bad type, return 0
3075 if T = Any_Type then
3078 -- For generic types, just return zero. There cannot be any legitimate
3079 -- need to know such a size, but this routine may be called with a
3080 -- generic type as part of normal processing.
3082 elsif Is_Generic_Type (R_Typ)
3083 or else R_Typ = Any_Type
3087 -- Access types. Normally an access type cannot have a size smaller
3088 -- than the size of System.Address. The exception is on VMS, where
3089 -- we have short and long addresses, and it is possible for an access
3090 -- type to have a short address size (and thus be less than the size
3091 -- of System.Address itself). We simply skip the check for VMS, and
3092 -- leave the back end to do the check.
3094 elsif Is_Access_Type (T) then
3095 if OpenVMS_On_Target then
3098 return System_Address_Size;
3101 -- Floating-point types
3103 elsif Is_Floating_Point_Type (T) then
3104 return UI_To_Int (Esize (R_Typ));
3108 elsif Is_Discrete_Type (T) then
3110 -- The following loop is looking for the nearest compile time
3111 -- known bounds following the ancestor subtype chain. The idea
3112 -- is to find the most restrictive known bounds information.
3116 if Ancest = Any_Type or else Etype (Ancest) = Any_Type then
3121 if Compile_Time_Known_Value (Type_Low_Bound (Ancest)) then
3122 Lo := Expr_Rep_Value (Type_Low_Bound (Ancest));
3129 if Compile_Time_Known_Value (Type_High_Bound (Ancest)) then
3130 Hi := Expr_Rep_Value (Type_High_Bound (Ancest));
3136 Ancest := Ancestor_Subtype (Ancest);
3139 Ancest := Base_Type (T);
3141 if Is_Generic_Type (Ancest) then
3147 -- Fixed-point types. We can't simply use Expr_Value to get the
3148 -- Corresponding_Integer_Value values of the bounds, since these
3149 -- do not get set till the type is frozen, and this routine can
3150 -- be called before the type is frozen. Similarly the test for
3151 -- bounds being static needs to include the case where we have
3152 -- unanalyzed real literals for the same reason.
3154 elsif Is_Fixed_Point_Type (T) then
3156 -- The following loop is looking for the nearest compile time
3157 -- known bounds following the ancestor subtype chain. The idea
3158 -- is to find the most restrictive known bounds information.
3162 if Ancest = Any_Type or else Etype (Ancest) = Any_Type then
3167 if Nkind (Type_Low_Bound (Ancest)) = N_Real_Literal
3168 or else Compile_Time_Known_Value (Type_Low_Bound (Ancest))
3170 LoR := Expr_Value_R (Type_Low_Bound (Ancest));
3177 if Nkind (Type_High_Bound (Ancest)) = N_Real_Literal
3178 or else Compile_Time_Known_Value (Type_High_Bound (Ancest))
3180 HiR := Expr_Value_R (Type_High_Bound (Ancest));
3186 Ancest := Ancestor_Subtype (Ancest);
3189 Ancest := Base_Type (T);
3191 if Is_Generic_Type (Ancest) then
3197 Lo := UR_To_Uint (LoR / Small_Value (T));
3198 Hi := UR_To_Uint (HiR / Small_Value (T));
3200 -- No other types allowed
3203 raise Program_Error;
3206 -- Fall through with Hi and Lo set. Deal with biased case.
3208 if (Biased and then not Is_Fixed_Point_Type (T))
3209 or else Has_Biased_Representation (T)
3215 -- Signed case. Note that we consider types like range 1 .. -1 to be
3216 -- signed for the purpose of computing the size, since the bounds
3217 -- have to be accomodated in the base type.
3219 if Lo < 0 or else Hi < 0 then
3223 -- S = size, B = 2 ** (size - 1) (can accommodate -B .. +(B - 1))
3224 -- Note that we accommodate the case where the bounds cross. This
3225 -- can happen either because of the way the bounds are declared
3226 -- or because of the algorithm in Freeze_Fixed_Point_Type.
3240 -- If both bounds are positive, make sure that both are represen-
3241 -- table in the case where the bounds are crossed. This can happen
3242 -- either because of the way the bounds are declared, or because of
3243 -- the algorithm in Freeze_Fixed_Point_Type.
3249 -- S = size, (can accommodate 0 .. (2**size - 1))
3252 while Hi >= Uint_2 ** S loop
3260 -------------------------
3261 -- New_Stream_Function --
3262 -------------------------
3264 procedure New_Stream_Function
3268 Nam : TSS_Name_Type)
3270 Loc : constant Source_Ptr := Sloc (N);
3271 Sname : constant Name_Id := Make_TSS_Name (Base_Type (Ent), Nam);
3272 Subp_Id : Entity_Id;
3273 Subp_Decl : Node_Id;
3277 function Build_Spec return Node_Id;
3278 -- Used for declaration and renaming declaration, so that this is
3279 -- treated as a renaming_as_body.
3285 function Build_Spec return Node_Id is
3287 Subp_Id := Make_Defining_Identifier (Loc, Sname);
3290 Make_Function_Specification (Loc,
3291 Defining_Unit_Name => Subp_Id,
3292 Parameter_Specifications =>
3294 Make_Parameter_Specification (Loc,
3295 Defining_Identifier =>
3296 Make_Defining_Identifier (Loc, Name_S),
3298 Make_Access_Definition (Loc,
3301 Designated_Type (Etype (F)), Loc)))),
3304 New_Reference_To (Etyp, Loc));
3307 -- Start of processing for New_Stream_Function
3310 F := First_Formal (Subp);
3311 Etyp := Etype (Subp);
3313 if not Is_Tagged_Type (Ent) then
3315 Make_Subprogram_Declaration (Loc,
3316 Specification => Build_Spec);
3317 Insert_Action (N, Subp_Decl);
3321 Make_Subprogram_Renaming_Declaration (Loc,
3322 Specification => Build_Spec,
3323 Name => New_Reference_To (Subp, Loc));
3325 if Is_Tagged_Type (Ent) and then not Is_Limited_Type (Ent) then
3326 Set_TSS (Base_Type (Ent), Subp_Id);
3328 Insert_Action (N, Subp_Decl);
3329 Copy_TSS (Subp_Id, Base_Type (Ent));
3331 end New_Stream_Function;
3333 --------------------------
3334 -- New_Stream_Procedure --
3335 --------------------------
3337 procedure New_Stream_Procedure
3341 Nam : TSS_Name_Type;
3342 Out_P : Boolean := False)
3344 Loc : constant Source_Ptr := Sloc (N);
3345 Sname : constant Name_Id := Make_TSS_Name (Base_Type (Ent), Nam);
3346 Subp_Id : Entity_Id;
3347 Subp_Decl : Node_Id;
3351 function Build_Spec return Node_Id;
3352 -- Used for declaration and renaming declaration, so that this is
3353 -- treated as a renaming_as_body.
3359 function Build_Spec return Node_Id is
3361 Subp_Id := Make_Defining_Identifier (Loc, Sname);
3364 Make_Procedure_Specification (Loc,
3365 Defining_Unit_Name => Subp_Id,
3366 Parameter_Specifications =>
3368 Make_Parameter_Specification (Loc,
3369 Defining_Identifier =>
3370 Make_Defining_Identifier (Loc, Name_S),
3372 Make_Access_Definition (Loc,
3375 Designated_Type (Etype (F)), Loc))),
3377 Make_Parameter_Specification (Loc,
3378 Defining_Identifier =>
3379 Make_Defining_Identifier (Loc, Name_V),
3380 Out_Present => Out_P,
3382 New_Reference_To (Etyp, Loc))));
3385 -- Start of processing for New_Stream_Procedure
3388 F := First_Formal (Subp);
3389 Etyp := Etype (Next_Formal (F));
3391 if not Is_Tagged_Type (Ent) then
3393 Make_Subprogram_Declaration (Loc,
3394 Specification => Build_Spec);
3395 Insert_Action (N, Subp_Decl);
3399 Make_Subprogram_Renaming_Declaration (Loc,
3400 Specification => Build_Spec,
3401 Name => New_Reference_To (Subp, Loc));
3403 if Is_Tagged_Type (Ent) and then not Is_Limited_Type (Ent) then
3404 Set_TSS (Base_Type (Ent), Subp_Id);
3406 Insert_Action (N, Subp_Decl);
3407 Copy_TSS (Subp_Id, Base_Type (Ent));
3409 end New_Stream_Procedure;
3411 ---------------------
3412 -- Record_Rep_Item --
3413 ---------------------
3415 procedure Record_Rep_Item (T : Entity_Id; N : Node_Id) is
3417 Set_Next_Rep_Item (N, First_Rep_Item (T));
3418 Set_First_Rep_Item (T, N);
3419 end Record_Rep_Item;
3421 ------------------------
3422 -- Rep_Item_Too_Early --
3423 ------------------------
3425 function Rep_Item_Too_Early
3427 N : Node_Id) return Boolean
3430 -- Cannot apply rep items that are not operational items
3433 if Is_Operational_Item (N) then
3437 and then Is_Generic_Type (Root_Type (T))
3440 ("representation item not allowed for generic type", N);
3444 -- Otherwise check for incompleted type
3446 if Is_Incomplete_Or_Private_Type (T)
3447 and then No (Underlying_Type (T))
3450 ("representation item must be after full type declaration", N);
3453 -- If the type has incompleted components, a representation clause is
3454 -- illegal but stream attributes and Convention pragmas are correct.
3456 elsif Has_Private_Component (T) then
3457 if Nkind (N) = N_Pragma then
3461 ("representation item must appear after type is fully defined",
3468 end Rep_Item_Too_Early;
3470 -----------------------
3471 -- Rep_Item_Too_Late --
3472 -----------------------
3474 function Rep_Item_Too_Late
3477 FOnly : Boolean := False) return Boolean
3480 Parent_Type : Entity_Id;
3483 -- Output the too late message
3485 procedure Too_Late is
3487 Error_Msg_N ("representation item appears too late!", N);
3490 -- Start of processing for Rep_Item_Too_Late
3493 -- First make sure entity is not frozen (RM 13.1(9)). Exclude imported
3494 -- types, which may be frozen if they appear in a representation clause
3495 -- for a local type.
3498 and then not From_With_Type (T)
3501 S := First_Subtype (T);
3503 if Present (Freeze_Node (S)) then
3505 ("?no more representation items for }!", Freeze_Node (S), S);
3510 -- Check for case of non-tagged derived type whose parent either has
3511 -- primitive operations, or is a by reference type (RM 13.1(10)).
3515 and then Is_Derived_Type (T)
3516 and then not Is_Tagged_Type (T)
3518 Parent_Type := Etype (Base_Type (T));
3520 if Has_Primitive_Operations (Parent_Type) then
3523 ("primitive operations already defined for&!", N, Parent_Type);
3526 elsif Is_By_Reference_Type (Parent_Type) then
3529 ("parent type & is a by reference type!", N, Parent_Type);
3534 -- No error, link item into head of chain of rep items for the entity
3536 Record_Rep_Item (T, N);
3538 end Rep_Item_Too_Late;
3540 -------------------------
3541 -- Same_Representation --
3542 -------------------------
3544 function Same_Representation (Typ1, Typ2 : Entity_Id) return Boolean is
3545 T1 : constant Entity_Id := Underlying_Type (Typ1);
3546 T2 : constant Entity_Id := Underlying_Type (Typ2);
3549 -- A quick check, if base types are the same, then we definitely have
3550 -- the same representation, because the subtype specific representation
3551 -- attributes (Size and Alignment) do not affect representation from
3552 -- the point of view of this test.
3554 if Base_Type (T1) = Base_Type (T2) then
3557 elsif Is_Private_Type (Base_Type (T2))
3558 and then Base_Type (T1) = Full_View (Base_Type (T2))
3563 -- Tagged types never have differing representations
3565 if Is_Tagged_Type (T1) then
3569 -- Representations are definitely different if conventions differ
3571 if Convention (T1) /= Convention (T2) then
3575 -- Representations are different if component alignments differ
3577 if (Is_Record_Type (T1) or else Is_Array_Type (T1))
3579 (Is_Record_Type (T2) or else Is_Array_Type (T2))
3580 and then Component_Alignment (T1) /= Component_Alignment (T2)
3585 -- For arrays, the only real issue is component size. If we know the
3586 -- component size for both arrays, and it is the same, then that's
3587 -- good enough to know we don't have a change of representation.
3589 if Is_Array_Type (T1) then
3590 if Known_Component_Size (T1)
3591 and then Known_Component_Size (T2)
3592 and then Component_Size (T1) = Component_Size (T2)
3598 -- Types definitely have same representation if neither has non-standard
3599 -- representation since default representations are always consistent.
3600 -- If only one has non-standard representation, and the other does not,
3601 -- then we consider that they do not have the same representation. They
3602 -- might, but there is no way of telling early enough.
3604 if Has_Non_Standard_Rep (T1) then
3605 if not Has_Non_Standard_Rep (T2) then
3609 return not Has_Non_Standard_Rep (T2);
3612 -- Here the two types both have non-standard representation, and we
3613 -- need to determine if they have the same non-standard representation
3615 -- For arrays, we simply need to test if the component sizes are the
3616 -- same. Pragma Pack is reflected in modified component sizes, so this
3617 -- check also deals with pragma Pack.
3619 if Is_Array_Type (T1) then
3620 return Component_Size (T1) = Component_Size (T2);
3622 -- Tagged types always have the same representation, because it is not
3623 -- possible to specify different representations for common fields.
3625 elsif Is_Tagged_Type (T1) then
3628 -- Case of record types
3630 elsif Is_Record_Type (T1) then
3632 -- Packed status must conform
3634 if Is_Packed (T1) /= Is_Packed (T2) then
3637 -- Otherwise we must check components. Typ2 maybe a constrained
3638 -- subtype with fewer components, so we compare the components
3639 -- of the base types.
3642 Record_Case : declare
3643 CD1, CD2 : Entity_Id;
3645 function Same_Rep return Boolean;
3646 -- CD1 and CD2 are either components or discriminants. This
3647 -- function tests whether the two have the same representation
3649 function Same_Rep return Boolean is
3651 if No (Component_Clause (CD1)) then
3652 return No (Component_Clause (CD2));
3656 Present (Component_Clause (CD2))
3658 Component_Bit_Offset (CD1) = Component_Bit_Offset (CD2)
3660 Esize (CD1) = Esize (CD2);
3664 -- Start processing for Record_Case
3667 if Has_Discriminants (T1) then
3668 CD1 := First_Discriminant (T1);
3669 CD2 := First_Discriminant (T2);
3671 -- The number of discriminants may be different if the
3672 -- derived type has fewer (constrained by values). The
3673 -- invisible discriminants retain the representation of
3674 -- the original, so the discrepancy does not per se
3675 -- indicate a different representation.
3678 and then Present (CD2)
3680 if not Same_Rep then
3683 Next_Discriminant (CD1);
3684 Next_Discriminant (CD2);
3689 CD1 := First_Component (Underlying_Type (Base_Type (T1)));
3690 CD2 := First_Component (Underlying_Type (Base_Type (T2)));
3692 while Present (CD1) loop
3693 if not Same_Rep then
3696 Next_Component (CD1);
3697 Next_Component (CD2);
3705 -- For enumeration types, we must check each literal to see if the
3706 -- representation is the same. Note that we do not permit enumeration
3707 -- reprsentation clauses for Character and Wide_Character, so these
3708 -- cases were already dealt with.
3710 elsif Is_Enumeration_Type (T1) then
3712 Enumeration_Case : declare
3716 L1 := First_Literal (T1);
3717 L2 := First_Literal (T2);
3719 while Present (L1) loop
3720 if Enumeration_Rep (L1) /= Enumeration_Rep (L2) then
3730 end Enumeration_Case;
3732 -- Any other types have the same representation for these purposes
3737 end Same_Representation;
3739 --------------------
3740 -- Set_Enum_Esize --
3741 --------------------
3743 procedure Set_Enum_Esize (T : Entity_Id) is
3751 -- Find the minimum standard size (8,16,32,64) that fits
3753 Lo := Enumeration_Rep (Entity (Type_Low_Bound (T)));
3754 Hi := Enumeration_Rep (Entity (Type_High_Bound (T)));
3757 if Lo >= -Uint_2**07 and then Hi < Uint_2**07 then
3758 Sz := Standard_Character_Size; -- May be > 8 on some targets
3760 elsif Lo >= -Uint_2**15 and then Hi < Uint_2**15 then
3763 elsif Lo >= -Uint_2**31 and then Hi < Uint_2**31 then
3766 else pragma Assert (Lo >= -Uint_2**63 and then Hi < Uint_2**63);
3771 if Hi < Uint_2**08 then
3772 Sz := Standard_Character_Size; -- May be > 8 on some targets
3774 elsif Hi < Uint_2**16 then
3777 elsif Hi < Uint_2**32 then
3780 else pragma Assert (Hi < Uint_2**63);
3785 -- That minimum is the proper size unless we have a foreign convention
3786 -- and the size required is 32 or less, in which case we bump the size
3787 -- up to 32. This is required for C and C++ and seems reasonable for
3788 -- all other foreign conventions.
3790 if Has_Foreign_Convention (T)
3791 and then Esize (T) < Standard_Integer_Size
3793 Init_Esize (T, Standard_Integer_Size);
3800 -----------------------------------
3801 -- Validate_Unchecked_Conversion --
3802 -----------------------------------
3804 procedure Validate_Unchecked_Conversion
3806 Act_Unit : Entity_Id)
3813 -- Obtain source and target types. Note that we call Ancestor_Subtype
3814 -- here because the processing for generic instantiation always makes
3815 -- subtypes, and we want the original frozen actual types.
3817 -- If we are dealing with private types, then do the check on their
3818 -- fully declared counterparts if the full declarations have been
3819 -- encountered (they don't have to be visible, but they must exist!)
3821 Source := Ancestor_Subtype (Etype (First_Formal (Act_Unit)));
3823 if Is_Private_Type (Source)
3824 and then Present (Underlying_Type (Source))
3826 Source := Underlying_Type (Source);
3829 Target := Ancestor_Subtype (Etype (Act_Unit));
3831 -- If either type is generic, the instantiation happens within a
3832 -- generic unit, and there is nothing to check. The proper check
3833 -- will happen when the enclosing generic is instantiated.
3835 if Is_Generic_Type (Source) or else Is_Generic_Type (Target) then
3839 if Is_Private_Type (Target)
3840 and then Present (Underlying_Type (Target))
3842 Target := Underlying_Type (Target);
3845 -- Source may be unconstrained array, but not target
3847 if Is_Array_Type (Target)
3848 and then not Is_Constrained (Target)
3851 ("unchecked conversion to unconstrained array not allowed", N);
3855 -- Make entry in unchecked conversion table for later processing
3856 -- by Validate_Unchecked_Conversions, which will check sizes and
3857 -- alignments (using values set by the back-end where possible).
3858 -- This is only done if the appropriate warning is active
3860 if Warn_On_Unchecked_Conversion then
3861 Unchecked_Conversions.Append
3862 (New_Val => UC_Entry'
3867 -- If both sizes are known statically now, then back end annotation
3868 -- is not required to do a proper check but if either size is not
3869 -- known statically, then we need the annotation.
3871 if Known_Static_RM_Size (Source)
3872 and then Known_Static_RM_Size (Target)
3876 Back_Annotate_Rep_Info := True;
3880 -- If unchecked conversion to access type, and access type is
3881 -- declared in the same unit as the unchecked conversion, then
3882 -- set the No_Strict_Aliasing flag (no strict aliasing is
3883 -- implicit in this situation).
3885 if Is_Access_Type (Target) and then
3886 In_Same_Source_Unit (Target, N)
3888 Set_No_Strict_Aliasing (Implementation_Base_Type (Target));
3891 -- Generate N_Validate_Unchecked_Conversion node for back end in
3892 -- case the back end needs to perform special validation checks.
3894 -- Shouldn't this be in exp_ch13, since the check only gets done
3895 -- if we have full expansion and the back end is called ???
3898 Make_Validate_Unchecked_Conversion (Sloc (N));
3899 Set_Source_Type (Vnode, Source);
3900 Set_Target_Type (Vnode, Target);
3902 -- If the unchecked conversion node is in a list, just insert before
3903 -- it. If not we have some strange case, not worth bothering about.
3905 if Is_List_Member (N) then
3906 Insert_After (N, Vnode);
3908 end Validate_Unchecked_Conversion;
3910 ------------------------------------
3911 -- Validate_Unchecked_Conversions --
3912 ------------------------------------
3914 procedure Validate_Unchecked_Conversions is
3916 for N in Unchecked_Conversions.First .. Unchecked_Conversions.Last loop
3918 T : UC_Entry renames Unchecked_Conversions.Table (N);
3920 Enode : constant Node_Id := T.Enode;
3921 Source : constant Entity_Id := T.Source;
3922 Target : constant Entity_Id := T.Target;
3928 -- This validation check, which warns if we have unequal sizes
3929 -- for unchecked conversion, and thus potentially implementation
3930 -- dependent semantics, is one of the few occasions on which we
3931 -- use the official RM size instead of Esize. See description
3932 -- in Einfo "Handling of Type'Size Values" for details.
3934 if Serious_Errors_Detected = 0
3935 and then Known_Static_RM_Size (Source)
3936 and then Known_Static_RM_Size (Target)
3938 Source_Siz := RM_Size (Source);
3939 Target_Siz := RM_Size (Target);
3941 if Source_Siz /= Target_Siz then
3943 ("types for unchecked conversion have different sizes?",
3946 if All_Errors_Mode then
3947 Error_Msg_Name_1 := Chars (Source);
3948 Error_Msg_Uint_1 := Source_Siz;
3949 Error_Msg_Name_2 := Chars (Target);
3950 Error_Msg_Uint_2 := Target_Siz;
3952 ("\size of % is ^, size of % is ^?", Enode);
3954 Error_Msg_Uint_1 := UI_Abs (Source_Siz - Target_Siz);
3956 if Is_Discrete_Type (Source)
3957 and then Is_Discrete_Type (Target)
3959 if Source_Siz > Target_Siz then
3961 ("\^ high order bits of source will be ignored?",
3964 elsif Is_Unsigned_Type (Source) then
3966 ("\source will be extended with ^ high order " &
3967 "zero bits?", Enode);
3971 ("\source will be extended with ^ high order " &
3976 elsif Source_Siz < Target_Siz then
3977 if Is_Discrete_Type (Target) then
3978 if Bytes_Big_Endian then
3980 ("\target value will include ^ undefined " &
3985 ("\target value will include ^ undefined " &
3992 ("\^ trailing bits of target value will be " &
3993 "undefined?", Enode);
3996 else pragma Assert (Source_Siz > Target_Siz);
3998 ("\^ trailing bits of source will be ignored?",
4005 -- If both types are access types, we need to check the alignment.
4006 -- If the alignment of both is specified, we can do it here.
4008 if Serious_Errors_Detected = 0
4009 and then Ekind (Source) in Access_Kind
4010 and then Ekind (Target) in Access_Kind
4011 and then Target_Strict_Alignment
4012 and then Present (Designated_Type (Source))
4013 and then Present (Designated_Type (Target))
4016 D_Source : constant Entity_Id := Designated_Type (Source);
4017 D_Target : constant Entity_Id := Designated_Type (Target);
4020 if Known_Alignment (D_Source)
4021 and then Known_Alignment (D_Target)
4024 Source_Align : constant Uint := Alignment (D_Source);
4025 Target_Align : constant Uint := Alignment (D_Target);
4028 if Source_Align < Target_Align
4029 and then not Is_Tagged_Type (D_Source)
4031 Error_Msg_Uint_1 := Target_Align;
4032 Error_Msg_Uint_2 := Source_Align;
4033 Error_Msg_Node_2 := D_Source;
4035 ("alignment of & (^) is stricter than " &
4036 "alignment of & (^)?", Enode, D_Target);
4038 if All_Errors_Mode then
4040 ("\resulting access value may have invalid " &
4041 "alignment?", Enode);
4050 end Validate_Unchecked_Conversions;