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 2, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING. If not, write --
19 -- to the Free Software Foundation, 51 Franklin Street, Fifth Floor, --
20 -- Boston, MA 02110-1301, USA. --
22 -- GNAT was originally developed by the GNAT team at New York University. --
23 -- Extensive contributions were provided by Ada Core Technologies Inc. --
25 ------------------------------------------------------------------------------
27 with Atree; use Atree;
28 with Checks; use Checks;
29 with Einfo; use Einfo;
30 with Errout; use Errout;
31 with Exp_Tss; use Exp_Tss;
32 with Exp_Util; use Exp_Util;
34 with 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_A; use GNAT.Heap_Sort_A;
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 Mark_Aliased_Address_As_Volatile (N : Node_Id);
93 -- This is used for processing of an address representation clause. If
94 -- the expression N is of the form of K'Address, then the entity that
95 -- is associated with K is marked as volatile.
97 procedure New_Stream_Subprogram
101 Nam : TSS_Name_Type);
102 -- Create a subprogram renaming of a given stream attribute to the
103 -- designated subprogram and then in the tagged case, provide this as a
104 -- primitive operation, or in the non-tagged case make an appropriate TSS
105 -- entry. This is more properly an expansion activity than just semantics,
106 -- but the presence of user-defined stream functions for limited types is a
107 -- legality check, which is why this takes place here rather than in
108 -- exp_ch13, where it was previously. Nam indicates the name of the TSS
109 -- function to be generated.
111 -- To avoid elaboration anomalies with freeze nodes, for untagged types
112 -- we generate both a subprogram declaration and a subprogram renaming
113 -- declaration, so that the attribute specification is handled as a
114 -- renaming_as_body. For tagged types, the specification is one of the
117 ----------------------------------------------
118 -- Table for Validate_Unchecked_Conversions --
119 ----------------------------------------------
121 -- The following table collects unchecked conversions for validation.
122 -- Entries are made by Validate_Unchecked_Conversion and then the
123 -- call to Validate_Unchecked_Conversions does the actual error
124 -- checking and posting of warnings. The reason for this delayed
125 -- processing is to take advantage of back-annotations of size and
126 -- alignment values peformed by the back end.
128 type UC_Entry is record
129 Enode : Node_Id; -- node used for posting warnings
130 Source : Entity_Id; -- source type for unchecked conversion
131 Target : Entity_Id; -- target type for unchecked conversion
134 package Unchecked_Conversions is new Table.Table (
135 Table_Component_Type => UC_Entry,
136 Table_Index_Type => Int,
137 Table_Low_Bound => 1,
139 Table_Increment => 200,
140 Table_Name => "Unchecked_Conversions");
142 ----------------------------
143 -- Address_Aliased_Entity --
144 ----------------------------
146 function Address_Aliased_Entity (N : Node_Id) return Entity_Id is
148 if Nkind (N) = N_Attribute_Reference
149 and then Attribute_Name (N) = Name_Address
152 Nam : Node_Id := Prefix (N);
155 or else Nkind (Nam) = N_Selected_Component
156 or else Nkind (Nam) = N_Indexed_Component
161 if Is_Entity_Name (Nam) then
168 end Address_Aliased_Entity;
170 -----------------------------------------
171 -- Adjust_Record_For_Reverse_Bit_Order --
172 -----------------------------------------
174 procedure Adjust_Record_For_Reverse_Bit_Order (R : Entity_Id) is
175 Max_Machine_Scalar_Size : constant Uint :=
177 (Standard_Long_Long_Integer_Size);
178 -- We use this as the maximum machine scalar size in the sense of AI-133
182 SSU : constant Uint := UI_From_Int (System_Storage_Unit);
185 -- This first loop through components does two things. First it deals
186 -- with the case of components with component clauses whose length is
187 -- greater than the maximum machine scalar size (either accepting them
188 -- or rejecting as needed). Second, it counts the number of components
189 -- with component clauses whose length does not exceed this maximum for
193 Comp := First_Component_Or_Discriminant (R);
194 while Present (Comp) loop
196 CC : constant Node_Id := Component_Clause (Comp);
197 Fbit : constant Uint := Static_Integer (First_Bit (CC));
202 -- Case of component with size > max machine scalar
204 if Esize (Comp) > Max_Machine_Scalar_Size then
206 -- Must begin on byte boundary
208 if Fbit mod SSU /= 0 then
210 ("illegal first bit value for reverse bit order",
212 Error_Msg_Uint_1 := SSU;
213 Error_Msg_Uint_2 := Max_Machine_Scalar_Size;
216 ("\must be a multiple of ^ if size greater than ^",
219 -- Must end on byte boundary
221 elsif Esize (Comp) mod SSU /= 0 then
223 ("illegal last bit value for reverse bit order",
225 Error_Msg_Uint_1 := SSU;
226 Error_Msg_Uint_2 := Max_Machine_Scalar_Size;
229 ("\must be a multiple of ^ if size greater than ^",
232 -- OK, give warning if enabled
234 elsif Warn_On_Reverse_Bit_Order then
236 ("multi-byte field specified with non-standard"
237 & " Bit_Order?", CC);
239 if Bytes_Big_Endian then
241 ("\bytes are not reversed "
242 & "(component is big-endian)?", CC);
245 ("\bytes are not reversed "
246 & "(component is little-endian)?", CC);
250 -- Case where size is not greater than max machine scalar.
251 -- For now, we just count these.
254 Num_CC := Num_CC + 1;
259 Next_Component_Or_Discriminant (Comp);
262 -- We need to sort the component clauses on the basis of the Position
263 -- values in the clause, so we can group clauses with the same Position
264 -- together to determine the relevant machine scalar size.
267 Comps : array (0 .. Num_CC) of Entity_Id;
268 -- Array to collect component and discrimninant entities. The data
269 -- starts at index 1, the 0'th entry is for GNAT.Heap_Sort_A.
271 function CP_Lt (Op1, Op2 : Natural) return Boolean;
272 -- Compare routine for Sort (See GNAT.Heap_Sort_A)
274 procedure CP_Move (From : Natural; To : Natural);
275 -- Move routine for Sort (see GNAT.Heap_Sort_A)
279 -- Start and stop positions in component list of set of components
280 -- with the same starting position (that constitute components in
281 -- a single machine scalar).
284 -- Maximum last bit value of any component in this set
287 -- Corresponding machine scalar size
293 function CP_Lt (Op1, Op2 : Natural) return Boolean is
295 return Position (Component_Clause (Comps (Op1))) <
296 Position (Component_Clause (Comps (Op2)));
303 procedure CP_Move (From : Natural; To : Natural) is
305 Comps (To) := Comps (From);
309 -- Collect the component clauses
312 Comp := First_Component_Or_Discriminant (R);
313 while Present (Comp) loop
314 if Present (Component_Clause (Comp))
315 and then Esize (Comp) <= Max_Machine_Scalar_Size
317 Num_CC := Num_CC + 1;
318 Comps (Num_CC) := Comp;
321 Next_Component_Or_Discriminant (Comp);
324 -- Sort by ascending position number
326 Sort (Num_CC, CP_Move'Unrestricted_Access, CP_Lt'Unrestricted_Access);
328 -- We now have all the components whose size does not exceed the max
329 -- machine scalar value, sorted by starting position. In this loop
330 -- we gather groups of clauses starting at the same position, to
331 -- process them in accordance with Ada 2005 AI-133.
334 while Stop < Num_CC loop
338 Static_Integer (Last_Bit (Component_Clause (Comps (Start))));
339 while Stop < Num_CC loop
341 (Position (Component_Clause (Comps (Stop + 1)))) =
343 (Position (Component_Clause (Comps (Stop))))
350 (Last_Bit (Component_Clause (Comps (Stop)))));
356 -- Now we have a group of component clauses from Start to Stop
357 -- whose positions are identical, and MaxL is the maximum last bit
358 -- value of any of these components.
360 -- We need to determine the corresponding machine scalar size.
361 -- This loop assumes that machine scalar sizes are even, and that
362 -- each possible machine scalar has twice as many bits as the
365 MSS := Max_Machine_Scalar_Size;
367 and then (MSS / 2) >= SSU
368 and then (MSS / 2) > MaxL
373 -- Here is where we fix up the Component_Bit_Offset value to
374 -- account for the reverse bit order. Some examples of what needs
375 -- to be done for the case of a machine scalar size of 8 are:
377 -- First_Bit .. Last_Bit Component_Bit_Offset
389 -- The general rule is that the first bit is is obtained by
390 -- subtracting the old ending bit from machine scalar size - 1.
392 for C in Start .. Stop loop
394 Comp : constant Entity_Id := Comps (C);
395 CC : constant Node_Id := Component_Clause (Comp);
396 LB : constant Uint := Static_Integer (Last_Bit (CC));
397 NFB : constant Uint := MSS - Uint_1 - LB;
398 NLB : constant Uint := NFB + Esize (Comp) - 1;
399 Pos : constant Uint := Static_Integer (Position (CC));
402 if Warn_On_Reverse_Bit_Order then
403 Error_Msg_Uint_1 := MSS;
405 ("?reverse bit order in machine " &
406 "scalar of length^", First_Bit (CC));
407 Error_Msg_Uint_1 := NFB;
408 Error_Msg_Uint_2 := NLB;
410 if Bytes_Big_Endian then
412 ("?\big-endian range for component & is ^ .. ^",
413 First_Bit (CC), Comp);
416 ("?\little-endian range for component & is ^ .. ^",
417 First_Bit (CC), Comp);
421 Set_Component_Bit_Offset (Comp, Pos * SSU + NFB);
422 Set_Normalized_First_Bit (Comp, NFB mod SSU);
427 end Adjust_Record_For_Reverse_Bit_Order;
429 --------------------------------------
430 -- Alignment_Check_For_Esize_Change --
431 --------------------------------------
433 procedure Alignment_Check_For_Esize_Change (Typ : Entity_Id) is
435 -- If the alignment is known, and not set by a rep clause, and is
436 -- inconsistent with the size being set, then reset it to unknown,
437 -- we assume in this case that the size overrides the inherited
438 -- alignment, and that the alignment must be recomputed.
440 if Known_Alignment (Typ)
441 and then not Has_Alignment_Clause (Typ)
442 and then Esize (Typ) mod (Alignment (Typ) * SSU) /= 0
444 Init_Alignment (Typ);
446 end Alignment_Check_For_Esize_Change;
448 -----------------------
449 -- Analyze_At_Clause --
450 -----------------------
452 -- An at clause is replaced by the corresponding Address attribute
453 -- definition clause that is the preferred approach in Ada 95.
455 procedure Analyze_At_Clause (N : Node_Id) is
457 Check_Restriction (No_Obsolescent_Features, N);
459 if Warn_On_Obsolescent_Feature then
461 ("at clause is an obsolescent feature ('R'M 'J.7(2))?", N);
463 ("\use address attribute definition clause instead?", N);
467 Make_Attribute_Definition_Clause (Sloc (N),
468 Name => Identifier (N),
469 Chars => Name_Address,
470 Expression => Expression (N)));
471 Analyze_Attribute_Definition_Clause (N);
472 end Analyze_At_Clause;
474 -----------------------------------------
475 -- Analyze_Attribute_Definition_Clause --
476 -----------------------------------------
478 procedure Analyze_Attribute_Definition_Clause (N : Node_Id) is
479 Loc : constant Source_Ptr := Sloc (N);
480 Nam : constant Node_Id := Name (N);
481 Attr : constant Name_Id := Chars (N);
482 Expr : constant Node_Id := Expression (N);
483 Id : constant Attribute_Id := Get_Attribute_Id (Attr);
487 FOnly : Boolean := False;
488 -- Reset to True for subtype specific attribute (Alignment, Size)
489 -- and for stream attributes, i.e. those cases where in the call
490 -- to Rep_Item_Too_Late, FOnly is set True so that only the freezing
491 -- rules are checked. Note that the case of stream attributes is not
492 -- clear from the RM, but see AI95-00137. Also, the RM seems to
493 -- disallow Storage_Size for derived task types, but that is also
494 -- clearly unintentional.
496 procedure Analyze_Stream_TSS_Definition (TSS_Nam : TSS_Name_Type);
497 -- Common processing for 'Read, 'Write, 'Input and 'Output attribute
498 -- definition clauses.
500 procedure Analyze_Stream_TSS_Definition (TSS_Nam : TSS_Name_Type) is
501 Subp : Entity_Id := Empty;
506 Is_Read : constant Boolean := (TSS_Nam = TSS_Stream_Read);
508 function Has_Good_Profile (Subp : Entity_Id) return Boolean;
509 -- Return true if the entity is a subprogram with an appropriate
510 -- profile for the attribute being defined.
512 ----------------------
513 -- Has_Good_Profile --
514 ----------------------
516 function Has_Good_Profile (Subp : Entity_Id) return Boolean is
518 Is_Function : constant Boolean := (TSS_Nam = TSS_Stream_Input);
519 Expected_Ekind : constant array (Boolean) of Entity_Kind :=
520 (False => E_Procedure, True => E_Function);
524 if Ekind (Subp) /= Expected_Ekind (Is_Function) then
528 F := First_Formal (Subp);
531 or else Ekind (Etype (F)) /= E_Anonymous_Access_Type
532 or else Designated_Type (Etype (F)) /=
533 Class_Wide_Type (RTE (RE_Root_Stream_Type))
538 if not Is_Function then
542 Expected_Mode : constant array (Boolean) of Entity_Kind :=
543 (False => E_In_Parameter,
544 True => E_Out_Parameter);
546 if Parameter_Mode (F) /= Expected_Mode (Is_Read) then
557 return Base_Type (Typ) = Base_Type (Ent)
558 and then No (Next_Formal (F));
560 end Has_Good_Profile;
562 -- Start of processing for Analyze_Stream_TSS_Definition
567 if not Is_Type (U_Ent) then
568 Error_Msg_N ("local name must be a subtype", Nam);
572 Pnam := TSS (Base_Type (U_Ent), TSS_Nam);
574 -- If Pnam is present, it can be either inherited from an ancestor
575 -- type (in which case it is legal to redefine it for this type), or
576 -- be a previous definition of the attribute for the same type (in
577 -- which case it is illegal).
579 -- In the first case, it will have been analyzed already, and we
580 -- can check that its profile does not match the expected profile
581 -- for a stream attribute of U_Ent. In the second case, either Pnam
582 -- has been analyzed (and has the expected profile), or it has not
583 -- been analyzed yet (case of a type that has not been frozen yet
584 -- and for which the stream attribute has been set using Set_TSS).
587 and then (No (First_Entity (Pnam)) or else Has_Good_Profile (Pnam))
589 Error_Msg_Sloc := Sloc (Pnam);
590 Error_Msg_Name_1 := Attr;
591 Error_Msg_N ("% attribute already defined #", Nam);
597 if Is_Entity_Name (Expr) then
598 if not Is_Overloaded (Expr) then
599 if Has_Good_Profile (Entity (Expr)) then
600 Subp := Entity (Expr);
604 Get_First_Interp (Expr, I, It);
606 while Present (It.Nam) loop
607 if Has_Good_Profile (It.Nam) then
612 Get_Next_Interp (I, It);
617 if Present (Subp) then
618 if Is_Abstract_Subprogram (Subp) then
619 Error_Msg_N ("stream subprogram must not be abstract", Expr);
623 Set_Entity (Expr, Subp);
624 Set_Etype (Expr, Etype (Subp));
626 New_Stream_Subprogram (N, U_Ent, Subp, TSS_Nam);
629 Error_Msg_Name_1 := Attr;
630 Error_Msg_N ("incorrect expression for% attribute", Expr);
632 end Analyze_Stream_TSS_Definition;
634 -- Start of processing for Analyze_Attribute_Definition_Clause
640 if Rep_Item_Too_Early (Ent, N) then
644 -- Rep clause applies to full view of incomplete type or private type if
645 -- we have one (if not, this is a premature use of the type). However,
646 -- certain semantic checks need to be done on the specified entity (i.e.
647 -- the private view), so we save it in Ent.
649 if Is_Private_Type (Ent)
650 and then Is_Derived_Type (Ent)
651 and then not Is_Tagged_Type (Ent)
652 and then No (Full_View (Ent))
654 -- If this is a private type whose completion is a derivation from
655 -- another private type, there is no full view, and the attribute
656 -- belongs to the type itself, not its underlying parent.
660 elsif Ekind (Ent) = E_Incomplete_Type then
662 -- The attribute applies to the full view, set the entity of the
663 -- attribute definition accordingly.
665 Ent := Underlying_Type (Ent);
667 Set_Entity (Nam, Ent);
670 U_Ent := Underlying_Type (Ent);
673 -- Complete other routine error checks
675 if Etype (Nam) = Any_Type then
678 elsif Scope (Ent) /= Current_Scope then
679 Error_Msg_N ("entity must be declared in this scope", Nam);
682 elsif No (U_Ent) then
685 elsif Is_Type (U_Ent)
686 and then not Is_First_Subtype (U_Ent)
687 and then Id /= Attribute_Object_Size
688 and then Id /= Attribute_Value_Size
689 and then not From_At_Mod (N)
691 Error_Msg_N ("cannot specify attribute for subtype", Nam);
695 -- Switch on particular attribute
703 -- Address attribute definition clause
705 when Attribute_Address => Address : begin
706 Analyze_And_Resolve (Expr, RTE (RE_Address));
708 if Present (Address_Clause (U_Ent)) then
709 Error_Msg_N ("address already given for &", Nam);
711 -- Case of address clause for subprogram
713 elsif Is_Subprogram (U_Ent) then
714 if Has_Homonym (U_Ent) then
716 ("address clause cannot be given " &
717 "for overloaded subprogram",
721 -- For subprograms, all address clauses are permitted,
722 -- and we mark the subprogram as having a deferred freeze
723 -- so that Gigi will not elaborate it too soon.
725 -- Above needs more comments, what is too soon about???
727 Set_Has_Delayed_Freeze (U_Ent);
729 -- Case of address clause for entry
731 elsif Ekind (U_Ent) = E_Entry then
732 if Nkind (Parent (N)) = N_Task_Body then
734 ("entry address must be specified in task spec", Nam);
737 -- For entries, we require a constant address
739 Check_Constant_Address_Clause (Expr, U_Ent);
741 if Is_Task_Type (Scope (U_Ent))
742 and then Comes_From_Source (Scope (U_Ent))
745 ("?entry address declared for entry in task type", N);
747 ("\?only one task can be declared of this type", N);
750 Check_Restriction (No_Obsolescent_Features, N);
752 if Warn_On_Obsolescent_Feature then
754 ("attaching interrupt to task entry is an " &
755 "obsolescent feature ('R'M 'J.7.1)?", N);
757 ("\use interrupt procedure instead?", N);
760 -- Case of an address clause for a controlled object:
761 -- erroneous execution.
763 elsif Is_Controlled (Etype (U_Ent)) then
765 ("?controlled object& must not be overlaid", Nam, U_Ent);
767 ("\?Program_Error will be raised at run time", Nam);
768 Insert_Action (Declaration_Node (U_Ent),
769 Make_Raise_Program_Error (Loc,
770 Reason => PE_Overlaid_Controlled_Object));
772 -- Case of address clause for a (non-controlled) object
775 Ekind (U_Ent) = E_Variable
777 Ekind (U_Ent) = E_Constant
780 Expr : constant Node_Id := Expression (N);
781 Aent : constant Entity_Id := Address_Aliased_Entity (Expr);
784 -- Exported variables cannot have an address clause,
785 -- because this cancels the effect of the pragma Export
787 if Is_Exported (U_Ent) then
789 ("cannot export object with address clause", Nam);
791 -- Overlaying controlled objects is erroneous
794 and then Is_Controlled (Etype (Aent))
797 ("?controlled object must not be overlaid", Expr);
799 ("\?Program_Error will be raised at run time", Expr);
800 Insert_Action (Declaration_Node (U_Ent),
801 Make_Raise_Program_Error (Loc,
802 Reason => PE_Overlaid_Controlled_Object));
805 and then Ekind (U_Ent) = E_Constant
806 and then Ekind (Aent) /= E_Constant
808 Error_Msg_N ("constant overlays a variable?", Expr);
810 elsif Present (Renamed_Object (U_Ent)) then
812 ("address clause not allowed"
813 & " for a renaming declaration ('R'M 13.1(6))", Nam);
815 -- Imported variables can have an address clause, but then
816 -- the import is pretty meaningless except to suppress
817 -- initializations, so we do not need such variables to
818 -- be statically allocated (and in fact it causes trouble
819 -- if the address clause is a local value).
821 elsif Is_Imported (U_Ent) then
822 Set_Is_Statically_Allocated (U_Ent, False);
825 -- We mark a possible modification of a variable with an
826 -- address clause, since it is likely aliasing is occurring.
828 Note_Possible_Modification (Nam);
830 -- Here we are checking for explicit overlap of one
831 -- variable by another, and if we find this, then we
832 -- mark the overlapped variable as also being aliased.
834 -- First case is where we have an explicit
836 -- for J'Address use K'Address;
838 -- In this case, we mark K as volatile
840 Mark_Aliased_Address_As_Volatile (Expr);
842 -- Second case is where we have a constant whose
843 -- definition is of the form of an address as in:
845 -- A : constant Address := K'Address;
847 -- for B'Address use A;
849 -- In this case we also mark K as volatile
851 if Is_Entity_Name (Expr) then
853 Ent : constant Entity_Id := Entity (Expr);
854 Decl : constant Node_Id := Declaration_Node (Ent);
857 if Ekind (Ent) = E_Constant
858 and then Nkind (Decl) = N_Object_Declaration
859 and then Present (Expression (Decl))
861 Mark_Aliased_Address_As_Volatile
867 -- Legality checks on the address clause for initialized
868 -- objects is deferred until the freeze point, because
869 -- a subsequent pragma might indicate that the object is
870 -- imported and thus not initialized.
872 Set_Has_Delayed_Freeze (U_Ent);
874 if Is_Exported (U_Ent) then
876 ("& cannot be exported if an address clause is given",
879 ("\define and export a variable " &
880 "that holds its address instead",
884 -- Entity has delayed freeze, so we will generate an
885 -- alignment check at the freeze point unless suppressed.
887 if not Range_Checks_Suppressed (U_Ent)
888 and then not Alignment_Checks_Suppressed (U_Ent)
890 Set_Check_Address_Alignment (N);
893 -- Kill the size check code, since we are not allocating
894 -- the variable, it is somewhere else.
896 Kill_Size_Check_Code (U_Ent);
899 -- Not a valid entity for an address clause
902 Error_Msg_N ("address cannot be given for &", Nam);
910 -- Alignment attribute definition clause
912 when Attribute_Alignment => Alignment_Block : declare
913 Align : constant Uint := Get_Alignment_Value (Expr);
918 if not Is_Type (U_Ent)
919 and then Ekind (U_Ent) /= E_Variable
920 and then Ekind (U_Ent) /= E_Constant
922 Error_Msg_N ("alignment cannot be given for &", Nam);
924 elsif Has_Alignment_Clause (U_Ent) then
925 Error_Msg_Sloc := Sloc (Alignment_Clause (U_Ent));
926 Error_Msg_N ("alignment clause previously given#", N);
928 elsif Align /= No_Uint then
929 Set_Has_Alignment_Clause (U_Ent);
930 Set_Alignment (U_Ent, Align);
938 -- Bit_Order attribute definition clause
940 when Attribute_Bit_Order => Bit_Order : declare
942 if not Is_Record_Type (U_Ent) then
944 ("Bit_Order can only be defined for record type", Nam);
947 Analyze_And_Resolve (Expr, RTE (RE_Bit_Order));
949 if Etype (Expr) = Any_Type then
952 elsif not Is_Static_Expression (Expr) then
954 ("Bit_Order requires static expression!", Expr);
957 if (Expr_Value (Expr) = 0) /= Bytes_Big_Endian then
958 Set_Reverse_Bit_Order (U_Ent, True);
968 -- Component_Size attribute definition clause
970 when Attribute_Component_Size => Component_Size_Case : declare
971 Csize : constant Uint := Static_Integer (Expr);
974 New_Ctyp : Entity_Id;
978 if not Is_Array_Type (U_Ent) then
979 Error_Msg_N ("component size requires array type", Nam);
983 Btype := Base_Type (U_Ent);
985 if Has_Component_Size_Clause (Btype) then
987 ("component size clase for& previously given", Nam);
989 elsif Csize /= No_Uint then
990 Check_Size (Expr, Component_Type (Btype), Csize, Biased);
992 if Has_Aliased_Components (Btype)
998 ("component size incorrect for aliased components", N);
1002 -- For the biased case, build a declaration for a subtype
1003 -- that will be used to represent the biased subtype that
1004 -- reflects the biased representation of components. We need
1005 -- this subtype to get proper conversions on referencing
1006 -- elements of the array.
1010 Make_Defining_Identifier (Loc,
1011 Chars => New_External_Name (Chars (U_Ent), 'C', 0, 'T'));
1014 Make_Subtype_Declaration (Loc,
1015 Defining_Identifier => New_Ctyp,
1016 Subtype_Indication =>
1017 New_Occurrence_Of (Component_Type (Btype), Loc));
1019 Set_Parent (Decl, N);
1020 Analyze (Decl, Suppress => All_Checks);
1022 Set_Has_Delayed_Freeze (New_Ctyp, False);
1023 Set_Esize (New_Ctyp, Csize);
1024 Set_RM_Size (New_Ctyp, Csize);
1025 Init_Alignment (New_Ctyp);
1026 Set_Has_Biased_Representation (New_Ctyp, True);
1027 Set_Is_Itype (New_Ctyp, True);
1028 Set_Associated_Node_For_Itype (New_Ctyp, U_Ent);
1030 Set_Component_Type (Btype, New_Ctyp);
1033 Set_Component_Size (Btype, Csize);
1034 Set_Has_Component_Size_Clause (Btype, True);
1035 Set_Has_Non_Standard_Rep (Btype, True);
1037 end Component_Size_Case;
1043 when Attribute_External_Tag => External_Tag :
1045 if not Is_Tagged_Type (U_Ent) then
1046 Error_Msg_N ("should be a tagged type", Nam);
1049 Analyze_And_Resolve (Expr, Standard_String);
1051 if not Is_Static_Expression (Expr) then
1052 Flag_Non_Static_Expr
1053 ("static string required for tag name!", Nam);
1056 if VM_Target = No_VM then
1057 Set_Has_External_Tag_Rep_Clause (U_Ent);
1059 Error_Msg_Name_1 := Attr;
1061 ("% attribute unsupported in this configuration", Nam);
1069 when Attribute_Input =>
1070 Analyze_Stream_TSS_Definition (TSS_Stream_Input);
1071 Set_Has_Specified_Stream_Input (Ent);
1077 -- Machine radix attribute definition clause
1079 when Attribute_Machine_Radix => Machine_Radix : declare
1080 Radix : constant Uint := Static_Integer (Expr);
1083 if not Is_Decimal_Fixed_Point_Type (U_Ent) then
1084 Error_Msg_N ("decimal fixed-point type expected for &", Nam);
1086 elsif Has_Machine_Radix_Clause (U_Ent) then
1087 Error_Msg_Sloc := Sloc (Alignment_Clause (U_Ent));
1088 Error_Msg_N ("machine radix clause previously given#", N);
1090 elsif Radix /= No_Uint then
1091 Set_Has_Machine_Radix_Clause (U_Ent);
1092 Set_Has_Non_Standard_Rep (Base_Type (U_Ent));
1096 elsif Radix = 10 then
1097 Set_Machine_Radix_10 (U_Ent);
1099 Error_Msg_N ("machine radix value must be 2 or 10", Expr);
1108 -- Object_Size attribute definition clause
1110 when Attribute_Object_Size => Object_Size : declare
1111 Size : constant Uint := Static_Integer (Expr);
1115 if not Is_Type (U_Ent) then
1116 Error_Msg_N ("Object_Size cannot be given for &", Nam);
1118 elsif Has_Object_Size_Clause (U_Ent) then
1119 Error_Msg_N ("Object_Size already given for &", Nam);
1122 Check_Size (Expr, U_Ent, Size, Biased);
1130 UI_Mod (Size, 64) /= 0
1133 ("Object_Size must be 8, 16, 32, or multiple of 64",
1137 Set_Esize (U_Ent, Size);
1138 Set_Has_Object_Size_Clause (U_Ent);
1139 Alignment_Check_For_Esize_Change (U_Ent);
1147 when Attribute_Output =>
1148 Analyze_Stream_TSS_Definition (TSS_Stream_Output);
1149 Set_Has_Specified_Stream_Output (Ent);
1155 when Attribute_Read =>
1156 Analyze_Stream_TSS_Definition (TSS_Stream_Read);
1157 Set_Has_Specified_Stream_Read (Ent);
1163 -- Size attribute definition clause
1165 when Attribute_Size => Size : declare
1166 Size : constant Uint := Static_Integer (Expr);
1173 if Has_Size_Clause (U_Ent) then
1174 Error_Msg_N ("size already given for &", Nam);
1176 elsif not Is_Type (U_Ent)
1177 and then Ekind (U_Ent) /= E_Variable
1178 and then Ekind (U_Ent) /= E_Constant
1180 Error_Msg_N ("size cannot be given for &", Nam);
1182 elsif Is_Array_Type (U_Ent)
1183 and then not Is_Constrained (U_Ent)
1186 ("size cannot be given for unconstrained array", Nam);
1188 elsif Size /= No_Uint then
1189 if Is_Type (U_Ent) then
1192 Etyp := Etype (U_Ent);
1195 -- Check size, note that Gigi is in charge of checking that the
1196 -- size of an array or record type is OK. Also we do not check
1197 -- the size in the ordinary fixed-point case, since it is too
1198 -- early to do so (there may be subsequent small clause that
1199 -- affects the size). We can check the size if a small clause
1200 -- has already been given.
1202 if not Is_Ordinary_Fixed_Point_Type (U_Ent)
1203 or else Has_Small_Clause (U_Ent)
1205 Check_Size (Expr, Etyp, Size, Biased);
1206 Set_Has_Biased_Representation (U_Ent, Biased);
1209 -- For types set RM_Size and Esize if possible
1211 if Is_Type (U_Ent) then
1212 Set_RM_Size (U_Ent, Size);
1214 -- For scalar types, increase Object_Size to power of 2, but
1215 -- not less than a storage unit in any case (i.e., normally
1216 -- this means it will be byte addressable).
1218 if Is_Scalar_Type (U_Ent) then
1219 if Size <= System_Storage_Unit then
1220 Init_Esize (U_Ent, System_Storage_Unit);
1221 elsif Size <= 16 then
1222 Init_Esize (U_Ent, 16);
1223 elsif Size <= 32 then
1224 Init_Esize (U_Ent, 32);
1226 Set_Esize (U_Ent, (Size + 63) / 64 * 64);
1229 -- For all other types, object size = value size. The
1230 -- backend will adjust as needed.
1233 Set_Esize (U_Ent, Size);
1236 Alignment_Check_For_Esize_Change (U_Ent);
1238 -- For objects, set Esize only
1241 if Is_Elementary_Type (Etyp) then
1242 if Size /= System_Storage_Unit
1244 Size /= System_Storage_Unit * 2
1246 Size /= System_Storage_Unit * 4
1248 Size /= System_Storage_Unit * 8
1250 Error_Msg_Uint_1 := UI_From_Int (System_Storage_Unit);
1251 Error_Msg_Uint_2 := Error_Msg_Uint_1 * 8;
1253 ("size for primitive object must be a power of 2"
1254 & " in the range ^-^", N);
1258 Set_Esize (U_Ent, Size);
1261 Set_Has_Size_Clause (U_Ent);
1269 -- Small attribute definition clause
1271 when Attribute_Small => Small : declare
1272 Implicit_Base : constant Entity_Id := Base_Type (U_Ent);
1276 Analyze_And_Resolve (Expr, Any_Real);
1278 if Etype (Expr) = Any_Type then
1281 elsif not Is_Static_Expression (Expr) then
1282 Flag_Non_Static_Expr
1283 ("small requires static expression!", Expr);
1287 Small := Expr_Value_R (Expr);
1289 if Small <= Ureal_0 then
1290 Error_Msg_N ("small value must be greater than zero", Expr);
1296 if not Is_Ordinary_Fixed_Point_Type (U_Ent) then
1298 ("small requires an ordinary fixed point type", Nam);
1300 elsif Has_Small_Clause (U_Ent) then
1301 Error_Msg_N ("small already given for &", Nam);
1303 elsif Small > Delta_Value (U_Ent) then
1305 ("small value must not be greater then delta value", Nam);
1308 Set_Small_Value (U_Ent, Small);
1309 Set_Small_Value (Implicit_Base, Small);
1310 Set_Has_Small_Clause (U_Ent);
1311 Set_Has_Small_Clause (Implicit_Base);
1312 Set_Has_Non_Standard_Rep (Implicit_Base);
1320 -- Storage_Pool attribute definition clause
1322 when Attribute_Storage_Pool => Storage_Pool : declare
1327 if Ekind (U_Ent) = E_Access_Subprogram_Type then
1329 ("storage pool cannot be given for access-to-subprogram type",
1333 elsif Ekind (U_Ent) /= E_Access_Type
1334 and then Ekind (U_Ent) /= E_General_Access_Type
1337 ("storage pool can only be given for access types", Nam);
1340 elsif Is_Derived_Type (U_Ent) then
1342 ("storage pool cannot be given for a derived access type",
1345 elsif Has_Storage_Size_Clause (U_Ent) then
1346 Error_Msg_N ("storage size already given for &", Nam);
1349 elsif Present (Associated_Storage_Pool (U_Ent)) then
1350 Error_Msg_N ("storage pool already given for &", Nam);
1355 (Expr, Class_Wide_Type (RTE (RE_Root_Storage_Pool)));
1357 if Nkind (Expr) = N_Type_Conversion then
1358 T := Etype (Expression (Expr));
1363 -- The Stack_Bounded_Pool is used internally for implementing
1364 -- access types with a Storage_Size. Since it only work
1365 -- properly when used on one specific type, we need to check
1366 -- that it is not highjacked improperly:
1367 -- type T is access Integer;
1368 -- for T'Storage_Size use n;
1369 -- type Q is access Float;
1370 -- for Q'Storage_Size use T'Storage_Size; -- incorrect
1372 if RTE_Available (RE_Stack_Bounded_Pool)
1373 and then Base_Type (T) = RTE (RE_Stack_Bounded_Pool)
1375 Error_Msg_N ("non-shareable internal Pool", Expr);
1379 -- If the argument is a name that is not an entity name, then
1380 -- we construct a renaming operation to define an entity of
1381 -- type storage pool.
1383 if not Is_Entity_Name (Expr)
1384 and then Is_Object_Reference (Expr)
1387 Make_Defining_Identifier (Loc,
1388 Chars => New_Internal_Name ('P'));
1391 Rnode : constant Node_Id :=
1392 Make_Object_Renaming_Declaration (Loc,
1393 Defining_Identifier => Pool,
1395 New_Occurrence_Of (Etype (Expr), Loc),
1399 Insert_Before (N, Rnode);
1401 Set_Associated_Storage_Pool (U_Ent, Pool);
1404 elsif Is_Entity_Name (Expr) then
1405 Pool := Entity (Expr);
1407 -- If pool is a renamed object, get original one. This can
1408 -- happen with an explicit renaming, and within instances.
1410 while Present (Renamed_Object (Pool))
1411 and then Is_Entity_Name (Renamed_Object (Pool))
1413 Pool := Entity (Renamed_Object (Pool));
1416 if Present (Renamed_Object (Pool))
1417 and then Nkind (Renamed_Object (Pool)) = N_Type_Conversion
1418 and then Is_Entity_Name (Expression (Renamed_Object (Pool)))
1420 Pool := Entity (Expression (Renamed_Object (Pool)));
1423 Set_Associated_Storage_Pool (U_Ent, Pool);
1425 elsif Nkind (Expr) = N_Type_Conversion
1426 and then Is_Entity_Name (Expression (Expr))
1427 and then Nkind (Original_Node (Expr)) = N_Attribute_Reference
1429 Pool := Entity (Expression (Expr));
1430 Set_Associated_Storage_Pool (U_Ent, Pool);
1433 Error_Msg_N ("incorrect reference to a Storage Pool", Expr);
1442 -- Storage_Size attribute definition clause
1444 when Attribute_Storage_Size => Storage_Size : declare
1445 Btype : constant Entity_Id := Base_Type (U_Ent);
1449 if Is_Task_Type (U_Ent) then
1450 Check_Restriction (No_Obsolescent_Features, N);
1452 if Warn_On_Obsolescent_Feature then
1454 ("storage size clause for task is an " &
1455 "obsolescent feature ('R'M 'J.9)?", N);
1457 ("\use Storage_Size pragma instead?", N);
1463 if not Is_Access_Type (U_Ent)
1464 and then Ekind (U_Ent) /= E_Task_Type
1466 Error_Msg_N ("storage size cannot be given for &", Nam);
1468 elsif Is_Access_Type (U_Ent) and Is_Derived_Type (U_Ent) then
1470 ("storage size cannot be given for a derived access type",
1473 elsif Has_Storage_Size_Clause (Btype) then
1474 Error_Msg_N ("storage size already given for &", Nam);
1477 Analyze_And_Resolve (Expr, Any_Integer);
1479 if Is_Access_Type (U_Ent) then
1480 if Present (Associated_Storage_Pool (U_Ent)) then
1481 Error_Msg_N ("storage pool already given for &", Nam);
1485 if Compile_Time_Known_Value (Expr)
1486 and then Expr_Value (Expr) = 0
1488 Set_No_Pool_Assigned (Btype);
1491 else -- Is_Task_Type (U_Ent)
1492 Sprag := Get_Rep_Pragma (Btype, Name_Storage_Size);
1494 if Present (Sprag) then
1495 Error_Msg_Sloc := Sloc (Sprag);
1497 ("Storage_Size already specified#", Nam);
1502 Set_Has_Storage_Size_Clause (Btype);
1510 when Attribute_Stream_Size => Stream_Size : declare
1511 Size : constant Uint := Static_Integer (Expr);
1514 if Ada_Version <= Ada_95 then
1515 Check_Restriction (No_Implementation_Attributes, N);
1518 if Has_Stream_Size_Clause (U_Ent) then
1519 Error_Msg_N ("Stream_Size already given for &", Nam);
1521 elsif Is_Elementary_Type (U_Ent) then
1522 if Size /= System_Storage_Unit
1524 Size /= System_Storage_Unit * 2
1526 Size /= System_Storage_Unit * 4
1528 Size /= System_Storage_Unit * 8
1530 Error_Msg_Uint_1 := UI_From_Int (System_Storage_Unit);
1532 ("stream size for elementary type must be a"
1533 & " power of 2 and at least ^", N);
1535 elsif RM_Size (U_Ent) > Size then
1536 Error_Msg_Uint_1 := RM_Size (U_Ent);
1538 ("stream size for elementary type must be a"
1539 & " power of 2 and at least ^", N);
1542 Set_Has_Stream_Size_Clause (U_Ent);
1545 Error_Msg_N ("Stream_Size cannot be given for &", Nam);
1553 -- Value_Size attribute definition clause
1555 when Attribute_Value_Size => Value_Size : declare
1556 Size : constant Uint := Static_Integer (Expr);
1560 if not Is_Type (U_Ent) then
1561 Error_Msg_N ("Value_Size cannot be given for &", Nam);
1564 (Get_Attribute_Definition_Clause
1565 (U_Ent, Attribute_Value_Size))
1567 Error_Msg_N ("Value_Size already given for &", Nam);
1569 elsif Is_Array_Type (U_Ent)
1570 and then not Is_Constrained (U_Ent)
1573 ("Value_Size cannot be given for unconstrained array", Nam);
1576 if Is_Elementary_Type (U_Ent) then
1577 Check_Size (Expr, U_Ent, Size, Biased);
1578 Set_Has_Biased_Representation (U_Ent, Biased);
1581 Set_RM_Size (U_Ent, Size);
1589 when Attribute_Write =>
1590 Analyze_Stream_TSS_Definition (TSS_Stream_Write);
1591 Set_Has_Specified_Stream_Write (Ent);
1593 -- All other attributes cannot be set
1597 ("attribute& cannot be set with definition clause", N);
1600 -- The test for the type being frozen must be performed after
1601 -- any expression the clause has been analyzed since the expression
1602 -- itself might cause freezing that makes the clause illegal.
1604 if Rep_Item_Too_Late (U_Ent, N, FOnly) then
1607 end Analyze_Attribute_Definition_Clause;
1609 ----------------------------
1610 -- Analyze_Code_Statement --
1611 ----------------------------
1613 procedure Analyze_Code_Statement (N : Node_Id) is
1614 HSS : constant Node_Id := Parent (N);
1615 SBody : constant Node_Id := Parent (HSS);
1616 Subp : constant Entity_Id := Current_Scope;
1623 -- Analyze and check we get right type, note that this implements the
1624 -- requirement (RM 13.8(1)) that Machine_Code be with'ed, since that
1625 -- is the only way that Asm_Insn could possibly be visible.
1627 Analyze_And_Resolve (Expression (N));
1629 if Etype (Expression (N)) = Any_Type then
1631 elsif Etype (Expression (N)) /= RTE (RE_Asm_Insn) then
1632 Error_Msg_N ("incorrect type for code statement", N);
1636 Check_Code_Statement (N);
1638 -- Make sure we appear in the handled statement sequence of a
1639 -- subprogram (RM 13.8(3)).
1641 if Nkind (HSS) /= N_Handled_Sequence_Of_Statements
1642 or else Nkind (SBody) /= N_Subprogram_Body
1645 ("code statement can only appear in body of subprogram", N);
1649 -- Do remaining checks (RM 13.8(3)) if not already done
1651 if not Is_Machine_Code_Subprogram (Subp) then
1652 Set_Is_Machine_Code_Subprogram (Subp);
1654 -- No exception handlers allowed
1656 if Present (Exception_Handlers (HSS)) then
1658 ("exception handlers not permitted in machine code subprogram",
1659 First (Exception_Handlers (HSS)));
1662 -- No declarations other than use clauses and pragmas (we allow
1663 -- certain internally generated declarations as well).
1665 Decl := First (Declarations (SBody));
1666 while Present (Decl) loop
1667 DeclO := Original_Node (Decl);
1668 if Comes_From_Source (DeclO)
1669 and then Nkind (DeclO) /= N_Pragma
1670 and then Nkind (DeclO) /= N_Use_Package_Clause
1671 and then Nkind (DeclO) /= N_Use_Type_Clause
1672 and then Nkind (DeclO) /= N_Implicit_Label_Declaration
1675 ("this declaration not allowed in machine code subprogram",
1682 -- No statements other than code statements, pragmas, and labels.
1683 -- Again we allow certain internally generated statements.
1685 Stmt := First (Statements (HSS));
1686 while Present (Stmt) loop
1687 StmtO := Original_Node (Stmt);
1688 if Comes_From_Source (StmtO)
1689 and then Nkind (StmtO) /= N_Pragma
1690 and then Nkind (StmtO) /= N_Label
1691 and then Nkind (StmtO) /= N_Code_Statement
1694 ("this statement is not allowed in machine code subprogram",
1701 end Analyze_Code_Statement;
1703 -----------------------------------------------
1704 -- Analyze_Enumeration_Representation_Clause --
1705 -----------------------------------------------
1707 procedure Analyze_Enumeration_Representation_Clause (N : Node_Id) is
1708 Ident : constant Node_Id := Identifier (N);
1709 Aggr : constant Node_Id := Array_Aggregate (N);
1710 Enumtype : Entity_Id;
1716 Err : Boolean := False;
1718 Lo : constant Uint := Expr_Value (Type_Low_Bound (Universal_Integer));
1719 Hi : constant Uint := Expr_Value (Type_High_Bound (Universal_Integer));
1724 -- First some basic error checks
1727 Enumtype := Entity (Ident);
1729 if Enumtype = Any_Type
1730 or else Rep_Item_Too_Early (Enumtype, N)
1734 Enumtype := Underlying_Type (Enumtype);
1737 if not Is_Enumeration_Type (Enumtype) then
1739 ("enumeration type required, found}",
1740 Ident, First_Subtype (Enumtype));
1744 -- Ignore rep clause on generic actual type. This will already have
1745 -- been flagged on the template as an error, and this is the safest
1746 -- way to ensure we don't get a junk cascaded message in the instance.
1748 if Is_Generic_Actual_Type (Enumtype) then
1751 -- Type must be in current scope
1753 elsif Scope (Enumtype) /= Current_Scope then
1754 Error_Msg_N ("type must be declared in this scope", Ident);
1757 -- Type must be a first subtype
1759 elsif not Is_First_Subtype (Enumtype) then
1760 Error_Msg_N ("cannot give enumeration rep clause for subtype", N);
1763 -- Ignore duplicate rep clause
1765 elsif Has_Enumeration_Rep_Clause (Enumtype) then
1766 Error_Msg_N ("duplicate enumeration rep clause ignored", N);
1769 -- Don't allow rep clause for standard [wide_[wide_]]character
1771 elsif Root_Type (Enumtype) = Standard_Character
1772 or else Root_Type (Enumtype) = Standard_Wide_Character
1773 or else Root_Type (Enumtype) = Standard_Wide_Wide_Character
1775 Error_Msg_N ("enumeration rep clause not allowed for this type", N);
1778 -- Check that the expression is a proper aggregate (no parentheses)
1780 elsif Paren_Count (Aggr) /= 0 then
1782 ("extra parentheses surrounding aggregate not allowed",
1786 -- All tests passed, so set rep clause in place
1789 Set_Has_Enumeration_Rep_Clause (Enumtype);
1790 Set_Has_Enumeration_Rep_Clause (Base_Type (Enumtype));
1793 -- Now we process the aggregate. Note that we don't use the normal
1794 -- aggregate code for this purpose, because we don't want any of the
1795 -- normal expansion activities, and a number of special semantic
1796 -- rules apply (including the component type being any integer type)
1798 Elit := First_Literal (Enumtype);
1800 -- First the positional entries if any
1802 if Present (Expressions (Aggr)) then
1803 Expr := First (Expressions (Aggr));
1804 while Present (Expr) loop
1806 Error_Msg_N ("too many entries in aggregate", Expr);
1810 Val := Static_Integer (Expr);
1812 -- Err signals that we found some incorrect entries processing
1813 -- the list. The final checks for completeness and ordering are
1814 -- skipped in this case.
1816 if Val = No_Uint then
1818 elsif Val < Lo or else Hi < Val then
1819 Error_Msg_N ("value outside permitted range", Expr);
1823 Set_Enumeration_Rep (Elit, Val);
1824 Set_Enumeration_Rep_Expr (Elit, Expr);
1830 -- Now process the named entries if present
1832 if Present (Component_Associations (Aggr)) then
1833 Assoc := First (Component_Associations (Aggr));
1834 while Present (Assoc) loop
1835 Choice := First (Choices (Assoc));
1837 if Present (Next (Choice)) then
1839 ("multiple choice not allowed here", Next (Choice));
1843 if Nkind (Choice) = N_Others_Choice then
1844 Error_Msg_N ("others choice not allowed here", Choice);
1847 elsif Nkind (Choice) = N_Range then
1848 -- ??? should allow zero/one element range here
1849 Error_Msg_N ("range not allowed here", Choice);
1853 Analyze_And_Resolve (Choice, Enumtype);
1855 if Is_Entity_Name (Choice)
1856 and then Is_Type (Entity (Choice))
1858 Error_Msg_N ("subtype name not allowed here", Choice);
1860 -- ??? should allow static subtype with zero/one entry
1862 elsif Etype (Choice) = Base_Type (Enumtype) then
1863 if not Is_Static_Expression (Choice) then
1864 Flag_Non_Static_Expr
1865 ("non-static expression used for choice!", Choice);
1869 Elit := Expr_Value_E (Choice);
1871 if Present (Enumeration_Rep_Expr (Elit)) then
1872 Error_Msg_Sloc := Sloc (Enumeration_Rep_Expr (Elit));
1874 ("representation for& previously given#",
1879 Set_Enumeration_Rep_Expr (Elit, Choice);
1881 Expr := Expression (Assoc);
1882 Val := Static_Integer (Expr);
1884 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);
1901 -- Aggregate is fully processed. Now we check that a full set of
1902 -- representations was given, and that they are in range and in order.
1903 -- These checks are only done if no other errors occurred.
1909 Elit := First_Literal (Enumtype);
1910 while Present (Elit) loop
1911 if No (Enumeration_Rep_Expr (Elit)) then
1912 Error_Msg_NE ("missing representation for&!", N, Elit);
1915 Val := Enumeration_Rep (Elit);
1917 if Min = No_Uint then
1921 if Val /= No_Uint then
1922 if Max /= No_Uint and then Val <= Max then
1924 ("enumeration value for& not ordered!",
1925 Enumeration_Rep_Expr (Elit), Elit);
1931 -- If there is at least one literal whose representation
1932 -- is not equal to the Pos value, then note that this
1933 -- enumeration type has a non-standard representation.
1935 if Val /= Enumeration_Pos (Elit) then
1936 Set_Has_Non_Standard_Rep (Base_Type (Enumtype));
1943 -- Now set proper size information
1946 Minsize : Uint := UI_From_Int (Minimum_Size (Enumtype));
1949 if Has_Size_Clause (Enumtype) then
1950 if Esize (Enumtype) >= Minsize then
1955 UI_From_Int (Minimum_Size (Enumtype, Biased => True));
1957 if Esize (Enumtype) < Minsize then
1958 Error_Msg_N ("previously given size is too small", N);
1961 Set_Has_Biased_Representation (Enumtype);
1966 Set_RM_Size (Enumtype, Minsize);
1967 Set_Enum_Esize (Enumtype);
1970 Set_RM_Size (Base_Type (Enumtype), RM_Size (Enumtype));
1971 Set_Esize (Base_Type (Enumtype), Esize (Enumtype));
1972 Set_Alignment (Base_Type (Enumtype), Alignment (Enumtype));
1976 -- We repeat the too late test in case it froze itself!
1978 if Rep_Item_Too_Late (Enumtype, N) then
1981 end Analyze_Enumeration_Representation_Clause;
1983 ----------------------------
1984 -- Analyze_Free_Statement --
1985 ----------------------------
1987 procedure Analyze_Free_Statement (N : Node_Id) is
1989 Analyze (Expression (N));
1990 end Analyze_Free_Statement;
1992 ------------------------------------------
1993 -- Analyze_Record_Representation_Clause --
1994 ------------------------------------------
1996 procedure Analyze_Record_Representation_Clause (N : Node_Id) is
1997 Loc : constant Source_Ptr := Sloc (N);
1998 Ident : constant Node_Id := Identifier (N);
1999 Rectype : Entity_Id;
2005 Hbit : Uint := Uint_0;
2010 Max_Bit_So_Far : Uint;
2011 -- Records the maximum bit position so far. If all field positions
2012 -- are monotonically increasing, then we can skip the circuit for
2013 -- checking for overlap, since no overlap is possible.
2015 Overlap_Check_Required : Boolean;
2016 -- Used to keep track of whether or not an overlap check is required
2018 Ccount : Natural := 0;
2019 -- Number of component clauses in record rep clause
2021 CR_Pragma : Node_Id := Empty;
2022 -- Points to N_Pragma node if Complete_Representation pragma present
2026 Rectype := Entity (Ident);
2028 if Rectype = Any_Type
2029 or else Rep_Item_Too_Early (Rectype, N)
2033 Rectype := Underlying_Type (Rectype);
2036 -- First some basic error checks
2038 if not Is_Record_Type (Rectype) then
2040 ("record type required, found}", Ident, First_Subtype (Rectype));
2043 elsif Is_Unchecked_Union (Rectype) then
2045 ("record rep clause not allowed for Unchecked_Union", N);
2047 elsif Scope (Rectype) /= Current_Scope then
2048 Error_Msg_N ("type must be declared in this scope", N);
2051 elsif not Is_First_Subtype (Rectype) then
2052 Error_Msg_N ("cannot give record rep clause for subtype", N);
2055 elsif Has_Record_Rep_Clause (Rectype) then
2056 Error_Msg_N ("duplicate record rep clause ignored", N);
2059 elsif Rep_Item_Too_Late (Rectype, N) then
2063 if Present (Mod_Clause (N)) then
2065 Loc : constant Source_Ptr := Sloc (N);
2066 M : constant Node_Id := Mod_Clause (N);
2067 P : constant List_Id := Pragmas_Before (M);
2071 pragma Warnings (Off, Mod_Val);
2074 Check_Restriction (No_Obsolescent_Features, Mod_Clause (N));
2076 if Warn_On_Obsolescent_Feature then
2078 ("mod clause is an obsolescent feature ('R'M 'J.8)?", N);
2080 ("\use alignment attribute definition clause instead?", N);
2087 -- In ASIS_Mode mode, expansion is disabled, but we must
2088 -- convert the Mod clause into an alignment clause anyway, so
2089 -- that the back-end can compute and back-annotate properly the
2090 -- size and alignment of types that may include this record.
2092 -- This seems dubious, this destroys the source tree in a manner
2093 -- not detectable by ASIS ???
2095 if Operating_Mode = Check_Semantics
2099 Make_Attribute_Definition_Clause (Loc,
2100 Name => New_Reference_To (Base_Type (Rectype), Loc),
2101 Chars => Name_Alignment,
2102 Expression => Relocate_Node (Expression (M)));
2104 Set_From_At_Mod (AtM_Nod);
2105 Insert_After (N, AtM_Nod);
2106 Mod_Val := Get_Alignment_Value (Expression (AtM_Nod));
2107 Set_Mod_Clause (N, Empty);
2110 -- Get the alignment value to perform error checking
2112 Mod_Val := Get_Alignment_Value (Expression (M));
2118 -- Clear any existing component clauses for the type (this happens
2119 -- with derived types, where we are now overriding the original)
2121 Comp := First_Component_Or_Discriminant (Rectype);
2122 while Present (Comp) loop
2123 Set_Component_Clause (Comp, Empty);
2124 Next_Component_Or_Discriminant (Comp);
2127 -- All done if no component clauses
2129 CC := First (Component_Clauses (N));
2135 -- If a tag is present, then create a component clause that places it
2136 -- at the start of the record (otherwise gigi may place it after other
2137 -- fields that have rep clauses).
2139 Fent := First_Entity (Rectype);
2141 if Nkind (Fent) = N_Defining_Identifier
2142 and then Chars (Fent) = Name_uTag
2144 Set_Component_Bit_Offset (Fent, Uint_0);
2145 Set_Normalized_Position (Fent, Uint_0);
2146 Set_Normalized_First_Bit (Fent, Uint_0);
2147 Set_Normalized_Position_Max (Fent, Uint_0);
2148 Init_Esize (Fent, System_Address_Size);
2150 Set_Component_Clause (Fent,
2151 Make_Component_Clause (Loc,
2153 Make_Identifier (Loc,
2154 Chars => Name_uTag),
2157 Make_Integer_Literal (Loc,
2161 Make_Integer_Literal (Loc,
2165 Make_Integer_Literal (Loc,
2166 UI_From_Int (System_Address_Size))));
2168 Ccount := Ccount + 1;
2171 -- A representation like this applies to the base type
2173 Set_Has_Record_Rep_Clause (Base_Type (Rectype));
2174 Set_Has_Non_Standard_Rep (Base_Type (Rectype));
2175 Set_Has_Specified_Layout (Base_Type (Rectype));
2177 Max_Bit_So_Far := Uint_Minus_1;
2178 Overlap_Check_Required := False;
2180 -- Process the component clauses
2182 while Present (CC) loop
2186 if Nkind (CC) = N_Pragma then
2189 -- The only pragma of interest is Complete_Representation
2191 if Chars (CC) = Name_Complete_Representation then
2195 -- Processing for real component clause
2198 Ccount := Ccount + 1;
2199 Posit := Static_Integer (Position (CC));
2200 Fbit := Static_Integer (First_Bit (CC));
2201 Lbit := Static_Integer (Last_Bit (CC));
2204 and then Fbit /= No_Uint
2205 and then Lbit /= No_Uint
2209 ("position cannot be negative", Position (CC));
2213 ("first bit cannot be negative", First_Bit (CC));
2215 -- Values look OK, so find the corresponding record component
2216 -- Even though the syntax allows an attribute reference for
2217 -- implementation-defined components, GNAT does not allow the
2218 -- tag to get an explicit position.
2220 elsif Nkind (Component_Name (CC)) = N_Attribute_Reference then
2221 if Attribute_Name (Component_Name (CC)) = Name_Tag then
2222 Error_Msg_N ("position of tag cannot be specified", CC);
2224 Error_Msg_N ("illegal component name", CC);
2228 Comp := First_Entity (Rectype);
2229 while Present (Comp) loop
2230 exit when Chars (Comp) = Chars (Component_Name (CC));
2236 -- Maybe component of base type that is absent from
2237 -- statically constrained first subtype.
2239 Comp := First_Entity (Base_Type (Rectype));
2240 while Present (Comp) loop
2241 exit when Chars (Comp) = Chars (Component_Name (CC));
2248 ("component clause is for non-existent field", CC);
2250 elsif Present (Component_Clause (Comp)) then
2251 Error_Msg_Sloc := Sloc (Component_Clause (Comp));
2253 ("component clause previously given#", CC);
2256 -- Update Fbit and Lbit to the actual bit number
2258 Fbit := Fbit + UI_From_Int (SSU) * Posit;
2259 Lbit := Lbit + UI_From_Int (SSU) * Posit;
2261 if Fbit <= Max_Bit_So_Far then
2262 Overlap_Check_Required := True;
2264 Max_Bit_So_Far := Lbit;
2267 if Has_Size_Clause (Rectype)
2268 and then Esize (Rectype) <= Lbit
2271 ("bit number out of range of specified size",
2274 Set_Component_Clause (Comp, CC);
2275 Set_Component_Bit_Offset (Comp, Fbit);
2276 Set_Esize (Comp, 1 + (Lbit - Fbit));
2277 Set_Normalized_First_Bit (Comp, Fbit mod SSU);
2278 Set_Normalized_Position (Comp, Fbit / SSU);
2280 Set_Normalized_Position_Max
2281 (Fent, Normalized_Position (Fent));
2283 if Is_Tagged_Type (Rectype)
2284 and then Fbit < System_Address_Size
2287 ("component overlaps tag field of&",
2291 -- This information is also set in the corresponding
2292 -- component of the base type, found by accessing the
2293 -- Original_Record_Component link if it is present.
2295 Ocomp := Original_Record_Component (Comp);
2302 (Component_Name (CC),
2307 Set_Has_Biased_Representation (Comp, Biased);
2309 if Present (Ocomp) then
2310 Set_Component_Clause (Ocomp, CC);
2311 Set_Component_Bit_Offset (Ocomp, Fbit);
2312 Set_Normalized_First_Bit (Ocomp, Fbit mod SSU);
2313 Set_Normalized_Position (Ocomp, Fbit / SSU);
2314 Set_Esize (Ocomp, 1 + (Lbit - Fbit));
2316 Set_Normalized_Position_Max
2317 (Ocomp, Normalized_Position (Ocomp));
2319 Set_Has_Biased_Representation
2320 (Ocomp, Has_Biased_Representation (Comp));
2323 if Esize (Comp) < 0 then
2324 Error_Msg_N ("component size is negative", CC);
2335 -- Now that we have processed all the component clauses, check for
2336 -- overlap. We have to leave this till last, since the components
2337 -- can appear in any arbitrary order in the representation clause.
2339 -- We do not need this check if all specified ranges were monotonic,
2340 -- as recorded by Overlap_Check_Required being False at this stage.
2342 -- This first section checks if there are any overlapping entries
2343 -- at all. It does this by sorting all entries and then seeing if
2344 -- there are any overlaps. If there are none, then that is decisive,
2345 -- but if there are overlaps, they may still be OK (they may result
2346 -- from fields in different variants).
2348 if Overlap_Check_Required then
2349 Overlap_Check1 : declare
2351 OC_Fbit : array (0 .. Ccount) of Uint;
2352 -- First-bit values for component clauses, the value is the
2353 -- offset of the first bit of the field from start of record.
2354 -- The zero entry is for use in sorting.
2356 OC_Lbit : array (0 .. Ccount) of Uint;
2357 -- Last-bit values for component clauses, the value is the
2358 -- offset of the last bit of the field from start of record.
2359 -- The zero entry is for use in sorting.
2361 OC_Count : Natural := 0;
2362 -- Count of entries in OC_Fbit and OC_Lbit
2364 function OC_Lt (Op1, Op2 : Natural) return Boolean;
2365 -- Compare routine for Sort (See GNAT.Heap_Sort_A)
2367 procedure OC_Move (From : Natural; To : Natural);
2368 -- Move routine for Sort (see GNAT.Heap_Sort_A)
2370 function OC_Lt (Op1, Op2 : Natural) return Boolean is
2372 return OC_Fbit (Op1) < OC_Fbit (Op2);
2375 procedure OC_Move (From : Natural; To : Natural) is
2377 OC_Fbit (To) := OC_Fbit (From);
2378 OC_Lbit (To) := OC_Lbit (From);
2382 CC := First (Component_Clauses (N));
2383 while Present (CC) loop
2384 if Nkind (CC) /= N_Pragma then
2385 Posit := Static_Integer (Position (CC));
2386 Fbit := Static_Integer (First_Bit (CC));
2387 Lbit := Static_Integer (Last_Bit (CC));
2390 and then Fbit /= No_Uint
2391 and then Lbit /= No_Uint
2393 OC_Count := OC_Count + 1;
2394 Posit := Posit * SSU;
2395 OC_Fbit (OC_Count) := Fbit + Posit;
2396 OC_Lbit (OC_Count) := Lbit + Posit;
2405 OC_Move'Unrestricted_Access,
2406 OC_Lt'Unrestricted_Access);
2408 Overlap_Check_Required := False;
2409 for J in 1 .. OC_Count - 1 loop
2410 if OC_Lbit (J) >= OC_Fbit (J + 1) then
2411 Overlap_Check_Required := True;
2418 -- If Overlap_Check_Required is still True, then we have to do
2419 -- the full scale overlap check, since we have at least two fields
2420 -- that do overlap, and we need to know if that is OK since they
2421 -- are in the same variant, or whether we have a definite problem
2423 if Overlap_Check_Required then
2424 Overlap_Check2 : declare
2425 C1_Ent, C2_Ent : Entity_Id;
2426 -- Entities of components being checked for overlap
2429 -- Component_List node whose Component_Items are being checked
2432 -- Component declaration for component being checked
2435 C1_Ent := First_Entity (Base_Type (Rectype));
2437 -- Loop through all components in record. For each component check
2438 -- for overlap with any of the preceding elements on the component
2439 -- list containing the component, and also, if the component is in
2440 -- a variant, check against components outside the case structure.
2441 -- This latter test is repeated recursively up the variant tree.
2443 Main_Component_Loop : while Present (C1_Ent) loop
2444 if Ekind (C1_Ent) /= E_Component
2445 and then Ekind (C1_Ent) /= E_Discriminant
2447 goto Continue_Main_Component_Loop;
2450 -- Skip overlap check if entity has no declaration node. This
2451 -- happens with discriminants in constrained derived types.
2452 -- Probably we are missing some checks as a result, but that
2453 -- does not seem terribly serious ???
2455 if No (Declaration_Node (C1_Ent)) then
2456 goto Continue_Main_Component_Loop;
2459 Clist := Parent (List_Containing (Declaration_Node (C1_Ent)));
2461 -- Loop through component lists that need checking. Check the
2462 -- current component list and all lists in variants above us.
2464 Component_List_Loop : loop
2466 -- If derived type definition, go to full declaration
2467 -- If at outer level, check discriminants if there are any
2469 if Nkind (Clist) = N_Derived_Type_Definition then
2470 Clist := Parent (Clist);
2473 -- Outer level of record definition, check discriminants
2475 if Nkind (Clist) = N_Full_Type_Declaration
2476 or else Nkind (Clist) = N_Private_Type_Declaration
2478 if Has_Discriminants (Defining_Identifier (Clist)) then
2480 First_Discriminant (Defining_Identifier (Clist));
2482 while Present (C2_Ent) loop
2483 exit when C1_Ent = C2_Ent;
2484 Check_Component_Overlap (C1_Ent, C2_Ent);
2485 Next_Discriminant (C2_Ent);
2489 -- Record extension case
2491 elsif Nkind (Clist) = N_Derived_Type_Definition then
2494 -- Otherwise check one component list
2497 Citem := First (Component_Items (Clist));
2499 while Present (Citem) loop
2500 if Nkind (Citem) = N_Component_Declaration then
2501 C2_Ent := Defining_Identifier (Citem);
2502 exit when C1_Ent = C2_Ent;
2503 Check_Component_Overlap (C1_Ent, C2_Ent);
2510 -- Check for variants above us (the parent of the Clist can
2511 -- be a variant, in which case its parent is a variant part,
2512 -- and the parent of the variant part is a component list
2513 -- whose components must all be checked against the current
2514 -- component for overlap.
2516 if Nkind (Parent (Clist)) = N_Variant then
2517 Clist := Parent (Parent (Parent (Clist)));
2519 -- Check for possible discriminant part in record, this is
2520 -- treated essentially as another level in the recursion.
2521 -- For this case we have the parent of the component list
2522 -- is the record definition, and its parent is the full
2523 -- type declaration which contains the discriminant
2526 elsif Nkind (Parent (Clist)) = N_Record_Definition then
2527 Clist := Parent (Parent ((Clist)));
2529 -- If neither of these two cases, we are at the top of
2533 exit Component_List_Loop;
2535 end loop Component_List_Loop;
2537 <<Continue_Main_Component_Loop>>
2538 Next_Entity (C1_Ent);
2540 end loop Main_Component_Loop;
2544 -- For records that have component clauses for all components, and
2545 -- whose size is less than or equal to 32, we need to know the size
2546 -- in the front end to activate possible packed array processing
2547 -- where the component type is a record.
2549 -- At this stage Hbit + 1 represents the first unused bit from all
2550 -- the component clauses processed, so if the component clauses are
2551 -- complete, then this is the length of the record.
2553 -- For records longer than System.Storage_Unit, and for those where
2554 -- not all components have component clauses, the back end determines
2555 -- the length (it may for example be appopriate to round up the size
2556 -- to some convenient boundary, based on alignment considerations etc).
2558 if Unknown_RM_Size (Rectype)
2559 and then Hbit + 1 <= 32
2561 -- Nothing to do if at least one component with no component clause
2563 Comp := First_Component_Or_Discriminant (Rectype);
2564 while Present (Comp) loop
2565 exit when No (Component_Clause (Comp));
2566 Next_Component_Or_Discriminant (Comp);
2569 -- If we fall out of loop, all components have component clauses
2570 -- and so we can set the size to the maximum value.
2573 Set_RM_Size (Rectype, Hbit + 1);
2577 -- Check missing components if Complete_Representation pragma appeared
2579 if Present (CR_Pragma) then
2580 Comp := First_Component_Or_Discriminant (Rectype);
2581 while Present (Comp) loop
2582 if No (Component_Clause (Comp)) then
2584 ("missing component clause for &", CR_Pragma, Comp);
2587 Next_Component_Or_Discriminant (Comp);
2590 -- If no Complete_Representation pragma, warn if missing components
2592 elsif Warn_On_Unrepped_Components
2593 and then not Warnings_Off (Rectype)
2596 Num_Repped_Components : Nat := 0;
2597 Num_Unrepped_Components : Nat := 0;
2600 -- First count number of repped and unrepped components
2602 Comp := First_Component_Or_Discriminant (Rectype);
2603 while Present (Comp) loop
2604 if Present (Component_Clause (Comp)) then
2605 Num_Repped_Components := Num_Repped_Components + 1;
2607 Num_Unrepped_Components := Num_Unrepped_Components + 1;
2610 Next_Component_Or_Discriminant (Comp);
2613 -- We are only interested in the case where there is at least one
2614 -- unrepped component, and at least half the components have rep
2615 -- clauses. We figure that if less than half have them, then the
2616 -- partial rep clause is really intentional.
2618 if Num_Unrepped_Components > 0
2619 and then Num_Unrepped_Components < Num_Repped_Components
2621 Comp := First_Component_Or_Discriminant (Rectype);
2622 while Present (Comp) loop
2623 if No (Component_Clause (Comp)) then
2624 Error_Msg_Sloc := Sloc (Comp);
2626 ("?no component clause given for & declared #",
2630 Next_Component_Or_Discriminant (Comp);
2635 end Analyze_Record_Representation_Clause;
2637 -----------------------------
2638 -- Check_Component_Overlap --
2639 -----------------------------
2641 procedure Check_Component_Overlap (C1_Ent, C2_Ent : Entity_Id) is
2643 if Present (Component_Clause (C1_Ent))
2644 and then Present (Component_Clause (C2_Ent))
2646 -- Exclude odd case where we have two tag fields in the same
2647 -- record, both at location zero. This seems a bit strange,
2648 -- but it seems to happen in some circumstances ???
2650 if Chars (C1_Ent) = Name_uTag
2651 and then Chars (C2_Ent) = Name_uTag
2656 -- Here we check if the two fields overlap
2659 S1 : constant Uint := Component_Bit_Offset (C1_Ent);
2660 S2 : constant Uint := Component_Bit_Offset (C2_Ent);
2661 E1 : constant Uint := S1 + Esize (C1_Ent);
2662 E2 : constant Uint := S2 + Esize (C2_Ent);
2665 if E2 <= S1 or else E1 <= S2 then
2669 Component_Name (Component_Clause (C2_Ent));
2670 Error_Msg_Sloc := Sloc (Error_Msg_Node_2);
2672 Component_Name (Component_Clause (C1_Ent));
2674 ("component& overlaps & #",
2675 Component_Name (Component_Clause (C1_Ent)));
2679 end Check_Component_Overlap;
2681 -----------------------------------
2682 -- Check_Constant_Address_Clause --
2683 -----------------------------------
2685 procedure Check_Constant_Address_Clause
2689 procedure Check_At_Constant_Address (Nod : Node_Id);
2690 -- Checks that the given node N represents a name whose 'Address
2691 -- is constant (in the same sense as OK_Constant_Address_Clause,
2692 -- i.e. the address value is the same at the point of declaration
2693 -- of U_Ent and at the time of elaboration of the address clause.
2695 procedure Check_Expr_Constants (Nod : Node_Id);
2696 -- Checks that Nod meets the requirements for a constant address
2697 -- clause in the sense of the enclosing procedure.
2699 procedure Check_List_Constants (Lst : List_Id);
2700 -- Check that all elements of list Lst meet the requirements for a
2701 -- constant address clause in the sense of the enclosing procedure.
2703 -------------------------------
2704 -- Check_At_Constant_Address --
2705 -------------------------------
2707 procedure Check_At_Constant_Address (Nod : Node_Id) is
2709 if Is_Entity_Name (Nod) then
2710 if Present (Address_Clause (Entity ((Nod)))) then
2712 ("invalid address clause for initialized object &!",
2715 ("address for& cannot" &
2716 " depend on another address clause! ('R'M 13.1(22))!",
2719 elsif In_Same_Source_Unit (Entity (Nod), U_Ent)
2720 and then Sloc (U_Ent) < Sloc (Entity (Nod))
2723 ("invalid address clause for initialized object &!",
2725 Error_Msg_Name_1 := Chars (Entity (Nod));
2726 Error_Msg_Name_2 := Chars (U_Ent);
2728 ("\% must be defined before % ('R'M 13.1(22))!",
2732 elsif Nkind (Nod) = N_Selected_Component then
2734 T : constant Entity_Id := Etype (Prefix (Nod));
2737 if (Is_Record_Type (T)
2738 and then Has_Discriminants (T))
2741 and then Is_Record_Type (Designated_Type (T))
2742 and then Has_Discriminants (Designated_Type (T)))
2745 ("invalid address clause for initialized object &!",
2748 ("\address cannot depend on component" &
2749 " of discriminated record ('R'M 13.1(22))!",
2752 Check_At_Constant_Address (Prefix (Nod));
2756 elsif Nkind (Nod) = N_Indexed_Component then
2757 Check_At_Constant_Address (Prefix (Nod));
2758 Check_List_Constants (Expressions (Nod));
2761 Check_Expr_Constants (Nod);
2763 end Check_At_Constant_Address;
2765 --------------------------
2766 -- Check_Expr_Constants --
2767 --------------------------
2769 procedure Check_Expr_Constants (Nod : Node_Id) is
2770 Loc_U_Ent : constant Source_Ptr := Sloc (U_Ent);
2771 Ent : Entity_Id := Empty;
2774 if Nkind (Nod) in N_Has_Etype
2775 and then Etype (Nod) = Any_Type
2781 when N_Empty | N_Error =>
2784 when N_Identifier | N_Expanded_Name =>
2785 Ent := Entity (Nod);
2787 -- We need to look at the original node if it is different
2788 -- from the node, since we may have rewritten things and
2789 -- substituted an identifier representing the rewrite.
2791 if Original_Node (Nod) /= Nod then
2792 Check_Expr_Constants (Original_Node (Nod));
2794 -- If the node is an object declaration without initial
2795 -- value, some code has been expanded, and the expression
2796 -- is not constant, even if the constituents might be
2797 -- acceptable, as in A'Address + offset.
2799 if Ekind (Ent) = E_Variable
2800 and then Nkind (Declaration_Node (Ent))
2801 = N_Object_Declaration
2803 No (Expression (Declaration_Node (Ent)))
2806 ("invalid address clause for initialized object &!",
2809 -- If entity is constant, it may be the result of expanding
2810 -- a check. We must verify that its declaration appears
2811 -- before the object in question, else we also reject the
2814 elsif Ekind (Ent) = E_Constant
2815 and then In_Same_Source_Unit (Ent, U_Ent)
2816 and then Sloc (Ent) > Loc_U_Ent
2819 ("invalid address clause for initialized object &!",
2826 -- Otherwise look at the identifier and see if it is OK
2828 if Ekind (Ent) = E_Named_Integer
2830 Ekind (Ent) = E_Named_Real
2837 Ekind (Ent) = E_Constant
2839 Ekind (Ent) = E_In_Parameter
2841 -- This is the case where we must have Ent defined
2842 -- before U_Ent. Clearly if they are in different
2843 -- units this requirement is met since the unit
2844 -- containing Ent is already processed.
2846 if not In_Same_Source_Unit (Ent, U_Ent) then
2849 -- Otherwise location of Ent must be before the
2850 -- location of U_Ent, that's what prior defined means.
2852 elsif Sloc (Ent) < Loc_U_Ent then
2857 ("invalid address clause for initialized object &!",
2859 Error_Msg_Name_1 := Chars (Ent);
2860 Error_Msg_Name_2 := Chars (U_Ent);
2862 ("\% must be defined before % ('R'M 13.1(22))!",
2866 elsif Nkind (Original_Node (Nod)) = N_Function_Call then
2867 Check_Expr_Constants (Original_Node (Nod));
2871 ("invalid address clause for initialized object &!",
2874 if Comes_From_Source (Ent) then
2875 Error_Msg_Name_1 := Chars (Ent);
2877 ("\reference to variable% not allowed"
2878 & " ('R'M 13.1(22))!", Nod);
2881 ("non-static expression not allowed"
2882 & " ('R'M 13.1(22))!", Nod);
2886 when N_Integer_Literal =>
2888 -- If this is a rewritten unchecked conversion, in a system
2889 -- where Address is an integer type, always use the base type
2890 -- for a literal value. This is user-friendly and prevents
2891 -- order-of-elaboration issues with instances of unchecked
2894 if Nkind (Original_Node (Nod)) = N_Function_Call then
2895 Set_Etype (Nod, Base_Type (Etype (Nod)));
2898 when N_Real_Literal |
2900 N_Character_Literal =>
2904 Check_Expr_Constants (Low_Bound (Nod));
2905 Check_Expr_Constants (High_Bound (Nod));
2907 when N_Explicit_Dereference =>
2908 Check_Expr_Constants (Prefix (Nod));
2910 when N_Indexed_Component =>
2911 Check_Expr_Constants (Prefix (Nod));
2912 Check_List_Constants (Expressions (Nod));
2915 Check_Expr_Constants (Prefix (Nod));
2916 Check_Expr_Constants (Discrete_Range (Nod));
2918 when N_Selected_Component =>
2919 Check_Expr_Constants (Prefix (Nod));
2921 when N_Attribute_Reference =>
2922 if Attribute_Name (Nod) = Name_Address
2924 Attribute_Name (Nod) = Name_Access
2926 Attribute_Name (Nod) = Name_Unchecked_Access
2928 Attribute_Name (Nod) = Name_Unrestricted_Access
2930 Check_At_Constant_Address (Prefix (Nod));
2933 Check_Expr_Constants (Prefix (Nod));
2934 Check_List_Constants (Expressions (Nod));
2938 Check_List_Constants (Component_Associations (Nod));
2939 Check_List_Constants (Expressions (Nod));
2941 when N_Component_Association =>
2942 Check_Expr_Constants (Expression (Nod));
2944 when N_Extension_Aggregate =>
2945 Check_Expr_Constants (Ancestor_Part (Nod));
2946 Check_List_Constants (Component_Associations (Nod));
2947 Check_List_Constants (Expressions (Nod));
2952 when N_Binary_Op | N_And_Then | N_Or_Else | N_Membership_Test =>
2953 Check_Expr_Constants (Left_Opnd (Nod));
2954 Check_Expr_Constants (Right_Opnd (Nod));
2957 Check_Expr_Constants (Right_Opnd (Nod));
2959 when N_Type_Conversion |
2960 N_Qualified_Expression |
2962 Check_Expr_Constants (Expression (Nod));
2964 when N_Unchecked_Type_Conversion =>
2965 Check_Expr_Constants (Expression (Nod));
2967 -- If this is a rewritten unchecked conversion, subtypes
2968 -- in this node are those created within the instance.
2969 -- To avoid order of elaboration issues, replace them
2970 -- with their base types. Note that address clauses can
2971 -- cause order of elaboration problems because they are
2972 -- elaborated by the back-end at the point of definition,
2973 -- and may mention entities declared in between (as long
2974 -- as everything is static). It is user-friendly to allow
2975 -- unchecked conversions in this context.
2977 if Nkind (Original_Node (Nod)) = N_Function_Call then
2978 Set_Etype (Expression (Nod),
2979 Base_Type (Etype (Expression (Nod))));
2980 Set_Etype (Nod, Base_Type (Etype (Nod)));
2983 when N_Function_Call =>
2984 if not Is_Pure (Entity (Name (Nod))) then
2986 ("invalid address clause for initialized object &!",
2990 ("\function & is not pure ('R'M 13.1(22))!",
2991 Nod, Entity (Name (Nod)));
2994 Check_List_Constants (Parameter_Associations (Nod));
2997 when N_Parameter_Association =>
2998 Check_Expr_Constants (Explicit_Actual_Parameter (Nod));
3002 ("invalid address clause for initialized object &!",
3005 ("\must be constant defined before& ('R'M 13.1(22))!",
3008 end Check_Expr_Constants;
3010 --------------------------
3011 -- Check_List_Constants --
3012 --------------------------
3014 procedure Check_List_Constants (Lst : List_Id) is
3018 if Present (Lst) then
3019 Nod1 := First (Lst);
3020 while Present (Nod1) loop
3021 Check_Expr_Constants (Nod1);
3025 end Check_List_Constants;
3027 -- Start of processing for Check_Constant_Address_Clause
3030 Check_Expr_Constants (Expr);
3031 end Check_Constant_Address_Clause;
3037 procedure Check_Size
3041 Biased : out Boolean)
3043 UT : constant Entity_Id := Underlying_Type (T);
3049 -- Dismiss cases for generic types or types with previous errors
3052 or else UT = Any_Type
3053 or else Is_Generic_Type (UT)
3054 or else Is_Generic_Type (Root_Type (UT))
3058 -- Check case of bit packed array
3060 elsif Is_Array_Type (UT)
3061 and then Known_Static_Component_Size (UT)
3062 and then Is_Bit_Packed_Array (UT)
3070 Asiz := Component_Size (UT);
3071 Indx := First_Index (UT);
3073 Ityp := Etype (Indx);
3075 -- If non-static bound, then we are not in the business of
3076 -- trying to check the length, and indeed an error will be
3077 -- issued elsewhere, since sizes of non-static array types
3078 -- cannot be set implicitly or explicitly.
3080 if not Is_Static_Subtype (Ityp) then
3084 -- Otherwise accumulate next dimension
3086 Asiz := Asiz * (Expr_Value (Type_High_Bound (Ityp)) -
3087 Expr_Value (Type_Low_Bound (Ityp)) +
3091 exit when No (Indx);
3097 Error_Msg_Uint_1 := Asiz;
3099 ("size for& too small, minimum allowed is ^", N, T);
3100 Set_Esize (T, Asiz);
3101 Set_RM_Size (T, Asiz);
3105 -- All other composite types are ignored
3107 elsif Is_Composite_Type (UT) then
3110 -- For fixed-point types, don't check minimum if type is not frozen,
3111 -- since we don't know all the characteristics of the type that can
3112 -- affect the size (e.g. a specified small) till freeze time.
3114 elsif Is_Fixed_Point_Type (UT)
3115 and then not Is_Frozen (UT)
3119 -- Cases for which a minimum check is required
3122 -- Ignore if specified size is correct for the type
3124 if Known_Esize (UT) and then Siz = Esize (UT) then
3128 -- Otherwise get minimum size
3130 M := UI_From_Int (Minimum_Size (UT));
3134 -- Size is less than minimum size, but one possibility remains
3135 -- that we can manage with the new size if we bias the type
3137 M := UI_From_Int (Minimum_Size (UT, Biased => True));
3140 Error_Msg_Uint_1 := M;
3142 ("size for& too small, minimum allowed is ^", N, T);
3152 -------------------------
3153 -- Get_Alignment_Value --
3154 -------------------------
3156 function Get_Alignment_Value (Expr : Node_Id) return Uint is
3157 Align : constant Uint := Static_Integer (Expr);
3160 if Align = No_Uint then
3163 elsif Align <= 0 then
3164 Error_Msg_N ("alignment value must be positive", Expr);
3168 for J in Int range 0 .. 64 loop
3170 M : constant Uint := Uint_2 ** J;
3173 exit when M = Align;
3177 ("alignment value must be power of 2", Expr);
3185 end Get_Alignment_Value;
3191 procedure Initialize is
3193 Unchecked_Conversions.Init;
3196 -------------------------
3197 -- Is_Operational_Item --
3198 -------------------------
3200 function Is_Operational_Item (N : Node_Id) return Boolean is
3202 if Nkind (N) /= N_Attribute_Definition_Clause then
3206 Id : constant Attribute_Id := Get_Attribute_Id (Chars (N));
3209 return Id = Attribute_Input
3210 or else Id = Attribute_Output
3211 or else Id = Attribute_Read
3212 or else Id = Attribute_Write
3213 or else Id = Attribute_External_Tag;
3216 end Is_Operational_Item;
3218 --------------------------------------
3219 -- Mark_Aliased_Address_As_Volatile --
3220 --------------------------------------
3222 procedure Mark_Aliased_Address_As_Volatile (N : Node_Id) is
3223 Ent : constant Entity_Id := Address_Aliased_Entity (N);
3226 if Present (Ent) then
3227 Set_Treat_As_Volatile (Ent);
3229 end Mark_Aliased_Address_As_Volatile;
3235 function Minimum_Size
3237 Biased : Boolean := False) return Nat
3239 Lo : Uint := No_Uint;
3240 Hi : Uint := No_Uint;
3241 LoR : Ureal := No_Ureal;
3242 HiR : Ureal := No_Ureal;
3243 LoSet : Boolean := False;
3244 HiSet : Boolean := False;
3248 R_Typ : constant Entity_Id := Root_Type (T);
3251 -- If bad type, return 0
3253 if T = Any_Type then
3256 -- For generic types, just return zero. There cannot be any legitimate
3257 -- need to know such a size, but this routine may be called with a
3258 -- generic type as part of normal processing.
3260 elsif Is_Generic_Type (R_Typ)
3261 or else R_Typ = Any_Type
3265 -- Access types. Normally an access type cannot have a size smaller
3266 -- than the size of System.Address. The exception is on VMS, where
3267 -- we have short and long addresses, and it is possible for an access
3268 -- type to have a short address size (and thus be less than the size
3269 -- of System.Address itself). We simply skip the check for VMS, and
3270 -- leave the back end to do the check.
3272 elsif Is_Access_Type (T) then
3273 if OpenVMS_On_Target then
3276 return System_Address_Size;
3279 -- Floating-point types
3281 elsif Is_Floating_Point_Type (T) then
3282 return UI_To_Int (Esize (R_Typ));
3286 elsif Is_Discrete_Type (T) then
3288 -- The following loop is looking for the nearest compile time
3289 -- known bounds following the ancestor subtype chain. The idea
3290 -- is to find the most restrictive known bounds information.
3294 if Ancest = Any_Type or else Etype (Ancest) = Any_Type then
3299 if Compile_Time_Known_Value (Type_Low_Bound (Ancest)) then
3300 Lo := Expr_Rep_Value (Type_Low_Bound (Ancest));
3307 if Compile_Time_Known_Value (Type_High_Bound (Ancest)) then
3308 Hi := Expr_Rep_Value (Type_High_Bound (Ancest));
3314 Ancest := Ancestor_Subtype (Ancest);
3317 Ancest := Base_Type (T);
3319 if Is_Generic_Type (Ancest) then
3325 -- Fixed-point types. We can't simply use Expr_Value to get the
3326 -- Corresponding_Integer_Value values of the bounds, since these
3327 -- do not get set till the type is frozen, and this routine can
3328 -- be called before the type is frozen. Similarly the test for
3329 -- bounds being static needs to include the case where we have
3330 -- unanalyzed real literals for the same reason.
3332 elsif Is_Fixed_Point_Type (T) then
3334 -- The following loop is looking for the nearest compile time
3335 -- known bounds following the ancestor subtype chain. The idea
3336 -- is to find the most restrictive known bounds information.
3340 if Ancest = Any_Type or else Etype (Ancest) = Any_Type then
3345 if Nkind (Type_Low_Bound (Ancest)) = N_Real_Literal
3346 or else Compile_Time_Known_Value (Type_Low_Bound (Ancest))
3348 LoR := Expr_Value_R (Type_Low_Bound (Ancest));
3355 if Nkind (Type_High_Bound (Ancest)) = N_Real_Literal
3356 or else Compile_Time_Known_Value (Type_High_Bound (Ancest))
3358 HiR := Expr_Value_R (Type_High_Bound (Ancest));
3364 Ancest := Ancestor_Subtype (Ancest);
3367 Ancest := Base_Type (T);
3369 if Is_Generic_Type (Ancest) then
3375 Lo := UR_To_Uint (LoR / Small_Value (T));
3376 Hi := UR_To_Uint (HiR / Small_Value (T));
3378 -- No other types allowed
3381 raise Program_Error;
3384 -- Fall through with Hi and Lo set. Deal with biased case
3386 if (Biased and then not Is_Fixed_Point_Type (T))
3387 or else Has_Biased_Representation (T)
3393 -- Signed case. Note that we consider types like range 1 .. -1 to be
3394 -- signed for the purpose of computing the size, since the bounds
3395 -- have to be accomodated in the base type.
3397 if Lo < 0 or else Hi < 0 then
3401 -- S = size, B = 2 ** (size - 1) (can accommodate -B .. +(B - 1))
3402 -- Note that we accommodate the case where the bounds cross. This
3403 -- can happen either because of the way the bounds are declared
3404 -- or because of the algorithm in Freeze_Fixed_Point_Type.
3418 -- If both bounds are positive, make sure that both are represen-
3419 -- table in the case where the bounds are crossed. This can happen
3420 -- either because of the way the bounds are declared, or because of
3421 -- the algorithm in Freeze_Fixed_Point_Type.
3427 -- S = size, (can accommodate 0 .. (2**size - 1))
3430 while Hi >= Uint_2 ** S loop
3438 ---------------------------
3439 -- New_Stream_Subprogram --
3440 ---------------------------
3442 procedure New_Stream_Subprogram
3446 Nam : TSS_Name_Type)
3448 Loc : constant Source_Ptr := Sloc (N);
3449 Sname : constant Name_Id := Make_TSS_Name (Base_Type (Ent), Nam);
3450 Subp_Id : Entity_Id;
3451 Subp_Decl : Node_Id;
3455 Defer_Declaration : constant Boolean :=
3456 Is_Tagged_Type (Ent) or else Is_Private_Type (Ent);
3457 -- For a tagged type, there is a declaration for each stream attribute
3458 -- at the freeze point, and we must generate only a completion of this
3459 -- declaration. We do the same for private types, because the full view
3460 -- might be tagged. Otherwise we generate a declaration at the point of
3461 -- the attribute definition clause.
3463 function Build_Spec return Node_Id;
3464 -- Used for declaration and renaming declaration, so that this is
3465 -- treated as a renaming_as_body.
3471 function Build_Spec return Node_Id is
3472 Out_P : constant Boolean := (Nam = TSS_Stream_Read);
3475 T_Ref : constant Node_Id := New_Reference_To (Etyp, Loc);
3478 Subp_Id := Make_Defining_Identifier (Loc, Sname);
3480 -- S : access Root_Stream_Type'Class
3482 Formals := New_List (
3483 Make_Parameter_Specification (Loc,
3484 Defining_Identifier =>
3485 Make_Defining_Identifier (Loc, Name_S),
3487 Make_Access_Definition (Loc,
3490 Designated_Type (Etype (F)), Loc))));
3492 if Nam = TSS_Stream_Input then
3493 Spec := Make_Function_Specification (Loc,
3494 Defining_Unit_Name => Subp_Id,
3495 Parameter_Specifications => Formals,
3496 Result_Definition => T_Ref);
3501 Make_Parameter_Specification (Loc,
3502 Defining_Identifier => Make_Defining_Identifier (Loc, Name_V),
3503 Out_Present => Out_P,
3504 Parameter_Type => T_Ref));
3506 Spec := Make_Procedure_Specification (Loc,
3507 Defining_Unit_Name => Subp_Id,
3508 Parameter_Specifications => Formals);
3514 -- Start of processing for New_Stream_Subprogram
3517 F := First_Formal (Subp);
3519 if Ekind (Subp) = E_Procedure then
3520 Etyp := Etype (Next_Formal (F));
3522 Etyp := Etype (Subp);
3525 -- Prepare subprogram declaration and insert it as an action on the
3526 -- clause node. The visibility for this entity is used to test for
3527 -- visibility of the attribute definition clause (in the sense of
3528 -- 8.3(23) as amended by AI-195).
3530 if not Defer_Declaration then
3532 Make_Subprogram_Declaration (Loc,
3533 Specification => Build_Spec);
3535 -- For a tagged type, there is always a visible declaration for each
3536 -- stream TSS (it is a predefined primitive operation), and the
3537 -- completion of this declaration occurs at the freeze point, which is
3538 -- not always visible at places where the attribute definition clause is
3539 -- visible. So, we create a dummy entity here for the purpose of
3540 -- tracking the visibility of the attribute definition clause itself.
3544 Make_Defining_Identifier (Loc,
3545 Chars => New_External_Name (Sname, 'V'));
3547 Make_Object_Declaration (Loc,
3548 Defining_Identifier => Subp_Id,
3549 Object_Definition => New_Occurrence_Of (Standard_Boolean, Loc));
3552 Insert_Action (N, Subp_Decl);
3553 Set_Entity (N, Subp_Id);
3556 Make_Subprogram_Renaming_Declaration (Loc,
3557 Specification => Build_Spec,
3558 Name => New_Reference_To (Subp, Loc));
3560 if Defer_Declaration then
3561 Set_TSS (Base_Type (Ent), Subp_Id);
3563 Insert_Action (N, Subp_Decl);
3564 Copy_TSS (Subp_Id, Base_Type (Ent));
3566 end New_Stream_Subprogram;
3568 ------------------------
3569 -- Rep_Item_Too_Early --
3570 ------------------------
3572 function Rep_Item_Too_Early (T : Entity_Id; N : Node_Id) return Boolean is
3574 -- Cannot apply non-operational rep items to generic types
3576 if Is_Operational_Item (N) then
3580 and then Is_Generic_Type (Root_Type (T))
3583 ("representation item not allowed for generic type", N);
3587 -- Otherwise check for incompleted type
3589 if Is_Incomplete_Or_Private_Type (T)
3590 and then No (Underlying_Type (T))
3593 ("representation item must be after full type declaration", N);
3596 -- If the type has incompleted components, a representation clause is
3597 -- illegal but stream attributes and Convention pragmas are correct.
3599 elsif Has_Private_Component (T) then
3600 if Nkind (N) = N_Pragma then
3604 ("representation item must appear after type is fully defined",
3611 end Rep_Item_Too_Early;
3613 -----------------------
3614 -- Rep_Item_Too_Late --
3615 -----------------------
3617 function Rep_Item_Too_Late
3620 FOnly : Boolean := False) return Boolean
3623 Parent_Type : Entity_Id;
3626 -- Output the too late message. Note that this is not considered a
3627 -- serious error, since the effect is simply that we ignore the
3628 -- representation clause in this case.
3634 procedure Too_Late is
3636 Error_Msg_N ("|representation item appears too late!", N);
3639 -- Start of processing for Rep_Item_Too_Late
3642 -- First make sure entity is not frozen (RM 13.1(9)). Exclude imported
3643 -- types, which may be frozen if they appear in a representation clause
3644 -- for a local type.
3647 and then not From_With_Type (T)
3650 S := First_Subtype (T);
3652 if Present (Freeze_Node (S)) then
3654 ("?no more representation items for }", Freeze_Node (S), S);
3659 -- Check for case of non-tagged derived type whose parent either has
3660 -- primitive operations, or is a by reference type (RM 13.1(10)).
3664 and then Is_Derived_Type (T)
3665 and then not Is_Tagged_Type (T)
3667 Parent_Type := Etype (Base_Type (T));
3669 if Has_Primitive_Operations (Parent_Type) then
3672 ("primitive operations already defined for&!", N, Parent_Type);
3675 elsif Is_By_Reference_Type (Parent_Type) then
3678 ("parent type & is a by reference type!", N, Parent_Type);
3683 -- No error, link item into head of chain of rep items for the entity
3685 Record_Rep_Item (T, N);
3687 end Rep_Item_Too_Late;
3689 -------------------------
3690 -- Same_Representation --
3691 -------------------------
3693 function Same_Representation (Typ1, Typ2 : Entity_Id) return Boolean is
3694 T1 : constant Entity_Id := Underlying_Type (Typ1);
3695 T2 : constant Entity_Id := Underlying_Type (Typ2);
3698 -- A quick check, if base types are the same, then we definitely have
3699 -- the same representation, because the subtype specific representation
3700 -- attributes (Size and Alignment) do not affect representation from
3701 -- the point of view of this test.
3703 if Base_Type (T1) = Base_Type (T2) then
3706 elsif Is_Private_Type (Base_Type (T2))
3707 and then Base_Type (T1) = Full_View (Base_Type (T2))
3712 -- Tagged types never have differing representations
3714 if Is_Tagged_Type (T1) then
3718 -- Representations are definitely different if conventions differ
3720 if Convention (T1) /= Convention (T2) then
3724 -- Representations are different if component alignments differ
3726 if (Is_Record_Type (T1) or else Is_Array_Type (T1))
3728 (Is_Record_Type (T2) or else Is_Array_Type (T2))
3729 and then Component_Alignment (T1) /= Component_Alignment (T2)
3734 -- For arrays, the only real issue is component size. If we know the
3735 -- component size for both arrays, and it is the same, then that's
3736 -- good enough to know we don't have a change of representation.
3738 if Is_Array_Type (T1) then
3739 if Known_Component_Size (T1)
3740 and then Known_Component_Size (T2)
3741 and then Component_Size (T1) = Component_Size (T2)
3747 -- Types definitely have same representation if neither has non-standard
3748 -- representation since default representations are always consistent.
3749 -- If only one has non-standard representation, and the other does not,
3750 -- then we consider that they do not have the same representation. They
3751 -- might, but there is no way of telling early enough.
3753 if Has_Non_Standard_Rep (T1) then
3754 if not Has_Non_Standard_Rep (T2) then
3758 return not Has_Non_Standard_Rep (T2);
3761 -- Here the two types both have non-standard representation, and we
3762 -- need to determine if they have the same non-standard representation
3764 -- For arrays, we simply need to test if the component sizes are the
3765 -- same. Pragma Pack is reflected in modified component sizes, so this
3766 -- check also deals with pragma Pack.
3768 if Is_Array_Type (T1) then
3769 return Component_Size (T1) = Component_Size (T2);
3771 -- Tagged types always have the same representation, because it is not
3772 -- possible to specify different representations for common fields.
3774 elsif Is_Tagged_Type (T1) then
3777 -- Case of record types
3779 elsif Is_Record_Type (T1) then
3781 -- Packed status must conform
3783 if Is_Packed (T1) /= Is_Packed (T2) then
3786 -- Otherwise we must check components. Typ2 maybe a constrained
3787 -- subtype with fewer components, so we compare the components
3788 -- of the base types.
3791 Record_Case : declare
3792 CD1, CD2 : Entity_Id;
3794 function Same_Rep return Boolean;
3795 -- CD1 and CD2 are either components or discriminants. This
3796 -- function tests whether the two have the same representation
3802 function Same_Rep return Boolean is
3804 if No (Component_Clause (CD1)) then
3805 return No (Component_Clause (CD2));
3809 Present (Component_Clause (CD2))
3811 Component_Bit_Offset (CD1) = Component_Bit_Offset (CD2)
3813 Esize (CD1) = Esize (CD2);
3817 -- Start processing for Record_Case
3820 if Has_Discriminants (T1) then
3821 CD1 := First_Discriminant (T1);
3822 CD2 := First_Discriminant (T2);
3824 -- The number of discriminants may be different if the
3825 -- derived type has fewer (constrained by values). The
3826 -- invisible discriminants retain the representation of
3827 -- the original, so the discrepancy does not per se
3828 -- indicate a different representation.
3831 and then Present (CD2)
3833 if not Same_Rep then
3836 Next_Discriminant (CD1);
3837 Next_Discriminant (CD2);
3842 CD1 := First_Component (Underlying_Type (Base_Type (T1)));
3843 CD2 := First_Component (Underlying_Type (Base_Type (T2)));
3845 while Present (CD1) loop
3846 if not Same_Rep then
3849 Next_Component (CD1);
3850 Next_Component (CD2);
3858 -- For enumeration types, we must check each literal to see if the
3859 -- representation is the same. Note that we do not permit enumeration
3860 -- reprsentation clauses for Character and Wide_Character, so these
3861 -- cases were already dealt with.
3863 elsif Is_Enumeration_Type (T1) then
3865 Enumeration_Case : declare
3869 L1 := First_Literal (T1);
3870 L2 := First_Literal (T2);
3872 while Present (L1) loop
3873 if Enumeration_Rep (L1) /= Enumeration_Rep (L2) then
3883 end Enumeration_Case;
3885 -- Any other types have the same representation for these purposes
3890 end Same_Representation;
3892 --------------------
3893 -- Set_Enum_Esize --
3894 --------------------
3896 procedure Set_Enum_Esize (T : Entity_Id) is
3904 -- Find the minimum standard size (8,16,32,64) that fits
3906 Lo := Enumeration_Rep (Entity (Type_Low_Bound (T)));
3907 Hi := Enumeration_Rep (Entity (Type_High_Bound (T)));
3910 if Lo >= -Uint_2**07 and then Hi < Uint_2**07 then
3911 Sz := Standard_Character_Size; -- May be > 8 on some targets
3913 elsif Lo >= -Uint_2**15 and then Hi < Uint_2**15 then
3916 elsif Lo >= -Uint_2**31 and then Hi < Uint_2**31 then
3919 else pragma Assert (Lo >= -Uint_2**63 and then Hi < Uint_2**63);
3924 if Hi < Uint_2**08 then
3925 Sz := Standard_Character_Size; -- May be > 8 on some targets
3927 elsif Hi < Uint_2**16 then
3930 elsif Hi < Uint_2**32 then
3933 else pragma Assert (Hi < Uint_2**63);
3938 -- That minimum is the proper size unless we have a foreign convention
3939 -- and the size required is 32 or less, in which case we bump the size
3940 -- up to 32. This is required for C and C++ and seems reasonable for
3941 -- all other foreign conventions.
3943 if Has_Foreign_Convention (T)
3944 and then Esize (T) < Standard_Integer_Size
3946 Init_Esize (T, Standard_Integer_Size);
3953 -----------------------------------
3954 -- Validate_Unchecked_Conversion --
3955 -----------------------------------
3957 procedure Validate_Unchecked_Conversion
3959 Act_Unit : Entity_Id)
3966 -- Obtain source and target types. Note that we call Ancestor_Subtype
3967 -- here because the processing for generic instantiation always makes
3968 -- subtypes, and we want the original frozen actual types.
3970 -- If we are dealing with private types, then do the check on their
3971 -- fully declared counterparts if the full declarations have been
3972 -- encountered (they don't have to be visible, but they must exist!)
3974 Source := Ancestor_Subtype (Etype (First_Formal (Act_Unit)));
3976 if Is_Private_Type (Source)
3977 and then Present (Underlying_Type (Source))
3979 Source := Underlying_Type (Source);
3982 Target := Ancestor_Subtype (Etype (Act_Unit));
3984 -- If either type is generic, the instantiation happens within a
3985 -- generic unit, and there is nothing to check. The proper check
3986 -- will happen when the enclosing generic is instantiated.
3988 if Is_Generic_Type (Source) or else Is_Generic_Type (Target) then
3992 if Is_Private_Type (Target)
3993 and then Present (Underlying_Type (Target))
3995 Target := Underlying_Type (Target);
3998 -- Source may be unconstrained array, but not target
4000 if Is_Array_Type (Target)
4001 and then not Is_Constrained (Target)
4004 ("unchecked conversion to unconstrained array not allowed", N);
4008 -- Make entry in unchecked conversion table for later processing
4009 -- by Validate_Unchecked_Conversions, which will check sizes and
4010 -- alignments (using values set by the back-end where possible).
4011 -- This is only done if the appropriate warning is active
4013 if Warn_On_Unchecked_Conversion then
4014 Unchecked_Conversions.Append
4015 (New_Val => UC_Entry'
4020 -- If both sizes are known statically now, then back end annotation
4021 -- is not required to do a proper check but if either size is not
4022 -- known statically, then we need the annotation.
4024 if Known_Static_RM_Size (Source)
4025 and then Known_Static_RM_Size (Target)
4029 Back_Annotate_Rep_Info := True;
4033 -- If unchecked conversion to access type, and access type is
4034 -- declared in the same unit as the unchecked conversion, then
4035 -- set the No_Strict_Aliasing flag (no strict aliasing is
4036 -- implicit in this situation).
4038 if Is_Access_Type (Target) and then
4039 In_Same_Source_Unit (Target, N)
4041 Set_No_Strict_Aliasing (Implementation_Base_Type (Target));
4044 -- Generate N_Validate_Unchecked_Conversion node for back end in
4045 -- case the back end needs to perform special validation checks.
4047 -- Shouldn't this be in exp_ch13, since the check only gets done
4048 -- if we have full expansion and the back end is called ???
4051 Make_Validate_Unchecked_Conversion (Sloc (N));
4052 Set_Source_Type (Vnode, Source);
4053 Set_Target_Type (Vnode, Target);
4055 -- If the unchecked conversion node is in a list, just insert before
4056 -- it. If not we have some strange case, not worth bothering about.
4058 if Is_List_Member (N) then
4059 Insert_After (N, Vnode);
4061 end Validate_Unchecked_Conversion;
4063 ------------------------------------
4064 -- Validate_Unchecked_Conversions --
4065 ------------------------------------
4067 procedure Validate_Unchecked_Conversions is
4069 for N in Unchecked_Conversions.First .. Unchecked_Conversions.Last loop
4071 T : UC_Entry renames Unchecked_Conversions.Table (N);
4073 Enode : constant Node_Id := T.Enode;
4074 Source : constant Entity_Id := T.Source;
4075 Target : constant Entity_Id := T.Target;
4081 -- This validation check, which warns if we have unequal sizes
4082 -- for unchecked conversion, and thus potentially implementation
4083 -- dependent semantics, is one of the few occasions on which we
4084 -- use the official RM size instead of Esize. See description
4085 -- in Einfo "Handling of Type'Size Values" for details.
4087 if Serious_Errors_Detected = 0
4088 and then Known_Static_RM_Size (Source)
4089 and then Known_Static_RM_Size (Target)
4091 Source_Siz := RM_Size (Source);
4092 Target_Siz := RM_Size (Target);
4094 if Source_Siz /= Target_Siz then
4096 ("types for unchecked conversion have different sizes?",
4099 if All_Errors_Mode then
4100 Error_Msg_Name_1 := Chars (Source);
4101 Error_Msg_Uint_1 := Source_Siz;
4102 Error_Msg_Name_2 := Chars (Target);
4103 Error_Msg_Uint_2 := Target_Siz;
4105 ("\size of % is ^, size of % is ^?", Enode);
4107 Error_Msg_Uint_1 := UI_Abs (Source_Siz - Target_Siz);
4109 if Is_Discrete_Type (Source)
4110 and then Is_Discrete_Type (Target)
4112 if Source_Siz > Target_Siz then
4114 ("\^ high order bits of source will be ignored?",
4117 elsif Is_Unsigned_Type (Source) then
4119 ("\source will be extended with ^ high order " &
4120 "zero bits?", Enode);
4124 ("\source will be extended with ^ high order " &
4129 elsif Source_Siz < Target_Siz then
4130 if Is_Discrete_Type (Target) then
4131 if Bytes_Big_Endian then
4133 ("\target value will include ^ undefined " &
4138 ("\target value will include ^ undefined " &
4145 ("\^ trailing bits of target value will be " &
4146 "undefined?", Enode);
4149 else pragma Assert (Source_Siz > Target_Siz);
4151 ("\^ trailing bits of source will be ignored?",
4158 -- If both types are access types, we need to check the alignment.
4159 -- If the alignment of both is specified, we can do it here.
4161 if Serious_Errors_Detected = 0
4162 and then Ekind (Source) in Access_Kind
4163 and then Ekind (Target) in Access_Kind
4164 and then Target_Strict_Alignment
4165 and then Present (Designated_Type (Source))
4166 and then Present (Designated_Type (Target))
4169 D_Source : constant Entity_Id := Designated_Type (Source);
4170 D_Target : constant Entity_Id := Designated_Type (Target);
4173 if Known_Alignment (D_Source)
4174 and then Known_Alignment (D_Target)
4177 Source_Align : constant Uint := Alignment (D_Source);
4178 Target_Align : constant Uint := Alignment (D_Target);
4181 if Source_Align < Target_Align
4182 and then not Is_Tagged_Type (D_Source)
4184 Error_Msg_Uint_1 := Target_Align;
4185 Error_Msg_Uint_2 := Source_Align;
4186 Error_Msg_Node_2 := D_Source;
4188 ("alignment of & (^) is stricter than " &
4189 "alignment of & (^)?", Enode, D_Target);
4191 if All_Errors_Mode then
4193 ("\resulting access value may have invalid " &
4194 "alignment?", Enode);
4203 end Validate_Unchecked_Conversions;