1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2007, Free Software Foundation, Inc. --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING3. If not, go to --
19 -- http://www.gnu.org/licenses for a complete copy of the license. --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
24 ------------------------------------------------------------------------------
26 with Atree; use Atree;
27 with Checks; use Checks;
28 with Einfo; use Einfo;
29 with Errout; use Errout;
30 with Exp_Tss; use Exp_Tss;
31 with Exp_Util; use Exp_Util;
33 with Namet; use Namet;
34 with Nlists; use Nlists;
35 with Nmake; use Nmake;
37 with Restrict; use Restrict;
38 with Rident; use Rident;
39 with Rtsfind; use Rtsfind;
41 with Sem_Ch8; use Sem_Ch8;
42 with Sem_Eval; use Sem_Eval;
43 with Sem_Res; use Sem_Res;
44 with Sem_Type; use Sem_Type;
45 with Sem_Util; use Sem_Util;
46 with Sem_Warn; use Sem_Warn;
47 with Snames; use Snames;
48 with Stand; use Stand;
49 with Sinfo; use Sinfo;
51 with Targparm; use Targparm;
52 with Ttypes; use Ttypes;
53 with Tbuild; use Tbuild;
54 with Urealp; use Urealp;
56 with GNAT.Heap_Sort_A; use GNAT.Heap_Sort_A;
58 package body Sem_Ch13 is
60 SSU : constant Pos := System_Storage_Unit;
61 -- Convenient short hand for commonly used constant
63 -----------------------
64 -- Local Subprograms --
65 -----------------------
67 procedure Alignment_Check_For_Esize_Change (Typ : Entity_Id);
68 -- This routine is called after setting the Esize of type entity Typ.
69 -- The purpose is to deal with the situation where an aligment has been
70 -- inherited from a derived type that is no longer appropriate for the
71 -- new Esize value. In this case, we reset the Alignment to unknown.
73 procedure Check_Component_Overlap (C1_Ent, C2_Ent : Entity_Id);
74 -- Given two entities for record components or discriminants, checks
75 -- if they hav overlapping component clauses and issues errors if so.
77 function Get_Alignment_Value (Expr : Node_Id) return Uint;
78 -- Given the expression for an alignment value, returns the corresponding
79 -- Uint value. If the value is inappropriate, then error messages are
80 -- posted as required, and a value of No_Uint is returned.
82 function Is_Operational_Item (N : Node_Id) return Boolean;
83 -- A specification for a stream attribute is allowed before the full
84 -- type is declared, as explained in AI-00137 and the corrigendum.
85 -- Attributes that do not specify a representation characteristic are
86 -- operational attributes.
88 function Address_Aliased_Entity (N : Node_Id) return Entity_Id;
89 -- If expression N is of the form E'Address, return E
91 procedure Mark_Aliased_Address_As_Volatile (N : Node_Id);
92 -- This is used for processing of an address representation clause. If
93 -- the expression N is of the form of K'Address, then the entity that
94 -- is associated with K is marked as volatile.
96 procedure New_Stream_Subprogram
100 Nam : TSS_Name_Type);
101 -- Create a subprogram renaming of a given stream attribute to the
102 -- designated subprogram and then in the tagged case, provide this as a
103 -- primitive operation, or in the non-tagged case make an appropriate TSS
104 -- entry. This is more properly an expansion activity than just semantics,
105 -- but the presence of user-defined stream functions for limited types is a
106 -- legality check, which is why this takes place here rather than in
107 -- exp_ch13, where it was previously. Nam indicates the name of the TSS
108 -- function to be generated.
110 -- To avoid elaboration anomalies with freeze nodes, for untagged types
111 -- we generate both a subprogram declaration and a subprogram renaming
112 -- declaration, so that the attribute specification is handled as a
113 -- renaming_as_body. For tagged types, the specification is one of the
116 ----------------------------------------------
117 -- Table for Validate_Unchecked_Conversions --
118 ----------------------------------------------
120 -- The following table collects unchecked conversions for validation.
121 -- Entries are made by Validate_Unchecked_Conversion and then the
122 -- call to Validate_Unchecked_Conversions does the actual error
123 -- checking and posting of warnings. The reason for this delayed
124 -- processing is to take advantage of back-annotations of size and
125 -- alignment values peformed by the back end.
127 type UC_Entry is record
128 Enode : Node_Id; -- node used for posting warnings
129 Source : Entity_Id; -- source type for unchecked conversion
130 Target : Entity_Id; -- target type for unchecked conversion
133 package Unchecked_Conversions is new Table.Table (
134 Table_Component_Type => UC_Entry,
135 Table_Index_Type => Int,
136 Table_Low_Bound => 1,
138 Table_Increment => 200,
139 Table_Name => "Unchecked_Conversions");
141 ----------------------------
142 -- Address_Aliased_Entity --
143 ----------------------------
145 function Address_Aliased_Entity (N : Node_Id) return Entity_Id is
147 if Nkind (N) = N_Attribute_Reference
148 and then Attribute_Name (N) = Name_Address
151 Nam : Node_Id := Prefix (N);
154 or else Nkind (Nam) = N_Selected_Component
155 or else Nkind (Nam) = N_Indexed_Component
160 if Is_Entity_Name (Nam) then
167 end Address_Aliased_Entity;
169 -----------------------------------------
170 -- Adjust_Record_For_Reverse_Bit_Order --
171 -----------------------------------------
173 procedure Adjust_Record_For_Reverse_Bit_Order (R : Entity_Id) is
174 Max_Machine_Scalar_Size : constant Uint :=
176 (Standard_Long_Long_Integer_Size);
177 -- We use this as the maximum machine scalar size in the sense of AI-133
181 SSU : constant Uint := UI_From_Int (System_Storage_Unit);
184 -- This first loop through components does two things. First it deals
185 -- with the case of components with component clauses whose length is
186 -- greater than the maximum machine scalar size (either accepting them
187 -- or rejecting as needed). Second, it counts the number of components
188 -- with component clauses whose length does not exceed this maximum for
192 Comp := First_Component_Or_Discriminant (R);
193 while Present (Comp) loop
195 CC : constant Node_Id := Component_Clause (Comp);
196 Fbit : constant Uint := Static_Integer (First_Bit (CC));
201 -- Case of component with size > max machine scalar
203 if Esize (Comp) > Max_Machine_Scalar_Size then
205 -- Must begin on byte boundary
207 if Fbit mod SSU /= 0 then
209 ("illegal first bit value for reverse bit order",
211 Error_Msg_Uint_1 := SSU;
212 Error_Msg_Uint_2 := Max_Machine_Scalar_Size;
215 ("\must be a multiple of ^ if size greater than ^",
218 -- Must end on byte boundary
220 elsif Esize (Comp) mod SSU /= 0 then
222 ("illegal last bit value for reverse bit order",
224 Error_Msg_Uint_1 := SSU;
225 Error_Msg_Uint_2 := Max_Machine_Scalar_Size;
228 ("\must be a multiple of ^ if size greater than ^",
231 -- OK, give warning if enabled
233 elsif Warn_On_Reverse_Bit_Order then
235 ("multi-byte field specified with non-standard"
236 & " Bit_Order?", CC);
238 if Bytes_Big_Endian then
240 ("\bytes are not reversed "
241 & "(component is big-endian)?", CC);
244 ("\bytes are not reversed "
245 & "(component is little-endian)?", CC);
249 -- Case where size is not greater than max machine scalar.
250 -- For now, we just count these.
253 Num_CC := Num_CC + 1;
258 Next_Component_Or_Discriminant (Comp);
261 -- We need to sort the component clauses on the basis of the Position
262 -- values in the clause, so we can group clauses with the same Position
263 -- together to determine the relevant machine scalar size.
266 Comps : array (0 .. Num_CC) of Entity_Id;
267 -- Array to collect component and discrimninant entities. The data
268 -- starts at index 1, the 0'th entry is for GNAT.Heap_Sort_A.
270 function CP_Lt (Op1, Op2 : Natural) return Boolean;
271 -- Compare routine for Sort (See GNAT.Heap_Sort_A)
273 procedure CP_Move (From : Natural; To : Natural);
274 -- Move routine for Sort (see GNAT.Heap_Sort_A)
278 -- Start and stop positions in component list of set of components
279 -- with the same starting position (that constitute components in
280 -- a single machine scalar).
283 -- Maximum last bit value of any component in this set
286 -- Corresponding machine scalar size
292 function CP_Lt (Op1, Op2 : Natural) return Boolean is
294 return Position (Component_Clause (Comps (Op1))) <
295 Position (Component_Clause (Comps (Op2)));
302 procedure CP_Move (From : Natural; To : Natural) is
304 Comps (To) := Comps (From);
308 -- Collect the component clauses
311 Comp := First_Component_Or_Discriminant (R);
312 while Present (Comp) loop
313 if Present (Component_Clause (Comp))
314 and then Esize (Comp) <= Max_Machine_Scalar_Size
316 Num_CC := Num_CC + 1;
317 Comps (Num_CC) := Comp;
320 Next_Component_Or_Discriminant (Comp);
323 -- Sort by ascending position number
325 Sort (Num_CC, CP_Move'Unrestricted_Access, CP_Lt'Unrestricted_Access);
327 -- We now have all the components whose size does not exceed the max
328 -- machine scalar value, sorted by starting position. In this loop
329 -- we gather groups of clauses starting at the same position, to
330 -- process them in accordance with Ada 2005 AI-133.
333 while Stop < Num_CC loop
337 Static_Integer (Last_Bit (Component_Clause (Comps (Start))));
338 while Stop < Num_CC loop
340 (Position (Component_Clause (Comps (Stop + 1)))) =
342 (Position (Component_Clause (Comps (Stop))))
349 (Last_Bit (Component_Clause (Comps (Stop)))));
355 -- Now we have a group of component clauses from Start to Stop
356 -- whose positions are identical, and MaxL is the maximum last bit
357 -- value of any of these components.
359 -- We need to determine the corresponding machine scalar size.
360 -- This loop assumes that machine scalar sizes are even, and that
361 -- each possible machine scalar has twice as many bits as the
364 MSS := Max_Machine_Scalar_Size;
366 and then (MSS / 2) >= SSU
367 and then (MSS / 2) > MaxL
372 -- Here is where we fix up the Component_Bit_Offset value to
373 -- account for the reverse bit order. Some examples of what needs
374 -- to be done for the case of a machine scalar size of 8 are:
376 -- First_Bit .. Last_Bit Component_Bit_Offset
388 -- The general rule is that the first bit is is obtained by
389 -- subtracting the old ending bit from machine scalar size - 1.
391 for C in Start .. Stop loop
393 Comp : constant Entity_Id := Comps (C);
394 CC : constant Node_Id := Component_Clause (Comp);
395 LB : constant Uint := Static_Integer (Last_Bit (CC));
396 NFB : constant Uint := MSS - Uint_1 - LB;
397 NLB : constant Uint := NFB + Esize (Comp) - 1;
398 Pos : constant Uint := Static_Integer (Position (CC));
401 if Warn_On_Reverse_Bit_Order then
402 Error_Msg_Uint_1 := MSS;
404 ("?reverse bit order in machine " &
405 "scalar of length^", First_Bit (CC));
406 Error_Msg_Uint_1 := NFB;
407 Error_Msg_Uint_2 := NLB;
409 if Bytes_Big_Endian then
411 ("?\big-endian range for component & is ^ .. ^",
412 First_Bit (CC), Comp);
415 ("?\little-endian range for component & is ^ .. ^",
416 First_Bit (CC), Comp);
420 Set_Component_Bit_Offset (Comp, Pos * SSU + NFB);
421 Set_Normalized_First_Bit (Comp, NFB mod SSU);
426 end Adjust_Record_For_Reverse_Bit_Order;
428 --------------------------------------
429 -- Alignment_Check_For_Esize_Change --
430 --------------------------------------
432 procedure Alignment_Check_For_Esize_Change (Typ : Entity_Id) is
434 -- If the alignment is known, and not set by a rep clause, and is
435 -- inconsistent with the size being set, then reset it to unknown,
436 -- we assume in this case that the size overrides the inherited
437 -- alignment, and that the alignment must be recomputed.
439 if Known_Alignment (Typ)
440 and then not Has_Alignment_Clause (Typ)
441 and then Esize (Typ) mod (Alignment (Typ) * SSU) /= 0
443 Init_Alignment (Typ);
445 end Alignment_Check_For_Esize_Change;
447 -----------------------
448 -- Analyze_At_Clause --
449 -----------------------
451 -- An at clause is replaced by the corresponding Address attribute
452 -- definition clause that is the preferred approach in Ada 95.
454 procedure Analyze_At_Clause (N : Node_Id) is
456 Check_Restriction (No_Obsolescent_Features, N);
458 if Warn_On_Obsolescent_Feature then
460 ("at clause is an obsolescent feature (RM J.7(2))?", N);
462 ("\use address attribute definition clause instead?", N);
466 Make_Attribute_Definition_Clause (Sloc (N),
467 Name => Identifier (N),
468 Chars => Name_Address,
469 Expression => Expression (N)));
470 Analyze_Attribute_Definition_Clause (N);
471 end Analyze_At_Clause;
473 -----------------------------------------
474 -- Analyze_Attribute_Definition_Clause --
475 -----------------------------------------
477 procedure Analyze_Attribute_Definition_Clause (N : Node_Id) is
478 Loc : constant Source_Ptr := Sloc (N);
479 Nam : constant Node_Id := Name (N);
480 Attr : constant Name_Id := Chars (N);
481 Expr : constant Node_Id := Expression (N);
482 Id : constant Attribute_Id := Get_Attribute_Id (Attr);
486 FOnly : Boolean := False;
487 -- Reset to True for subtype specific attribute (Alignment, Size)
488 -- and for stream attributes, i.e. those cases where in the call
489 -- to Rep_Item_Too_Late, FOnly is set True so that only the freezing
490 -- rules are checked. Note that the case of stream attributes is not
491 -- clear from the RM, but see AI95-00137. Also, the RM seems to
492 -- disallow Storage_Size for derived task types, but that is also
493 -- clearly unintentional.
495 procedure Analyze_Stream_TSS_Definition (TSS_Nam : TSS_Name_Type);
496 -- Common processing for 'Read, 'Write, 'Input and 'Output attribute
497 -- definition clauses.
499 procedure Analyze_Stream_TSS_Definition (TSS_Nam : TSS_Name_Type) is
500 Subp : Entity_Id := Empty;
505 Is_Read : constant Boolean := (TSS_Nam = TSS_Stream_Read);
507 function Has_Good_Profile (Subp : Entity_Id) return Boolean;
508 -- Return true if the entity is a subprogram with an appropriate
509 -- profile for the attribute being defined.
511 ----------------------
512 -- Has_Good_Profile --
513 ----------------------
515 function Has_Good_Profile (Subp : Entity_Id) return Boolean is
517 Is_Function : constant Boolean := (TSS_Nam = TSS_Stream_Input);
518 Expected_Ekind : constant array (Boolean) of Entity_Kind :=
519 (False => E_Procedure, True => E_Function);
523 if Ekind (Subp) /= Expected_Ekind (Is_Function) then
527 F := First_Formal (Subp);
530 or else Ekind (Etype (F)) /= E_Anonymous_Access_Type
531 or else Designated_Type (Etype (F)) /=
532 Class_Wide_Type (RTE (RE_Root_Stream_Type))
537 if not Is_Function then
541 Expected_Mode : constant array (Boolean) of Entity_Kind :=
542 (False => E_In_Parameter,
543 True => E_Out_Parameter);
545 if Parameter_Mode (F) /= Expected_Mode (Is_Read) then
556 return Base_Type (Typ) = Base_Type (Ent)
557 and then No (Next_Formal (F));
559 end Has_Good_Profile;
561 -- Start of processing for Analyze_Stream_TSS_Definition
566 if not Is_Type (U_Ent) then
567 Error_Msg_N ("local name must be a subtype", Nam);
571 Pnam := TSS (Base_Type (U_Ent), TSS_Nam);
573 -- If Pnam is present, it can be either inherited from an ancestor
574 -- type (in which case it is legal to redefine it for this type), or
575 -- be a previous definition of the attribute for the same type (in
576 -- which case it is illegal).
578 -- In the first case, it will have been analyzed already, and we
579 -- can check that its profile does not match the expected profile
580 -- for a stream attribute of U_Ent. In the second case, either Pnam
581 -- has been analyzed (and has the expected profile), or it has not
582 -- been analyzed yet (case of a type that has not been frozen yet
583 -- and for which the stream attribute has been set using Set_TSS).
586 and then (No (First_Entity (Pnam)) or else Has_Good_Profile (Pnam))
588 Error_Msg_Sloc := Sloc (Pnam);
589 Error_Msg_Name_1 := Attr;
590 Error_Msg_N ("% attribute already defined #", Nam);
596 if Is_Entity_Name (Expr) then
597 if not Is_Overloaded (Expr) then
598 if Has_Good_Profile (Entity (Expr)) then
599 Subp := Entity (Expr);
603 Get_First_Interp (Expr, I, It);
605 while Present (It.Nam) loop
606 if Has_Good_Profile (It.Nam) then
611 Get_Next_Interp (I, It);
616 if Present (Subp) then
617 if Is_Abstract_Subprogram (Subp) then
618 Error_Msg_N ("stream subprogram must not be abstract", Expr);
622 Set_Entity (Expr, Subp);
623 Set_Etype (Expr, Etype (Subp));
625 New_Stream_Subprogram (N, U_Ent, Subp, TSS_Nam);
628 Error_Msg_Name_1 := Attr;
629 Error_Msg_N ("incorrect expression for% attribute", Expr);
631 end Analyze_Stream_TSS_Definition;
633 -- Start of processing for Analyze_Attribute_Definition_Clause
636 if Ignore_Rep_Clauses then
637 Rewrite (N, Make_Null_Statement (Sloc (N)));
644 if Rep_Item_Too_Early (Ent, N) then
648 -- Rep clause applies to full view of incomplete type or private type if
649 -- we have one (if not, this is a premature use of the type). However,
650 -- certain semantic checks need to be done on the specified entity (i.e.
651 -- the private view), so we save it in Ent.
653 if Is_Private_Type (Ent)
654 and then Is_Derived_Type (Ent)
655 and then not Is_Tagged_Type (Ent)
656 and then No (Full_View (Ent))
658 -- If this is a private type whose completion is a derivation from
659 -- another private type, there is no full view, and the attribute
660 -- belongs to the type itself, not its underlying parent.
664 elsif Ekind (Ent) = E_Incomplete_Type then
666 -- The attribute applies to the full view, set the entity of the
667 -- attribute definition accordingly.
669 Ent := Underlying_Type (Ent);
671 Set_Entity (Nam, Ent);
674 U_Ent := Underlying_Type (Ent);
677 -- Complete other routine error checks
679 if Etype (Nam) = Any_Type then
682 elsif Scope (Ent) /= Current_Scope then
683 Error_Msg_N ("entity must be declared in this scope", Nam);
686 elsif No (U_Ent) then
689 elsif Is_Type (U_Ent)
690 and then not Is_First_Subtype (U_Ent)
691 and then Id /= Attribute_Object_Size
692 and then Id /= Attribute_Value_Size
693 and then not From_At_Mod (N)
695 Error_Msg_N ("cannot specify attribute for subtype", Nam);
699 -- Switch on particular attribute
707 -- Address attribute definition clause
709 when Attribute_Address => Address : begin
710 Analyze_And_Resolve (Expr, RTE (RE_Address));
712 if Present (Address_Clause (U_Ent)) then
713 Error_Msg_N ("address already given for &", Nam);
715 -- Case of address clause for subprogram
717 elsif Is_Subprogram (U_Ent) then
718 if Has_Homonym (U_Ent) then
720 ("address clause cannot be given " &
721 "for overloaded subprogram",
725 -- For subprograms, all address clauses are permitted,
726 -- and we mark the subprogram as having a deferred freeze
727 -- so that Gigi will not elaborate it too soon.
729 -- Above needs more comments, what is too soon about???
731 Set_Has_Delayed_Freeze (U_Ent);
733 -- Case of address clause for entry
735 elsif Ekind (U_Ent) = E_Entry then
736 if Nkind (Parent (N)) = N_Task_Body then
738 ("entry address must be specified in task spec", Nam);
741 -- For entries, we require a constant address
743 Check_Constant_Address_Clause (Expr, U_Ent);
745 if Is_Task_Type (Scope (U_Ent))
746 and then Comes_From_Source (Scope (U_Ent))
749 ("?entry address declared for entry in task type", N);
751 ("\?only one task can be declared of this type", N);
754 Check_Restriction (No_Obsolescent_Features, N);
756 if Warn_On_Obsolescent_Feature then
758 ("attaching interrupt to task entry is an " &
759 "obsolescent feature (RM J.7.1)?", N);
761 ("\use interrupt procedure instead?", N);
764 -- Case of an address clause for a controlled object:
765 -- erroneous execution.
767 elsif Is_Controlled (Etype (U_Ent)) then
769 ("?controlled object& must not be overlaid", Nam, U_Ent);
771 ("\?Program_Error will be raised at run time", Nam);
772 Insert_Action (Declaration_Node (U_Ent),
773 Make_Raise_Program_Error (Loc,
774 Reason => PE_Overlaid_Controlled_Object));
776 -- Case of address clause for a (non-controlled) object
779 Ekind (U_Ent) = E_Variable
781 Ekind (U_Ent) = E_Constant
784 Expr : constant Node_Id := Expression (N);
785 Aent : constant Entity_Id := Address_Aliased_Entity (Expr);
788 -- Exported variables cannot have an address clause,
789 -- because this cancels the effect of the pragma Export
791 if Is_Exported (U_Ent) then
793 ("cannot export object with address clause", Nam);
795 -- Overlaying controlled objects is erroneous
798 and then Is_Controlled (Etype (Aent))
801 ("?controlled object must not be overlaid", Expr);
803 ("\?Program_Error will be raised at run time", Expr);
804 Insert_Action (Declaration_Node (U_Ent),
805 Make_Raise_Program_Error (Loc,
806 Reason => PE_Overlaid_Controlled_Object));
809 and then Ekind (U_Ent) = E_Constant
810 and then Ekind (Aent) /= E_Constant
812 Error_Msg_N ("constant overlays a variable?", Expr);
814 elsif Present (Renamed_Object (U_Ent)) then
816 ("address clause not allowed"
817 & " for a renaming declaration (RM 13.1(6))", Nam);
819 -- Imported variables can have an address clause, but then
820 -- the import is pretty meaningless except to suppress
821 -- initializations, so we do not need such variables to
822 -- be statically allocated (and in fact it causes trouble
823 -- if the address clause is a local value).
825 elsif Is_Imported (U_Ent) then
826 Set_Is_Statically_Allocated (U_Ent, False);
829 -- We mark a possible modification of a variable with an
830 -- address clause, since it is likely aliasing is occurring.
832 Note_Possible_Modification (Nam);
834 -- Here we are checking for explicit overlap of one
835 -- variable by another, and if we find this, then we
836 -- mark the overlapped variable as also being aliased.
838 -- First case is where we have an explicit
840 -- for J'Address use K'Address;
842 -- In this case, we mark K as volatile
844 Mark_Aliased_Address_As_Volatile (Expr);
846 -- Second case is where we have a constant whose
847 -- definition is of the form of an address as in:
849 -- A : constant Address := K'Address;
851 -- for B'Address use A;
853 -- In this case we also mark K as volatile
855 if Is_Entity_Name (Expr) then
857 Ent : constant Entity_Id := Entity (Expr);
858 Decl : constant Node_Id := Declaration_Node (Ent);
861 if Ekind (Ent) = E_Constant
862 and then Nkind (Decl) = N_Object_Declaration
863 and then Present (Expression (Decl))
865 Mark_Aliased_Address_As_Volatile
871 -- Legality checks on the address clause for initialized
872 -- objects is deferred until the freeze point, because
873 -- a subsequent pragma might indicate that the object is
874 -- imported and thus not initialized.
876 Set_Has_Delayed_Freeze (U_Ent);
878 if Is_Exported (U_Ent) then
880 ("& cannot be exported if an address clause is given",
883 ("\define and export a variable " &
884 "that holds its address instead",
888 -- Entity has delayed freeze, so we will generate an
889 -- alignment check at the freeze point unless suppressed.
891 if not Range_Checks_Suppressed (U_Ent)
892 and then not Alignment_Checks_Suppressed (U_Ent)
894 Set_Check_Address_Alignment (N);
897 -- Kill the size check code, since we are not allocating
898 -- the variable, it is somewhere else.
900 Kill_Size_Check_Code (U_Ent);
903 -- Not a valid entity for an address clause
906 Error_Msg_N ("address cannot be given for &", Nam);
914 -- Alignment attribute definition clause
916 when Attribute_Alignment => Alignment_Block : declare
917 Align : constant Uint := Get_Alignment_Value (Expr);
922 if not Is_Type (U_Ent)
923 and then Ekind (U_Ent) /= E_Variable
924 and then Ekind (U_Ent) /= E_Constant
926 Error_Msg_N ("alignment cannot be given for &", Nam);
928 elsif Has_Alignment_Clause (U_Ent) then
929 Error_Msg_Sloc := Sloc (Alignment_Clause (U_Ent));
930 Error_Msg_N ("alignment clause previously given#", N);
932 elsif Align /= No_Uint then
933 Set_Has_Alignment_Clause (U_Ent);
934 Set_Alignment (U_Ent, Align);
942 -- Bit_Order attribute definition clause
944 when Attribute_Bit_Order => Bit_Order : declare
946 if not Is_Record_Type (U_Ent) then
948 ("Bit_Order can only be defined for record type", Nam);
951 Analyze_And_Resolve (Expr, RTE (RE_Bit_Order));
953 if Etype (Expr) = Any_Type then
956 elsif not Is_Static_Expression (Expr) then
958 ("Bit_Order requires static expression!", Expr);
961 if (Expr_Value (Expr) = 0) /= Bytes_Big_Endian then
962 Set_Reverse_Bit_Order (U_Ent, True);
972 -- Component_Size attribute definition clause
974 when Attribute_Component_Size => Component_Size_Case : declare
975 Csize : constant Uint := Static_Integer (Expr);
978 New_Ctyp : Entity_Id;
982 if not Is_Array_Type (U_Ent) then
983 Error_Msg_N ("component size requires array type", Nam);
987 Btype := Base_Type (U_Ent);
989 if Has_Component_Size_Clause (Btype) then
991 ("component size clase for& previously given", Nam);
993 elsif Csize /= No_Uint then
994 Check_Size (Expr, Component_Type (Btype), Csize, Biased);
996 if Has_Aliased_Components (Btype)
1002 ("component size incorrect for aliased components", N);
1006 -- For the biased case, build a declaration for a subtype
1007 -- that will be used to represent the biased subtype that
1008 -- reflects the biased representation of components. We need
1009 -- this subtype to get proper conversions on referencing
1010 -- elements of the array.
1014 Make_Defining_Identifier (Loc,
1015 Chars => New_External_Name (Chars (U_Ent), 'C', 0, 'T'));
1018 Make_Subtype_Declaration (Loc,
1019 Defining_Identifier => New_Ctyp,
1020 Subtype_Indication =>
1021 New_Occurrence_Of (Component_Type (Btype), Loc));
1023 Set_Parent (Decl, N);
1024 Analyze (Decl, Suppress => All_Checks);
1026 Set_Has_Delayed_Freeze (New_Ctyp, False);
1027 Set_Esize (New_Ctyp, Csize);
1028 Set_RM_Size (New_Ctyp, Csize);
1029 Init_Alignment (New_Ctyp);
1030 Set_Has_Biased_Representation (New_Ctyp, True);
1031 Set_Is_Itype (New_Ctyp, True);
1032 Set_Associated_Node_For_Itype (New_Ctyp, U_Ent);
1034 Set_Component_Type (Btype, New_Ctyp);
1037 Set_Component_Size (Btype, Csize);
1038 Set_Has_Component_Size_Clause (Btype, True);
1039 Set_Has_Non_Standard_Rep (Btype, True);
1041 end Component_Size_Case;
1047 when Attribute_External_Tag => External_Tag :
1049 if not Is_Tagged_Type (U_Ent) then
1050 Error_Msg_N ("should be a tagged type", Nam);
1053 Analyze_And_Resolve (Expr, Standard_String);
1055 if not Is_Static_Expression (Expr) then
1056 Flag_Non_Static_Expr
1057 ("static string required for tag name!", Nam);
1060 if VM_Target = No_VM then
1061 Set_Has_External_Tag_Rep_Clause (U_Ent);
1063 Error_Msg_Name_1 := Attr;
1065 ("% attribute unsupported in this configuration", Nam);
1068 if not Is_Library_Level_Entity (U_Ent) then
1070 ("?non-unique external tag supplied for &", N, U_Ent);
1072 ("?\same external tag applies to all subprogram calls", N);
1074 ("?\corresponding internal tag cannot be obtained", N);
1082 when Attribute_Input =>
1083 Analyze_Stream_TSS_Definition (TSS_Stream_Input);
1084 Set_Has_Specified_Stream_Input (Ent);
1090 -- Machine radix attribute definition clause
1092 when Attribute_Machine_Radix => Machine_Radix : declare
1093 Radix : constant Uint := Static_Integer (Expr);
1096 if not Is_Decimal_Fixed_Point_Type (U_Ent) then
1097 Error_Msg_N ("decimal fixed-point type expected for &", Nam);
1099 elsif Has_Machine_Radix_Clause (U_Ent) then
1100 Error_Msg_Sloc := Sloc (Alignment_Clause (U_Ent));
1101 Error_Msg_N ("machine radix clause previously given#", N);
1103 elsif Radix /= No_Uint then
1104 Set_Has_Machine_Radix_Clause (U_Ent);
1105 Set_Has_Non_Standard_Rep (Base_Type (U_Ent));
1109 elsif Radix = 10 then
1110 Set_Machine_Radix_10 (U_Ent);
1112 Error_Msg_N ("machine radix value must be 2 or 10", Expr);
1121 -- Object_Size attribute definition clause
1123 when Attribute_Object_Size => Object_Size : declare
1124 Size : constant Uint := Static_Integer (Expr);
1128 if not Is_Type (U_Ent) then
1129 Error_Msg_N ("Object_Size cannot be given for &", Nam);
1131 elsif Has_Object_Size_Clause (U_Ent) then
1132 Error_Msg_N ("Object_Size already given for &", Nam);
1135 Check_Size (Expr, U_Ent, Size, Biased);
1143 UI_Mod (Size, 64) /= 0
1146 ("Object_Size must be 8, 16, 32, or multiple of 64",
1150 Set_Esize (U_Ent, Size);
1151 Set_Has_Object_Size_Clause (U_Ent);
1152 Alignment_Check_For_Esize_Change (U_Ent);
1160 when Attribute_Output =>
1161 Analyze_Stream_TSS_Definition (TSS_Stream_Output);
1162 Set_Has_Specified_Stream_Output (Ent);
1168 when Attribute_Read =>
1169 Analyze_Stream_TSS_Definition (TSS_Stream_Read);
1170 Set_Has_Specified_Stream_Read (Ent);
1176 -- Size attribute definition clause
1178 when Attribute_Size => Size : declare
1179 Size : constant Uint := Static_Integer (Expr);
1186 if Has_Size_Clause (U_Ent) then
1187 Error_Msg_N ("size already given for &", Nam);
1189 elsif not Is_Type (U_Ent)
1190 and then Ekind (U_Ent) /= E_Variable
1191 and then Ekind (U_Ent) /= E_Constant
1193 Error_Msg_N ("size cannot be given for &", Nam);
1195 elsif Is_Array_Type (U_Ent)
1196 and then not Is_Constrained (U_Ent)
1199 ("size cannot be given for unconstrained array", Nam);
1201 elsif Size /= No_Uint then
1202 if Is_Type (U_Ent) then
1205 Etyp := Etype (U_Ent);
1208 -- Check size, note that Gigi is in charge of checking that the
1209 -- size of an array or record type is OK. Also we do not check
1210 -- the size in the ordinary fixed-point case, since it is too
1211 -- early to do so (there may be subsequent small clause that
1212 -- affects the size). We can check the size if a small clause
1213 -- has already been given.
1215 if not Is_Ordinary_Fixed_Point_Type (U_Ent)
1216 or else Has_Small_Clause (U_Ent)
1218 Check_Size (Expr, Etyp, Size, Biased);
1219 Set_Has_Biased_Representation (U_Ent, Biased);
1222 -- For types set RM_Size and Esize if possible
1224 if Is_Type (U_Ent) then
1225 Set_RM_Size (U_Ent, Size);
1227 -- For scalar types, increase Object_Size to power of 2, but
1228 -- not less than a storage unit in any case (i.e., normally
1229 -- this means it will be byte addressable).
1231 if Is_Scalar_Type (U_Ent) then
1232 if Size <= System_Storage_Unit then
1233 Init_Esize (U_Ent, System_Storage_Unit);
1234 elsif Size <= 16 then
1235 Init_Esize (U_Ent, 16);
1236 elsif Size <= 32 then
1237 Init_Esize (U_Ent, 32);
1239 Set_Esize (U_Ent, (Size + 63) / 64 * 64);
1242 -- For all other types, object size = value size. The
1243 -- backend will adjust as needed.
1246 Set_Esize (U_Ent, Size);
1249 Alignment_Check_For_Esize_Change (U_Ent);
1251 -- For objects, set Esize only
1254 if Is_Elementary_Type (Etyp) then
1255 if Size /= System_Storage_Unit
1257 Size /= System_Storage_Unit * 2
1259 Size /= System_Storage_Unit * 4
1261 Size /= System_Storage_Unit * 8
1263 Error_Msg_Uint_1 := UI_From_Int (System_Storage_Unit);
1264 Error_Msg_Uint_2 := Error_Msg_Uint_1 * 8;
1266 ("size for primitive object must be a power of 2"
1267 & " in the range ^-^", N);
1271 Set_Esize (U_Ent, Size);
1274 Set_Has_Size_Clause (U_Ent);
1282 -- Small attribute definition clause
1284 when Attribute_Small => Small : declare
1285 Implicit_Base : constant Entity_Id := Base_Type (U_Ent);
1289 Analyze_And_Resolve (Expr, Any_Real);
1291 if Etype (Expr) = Any_Type then
1294 elsif not Is_Static_Expression (Expr) then
1295 Flag_Non_Static_Expr
1296 ("small requires static expression!", Expr);
1300 Small := Expr_Value_R (Expr);
1302 if Small <= Ureal_0 then
1303 Error_Msg_N ("small value must be greater than zero", Expr);
1309 if not Is_Ordinary_Fixed_Point_Type (U_Ent) then
1311 ("small requires an ordinary fixed point type", Nam);
1313 elsif Has_Small_Clause (U_Ent) then
1314 Error_Msg_N ("small already given for &", Nam);
1316 elsif Small > Delta_Value (U_Ent) then
1318 ("small value must not be greater then delta value", Nam);
1321 Set_Small_Value (U_Ent, Small);
1322 Set_Small_Value (Implicit_Base, Small);
1323 Set_Has_Small_Clause (U_Ent);
1324 Set_Has_Small_Clause (Implicit_Base);
1325 Set_Has_Non_Standard_Rep (Implicit_Base);
1333 -- Storage_Pool attribute definition clause
1335 when Attribute_Storage_Pool => Storage_Pool : declare
1340 if Ekind (U_Ent) = E_Access_Subprogram_Type then
1342 ("storage pool cannot be given for access-to-subprogram type",
1346 elsif Ekind (U_Ent) /= E_Access_Type
1347 and then Ekind (U_Ent) /= E_General_Access_Type
1350 ("storage pool can only be given for access types", Nam);
1353 elsif Is_Derived_Type (U_Ent) then
1355 ("storage pool cannot be given for a derived access type",
1358 elsif Has_Storage_Size_Clause (U_Ent) then
1359 Error_Msg_N ("storage size already given for &", Nam);
1362 elsif Present (Associated_Storage_Pool (U_Ent)) then
1363 Error_Msg_N ("storage pool already given for &", Nam);
1368 (Expr, Class_Wide_Type (RTE (RE_Root_Storage_Pool)));
1370 if Nkind (Expr) = N_Type_Conversion then
1371 T := Etype (Expression (Expr));
1376 -- The Stack_Bounded_Pool is used internally for implementing
1377 -- access types with a Storage_Size. Since it only work
1378 -- properly when used on one specific type, we need to check
1379 -- that it is not highjacked improperly:
1380 -- type T is access Integer;
1381 -- for T'Storage_Size use n;
1382 -- type Q is access Float;
1383 -- for Q'Storage_Size use T'Storage_Size; -- incorrect
1385 if RTE_Available (RE_Stack_Bounded_Pool)
1386 and then Base_Type (T) = RTE (RE_Stack_Bounded_Pool)
1388 Error_Msg_N ("non-shareable internal Pool", Expr);
1392 -- If the argument is a name that is not an entity name, then
1393 -- we construct a renaming operation to define an entity of
1394 -- type storage pool.
1396 if not Is_Entity_Name (Expr)
1397 and then Is_Object_Reference (Expr)
1400 Make_Defining_Identifier (Loc,
1401 Chars => New_Internal_Name ('P'));
1404 Rnode : constant Node_Id :=
1405 Make_Object_Renaming_Declaration (Loc,
1406 Defining_Identifier => Pool,
1408 New_Occurrence_Of (Etype (Expr), Loc),
1412 Insert_Before (N, Rnode);
1414 Set_Associated_Storage_Pool (U_Ent, Pool);
1417 elsif Is_Entity_Name (Expr) then
1418 Pool := Entity (Expr);
1420 -- If pool is a renamed object, get original one. This can
1421 -- happen with an explicit renaming, and within instances.
1423 while Present (Renamed_Object (Pool))
1424 and then Is_Entity_Name (Renamed_Object (Pool))
1426 Pool := Entity (Renamed_Object (Pool));
1429 if Present (Renamed_Object (Pool))
1430 and then Nkind (Renamed_Object (Pool)) = N_Type_Conversion
1431 and then Is_Entity_Name (Expression (Renamed_Object (Pool)))
1433 Pool := Entity (Expression (Renamed_Object (Pool)));
1436 Set_Associated_Storage_Pool (U_Ent, Pool);
1438 elsif Nkind (Expr) = N_Type_Conversion
1439 and then Is_Entity_Name (Expression (Expr))
1440 and then Nkind (Original_Node (Expr)) = N_Attribute_Reference
1442 Pool := Entity (Expression (Expr));
1443 Set_Associated_Storage_Pool (U_Ent, Pool);
1446 Error_Msg_N ("incorrect reference to a Storage Pool", Expr);
1455 -- Storage_Size attribute definition clause
1457 when Attribute_Storage_Size => Storage_Size : declare
1458 Btype : constant Entity_Id := Base_Type (U_Ent);
1462 if Is_Task_Type (U_Ent) then
1463 Check_Restriction (No_Obsolescent_Features, N);
1465 if Warn_On_Obsolescent_Feature then
1467 ("storage size clause for task is an " &
1468 "obsolescent feature (RM J.9)?", N);
1470 ("\use Storage_Size pragma instead?", N);
1476 if not Is_Access_Type (U_Ent)
1477 and then Ekind (U_Ent) /= E_Task_Type
1479 Error_Msg_N ("storage size cannot be given for &", Nam);
1481 elsif Is_Access_Type (U_Ent) and Is_Derived_Type (U_Ent) then
1483 ("storage size cannot be given for a derived access type",
1486 elsif Has_Storage_Size_Clause (Btype) then
1487 Error_Msg_N ("storage size already given for &", Nam);
1490 Analyze_And_Resolve (Expr, Any_Integer);
1492 if Is_Access_Type (U_Ent) then
1493 if Present (Associated_Storage_Pool (U_Ent)) then
1494 Error_Msg_N ("storage pool already given for &", Nam);
1498 if Compile_Time_Known_Value (Expr)
1499 and then Expr_Value (Expr) = 0
1501 Set_No_Pool_Assigned (Btype);
1504 else -- Is_Task_Type (U_Ent)
1505 Sprag := Get_Rep_Pragma (Btype, Name_Storage_Size);
1507 if Present (Sprag) then
1508 Error_Msg_Sloc := Sloc (Sprag);
1510 ("Storage_Size already specified#", Nam);
1515 Set_Has_Storage_Size_Clause (Btype);
1523 when Attribute_Stream_Size => Stream_Size : declare
1524 Size : constant Uint := Static_Integer (Expr);
1527 if Ada_Version <= Ada_95 then
1528 Check_Restriction (No_Implementation_Attributes, N);
1531 if Has_Stream_Size_Clause (U_Ent) then
1532 Error_Msg_N ("Stream_Size already given for &", Nam);
1534 elsif Is_Elementary_Type (U_Ent) then
1535 if Size /= System_Storage_Unit
1537 Size /= System_Storage_Unit * 2
1539 Size /= System_Storage_Unit * 4
1541 Size /= System_Storage_Unit * 8
1543 Error_Msg_Uint_1 := UI_From_Int (System_Storage_Unit);
1545 ("stream size for elementary type must be a"
1546 & " power of 2 and at least ^", N);
1548 elsif RM_Size (U_Ent) > Size then
1549 Error_Msg_Uint_1 := RM_Size (U_Ent);
1551 ("stream size for elementary type must be a"
1552 & " power of 2 and at least ^", N);
1555 Set_Has_Stream_Size_Clause (U_Ent);
1558 Error_Msg_N ("Stream_Size cannot be given for &", Nam);
1566 -- Value_Size attribute definition clause
1568 when Attribute_Value_Size => Value_Size : declare
1569 Size : constant Uint := Static_Integer (Expr);
1573 if not Is_Type (U_Ent) then
1574 Error_Msg_N ("Value_Size cannot be given for &", Nam);
1577 (Get_Attribute_Definition_Clause
1578 (U_Ent, Attribute_Value_Size))
1580 Error_Msg_N ("Value_Size already given for &", Nam);
1582 elsif Is_Array_Type (U_Ent)
1583 and then not Is_Constrained (U_Ent)
1586 ("Value_Size cannot be given for unconstrained array", Nam);
1589 if Is_Elementary_Type (U_Ent) then
1590 Check_Size (Expr, U_Ent, Size, Biased);
1591 Set_Has_Biased_Representation (U_Ent, Biased);
1594 Set_RM_Size (U_Ent, Size);
1602 when Attribute_Write =>
1603 Analyze_Stream_TSS_Definition (TSS_Stream_Write);
1604 Set_Has_Specified_Stream_Write (Ent);
1606 -- All other attributes cannot be set
1610 ("attribute& cannot be set with definition clause", N);
1613 -- The test for the type being frozen must be performed after
1614 -- any expression the clause has been analyzed since the expression
1615 -- itself might cause freezing that makes the clause illegal.
1617 if Rep_Item_Too_Late (U_Ent, N, FOnly) then
1620 end Analyze_Attribute_Definition_Clause;
1622 ----------------------------
1623 -- Analyze_Code_Statement --
1624 ----------------------------
1626 procedure Analyze_Code_Statement (N : Node_Id) is
1627 HSS : constant Node_Id := Parent (N);
1628 SBody : constant Node_Id := Parent (HSS);
1629 Subp : constant Entity_Id := Current_Scope;
1636 -- Analyze and check we get right type, note that this implements the
1637 -- requirement (RM 13.8(1)) that Machine_Code be with'ed, since that
1638 -- is the only way that Asm_Insn could possibly be visible.
1640 Analyze_And_Resolve (Expression (N));
1642 if Etype (Expression (N)) = Any_Type then
1644 elsif Etype (Expression (N)) /= RTE (RE_Asm_Insn) then
1645 Error_Msg_N ("incorrect type for code statement", N);
1649 Check_Code_Statement (N);
1651 -- Make sure we appear in the handled statement sequence of a
1652 -- subprogram (RM 13.8(3)).
1654 if Nkind (HSS) /= N_Handled_Sequence_Of_Statements
1655 or else Nkind (SBody) /= N_Subprogram_Body
1658 ("code statement can only appear in body of subprogram", N);
1662 -- Do remaining checks (RM 13.8(3)) if not already done
1664 if not Is_Machine_Code_Subprogram (Subp) then
1665 Set_Is_Machine_Code_Subprogram (Subp);
1667 -- No exception handlers allowed
1669 if Present (Exception_Handlers (HSS)) then
1671 ("exception handlers not permitted in machine code subprogram",
1672 First (Exception_Handlers (HSS)));
1675 -- No declarations other than use clauses and pragmas (we allow
1676 -- certain internally generated declarations as well).
1678 Decl := First (Declarations (SBody));
1679 while Present (Decl) loop
1680 DeclO := Original_Node (Decl);
1681 if Comes_From_Source (DeclO)
1682 and then Nkind (DeclO) /= N_Pragma
1683 and then Nkind (DeclO) /= N_Use_Package_Clause
1684 and then Nkind (DeclO) /= N_Use_Type_Clause
1685 and then Nkind (DeclO) /= N_Implicit_Label_Declaration
1688 ("this declaration not allowed in machine code subprogram",
1695 -- No statements other than code statements, pragmas, and labels.
1696 -- Again we allow certain internally generated statements.
1698 Stmt := First (Statements (HSS));
1699 while Present (Stmt) loop
1700 StmtO := Original_Node (Stmt);
1701 if Comes_From_Source (StmtO)
1702 and then Nkind (StmtO) /= N_Pragma
1703 and then Nkind (StmtO) /= N_Label
1704 and then Nkind (StmtO) /= N_Code_Statement
1707 ("this statement is not allowed in machine code subprogram",
1714 end Analyze_Code_Statement;
1716 -----------------------------------------------
1717 -- Analyze_Enumeration_Representation_Clause --
1718 -----------------------------------------------
1720 procedure Analyze_Enumeration_Representation_Clause (N : Node_Id) is
1721 Ident : constant Node_Id := Identifier (N);
1722 Aggr : constant Node_Id := Array_Aggregate (N);
1723 Enumtype : Entity_Id;
1729 Err : Boolean := False;
1731 Lo : constant Uint := Expr_Value (Type_Low_Bound (Universal_Integer));
1732 Hi : constant Uint := Expr_Value (Type_High_Bound (Universal_Integer));
1737 if Ignore_Rep_Clauses then
1741 -- First some basic error checks
1744 Enumtype := Entity (Ident);
1746 if Enumtype = Any_Type
1747 or else Rep_Item_Too_Early (Enumtype, N)
1751 Enumtype := Underlying_Type (Enumtype);
1754 if not Is_Enumeration_Type (Enumtype) then
1756 ("enumeration type required, found}",
1757 Ident, First_Subtype (Enumtype));
1761 -- Ignore rep clause on generic actual type. This will already have
1762 -- been flagged on the template as an error, and this is the safest
1763 -- way to ensure we don't get a junk cascaded message in the instance.
1765 if Is_Generic_Actual_Type (Enumtype) then
1768 -- Type must be in current scope
1770 elsif Scope (Enumtype) /= Current_Scope then
1771 Error_Msg_N ("type must be declared in this scope", Ident);
1774 -- Type must be a first subtype
1776 elsif not Is_First_Subtype (Enumtype) then
1777 Error_Msg_N ("cannot give enumeration rep clause for subtype", N);
1780 -- Ignore duplicate rep clause
1782 elsif Has_Enumeration_Rep_Clause (Enumtype) then
1783 Error_Msg_N ("duplicate enumeration rep clause ignored", N);
1786 -- Don't allow rep clause for standard [wide_[wide_]]character
1788 elsif Root_Type (Enumtype) = Standard_Character
1789 or else Root_Type (Enumtype) = Standard_Wide_Character
1790 or else Root_Type (Enumtype) = Standard_Wide_Wide_Character
1792 Error_Msg_N ("enumeration rep clause not allowed for this type", N);
1795 -- Check that the expression is a proper aggregate (no parentheses)
1797 elsif Paren_Count (Aggr) /= 0 then
1799 ("extra parentheses surrounding aggregate not allowed",
1803 -- All tests passed, so set rep clause in place
1806 Set_Has_Enumeration_Rep_Clause (Enumtype);
1807 Set_Has_Enumeration_Rep_Clause (Base_Type (Enumtype));
1810 -- Now we process the aggregate. Note that we don't use the normal
1811 -- aggregate code for this purpose, because we don't want any of the
1812 -- normal expansion activities, and a number of special semantic
1813 -- rules apply (including the component type being any integer type)
1815 Elit := First_Literal (Enumtype);
1817 -- First the positional entries if any
1819 if Present (Expressions (Aggr)) then
1820 Expr := First (Expressions (Aggr));
1821 while Present (Expr) loop
1823 Error_Msg_N ("too many entries in aggregate", Expr);
1827 Val := Static_Integer (Expr);
1829 -- Err signals that we found some incorrect entries processing
1830 -- the list. The final checks for completeness and ordering are
1831 -- skipped in this case.
1833 if Val = No_Uint then
1835 elsif Val < Lo or else Hi < Val then
1836 Error_Msg_N ("value outside permitted range", Expr);
1840 Set_Enumeration_Rep (Elit, Val);
1841 Set_Enumeration_Rep_Expr (Elit, Expr);
1847 -- Now process the named entries if present
1849 if Present (Component_Associations (Aggr)) then
1850 Assoc := First (Component_Associations (Aggr));
1851 while Present (Assoc) loop
1852 Choice := First (Choices (Assoc));
1854 if Present (Next (Choice)) then
1856 ("multiple choice not allowed here", Next (Choice));
1860 if Nkind (Choice) = N_Others_Choice then
1861 Error_Msg_N ("others choice not allowed here", Choice);
1864 elsif Nkind (Choice) = N_Range then
1865 -- ??? should allow zero/one element range here
1866 Error_Msg_N ("range not allowed here", Choice);
1870 Analyze_And_Resolve (Choice, Enumtype);
1872 if Is_Entity_Name (Choice)
1873 and then Is_Type (Entity (Choice))
1875 Error_Msg_N ("subtype name not allowed here", Choice);
1877 -- ??? should allow static subtype with zero/one entry
1879 elsif Etype (Choice) = Base_Type (Enumtype) then
1880 if not Is_Static_Expression (Choice) then
1881 Flag_Non_Static_Expr
1882 ("non-static expression used for choice!", Choice);
1886 Elit := Expr_Value_E (Choice);
1888 if Present (Enumeration_Rep_Expr (Elit)) then
1889 Error_Msg_Sloc := Sloc (Enumeration_Rep_Expr (Elit));
1891 ("representation for& previously given#",
1896 Set_Enumeration_Rep_Expr (Elit, Choice);
1898 Expr := Expression (Assoc);
1899 Val := Static_Integer (Expr);
1901 if Val = No_Uint then
1904 elsif Val < Lo or else Hi < Val then
1905 Error_Msg_N ("value outside permitted range", Expr);
1909 Set_Enumeration_Rep (Elit, Val);
1918 -- Aggregate is fully processed. Now we check that a full set of
1919 -- representations was given, and that they are in range and in order.
1920 -- These checks are only done if no other errors occurred.
1926 Elit := First_Literal (Enumtype);
1927 while Present (Elit) loop
1928 if No (Enumeration_Rep_Expr (Elit)) then
1929 Error_Msg_NE ("missing representation for&!", N, Elit);
1932 Val := Enumeration_Rep (Elit);
1934 if Min = No_Uint then
1938 if Val /= No_Uint then
1939 if Max /= No_Uint and then Val <= Max then
1941 ("enumeration value for& not ordered!",
1942 Enumeration_Rep_Expr (Elit), Elit);
1948 -- If there is at least one literal whose representation
1949 -- is not equal to the Pos value, then note that this
1950 -- enumeration type has a non-standard representation.
1952 if Val /= Enumeration_Pos (Elit) then
1953 Set_Has_Non_Standard_Rep (Base_Type (Enumtype));
1960 -- Now set proper size information
1963 Minsize : Uint := UI_From_Int (Minimum_Size (Enumtype));
1966 if Has_Size_Clause (Enumtype) then
1967 if Esize (Enumtype) >= Minsize then
1972 UI_From_Int (Minimum_Size (Enumtype, Biased => True));
1974 if Esize (Enumtype) < Minsize then
1975 Error_Msg_N ("previously given size is too small", N);
1978 Set_Has_Biased_Representation (Enumtype);
1983 Set_RM_Size (Enumtype, Minsize);
1984 Set_Enum_Esize (Enumtype);
1987 Set_RM_Size (Base_Type (Enumtype), RM_Size (Enumtype));
1988 Set_Esize (Base_Type (Enumtype), Esize (Enumtype));
1989 Set_Alignment (Base_Type (Enumtype), Alignment (Enumtype));
1993 -- We repeat the too late test in case it froze itself!
1995 if Rep_Item_Too_Late (Enumtype, N) then
1998 end Analyze_Enumeration_Representation_Clause;
2000 ----------------------------
2001 -- Analyze_Free_Statement --
2002 ----------------------------
2004 procedure Analyze_Free_Statement (N : Node_Id) is
2006 Analyze (Expression (N));
2007 end Analyze_Free_Statement;
2009 ------------------------------------------
2010 -- Analyze_Record_Representation_Clause --
2011 ------------------------------------------
2013 procedure Analyze_Record_Representation_Clause (N : Node_Id) is
2014 Loc : constant Source_Ptr := Sloc (N);
2015 Ident : constant Node_Id := Identifier (N);
2016 Rectype : Entity_Id;
2022 Hbit : Uint := Uint_0;
2027 Max_Bit_So_Far : Uint;
2028 -- Records the maximum bit position so far. If all field positions
2029 -- are monotonically increasing, then we can skip the circuit for
2030 -- checking for overlap, since no overlap is possible.
2032 Overlap_Check_Required : Boolean;
2033 -- Used to keep track of whether or not an overlap check is required
2035 Ccount : Natural := 0;
2036 -- Number of component clauses in record rep clause
2038 CR_Pragma : Node_Id := Empty;
2039 -- Points to N_Pragma node if Complete_Representation pragma present
2042 if Ignore_Rep_Clauses then
2047 Rectype := Entity (Ident);
2049 if Rectype = Any_Type
2050 or else Rep_Item_Too_Early (Rectype, N)
2054 Rectype := Underlying_Type (Rectype);
2057 -- First some basic error checks
2059 if not Is_Record_Type (Rectype) then
2061 ("record type required, found}", Ident, First_Subtype (Rectype));
2064 elsif Is_Unchecked_Union (Rectype) then
2066 ("record rep clause not allowed for Unchecked_Union", N);
2068 elsif Scope (Rectype) /= Current_Scope then
2069 Error_Msg_N ("type must be declared in this scope", N);
2072 elsif not Is_First_Subtype (Rectype) then
2073 Error_Msg_N ("cannot give record rep clause for subtype", N);
2076 elsif Has_Record_Rep_Clause (Rectype) then
2077 Error_Msg_N ("duplicate record rep clause ignored", N);
2080 elsif Rep_Item_Too_Late (Rectype, N) then
2084 if Present (Mod_Clause (N)) then
2086 Loc : constant Source_Ptr := Sloc (N);
2087 M : constant Node_Id := Mod_Clause (N);
2088 P : constant List_Id := Pragmas_Before (M);
2092 pragma Warnings (Off, Mod_Val);
2095 Check_Restriction (No_Obsolescent_Features, Mod_Clause (N));
2097 if Warn_On_Obsolescent_Feature then
2099 ("mod clause is an obsolescent feature (RM J.8)?", N);
2101 ("\use alignment attribute definition clause instead?", N);
2108 -- In ASIS_Mode mode, expansion is disabled, but we must convert
2109 -- the Mod clause into an alignment clause anyway, so that the
2110 -- back-end can compute and back-annotate properly the size and
2111 -- alignment of types that may include this record.
2113 -- This seems dubious, this destroys the source tree in a manner
2114 -- not detectable by ASIS ???
2116 if Operating_Mode = Check_Semantics
2120 Make_Attribute_Definition_Clause (Loc,
2121 Name => New_Reference_To (Base_Type (Rectype), Loc),
2122 Chars => Name_Alignment,
2123 Expression => Relocate_Node (Expression (M)));
2125 Set_From_At_Mod (AtM_Nod);
2126 Insert_After (N, AtM_Nod);
2127 Mod_Val := Get_Alignment_Value (Expression (AtM_Nod));
2128 Set_Mod_Clause (N, Empty);
2131 -- Get the alignment value to perform error checking
2133 Mod_Val := Get_Alignment_Value (Expression (M));
2139 -- Clear any existing component clauses for the type (this happens with
2140 -- derived types, where we are now overriding the original)
2142 Comp := First_Component_Or_Discriminant (Rectype);
2143 while Present (Comp) loop
2144 Set_Component_Clause (Comp, Empty);
2145 Next_Component_Or_Discriminant (Comp);
2148 -- All done if no component clauses
2150 CC := First (Component_Clauses (N));
2156 -- If a tag is present, then create a component clause that places it
2157 -- at the start of the record (otherwise gigi may place it after other
2158 -- fields that have rep clauses).
2160 Fent := First_Entity (Rectype);
2162 if Nkind (Fent) = N_Defining_Identifier
2163 and then Chars (Fent) = Name_uTag
2165 Set_Component_Bit_Offset (Fent, Uint_0);
2166 Set_Normalized_Position (Fent, Uint_0);
2167 Set_Normalized_First_Bit (Fent, Uint_0);
2168 Set_Normalized_Position_Max (Fent, Uint_0);
2169 Init_Esize (Fent, System_Address_Size);
2171 Set_Component_Clause (Fent,
2172 Make_Component_Clause (Loc,
2174 Make_Identifier (Loc,
2175 Chars => Name_uTag),
2178 Make_Integer_Literal (Loc,
2182 Make_Integer_Literal (Loc,
2186 Make_Integer_Literal (Loc,
2187 UI_From_Int (System_Address_Size))));
2189 Ccount := Ccount + 1;
2192 -- A representation like this applies to the base type
2194 Set_Has_Record_Rep_Clause (Base_Type (Rectype));
2195 Set_Has_Non_Standard_Rep (Base_Type (Rectype));
2196 Set_Has_Specified_Layout (Base_Type (Rectype));
2198 Max_Bit_So_Far := Uint_Minus_1;
2199 Overlap_Check_Required := False;
2201 -- Process the component clauses
2203 while Present (CC) loop
2207 if Nkind (CC) = N_Pragma then
2210 -- The only pragma of interest is Complete_Representation
2212 if Chars (CC) = Name_Complete_Representation then
2216 -- Processing for real component clause
2219 Ccount := Ccount + 1;
2220 Posit := Static_Integer (Position (CC));
2221 Fbit := Static_Integer (First_Bit (CC));
2222 Lbit := Static_Integer (Last_Bit (CC));
2225 and then Fbit /= No_Uint
2226 and then Lbit /= No_Uint
2230 ("position cannot be negative", Position (CC));
2234 ("first bit cannot be negative", First_Bit (CC));
2236 -- Values look OK, so find the corresponding record component
2237 -- Even though the syntax allows an attribute reference for
2238 -- implementation-defined components, GNAT does not allow the
2239 -- tag to get an explicit position.
2241 elsif Nkind (Component_Name (CC)) = N_Attribute_Reference then
2242 if Attribute_Name (Component_Name (CC)) = Name_Tag then
2243 Error_Msg_N ("position of tag cannot be specified", CC);
2245 Error_Msg_N ("illegal component name", CC);
2249 Comp := First_Entity (Rectype);
2250 while Present (Comp) loop
2251 exit when Chars (Comp) = Chars (Component_Name (CC));
2257 -- Maybe component of base type that is absent from
2258 -- statically constrained first subtype.
2260 Comp := First_Entity (Base_Type (Rectype));
2261 while Present (Comp) loop
2262 exit when Chars (Comp) = Chars (Component_Name (CC));
2269 ("component clause is for non-existent field", CC);
2271 elsif Present (Component_Clause (Comp)) then
2272 Error_Msg_Sloc := Sloc (Component_Clause (Comp));
2274 ("component clause previously given#", CC);
2277 -- Update Fbit and Lbit to the actual bit number
2279 Fbit := Fbit + UI_From_Int (SSU) * Posit;
2280 Lbit := Lbit + UI_From_Int (SSU) * Posit;
2282 if Fbit <= Max_Bit_So_Far then
2283 Overlap_Check_Required := True;
2285 Max_Bit_So_Far := Lbit;
2288 if Has_Size_Clause (Rectype)
2289 and then Esize (Rectype) <= Lbit
2292 ("bit number out of range of specified size",
2295 Set_Component_Clause (Comp, CC);
2296 Set_Component_Bit_Offset (Comp, Fbit);
2297 Set_Esize (Comp, 1 + (Lbit - Fbit));
2298 Set_Normalized_First_Bit (Comp, Fbit mod SSU);
2299 Set_Normalized_Position (Comp, Fbit / SSU);
2301 Set_Normalized_Position_Max
2302 (Fent, Normalized_Position (Fent));
2304 if Is_Tagged_Type (Rectype)
2305 and then Fbit < System_Address_Size
2308 ("component overlaps tag field of&",
2312 -- This information is also set in the corresponding
2313 -- component of the base type, found by accessing the
2314 -- Original_Record_Component link if it is present.
2316 Ocomp := Original_Record_Component (Comp);
2323 (Component_Name (CC),
2328 Set_Has_Biased_Representation (Comp, Biased);
2330 if Present (Ocomp) then
2331 Set_Component_Clause (Ocomp, CC);
2332 Set_Component_Bit_Offset (Ocomp, Fbit);
2333 Set_Normalized_First_Bit (Ocomp, Fbit mod SSU);
2334 Set_Normalized_Position (Ocomp, Fbit / SSU);
2335 Set_Esize (Ocomp, 1 + (Lbit - Fbit));
2337 Set_Normalized_Position_Max
2338 (Ocomp, Normalized_Position (Ocomp));
2340 Set_Has_Biased_Representation
2341 (Ocomp, Has_Biased_Representation (Comp));
2344 if Esize (Comp) < 0 then
2345 Error_Msg_N ("component size is negative", CC);
2356 -- Now that we have processed all the component clauses, check for
2357 -- overlap. We have to leave this till last, since the components
2358 -- can appear in any arbitrary order in the representation clause.
2360 -- We do not need this check if all specified ranges were monotonic,
2361 -- as recorded by Overlap_Check_Required being False at this stage.
2363 -- This first section checks if there are any overlapping entries
2364 -- at all. It does this by sorting all entries and then seeing if
2365 -- there are any overlaps. If there are none, then that is decisive,
2366 -- but if there are overlaps, they may still be OK (they may result
2367 -- from fields in different variants).
2369 if Overlap_Check_Required then
2370 Overlap_Check1 : declare
2372 OC_Fbit : array (0 .. Ccount) of Uint;
2373 -- First-bit values for component clauses, the value is the
2374 -- offset of the first bit of the field from start of record.
2375 -- The zero entry is for use in sorting.
2377 OC_Lbit : array (0 .. Ccount) of Uint;
2378 -- Last-bit values for component clauses, the value is the
2379 -- offset of the last bit of the field from start of record.
2380 -- The zero entry is for use in sorting.
2382 OC_Count : Natural := 0;
2383 -- Count of entries in OC_Fbit and OC_Lbit
2385 function OC_Lt (Op1, Op2 : Natural) return Boolean;
2386 -- Compare routine for Sort (See GNAT.Heap_Sort_A)
2388 procedure OC_Move (From : Natural; To : Natural);
2389 -- Move routine for Sort (see GNAT.Heap_Sort_A)
2391 function OC_Lt (Op1, Op2 : Natural) return Boolean is
2393 return OC_Fbit (Op1) < OC_Fbit (Op2);
2396 procedure OC_Move (From : Natural; To : Natural) is
2398 OC_Fbit (To) := OC_Fbit (From);
2399 OC_Lbit (To) := OC_Lbit (From);
2403 CC := First (Component_Clauses (N));
2404 while Present (CC) loop
2405 if Nkind (CC) /= N_Pragma then
2406 Posit := Static_Integer (Position (CC));
2407 Fbit := Static_Integer (First_Bit (CC));
2408 Lbit := Static_Integer (Last_Bit (CC));
2411 and then Fbit /= No_Uint
2412 and then Lbit /= No_Uint
2414 OC_Count := OC_Count + 1;
2415 Posit := Posit * SSU;
2416 OC_Fbit (OC_Count) := Fbit + Posit;
2417 OC_Lbit (OC_Count) := Lbit + Posit;
2426 OC_Move'Unrestricted_Access,
2427 OC_Lt'Unrestricted_Access);
2429 Overlap_Check_Required := False;
2430 for J in 1 .. OC_Count - 1 loop
2431 if OC_Lbit (J) >= OC_Fbit (J + 1) then
2432 Overlap_Check_Required := True;
2439 -- If Overlap_Check_Required is still True, then we have to do
2440 -- the full scale overlap check, since we have at least two fields
2441 -- that do overlap, and we need to know if that is OK since they
2442 -- are in the same variant, or whether we have a definite problem
2444 if Overlap_Check_Required then
2445 Overlap_Check2 : declare
2446 C1_Ent, C2_Ent : Entity_Id;
2447 -- Entities of components being checked for overlap
2450 -- Component_List node whose Component_Items are being checked
2453 -- Component declaration for component being checked
2456 C1_Ent := First_Entity (Base_Type (Rectype));
2458 -- Loop through all components in record. For each component check
2459 -- for overlap with any of the preceding elements on the component
2460 -- list containing the component, and also, if the component is in
2461 -- a variant, check against components outside the case structure.
2462 -- This latter test is repeated recursively up the variant tree.
2464 Main_Component_Loop : while Present (C1_Ent) loop
2465 if Ekind (C1_Ent) /= E_Component
2466 and then Ekind (C1_Ent) /= E_Discriminant
2468 goto Continue_Main_Component_Loop;
2471 -- Skip overlap check if entity has no declaration node. This
2472 -- happens with discriminants in constrained derived types.
2473 -- Probably we are missing some checks as a result, but that
2474 -- does not seem terribly serious ???
2476 if No (Declaration_Node (C1_Ent)) then
2477 goto Continue_Main_Component_Loop;
2480 Clist := Parent (List_Containing (Declaration_Node (C1_Ent)));
2482 -- Loop through component lists that need checking. Check the
2483 -- current component list and all lists in variants above us.
2485 Component_List_Loop : loop
2487 -- If derived type definition, go to full declaration
2488 -- If at outer level, check discriminants if there are any
2490 if Nkind (Clist) = N_Derived_Type_Definition then
2491 Clist := Parent (Clist);
2494 -- Outer level of record definition, check discriminants
2496 if Nkind (Clist) = N_Full_Type_Declaration
2497 or else Nkind (Clist) = N_Private_Type_Declaration
2499 if Has_Discriminants (Defining_Identifier (Clist)) then
2501 First_Discriminant (Defining_Identifier (Clist));
2503 while Present (C2_Ent) loop
2504 exit when C1_Ent = C2_Ent;
2505 Check_Component_Overlap (C1_Ent, C2_Ent);
2506 Next_Discriminant (C2_Ent);
2510 -- Record extension case
2512 elsif Nkind (Clist) = N_Derived_Type_Definition then
2515 -- Otherwise check one component list
2518 Citem := First (Component_Items (Clist));
2520 while Present (Citem) loop
2521 if Nkind (Citem) = N_Component_Declaration then
2522 C2_Ent := Defining_Identifier (Citem);
2523 exit when C1_Ent = C2_Ent;
2524 Check_Component_Overlap (C1_Ent, C2_Ent);
2531 -- Check for variants above us (the parent of the Clist can
2532 -- be a variant, in which case its parent is a variant part,
2533 -- and the parent of the variant part is a component list
2534 -- whose components must all be checked against the current
2535 -- component for overlap.
2537 if Nkind (Parent (Clist)) = N_Variant then
2538 Clist := Parent (Parent (Parent (Clist)));
2540 -- Check for possible discriminant part in record, this is
2541 -- treated essentially as another level in the recursion.
2542 -- For this case we have the parent of the component list
2543 -- is the record definition, and its parent is the full
2544 -- type declaration which contains the discriminant
2547 elsif Nkind (Parent (Clist)) = N_Record_Definition then
2548 Clist := Parent (Parent ((Clist)));
2550 -- If neither of these two cases, we are at the top of
2554 exit Component_List_Loop;
2556 end loop Component_List_Loop;
2558 <<Continue_Main_Component_Loop>>
2559 Next_Entity (C1_Ent);
2561 end loop Main_Component_Loop;
2565 -- For records that have component clauses for all components, and
2566 -- whose size is less than or equal to 32, we need to know the size
2567 -- in the front end to activate possible packed array processing
2568 -- where the component type is a record.
2570 -- At this stage Hbit + 1 represents the first unused bit from all
2571 -- the component clauses processed, so if the component clauses are
2572 -- complete, then this is the length of the record.
2574 -- For records longer than System.Storage_Unit, and for those where
2575 -- not all components have component clauses, the back end determines
2576 -- the length (it may for example be appopriate to round up the size
2577 -- to some convenient boundary, based on alignment considerations etc).
2579 if Unknown_RM_Size (Rectype)
2580 and then Hbit + 1 <= 32
2582 -- Nothing to do if at least one component with no component clause
2584 Comp := First_Component_Or_Discriminant (Rectype);
2585 while Present (Comp) loop
2586 exit when No (Component_Clause (Comp));
2587 Next_Component_Or_Discriminant (Comp);
2590 -- If we fall out of loop, all components have component clauses
2591 -- and so we can set the size to the maximum value.
2594 Set_RM_Size (Rectype, Hbit + 1);
2598 -- Check missing components if Complete_Representation pragma appeared
2600 if Present (CR_Pragma) then
2601 Comp := First_Component_Or_Discriminant (Rectype);
2602 while Present (Comp) loop
2603 if No (Component_Clause (Comp)) then
2605 ("missing component clause for &", CR_Pragma, Comp);
2608 Next_Component_Or_Discriminant (Comp);
2611 -- If no Complete_Representation pragma, warn if missing components
2613 elsif Warn_On_Unrepped_Components
2614 and then not Warnings_Off (Rectype)
2617 Num_Repped_Components : Nat := 0;
2618 Num_Unrepped_Components : Nat := 0;
2621 -- First count number of repped and unrepped components
2623 Comp := First_Component_Or_Discriminant (Rectype);
2624 while Present (Comp) loop
2625 if Present (Component_Clause (Comp)) then
2626 Num_Repped_Components := Num_Repped_Components + 1;
2628 Num_Unrepped_Components := Num_Unrepped_Components + 1;
2631 Next_Component_Or_Discriminant (Comp);
2634 -- We are only interested in the case where there is at least one
2635 -- unrepped component, and at least half the components have rep
2636 -- clauses. We figure that if less than half have them, then the
2637 -- partial rep clause is really intentional.
2639 if Num_Unrepped_Components > 0
2640 and then Num_Unrepped_Components < Num_Repped_Components
2642 Comp := First_Component_Or_Discriminant (Rectype);
2643 while Present (Comp) loop
2644 if No (Component_Clause (Comp)) then
2645 Error_Msg_Sloc := Sloc (Comp);
2647 ("?no component clause given for & declared #",
2651 Next_Component_Or_Discriminant (Comp);
2656 end Analyze_Record_Representation_Clause;
2658 -----------------------------
2659 -- Check_Component_Overlap --
2660 -----------------------------
2662 procedure Check_Component_Overlap (C1_Ent, C2_Ent : Entity_Id) is
2664 if Present (Component_Clause (C1_Ent))
2665 and then Present (Component_Clause (C2_Ent))
2667 -- Exclude odd case where we have two tag fields in the same
2668 -- record, both at location zero. This seems a bit strange,
2669 -- but it seems to happen in some circumstances ???
2671 if Chars (C1_Ent) = Name_uTag
2672 and then Chars (C2_Ent) = Name_uTag
2677 -- Here we check if the two fields overlap
2680 S1 : constant Uint := Component_Bit_Offset (C1_Ent);
2681 S2 : constant Uint := Component_Bit_Offset (C2_Ent);
2682 E1 : constant Uint := S1 + Esize (C1_Ent);
2683 E2 : constant Uint := S2 + Esize (C2_Ent);
2686 if E2 <= S1 or else E1 <= S2 then
2690 Component_Name (Component_Clause (C2_Ent));
2691 Error_Msg_Sloc := Sloc (Error_Msg_Node_2);
2693 Component_Name (Component_Clause (C1_Ent));
2695 ("component& overlaps & #",
2696 Component_Name (Component_Clause (C1_Ent)));
2700 end Check_Component_Overlap;
2702 -----------------------------------
2703 -- Check_Constant_Address_Clause --
2704 -----------------------------------
2706 procedure Check_Constant_Address_Clause
2710 procedure Check_At_Constant_Address (Nod : Node_Id);
2711 -- Checks that the given node N represents a name whose 'Address
2712 -- is constant (in the same sense as OK_Constant_Address_Clause,
2713 -- i.e. the address value is the same at the point of declaration
2714 -- of U_Ent and at the time of elaboration of the address clause.
2716 procedure Check_Expr_Constants (Nod : Node_Id);
2717 -- Checks that Nod meets the requirements for a constant address
2718 -- clause in the sense of the enclosing procedure.
2720 procedure Check_List_Constants (Lst : List_Id);
2721 -- Check that all elements of list Lst meet the requirements for a
2722 -- constant address clause in the sense of the enclosing procedure.
2724 -------------------------------
2725 -- Check_At_Constant_Address --
2726 -------------------------------
2728 procedure Check_At_Constant_Address (Nod : Node_Id) is
2730 if Is_Entity_Name (Nod) then
2731 if Present (Address_Clause (Entity ((Nod)))) then
2733 ("invalid address clause for initialized object &!",
2736 ("address for& cannot" &
2737 " depend on another address clause! (RM 13.1(22))!",
2740 elsif In_Same_Source_Unit (Entity (Nod), U_Ent)
2741 and then Sloc (U_Ent) < Sloc (Entity (Nod))
2744 ("invalid address clause for initialized object &!",
2746 Error_Msg_Name_1 := Chars (Entity (Nod));
2747 Error_Msg_Name_2 := Chars (U_Ent);
2749 ("\% must be defined before % (RM 13.1(22))!",
2753 elsif Nkind (Nod) = N_Selected_Component then
2755 T : constant Entity_Id := Etype (Prefix (Nod));
2758 if (Is_Record_Type (T)
2759 and then Has_Discriminants (T))
2762 and then Is_Record_Type (Designated_Type (T))
2763 and then Has_Discriminants (Designated_Type (T)))
2766 ("invalid address clause for initialized object &!",
2769 ("\address cannot depend on component" &
2770 " of discriminated record (RM 13.1(22))!",
2773 Check_At_Constant_Address (Prefix (Nod));
2777 elsif Nkind (Nod) = N_Indexed_Component then
2778 Check_At_Constant_Address (Prefix (Nod));
2779 Check_List_Constants (Expressions (Nod));
2782 Check_Expr_Constants (Nod);
2784 end Check_At_Constant_Address;
2786 --------------------------
2787 -- Check_Expr_Constants --
2788 --------------------------
2790 procedure Check_Expr_Constants (Nod : Node_Id) is
2791 Loc_U_Ent : constant Source_Ptr := Sloc (U_Ent);
2792 Ent : Entity_Id := Empty;
2795 if Nkind (Nod) in N_Has_Etype
2796 and then Etype (Nod) = Any_Type
2802 when N_Empty | N_Error =>
2805 when N_Identifier | N_Expanded_Name =>
2806 Ent := Entity (Nod);
2808 -- We need to look at the original node if it is different
2809 -- from the node, since we may have rewritten things and
2810 -- substituted an identifier representing the rewrite.
2812 if Original_Node (Nod) /= Nod then
2813 Check_Expr_Constants (Original_Node (Nod));
2815 -- If the node is an object declaration without initial
2816 -- value, some code has been expanded, and the expression
2817 -- is not constant, even if the constituents might be
2818 -- acceptable, as in A'Address + offset.
2820 if Ekind (Ent) = E_Variable
2821 and then Nkind (Declaration_Node (Ent))
2822 = N_Object_Declaration
2824 No (Expression (Declaration_Node (Ent)))
2827 ("invalid address clause for initialized object &!",
2830 -- If entity is constant, it may be the result of expanding
2831 -- a check. We must verify that its declaration appears
2832 -- before the object in question, else we also reject the
2835 elsif Ekind (Ent) = E_Constant
2836 and then In_Same_Source_Unit (Ent, U_Ent)
2837 and then Sloc (Ent) > Loc_U_Ent
2840 ("invalid address clause for initialized object &!",
2847 -- Otherwise look at the identifier and see if it is OK
2849 if Ekind (Ent) = E_Named_Integer
2851 Ekind (Ent) = E_Named_Real
2858 Ekind (Ent) = E_Constant
2860 Ekind (Ent) = E_In_Parameter
2862 -- This is the case where we must have Ent defined
2863 -- before U_Ent. Clearly if they are in different
2864 -- units this requirement is met since the unit
2865 -- containing Ent is already processed.
2867 if not In_Same_Source_Unit (Ent, U_Ent) then
2870 -- Otherwise location of Ent must be before the
2871 -- location of U_Ent, that's what prior defined means.
2873 elsif Sloc (Ent) < Loc_U_Ent then
2878 ("invalid address clause for initialized object &!",
2880 Error_Msg_Name_1 := Chars (Ent);
2881 Error_Msg_Name_2 := Chars (U_Ent);
2883 ("\% must be defined before % (RM 13.1(22))!",
2887 elsif Nkind (Original_Node (Nod)) = N_Function_Call then
2888 Check_Expr_Constants (Original_Node (Nod));
2892 ("invalid address clause for initialized object &!",
2895 if Comes_From_Source (Ent) then
2896 Error_Msg_Name_1 := Chars (Ent);
2898 ("\reference to variable% not allowed"
2899 & " (RM 13.1(22))!", Nod);
2902 ("non-static expression not allowed"
2903 & " (RM 13.1(22))!", Nod);
2907 when N_Integer_Literal =>
2909 -- If this is a rewritten unchecked conversion, in a system
2910 -- where Address is an integer type, always use the base type
2911 -- for a literal value. This is user-friendly and prevents
2912 -- order-of-elaboration issues with instances of unchecked
2915 if Nkind (Original_Node (Nod)) = N_Function_Call then
2916 Set_Etype (Nod, Base_Type (Etype (Nod)));
2919 when N_Real_Literal |
2921 N_Character_Literal =>
2925 Check_Expr_Constants (Low_Bound (Nod));
2926 Check_Expr_Constants (High_Bound (Nod));
2928 when N_Explicit_Dereference =>
2929 Check_Expr_Constants (Prefix (Nod));
2931 when N_Indexed_Component =>
2932 Check_Expr_Constants (Prefix (Nod));
2933 Check_List_Constants (Expressions (Nod));
2936 Check_Expr_Constants (Prefix (Nod));
2937 Check_Expr_Constants (Discrete_Range (Nod));
2939 when N_Selected_Component =>
2940 Check_Expr_Constants (Prefix (Nod));
2942 when N_Attribute_Reference =>
2943 if Attribute_Name (Nod) = Name_Address
2945 Attribute_Name (Nod) = Name_Access
2947 Attribute_Name (Nod) = Name_Unchecked_Access
2949 Attribute_Name (Nod) = Name_Unrestricted_Access
2951 Check_At_Constant_Address (Prefix (Nod));
2954 Check_Expr_Constants (Prefix (Nod));
2955 Check_List_Constants (Expressions (Nod));
2959 Check_List_Constants (Component_Associations (Nod));
2960 Check_List_Constants (Expressions (Nod));
2962 when N_Component_Association =>
2963 Check_Expr_Constants (Expression (Nod));
2965 when N_Extension_Aggregate =>
2966 Check_Expr_Constants (Ancestor_Part (Nod));
2967 Check_List_Constants (Component_Associations (Nod));
2968 Check_List_Constants (Expressions (Nod));
2973 when N_Binary_Op | N_And_Then | N_Or_Else | N_Membership_Test =>
2974 Check_Expr_Constants (Left_Opnd (Nod));
2975 Check_Expr_Constants (Right_Opnd (Nod));
2978 Check_Expr_Constants (Right_Opnd (Nod));
2980 when N_Type_Conversion |
2981 N_Qualified_Expression |
2983 Check_Expr_Constants (Expression (Nod));
2985 when N_Unchecked_Type_Conversion =>
2986 Check_Expr_Constants (Expression (Nod));
2988 -- If this is a rewritten unchecked conversion, subtypes
2989 -- in this node are those created within the instance.
2990 -- To avoid order of elaboration issues, replace them
2991 -- with their base types. Note that address clauses can
2992 -- cause order of elaboration problems because they are
2993 -- elaborated by the back-end at the point of definition,
2994 -- and may mention entities declared in between (as long
2995 -- as everything is static). It is user-friendly to allow
2996 -- unchecked conversions in this context.
2998 if Nkind (Original_Node (Nod)) = N_Function_Call then
2999 Set_Etype (Expression (Nod),
3000 Base_Type (Etype (Expression (Nod))));
3001 Set_Etype (Nod, Base_Type (Etype (Nod)));
3004 when N_Function_Call =>
3005 if not Is_Pure (Entity (Name (Nod))) then
3007 ("invalid address clause for initialized object &!",
3011 ("\function & is not pure (RM 13.1(22))!",
3012 Nod, Entity (Name (Nod)));
3015 Check_List_Constants (Parameter_Associations (Nod));
3018 when N_Parameter_Association =>
3019 Check_Expr_Constants (Explicit_Actual_Parameter (Nod));
3023 ("invalid address clause for initialized object &!",
3026 ("\must be constant defined before& (RM 13.1(22))!",
3029 end Check_Expr_Constants;
3031 --------------------------
3032 -- Check_List_Constants --
3033 --------------------------
3035 procedure Check_List_Constants (Lst : List_Id) is
3039 if Present (Lst) then
3040 Nod1 := First (Lst);
3041 while Present (Nod1) loop
3042 Check_Expr_Constants (Nod1);
3046 end Check_List_Constants;
3048 -- Start of processing for Check_Constant_Address_Clause
3051 Check_Expr_Constants (Expr);
3052 end Check_Constant_Address_Clause;
3058 procedure Check_Size
3062 Biased : out Boolean)
3064 UT : constant Entity_Id := Underlying_Type (T);
3070 -- Dismiss cases for generic types or types with previous errors
3073 or else UT = Any_Type
3074 or else Is_Generic_Type (UT)
3075 or else Is_Generic_Type (Root_Type (UT))
3079 -- Check case of bit packed array
3081 elsif Is_Array_Type (UT)
3082 and then Known_Static_Component_Size (UT)
3083 and then Is_Bit_Packed_Array (UT)
3091 Asiz := Component_Size (UT);
3092 Indx := First_Index (UT);
3094 Ityp := Etype (Indx);
3096 -- If non-static bound, then we are not in the business of
3097 -- trying to check the length, and indeed an error will be
3098 -- issued elsewhere, since sizes of non-static array types
3099 -- cannot be set implicitly or explicitly.
3101 if not Is_Static_Subtype (Ityp) then
3105 -- Otherwise accumulate next dimension
3107 Asiz := Asiz * (Expr_Value (Type_High_Bound (Ityp)) -
3108 Expr_Value (Type_Low_Bound (Ityp)) +
3112 exit when No (Indx);
3118 Error_Msg_Uint_1 := Asiz;
3120 ("size for& too small, minimum allowed is ^", N, T);
3121 Set_Esize (T, Asiz);
3122 Set_RM_Size (T, Asiz);
3126 -- All other composite types are ignored
3128 elsif Is_Composite_Type (UT) then
3131 -- For fixed-point types, don't check minimum if type is not frozen,
3132 -- since we don't know all the characteristics of the type that can
3133 -- affect the size (e.g. a specified small) till freeze time.
3135 elsif Is_Fixed_Point_Type (UT)
3136 and then not Is_Frozen (UT)
3140 -- Cases for which a minimum check is required
3143 -- Ignore if specified size is correct for the type
3145 if Known_Esize (UT) and then Siz = Esize (UT) then
3149 -- Otherwise get minimum size
3151 M := UI_From_Int (Minimum_Size (UT));
3155 -- Size is less than minimum size, but one possibility remains
3156 -- that we can manage with the new size if we bias the type
3158 M := UI_From_Int (Minimum_Size (UT, Biased => True));
3161 Error_Msg_Uint_1 := M;
3163 ("size for& too small, minimum allowed is ^", N, T);
3173 -------------------------
3174 -- Get_Alignment_Value --
3175 -------------------------
3177 function Get_Alignment_Value (Expr : Node_Id) return Uint is
3178 Align : constant Uint := Static_Integer (Expr);
3181 if Align = No_Uint then
3184 elsif Align <= 0 then
3185 Error_Msg_N ("alignment value must be positive", Expr);
3189 for J in Int range 0 .. 64 loop
3191 M : constant Uint := Uint_2 ** J;
3194 exit when M = Align;
3198 ("alignment value must be power of 2", Expr);
3206 end Get_Alignment_Value;
3212 procedure Initialize is
3214 Unchecked_Conversions.Init;
3217 -------------------------
3218 -- Is_Operational_Item --
3219 -------------------------
3221 function Is_Operational_Item (N : Node_Id) return Boolean is
3223 if Nkind (N) /= N_Attribute_Definition_Clause then
3227 Id : constant Attribute_Id := Get_Attribute_Id (Chars (N));
3230 return Id = Attribute_Input
3231 or else Id = Attribute_Output
3232 or else Id = Attribute_Read
3233 or else Id = Attribute_Write
3234 or else Id = Attribute_External_Tag;
3237 end Is_Operational_Item;
3239 --------------------------------------
3240 -- Mark_Aliased_Address_As_Volatile --
3241 --------------------------------------
3243 procedure Mark_Aliased_Address_As_Volatile (N : Node_Id) is
3244 Ent : constant Entity_Id := Address_Aliased_Entity (N);
3247 if Present (Ent) then
3248 Set_Treat_As_Volatile (Ent);
3250 end Mark_Aliased_Address_As_Volatile;
3256 function Minimum_Size
3258 Biased : Boolean := False) return Nat
3260 Lo : Uint := No_Uint;
3261 Hi : Uint := No_Uint;
3262 LoR : Ureal := No_Ureal;
3263 HiR : Ureal := No_Ureal;
3264 LoSet : Boolean := False;
3265 HiSet : Boolean := False;
3269 R_Typ : constant Entity_Id := Root_Type (T);
3272 -- If bad type, return 0
3274 if T = Any_Type then
3277 -- For generic types, just return zero. There cannot be any legitimate
3278 -- need to know such a size, but this routine may be called with a
3279 -- generic type as part of normal processing.
3281 elsif Is_Generic_Type (R_Typ)
3282 or else R_Typ = Any_Type
3286 -- Access types. Normally an access type cannot have a size smaller
3287 -- than the size of System.Address. The exception is on VMS, where
3288 -- we have short and long addresses, and it is possible for an access
3289 -- type to have a short address size (and thus be less than the size
3290 -- of System.Address itself). We simply skip the check for VMS, and
3291 -- leave the back end to do the check.
3293 elsif Is_Access_Type (T) then
3294 if OpenVMS_On_Target then
3297 return System_Address_Size;
3300 -- Floating-point types
3302 elsif Is_Floating_Point_Type (T) then
3303 return UI_To_Int (Esize (R_Typ));
3307 elsif Is_Discrete_Type (T) then
3309 -- The following loop is looking for the nearest compile time
3310 -- known bounds following the ancestor subtype chain. The idea
3311 -- is to find the most restrictive known bounds information.
3315 if Ancest = Any_Type or else Etype (Ancest) = Any_Type then
3320 if Compile_Time_Known_Value (Type_Low_Bound (Ancest)) then
3321 Lo := Expr_Rep_Value (Type_Low_Bound (Ancest));
3328 if Compile_Time_Known_Value (Type_High_Bound (Ancest)) then
3329 Hi := Expr_Rep_Value (Type_High_Bound (Ancest));
3335 Ancest := Ancestor_Subtype (Ancest);
3338 Ancest := Base_Type (T);
3340 if Is_Generic_Type (Ancest) then
3346 -- Fixed-point types. We can't simply use Expr_Value to get the
3347 -- Corresponding_Integer_Value values of the bounds, since these
3348 -- do not get set till the type is frozen, and this routine can
3349 -- be called before the type is frozen. Similarly the test for
3350 -- bounds being static needs to include the case where we have
3351 -- unanalyzed real literals for the same reason.
3353 elsif Is_Fixed_Point_Type (T) then
3355 -- The following loop is looking for the nearest compile time
3356 -- known bounds following the ancestor subtype chain. The idea
3357 -- is to find the most restrictive known bounds information.
3361 if Ancest = Any_Type or else Etype (Ancest) = Any_Type then
3366 if Nkind (Type_Low_Bound (Ancest)) = N_Real_Literal
3367 or else Compile_Time_Known_Value (Type_Low_Bound (Ancest))
3369 LoR := Expr_Value_R (Type_Low_Bound (Ancest));
3376 if Nkind (Type_High_Bound (Ancest)) = N_Real_Literal
3377 or else Compile_Time_Known_Value (Type_High_Bound (Ancest))
3379 HiR := Expr_Value_R (Type_High_Bound (Ancest));
3385 Ancest := Ancestor_Subtype (Ancest);
3388 Ancest := Base_Type (T);
3390 if Is_Generic_Type (Ancest) then
3396 Lo := UR_To_Uint (LoR / Small_Value (T));
3397 Hi := UR_To_Uint (HiR / Small_Value (T));
3399 -- No other types allowed
3402 raise Program_Error;
3405 -- Fall through with Hi and Lo set. Deal with biased case
3407 if (Biased and then not Is_Fixed_Point_Type (T))
3408 or else Has_Biased_Representation (T)
3414 -- Signed case. Note that we consider types like range 1 .. -1 to be
3415 -- signed for the purpose of computing the size, since the bounds
3416 -- have to be accomodated in the base type.
3418 if Lo < 0 or else Hi < 0 then
3422 -- S = size, B = 2 ** (size - 1) (can accommodate -B .. +(B - 1))
3423 -- Note that we accommodate the case where the bounds cross. This
3424 -- can happen either because of the way the bounds are declared
3425 -- or because of the algorithm in Freeze_Fixed_Point_Type.
3439 -- If both bounds are positive, make sure that both are represen-
3440 -- table in the case where the bounds are crossed. This can happen
3441 -- either because of the way the bounds are declared, or because of
3442 -- the algorithm in Freeze_Fixed_Point_Type.
3448 -- S = size, (can accommodate 0 .. (2**size - 1))
3451 while Hi >= Uint_2 ** S loop
3459 ---------------------------
3460 -- New_Stream_Subprogram --
3461 ---------------------------
3463 procedure New_Stream_Subprogram
3467 Nam : TSS_Name_Type)
3469 Loc : constant Source_Ptr := Sloc (N);
3470 Sname : constant Name_Id := Make_TSS_Name (Base_Type (Ent), Nam);
3471 Subp_Id : Entity_Id;
3472 Subp_Decl : Node_Id;
3476 Defer_Declaration : constant Boolean :=
3477 Is_Tagged_Type (Ent) or else Is_Private_Type (Ent);
3478 -- For a tagged type, there is a declaration for each stream attribute
3479 -- at the freeze point, and we must generate only a completion of this
3480 -- declaration. We do the same for private types, because the full view
3481 -- might be tagged. Otherwise we generate a declaration at the point of
3482 -- the attribute definition clause.
3484 function Build_Spec return Node_Id;
3485 -- Used for declaration and renaming declaration, so that this is
3486 -- treated as a renaming_as_body.
3492 function Build_Spec return Node_Id is
3493 Out_P : constant Boolean := (Nam = TSS_Stream_Read);
3496 T_Ref : constant Node_Id := New_Reference_To (Etyp, Loc);
3499 Subp_Id := Make_Defining_Identifier (Loc, Sname);
3501 -- S : access Root_Stream_Type'Class
3503 Formals := New_List (
3504 Make_Parameter_Specification (Loc,
3505 Defining_Identifier =>
3506 Make_Defining_Identifier (Loc, Name_S),
3508 Make_Access_Definition (Loc,
3511 Designated_Type (Etype (F)), Loc))));
3513 if Nam = TSS_Stream_Input then
3514 Spec := Make_Function_Specification (Loc,
3515 Defining_Unit_Name => Subp_Id,
3516 Parameter_Specifications => Formals,
3517 Result_Definition => T_Ref);
3522 Make_Parameter_Specification (Loc,
3523 Defining_Identifier => Make_Defining_Identifier (Loc, Name_V),
3524 Out_Present => Out_P,
3525 Parameter_Type => T_Ref));
3527 Spec := Make_Procedure_Specification (Loc,
3528 Defining_Unit_Name => Subp_Id,
3529 Parameter_Specifications => Formals);
3535 -- Start of processing for New_Stream_Subprogram
3538 F := First_Formal (Subp);
3540 if Ekind (Subp) = E_Procedure then
3541 Etyp := Etype (Next_Formal (F));
3543 Etyp := Etype (Subp);
3546 -- Prepare subprogram declaration and insert it as an action on the
3547 -- clause node. The visibility for this entity is used to test for
3548 -- visibility of the attribute definition clause (in the sense of
3549 -- 8.3(23) as amended by AI-195).
3551 if not Defer_Declaration then
3553 Make_Subprogram_Declaration (Loc,
3554 Specification => Build_Spec);
3556 -- For a tagged type, there is always a visible declaration for each
3557 -- stream TSS (it is a predefined primitive operation), and the
3558 -- completion of this declaration occurs at the freeze point, which is
3559 -- not always visible at places where the attribute definition clause is
3560 -- visible. So, we create a dummy entity here for the purpose of
3561 -- tracking the visibility of the attribute definition clause itself.
3565 Make_Defining_Identifier (Loc,
3566 Chars => New_External_Name (Sname, 'V'));
3568 Make_Object_Declaration (Loc,
3569 Defining_Identifier => Subp_Id,
3570 Object_Definition => New_Occurrence_Of (Standard_Boolean, Loc));
3573 Insert_Action (N, Subp_Decl);
3574 Set_Entity (N, Subp_Id);
3577 Make_Subprogram_Renaming_Declaration (Loc,
3578 Specification => Build_Spec,
3579 Name => New_Reference_To (Subp, Loc));
3581 if Defer_Declaration then
3582 Set_TSS (Base_Type (Ent), Subp_Id);
3584 Insert_Action (N, Subp_Decl);
3585 Copy_TSS (Subp_Id, Base_Type (Ent));
3587 end New_Stream_Subprogram;
3589 ------------------------
3590 -- Rep_Item_Too_Early --
3591 ------------------------
3593 function Rep_Item_Too_Early (T : Entity_Id; N : Node_Id) return Boolean is
3595 -- Cannot apply non-operational rep items to generic types
3597 if Is_Operational_Item (N) then
3601 and then Is_Generic_Type (Root_Type (T))
3604 ("representation item not allowed for generic type", N);
3608 -- Otherwise check for incompleted type
3610 if Is_Incomplete_Or_Private_Type (T)
3611 and then No (Underlying_Type (T))
3614 ("representation item must be after full type declaration", N);
3617 -- If the type has incompleted components, a representation clause is
3618 -- illegal but stream attributes and Convention pragmas are correct.
3620 elsif Has_Private_Component (T) then
3621 if Nkind (N) = N_Pragma then
3625 ("representation item must appear after type is fully defined",
3632 end Rep_Item_Too_Early;
3634 -----------------------
3635 -- Rep_Item_Too_Late --
3636 -----------------------
3638 function Rep_Item_Too_Late
3641 FOnly : Boolean := False) return Boolean
3644 Parent_Type : Entity_Id;
3647 -- Output the too late message. Note that this is not considered a
3648 -- serious error, since the effect is simply that we ignore the
3649 -- representation clause in this case.
3655 procedure Too_Late is
3657 Error_Msg_N ("|representation item appears too late!", N);
3660 -- Start of processing for Rep_Item_Too_Late
3663 -- First make sure entity is not frozen (RM 13.1(9)). Exclude imported
3664 -- types, which may be frozen if they appear in a representation clause
3665 -- for a local type.
3668 and then not From_With_Type (T)
3671 S := First_Subtype (T);
3673 if Present (Freeze_Node (S)) then
3675 ("?no more representation items for }", Freeze_Node (S), S);
3680 -- Check for case of non-tagged derived type whose parent either has
3681 -- primitive operations, or is a by reference type (RM 13.1(10)).
3685 and then Is_Derived_Type (T)
3686 and then not Is_Tagged_Type (T)
3688 Parent_Type := Etype (Base_Type (T));
3690 if Has_Primitive_Operations (Parent_Type) then
3693 ("primitive operations already defined for&!", N, Parent_Type);
3696 elsif Is_By_Reference_Type (Parent_Type) then
3699 ("parent type & is a by reference type!", N, Parent_Type);
3704 -- No error, link item into head of chain of rep items for the entity
3706 Record_Rep_Item (T, N);
3708 end Rep_Item_Too_Late;
3710 -------------------------
3711 -- Same_Representation --
3712 -------------------------
3714 function Same_Representation (Typ1, Typ2 : Entity_Id) return Boolean is
3715 T1 : constant Entity_Id := Underlying_Type (Typ1);
3716 T2 : constant Entity_Id := Underlying_Type (Typ2);
3719 -- A quick check, if base types are the same, then we definitely have
3720 -- the same representation, because the subtype specific representation
3721 -- attributes (Size and Alignment) do not affect representation from
3722 -- the point of view of this test.
3724 if Base_Type (T1) = Base_Type (T2) then
3727 elsif Is_Private_Type (Base_Type (T2))
3728 and then Base_Type (T1) = Full_View (Base_Type (T2))
3733 -- Tagged types never have differing representations
3735 if Is_Tagged_Type (T1) then
3739 -- Representations are definitely different if conventions differ
3741 if Convention (T1) /= Convention (T2) then
3745 -- Representations are different if component alignments differ
3747 if (Is_Record_Type (T1) or else Is_Array_Type (T1))
3749 (Is_Record_Type (T2) or else Is_Array_Type (T2))
3750 and then Component_Alignment (T1) /= Component_Alignment (T2)
3755 -- For arrays, the only real issue is component size. If we know the
3756 -- component size for both arrays, and it is the same, then that's
3757 -- good enough to know we don't have a change of representation.
3759 if Is_Array_Type (T1) then
3760 if Known_Component_Size (T1)
3761 and then Known_Component_Size (T2)
3762 and then Component_Size (T1) = Component_Size (T2)
3768 -- Types definitely have same representation if neither has non-standard
3769 -- representation since default representations are always consistent.
3770 -- If only one has non-standard representation, and the other does not,
3771 -- then we consider that they do not have the same representation. They
3772 -- might, but there is no way of telling early enough.
3774 if Has_Non_Standard_Rep (T1) then
3775 if not Has_Non_Standard_Rep (T2) then
3779 return not Has_Non_Standard_Rep (T2);
3782 -- Here the two types both have non-standard representation, and we
3783 -- need to determine if they have the same non-standard representation
3785 -- For arrays, we simply need to test if the component sizes are the
3786 -- same. Pragma Pack is reflected in modified component sizes, so this
3787 -- check also deals with pragma Pack.
3789 if Is_Array_Type (T1) then
3790 return Component_Size (T1) = Component_Size (T2);
3792 -- Tagged types always have the same representation, because it is not
3793 -- possible to specify different representations for common fields.
3795 elsif Is_Tagged_Type (T1) then
3798 -- Case of record types
3800 elsif Is_Record_Type (T1) then
3802 -- Packed status must conform
3804 if Is_Packed (T1) /= Is_Packed (T2) then
3807 -- Otherwise we must check components. Typ2 maybe a constrained
3808 -- subtype with fewer components, so we compare the components
3809 -- of the base types.
3812 Record_Case : declare
3813 CD1, CD2 : Entity_Id;
3815 function Same_Rep return Boolean;
3816 -- CD1 and CD2 are either components or discriminants. This
3817 -- function tests whether the two have the same representation
3823 function Same_Rep return Boolean is
3825 if No (Component_Clause (CD1)) then
3826 return No (Component_Clause (CD2));
3830 Present (Component_Clause (CD2))
3832 Component_Bit_Offset (CD1) = Component_Bit_Offset (CD2)
3834 Esize (CD1) = Esize (CD2);
3838 -- Start processing for Record_Case
3841 if Has_Discriminants (T1) then
3842 CD1 := First_Discriminant (T1);
3843 CD2 := First_Discriminant (T2);
3845 -- The number of discriminants may be different if the
3846 -- derived type has fewer (constrained by values). The
3847 -- invisible discriminants retain the representation of
3848 -- the original, so the discrepancy does not per se
3849 -- indicate a different representation.
3852 and then Present (CD2)
3854 if not Same_Rep then
3857 Next_Discriminant (CD1);
3858 Next_Discriminant (CD2);
3863 CD1 := First_Component (Underlying_Type (Base_Type (T1)));
3864 CD2 := First_Component (Underlying_Type (Base_Type (T2)));
3866 while Present (CD1) loop
3867 if not Same_Rep then
3870 Next_Component (CD1);
3871 Next_Component (CD2);
3879 -- For enumeration types, we must check each literal to see if the
3880 -- representation is the same. Note that we do not permit enumeration
3881 -- reprsentation clauses for Character and Wide_Character, so these
3882 -- cases were already dealt with.
3884 elsif Is_Enumeration_Type (T1) then
3886 Enumeration_Case : declare
3890 L1 := First_Literal (T1);
3891 L2 := First_Literal (T2);
3893 while Present (L1) loop
3894 if Enumeration_Rep (L1) /= Enumeration_Rep (L2) then
3904 end Enumeration_Case;
3906 -- Any other types have the same representation for these purposes
3911 end Same_Representation;
3913 --------------------
3914 -- Set_Enum_Esize --
3915 --------------------
3917 procedure Set_Enum_Esize (T : Entity_Id) is
3925 -- Find the minimum standard size (8,16,32,64) that fits
3927 Lo := Enumeration_Rep (Entity (Type_Low_Bound (T)));
3928 Hi := Enumeration_Rep (Entity (Type_High_Bound (T)));
3931 if Lo >= -Uint_2**07 and then Hi < Uint_2**07 then
3932 Sz := Standard_Character_Size; -- May be > 8 on some targets
3934 elsif Lo >= -Uint_2**15 and then Hi < Uint_2**15 then
3937 elsif Lo >= -Uint_2**31 and then Hi < Uint_2**31 then
3940 else pragma Assert (Lo >= -Uint_2**63 and then Hi < Uint_2**63);
3945 if Hi < Uint_2**08 then
3946 Sz := Standard_Character_Size; -- May be > 8 on some targets
3948 elsif Hi < Uint_2**16 then
3951 elsif Hi < Uint_2**32 then
3954 else pragma Assert (Hi < Uint_2**63);
3959 -- That minimum is the proper size unless we have a foreign convention
3960 -- and the size required is 32 or less, in which case we bump the size
3961 -- up to 32. This is required for C and C++ and seems reasonable for
3962 -- all other foreign conventions.
3964 if Has_Foreign_Convention (T)
3965 and then Esize (T) < Standard_Integer_Size
3967 Init_Esize (T, Standard_Integer_Size);
3974 -----------------------------------
3975 -- Validate_Unchecked_Conversion --
3976 -----------------------------------
3978 procedure Validate_Unchecked_Conversion
3980 Act_Unit : Entity_Id)
3987 -- Obtain source and target types. Note that we call Ancestor_Subtype
3988 -- here because the processing for generic instantiation always makes
3989 -- subtypes, and we want the original frozen actual types.
3991 -- If we are dealing with private types, then do the check on their
3992 -- fully declared counterparts if the full declarations have been
3993 -- encountered (they don't have to be visible, but they must exist!)
3995 Source := Ancestor_Subtype (Etype (First_Formal (Act_Unit)));
3997 if Is_Private_Type (Source)
3998 and then Present (Underlying_Type (Source))
4000 Source := Underlying_Type (Source);
4003 Target := Ancestor_Subtype (Etype (Act_Unit));
4005 -- If either type is generic, the instantiation happens within a
4006 -- generic unit, and there is nothing to check. The proper check
4007 -- will happen when the enclosing generic is instantiated.
4009 if Is_Generic_Type (Source) or else Is_Generic_Type (Target) then
4013 if Is_Private_Type (Target)
4014 and then Present (Underlying_Type (Target))
4016 Target := Underlying_Type (Target);
4019 -- Source may be unconstrained array, but not target
4021 if Is_Array_Type (Target)
4022 and then not Is_Constrained (Target)
4025 ("unchecked conversion to unconstrained array not allowed", N);
4029 -- Warn if conversion between two different convention pointers
4031 if Is_Access_Type (Target)
4032 and then Is_Access_Type (Source)
4033 and then Convention (Target) /= Convention (Source)
4034 and then Warn_On_Unchecked_Conversion
4037 ("?conversion between pointers with different conventions!", N);
4040 -- Make entry in unchecked conversion table for later processing
4041 -- by Validate_Unchecked_Conversions, which will check sizes and
4042 -- alignments (using values set by the back-end where possible).
4043 -- This is only done if the appropriate warning is active
4045 if Warn_On_Unchecked_Conversion then
4046 Unchecked_Conversions.Append
4047 (New_Val => UC_Entry'
4052 -- If both sizes are known statically now, then back end annotation
4053 -- is not required to do a proper check but if either size is not
4054 -- known statically, then we need the annotation.
4056 if Known_Static_RM_Size (Source)
4057 and then Known_Static_RM_Size (Target)
4061 Back_Annotate_Rep_Info := True;
4065 -- If unchecked conversion to access type, and access type is
4066 -- declared in the same unit as the unchecked conversion, then
4067 -- set the No_Strict_Aliasing flag (no strict aliasing is
4068 -- implicit in this situation).
4070 if Is_Access_Type (Target) and then
4071 In_Same_Source_Unit (Target, N)
4073 Set_No_Strict_Aliasing (Implementation_Base_Type (Target));
4076 -- Generate N_Validate_Unchecked_Conversion node for back end in
4077 -- case the back end needs to perform special validation checks.
4079 -- Shouldn't this be in exp_ch13, since the check only gets done
4080 -- if we have full expansion and the back end is called ???
4083 Make_Validate_Unchecked_Conversion (Sloc (N));
4084 Set_Source_Type (Vnode, Source);
4085 Set_Target_Type (Vnode, Target);
4087 -- If the unchecked conversion node is in a list, just insert before
4088 -- it. If not we have some strange case, not worth bothering about.
4090 if Is_List_Member (N) then
4091 Insert_After (N, Vnode);
4093 end Validate_Unchecked_Conversion;
4095 ------------------------------------
4096 -- Validate_Unchecked_Conversions --
4097 ------------------------------------
4099 procedure Validate_Unchecked_Conversions is
4101 for N in Unchecked_Conversions.First .. Unchecked_Conversions.Last loop
4103 T : UC_Entry renames Unchecked_Conversions.Table (N);
4105 Enode : constant Node_Id := T.Enode;
4106 Source : constant Entity_Id := T.Source;
4107 Target : constant Entity_Id := T.Target;
4113 -- This validation check, which warns if we have unequal sizes
4114 -- for unchecked conversion, and thus potentially implementation
4115 -- dependent semantics, is one of the few occasions on which we
4116 -- use the official RM size instead of Esize. See description
4117 -- in Einfo "Handling of Type'Size Values" for details.
4119 if Serious_Errors_Detected = 0
4120 and then Known_Static_RM_Size (Source)
4121 and then Known_Static_RM_Size (Target)
4123 Source_Siz := RM_Size (Source);
4124 Target_Siz := RM_Size (Target);
4126 if Source_Siz /= Target_Siz then
4128 ("?types for unchecked conversion have different sizes!",
4131 if All_Errors_Mode then
4132 Error_Msg_Name_1 := Chars (Source);
4133 Error_Msg_Uint_1 := Source_Siz;
4134 Error_Msg_Name_2 := Chars (Target);
4135 Error_Msg_Uint_2 := Target_Siz;
4137 ("\size of % is ^, size of % is ^?", Enode);
4139 Error_Msg_Uint_1 := UI_Abs (Source_Siz - Target_Siz);
4141 if Is_Discrete_Type (Source)
4142 and then Is_Discrete_Type (Target)
4144 if Source_Siz > Target_Siz then
4146 ("\?^ high order bits of source will be ignored!",
4149 elsif Is_Unsigned_Type (Source) then
4151 ("\?source will be extended with ^ high order " &
4152 "zero bits?!", Enode);
4156 ("\?source will be extended with ^ high order " &
4161 elsif Source_Siz < Target_Siz then
4162 if Is_Discrete_Type (Target) then
4163 if Bytes_Big_Endian then
4165 ("\?target value will include ^ undefined " &
4170 ("\?target value will include ^ undefined " &
4177 ("\?^ trailing bits of target value will be " &
4178 "undefined!", Enode);
4181 else pragma Assert (Source_Siz > Target_Siz);
4183 ("\?^ trailing bits of source will be ignored!",
4190 -- If both types are access types, we need to check the alignment.
4191 -- If the alignment of both is specified, we can do it here.
4193 if Serious_Errors_Detected = 0
4194 and then Ekind (Source) in Access_Kind
4195 and then Ekind (Target) in Access_Kind
4196 and then Target_Strict_Alignment
4197 and then Present (Designated_Type (Source))
4198 and then Present (Designated_Type (Target))
4201 D_Source : constant Entity_Id := Designated_Type (Source);
4202 D_Target : constant Entity_Id := Designated_Type (Target);
4205 if Known_Alignment (D_Source)
4206 and then Known_Alignment (D_Target)
4209 Source_Align : constant Uint := Alignment (D_Source);
4210 Target_Align : constant Uint := Alignment (D_Target);
4213 if Source_Align < Target_Align
4214 and then not Is_Tagged_Type (D_Source)
4216 Error_Msg_Uint_1 := Target_Align;
4217 Error_Msg_Uint_2 := Source_Align;
4218 Error_Msg_Node_2 := D_Source;
4220 ("?alignment of & (^) is stricter than " &
4221 "alignment of & (^)!", Enode, D_Target);
4223 if All_Errors_Mode then
4225 ("\?resulting access value may have invalid " &
4226 "alignment!", Enode);
4235 end Validate_Unchecked_Conversions;