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 Lib.Xref; use Lib.Xref;
34 with Namet; use Namet;
35 with Nlists; use Nlists;
36 with Nmake; use Nmake;
38 with Restrict; use Restrict;
39 with Rident; use Rident;
40 with Rtsfind; use Rtsfind;
42 with Sem_Ch8; use Sem_Ch8;
43 with Sem_Eval; use Sem_Eval;
44 with Sem_Res; use Sem_Res;
45 with Sem_Type; use Sem_Type;
46 with Sem_Util; use Sem_Util;
47 with Sem_Warn; use Sem_Warn;
48 with Snames; use Snames;
49 with Stand; use Stand;
50 with Sinfo; use Sinfo;
52 with Targparm; use Targparm;
53 with Ttypes; use Ttypes;
54 with Tbuild; use Tbuild;
55 with Urealp; use Urealp;
57 with GNAT.Heap_Sort_G;
59 package body Sem_Ch13 is
61 SSU : constant Pos := System_Storage_Unit;
62 -- Convenient short hand for commonly used constant
64 -----------------------
65 -- Local Subprograms --
66 -----------------------
68 procedure Alignment_Check_For_Esize_Change (Typ : Entity_Id);
69 -- This routine is called after setting the Esize of type entity Typ.
70 -- The purpose is to deal with the situation where an aligment has been
71 -- inherited from a derived type that is no longer appropriate for the
72 -- new Esize value. In this case, we reset the Alignment to unknown.
74 procedure Check_Component_Overlap (C1_Ent, C2_Ent : Entity_Id);
75 -- Given two entities for record components or discriminants, checks
76 -- if they hav overlapping component clauses and issues errors if so.
78 function Get_Alignment_Value (Expr : Node_Id) return Uint;
79 -- Given the expression for an alignment value, returns the corresponding
80 -- Uint value. If the value is inappropriate, then error messages are
81 -- posted as required, and a value of No_Uint is returned.
83 function Is_Operational_Item (N : Node_Id) return Boolean;
84 -- A specification for a stream attribute is allowed before the full
85 -- type is declared, as explained in AI-00137 and the corrigendum.
86 -- Attributes that do not specify a representation characteristic are
87 -- operational attributes.
89 function Address_Aliased_Entity (N : Node_Id) return Entity_Id;
90 -- If expression N is of the form E'Address, return E
92 procedure New_Stream_Subprogram
97 -- Create a subprogram renaming of a given stream attribute to the
98 -- designated subprogram and then in the tagged case, provide this as a
99 -- primitive operation, or in the non-tagged case make an appropriate TSS
100 -- entry. This is more properly an expansion activity than just semantics,
101 -- but the presence of user-defined stream functions for limited types is a
102 -- legality check, which is why this takes place here rather than in
103 -- exp_ch13, where it was previously. Nam indicates the name of the TSS
104 -- function to be generated.
106 -- To avoid elaboration anomalies with freeze nodes, for untagged types
107 -- we generate both a subprogram declaration and a subprogram renaming
108 -- declaration, so that the attribute specification is handled as a
109 -- renaming_as_body. For tagged types, the specification is one of the
112 ----------------------------------------------
113 -- Table for Validate_Unchecked_Conversions --
114 ----------------------------------------------
116 -- The following table collects unchecked conversions for validation.
117 -- Entries are made by Validate_Unchecked_Conversion and then the
118 -- call to Validate_Unchecked_Conversions does the actual error
119 -- checking and posting of warnings. The reason for this delayed
120 -- processing is to take advantage of back-annotations of size and
121 -- alignment values peformed by the back end.
123 type UC_Entry is record
124 Enode : Node_Id; -- node used for posting warnings
125 Source : Entity_Id; -- source type for unchecked conversion
126 Target : Entity_Id; -- target type for unchecked conversion
129 package Unchecked_Conversions is new Table.Table (
130 Table_Component_Type => UC_Entry,
131 Table_Index_Type => Int,
132 Table_Low_Bound => 1,
134 Table_Increment => 200,
135 Table_Name => "Unchecked_Conversions");
137 ----------------------------------------
138 -- Table for Validate_Address_Clauses --
139 ----------------------------------------
141 -- If an address clause has the form
143 -- for X'Address use Expr
145 -- where Expr is of the form Y'Address or recursively is a reference
146 -- to a constant of either of these forms, and X and Y are entities of
147 -- objects, then if Y has a smaller alignment than X, that merits a
148 -- warning about possible bad alignment. The following table collects
149 -- address clauses of this kind. We put these in a table so that they
150 -- can be checked after the back end has completed annotation of the
151 -- alignments of objects, since we can catch more cases that way.
153 type Address_Clause_Check_Record is record
155 -- The address clause
158 -- The entity of the object overlaying Y
161 -- The entity of the object being overlaid
164 package Address_Clause_Checks is new Table.Table (
165 Table_Component_Type => Address_Clause_Check_Record,
166 Table_Index_Type => Int,
167 Table_Low_Bound => 1,
169 Table_Increment => 200,
170 Table_Name => "Address_Clause_Checks");
172 ----------------------------
173 -- Address_Aliased_Entity --
174 ----------------------------
176 function Address_Aliased_Entity (N : Node_Id) return Entity_Id is
178 if Nkind (N) = N_Attribute_Reference
179 and then Attribute_Name (N) = Name_Address
182 Nam : Node_Id := Prefix (N);
185 or else Nkind (Nam) = N_Selected_Component
186 or else Nkind (Nam) = N_Indexed_Component
191 if Is_Entity_Name (Nam) then
198 end Address_Aliased_Entity;
200 -----------------------------------------
201 -- Adjust_Record_For_Reverse_Bit_Order --
202 -----------------------------------------
204 procedure Adjust_Record_For_Reverse_Bit_Order (R : Entity_Id) is
205 Max_Machine_Scalar_Size : constant Uint :=
207 (Standard_Long_Long_Integer_Size);
208 -- We use this as the maximum machine scalar size in the sense of AI-133
212 SSU : constant Uint := UI_From_Int (System_Storage_Unit);
215 -- This first loop through components does two things. First it deals
216 -- with the case of components with component clauses whose length is
217 -- greater than the maximum machine scalar size (either accepting them
218 -- or rejecting as needed). Second, it counts the number of components
219 -- with component clauses whose length does not exceed this maximum for
223 Comp := First_Component_Or_Discriminant (R);
224 while Present (Comp) loop
226 CC : constant Node_Id := Component_Clause (Comp);
227 Fbit : constant Uint := Static_Integer (First_Bit (CC));
232 -- Case of component with size > max machine scalar
234 if Esize (Comp) > Max_Machine_Scalar_Size then
236 -- Must begin on byte boundary
238 if Fbit mod SSU /= 0 then
240 ("illegal first bit value for reverse bit order",
242 Error_Msg_Uint_1 := SSU;
243 Error_Msg_Uint_2 := Max_Machine_Scalar_Size;
246 ("\must be a multiple of ^ if size greater than ^",
249 -- Must end on byte boundary
251 elsif Esize (Comp) mod SSU /= 0 then
253 ("illegal last bit value for reverse bit order",
255 Error_Msg_Uint_1 := SSU;
256 Error_Msg_Uint_2 := Max_Machine_Scalar_Size;
259 ("\must be a multiple of ^ if size greater than ^",
262 -- OK, give warning if enabled
264 elsif Warn_On_Reverse_Bit_Order then
266 ("multi-byte field specified with non-standard"
267 & " Bit_Order?", CC);
269 if Bytes_Big_Endian then
271 ("\bytes are not reversed "
272 & "(component is big-endian)?", CC);
275 ("\bytes are not reversed "
276 & "(component is little-endian)?", CC);
280 -- Case where size is not greater than max machine scalar.
281 -- For now, we just count these.
284 Num_CC := Num_CC + 1;
289 Next_Component_Or_Discriminant (Comp);
292 -- We need to sort the component clauses on the basis of the Position
293 -- values in the clause, so we can group clauses with the same Position.
294 -- together to determine the relevant machine scalar size.
297 Comps : array (0 .. Num_CC) of Entity_Id;
298 -- Array to collect component and discrimninant entities. The data
299 -- starts at index 1, the 0'th entry is for the sort routine.
301 function CP_Lt (Op1, Op2 : Natural) return Boolean;
302 -- Compare routine for Sort
304 procedure CP_Move (From : Natural; To : Natural);
305 -- Move routine for Sort
307 package Sorting is new GNAT.Heap_Sort_G (CP_Move, CP_Lt);
311 -- Start and stop positions in component list of set of components
312 -- with the same starting position (that constitute components in
313 -- a single machine scalar).
316 -- Maximum last bit value of any component in this set
319 -- Corresponding machine scalar size
325 function CP_Lt (Op1, Op2 : Natural) return Boolean is
327 return Position (Component_Clause (Comps (Op1))) <
328 Position (Component_Clause (Comps (Op2)));
335 procedure CP_Move (From : Natural; To : Natural) is
337 Comps (To) := Comps (From);
341 -- Collect the component clauses
344 Comp := First_Component_Or_Discriminant (R);
345 while Present (Comp) loop
346 if Present (Component_Clause (Comp))
347 and then Esize (Comp) <= Max_Machine_Scalar_Size
349 Num_CC := Num_CC + 1;
350 Comps (Num_CC) := Comp;
353 Next_Component_Or_Discriminant (Comp);
356 -- Sort by ascending position number
358 Sorting.Sort (Num_CC);
360 -- We now have all the components whose size does not exceed the max
361 -- machine scalar value, sorted by starting position. In this loop
362 -- we gather groups of clauses starting at the same position, to
363 -- process them in accordance with Ada 2005 AI-133.
366 while Stop < Num_CC loop
370 Static_Integer (Last_Bit (Component_Clause (Comps (Start))));
371 while Stop < Num_CC loop
373 (Position (Component_Clause (Comps (Stop + 1)))) =
375 (Position (Component_Clause (Comps (Stop))))
382 (Last_Bit (Component_Clause (Comps (Stop)))));
388 -- Now we have a group of component clauses from Start to Stop
389 -- whose positions are identical, and MaxL is the maximum last bit
390 -- value of any of these components.
392 -- We need to determine the corresponding machine scalar size.
393 -- This loop assumes that machine scalar sizes are even, and that
394 -- each possible machine scalar has twice as many bits as the
397 MSS := Max_Machine_Scalar_Size;
399 and then (MSS / 2) >= SSU
400 and then (MSS / 2) > MaxL
405 -- Here is where we fix up the Component_Bit_Offset value to
406 -- account for the reverse bit order. Some examples of what needs
407 -- to be done for the case of a machine scalar size of 8 are:
409 -- First_Bit .. Last_Bit Component_Bit_Offset
421 -- The general rule is that the first bit is is obtained by
422 -- subtracting the old ending bit from machine scalar size - 1.
424 for C in Start .. Stop loop
426 Comp : constant Entity_Id := Comps (C);
427 CC : constant Node_Id := Component_Clause (Comp);
428 LB : constant Uint := Static_Integer (Last_Bit (CC));
429 NFB : constant Uint := MSS - Uint_1 - LB;
430 NLB : constant Uint := NFB + Esize (Comp) - 1;
431 Pos : constant Uint := Static_Integer (Position (CC));
434 if Warn_On_Reverse_Bit_Order then
435 Error_Msg_Uint_1 := MSS;
437 ("?reverse bit order in machine " &
438 "scalar of length^", First_Bit (CC));
439 Error_Msg_Uint_1 := NFB;
440 Error_Msg_Uint_2 := NLB;
442 if Bytes_Big_Endian then
444 ("?\big-endian range for component & is ^ .. ^",
445 First_Bit (CC), Comp);
448 ("?\little-endian range for component & is ^ .. ^",
449 First_Bit (CC), Comp);
453 Set_Component_Bit_Offset (Comp, Pos * SSU + NFB);
454 Set_Normalized_First_Bit (Comp, NFB mod SSU);
459 end Adjust_Record_For_Reverse_Bit_Order;
461 --------------------------------------
462 -- Alignment_Check_For_Esize_Change --
463 --------------------------------------
465 procedure Alignment_Check_For_Esize_Change (Typ : Entity_Id) is
467 -- If the alignment is known, and not set by a rep clause, and is
468 -- inconsistent with the size being set, then reset it to unknown,
469 -- we assume in this case that the size overrides the inherited
470 -- alignment, and that the alignment must be recomputed.
472 if Known_Alignment (Typ)
473 and then not Has_Alignment_Clause (Typ)
474 and then Esize (Typ) mod (Alignment (Typ) * SSU) /= 0
476 Init_Alignment (Typ);
478 end Alignment_Check_For_Esize_Change;
480 -----------------------
481 -- Analyze_At_Clause --
482 -----------------------
484 -- An at clause is replaced by the corresponding Address attribute
485 -- definition clause that is the preferred approach in Ada 95.
487 procedure Analyze_At_Clause (N : Node_Id) is
489 Check_Restriction (No_Obsolescent_Features, N);
491 if Warn_On_Obsolescent_Feature then
493 ("at clause is an obsolescent feature (RM J.7(2))?", N);
495 ("\use address attribute definition clause instead?", N);
499 Make_Attribute_Definition_Clause (Sloc (N),
500 Name => Identifier (N),
501 Chars => Name_Address,
502 Expression => Expression (N)));
503 Analyze_Attribute_Definition_Clause (N);
504 end Analyze_At_Clause;
506 -----------------------------------------
507 -- Analyze_Attribute_Definition_Clause --
508 -----------------------------------------
510 procedure Analyze_Attribute_Definition_Clause (N : Node_Id) is
511 Loc : constant Source_Ptr := Sloc (N);
512 Nam : constant Node_Id := Name (N);
513 Attr : constant Name_Id := Chars (N);
514 Expr : constant Node_Id := Expression (N);
515 Id : constant Attribute_Id := Get_Attribute_Id (Attr);
519 FOnly : Boolean := False;
520 -- Reset to True for subtype specific attribute (Alignment, Size)
521 -- and for stream attributes, i.e. those cases where in the call
522 -- to Rep_Item_Too_Late, FOnly is set True so that only the freezing
523 -- rules are checked. Note that the case of stream attributes is not
524 -- clear from the RM, but see AI95-00137. Also, the RM seems to
525 -- disallow Storage_Size for derived task types, but that is also
526 -- clearly unintentional.
528 procedure Analyze_Stream_TSS_Definition (TSS_Nam : TSS_Name_Type);
529 -- Common processing for 'Read, 'Write, 'Input and 'Output attribute
530 -- definition clauses.
532 procedure Analyze_Stream_TSS_Definition (TSS_Nam : TSS_Name_Type) is
533 Subp : Entity_Id := Empty;
538 Is_Read : constant Boolean := (TSS_Nam = TSS_Stream_Read);
540 function Has_Good_Profile (Subp : Entity_Id) return Boolean;
541 -- Return true if the entity is a subprogram with an appropriate
542 -- profile for the attribute being defined.
544 ----------------------
545 -- Has_Good_Profile --
546 ----------------------
548 function Has_Good_Profile (Subp : Entity_Id) return Boolean is
550 Is_Function : constant Boolean := (TSS_Nam = TSS_Stream_Input);
551 Expected_Ekind : constant array (Boolean) of Entity_Kind :=
552 (False => E_Procedure, True => E_Function);
556 if Ekind (Subp) /= Expected_Ekind (Is_Function) then
560 F := First_Formal (Subp);
563 or else Ekind (Etype (F)) /= E_Anonymous_Access_Type
564 or else Designated_Type (Etype (F)) /=
565 Class_Wide_Type (RTE (RE_Root_Stream_Type))
570 if not Is_Function then
574 Expected_Mode : constant array (Boolean) of Entity_Kind :=
575 (False => E_In_Parameter,
576 True => E_Out_Parameter);
578 if Parameter_Mode (F) /= Expected_Mode (Is_Read) then
589 return Base_Type (Typ) = Base_Type (Ent)
590 and then No (Next_Formal (F));
592 end Has_Good_Profile;
594 -- Start of processing for Analyze_Stream_TSS_Definition
599 if not Is_Type (U_Ent) then
600 Error_Msg_N ("local name must be a subtype", Nam);
604 Pnam := TSS (Base_Type (U_Ent), TSS_Nam);
606 -- If Pnam is present, it can be either inherited from an ancestor
607 -- type (in which case it is legal to redefine it for this type), or
608 -- be a previous definition of the attribute for the same type (in
609 -- which case it is illegal).
611 -- In the first case, it will have been analyzed already, and we
612 -- can check that its profile does not match the expected profile
613 -- for a stream attribute of U_Ent. In the second case, either Pnam
614 -- has been analyzed (and has the expected profile), or it has not
615 -- been analyzed yet (case of a type that has not been frozen yet
616 -- and for which the stream attribute has been set using Set_TSS).
619 and then (No (First_Entity (Pnam)) or else Has_Good_Profile (Pnam))
621 Error_Msg_Sloc := Sloc (Pnam);
622 Error_Msg_Name_1 := Attr;
623 Error_Msg_N ("% attribute already defined #", Nam);
629 if Is_Entity_Name (Expr) then
630 if not Is_Overloaded (Expr) then
631 if Has_Good_Profile (Entity (Expr)) then
632 Subp := Entity (Expr);
636 Get_First_Interp (Expr, I, It);
637 while Present (It.Nam) loop
638 if Has_Good_Profile (It.Nam) then
643 Get_Next_Interp (I, It);
648 if Present (Subp) then
649 if Is_Abstract_Subprogram (Subp) then
650 Error_Msg_N ("stream subprogram must not be abstract", Expr);
654 Set_Entity (Expr, Subp);
655 Set_Etype (Expr, Etype (Subp));
657 New_Stream_Subprogram (N, U_Ent, Subp, TSS_Nam);
660 Error_Msg_Name_1 := Attr;
661 Error_Msg_N ("incorrect expression for% attribute", Expr);
663 end Analyze_Stream_TSS_Definition;
665 -- Start of processing for Analyze_Attribute_Definition_Clause
668 if Ignore_Rep_Clauses then
669 Rewrite (N, Make_Null_Statement (Sloc (N)));
676 if Rep_Item_Too_Early (Ent, N) then
680 -- Rep clause applies to full view of incomplete type or private type if
681 -- we have one (if not, this is a premature use of the type). However,
682 -- certain semantic checks need to be done on the specified entity (i.e.
683 -- the private view), so we save it in Ent.
685 if Is_Private_Type (Ent)
686 and then Is_Derived_Type (Ent)
687 and then not Is_Tagged_Type (Ent)
688 and then No (Full_View (Ent))
690 -- If this is a private type whose completion is a derivation from
691 -- another private type, there is no full view, and the attribute
692 -- belongs to the type itself, not its underlying parent.
696 elsif Ekind (Ent) = E_Incomplete_Type then
698 -- The attribute applies to the full view, set the entity of the
699 -- attribute definition accordingly.
701 Ent := Underlying_Type (Ent);
703 Set_Entity (Nam, Ent);
706 U_Ent := Underlying_Type (Ent);
709 -- Complete other routine error checks
711 if Etype (Nam) = Any_Type then
714 elsif Scope (Ent) /= Current_Scope then
715 Error_Msg_N ("entity must be declared in this scope", Nam);
718 elsif No (U_Ent) then
721 elsif Is_Type (U_Ent)
722 and then not Is_First_Subtype (U_Ent)
723 and then Id /= Attribute_Object_Size
724 and then Id /= Attribute_Value_Size
725 and then not From_At_Mod (N)
727 Error_Msg_N ("cannot specify attribute for subtype", Nam);
731 -- Switch on particular attribute
739 -- Address attribute definition clause
741 when Attribute_Address => Address : begin
742 Analyze_And_Resolve (Expr, RTE (RE_Address));
744 if Present (Address_Clause (U_Ent)) then
745 Error_Msg_N ("address already given for &", Nam);
747 -- Case of address clause for subprogram
749 elsif Is_Subprogram (U_Ent) then
750 if Has_Homonym (U_Ent) then
752 ("address clause cannot be given " &
753 "for overloaded subprogram",
758 -- For subprograms, all address clauses are permitted, and we
759 -- mark the subprogram as having a deferred freeze so that Gigi
760 -- will not elaborate it too soon.
762 -- Above needs more comments, what is too soon about???
764 Set_Has_Delayed_Freeze (U_Ent);
766 -- Case of address clause for entry
768 elsif Ekind (U_Ent) = E_Entry then
769 if Nkind (Parent (N)) = N_Task_Body then
771 ("entry address must be specified in task spec", Nam);
775 -- For entries, we require a constant address
777 Check_Constant_Address_Clause (Expr, U_Ent);
779 -- Special checks for task types
781 if Is_Task_Type (Scope (U_Ent))
782 and then Comes_From_Source (Scope (U_Ent))
785 ("?entry address declared for entry in task type", N);
787 ("\?only one task can be declared of this type", N);
790 -- Entry address clauses are obsolescent
792 Check_Restriction (No_Obsolescent_Features, N);
794 if Warn_On_Obsolescent_Feature then
796 ("attaching interrupt to task entry is an " &
797 "obsolescent feature (RM J.7.1)?", N);
799 ("\use interrupt procedure instead?", N);
802 -- Case of an address clause for a controlled object which we
803 -- consider to be erroneous.
805 elsif Is_Controlled (Etype (U_Ent))
806 or else Has_Controlled_Component (Etype (U_Ent))
809 ("?controlled object& must not be overlaid", Nam, U_Ent);
811 ("\?Program_Error will be raised at run time", Nam);
812 Insert_Action (Declaration_Node (U_Ent),
813 Make_Raise_Program_Error (Loc,
814 Reason => PE_Overlaid_Controlled_Object));
817 -- Case of address clause for a (non-controlled) object
820 Ekind (U_Ent) = E_Variable
822 Ekind (U_Ent) = E_Constant
825 Expr : constant Node_Id := Expression (N);
826 Aent : constant Entity_Id := Address_Aliased_Entity (Expr);
827 Ent_Y : constant Entity_Id := Find_Overlaid_Object (N);
830 -- Exported variables cannot have an address clause,
831 -- because this cancels the effect of the pragma Export
833 if Is_Exported (U_Ent) then
835 ("cannot export object with address clause", Nam);
838 -- Overlaying controlled objects is erroneous
841 and then (Has_Controlled_Component (Etype (Aent))
842 or else Is_Controlled (Etype (Aent)))
845 ("?cannot overlay with controlled object", Expr);
847 ("\?Program_Error will be raised at run time", Expr);
848 Insert_Action (Declaration_Node (U_Ent),
849 Make_Raise_Program_Error (Loc,
850 Reason => PE_Overlaid_Controlled_Object));
854 and then Ekind (U_Ent) = E_Constant
855 and then Ekind (Aent) /= E_Constant
857 Error_Msg_N ("constant overlays a variable?", Expr);
859 elsif Present (Renamed_Object (U_Ent)) then
861 ("address clause not allowed"
862 & " for a renaming declaration (RM 13.1(6))", Nam);
865 -- Imported variables can have an address clause, but then
866 -- the import is pretty meaningless except to suppress
867 -- initializations, so we do not need such variables to
868 -- be statically allocated (and in fact it causes trouble
869 -- if the address clause is a local value).
871 elsif Is_Imported (U_Ent) then
872 Set_Is_Statically_Allocated (U_Ent, False);
875 -- We mark a possible modification of a variable with an
876 -- address clause, since it is likely aliasing is occurring.
878 Note_Possible_Modification (Nam);
880 -- Here we are checking for explicit overlap of one variable
881 -- by another, and if we find this then mark the overlapped
882 -- variable as also being volatile to prevent unwanted
885 if Present (Ent_Y) then
886 Set_Treat_As_Volatile (Ent_Y);
889 -- Legality checks on the address clause for initialized
890 -- objects is deferred until the freeze point, because
891 -- a subsequent pragma might indicate that the object is
892 -- imported and thus not initialized.
894 Set_Has_Delayed_Freeze (U_Ent);
896 if Is_Exported (U_Ent) then
898 ("& cannot be exported if an address clause is given",
901 ("\define and export a variable " &
902 "that holds its address instead",
906 -- Entity has delayed freeze, so we will generate an
907 -- alignment check at the freeze point unless suppressed.
909 if not Range_Checks_Suppressed (U_Ent)
910 and then not Alignment_Checks_Suppressed (U_Ent)
912 Set_Check_Address_Alignment (N);
915 -- Kill the size check code, since we are not allocating
916 -- the variable, it is somewhere else.
918 Kill_Size_Check_Code (U_Ent);
921 -- If the address clause is of the form:
923 -- for X'Address use Y'Address
927 -- Const : constant Address := Y'Address;
929 -- for X'Address use Const;
931 -- then we make an entry in the table for checking the size and
932 -- alignment of the overlaying variable. We defer this check
933 -- till after code generation to take full advantage of the
934 -- annotation done by the back end. This entry is only made if
935 -- we have not already posted a warning about size/alignment
936 -- (some warnings of this type are posted in Checks).
938 if Address_Clause_Overlay_Warnings then
940 Ent_X : Entity_Id := Empty;
941 Ent_Y : Entity_Id := Empty;
944 Ent_Y := Find_Overlaid_Object (N);
946 if Present (Ent_Y) and then Is_Entity_Name (Name (N)) then
947 Ent_X := Entity (Name (N));
948 Address_Clause_Checks.Append ((N, Ent_X, Ent_Y));
953 -- Not a valid entity for an address clause
956 Error_Msg_N ("address cannot be given for &", Nam);
964 -- Alignment attribute definition clause
966 when Attribute_Alignment => Alignment_Block : declare
967 Align : constant Uint := Get_Alignment_Value (Expr);
972 if not Is_Type (U_Ent)
973 and then Ekind (U_Ent) /= E_Variable
974 and then Ekind (U_Ent) /= E_Constant
976 Error_Msg_N ("alignment cannot be given for &", Nam);
978 elsif Has_Alignment_Clause (U_Ent) then
979 Error_Msg_Sloc := Sloc (Alignment_Clause (U_Ent));
980 Error_Msg_N ("alignment clause previously given#", N);
982 elsif Align /= No_Uint then
983 Set_Has_Alignment_Clause (U_Ent);
984 Set_Alignment (U_Ent, Align);
992 -- Bit_Order attribute definition clause
994 when Attribute_Bit_Order => Bit_Order : declare
996 if not Is_Record_Type (U_Ent) then
998 ("Bit_Order can only be defined for record type", Nam);
1001 Analyze_And_Resolve (Expr, RTE (RE_Bit_Order));
1003 if Etype (Expr) = Any_Type then
1006 elsif not Is_Static_Expression (Expr) then
1007 Flag_Non_Static_Expr
1008 ("Bit_Order requires static expression!", Expr);
1011 if (Expr_Value (Expr) = 0) /= Bytes_Big_Endian then
1012 Set_Reverse_Bit_Order (U_Ent, True);
1018 --------------------
1019 -- Component_Size --
1020 --------------------
1022 -- Component_Size attribute definition clause
1024 when Attribute_Component_Size => Component_Size_Case : declare
1025 Csize : constant Uint := Static_Integer (Expr);
1028 New_Ctyp : Entity_Id;
1032 if not Is_Array_Type (U_Ent) then
1033 Error_Msg_N ("component size requires array type", Nam);
1037 Btype := Base_Type (U_Ent);
1039 if Has_Component_Size_Clause (Btype) then
1041 ("component size clase for& previously given", Nam);
1043 elsif Csize /= No_Uint then
1044 Check_Size (Expr, Component_Type (Btype), Csize, Biased);
1046 if Has_Aliased_Components (Btype)
1049 and then Csize /= 16
1052 ("component size incorrect for aliased components", N);
1056 -- For the biased case, build a declaration for a subtype
1057 -- that will be used to represent the biased subtype that
1058 -- reflects the biased representation of components. We need
1059 -- this subtype to get proper conversions on referencing
1060 -- elements of the array.
1064 Make_Defining_Identifier (Loc,
1065 Chars => New_External_Name (Chars (U_Ent), 'C', 0, 'T'));
1068 Make_Subtype_Declaration (Loc,
1069 Defining_Identifier => New_Ctyp,
1070 Subtype_Indication =>
1071 New_Occurrence_Of (Component_Type (Btype), Loc));
1073 Set_Parent (Decl, N);
1074 Analyze (Decl, Suppress => All_Checks);
1076 Set_Has_Delayed_Freeze (New_Ctyp, False);
1077 Set_Esize (New_Ctyp, Csize);
1078 Set_RM_Size (New_Ctyp, Csize);
1079 Init_Alignment (New_Ctyp);
1080 Set_Has_Biased_Representation (New_Ctyp, True);
1081 Set_Is_Itype (New_Ctyp, True);
1082 Set_Associated_Node_For_Itype (New_Ctyp, U_Ent);
1084 Set_Component_Type (Btype, New_Ctyp);
1087 Set_Component_Size (Btype, Csize);
1088 Set_Has_Component_Size_Clause (Btype, True);
1089 Set_Has_Non_Standard_Rep (Btype, True);
1091 end Component_Size_Case;
1097 when Attribute_External_Tag => External_Tag :
1099 if not Is_Tagged_Type (U_Ent) then
1100 Error_Msg_N ("should be a tagged type", Nam);
1103 Analyze_And_Resolve (Expr, Standard_String);
1105 if not Is_Static_Expression (Expr) then
1106 Flag_Non_Static_Expr
1107 ("static string required for tag name!", Nam);
1110 if VM_Target = No_VM then
1111 Set_Has_External_Tag_Rep_Clause (U_Ent);
1112 elsif not Inspector_Mode then
1113 Error_Msg_Name_1 := Attr;
1115 ("% attribute unsupported in this configuration", Nam);
1118 if not Is_Library_Level_Entity (U_Ent) then
1120 ("?non-unique external tag supplied for &", N, U_Ent);
1122 ("?\same external tag applies to all subprogram calls", N);
1124 ("?\corresponding internal tag cannot be obtained", N);
1132 when Attribute_Input =>
1133 Analyze_Stream_TSS_Definition (TSS_Stream_Input);
1134 Set_Has_Specified_Stream_Input (Ent);
1140 -- Machine radix attribute definition clause
1142 when Attribute_Machine_Radix => Machine_Radix : declare
1143 Radix : constant Uint := Static_Integer (Expr);
1146 if not Is_Decimal_Fixed_Point_Type (U_Ent) then
1147 Error_Msg_N ("decimal fixed-point type expected for &", Nam);
1149 elsif Has_Machine_Radix_Clause (U_Ent) then
1150 Error_Msg_Sloc := Sloc (Alignment_Clause (U_Ent));
1151 Error_Msg_N ("machine radix clause previously given#", N);
1153 elsif Radix /= No_Uint then
1154 Set_Has_Machine_Radix_Clause (U_Ent);
1155 Set_Has_Non_Standard_Rep (Base_Type (U_Ent));
1159 elsif Radix = 10 then
1160 Set_Machine_Radix_10 (U_Ent);
1162 Error_Msg_N ("machine radix value must be 2 or 10", Expr);
1171 -- Object_Size attribute definition clause
1173 when Attribute_Object_Size => Object_Size : declare
1174 Size : constant Uint := Static_Integer (Expr);
1177 pragma Warnings (Off, Biased);
1180 if not Is_Type (U_Ent) then
1181 Error_Msg_N ("Object_Size cannot be given for &", Nam);
1183 elsif Has_Object_Size_Clause (U_Ent) then
1184 Error_Msg_N ("Object_Size already given for &", Nam);
1187 Check_Size (Expr, U_Ent, Size, Biased);
1195 UI_Mod (Size, 64) /= 0
1198 ("Object_Size must be 8, 16, 32, or multiple of 64",
1202 Set_Esize (U_Ent, Size);
1203 Set_Has_Object_Size_Clause (U_Ent);
1204 Alignment_Check_For_Esize_Change (U_Ent);
1212 when Attribute_Output =>
1213 Analyze_Stream_TSS_Definition (TSS_Stream_Output);
1214 Set_Has_Specified_Stream_Output (Ent);
1220 when Attribute_Read =>
1221 Analyze_Stream_TSS_Definition (TSS_Stream_Read);
1222 Set_Has_Specified_Stream_Read (Ent);
1228 -- Size attribute definition clause
1230 when Attribute_Size => Size : declare
1231 Size : constant Uint := Static_Integer (Expr);
1238 if Has_Size_Clause (U_Ent) then
1239 Error_Msg_N ("size already given for &", Nam);
1241 elsif not Is_Type (U_Ent)
1242 and then Ekind (U_Ent) /= E_Variable
1243 and then Ekind (U_Ent) /= E_Constant
1245 Error_Msg_N ("size cannot be given for &", Nam);
1247 elsif Is_Array_Type (U_Ent)
1248 and then not Is_Constrained (U_Ent)
1251 ("size cannot be given for unconstrained array", Nam);
1253 elsif Size /= No_Uint then
1254 if Is_Type (U_Ent) then
1257 Etyp := Etype (U_Ent);
1260 -- Check size, note that Gigi is in charge of checking that the
1261 -- size of an array or record type is OK. Also we do not check
1262 -- the size in the ordinary fixed-point case, since it is too
1263 -- early to do so (there may be subsequent small clause that
1264 -- affects the size). We can check the size if a small clause
1265 -- has already been given.
1267 if not Is_Ordinary_Fixed_Point_Type (U_Ent)
1268 or else Has_Small_Clause (U_Ent)
1270 Check_Size (Expr, Etyp, Size, Biased);
1271 Set_Has_Biased_Representation (U_Ent, Biased);
1274 -- For types set RM_Size and Esize if possible
1276 if Is_Type (U_Ent) then
1277 Set_RM_Size (U_Ent, Size);
1279 -- For scalar types, increase Object_Size to power of 2, but
1280 -- not less than a storage unit in any case (i.e., normally
1281 -- this means it will be byte addressable).
1283 if Is_Scalar_Type (U_Ent) then
1284 if Size <= System_Storage_Unit then
1285 Init_Esize (U_Ent, System_Storage_Unit);
1286 elsif Size <= 16 then
1287 Init_Esize (U_Ent, 16);
1288 elsif Size <= 32 then
1289 Init_Esize (U_Ent, 32);
1291 Set_Esize (U_Ent, (Size + 63) / 64 * 64);
1294 -- For all other types, object size = value size. The
1295 -- backend will adjust as needed.
1298 Set_Esize (U_Ent, Size);
1301 Alignment_Check_For_Esize_Change (U_Ent);
1303 -- For objects, set Esize only
1306 if Is_Elementary_Type (Etyp) then
1307 if Size /= System_Storage_Unit
1309 Size /= System_Storage_Unit * 2
1311 Size /= System_Storage_Unit * 4
1313 Size /= System_Storage_Unit * 8
1315 Error_Msg_Uint_1 := UI_From_Int (System_Storage_Unit);
1316 Error_Msg_Uint_2 := Error_Msg_Uint_1 * 8;
1318 ("size for primitive object must be a power of 2"
1319 & " in the range ^-^", N);
1323 Set_Esize (U_Ent, Size);
1326 Set_Has_Size_Clause (U_Ent);
1334 -- Small attribute definition clause
1336 when Attribute_Small => Small : declare
1337 Implicit_Base : constant Entity_Id := Base_Type (U_Ent);
1341 Analyze_And_Resolve (Expr, Any_Real);
1343 if Etype (Expr) = Any_Type then
1346 elsif not Is_Static_Expression (Expr) then
1347 Flag_Non_Static_Expr
1348 ("small requires static expression!", Expr);
1352 Small := Expr_Value_R (Expr);
1354 if Small <= Ureal_0 then
1355 Error_Msg_N ("small value must be greater than zero", Expr);
1361 if not Is_Ordinary_Fixed_Point_Type (U_Ent) then
1363 ("small requires an ordinary fixed point type", Nam);
1365 elsif Has_Small_Clause (U_Ent) then
1366 Error_Msg_N ("small already given for &", Nam);
1368 elsif Small > Delta_Value (U_Ent) then
1370 ("small value must not be greater then delta value", Nam);
1373 Set_Small_Value (U_Ent, Small);
1374 Set_Small_Value (Implicit_Base, Small);
1375 Set_Has_Small_Clause (U_Ent);
1376 Set_Has_Small_Clause (Implicit_Base);
1377 Set_Has_Non_Standard_Rep (Implicit_Base);
1385 -- Storage_Pool attribute definition clause
1387 when Attribute_Storage_Pool => Storage_Pool : declare
1392 if Ekind (U_Ent) = E_Access_Subprogram_Type then
1394 ("storage pool cannot be given for access-to-subprogram type",
1398 elsif Ekind (U_Ent) /= E_Access_Type
1399 and then Ekind (U_Ent) /= E_General_Access_Type
1402 ("storage pool can only be given for access types", Nam);
1405 elsif Is_Derived_Type (U_Ent) then
1407 ("storage pool cannot be given for a derived access type",
1410 elsif Has_Storage_Size_Clause (U_Ent) then
1411 Error_Msg_N ("storage size already given for &", Nam);
1414 elsif Present (Associated_Storage_Pool (U_Ent)) then
1415 Error_Msg_N ("storage pool already given for &", Nam);
1420 (Expr, Class_Wide_Type (RTE (RE_Root_Storage_Pool)));
1422 if Nkind (Expr) = N_Type_Conversion then
1423 T := Etype (Expression (Expr));
1428 -- The Stack_Bounded_Pool is used internally for implementing
1429 -- access types with a Storage_Size. Since it only work
1430 -- properly when used on one specific type, we need to check
1431 -- that it is not highjacked improperly:
1432 -- type T is access Integer;
1433 -- for T'Storage_Size use n;
1434 -- type Q is access Float;
1435 -- for Q'Storage_Size use T'Storage_Size; -- incorrect
1437 if RTE_Available (RE_Stack_Bounded_Pool)
1438 and then Base_Type (T) = RTE (RE_Stack_Bounded_Pool)
1440 Error_Msg_N ("non-shareable internal Pool", Expr);
1444 -- If the argument is a name that is not an entity name, then
1445 -- we construct a renaming operation to define an entity of
1446 -- type storage pool.
1448 if not Is_Entity_Name (Expr)
1449 and then Is_Object_Reference (Expr)
1452 Make_Defining_Identifier (Loc,
1453 Chars => New_Internal_Name ('P'));
1456 Rnode : constant Node_Id :=
1457 Make_Object_Renaming_Declaration (Loc,
1458 Defining_Identifier => Pool,
1460 New_Occurrence_Of (Etype (Expr), Loc),
1464 Insert_Before (N, Rnode);
1466 Set_Associated_Storage_Pool (U_Ent, Pool);
1469 elsif Is_Entity_Name (Expr) then
1470 Pool := Entity (Expr);
1472 -- If pool is a renamed object, get original one. This can
1473 -- happen with an explicit renaming, and within instances.
1475 while Present (Renamed_Object (Pool))
1476 and then Is_Entity_Name (Renamed_Object (Pool))
1478 Pool := Entity (Renamed_Object (Pool));
1481 if Present (Renamed_Object (Pool))
1482 and then Nkind (Renamed_Object (Pool)) = N_Type_Conversion
1483 and then Is_Entity_Name (Expression (Renamed_Object (Pool)))
1485 Pool := Entity (Expression (Renamed_Object (Pool)));
1488 Set_Associated_Storage_Pool (U_Ent, Pool);
1490 elsif Nkind (Expr) = N_Type_Conversion
1491 and then Is_Entity_Name (Expression (Expr))
1492 and then Nkind (Original_Node (Expr)) = N_Attribute_Reference
1494 Pool := Entity (Expression (Expr));
1495 Set_Associated_Storage_Pool (U_Ent, Pool);
1498 Error_Msg_N ("incorrect reference to a Storage Pool", Expr);
1507 -- Storage_Size attribute definition clause
1509 when Attribute_Storage_Size => Storage_Size : declare
1510 Btype : constant Entity_Id := Base_Type (U_Ent);
1514 if Is_Task_Type (U_Ent) then
1515 Check_Restriction (No_Obsolescent_Features, N);
1517 if Warn_On_Obsolescent_Feature then
1519 ("storage size clause for task is an " &
1520 "obsolescent feature (RM J.9)?", N);
1522 ("\use Storage_Size pragma instead?", N);
1528 if not Is_Access_Type (U_Ent)
1529 and then Ekind (U_Ent) /= E_Task_Type
1531 Error_Msg_N ("storage size cannot be given for &", Nam);
1533 elsif Is_Access_Type (U_Ent) and Is_Derived_Type (U_Ent) then
1535 ("storage size cannot be given for a derived access type",
1538 elsif Has_Storage_Size_Clause (Btype) then
1539 Error_Msg_N ("storage size already given for &", Nam);
1542 Analyze_And_Resolve (Expr, Any_Integer);
1544 if Is_Access_Type (U_Ent) then
1545 if Present (Associated_Storage_Pool (U_Ent)) then
1546 Error_Msg_N ("storage pool already given for &", Nam);
1550 if Compile_Time_Known_Value (Expr)
1551 and then Expr_Value (Expr) = 0
1553 Set_No_Pool_Assigned (Btype);
1556 else -- Is_Task_Type (U_Ent)
1557 Sprag := Get_Rep_Pragma (Btype, Name_Storage_Size);
1559 if Present (Sprag) then
1560 Error_Msg_Sloc := Sloc (Sprag);
1562 ("Storage_Size already specified#", Nam);
1567 Set_Has_Storage_Size_Clause (Btype);
1575 when Attribute_Stream_Size => Stream_Size : declare
1576 Size : constant Uint := Static_Integer (Expr);
1579 if Ada_Version <= Ada_95 then
1580 Check_Restriction (No_Implementation_Attributes, N);
1583 if Has_Stream_Size_Clause (U_Ent) then
1584 Error_Msg_N ("Stream_Size already given for &", Nam);
1586 elsif Is_Elementary_Type (U_Ent) then
1587 if Size /= System_Storage_Unit
1589 Size /= System_Storage_Unit * 2
1591 Size /= System_Storage_Unit * 4
1593 Size /= System_Storage_Unit * 8
1595 Error_Msg_Uint_1 := UI_From_Int (System_Storage_Unit);
1597 ("stream size for elementary type must be a"
1598 & " power of 2 and at least ^", N);
1600 elsif RM_Size (U_Ent) > Size then
1601 Error_Msg_Uint_1 := RM_Size (U_Ent);
1603 ("stream size for elementary type must be a"
1604 & " power of 2 and at least ^", N);
1607 Set_Has_Stream_Size_Clause (U_Ent);
1610 Error_Msg_N ("Stream_Size cannot be given for &", Nam);
1618 -- Value_Size attribute definition clause
1620 when Attribute_Value_Size => Value_Size : declare
1621 Size : constant Uint := Static_Integer (Expr);
1625 if not Is_Type (U_Ent) then
1626 Error_Msg_N ("Value_Size cannot be given for &", Nam);
1629 (Get_Attribute_Definition_Clause
1630 (U_Ent, Attribute_Value_Size))
1632 Error_Msg_N ("Value_Size already given for &", Nam);
1634 elsif Is_Array_Type (U_Ent)
1635 and then not Is_Constrained (U_Ent)
1638 ("Value_Size cannot be given for unconstrained array", Nam);
1641 if Is_Elementary_Type (U_Ent) then
1642 Check_Size (Expr, U_Ent, Size, Biased);
1643 Set_Has_Biased_Representation (U_Ent, Biased);
1646 Set_RM_Size (U_Ent, Size);
1654 when Attribute_Write =>
1655 Analyze_Stream_TSS_Definition (TSS_Stream_Write);
1656 Set_Has_Specified_Stream_Write (Ent);
1658 -- All other attributes cannot be set
1662 ("attribute& cannot be set with definition clause", N);
1665 -- The test for the type being frozen must be performed after
1666 -- any expression the clause has been analyzed since the expression
1667 -- itself might cause freezing that makes the clause illegal.
1669 if Rep_Item_Too_Late (U_Ent, N, FOnly) then
1672 end Analyze_Attribute_Definition_Clause;
1674 ----------------------------
1675 -- Analyze_Code_Statement --
1676 ----------------------------
1678 procedure Analyze_Code_Statement (N : Node_Id) is
1679 HSS : constant Node_Id := Parent (N);
1680 SBody : constant Node_Id := Parent (HSS);
1681 Subp : constant Entity_Id := Current_Scope;
1688 -- Analyze and check we get right type, note that this implements the
1689 -- requirement (RM 13.8(1)) that Machine_Code be with'ed, since that
1690 -- is the only way that Asm_Insn could possibly be visible.
1692 Analyze_And_Resolve (Expression (N));
1694 if Etype (Expression (N)) = Any_Type then
1696 elsif Etype (Expression (N)) /= RTE (RE_Asm_Insn) then
1697 Error_Msg_N ("incorrect type for code statement", N);
1701 Check_Code_Statement (N);
1703 -- Make sure we appear in the handled statement sequence of a
1704 -- subprogram (RM 13.8(3)).
1706 if Nkind (HSS) /= N_Handled_Sequence_Of_Statements
1707 or else Nkind (SBody) /= N_Subprogram_Body
1710 ("code statement can only appear in body of subprogram", N);
1714 -- Do remaining checks (RM 13.8(3)) if not already done
1716 if not Is_Machine_Code_Subprogram (Subp) then
1717 Set_Is_Machine_Code_Subprogram (Subp);
1719 -- No exception handlers allowed
1721 if Present (Exception_Handlers (HSS)) then
1723 ("exception handlers not permitted in machine code subprogram",
1724 First (Exception_Handlers (HSS)));
1727 -- No declarations other than use clauses and pragmas (we allow
1728 -- certain internally generated declarations as well).
1730 Decl := First (Declarations (SBody));
1731 while Present (Decl) loop
1732 DeclO := Original_Node (Decl);
1733 if Comes_From_Source (DeclO)
1734 and then Nkind (DeclO) /= N_Pragma
1735 and then Nkind (DeclO) /= N_Use_Package_Clause
1736 and then Nkind (DeclO) /= N_Use_Type_Clause
1737 and then Nkind (DeclO) /= N_Implicit_Label_Declaration
1740 ("this declaration not allowed in machine code subprogram",
1747 -- No statements other than code statements, pragmas, and labels.
1748 -- Again we allow certain internally generated statements.
1750 Stmt := First (Statements (HSS));
1751 while Present (Stmt) loop
1752 StmtO := Original_Node (Stmt);
1753 if Comes_From_Source (StmtO)
1754 and then Nkind (StmtO) /= N_Pragma
1755 and then Nkind (StmtO) /= N_Label
1756 and then Nkind (StmtO) /= N_Code_Statement
1759 ("this statement is not allowed in machine code subprogram",
1766 end Analyze_Code_Statement;
1768 -----------------------------------------------
1769 -- Analyze_Enumeration_Representation_Clause --
1770 -----------------------------------------------
1772 procedure Analyze_Enumeration_Representation_Clause (N : Node_Id) is
1773 Ident : constant Node_Id := Identifier (N);
1774 Aggr : constant Node_Id := Array_Aggregate (N);
1775 Enumtype : Entity_Id;
1781 Err : Boolean := False;
1783 Lo : constant Uint := Expr_Value (Type_Low_Bound (Universal_Integer));
1784 Hi : constant Uint := Expr_Value (Type_High_Bound (Universal_Integer));
1789 if Ignore_Rep_Clauses then
1793 -- First some basic error checks
1796 Enumtype := Entity (Ident);
1798 if Enumtype = Any_Type
1799 or else Rep_Item_Too_Early (Enumtype, N)
1803 Enumtype := Underlying_Type (Enumtype);
1806 if not Is_Enumeration_Type (Enumtype) then
1808 ("enumeration type required, found}",
1809 Ident, First_Subtype (Enumtype));
1813 -- Ignore rep clause on generic actual type. This will already have
1814 -- been flagged on the template as an error, and this is the safest
1815 -- way to ensure we don't get a junk cascaded message in the instance.
1817 if Is_Generic_Actual_Type (Enumtype) then
1820 -- Type must be in current scope
1822 elsif Scope (Enumtype) /= Current_Scope then
1823 Error_Msg_N ("type must be declared in this scope", Ident);
1826 -- Type must be a first subtype
1828 elsif not Is_First_Subtype (Enumtype) then
1829 Error_Msg_N ("cannot give enumeration rep clause for subtype", N);
1832 -- Ignore duplicate rep clause
1834 elsif Has_Enumeration_Rep_Clause (Enumtype) then
1835 Error_Msg_N ("duplicate enumeration rep clause ignored", N);
1838 -- Don't allow rep clause for standard [wide_[wide_]]character
1840 elsif Root_Type (Enumtype) = Standard_Character
1841 or else Root_Type (Enumtype) = Standard_Wide_Character
1842 or else Root_Type (Enumtype) = Standard_Wide_Wide_Character
1844 Error_Msg_N ("enumeration rep clause not allowed for this type", N);
1847 -- Check that the expression is a proper aggregate (no parentheses)
1849 elsif Paren_Count (Aggr) /= 0 then
1851 ("extra parentheses surrounding aggregate not allowed",
1855 -- All tests passed, so set rep clause in place
1858 Set_Has_Enumeration_Rep_Clause (Enumtype);
1859 Set_Has_Enumeration_Rep_Clause (Base_Type (Enumtype));
1862 -- Now we process the aggregate. Note that we don't use the normal
1863 -- aggregate code for this purpose, because we don't want any of the
1864 -- normal expansion activities, and a number of special semantic
1865 -- rules apply (including the component type being any integer type)
1867 Elit := First_Literal (Enumtype);
1869 -- First the positional entries if any
1871 if Present (Expressions (Aggr)) then
1872 Expr := First (Expressions (Aggr));
1873 while Present (Expr) loop
1875 Error_Msg_N ("too many entries in aggregate", Expr);
1879 Val := Static_Integer (Expr);
1881 -- Err signals that we found some incorrect entries processing
1882 -- the list. The final checks for completeness and ordering are
1883 -- skipped in this case.
1885 if Val = No_Uint then
1887 elsif Val < Lo or else Hi < Val then
1888 Error_Msg_N ("value outside permitted range", Expr);
1892 Set_Enumeration_Rep (Elit, Val);
1893 Set_Enumeration_Rep_Expr (Elit, Expr);
1899 -- Now process the named entries if present
1901 if Present (Component_Associations (Aggr)) then
1902 Assoc := First (Component_Associations (Aggr));
1903 while Present (Assoc) loop
1904 Choice := First (Choices (Assoc));
1906 if Present (Next (Choice)) then
1908 ("multiple choice not allowed here", Next (Choice));
1912 if Nkind (Choice) = N_Others_Choice then
1913 Error_Msg_N ("others choice not allowed here", Choice);
1916 elsif Nkind (Choice) = N_Range then
1917 -- ??? should allow zero/one element range here
1918 Error_Msg_N ("range not allowed here", Choice);
1922 Analyze_And_Resolve (Choice, Enumtype);
1924 if Is_Entity_Name (Choice)
1925 and then Is_Type (Entity (Choice))
1927 Error_Msg_N ("subtype name not allowed here", Choice);
1929 -- ??? should allow static subtype with zero/one entry
1931 elsif Etype (Choice) = Base_Type (Enumtype) then
1932 if not Is_Static_Expression (Choice) then
1933 Flag_Non_Static_Expr
1934 ("non-static expression used for choice!", Choice);
1938 Elit := Expr_Value_E (Choice);
1940 if Present (Enumeration_Rep_Expr (Elit)) then
1941 Error_Msg_Sloc := Sloc (Enumeration_Rep_Expr (Elit));
1943 ("representation for& previously given#",
1948 Set_Enumeration_Rep_Expr (Elit, Choice);
1950 Expr := Expression (Assoc);
1951 Val := Static_Integer (Expr);
1953 if Val = No_Uint then
1956 elsif Val < Lo or else Hi < Val then
1957 Error_Msg_N ("value outside permitted range", Expr);
1961 Set_Enumeration_Rep (Elit, Val);
1970 -- Aggregate is fully processed. Now we check that a full set of
1971 -- representations was given, and that they are in range and in order.
1972 -- These checks are only done if no other errors occurred.
1978 Elit := First_Literal (Enumtype);
1979 while Present (Elit) loop
1980 if No (Enumeration_Rep_Expr (Elit)) then
1981 Error_Msg_NE ("missing representation for&!", N, Elit);
1984 Val := Enumeration_Rep (Elit);
1986 if Min = No_Uint then
1990 if Val /= No_Uint then
1991 if Max /= No_Uint and then Val <= Max then
1993 ("enumeration value for& not ordered!",
1994 Enumeration_Rep_Expr (Elit), Elit);
2000 -- If there is at least one literal whose representation
2001 -- is not equal to the Pos value, then note that this
2002 -- enumeration type has a non-standard representation.
2004 if Val /= Enumeration_Pos (Elit) then
2005 Set_Has_Non_Standard_Rep (Base_Type (Enumtype));
2012 -- Now set proper size information
2015 Minsize : Uint := UI_From_Int (Minimum_Size (Enumtype));
2018 if Has_Size_Clause (Enumtype) then
2019 if Esize (Enumtype) >= Minsize then
2024 UI_From_Int (Minimum_Size (Enumtype, Biased => True));
2026 if Esize (Enumtype) < Minsize then
2027 Error_Msg_N ("previously given size is too small", N);
2030 Set_Has_Biased_Representation (Enumtype);
2035 Set_RM_Size (Enumtype, Minsize);
2036 Set_Enum_Esize (Enumtype);
2039 Set_RM_Size (Base_Type (Enumtype), RM_Size (Enumtype));
2040 Set_Esize (Base_Type (Enumtype), Esize (Enumtype));
2041 Set_Alignment (Base_Type (Enumtype), Alignment (Enumtype));
2045 -- We repeat the too late test in case it froze itself!
2047 if Rep_Item_Too_Late (Enumtype, N) then
2050 end Analyze_Enumeration_Representation_Clause;
2052 ----------------------------
2053 -- Analyze_Free_Statement --
2054 ----------------------------
2056 procedure Analyze_Free_Statement (N : Node_Id) is
2058 Analyze (Expression (N));
2059 end Analyze_Free_Statement;
2061 ------------------------------------------
2062 -- Analyze_Record_Representation_Clause --
2063 ------------------------------------------
2065 procedure Analyze_Record_Representation_Clause (N : Node_Id) is
2066 Loc : constant Source_Ptr := Sloc (N);
2067 Ident : constant Node_Id := Identifier (N);
2068 Rectype : Entity_Id;
2074 Hbit : Uint := Uint_0;
2079 Max_Bit_So_Far : Uint;
2080 -- Records the maximum bit position so far. If all field positions
2081 -- are monotonically increasing, then we can skip the circuit for
2082 -- checking for overlap, since no overlap is possible.
2084 Overlap_Check_Required : Boolean;
2085 -- Used to keep track of whether or not an overlap check is required
2087 Ccount : Natural := 0;
2088 -- Number of component clauses in record rep clause
2090 CR_Pragma : Node_Id := Empty;
2091 -- Points to N_Pragma node if Complete_Representation pragma present
2094 if Ignore_Rep_Clauses then
2099 Rectype := Entity (Ident);
2101 if Rectype = Any_Type
2102 or else Rep_Item_Too_Early (Rectype, N)
2106 Rectype := Underlying_Type (Rectype);
2109 -- First some basic error checks
2111 if not Is_Record_Type (Rectype) then
2113 ("record type required, found}", Ident, First_Subtype (Rectype));
2116 elsif Is_Unchecked_Union (Rectype) then
2118 ("record rep clause not allowed for Unchecked_Union", N);
2120 elsif Scope (Rectype) /= Current_Scope then
2121 Error_Msg_N ("type must be declared in this scope", N);
2124 elsif not Is_First_Subtype (Rectype) then
2125 Error_Msg_N ("cannot give record rep clause for subtype", N);
2128 elsif Has_Record_Rep_Clause (Rectype) then
2129 Error_Msg_N ("duplicate record rep clause ignored", N);
2132 elsif Rep_Item_Too_Late (Rectype, N) then
2136 if Present (Mod_Clause (N)) then
2138 Loc : constant Source_Ptr := Sloc (N);
2139 M : constant Node_Id := Mod_Clause (N);
2140 P : constant List_Id := Pragmas_Before (M);
2144 pragma Warnings (Off, Mod_Val);
2147 Check_Restriction (No_Obsolescent_Features, Mod_Clause (N));
2149 if Warn_On_Obsolescent_Feature then
2151 ("mod clause is an obsolescent feature (RM J.8)?", N);
2153 ("\use alignment attribute definition clause instead?", N);
2160 -- In ASIS_Mode mode, expansion is disabled, but we must convert
2161 -- the Mod clause into an alignment clause anyway, so that the
2162 -- back-end can compute and back-annotate properly the size and
2163 -- alignment of types that may include this record.
2165 -- This seems dubious, this destroys the source tree in a manner
2166 -- not detectable by ASIS ???
2168 if Operating_Mode = Check_Semantics
2172 Make_Attribute_Definition_Clause (Loc,
2173 Name => New_Reference_To (Base_Type (Rectype), Loc),
2174 Chars => Name_Alignment,
2175 Expression => Relocate_Node (Expression (M)));
2177 Set_From_At_Mod (AtM_Nod);
2178 Insert_After (N, AtM_Nod);
2179 Mod_Val := Get_Alignment_Value (Expression (AtM_Nod));
2180 Set_Mod_Clause (N, Empty);
2183 -- Get the alignment value to perform error checking
2185 Mod_Val := Get_Alignment_Value (Expression (M));
2191 -- Clear any existing component clauses for the type (this happens with
2192 -- derived types, where we are now overriding the original).
2194 Comp := First_Component_Or_Discriminant (Rectype);
2195 while Present (Comp) loop
2196 Set_Component_Clause (Comp, Empty);
2197 Next_Component_Or_Discriminant (Comp);
2200 -- All done if no component clauses
2202 CC := First (Component_Clauses (N));
2208 -- If a tag is present, then create a component clause that places it
2209 -- at the start of the record (otherwise gigi may place it after other
2210 -- fields that have rep clauses).
2212 Fent := First_Entity (Rectype);
2214 if Nkind (Fent) = N_Defining_Identifier
2215 and then Chars (Fent) = Name_uTag
2217 Set_Component_Bit_Offset (Fent, Uint_0);
2218 Set_Normalized_Position (Fent, Uint_0);
2219 Set_Normalized_First_Bit (Fent, Uint_0);
2220 Set_Normalized_Position_Max (Fent, Uint_0);
2221 Init_Esize (Fent, System_Address_Size);
2223 Set_Component_Clause (Fent,
2224 Make_Component_Clause (Loc,
2226 Make_Identifier (Loc,
2227 Chars => Name_uTag),
2230 Make_Integer_Literal (Loc,
2234 Make_Integer_Literal (Loc,
2238 Make_Integer_Literal (Loc,
2239 UI_From_Int (System_Address_Size))));
2241 Ccount := Ccount + 1;
2244 -- A representation like this applies to the base type
2246 Set_Has_Record_Rep_Clause (Base_Type (Rectype));
2247 Set_Has_Non_Standard_Rep (Base_Type (Rectype));
2248 Set_Has_Specified_Layout (Base_Type (Rectype));
2250 Max_Bit_So_Far := Uint_Minus_1;
2251 Overlap_Check_Required := False;
2253 -- Process the component clauses
2255 while Present (CC) loop
2259 if Nkind (CC) = N_Pragma then
2262 -- The only pragma of interest is Complete_Representation
2264 if Chars (CC) = Name_Complete_Representation then
2268 -- Processing for real component clause
2271 Ccount := Ccount + 1;
2272 Posit := Static_Integer (Position (CC));
2273 Fbit := Static_Integer (First_Bit (CC));
2274 Lbit := Static_Integer (Last_Bit (CC));
2277 and then Fbit /= No_Uint
2278 and then Lbit /= No_Uint
2282 ("position cannot be negative", Position (CC));
2286 ("first bit cannot be negative", First_Bit (CC));
2288 -- Values look OK, so find the corresponding record component
2289 -- Even though the syntax allows an attribute reference for
2290 -- implementation-defined components, GNAT does not allow the
2291 -- tag to get an explicit position.
2293 elsif Nkind (Component_Name (CC)) = N_Attribute_Reference then
2294 if Attribute_Name (Component_Name (CC)) = Name_Tag then
2295 Error_Msg_N ("position of tag cannot be specified", CC);
2297 Error_Msg_N ("illegal component name", CC);
2301 Comp := First_Entity (Rectype);
2302 while Present (Comp) loop
2303 exit when Chars (Comp) = Chars (Component_Name (CC));
2309 -- Maybe component of base type that is absent from
2310 -- statically constrained first subtype.
2312 Comp := First_Entity (Base_Type (Rectype));
2313 while Present (Comp) loop
2314 exit when Chars (Comp) = Chars (Component_Name (CC));
2321 ("component clause is for non-existent field", CC);
2323 elsif Present (Component_Clause (Comp)) then
2324 Error_Msg_Sloc := Sloc (Component_Clause (Comp));
2326 ("component clause previously given#", CC);
2329 -- Make reference for field in record rep clause and set
2330 -- appropriate entity field in the field identifier.
2333 (Comp, Component_Name (CC), Set_Ref => False);
2334 Set_Entity (Component_Name (CC), Comp);
2336 -- Update Fbit and Lbit to the actual bit number
2338 Fbit := Fbit + UI_From_Int (SSU) * Posit;
2339 Lbit := Lbit + UI_From_Int (SSU) * Posit;
2341 if Fbit <= Max_Bit_So_Far then
2342 Overlap_Check_Required := True;
2344 Max_Bit_So_Far := Lbit;
2347 if Has_Size_Clause (Rectype)
2348 and then Esize (Rectype) <= Lbit
2351 ("bit number out of range of specified size",
2354 Set_Component_Clause (Comp, CC);
2355 Set_Component_Bit_Offset (Comp, Fbit);
2356 Set_Esize (Comp, 1 + (Lbit - Fbit));
2357 Set_Normalized_First_Bit (Comp, Fbit mod SSU);
2358 Set_Normalized_Position (Comp, Fbit / SSU);
2360 Set_Normalized_Position_Max
2361 (Fent, Normalized_Position (Fent));
2363 if Is_Tagged_Type (Rectype)
2364 and then Fbit < System_Address_Size
2367 ("component overlaps tag field of&",
2371 -- This information is also set in the corresponding
2372 -- component of the base type, found by accessing the
2373 -- Original_Record_Component link if it is present.
2375 Ocomp := Original_Record_Component (Comp);
2382 (Component_Name (CC),
2387 Set_Has_Biased_Representation (Comp, Biased);
2389 if Present (Ocomp) then
2390 Set_Component_Clause (Ocomp, CC);
2391 Set_Component_Bit_Offset (Ocomp, Fbit);
2392 Set_Normalized_First_Bit (Ocomp, Fbit mod SSU);
2393 Set_Normalized_Position (Ocomp, Fbit / SSU);
2394 Set_Esize (Ocomp, 1 + (Lbit - Fbit));
2396 Set_Normalized_Position_Max
2397 (Ocomp, Normalized_Position (Ocomp));
2399 Set_Has_Biased_Representation
2400 (Ocomp, Has_Biased_Representation (Comp));
2403 if Esize (Comp) < 0 then
2404 Error_Msg_N ("component size is negative", CC);
2415 -- Now that we have processed all the component clauses, check for
2416 -- overlap. We have to leave this till last, since the components
2417 -- can appear in any arbitrary order in the representation clause.
2419 -- We do not need this check if all specified ranges were monotonic,
2420 -- as recorded by Overlap_Check_Required being False at this stage.
2422 -- This first section checks if there are any overlapping entries
2423 -- at all. It does this by sorting all entries and then seeing if
2424 -- there are any overlaps. If there are none, then that is decisive,
2425 -- but if there are overlaps, they may still be OK (they may result
2426 -- from fields in different variants).
2428 if Overlap_Check_Required then
2429 Overlap_Check1 : declare
2431 OC_Fbit : array (0 .. Ccount) of Uint;
2432 -- First-bit values for component clauses, the value is the
2433 -- offset of the first bit of the field from start of record.
2434 -- The zero entry is for use in sorting.
2436 OC_Lbit : array (0 .. Ccount) of Uint;
2437 -- Last-bit values for component clauses, the value is the
2438 -- offset of the last bit of the field from start of record.
2439 -- The zero entry is for use in sorting.
2441 OC_Count : Natural := 0;
2442 -- Count of entries in OC_Fbit and OC_Lbit
2444 function OC_Lt (Op1, Op2 : Natural) return Boolean;
2445 -- Compare routine for Sort
2447 procedure OC_Move (From : Natural; To : Natural);
2448 -- Move routine for Sort
2450 package Sorting is new GNAT.Heap_Sort_G (OC_Move, OC_Lt);
2452 function OC_Lt (Op1, Op2 : Natural) return Boolean is
2454 return OC_Fbit (Op1) < OC_Fbit (Op2);
2457 procedure OC_Move (From : Natural; To : Natural) is
2459 OC_Fbit (To) := OC_Fbit (From);
2460 OC_Lbit (To) := OC_Lbit (From);
2464 CC := First (Component_Clauses (N));
2465 while Present (CC) loop
2466 if Nkind (CC) /= N_Pragma then
2467 Posit := Static_Integer (Position (CC));
2468 Fbit := Static_Integer (First_Bit (CC));
2469 Lbit := Static_Integer (Last_Bit (CC));
2472 and then Fbit /= No_Uint
2473 and then Lbit /= No_Uint
2475 OC_Count := OC_Count + 1;
2476 Posit := Posit * SSU;
2477 OC_Fbit (OC_Count) := Fbit + Posit;
2478 OC_Lbit (OC_Count) := Lbit + Posit;
2485 Sorting.Sort (OC_Count);
2487 Overlap_Check_Required := False;
2488 for J in 1 .. OC_Count - 1 loop
2489 if OC_Lbit (J) >= OC_Fbit (J + 1) then
2490 Overlap_Check_Required := True;
2497 -- If Overlap_Check_Required is still True, then we have to do
2498 -- the full scale overlap check, since we have at least two fields
2499 -- that do overlap, and we need to know if that is OK since they
2500 -- are in the same variant, or whether we have a definite problem
2502 if Overlap_Check_Required then
2503 Overlap_Check2 : declare
2504 C1_Ent, C2_Ent : Entity_Id;
2505 -- Entities of components being checked for overlap
2508 -- Component_List node whose Component_Items are being checked
2511 -- Component declaration for component being checked
2514 C1_Ent := First_Entity (Base_Type (Rectype));
2516 -- Loop through all components in record. For each component check
2517 -- for overlap with any of the preceding elements on the component
2518 -- list containing the component, and also, if the component is in
2519 -- a variant, check against components outside the case structure.
2520 -- This latter test is repeated recursively up the variant tree.
2522 Main_Component_Loop : while Present (C1_Ent) loop
2523 if Ekind (C1_Ent) /= E_Component
2524 and then Ekind (C1_Ent) /= E_Discriminant
2526 goto Continue_Main_Component_Loop;
2529 -- Skip overlap check if entity has no declaration node. This
2530 -- happens with discriminants in constrained derived types.
2531 -- Probably we are missing some checks as a result, but that
2532 -- does not seem terribly serious ???
2534 if No (Declaration_Node (C1_Ent)) then
2535 goto Continue_Main_Component_Loop;
2538 Clist := Parent (List_Containing (Declaration_Node (C1_Ent)));
2540 -- Loop through component lists that need checking. Check the
2541 -- current component list and all lists in variants above us.
2543 Component_List_Loop : loop
2545 -- If derived type definition, go to full declaration
2546 -- If at outer level, check discriminants if there are any
2548 if Nkind (Clist) = N_Derived_Type_Definition then
2549 Clist := Parent (Clist);
2552 -- Outer level of record definition, check discriminants
2554 if Nkind (Clist) = N_Full_Type_Declaration
2555 or else Nkind (Clist) = N_Private_Type_Declaration
2557 if Has_Discriminants (Defining_Identifier (Clist)) then
2559 First_Discriminant (Defining_Identifier (Clist));
2561 while Present (C2_Ent) loop
2562 exit when C1_Ent = C2_Ent;
2563 Check_Component_Overlap (C1_Ent, C2_Ent);
2564 Next_Discriminant (C2_Ent);
2568 -- Record extension case
2570 elsif Nkind (Clist) = N_Derived_Type_Definition then
2573 -- Otherwise check one component list
2576 Citem := First (Component_Items (Clist));
2578 while Present (Citem) loop
2579 if Nkind (Citem) = N_Component_Declaration then
2580 C2_Ent := Defining_Identifier (Citem);
2581 exit when C1_Ent = C2_Ent;
2582 Check_Component_Overlap (C1_Ent, C2_Ent);
2589 -- Check for variants above us (the parent of the Clist can
2590 -- be a variant, in which case its parent is a variant part,
2591 -- and the parent of the variant part is a component list
2592 -- whose components must all be checked against the current
2593 -- component for overlap.
2595 if Nkind (Parent (Clist)) = N_Variant then
2596 Clist := Parent (Parent (Parent (Clist)));
2598 -- Check for possible discriminant part in record, this is
2599 -- treated essentially as another level in the recursion.
2600 -- For this case we have the parent of the component list
2601 -- is the record definition, and its parent is the full
2602 -- type declaration which contains the discriminant
2605 elsif Nkind (Parent (Clist)) = N_Record_Definition then
2606 Clist := Parent (Parent ((Clist)));
2608 -- If neither of these two cases, we are at the top of
2612 exit Component_List_Loop;
2614 end loop Component_List_Loop;
2616 <<Continue_Main_Component_Loop>>
2617 Next_Entity (C1_Ent);
2619 end loop Main_Component_Loop;
2623 -- For records that have component clauses for all components, and
2624 -- whose size is less than or equal to 32, we need to know the size
2625 -- in the front end to activate possible packed array processing
2626 -- where the component type is a record.
2628 -- At this stage Hbit + 1 represents the first unused bit from all
2629 -- the component clauses processed, so if the component clauses are
2630 -- complete, then this is the length of the record.
2632 -- For records longer than System.Storage_Unit, and for those where
2633 -- not all components have component clauses, the back end determines
2634 -- the length (it may for example be appopriate to round up the size
2635 -- to some convenient boundary, based on alignment considerations etc).
2637 if Unknown_RM_Size (Rectype)
2638 and then Hbit + 1 <= 32
2640 -- Nothing to do if at least one component with no component clause
2642 Comp := First_Component_Or_Discriminant (Rectype);
2643 while Present (Comp) loop
2644 exit when No (Component_Clause (Comp));
2645 Next_Component_Or_Discriminant (Comp);
2648 -- If we fall out of loop, all components have component clauses
2649 -- and so we can set the size to the maximum value.
2652 Set_RM_Size (Rectype, Hbit + 1);
2656 -- Check missing components if Complete_Representation pragma appeared
2658 if Present (CR_Pragma) then
2659 Comp := First_Component_Or_Discriminant (Rectype);
2660 while Present (Comp) loop
2661 if No (Component_Clause (Comp)) then
2663 ("missing component clause for &", CR_Pragma, Comp);
2666 Next_Component_Or_Discriminant (Comp);
2669 -- If no Complete_Representation pragma, warn if missing components
2671 elsif Warn_On_Unrepped_Components
2672 and then not Warnings_Off (Rectype)
2675 Num_Repped_Components : Nat := 0;
2676 Num_Unrepped_Components : Nat := 0;
2679 -- First count number of repped and unrepped components
2681 Comp := First_Component_Or_Discriminant (Rectype);
2682 while Present (Comp) loop
2683 if Present (Component_Clause (Comp)) then
2684 Num_Repped_Components := Num_Repped_Components + 1;
2686 Num_Unrepped_Components := Num_Unrepped_Components + 1;
2689 Next_Component_Or_Discriminant (Comp);
2692 -- We are only interested in the case where there is at least one
2693 -- unrepped component, and at least half the components have rep
2694 -- clauses. We figure that if less than half have them, then the
2695 -- partial rep clause is really intentional.
2697 if Num_Unrepped_Components > 0
2698 and then Num_Unrepped_Components < Num_Repped_Components
2700 Comp := First_Component_Or_Discriminant (Rectype);
2701 while Present (Comp) loop
2702 if No (Component_Clause (Comp))
2703 and then (Is_Scalar_Type (Underlying_Type (Etype (Comp)))
2704 or else Size_Known_At_Compile_Time
2705 (Underlying_Type (Etype (Comp))))
2707 Error_Msg_Sloc := Sloc (Comp);
2709 ("?no component clause given for & declared #",
2713 Next_Component_Or_Discriminant (Comp);
2718 end Analyze_Record_Representation_Clause;
2720 -----------------------------
2721 -- Check_Component_Overlap --
2722 -----------------------------
2724 procedure Check_Component_Overlap (C1_Ent, C2_Ent : Entity_Id) is
2726 if Present (Component_Clause (C1_Ent))
2727 and then Present (Component_Clause (C2_Ent))
2729 -- Exclude odd case where we have two tag fields in the same
2730 -- record, both at location zero. This seems a bit strange,
2731 -- but it seems to happen in some circumstances ???
2733 if Chars (C1_Ent) = Name_uTag
2734 and then Chars (C2_Ent) = Name_uTag
2739 -- Here we check if the two fields overlap
2742 S1 : constant Uint := Component_Bit_Offset (C1_Ent);
2743 S2 : constant Uint := Component_Bit_Offset (C2_Ent);
2744 E1 : constant Uint := S1 + Esize (C1_Ent);
2745 E2 : constant Uint := S2 + Esize (C2_Ent);
2748 if E2 <= S1 or else E1 <= S2 then
2752 Component_Name (Component_Clause (C2_Ent));
2753 Error_Msg_Sloc := Sloc (Error_Msg_Node_2);
2755 Component_Name (Component_Clause (C1_Ent));
2757 ("component& overlaps & #",
2758 Component_Name (Component_Clause (C1_Ent)));
2762 end Check_Component_Overlap;
2764 -----------------------------------
2765 -- Check_Constant_Address_Clause --
2766 -----------------------------------
2768 procedure Check_Constant_Address_Clause
2772 procedure Check_At_Constant_Address (Nod : Node_Id);
2773 -- Checks that the given node N represents a name whose 'Address
2774 -- is constant (in the same sense as OK_Constant_Address_Clause,
2775 -- i.e. the address value is the same at the point of declaration
2776 -- of U_Ent and at the time of elaboration of the address clause.
2778 procedure Check_Expr_Constants (Nod : Node_Id);
2779 -- Checks that Nod meets the requirements for a constant address
2780 -- clause in the sense of the enclosing procedure.
2782 procedure Check_List_Constants (Lst : List_Id);
2783 -- Check that all elements of list Lst meet the requirements for a
2784 -- constant address clause in the sense of the enclosing procedure.
2786 -------------------------------
2787 -- Check_At_Constant_Address --
2788 -------------------------------
2790 procedure Check_At_Constant_Address (Nod : Node_Id) is
2792 if Is_Entity_Name (Nod) then
2793 if Present (Address_Clause (Entity ((Nod)))) then
2795 ("invalid address clause for initialized object &!",
2798 ("address for& cannot" &
2799 " depend on another address clause! (RM 13.1(22))!",
2802 elsif In_Same_Source_Unit (Entity (Nod), U_Ent)
2803 and then Sloc (U_Ent) < Sloc (Entity (Nod))
2806 ("invalid address clause for initialized object &!",
2808 Error_Msg_Name_1 := Chars (Entity (Nod));
2809 Error_Msg_Name_2 := Chars (U_Ent);
2811 ("\% must be defined before % (RM 13.1(22))!",
2815 elsif Nkind (Nod) = N_Selected_Component then
2817 T : constant Entity_Id := Etype (Prefix (Nod));
2820 if (Is_Record_Type (T)
2821 and then Has_Discriminants (T))
2824 and then Is_Record_Type (Designated_Type (T))
2825 and then Has_Discriminants (Designated_Type (T)))
2828 ("invalid address clause for initialized object &!",
2831 ("\address cannot depend on component" &
2832 " of discriminated record (RM 13.1(22))!",
2835 Check_At_Constant_Address (Prefix (Nod));
2839 elsif Nkind (Nod) = N_Indexed_Component then
2840 Check_At_Constant_Address (Prefix (Nod));
2841 Check_List_Constants (Expressions (Nod));
2844 Check_Expr_Constants (Nod);
2846 end Check_At_Constant_Address;
2848 --------------------------
2849 -- Check_Expr_Constants --
2850 --------------------------
2852 procedure Check_Expr_Constants (Nod : Node_Id) is
2853 Loc_U_Ent : constant Source_Ptr := Sloc (U_Ent);
2854 Ent : Entity_Id := Empty;
2857 if Nkind (Nod) in N_Has_Etype
2858 and then Etype (Nod) = Any_Type
2864 when N_Empty | N_Error =>
2867 when N_Identifier | N_Expanded_Name =>
2868 Ent := Entity (Nod);
2870 -- We need to look at the original node if it is different
2871 -- from the node, since we may have rewritten things and
2872 -- substituted an identifier representing the rewrite.
2874 if Original_Node (Nod) /= Nod then
2875 Check_Expr_Constants (Original_Node (Nod));
2877 -- If the node is an object declaration without initial
2878 -- value, some code has been expanded, and the expression
2879 -- is not constant, even if the constituents might be
2880 -- acceptable, as in A'Address + offset.
2882 if Ekind (Ent) = E_Variable
2883 and then Nkind (Declaration_Node (Ent))
2884 = N_Object_Declaration
2886 No (Expression (Declaration_Node (Ent)))
2889 ("invalid address clause for initialized object &!",
2892 -- If entity is constant, it may be the result of expanding
2893 -- a check. We must verify that its declaration appears
2894 -- before the object in question, else we also reject the
2897 elsif Ekind (Ent) = E_Constant
2898 and then In_Same_Source_Unit (Ent, U_Ent)
2899 and then Sloc (Ent) > Loc_U_Ent
2902 ("invalid address clause for initialized object &!",
2909 -- Otherwise look at the identifier and see if it is OK
2911 if Ekind (Ent) = E_Named_Integer
2913 Ekind (Ent) = E_Named_Real
2920 Ekind (Ent) = E_Constant
2922 Ekind (Ent) = E_In_Parameter
2924 -- This is the case where we must have Ent defined
2925 -- before U_Ent. Clearly if they are in different
2926 -- units this requirement is met since the unit
2927 -- containing Ent is already processed.
2929 if not In_Same_Source_Unit (Ent, U_Ent) then
2932 -- Otherwise location of Ent must be before the
2933 -- location of U_Ent, that's what prior defined means.
2935 elsif Sloc (Ent) < Loc_U_Ent then
2940 ("invalid address clause for initialized object &!",
2942 Error_Msg_Name_1 := Chars (Ent);
2943 Error_Msg_Name_2 := Chars (U_Ent);
2945 ("\% must be defined before % (RM 13.1(22))!",
2949 elsif Nkind (Original_Node (Nod)) = N_Function_Call then
2950 Check_Expr_Constants (Original_Node (Nod));
2954 ("invalid address clause for initialized object &!",
2957 if Comes_From_Source (Ent) then
2958 Error_Msg_Name_1 := Chars (Ent);
2960 ("\reference to variable% not allowed"
2961 & " (RM 13.1(22))!", Nod);
2964 ("non-static expression not allowed"
2965 & " (RM 13.1(22))!", Nod);
2969 when N_Integer_Literal =>
2971 -- If this is a rewritten unchecked conversion, in a system
2972 -- where Address is an integer type, always use the base type
2973 -- for a literal value. This is user-friendly and prevents
2974 -- order-of-elaboration issues with instances of unchecked
2977 if Nkind (Original_Node (Nod)) = N_Function_Call then
2978 Set_Etype (Nod, Base_Type (Etype (Nod)));
2981 when N_Real_Literal |
2983 N_Character_Literal =>
2987 Check_Expr_Constants (Low_Bound (Nod));
2988 Check_Expr_Constants (High_Bound (Nod));
2990 when N_Explicit_Dereference =>
2991 Check_Expr_Constants (Prefix (Nod));
2993 when N_Indexed_Component =>
2994 Check_Expr_Constants (Prefix (Nod));
2995 Check_List_Constants (Expressions (Nod));
2998 Check_Expr_Constants (Prefix (Nod));
2999 Check_Expr_Constants (Discrete_Range (Nod));
3001 when N_Selected_Component =>
3002 Check_Expr_Constants (Prefix (Nod));
3004 when N_Attribute_Reference =>
3005 if Attribute_Name (Nod) = Name_Address
3007 Attribute_Name (Nod) = Name_Access
3009 Attribute_Name (Nod) = Name_Unchecked_Access
3011 Attribute_Name (Nod) = Name_Unrestricted_Access
3013 Check_At_Constant_Address (Prefix (Nod));
3016 Check_Expr_Constants (Prefix (Nod));
3017 Check_List_Constants (Expressions (Nod));
3021 Check_List_Constants (Component_Associations (Nod));
3022 Check_List_Constants (Expressions (Nod));
3024 when N_Component_Association =>
3025 Check_Expr_Constants (Expression (Nod));
3027 when N_Extension_Aggregate =>
3028 Check_Expr_Constants (Ancestor_Part (Nod));
3029 Check_List_Constants (Component_Associations (Nod));
3030 Check_List_Constants (Expressions (Nod));
3035 when N_Binary_Op | N_And_Then | N_Or_Else | N_Membership_Test =>
3036 Check_Expr_Constants (Left_Opnd (Nod));
3037 Check_Expr_Constants (Right_Opnd (Nod));
3040 Check_Expr_Constants (Right_Opnd (Nod));
3042 when N_Type_Conversion |
3043 N_Qualified_Expression |
3045 Check_Expr_Constants (Expression (Nod));
3047 when N_Unchecked_Type_Conversion =>
3048 Check_Expr_Constants (Expression (Nod));
3050 -- If this is a rewritten unchecked conversion, subtypes
3051 -- in this node are those created within the instance.
3052 -- To avoid order of elaboration issues, replace them
3053 -- with their base types. Note that address clauses can
3054 -- cause order of elaboration problems because they are
3055 -- elaborated by the back-end at the point of definition,
3056 -- and may mention entities declared in between (as long
3057 -- as everything is static). It is user-friendly to allow
3058 -- unchecked conversions in this context.
3060 if Nkind (Original_Node (Nod)) = N_Function_Call then
3061 Set_Etype (Expression (Nod),
3062 Base_Type (Etype (Expression (Nod))));
3063 Set_Etype (Nod, Base_Type (Etype (Nod)));
3066 when N_Function_Call =>
3067 if not Is_Pure (Entity (Name (Nod))) then
3069 ("invalid address clause for initialized object &!",
3073 ("\function & is not pure (RM 13.1(22))!",
3074 Nod, Entity (Name (Nod)));
3077 Check_List_Constants (Parameter_Associations (Nod));
3080 when N_Parameter_Association =>
3081 Check_Expr_Constants (Explicit_Actual_Parameter (Nod));
3085 ("invalid address clause for initialized object &!",
3088 ("\must be constant defined before& (RM 13.1(22))!",
3091 end Check_Expr_Constants;
3093 --------------------------
3094 -- Check_List_Constants --
3095 --------------------------
3097 procedure Check_List_Constants (Lst : List_Id) is
3101 if Present (Lst) then
3102 Nod1 := First (Lst);
3103 while Present (Nod1) loop
3104 Check_Expr_Constants (Nod1);
3108 end Check_List_Constants;
3110 -- Start of processing for Check_Constant_Address_Clause
3113 Check_Expr_Constants (Expr);
3114 end Check_Constant_Address_Clause;
3120 procedure Check_Size
3124 Biased : out Boolean)
3126 UT : constant Entity_Id := Underlying_Type (T);
3132 -- Dismiss cases for generic types or types with previous errors
3135 or else UT = Any_Type
3136 or else Is_Generic_Type (UT)
3137 or else Is_Generic_Type (Root_Type (UT))
3141 -- Check case of bit packed array
3143 elsif Is_Array_Type (UT)
3144 and then Known_Static_Component_Size (UT)
3145 and then Is_Bit_Packed_Array (UT)
3153 Asiz := Component_Size (UT);
3154 Indx := First_Index (UT);
3156 Ityp := Etype (Indx);
3158 -- If non-static bound, then we are not in the business of
3159 -- trying to check the length, and indeed an error will be
3160 -- issued elsewhere, since sizes of non-static array types
3161 -- cannot be set implicitly or explicitly.
3163 if not Is_Static_Subtype (Ityp) then
3167 -- Otherwise accumulate next dimension
3169 Asiz := Asiz * (Expr_Value (Type_High_Bound (Ityp)) -
3170 Expr_Value (Type_Low_Bound (Ityp)) +
3174 exit when No (Indx);
3180 Error_Msg_Uint_1 := Asiz;
3182 ("size for& too small, minimum allowed is ^", N, T);
3183 Set_Esize (T, Asiz);
3184 Set_RM_Size (T, Asiz);
3188 -- All other composite types are ignored
3190 elsif Is_Composite_Type (UT) then
3193 -- For fixed-point types, don't check minimum if type is not frozen,
3194 -- since we don't know all the characteristics of the type that can
3195 -- affect the size (e.g. a specified small) till freeze time.
3197 elsif Is_Fixed_Point_Type (UT)
3198 and then not Is_Frozen (UT)
3202 -- Cases for which a minimum check is required
3205 -- Ignore if specified size is correct for the type
3207 if Known_Esize (UT) and then Siz = Esize (UT) then
3211 -- Otherwise get minimum size
3213 M := UI_From_Int (Minimum_Size (UT));
3217 -- Size is less than minimum size, but one possibility remains
3218 -- that we can manage with the new size if we bias the type
3220 M := UI_From_Int (Minimum_Size (UT, Biased => True));
3223 Error_Msg_Uint_1 := M;
3225 ("size for& too small, minimum allowed is ^", N, T);
3235 -------------------------
3236 -- Get_Alignment_Value --
3237 -------------------------
3239 function Get_Alignment_Value (Expr : Node_Id) return Uint is
3240 Align : constant Uint := Static_Integer (Expr);
3243 if Align = No_Uint then
3246 elsif Align <= 0 then
3247 Error_Msg_N ("alignment value must be positive", Expr);
3251 for J in Int range 0 .. 64 loop
3253 M : constant Uint := Uint_2 ** J;
3256 exit when M = Align;
3260 ("alignment value must be power of 2", Expr);
3268 end Get_Alignment_Value;
3274 procedure Initialize is
3276 Unchecked_Conversions.Init;
3279 -------------------------
3280 -- Is_Operational_Item --
3281 -------------------------
3283 function Is_Operational_Item (N : Node_Id) return Boolean is
3285 if Nkind (N) /= N_Attribute_Definition_Clause then
3289 Id : constant Attribute_Id := Get_Attribute_Id (Chars (N));
3292 return Id = Attribute_Input
3293 or else Id = Attribute_Output
3294 or else Id = Attribute_Read
3295 or else Id = Attribute_Write
3296 or else Id = Attribute_External_Tag;
3299 end Is_Operational_Item;
3305 function Minimum_Size
3307 Biased : Boolean := False) return Nat
3309 Lo : Uint := No_Uint;
3310 Hi : Uint := No_Uint;
3311 LoR : Ureal := No_Ureal;
3312 HiR : Ureal := No_Ureal;
3313 LoSet : Boolean := False;
3314 HiSet : Boolean := False;
3318 R_Typ : constant Entity_Id := Root_Type (T);
3321 -- If bad type, return 0
3323 if T = Any_Type then
3326 -- For generic types, just return zero. There cannot be any legitimate
3327 -- need to know such a size, but this routine may be called with a
3328 -- generic type as part of normal processing.
3330 elsif Is_Generic_Type (R_Typ)
3331 or else R_Typ = Any_Type
3335 -- Access types. Normally an access type cannot have a size smaller
3336 -- than the size of System.Address. The exception is on VMS, where
3337 -- we have short and long addresses, and it is possible for an access
3338 -- type to have a short address size (and thus be less than the size
3339 -- of System.Address itself). We simply skip the check for VMS, and
3340 -- leave the back end to do the check.
3342 elsif Is_Access_Type (T) then
3343 if OpenVMS_On_Target then
3346 return System_Address_Size;
3349 -- Floating-point types
3351 elsif Is_Floating_Point_Type (T) then
3352 return UI_To_Int (Esize (R_Typ));
3356 elsif Is_Discrete_Type (T) then
3358 -- The following loop is looking for the nearest compile time
3359 -- known bounds following the ancestor subtype chain. The idea
3360 -- is to find the most restrictive known bounds information.
3364 if Ancest = Any_Type or else Etype (Ancest) = Any_Type then
3369 if Compile_Time_Known_Value (Type_Low_Bound (Ancest)) then
3370 Lo := Expr_Rep_Value (Type_Low_Bound (Ancest));
3377 if Compile_Time_Known_Value (Type_High_Bound (Ancest)) then
3378 Hi := Expr_Rep_Value (Type_High_Bound (Ancest));
3384 Ancest := Ancestor_Subtype (Ancest);
3387 Ancest := Base_Type (T);
3389 if Is_Generic_Type (Ancest) then
3395 -- Fixed-point types. We can't simply use Expr_Value to get the
3396 -- Corresponding_Integer_Value values of the bounds, since these
3397 -- do not get set till the type is frozen, and this routine can
3398 -- be called before the type is frozen. Similarly the test for
3399 -- bounds being static needs to include the case where we have
3400 -- unanalyzed real literals for the same reason.
3402 elsif Is_Fixed_Point_Type (T) then
3404 -- The following loop is looking for the nearest compile time
3405 -- known bounds following the ancestor subtype chain. The idea
3406 -- is to find the most restrictive known bounds information.
3410 if Ancest = Any_Type or else Etype (Ancest) = Any_Type then
3415 if Nkind (Type_Low_Bound (Ancest)) = N_Real_Literal
3416 or else Compile_Time_Known_Value (Type_Low_Bound (Ancest))
3418 LoR := Expr_Value_R (Type_Low_Bound (Ancest));
3425 if Nkind (Type_High_Bound (Ancest)) = N_Real_Literal
3426 or else Compile_Time_Known_Value (Type_High_Bound (Ancest))
3428 HiR := Expr_Value_R (Type_High_Bound (Ancest));
3434 Ancest := Ancestor_Subtype (Ancest);
3437 Ancest := Base_Type (T);
3439 if Is_Generic_Type (Ancest) then
3445 Lo := UR_To_Uint (LoR / Small_Value (T));
3446 Hi := UR_To_Uint (HiR / Small_Value (T));
3448 -- No other types allowed
3451 raise Program_Error;
3454 -- Fall through with Hi and Lo set. Deal with biased case
3456 if (Biased and then not Is_Fixed_Point_Type (T))
3457 or else Has_Biased_Representation (T)
3463 -- Signed case. Note that we consider types like range 1 .. -1 to be
3464 -- signed for the purpose of computing the size, since the bounds
3465 -- have to be accomodated in the base type.
3467 if Lo < 0 or else Hi < 0 then
3471 -- S = size, B = 2 ** (size - 1) (can accommodate -B .. +(B - 1))
3472 -- Note that we accommodate the case where the bounds cross. This
3473 -- can happen either because of the way the bounds are declared
3474 -- or because of the algorithm in Freeze_Fixed_Point_Type.
3488 -- If both bounds are positive, make sure that both are represen-
3489 -- table in the case where the bounds are crossed. This can happen
3490 -- either because of the way the bounds are declared, or because of
3491 -- the algorithm in Freeze_Fixed_Point_Type.
3497 -- S = size, (can accommodate 0 .. (2**size - 1))
3500 while Hi >= Uint_2 ** S loop
3508 ---------------------------
3509 -- New_Stream_Subprogram --
3510 ---------------------------
3512 procedure New_Stream_Subprogram
3516 Nam : TSS_Name_Type)
3518 Loc : constant Source_Ptr := Sloc (N);
3519 Sname : constant Name_Id := Make_TSS_Name (Base_Type (Ent), Nam);
3520 Subp_Id : Entity_Id;
3521 Subp_Decl : Node_Id;
3525 Defer_Declaration : constant Boolean :=
3526 Is_Tagged_Type (Ent) or else Is_Private_Type (Ent);
3527 -- For a tagged type, there is a declaration for each stream attribute
3528 -- at the freeze point, and we must generate only a completion of this
3529 -- declaration. We do the same for private types, because the full view
3530 -- might be tagged. Otherwise we generate a declaration at the point of
3531 -- the attribute definition clause.
3533 function Build_Spec return Node_Id;
3534 -- Used for declaration and renaming declaration, so that this is
3535 -- treated as a renaming_as_body.
3541 function Build_Spec return Node_Id is
3542 Out_P : constant Boolean := (Nam = TSS_Stream_Read);
3545 T_Ref : constant Node_Id := New_Reference_To (Etyp, Loc);
3548 Subp_Id := Make_Defining_Identifier (Loc, Sname);
3550 -- S : access Root_Stream_Type'Class
3552 Formals := New_List (
3553 Make_Parameter_Specification (Loc,
3554 Defining_Identifier =>
3555 Make_Defining_Identifier (Loc, Name_S),
3557 Make_Access_Definition (Loc,
3560 Designated_Type (Etype (F)), Loc))));
3562 if Nam = TSS_Stream_Input then
3563 Spec := Make_Function_Specification (Loc,
3564 Defining_Unit_Name => Subp_Id,
3565 Parameter_Specifications => Formals,
3566 Result_Definition => T_Ref);
3571 Make_Parameter_Specification (Loc,
3572 Defining_Identifier => Make_Defining_Identifier (Loc, Name_V),
3573 Out_Present => Out_P,
3574 Parameter_Type => T_Ref));
3576 Spec := Make_Procedure_Specification (Loc,
3577 Defining_Unit_Name => Subp_Id,
3578 Parameter_Specifications => Formals);
3584 -- Start of processing for New_Stream_Subprogram
3587 F := First_Formal (Subp);
3589 if Ekind (Subp) = E_Procedure then
3590 Etyp := Etype (Next_Formal (F));
3592 Etyp := Etype (Subp);
3595 -- Prepare subprogram declaration and insert it as an action on the
3596 -- clause node. The visibility for this entity is used to test for
3597 -- visibility of the attribute definition clause (in the sense of
3598 -- 8.3(23) as amended by AI-195).
3600 if not Defer_Declaration then
3602 Make_Subprogram_Declaration (Loc,
3603 Specification => Build_Spec);
3605 -- For a tagged type, there is always a visible declaration for each
3606 -- stream TSS (it is a predefined primitive operation), and the
3607 -- completion of this declaration occurs at the freeze point, which is
3608 -- not always visible at places where the attribute definition clause is
3609 -- visible. So, we create a dummy entity here for the purpose of
3610 -- tracking the visibility of the attribute definition clause itself.
3614 Make_Defining_Identifier (Loc,
3615 Chars => New_External_Name (Sname, 'V'));
3617 Make_Object_Declaration (Loc,
3618 Defining_Identifier => Subp_Id,
3619 Object_Definition => New_Occurrence_Of (Standard_Boolean, Loc));
3622 Insert_Action (N, Subp_Decl);
3623 Set_Entity (N, Subp_Id);
3626 Make_Subprogram_Renaming_Declaration (Loc,
3627 Specification => Build_Spec,
3628 Name => New_Reference_To (Subp, Loc));
3630 if Defer_Declaration then
3631 Set_TSS (Base_Type (Ent), Subp_Id);
3633 Insert_Action (N, Subp_Decl);
3634 Copy_TSS (Subp_Id, Base_Type (Ent));
3636 end New_Stream_Subprogram;
3638 ------------------------
3639 -- Rep_Item_Too_Early --
3640 ------------------------
3642 function Rep_Item_Too_Early (T : Entity_Id; N : Node_Id) return Boolean is
3644 -- Cannot apply non-operational rep items to generic types
3646 if Is_Operational_Item (N) then
3650 and then Is_Generic_Type (Root_Type (T))
3653 ("representation item not allowed for generic type", N);
3657 -- Otherwise check for incompleted type
3659 if Is_Incomplete_Or_Private_Type (T)
3660 and then No (Underlying_Type (T))
3663 ("representation item must be after full type declaration", N);
3666 -- If the type has incompleted components, a representation clause is
3667 -- illegal but stream attributes and Convention pragmas are correct.
3669 elsif Has_Private_Component (T) then
3670 if Nkind (N) = N_Pragma then
3674 ("representation item must appear after type is fully defined",
3681 end Rep_Item_Too_Early;
3683 -----------------------
3684 -- Rep_Item_Too_Late --
3685 -----------------------
3687 function Rep_Item_Too_Late
3690 FOnly : Boolean := False) return Boolean
3693 Parent_Type : Entity_Id;
3696 -- Output the too late message. Note that this is not considered a
3697 -- serious error, since the effect is simply that we ignore the
3698 -- representation clause in this case.
3704 procedure Too_Late is
3706 Error_Msg_N ("|representation item appears too late!", N);
3709 -- Start of processing for Rep_Item_Too_Late
3712 -- First make sure entity is not frozen (RM 13.1(9)). Exclude imported
3713 -- types, which may be frozen if they appear in a representation clause
3714 -- for a local type.
3717 and then not From_With_Type (T)
3720 S := First_Subtype (T);
3722 if Present (Freeze_Node (S)) then
3724 ("?no more representation items for }", Freeze_Node (S), S);
3729 -- Check for case of non-tagged derived type whose parent either has
3730 -- primitive operations, or is a by reference type (RM 13.1(10)).
3734 and then Is_Derived_Type (T)
3735 and then not Is_Tagged_Type (T)
3737 Parent_Type := Etype (Base_Type (T));
3739 if Has_Primitive_Operations (Parent_Type) then
3742 ("primitive operations already defined for&!", N, Parent_Type);
3745 elsif Is_By_Reference_Type (Parent_Type) then
3748 ("parent type & is a by reference type!", N, Parent_Type);
3753 -- No error, link item into head of chain of rep items for the entity
3755 Record_Rep_Item (T, N);
3757 end Rep_Item_Too_Late;
3759 -------------------------
3760 -- Same_Representation --
3761 -------------------------
3763 function Same_Representation (Typ1, Typ2 : Entity_Id) return Boolean is
3764 T1 : constant Entity_Id := Underlying_Type (Typ1);
3765 T2 : constant Entity_Id := Underlying_Type (Typ2);
3768 -- A quick check, if base types are the same, then we definitely have
3769 -- the same representation, because the subtype specific representation
3770 -- attributes (Size and Alignment) do not affect representation from
3771 -- the point of view of this test.
3773 if Base_Type (T1) = Base_Type (T2) then
3776 elsif Is_Private_Type (Base_Type (T2))
3777 and then Base_Type (T1) = Full_View (Base_Type (T2))
3782 -- Tagged types never have differing representations
3784 if Is_Tagged_Type (T1) then
3788 -- Representations are definitely different if conventions differ
3790 if Convention (T1) /= Convention (T2) then
3794 -- Representations are different if component alignments differ
3796 if (Is_Record_Type (T1) or else Is_Array_Type (T1))
3798 (Is_Record_Type (T2) or else Is_Array_Type (T2))
3799 and then Component_Alignment (T1) /= Component_Alignment (T2)
3804 -- For arrays, the only real issue is component size. If we know the
3805 -- component size for both arrays, and it is the same, then that's
3806 -- good enough to know we don't have a change of representation.
3808 if Is_Array_Type (T1) then
3809 if Known_Component_Size (T1)
3810 and then Known_Component_Size (T2)
3811 and then Component_Size (T1) = Component_Size (T2)
3817 -- Types definitely have same representation if neither has non-standard
3818 -- representation since default representations are always consistent.
3819 -- If only one has non-standard representation, and the other does not,
3820 -- then we consider that they do not have the same representation. They
3821 -- might, but there is no way of telling early enough.
3823 if Has_Non_Standard_Rep (T1) then
3824 if not Has_Non_Standard_Rep (T2) then
3828 return not Has_Non_Standard_Rep (T2);
3831 -- Here the two types both have non-standard representation, and we
3832 -- need to determine if they have the same non-standard representation
3834 -- For arrays, we simply need to test if the component sizes are the
3835 -- same. Pragma Pack is reflected in modified component sizes, so this
3836 -- check also deals with pragma Pack.
3838 if Is_Array_Type (T1) then
3839 return Component_Size (T1) = Component_Size (T2);
3841 -- Tagged types always have the same representation, because it is not
3842 -- possible to specify different representations for common fields.
3844 elsif Is_Tagged_Type (T1) then
3847 -- Case of record types
3849 elsif Is_Record_Type (T1) then
3851 -- Packed status must conform
3853 if Is_Packed (T1) /= Is_Packed (T2) then
3856 -- Otherwise we must check components. Typ2 maybe a constrained
3857 -- subtype with fewer components, so we compare the components
3858 -- of the base types.
3861 Record_Case : declare
3862 CD1, CD2 : Entity_Id;
3864 function Same_Rep return Boolean;
3865 -- CD1 and CD2 are either components or discriminants. This
3866 -- function tests whether the two have the same representation
3872 function Same_Rep return Boolean is
3874 if No (Component_Clause (CD1)) then
3875 return No (Component_Clause (CD2));
3879 Present (Component_Clause (CD2))
3881 Component_Bit_Offset (CD1) = Component_Bit_Offset (CD2)
3883 Esize (CD1) = Esize (CD2);
3887 -- Start processing for Record_Case
3890 if Has_Discriminants (T1) then
3891 CD1 := First_Discriminant (T1);
3892 CD2 := First_Discriminant (T2);
3894 -- The number of discriminants may be different if the
3895 -- derived type has fewer (constrained by values). The
3896 -- invisible discriminants retain the representation of
3897 -- the original, so the discrepancy does not per se
3898 -- indicate a different representation.
3901 and then Present (CD2)
3903 if not Same_Rep then
3906 Next_Discriminant (CD1);
3907 Next_Discriminant (CD2);
3912 CD1 := First_Component (Underlying_Type (Base_Type (T1)));
3913 CD2 := First_Component (Underlying_Type (Base_Type (T2)));
3915 while Present (CD1) loop
3916 if not Same_Rep then
3919 Next_Component (CD1);
3920 Next_Component (CD2);
3928 -- For enumeration types, we must check each literal to see if the
3929 -- representation is the same. Note that we do not permit enumeration
3930 -- reprsentation clauses for Character and Wide_Character, so these
3931 -- cases were already dealt with.
3933 elsif Is_Enumeration_Type (T1) then
3935 Enumeration_Case : declare
3939 L1 := First_Literal (T1);
3940 L2 := First_Literal (T2);
3942 while Present (L1) loop
3943 if Enumeration_Rep (L1) /= Enumeration_Rep (L2) then
3953 end Enumeration_Case;
3955 -- Any other types have the same representation for these purposes
3960 end Same_Representation;
3962 --------------------
3963 -- Set_Enum_Esize --
3964 --------------------
3966 procedure Set_Enum_Esize (T : Entity_Id) is
3974 -- Find the minimum standard size (8,16,32,64) that fits
3976 Lo := Enumeration_Rep (Entity (Type_Low_Bound (T)));
3977 Hi := Enumeration_Rep (Entity (Type_High_Bound (T)));
3980 if Lo >= -Uint_2**07 and then Hi < Uint_2**07 then
3981 Sz := Standard_Character_Size; -- May be > 8 on some targets
3983 elsif Lo >= -Uint_2**15 and then Hi < Uint_2**15 then
3986 elsif Lo >= -Uint_2**31 and then Hi < Uint_2**31 then
3989 else pragma Assert (Lo >= -Uint_2**63 and then Hi < Uint_2**63);
3994 if Hi < Uint_2**08 then
3995 Sz := Standard_Character_Size; -- May be > 8 on some targets
3997 elsif Hi < Uint_2**16 then
4000 elsif Hi < Uint_2**32 then
4003 else pragma Assert (Hi < Uint_2**63);
4008 -- That minimum is the proper size unless we have a foreign convention
4009 -- and the size required is 32 or less, in which case we bump the size
4010 -- up to 32. This is required for C and C++ and seems reasonable for
4011 -- all other foreign conventions.
4013 if Has_Foreign_Convention (T)
4014 and then Esize (T) < Standard_Integer_Size
4016 Init_Esize (T, Standard_Integer_Size);
4022 ------------------------------
4023 -- Validate_Address_Clauses --
4024 ------------------------------
4026 procedure Validate_Address_Clauses is
4028 for J in Address_Clause_Checks.First .. Address_Clause_Checks.Last loop
4030 ACCR : Address_Clause_Check_Record
4031 renames Address_Clause_Checks.Table (J);
4040 -- Skip processing of this entry if warning already posted
4042 if not Address_Warning_Posted (ACCR.N) then
4044 -- Get alignments. Really we should always have the alignment
4045 -- of the objects properly back annotated, but right now the
4046 -- back end fails to back annotate for address clauses???
4048 if Known_Alignment (ACCR.X) then
4049 X_Alignment := Alignment (ACCR.X);
4051 X_Alignment := Alignment (Etype (ACCR.X));
4054 if Known_Alignment (ACCR.Y) then
4055 Y_Alignment := Alignment (ACCR.Y);
4057 Y_Alignment := Alignment (Etype (ACCR.Y));
4060 -- Similarly obtain sizes
4062 if Known_Esize (ACCR.X) then
4063 X_Size := Esize (ACCR.X);
4065 X_Size := Esize (Etype (ACCR.X));
4068 if Known_Esize (ACCR.Y) then
4069 Y_Size := Esize (ACCR.Y);
4071 Y_Size := Esize (Etype (ACCR.Y));
4074 -- Check for large object overlaying smaller one
4077 and then X_Size > Uint_0
4078 and then X_Size > Y_Size
4081 ("?size for overlaid object is too small", ACCR.N);
4082 Error_Msg_Uint_1 := X_Size;
4084 ("\?size of & is ^", ACCR.N, ACCR.X);
4085 Error_Msg_Uint_1 := Y_Size;
4087 ("\?size of & is ^", ACCR.N, ACCR.Y);
4089 -- Check for inadequate alignment. Again the defensive check
4090 -- on Y_Alignment should not be needed, but because of the
4091 -- failure in back end annotation, we can have an alignment
4094 -- Note: we do not check alignments if we gave a size
4095 -- warning, since it would likely be redundant.
4097 elsif Y_Alignment /= Uint_0
4098 and then Y_Alignment < X_Alignment
4101 ("?specified address for& may be inconsistent "
4105 ("\?program execution may be erroneous (RM 13.3(27))",
4107 Error_Msg_Uint_1 := X_Alignment;
4109 ("\?alignment of & is ^",
4111 Error_Msg_Uint_1 := Y_Alignment;
4113 ("\?alignment of & is ^",
4119 end Validate_Address_Clauses;
4121 -----------------------------------
4122 -- Validate_Unchecked_Conversion --
4123 -----------------------------------
4125 procedure Validate_Unchecked_Conversion
4127 Act_Unit : Entity_Id)
4134 -- Obtain source and target types. Note that we call Ancestor_Subtype
4135 -- here because the processing for generic instantiation always makes
4136 -- subtypes, and we want the original frozen actual types.
4138 -- If we are dealing with private types, then do the check on their
4139 -- fully declared counterparts if the full declarations have been
4140 -- encountered (they don't have to be visible, but they must exist!)
4142 Source := Ancestor_Subtype (Etype (First_Formal (Act_Unit)));
4144 if Is_Private_Type (Source)
4145 and then Present (Underlying_Type (Source))
4147 Source := Underlying_Type (Source);
4150 Target := Ancestor_Subtype (Etype (Act_Unit));
4152 -- If either type is generic, the instantiation happens within a
4153 -- generic unit, and there is nothing to check. The proper check
4154 -- will happen when the enclosing generic is instantiated.
4156 if Is_Generic_Type (Source) or else Is_Generic_Type (Target) then
4160 if Is_Private_Type (Target)
4161 and then Present (Underlying_Type (Target))
4163 Target := Underlying_Type (Target);
4166 -- Source may be unconstrained array, but not target
4168 if Is_Array_Type (Target)
4169 and then not Is_Constrained (Target)
4172 ("unchecked conversion to unconstrained array not allowed", N);
4176 -- Warn if conversion between two different convention pointers
4178 if Is_Access_Type (Target)
4179 and then Is_Access_Type (Source)
4180 and then Convention (Target) /= Convention (Source)
4181 and then Warn_On_Unchecked_Conversion
4184 ("?conversion between pointers with different conventions!", N);
4187 -- Make entry in unchecked conversion table for later processing
4188 -- by Validate_Unchecked_Conversions, which will check sizes and
4189 -- alignments (using values set by the back-end where possible).
4190 -- This is only done if the appropriate warning is active
4192 if Warn_On_Unchecked_Conversion then
4193 Unchecked_Conversions.Append
4194 (New_Val => UC_Entry'
4199 -- If both sizes are known statically now, then back end annotation
4200 -- is not required to do a proper check but if either size is not
4201 -- known statically, then we need the annotation.
4203 if Known_Static_RM_Size (Source)
4204 and then Known_Static_RM_Size (Target)
4208 Back_Annotate_Rep_Info := True;
4212 -- If unchecked conversion to access type, and access type is
4213 -- declared in the same unit as the unchecked conversion, then
4214 -- set the No_Strict_Aliasing flag (no strict aliasing is
4215 -- implicit in this situation).
4217 if Is_Access_Type (Target) and then
4218 In_Same_Source_Unit (Target, N)
4220 Set_No_Strict_Aliasing (Implementation_Base_Type (Target));
4223 -- Generate N_Validate_Unchecked_Conversion node for back end in
4224 -- case the back end needs to perform special validation checks.
4226 -- Shouldn't this be in exp_ch13, since the check only gets done
4227 -- if we have full expansion and the back end is called ???
4230 Make_Validate_Unchecked_Conversion (Sloc (N));
4231 Set_Source_Type (Vnode, Source);
4232 Set_Target_Type (Vnode, Target);
4234 -- If the unchecked conversion node is in a list, just insert before
4235 -- it. If not we have some strange case, not worth bothering about.
4237 if Is_List_Member (N) then
4238 Insert_After (N, Vnode);
4240 end Validate_Unchecked_Conversion;
4242 ------------------------------------
4243 -- Validate_Unchecked_Conversions --
4244 ------------------------------------
4246 procedure Validate_Unchecked_Conversions is
4248 for N in Unchecked_Conversions.First .. Unchecked_Conversions.Last loop
4250 T : UC_Entry renames Unchecked_Conversions.Table (N);
4252 Enode : constant Node_Id := T.Enode;
4253 Source : constant Entity_Id := T.Source;
4254 Target : constant Entity_Id := T.Target;
4260 -- This validation check, which warns if we have unequal sizes
4261 -- for unchecked conversion, and thus potentially implementation
4262 -- dependent semantics, is one of the few occasions on which we
4263 -- use the official RM size instead of Esize. See description
4264 -- in Einfo "Handling of Type'Size Values" for details.
4266 if Serious_Errors_Detected = 0
4267 and then Known_Static_RM_Size (Source)
4268 and then Known_Static_RM_Size (Target)
4270 Source_Siz := RM_Size (Source);
4271 Target_Siz := RM_Size (Target);
4273 if Source_Siz /= Target_Siz then
4275 ("?types for unchecked conversion have different sizes!",
4278 if All_Errors_Mode then
4279 Error_Msg_Name_1 := Chars (Source);
4280 Error_Msg_Uint_1 := Source_Siz;
4281 Error_Msg_Name_2 := Chars (Target);
4282 Error_Msg_Uint_2 := Target_Siz;
4284 ("\size of % is ^, size of % is ^?", Enode);
4286 Error_Msg_Uint_1 := UI_Abs (Source_Siz - Target_Siz);
4288 if Is_Discrete_Type (Source)
4289 and then Is_Discrete_Type (Target)
4291 if Source_Siz > Target_Siz then
4293 ("\?^ high order bits of source will be ignored!",
4296 elsif Is_Unsigned_Type (Source) then
4298 ("\?source will be extended with ^ high order " &
4299 "zero bits?!", Enode);
4303 ("\?source will be extended with ^ high order " &
4308 elsif Source_Siz < Target_Siz then
4309 if Is_Discrete_Type (Target) then
4310 if Bytes_Big_Endian then
4312 ("\?target value will include ^ undefined " &
4317 ("\?target value will include ^ undefined " &
4324 ("\?^ trailing bits of target value will be " &
4325 "undefined!", Enode);
4328 else pragma Assert (Source_Siz > Target_Siz);
4330 ("\?^ trailing bits of source will be ignored!",
4337 -- If both types are access types, we need to check the alignment.
4338 -- If the alignment of both is specified, we can do it here.
4340 if Serious_Errors_Detected = 0
4341 and then Ekind (Source) in Access_Kind
4342 and then Ekind (Target) in Access_Kind
4343 and then Target_Strict_Alignment
4344 and then Present (Designated_Type (Source))
4345 and then Present (Designated_Type (Target))
4348 D_Source : constant Entity_Id := Designated_Type (Source);
4349 D_Target : constant Entity_Id := Designated_Type (Target);
4352 if Known_Alignment (D_Source)
4353 and then Known_Alignment (D_Target)
4356 Source_Align : constant Uint := Alignment (D_Source);
4357 Target_Align : constant Uint := Alignment (D_Target);
4360 if Source_Align < Target_Align
4361 and then not Is_Tagged_Type (D_Source)
4363 Error_Msg_Uint_1 := Target_Align;
4364 Error_Msg_Uint_2 := Source_Align;
4365 Error_Msg_Node_2 := D_Source;
4367 ("?alignment of & (^) is stricter than " &
4368 "alignment of & (^)!", Enode, D_Target);
4370 if All_Errors_Mode then
4372 ("\?resulting access value may have invalid " &
4373 "alignment!", Enode);
4382 end Validate_Unchecked_Conversions;