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 (RM 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
637 if Ignore_Rep_Clauses then
638 Rewrite (N, Make_Null_Statement (Sloc (N)));
645 if Rep_Item_Too_Early (Ent, N) then
649 -- Rep clause applies to full view of incomplete type or private type if
650 -- we have one (if not, this is a premature use of the type). However,
651 -- certain semantic checks need to be done on the specified entity (i.e.
652 -- the private view), so we save it in Ent.
654 if Is_Private_Type (Ent)
655 and then Is_Derived_Type (Ent)
656 and then not Is_Tagged_Type (Ent)
657 and then No (Full_View (Ent))
659 -- If this is a private type whose completion is a derivation from
660 -- another private type, there is no full view, and the attribute
661 -- belongs to the type itself, not its underlying parent.
665 elsif Ekind (Ent) = E_Incomplete_Type then
667 -- The attribute applies to the full view, set the entity of the
668 -- attribute definition accordingly.
670 Ent := Underlying_Type (Ent);
672 Set_Entity (Nam, Ent);
675 U_Ent := Underlying_Type (Ent);
678 -- Complete other routine error checks
680 if Etype (Nam) = Any_Type then
683 elsif Scope (Ent) /= Current_Scope then
684 Error_Msg_N ("entity must be declared in this scope", Nam);
687 elsif No (U_Ent) then
690 elsif Is_Type (U_Ent)
691 and then not Is_First_Subtype (U_Ent)
692 and then Id /= Attribute_Object_Size
693 and then Id /= Attribute_Value_Size
694 and then not From_At_Mod (N)
696 Error_Msg_N ("cannot specify attribute for subtype", Nam);
700 -- Switch on particular attribute
708 -- Address attribute definition clause
710 when Attribute_Address => Address : begin
711 Analyze_And_Resolve (Expr, RTE (RE_Address));
713 if Present (Address_Clause (U_Ent)) then
714 Error_Msg_N ("address already given for &", Nam);
716 -- Case of address clause for subprogram
718 elsif Is_Subprogram (U_Ent) then
719 if Has_Homonym (U_Ent) then
721 ("address clause cannot be given " &
722 "for overloaded subprogram",
726 -- For subprograms, all address clauses are permitted,
727 -- and we mark the subprogram as having a deferred freeze
728 -- so that Gigi will not elaborate it too soon.
730 -- Above needs more comments, what is too soon about???
732 Set_Has_Delayed_Freeze (U_Ent);
734 -- Case of address clause for entry
736 elsif Ekind (U_Ent) = E_Entry then
737 if Nkind (Parent (N)) = N_Task_Body then
739 ("entry address must be specified in task spec", Nam);
742 -- For entries, we require a constant address
744 Check_Constant_Address_Clause (Expr, U_Ent);
746 if Is_Task_Type (Scope (U_Ent))
747 and then Comes_From_Source (Scope (U_Ent))
750 ("?entry address declared for entry in task type", N);
752 ("\?only one task can be declared of this type", N);
755 Check_Restriction (No_Obsolescent_Features, N);
757 if Warn_On_Obsolescent_Feature then
759 ("attaching interrupt to task entry is an " &
760 "obsolescent feature (RM J.7.1)?", N);
762 ("\use interrupt procedure instead?", N);
765 -- Case of an address clause for a controlled object:
766 -- erroneous execution.
768 elsif Is_Controlled (Etype (U_Ent)) then
770 ("?controlled object& must not be overlaid", Nam, U_Ent);
772 ("\?Program_Error will be raised at run time", Nam);
773 Insert_Action (Declaration_Node (U_Ent),
774 Make_Raise_Program_Error (Loc,
775 Reason => PE_Overlaid_Controlled_Object));
777 -- Case of address clause for a (non-controlled) object
780 Ekind (U_Ent) = E_Variable
782 Ekind (U_Ent) = E_Constant
785 Expr : constant Node_Id := Expression (N);
786 Aent : constant Entity_Id := Address_Aliased_Entity (Expr);
789 -- Exported variables cannot have an address clause,
790 -- because this cancels the effect of the pragma Export
792 if Is_Exported (U_Ent) then
794 ("cannot export object with address clause", Nam);
796 -- Overlaying controlled objects is erroneous
799 and then Is_Controlled (Etype (Aent))
802 ("?controlled object must not be overlaid", Expr);
804 ("\?Program_Error will be raised at run time", Expr);
805 Insert_Action (Declaration_Node (U_Ent),
806 Make_Raise_Program_Error (Loc,
807 Reason => PE_Overlaid_Controlled_Object));
810 and then Ekind (U_Ent) = E_Constant
811 and then Ekind (Aent) /= E_Constant
813 Error_Msg_N ("constant overlays a variable?", Expr);
815 elsif Present (Renamed_Object (U_Ent)) then
817 ("address clause not allowed"
818 & " for a renaming declaration (RM 13.1(6))", Nam);
820 -- Imported variables can have an address clause, but then
821 -- the import is pretty meaningless except to suppress
822 -- initializations, so we do not need such variables to
823 -- be statically allocated (and in fact it causes trouble
824 -- if the address clause is a local value).
826 elsif Is_Imported (U_Ent) then
827 Set_Is_Statically_Allocated (U_Ent, False);
830 -- We mark a possible modification of a variable with an
831 -- address clause, since it is likely aliasing is occurring.
833 Note_Possible_Modification (Nam);
835 -- Here we are checking for explicit overlap of one
836 -- variable by another, and if we find this, then we
837 -- mark the overlapped variable as also being aliased.
839 -- First case is where we have an explicit
841 -- for J'Address use K'Address;
843 -- In this case, we mark K as volatile
845 Mark_Aliased_Address_As_Volatile (Expr);
847 -- Second case is where we have a constant whose
848 -- definition is of the form of an address as in:
850 -- A : constant Address := K'Address;
852 -- for B'Address use A;
854 -- In this case we also mark K as volatile
856 if Is_Entity_Name (Expr) then
858 Ent : constant Entity_Id := Entity (Expr);
859 Decl : constant Node_Id := Declaration_Node (Ent);
862 if Ekind (Ent) = E_Constant
863 and then Nkind (Decl) = N_Object_Declaration
864 and then Present (Expression (Decl))
866 Mark_Aliased_Address_As_Volatile
872 -- Legality checks on the address clause for initialized
873 -- objects is deferred until the freeze point, because
874 -- a subsequent pragma might indicate that the object is
875 -- imported and thus not initialized.
877 Set_Has_Delayed_Freeze (U_Ent);
879 if Is_Exported (U_Ent) then
881 ("& cannot be exported if an address clause is given",
884 ("\define and export a variable " &
885 "that holds its address instead",
889 -- Entity has delayed freeze, so we will generate an
890 -- alignment check at the freeze point unless suppressed.
892 if not Range_Checks_Suppressed (U_Ent)
893 and then not Alignment_Checks_Suppressed (U_Ent)
895 Set_Check_Address_Alignment (N);
898 -- Kill the size check code, since we are not allocating
899 -- the variable, it is somewhere else.
901 Kill_Size_Check_Code (U_Ent);
904 -- Not a valid entity for an address clause
907 Error_Msg_N ("address cannot be given for &", Nam);
915 -- Alignment attribute definition clause
917 when Attribute_Alignment => Alignment_Block : declare
918 Align : constant Uint := Get_Alignment_Value (Expr);
923 if not Is_Type (U_Ent)
924 and then Ekind (U_Ent) /= E_Variable
925 and then Ekind (U_Ent) /= E_Constant
927 Error_Msg_N ("alignment cannot be given for &", Nam);
929 elsif Has_Alignment_Clause (U_Ent) then
930 Error_Msg_Sloc := Sloc (Alignment_Clause (U_Ent));
931 Error_Msg_N ("alignment clause previously given#", N);
933 elsif Align /= No_Uint then
934 Set_Has_Alignment_Clause (U_Ent);
935 Set_Alignment (U_Ent, Align);
943 -- Bit_Order attribute definition clause
945 when Attribute_Bit_Order => Bit_Order : declare
947 if not Is_Record_Type (U_Ent) then
949 ("Bit_Order can only be defined for record type", Nam);
952 Analyze_And_Resolve (Expr, RTE (RE_Bit_Order));
954 if Etype (Expr) = Any_Type then
957 elsif not Is_Static_Expression (Expr) then
959 ("Bit_Order requires static expression!", Expr);
962 if (Expr_Value (Expr) = 0) /= Bytes_Big_Endian then
963 Set_Reverse_Bit_Order (U_Ent, True);
973 -- Component_Size attribute definition clause
975 when Attribute_Component_Size => Component_Size_Case : declare
976 Csize : constant Uint := Static_Integer (Expr);
979 New_Ctyp : Entity_Id;
983 if not Is_Array_Type (U_Ent) then
984 Error_Msg_N ("component size requires array type", Nam);
988 Btype := Base_Type (U_Ent);
990 if Has_Component_Size_Clause (Btype) then
992 ("component size clase for& previously given", Nam);
994 elsif Csize /= No_Uint then
995 Check_Size (Expr, Component_Type (Btype), Csize, Biased);
997 if Has_Aliased_Components (Btype)
1000 and then Csize /= 16
1003 ("component size incorrect for aliased components", N);
1007 -- For the biased case, build a declaration for a subtype
1008 -- that will be used to represent the biased subtype that
1009 -- reflects the biased representation of components. We need
1010 -- this subtype to get proper conversions on referencing
1011 -- elements of the array.
1015 Make_Defining_Identifier (Loc,
1016 Chars => New_External_Name (Chars (U_Ent), 'C', 0, 'T'));
1019 Make_Subtype_Declaration (Loc,
1020 Defining_Identifier => New_Ctyp,
1021 Subtype_Indication =>
1022 New_Occurrence_Of (Component_Type (Btype), Loc));
1024 Set_Parent (Decl, N);
1025 Analyze (Decl, Suppress => All_Checks);
1027 Set_Has_Delayed_Freeze (New_Ctyp, False);
1028 Set_Esize (New_Ctyp, Csize);
1029 Set_RM_Size (New_Ctyp, Csize);
1030 Init_Alignment (New_Ctyp);
1031 Set_Has_Biased_Representation (New_Ctyp, True);
1032 Set_Is_Itype (New_Ctyp, True);
1033 Set_Associated_Node_For_Itype (New_Ctyp, U_Ent);
1035 Set_Component_Type (Btype, New_Ctyp);
1038 Set_Component_Size (Btype, Csize);
1039 Set_Has_Component_Size_Clause (Btype, True);
1040 Set_Has_Non_Standard_Rep (Btype, True);
1042 end Component_Size_Case;
1048 when Attribute_External_Tag => External_Tag :
1050 if not Is_Tagged_Type (U_Ent) then
1051 Error_Msg_N ("should be a tagged type", Nam);
1054 Analyze_And_Resolve (Expr, Standard_String);
1056 if not Is_Static_Expression (Expr) then
1057 Flag_Non_Static_Expr
1058 ("static string required for tag name!", Nam);
1061 if VM_Target = No_VM then
1062 Set_Has_External_Tag_Rep_Clause (U_Ent);
1064 Error_Msg_Name_1 := Attr;
1066 ("% attribute unsupported in this configuration", Nam);
1069 if not Is_Library_Level_Entity (U_Ent) then
1071 ("?non-unique external tag supplied for &", N, U_Ent);
1073 ("?\same external tag applies to all subprogram calls", N);
1075 ("?\corresponding internal tag cannot be obtained", N);
1083 when Attribute_Input =>
1084 Analyze_Stream_TSS_Definition (TSS_Stream_Input);
1085 Set_Has_Specified_Stream_Input (Ent);
1091 -- Machine radix attribute definition clause
1093 when Attribute_Machine_Radix => Machine_Radix : declare
1094 Radix : constant Uint := Static_Integer (Expr);
1097 if not Is_Decimal_Fixed_Point_Type (U_Ent) then
1098 Error_Msg_N ("decimal fixed-point type expected for &", Nam);
1100 elsif Has_Machine_Radix_Clause (U_Ent) then
1101 Error_Msg_Sloc := Sloc (Alignment_Clause (U_Ent));
1102 Error_Msg_N ("machine radix clause previously given#", N);
1104 elsif Radix /= No_Uint then
1105 Set_Has_Machine_Radix_Clause (U_Ent);
1106 Set_Has_Non_Standard_Rep (Base_Type (U_Ent));
1110 elsif Radix = 10 then
1111 Set_Machine_Radix_10 (U_Ent);
1113 Error_Msg_N ("machine radix value must be 2 or 10", Expr);
1122 -- Object_Size attribute definition clause
1124 when Attribute_Object_Size => Object_Size : declare
1125 Size : constant Uint := Static_Integer (Expr);
1129 if not Is_Type (U_Ent) then
1130 Error_Msg_N ("Object_Size cannot be given for &", Nam);
1132 elsif Has_Object_Size_Clause (U_Ent) then
1133 Error_Msg_N ("Object_Size already given for &", Nam);
1136 Check_Size (Expr, U_Ent, Size, Biased);
1144 UI_Mod (Size, 64) /= 0
1147 ("Object_Size must be 8, 16, 32, or multiple of 64",
1151 Set_Esize (U_Ent, Size);
1152 Set_Has_Object_Size_Clause (U_Ent);
1153 Alignment_Check_For_Esize_Change (U_Ent);
1161 when Attribute_Output =>
1162 Analyze_Stream_TSS_Definition (TSS_Stream_Output);
1163 Set_Has_Specified_Stream_Output (Ent);
1169 when Attribute_Read =>
1170 Analyze_Stream_TSS_Definition (TSS_Stream_Read);
1171 Set_Has_Specified_Stream_Read (Ent);
1177 -- Size attribute definition clause
1179 when Attribute_Size => Size : declare
1180 Size : constant Uint := Static_Integer (Expr);
1187 if Has_Size_Clause (U_Ent) then
1188 Error_Msg_N ("size already given for &", Nam);
1190 elsif not Is_Type (U_Ent)
1191 and then Ekind (U_Ent) /= E_Variable
1192 and then Ekind (U_Ent) /= E_Constant
1194 Error_Msg_N ("size cannot be given for &", Nam);
1196 elsif Is_Array_Type (U_Ent)
1197 and then not Is_Constrained (U_Ent)
1200 ("size cannot be given for unconstrained array", Nam);
1202 elsif Size /= No_Uint then
1203 if Is_Type (U_Ent) then
1206 Etyp := Etype (U_Ent);
1209 -- Check size, note that Gigi is in charge of checking that the
1210 -- size of an array or record type is OK. Also we do not check
1211 -- the size in the ordinary fixed-point case, since it is too
1212 -- early to do so (there may be subsequent small clause that
1213 -- affects the size). We can check the size if a small clause
1214 -- has already been given.
1216 if not Is_Ordinary_Fixed_Point_Type (U_Ent)
1217 or else Has_Small_Clause (U_Ent)
1219 Check_Size (Expr, Etyp, Size, Biased);
1220 Set_Has_Biased_Representation (U_Ent, Biased);
1223 -- For types set RM_Size and Esize if possible
1225 if Is_Type (U_Ent) then
1226 Set_RM_Size (U_Ent, Size);
1228 -- For scalar types, increase Object_Size to power of 2, but
1229 -- not less than a storage unit in any case (i.e., normally
1230 -- this means it will be byte addressable).
1232 if Is_Scalar_Type (U_Ent) then
1233 if Size <= System_Storage_Unit then
1234 Init_Esize (U_Ent, System_Storage_Unit);
1235 elsif Size <= 16 then
1236 Init_Esize (U_Ent, 16);
1237 elsif Size <= 32 then
1238 Init_Esize (U_Ent, 32);
1240 Set_Esize (U_Ent, (Size + 63) / 64 * 64);
1243 -- For all other types, object size = value size. The
1244 -- backend will adjust as needed.
1247 Set_Esize (U_Ent, Size);
1250 Alignment_Check_For_Esize_Change (U_Ent);
1252 -- For objects, set Esize only
1255 if Is_Elementary_Type (Etyp) then
1256 if Size /= System_Storage_Unit
1258 Size /= System_Storage_Unit * 2
1260 Size /= System_Storage_Unit * 4
1262 Size /= System_Storage_Unit * 8
1264 Error_Msg_Uint_1 := UI_From_Int (System_Storage_Unit);
1265 Error_Msg_Uint_2 := Error_Msg_Uint_1 * 8;
1267 ("size for primitive object must be a power of 2"
1268 & " in the range ^-^", N);
1272 Set_Esize (U_Ent, Size);
1275 Set_Has_Size_Clause (U_Ent);
1283 -- Small attribute definition clause
1285 when Attribute_Small => Small : declare
1286 Implicit_Base : constant Entity_Id := Base_Type (U_Ent);
1290 Analyze_And_Resolve (Expr, Any_Real);
1292 if Etype (Expr) = Any_Type then
1295 elsif not Is_Static_Expression (Expr) then
1296 Flag_Non_Static_Expr
1297 ("small requires static expression!", Expr);
1301 Small := Expr_Value_R (Expr);
1303 if Small <= Ureal_0 then
1304 Error_Msg_N ("small value must be greater than zero", Expr);
1310 if not Is_Ordinary_Fixed_Point_Type (U_Ent) then
1312 ("small requires an ordinary fixed point type", Nam);
1314 elsif Has_Small_Clause (U_Ent) then
1315 Error_Msg_N ("small already given for &", Nam);
1317 elsif Small > Delta_Value (U_Ent) then
1319 ("small value must not be greater then delta value", Nam);
1322 Set_Small_Value (U_Ent, Small);
1323 Set_Small_Value (Implicit_Base, Small);
1324 Set_Has_Small_Clause (U_Ent);
1325 Set_Has_Small_Clause (Implicit_Base);
1326 Set_Has_Non_Standard_Rep (Implicit_Base);
1334 -- Storage_Pool attribute definition clause
1336 when Attribute_Storage_Pool => Storage_Pool : declare
1341 if Ekind (U_Ent) = E_Access_Subprogram_Type then
1343 ("storage pool cannot be given for access-to-subprogram type",
1347 elsif Ekind (U_Ent) /= E_Access_Type
1348 and then Ekind (U_Ent) /= E_General_Access_Type
1351 ("storage pool can only be given for access types", Nam);
1354 elsif Is_Derived_Type (U_Ent) then
1356 ("storage pool cannot be given for a derived access type",
1359 elsif Has_Storage_Size_Clause (U_Ent) then
1360 Error_Msg_N ("storage size already given for &", Nam);
1363 elsif Present (Associated_Storage_Pool (U_Ent)) then
1364 Error_Msg_N ("storage pool already given for &", Nam);
1369 (Expr, Class_Wide_Type (RTE (RE_Root_Storage_Pool)));
1371 if Nkind (Expr) = N_Type_Conversion then
1372 T := Etype (Expression (Expr));
1377 -- The Stack_Bounded_Pool is used internally for implementing
1378 -- access types with a Storage_Size. Since it only work
1379 -- properly when used on one specific type, we need to check
1380 -- that it is not highjacked improperly:
1381 -- type T is access Integer;
1382 -- for T'Storage_Size use n;
1383 -- type Q is access Float;
1384 -- for Q'Storage_Size use T'Storage_Size; -- incorrect
1386 if RTE_Available (RE_Stack_Bounded_Pool)
1387 and then Base_Type (T) = RTE (RE_Stack_Bounded_Pool)
1389 Error_Msg_N ("non-shareable internal Pool", Expr);
1393 -- If the argument is a name that is not an entity name, then
1394 -- we construct a renaming operation to define an entity of
1395 -- type storage pool.
1397 if not Is_Entity_Name (Expr)
1398 and then Is_Object_Reference (Expr)
1401 Make_Defining_Identifier (Loc,
1402 Chars => New_Internal_Name ('P'));
1405 Rnode : constant Node_Id :=
1406 Make_Object_Renaming_Declaration (Loc,
1407 Defining_Identifier => Pool,
1409 New_Occurrence_Of (Etype (Expr), Loc),
1413 Insert_Before (N, Rnode);
1415 Set_Associated_Storage_Pool (U_Ent, Pool);
1418 elsif Is_Entity_Name (Expr) then
1419 Pool := Entity (Expr);
1421 -- If pool is a renamed object, get original one. This can
1422 -- happen with an explicit renaming, and within instances.
1424 while Present (Renamed_Object (Pool))
1425 and then Is_Entity_Name (Renamed_Object (Pool))
1427 Pool := Entity (Renamed_Object (Pool));
1430 if Present (Renamed_Object (Pool))
1431 and then Nkind (Renamed_Object (Pool)) = N_Type_Conversion
1432 and then Is_Entity_Name (Expression (Renamed_Object (Pool)))
1434 Pool := Entity (Expression (Renamed_Object (Pool)));
1437 Set_Associated_Storage_Pool (U_Ent, Pool);
1439 elsif Nkind (Expr) = N_Type_Conversion
1440 and then Is_Entity_Name (Expression (Expr))
1441 and then Nkind (Original_Node (Expr)) = N_Attribute_Reference
1443 Pool := Entity (Expression (Expr));
1444 Set_Associated_Storage_Pool (U_Ent, Pool);
1447 Error_Msg_N ("incorrect reference to a Storage Pool", Expr);
1456 -- Storage_Size attribute definition clause
1458 when Attribute_Storage_Size => Storage_Size : declare
1459 Btype : constant Entity_Id := Base_Type (U_Ent);
1463 if Is_Task_Type (U_Ent) then
1464 Check_Restriction (No_Obsolescent_Features, N);
1466 if Warn_On_Obsolescent_Feature then
1468 ("storage size clause for task is an " &
1469 "obsolescent feature (RM J.9)?", N);
1471 ("\use Storage_Size pragma instead?", N);
1477 if not Is_Access_Type (U_Ent)
1478 and then Ekind (U_Ent) /= E_Task_Type
1480 Error_Msg_N ("storage size cannot be given for &", Nam);
1482 elsif Is_Access_Type (U_Ent) and Is_Derived_Type (U_Ent) then
1484 ("storage size cannot be given for a derived access type",
1487 elsif Has_Storage_Size_Clause (Btype) then
1488 Error_Msg_N ("storage size already given for &", Nam);
1491 Analyze_And_Resolve (Expr, Any_Integer);
1493 if Is_Access_Type (U_Ent) then
1494 if Present (Associated_Storage_Pool (U_Ent)) then
1495 Error_Msg_N ("storage pool already given for &", Nam);
1499 if Compile_Time_Known_Value (Expr)
1500 and then Expr_Value (Expr) = 0
1502 Set_No_Pool_Assigned (Btype);
1505 else -- Is_Task_Type (U_Ent)
1506 Sprag := Get_Rep_Pragma (Btype, Name_Storage_Size);
1508 if Present (Sprag) then
1509 Error_Msg_Sloc := Sloc (Sprag);
1511 ("Storage_Size already specified#", Nam);
1516 Set_Has_Storage_Size_Clause (Btype);
1524 when Attribute_Stream_Size => Stream_Size : declare
1525 Size : constant Uint := Static_Integer (Expr);
1528 if Ada_Version <= Ada_95 then
1529 Check_Restriction (No_Implementation_Attributes, N);
1532 if Has_Stream_Size_Clause (U_Ent) then
1533 Error_Msg_N ("Stream_Size already given for &", Nam);
1535 elsif Is_Elementary_Type (U_Ent) then
1536 if Size /= System_Storage_Unit
1538 Size /= System_Storage_Unit * 2
1540 Size /= System_Storage_Unit * 4
1542 Size /= System_Storage_Unit * 8
1544 Error_Msg_Uint_1 := UI_From_Int (System_Storage_Unit);
1546 ("stream size for elementary type must be a"
1547 & " power of 2 and at least ^", N);
1549 elsif RM_Size (U_Ent) > Size then
1550 Error_Msg_Uint_1 := RM_Size (U_Ent);
1552 ("stream size for elementary type must be a"
1553 & " power of 2 and at least ^", N);
1556 Set_Has_Stream_Size_Clause (U_Ent);
1559 Error_Msg_N ("Stream_Size cannot be given for &", Nam);
1567 -- Value_Size attribute definition clause
1569 when Attribute_Value_Size => Value_Size : declare
1570 Size : constant Uint := Static_Integer (Expr);
1574 if not Is_Type (U_Ent) then
1575 Error_Msg_N ("Value_Size cannot be given for &", Nam);
1578 (Get_Attribute_Definition_Clause
1579 (U_Ent, Attribute_Value_Size))
1581 Error_Msg_N ("Value_Size already given for &", Nam);
1583 elsif Is_Array_Type (U_Ent)
1584 and then not Is_Constrained (U_Ent)
1587 ("Value_Size cannot be given for unconstrained array", Nam);
1590 if Is_Elementary_Type (U_Ent) then
1591 Check_Size (Expr, U_Ent, Size, Biased);
1592 Set_Has_Biased_Representation (U_Ent, Biased);
1595 Set_RM_Size (U_Ent, Size);
1603 when Attribute_Write =>
1604 Analyze_Stream_TSS_Definition (TSS_Stream_Write);
1605 Set_Has_Specified_Stream_Write (Ent);
1607 -- All other attributes cannot be set
1611 ("attribute& cannot be set with definition clause", N);
1614 -- The test for the type being frozen must be performed after
1615 -- any expression the clause has been analyzed since the expression
1616 -- itself might cause freezing that makes the clause illegal.
1618 if Rep_Item_Too_Late (U_Ent, N, FOnly) then
1621 end Analyze_Attribute_Definition_Clause;
1623 ----------------------------
1624 -- Analyze_Code_Statement --
1625 ----------------------------
1627 procedure Analyze_Code_Statement (N : Node_Id) is
1628 HSS : constant Node_Id := Parent (N);
1629 SBody : constant Node_Id := Parent (HSS);
1630 Subp : constant Entity_Id := Current_Scope;
1637 -- Analyze and check we get right type, note that this implements the
1638 -- requirement (RM 13.8(1)) that Machine_Code be with'ed, since that
1639 -- is the only way that Asm_Insn could possibly be visible.
1641 Analyze_And_Resolve (Expression (N));
1643 if Etype (Expression (N)) = Any_Type then
1645 elsif Etype (Expression (N)) /= RTE (RE_Asm_Insn) then
1646 Error_Msg_N ("incorrect type for code statement", N);
1650 Check_Code_Statement (N);
1652 -- Make sure we appear in the handled statement sequence of a
1653 -- subprogram (RM 13.8(3)).
1655 if Nkind (HSS) /= N_Handled_Sequence_Of_Statements
1656 or else Nkind (SBody) /= N_Subprogram_Body
1659 ("code statement can only appear in body of subprogram", N);
1663 -- Do remaining checks (RM 13.8(3)) if not already done
1665 if not Is_Machine_Code_Subprogram (Subp) then
1666 Set_Is_Machine_Code_Subprogram (Subp);
1668 -- No exception handlers allowed
1670 if Present (Exception_Handlers (HSS)) then
1672 ("exception handlers not permitted in machine code subprogram",
1673 First (Exception_Handlers (HSS)));
1676 -- No declarations other than use clauses and pragmas (we allow
1677 -- certain internally generated declarations as well).
1679 Decl := First (Declarations (SBody));
1680 while Present (Decl) loop
1681 DeclO := Original_Node (Decl);
1682 if Comes_From_Source (DeclO)
1683 and then Nkind (DeclO) /= N_Pragma
1684 and then Nkind (DeclO) /= N_Use_Package_Clause
1685 and then Nkind (DeclO) /= N_Use_Type_Clause
1686 and then Nkind (DeclO) /= N_Implicit_Label_Declaration
1689 ("this declaration not allowed in machine code subprogram",
1696 -- No statements other than code statements, pragmas, and labels.
1697 -- Again we allow certain internally generated statements.
1699 Stmt := First (Statements (HSS));
1700 while Present (Stmt) loop
1701 StmtO := Original_Node (Stmt);
1702 if Comes_From_Source (StmtO)
1703 and then Nkind (StmtO) /= N_Pragma
1704 and then Nkind (StmtO) /= N_Label
1705 and then Nkind (StmtO) /= N_Code_Statement
1708 ("this statement is not allowed in machine code subprogram",
1715 end Analyze_Code_Statement;
1717 -----------------------------------------------
1718 -- Analyze_Enumeration_Representation_Clause --
1719 -----------------------------------------------
1721 procedure Analyze_Enumeration_Representation_Clause (N : Node_Id) is
1722 Ident : constant Node_Id := Identifier (N);
1723 Aggr : constant Node_Id := Array_Aggregate (N);
1724 Enumtype : Entity_Id;
1730 Err : Boolean := False;
1732 Lo : constant Uint := Expr_Value (Type_Low_Bound (Universal_Integer));
1733 Hi : constant Uint := Expr_Value (Type_High_Bound (Universal_Integer));
1738 if Ignore_Rep_Clauses then
1742 -- First some basic error checks
1745 Enumtype := Entity (Ident);
1747 if Enumtype = Any_Type
1748 or else Rep_Item_Too_Early (Enumtype, N)
1752 Enumtype := Underlying_Type (Enumtype);
1755 if not Is_Enumeration_Type (Enumtype) then
1757 ("enumeration type required, found}",
1758 Ident, First_Subtype (Enumtype));
1762 -- Ignore rep clause on generic actual type. This will already have
1763 -- been flagged on the template as an error, and this is the safest
1764 -- way to ensure we don't get a junk cascaded message in the instance.
1766 if Is_Generic_Actual_Type (Enumtype) then
1769 -- Type must be in current scope
1771 elsif Scope (Enumtype) /= Current_Scope then
1772 Error_Msg_N ("type must be declared in this scope", Ident);
1775 -- Type must be a first subtype
1777 elsif not Is_First_Subtype (Enumtype) then
1778 Error_Msg_N ("cannot give enumeration rep clause for subtype", N);
1781 -- Ignore duplicate rep clause
1783 elsif Has_Enumeration_Rep_Clause (Enumtype) then
1784 Error_Msg_N ("duplicate enumeration rep clause ignored", N);
1787 -- Don't allow rep clause for standard [wide_[wide_]]character
1789 elsif Root_Type (Enumtype) = Standard_Character
1790 or else Root_Type (Enumtype) = Standard_Wide_Character
1791 or else Root_Type (Enumtype) = Standard_Wide_Wide_Character
1793 Error_Msg_N ("enumeration rep clause not allowed for this type", N);
1796 -- Check that the expression is a proper aggregate (no parentheses)
1798 elsif Paren_Count (Aggr) /= 0 then
1800 ("extra parentheses surrounding aggregate not allowed",
1804 -- All tests passed, so set rep clause in place
1807 Set_Has_Enumeration_Rep_Clause (Enumtype);
1808 Set_Has_Enumeration_Rep_Clause (Base_Type (Enumtype));
1811 -- Now we process the aggregate. Note that we don't use the normal
1812 -- aggregate code for this purpose, because we don't want any of the
1813 -- normal expansion activities, and a number of special semantic
1814 -- rules apply (including the component type being any integer type)
1816 Elit := First_Literal (Enumtype);
1818 -- First the positional entries if any
1820 if Present (Expressions (Aggr)) then
1821 Expr := First (Expressions (Aggr));
1822 while Present (Expr) loop
1824 Error_Msg_N ("too many entries in aggregate", Expr);
1828 Val := Static_Integer (Expr);
1830 -- Err signals that we found some incorrect entries processing
1831 -- the list. The final checks for completeness and ordering are
1832 -- skipped in this case.
1834 if Val = No_Uint then
1836 elsif Val < Lo or else Hi < Val then
1837 Error_Msg_N ("value outside permitted range", Expr);
1841 Set_Enumeration_Rep (Elit, Val);
1842 Set_Enumeration_Rep_Expr (Elit, Expr);
1848 -- Now process the named entries if present
1850 if Present (Component_Associations (Aggr)) then
1851 Assoc := First (Component_Associations (Aggr));
1852 while Present (Assoc) loop
1853 Choice := First (Choices (Assoc));
1855 if Present (Next (Choice)) then
1857 ("multiple choice not allowed here", Next (Choice));
1861 if Nkind (Choice) = N_Others_Choice then
1862 Error_Msg_N ("others choice not allowed here", Choice);
1865 elsif Nkind (Choice) = N_Range then
1866 -- ??? should allow zero/one element range here
1867 Error_Msg_N ("range not allowed here", Choice);
1871 Analyze_And_Resolve (Choice, Enumtype);
1873 if Is_Entity_Name (Choice)
1874 and then Is_Type (Entity (Choice))
1876 Error_Msg_N ("subtype name not allowed here", Choice);
1878 -- ??? should allow static subtype with zero/one entry
1880 elsif Etype (Choice) = Base_Type (Enumtype) then
1881 if not Is_Static_Expression (Choice) then
1882 Flag_Non_Static_Expr
1883 ("non-static expression used for choice!", Choice);
1887 Elit := Expr_Value_E (Choice);
1889 if Present (Enumeration_Rep_Expr (Elit)) then
1890 Error_Msg_Sloc := Sloc (Enumeration_Rep_Expr (Elit));
1892 ("representation for& previously given#",
1897 Set_Enumeration_Rep_Expr (Elit, Choice);
1899 Expr := Expression (Assoc);
1900 Val := Static_Integer (Expr);
1902 if Val = No_Uint then
1905 elsif Val < Lo or else Hi < Val then
1906 Error_Msg_N ("value outside permitted range", Expr);
1910 Set_Enumeration_Rep (Elit, Val);
1919 -- Aggregate is fully processed. Now we check that a full set of
1920 -- representations was given, and that they are in range and in order.
1921 -- These checks are only done if no other errors occurred.
1927 Elit := First_Literal (Enumtype);
1928 while Present (Elit) loop
1929 if No (Enumeration_Rep_Expr (Elit)) then
1930 Error_Msg_NE ("missing representation for&!", N, Elit);
1933 Val := Enumeration_Rep (Elit);
1935 if Min = No_Uint then
1939 if Val /= No_Uint then
1940 if Max /= No_Uint and then Val <= Max then
1942 ("enumeration value for& not ordered!",
1943 Enumeration_Rep_Expr (Elit), Elit);
1949 -- If there is at least one literal whose representation
1950 -- is not equal to the Pos value, then note that this
1951 -- enumeration type has a non-standard representation.
1953 if Val /= Enumeration_Pos (Elit) then
1954 Set_Has_Non_Standard_Rep (Base_Type (Enumtype));
1961 -- Now set proper size information
1964 Minsize : Uint := UI_From_Int (Minimum_Size (Enumtype));
1967 if Has_Size_Clause (Enumtype) then
1968 if Esize (Enumtype) >= Minsize then
1973 UI_From_Int (Minimum_Size (Enumtype, Biased => True));
1975 if Esize (Enumtype) < Minsize then
1976 Error_Msg_N ("previously given size is too small", N);
1979 Set_Has_Biased_Representation (Enumtype);
1984 Set_RM_Size (Enumtype, Minsize);
1985 Set_Enum_Esize (Enumtype);
1988 Set_RM_Size (Base_Type (Enumtype), RM_Size (Enumtype));
1989 Set_Esize (Base_Type (Enumtype), Esize (Enumtype));
1990 Set_Alignment (Base_Type (Enumtype), Alignment (Enumtype));
1994 -- We repeat the too late test in case it froze itself!
1996 if Rep_Item_Too_Late (Enumtype, N) then
1999 end Analyze_Enumeration_Representation_Clause;
2001 ----------------------------
2002 -- Analyze_Free_Statement --
2003 ----------------------------
2005 procedure Analyze_Free_Statement (N : Node_Id) is
2007 Analyze (Expression (N));
2008 end Analyze_Free_Statement;
2010 ------------------------------------------
2011 -- Analyze_Record_Representation_Clause --
2012 ------------------------------------------
2014 procedure Analyze_Record_Representation_Clause (N : Node_Id) is
2015 Loc : constant Source_Ptr := Sloc (N);
2016 Ident : constant Node_Id := Identifier (N);
2017 Rectype : Entity_Id;
2023 Hbit : Uint := Uint_0;
2028 Max_Bit_So_Far : Uint;
2029 -- Records the maximum bit position so far. If all field positions
2030 -- are monotonically increasing, then we can skip the circuit for
2031 -- checking for overlap, since no overlap is possible.
2033 Overlap_Check_Required : Boolean;
2034 -- Used to keep track of whether or not an overlap check is required
2036 Ccount : Natural := 0;
2037 -- Number of component clauses in record rep clause
2039 CR_Pragma : Node_Id := Empty;
2040 -- Points to N_Pragma node if Complete_Representation pragma present
2043 if Ignore_Rep_Clauses then
2048 Rectype := Entity (Ident);
2050 if Rectype = Any_Type
2051 or else Rep_Item_Too_Early (Rectype, N)
2055 Rectype := Underlying_Type (Rectype);
2058 -- First some basic error checks
2060 if not Is_Record_Type (Rectype) then
2062 ("record type required, found}", Ident, First_Subtype (Rectype));
2065 elsif Is_Unchecked_Union (Rectype) then
2067 ("record rep clause not allowed for Unchecked_Union", N);
2069 elsif Scope (Rectype) /= Current_Scope then
2070 Error_Msg_N ("type must be declared in this scope", N);
2073 elsif not Is_First_Subtype (Rectype) then
2074 Error_Msg_N ("cannot give record rep clause for subtype", N);
2077 elsif Has_Record_Rep_Clause (Rectype) then
2078 Error_Msg_N ("duplicate record rep clause ignored", N);
2081 elsif Rep_Item_Too_Late (Rectype, N) then
2085 if Present (Mod_Clause (N)) then
2087 Loc : constant Source_Ptr := Sloc (N);
2088 M : constant Node_Id := Mod_Clause (N);
2089 P : constant List_Id := Pragmas_Before (M);
2093 pragma Warnings (Off, Mod_Val);
2096 Check_Restriction (No_Obsolescent_Features, Mod_Clause (N));
2098 if Warn_On_Obsolescent_Feature then
2100 ("mod clause is an obsolescent feature (RM J.8)?", N);
2102 ("\use alignment attribute definition clause instead?", N);
2109 -- In ASIS_Mode mode, expansion is disabled, but we must convert
2110 -- the Mod clause into an alignment clause anyway, so that the
2111 -- back-end can compute and back-annotate properly the size and
2112 -- alignment of types that may include this record.
2114 -- This seems dubious, this destroys the source tree in a manner
2115 -- not detectable by ASIS ???
2117 if Operating_Mode = Check_Semantics
2121 Make_Attribute_Definition_Clause (Loc,
2122 Name => New_Reference_To (Base_Type (Rectype), Loc),
2123 Chars => Name_Alignment,
2124 Expression => Relocate_Node (Expression (M)));
2126 Set_From_At_Mod (AtM_Nod);
2127 Insert_After (N, AtM_Nod);
2128 Mod_Val := Get_Alignment_Value (Expression (AtM_Nod));
2129 Set_Mod_Clause (N, Empty);
2132 -- Get the alignment value to perform error checking
2134 Mod_Val := Get_Alignment_Value (Expression (M));
2140 -- Clear any existing component clauses for the type (this happens with
2141 -- derived types, where we are now overriding the original)
2143 Comp := First_Component_Or_Discriminant (Rectype);
2144 while Present (Comp) loop
2145 Set_Component_Clause (Comp, Empty);
2146 Next_Component_Or_Discriminant (Comp);
2149 -- All done if no component clauses
2151 CC := First (Component_Clauses (N));
2157 -- If a tag is present, then create a component clause that places it
2158 -- at the start of the record (otherwise gigi may place it after other
2159 -- fields that have rep clauses).
2161 Fent := First_Entity (Rectype);
2163 if Nkind (Fent) = N_Defining_Identifier
2164 and then Chars (Fent) = Name_uTag
2166 Set_Component_Bit_Offset (Fent, Uint_0);
2167 Set_Normalized_Position (Fent, Uint_0);
2168 Set_Normalized_First_Bit (Fent, Uint_0);
2169 Set_Normalized_Position_Max (Fent, Uint_0);
2170 Init_Esize (Fent, System_Address_Size);
2172 Set_Component_Clause (Fent,
2173 Make_Component_Clause (Loc,
2175 Make_Identifier (Loc,
2176 Chars => Name_uTag),
2179 Make_Integer_Literal (Loc,
2183 Make_Integer_Literal (Loc,
2187 Make_Integer_Literal (Loc,
2188 UI_From_Int (System_Address_Size))));
2190 Ccount := Ccount + 1;
2193 -- A representation like this applies to the base type
2195 Set_Has_Record_Rep_Clause (Base_Type (Rectype));
2196 Set_Has_Non_Standard_Rep (Base_Type (Rectype));
2197 Set_Has_Specified_Layout (Base_Type (Rectype));
2199 Max_Bit_So_Far := Uint_Minus_1;
2200 Overlap_Check_Required := False;
2202 -- Process the component clauses
2204 while Present (CC) loop
2208 if Nkind (CC) = N_Pragma then
2211 -- The only pragma of interest is Complete_Representation
2213 if Chars (CC) = Name_Complete_Representation then
2217 -- Processing for real component clause
2220 Ccount := Ccount + 1;
2221 Posit := Static_Integer (Position (CC));
2222 Fbit := Static_Integer (First_Bit (CC));
2223 Lbit := Static_Integer (Last_Bit (CC));
2226 and then Fbit /= No_Uint
2227 and then Lbit /= No_Uint
2231 ("position cannot be negative", Position (CC));
2235 ("first bit cannot be negative", First_Bit (CC));
2237 -- Values look OK, so find the corresponding record component
2238 -- Even though the syntax allows an attribute reference for
2239 -- implementation-defined components, GNAT does not allow the
2240 -- tag to get an explicit position.
2242 elsif Nkind (Component_Name (CC)) = N_Attribute_Reference then
2243 if Attribute_Name (Component_Name (CC)) = Name_Tag then
2244 Error_Msg_N ("position of tag cannot be specified", CC);
2246 Error_Msg_N ("illegal component name", CC);
2250 Comp := First_Entity (Rectype);
2251 while Present (Comp) loop
2252 exit when Chars (Comp) = Chars (Component_Name (CC));
2258 -- Maybe component of base type that is absent from
2259 -- statically constrained first subtype.
2261 Comp := First_Entity (Base_Type (Rectype));
2262 while Present (Comp) loop
2263 exit when Chars (Comp) = Chars (Component_Name (CC));
2270 ("component clause is for non-existent field", CC);
2272 elsif Present (Component_Clause (Comp)) then
2273 Error_Msg_Sloc := Sloc (Component_Clause (Comp));
2275 ("component clause previously given#", CC);
2278 -- Update Fbit and Lbit to the actual bit number
2280 Fbit := Fbit + UI_From_Int (SSU) * Posit;
2281 Lbit := Lbit + UI_From_Int (SSU) * Posit;
2283 if Fbit <= Max_Bit_So_Far then
2284 Overlap_Check_Required := True;
2286 Max_Bit_So_Far := Lbit;
2289 if Has_Size_Clause (Rectype)
2290 and then Esize (Rectype) <= Lbit
2293 ("bit number out of range of specified size",
2296 Set_Component_Clause (Comp, CC);
2297 Set_Component_Bit_Offset (Comp, Fbit);
2298 Set_Esize (Comp, 1 + (Lbit - Fbit));
2299 Set_Normalized_First_Bit (Comp, Fbit mod SSU);
2300 Set_Normalized_Position (Comp, Fbit / SSU);
2302 Set_Normalized_Position_Max
2303 (Fent, Normalized_Position (Fent));
2305 if Is_Tagged_Type (Rectype)
2306 and then Fbit < System_Address_Size
2309 ("component overlaps tag field of&",
2313 -- This information is also set in the corresponding
2314 -- component of the base type, found by accessing the
2315 -- Original_Record_Component link if it is present.
2317 Ocomp := Original_Record_Component (Comp);
2324 (Component_Name (CC),
2329 Set_Has_Biased_Representation (Comp, Biased);
2331 if Present (Ocomp) then
2332 Set_Component_Clause (Ocomp, CC);
2333 Set_Component_Bit_Offset (Ocomp, Fbit);
2334 Set_Normalized_First_Bit (Ocomp, Fbit mod SSU);
2335 Set_Normalized_Position (Ocomp, Fbit / SSU);
2336 Set_Esize (Ocomp, 1 + (Lbit - Fbit));
2338 Set_Normalized_Position_Max
2339 (Ocomp, Normalized_Position (Ocomp));
2341 Set_Has_Biased_Representation
2342 (Ocomp, Has_Biased_Representation (Comp));
2345 if Esize (Comp) < 0 then
2346 Error_Msg_N ("component size is negative", CC);
2357 -- Now that we have processed all the component clauses, check for
2358 -- overlap. We have to leave this till last, since the components
2359 -- can appear in any arbitrary order in the representation clause.
2361 -- We do not need this check if all specified ranges were monotonic,
2362 -- as recorded by Overlap_Check_Required being False at this stage.
2364 -- This first section checks if there are any overlapping entries
2365 -- at all. It does this by sorting all entries and then seeing if
2366 -- there are any overlaps. If there are none, then that is decisive,
2367 -- but if there are overlaps, they may still be OK (they may result
2368 -- from fields in different variants).
2370 if Overlap_Check_Required then
2371 Overlap_Check1 : declare
2373 OC_Fbit : array (0 .. Ccount) of Uint;
2374 -- First-bit values for component clauses, the value is the
2375 -- offset of the first bit of the field from start of record.
2376 -- The zero entry is for use in sorting.
2378 OC_Lbit : array (0 .. Ccount) of Uint;
2379 -- Last-bit values for component clauses, the value is the
2380 -- offset of the last bit of the field from start of record.
2381 -- The zero entry is for use in sorting.
2383 OC_Count : Natural := 0;
2384 -- Count of entries in OC_Fbit and OC_Lbit
2386 function OC_Lt (Op1, Op2 : Natural) return Boolean;
2387 -- Compare routine for Sort (See GNAT.Heap_Sort_A)
2389 procedure OC_Move (From : Natural; To : Natural);
2390 -- Move routine for Sort (see GNAT.Heap_Sort_A)
2392 function OC_Lt (Op1, Op2 : Natural) return Boolean is
2394 return OC_Fbit (Op1) < OC_Fbit (Op2);
2397 procedure OC_Move (From : Natural; To : Natural) is
2399 OC_Fbit (To) := OC_Fbit (From);
2400 OC_Lbit (To) := OC_Lbit (From);
2404 CC := First (Component_Clauses (N));
2405 while Present (CC) loop
2406 if Nkind (CC) /= N_Pragma then
2407 Posit := Static_Integer (Position (CC));
2408 Fbit := Static_Integer (First_Bit (CC));
2409 Lbit := Static_Integer (Last_Bit (CC));
2412 and then Fbit /= No_Uint
2413 and then Lbit /= No_Uint
2415 OC_Count := OC_Count + 1;
2416 Posit := Posit * SSU;
2417 OC_Fbit (OC_Count) := Fbit + Posit;
2418 OC_Lbit (OC_Count) := Lbit + Posit;
2427 OC_Move'Unrestricted_Access,
2428 OC_Lt'Unrestricted_Access);
2430 Overlap_Check_Required := False;
2431 for J in 1 .. OC_Count - 1 loop
2432 if OC_Lbit (J) >= OC_Fbit (J + 1) then
2433 Overlap_Check_Required := True;
2440 -- If Overlap_Check_Required is still True, then we have to do
2441 -- the full scale overlap check, since we have at least two fields
2442 -- that do overlap, and we need to know if that is OK since they
2443 -- are in the same variant, or whether we have a definite problem
2445 if Overlap_Check_Required then
2446 Overlap_Check2 : declare
2447 C1_Ent, C2_Ent : Entity_Id;
2448 -- Entities of components being checked for overlap
2451 -- Component_List node whose Component_Items are being checked
2454 -- Component declaration for component being checked
2457 C1_Ent := First_Entity (Base_Type (Rectype));
2459 -- Loop through all components in record. For each component check
2460 -- for overlap with any of the preceding elements on the component
2461 -- list containing the component, and also, if the component is in
2462 -- a variant, check against components outside the case structure.
2463 -- This latter test is repeated recursively up the variant tree.
2465 Main_Component_Loop : while Present (C1_Ent) loop
2466 if Ekind (C1_Ent) /= E_Component
2467 and then Ekind (C1_Ent) /= E_Discriminant
2469 goto Continue_Main_Component_Loop;
2472 -- Skip overlap check if entity has no declaration node. This
2473 -- happens with discriminants in constrained derived types.
2474 -- Probably we are missing some checks as a result, but that
2475 -- does not seem terribly serious ???
2477 if No (Declaration_Node (C1_Ent)) then
2478 goto Continue_Main_Component_Loop;
2481 Clist := Parent (List_Containing (Declaration_Node (C1_Ent)));
2483 -- Loop through component lists that need checking. Check the
2484 -- current component list and all lists in variants above us.
2486 Component_List_Loop : loop
2488 -- If derived type definition, go to full declaration
2489 -- If at outer level, check discriminants if there are any
2491 if Nkind (Clist) = N_Derived_Type_Definition then
2492 Clist := Parent (Clist);
2495 -- Outer level of record definition, check discriminants
2497 if Nkind (Clist) = N_Full_Type_Declaration
2498 or else Nkind (Clist) = N_Private_Type_Declaration
2500 if Has_Discriminants (Defining_Identifier (Clist)) then
2502 First_Discriminant (Defining_Identifier (Clist));
2504 while Present (C2_Ent) loop
2505 exit when C1_Ent = C2_Ent;
2506 Check_Component_Overlap (C1_Ent, C2_Ent);
2507 Next_Discriminant (C2_Ent);
2511 -- Record extension case
2513 elsif Nkind (Clist) = N_Derived_Type_Definition then
2516 -- Otherwise check one component list
2519 Citem := First (Component_Items (Clist));
2521 while Present (Citem) loop
2522 if Nkind (Citem) = N_Component_Declaration then
2523 C2_Ent := Defining_Identifier (Citem);
2524 exit when C1_Ent = C2_Ent;
2525 Check_Component_Overlap (C1_Ent, C2_Ent);
2532 -- Check for variants above us (the parent of the Clist can
2533 -- be a variant, in which case its parent is a variant part,
2534 -- and the parent of the variant part is a component list
2535 -- whose components must all be checked against the current
2536 -- component for overlap.
2538 if Nkind (Parent (Clist)) = N_Variant then
2539 Clist := Parent (Parent (Parent (Clist)));
2541 -- Check for possible discriminant part in record, this is
2542 -- treated essentially as another level in the recursion.
2543 -- For this case we have the parent of the component list
2544 -- is the record definition, and its parent is the full
2545 -- type declaration which contains the discriminant
2548 elsif Nkind (Parent (Clist)) = N_Record_Definition then
2549 Clist := Parent (Parent ((Clist)));
2551 -- If neither of these two cases, we are at the top of
2555 exit Component_List_Loop;
2557 end loop Component_List_Loop;
2559 <<Continue_Main_Component_Loop>>
2560 Next_Entity (C1_Ent);
2562 end loop Main_Component_Loop;
2566 -- For records that have component clauses for all components, and
2567 -- whose size is less than or equal to 32, we need to know the size
2568 -- in the front end to activate possible packed array processing
2569 -- where the component type is a record.
2571 -- At this stage Hbit + 1 represents the first unused bit from all
2572 -- the component clauses processed, so if the component clauses are
2573 -- complete, then this is the length of the record.
2575 -- For records longer than System.Storage_Unit, and for those where
2576 -- not all components have component clauses, the back end determines
2577 -- the length (it may for example be appopriate to round up the size
2578 -- to some convenient boundary, based on alignment considerations etc).
2580 if Unknown_RM_Size (Rectype)
2581 and then Hbit + 1 <= 32
2583 -- Nothing to do if at least one component with no component clause
2585 Comp := First_Component_Or_Discriminant (Rectype);
2586 while Present (Comp) loop
2587 exit when No (Component_Clause (Comp));
2588 Next_Component_Or_Discriminant (Comp);
2591 -- If we fall out of loop, all components have component clauses
2592 -- and so we can set the size to the maximum value.
2595 Set_RM_Size (Rectype, Hbit + 1);
2599 -- Check missing components if Complete_Representation pragma appeared
2601 if Present (CR_Pragma) then
2602 Comp := First_Component_Or_Discriminant (Rectype);
2603 while Present (Comp) loop
2604 if No (Component_Clause (Comp)) then
2606 ("missing component clause for &", CR_Pragma, Comp);
2609 Next_Component_Or_Discriminant (Comp);
2612 -- If no Complete_Representation pragma, warn if missing components
2614 elsif Warn_On_Unrepped_Components
2615 and then not Warnings_Off (Rectype)
2618 Num_Repped_Components : Nat := 0;
2619 Num_Unrepped_Components : Nat := 0;
2622 -- First count number of repped and unrepped components
2624 Comp := First_Component_Or_Discriminant (Rectype);
2625 while Present (Comp) loop
2626 if Present (Component_Clause (Comp)) then
2627 Num_Repped_Components := Num_Repped_Components + 1;
2629 Num_Unrepped_Components := Num_Unrepped_Components + 1;
2632 Next_Component_Or_Discriminant (Comp);
2635 -- We are only interested in the case where there is at least one
2636 -- unrepped component, and at least half the components have rep
2637 -- clauses. We figure that if less than half have them, then the
2638 -- partial rep clause is really intentional.
2640 if Num_Unrepped_Components > 0
2641 and then Num_Unrepped_Components < Num_Repped_Components
2643 Comp := First_Component_Or_Discriminant (Rectype);
2644 while Present (Comp) loop
2645 if No (Component_Clause (Comp)) then
2646 Error_Msg_Sloc := Sloc (Comp);
2648 ("?no component clause given for & declared #",
2652 Next_Component_Or_Discriminant (Comp);
2657 end Analyze_Record_Representation_Clause;
2659 -----------------------------
2660 -- Check_Component_Overlap --
2661 -----------------------------
2663 procedure Check_Component_Overlap (C1_Ent, C2_Ent : Entity_Id) is
2665 if Present (Component_Clause (C1_Ent))
2666 and then Present (Component_Clause (C2_Ent))
2668 -- Exclude odd case where we have two tag fields in the same
2669 -- record, both at location zero. This seems a bit strange,
2670 -- but it seems to happen in some circumstances ???
2672 if Chars (C1_Ent) = Name_uTag
2673 and then Chars (C2_Ent) = Name_uTag
2678 -- Here we check if the two fields overlap
2681 S1 : constant Uint := Component_Bit_Offset (C1_Ent);
2682 S2 : constant Uint := Component_Bit_Offset (C2_Ent);
2683 E1 : constant Uint := S1 + Esize (C1_Ent);
2684 E2 : constant Uint := S2 + Esize (C2_Ent);
2687 if E2 <= S1 or else E1 <= S2 then
2691 Component_Name (Component_Clause (C2_Ent));
2692 Error_Msg_Sloc := Sloc (Error_Msg_Node_2);
2694 Component_Name (Component_Clause (C1_Ent));
2696 ("component& overlaps & #",
2697 Component_Name (Component_Clause (C1_Ent)));
2701 end Check_Component_Overlap;
2703 -----------------------------------
2704 -- Check_Constant_Address_Clause --
2705 -----------------------------------
2707 procedure Check_Constant_Address_Clause
2711 procedure Check_At_Constant_Address (Nod : Node_Id);
2712 -- Checks that the given node N represents a name whose 'Address
2713 -- is constant (in the same sense as OK_Constant_Address_Clause,
2714 -- i.e. the address value is the same at the point of declaration
2715 -- of U_Ent and at the time of elaboration of the address clause.
2717 procedure Check_Expr_Constants (Nod : Node_Id);
2718 -- Checks that Nod meets the requirements for a constant address
2719 -- clause in the sense of the enclosing procedure.
2721 procedure Check_List_Constants (Lst : List_Id);
2722 -- Check that all elements of list Lst meet the requirements for a
2723 -- constant address clause in the sense of the enclosing procedure.
2725 -------------------------------
2726 -- Check_At_Constant_Address --
2727 -------------------------------
2729 procedure Check_At_Constant_Address (Nod : Node_Id) is
2731 if Is_Entity_Name (Nod) then
2732 if Present (Address_Clause (Entity ((Nod)))) then
2734 ("invalid address clause for initialized object &!",
2737 ("address for& cannot" &
2738 " depend on another address clause! (RM 13.1(22))!",
2741 elsif In_Same_Source_Unit (Entity (Nod), U_Ent)
2742 and then Sloc (U_Ent) < Sloc (Entity (Nod))
2745 ("invalid address clause for initialized object &!",
2747 Error_Msg_Name_1 := Chars (Entity (Nod));
2748 Error_Msg_Name_2 := Chars (U_Ent);
2750 ("\% must be defined before % (RM 13.1(22))!",
2754 elsif Nkind (Nod) = N_Selected_Component then
2756 T : constant Entity_Id := Etype (Prefix (Nod));
2759 if (Is_Record_Type (T)
2760 and then Has_Discriminants (T))
2763 and then Is_Record_Type (Designated_Type (T))
2764 and then Has_Discriminants (Designated_Type (T)))
2767 ("invalid address clause for initialized object &!",
2770 ("\address cannot depend on component" &
2771 " of discriminated record (RM 13.1(22))!",
2774 Check_At_Constant_Address (Prefix (Nod));
2778 elsif Nkind (Nod) = N_Indexed_Component then
2779 Check_At_Constant_Address (Prefix (Nod));
2780 Check_List_Constants (Expressions (Nod));
2783 Check_Expr_Constants (Nod);
2785 end Check_At_Constant_Address;
2787 --------------------------
2788 -- Check_Expr_Constants --
2789 --------------------------
2791 procedure Check_Expr_Constants (Nod : Node_Id) is
2792 Loc_U_Ent : constant Source_Ptr := Sloc (U_Ent);
2793 Ent : Entity_Id := Empty;
2796 if Nkind (Nod) in N_Has_Etype
2797 and then Etype (Nod) = Any_Type
2803 when N_Empty | N_Error =>
2806 when N_Identifier | N_Expanded_Name =>
2807 Ent := Entity (Nod);
2809 -- We need to look at the original node if it is different
2810 -- from the node, since we may have rewritten things and
2811 -- substituted an identifier representing the rewrite.
2813 if Original_Node (Nod) /= Nod then
2814 Check_Expr_Constants (Original_Node (Nod));
2816 -- If the node is an object declaration without initial
2817 -- value, some code has been expanded, and the expression
2818 -- is not constant, even if the constituents might be
2819 -- acceptable, as in A'Address + offset.
2821 if Ekind (Ent) = E_Variable
2822 and then Nkind (Declaration_Node (Ent))
2823 = N_Object_Declaration
2825 No (Expression (Declaration_Node (Ent)))
2828 ("invalid address clause for initialized object &!",
2831 -- If entity is constant, it may be the result of expanding
2832 -- a check. We must verify that its declaration appears
2833 -- before the object in question, else we also reject the
2836 elsif Ekind (Ent) = E_Constant
2837 and then In_Same_Source_Unit (Ent, U_Ent)
2838 and then Sloc (Ent) > Loc_U_Ent
2841 ("invalid address clause for initialized object &!",
2848 -- Otherwise look at the identifier and see if it is OK
2850 if Ekind (Ent) = E_Named_Integer
2852 Ekind (Ent) = E_Named_Real
2859 Ekind (Ent) = E_Constant
2861 Ekind (Ent) = E_In_Parameter
2863 -- This is the case where we must have Ent defined
2864 -- before U_Ent. Clearly if they are in different
2865 -- units this requirement is met since the unit
2866 -- containing Ent is already processed.
2868 if not In_Same_Source_Unit (Ent, U_Ent) then
2871 -- Otherwise location of Ent must be before the
2872 -- location of U_Ent, that's what prior defined means.
2874 elsif Sloc (Ent) < Loc_U_Ent then
2879 ("invalid address clause for initialized object &!",
2881 Error_Msg_Name_1 := Chars (Ent);
2882 Error_Msg_Name_2 := Chars (U_Ent);
2884 ("\% must be defined before % (RM 13.1(22))!",
2888 elsif Nkind (Original_Node (Nod)) = N_Function_Call then
2889 Check_Expr_Constants (Original_Node (Nod));
2893 ("invalid address clause for initialized object &!",
2896 if Comes_From_Source (Ent) then
2897 Error_Msg_Name_1 := Chars (Ent);
2899 ("\reference to variable% not allowed"
2900 & " (RM 13.1(22))!", Nod);
2903 ("non-static expression not allowed"
2904 & " (RM 13.1(22))!", Nod);
2908 when N_Integer_Literal =>
2910 -- If this is a rewritten unchecked conversion, in a system
2911 -- where Address is an integer type, always use the base type
2912 -- for a literal value. This is user-friendly and prevents
2913 -- order-of-elaboration issues with instances of unchecked
2916 if Nkind (Original_Node (Nod)) = N_Function_Call then
2917 Set_Etype (Nod, Base_Type (Etype (Nod)));
2920 when N_Real_Literal |
2922 N_Character_Literal =>
2926 Check_Expr_Constants (Low_Bound (Nod));
2927 Check_Expr_Constants (High_Bound (Nod));
2929 when N_Explicit_Dereference =>
2930 Check_Expr_Constants (Prefix (Nod));
2932 when N_Indexed_Component =>
2933 Check_Expr_Constants (Prefix (Nod));
2934 Check_List_Constants (Expressions (Nod));
2937 Check_Expr_Constants (Prefix (Nod));
2938 Check_Expr_Constants (Discrete_Range (Nod));
2940 when N_Selected_Component =>
2941 Check_Expr_Constants (Prefix (Nod));
2943 when N_Attribute_Reference =>
2944 if Attribute_Name (Nod) = Name_Address
2946 Attribute_Name (Nod) = Name_Access
2948 Attribute_Name (Nod) = Name_Unchecked_Access
2950 Attribute_Name (Nod) = Name_Unrestricted_Access
2952 Check_At_Constant_Address (Prefix (Nod));
2955 Check_Expr_Constants (Prefix (Nod));
2956 Check_List_Constants (Expressions (Nod));
2960 Check_List_Constants (Component_Associations (Nod));
2961 Check_List_Constants (Expressions (Nod));
2963 when N_Component_Association =>
2964 Check_Expr_Constants (Expression (Nod));
2966 when N_Extension_Aggregate =>
2967 Check_Expr_Constants (Ancestor_Part (Nod));
2968 Check_List_Constants (Component_Associations (Nod));
2969 Check_List_Constants (Expressions (Nod));
2974 when N_Binary_Op | N_And_Then | N_Or_Else | N_Membership_Test =>
2975 Check_Expr_Constants (Left_Opnd (Nod));
2976 Check_Expr_Constants (Right_Opnd (Nod));
2979 Check_Expr_Constants (Right_Opnd (Nod));
2981 when N_Type_Conversion |
2982 N_Qualified_Expression |
2984 Check_Expr_Constants (Expression (Nod));
2986 when N_Unchecked_Type_Conversion =>
2987 Check_Expr_Constants (Expression (Nod));
2989 -- If this is a rewritten unchecked conversion, subtypes
2990 -- in this node are those created within the instance.
2991 -- To avoid order of elaboration issues, replace them
2992 -- with their base types. Note that address clauses can
2993 -- cause order of elaboration problems because they are
2994 -- elaborated by the back-end at the point of definition,
2995 -- and may mention entities declared in between (as long
2996 -- as everything is static). It is user-friendly to allow
2997 -- unchecked conversions in this context.
2999 if Nkind (Original_Node (Nod)) = N_Function_Call then
3000 Set_Etype (Expression (Nod),
3001 Base_Type (Etype (Expression (Nod))));
3002 Set_Etype (Nod, Base_Type (Etype (Nod)));
3005 when N_Function_Call =>
3006 if not Is_Pure (Entity (Name (Nod))) then
3008 ("invalid address clause for initialized object &!",
3012 ("\function & is not pure (RM 13.1(22))!",
3013 Nod, Entity (Name (Nod)));
3016 Check_List_Constants (Parameter_Associations (Nod));
3019 when N_Parameter_Association =>
3020 Check_Expr_Constants (Explicit_Actual_Parameter (Nod));
3024 ("invalid address clause for initialized object &!",
3027 ("\must be constant defined before& (RM 13.1(22))!",
3030 end Check_Expr_Constants;
3032 --------------------------
3033 -- Check_List_Constants --
3034 --------------------------
3036 procedure Check_List_Constants (Lst : List_Id) is
3040 if Present (Lst) then
3041 Nod1 := First (Lst);
3042 while Present (Nod1) loop
3043 Check_Expr_Constants (Nod1);
3047 end Check_List_Constants;
3049 -- Start of processing for Check_Constant_Address_Clause
3052 Check_Expr_Constants (Expr);
3053 end Check_Constant_Address_Clause;
3059 procedure Check_Size
3063 Biased : out Boolean)
3065 UT : constant Entity_Id := Underlying_Type (T);
3071 -- Dismiss cases for generic types or types with previous errors
3074 or else UT = Any_Type
3075 or else Is_Generic_Type (UT)
3076 or else Is_Generic_Type (Root_Type (UT))
3080 -- Check case of bit packed array
3082 elsif Is_Array_Type (UT)
3083 and then Known_Static_Component_Size (UT)
3084 and then Is_Bit_Packed_Array (UT)
3092 Asiz := Component_Size (UT);
3093 Indx := First_Index (UT);
3095 Ityp := Etype (Indx);
3097 -- If non-static bound, then we are not in the business of
3098 -- trying to check the length, and indeed an error will be
3099 -- issued elsewhere, since sizes of non-static array types
3100 -- cannot be set implicitly or explicitly.
3102 if not Is_Static_Subtype (Ityp) then
3106 -- Otherwise accumulate next dimension
3108 Asiz := Asiz * (Expr_Value (Type_High_Bound (Ityp)) -
3109 Expr_Value (Type_Low_Bound (Ityp)) +
3113 exit when No (Indx);
3119 Error_Msg_Uint_1 := Asiz;
3121 ("size for& too small, minimum allowed is ^", N, T);
3122 Set_Esize (T, Asiz);
3123 Set_RM_Size (T, Asiz);
3127 -- All other composite types are ignored
3129 elsif Is_Composite_Type (UT) then
3132 -- For fixed-point types, don't check minimum if type is not frozen,
3133 -- since we don't know all the characteristics of the type that can
3134 -- affect the size (e.g. a specified small) till freeze time.
3136 elsif Is_Fixed_Point_Type (UT)
3137 and then not Is_Frozen (UT)
3141 -- Cases for which a minimum check is required
3144 -- Ignore if specified size is correct for the type
3146 if Known_Esize (UT) and then Siz = Esize (UT) then
3150 -- Otherwise get minimum size
3152 M := UI_From_Int (Minimum_Size (UT));
3156 -- Size is less than minimum size, but one possibility remains
3157 -- that we can manage with the new size if we bias the type
3159 M := UI_From_Int (Minimum_Size (UT, Biased => True));
3162 Error_Msg_Uint_1 := M;
3164 ("size for& too small, minimum allowed is ^", N, T);
3174 -------------------------
3175 -- Get_Alignment_Value --
3176 -------------------------
3178 function Get_Alignment_Value (Expr : Node_Id) return Uint is
3179 Align : constant Uint := Static_Integer (Expr);
3182 if Align = No_Uint then
3185 elsif Align <= 0 then
3186 Error_Msg_N ("alignment value must be positive", Expr);
3190 for J in Int range 0 .. 64 loop
3192 M : constant Uint := Uint_2 ** J;
3195 exit when M = Align;
3199 ("alignment value must be power of 2", Expr);
3207 end Get_Alignment_Value;
3213 procedure Initialize is
3215 Unchecked_Conversions.Init;
3218 -------------------------
3219 -- Is_Operational_Item --
3220 -------------------------
3222 function Is_Operational_Item (N : Node_Id) return Boolean is
3224 if Nkind (N) /= N_Attribute_Definition_Clause then
3228 Id : constant Attribute_Id := Get_Attribute_Id (Chars (N));
3231 return Id = Attribute_Input
3232 or else Id = Attribute_Output
3233 or else Id = Attribute_Read
3234 or else Id = Attribute_Write
3235 or else Id = Attribute_External_Tag;
3238 end Is_Operational_Item;
3240 --------------------------------------
3241 -- Mark_Aliased_Address_As_Volatile --
3242 --------------------------------------
3244 procedure Mark_Aliased_Address_As_Volatile (N : Node_Id) is
3245 Ent : constant Entity_Id := Address_Aliased_Entity (N);
3248 if Present (Ent) then
3249 Set_Treat_As_Volatile (Ent);
3251 end Mark_Aliased_Address_As_Volatile;
3257 function Minimum_Size
3259 Biased : Boolean := False) return Nat
3261 Lo : Uint := No_Uint;
3262 Hi : Uint := No_Uint;
3263 LoR : Ureal := No_Ureal;
3264 HiR : Ureal := No_Ureal;
3265 LoSet : Boolean := False;
3266 HiSet : Boolean := False;
3270 R_Typ : constant Entity_Id := Root_Type (T);
3273 -- If bad type, return 0
3275 if T = Any_Type then
3278 -- For generic types, just return zero. There cannot be any legitimate
3279 -- need to know such a size, but this routine may be called with a
3280 -- generic type as part of normal processing.
3282 elsif Is_Generic_Type (R_Typ)
3283 or else R_Typ = Any_Type
3287 -- Access types. Normally an access type cannot have a size smaller
3288 -- than the size of System.Address. The exception is on VMS, where
3289 -- we have short and long addresses, and it is possible for an access
3290 -- type to have a short address size (and thus be less than the size
3291 -- of System.Address itself). We simply skip the check for VMS, and
3292 -- leave the back end to do the check.
3294 elsif Is_Access_Type (T) then
3295 if OpenVMS_On_Target then
3298 return System_Address_Size;
3301 -- Floating-point types
3303 elsif Is_Floating_Point_Type (T) then
3304 return UI_To_Int (Esize (R_Typ));
3308 elsif Is_Discrete_Type (T) then
3310 -- The following loop is looking for the nearest compile time
3311 -- known bounds following the ancestor subtype chain. The idea
3312 -- is to find the most restrictive known bounds information.
3316 if Ancest = Any_Type or else Etype (Ancest) = Any_Type then
3321 if Compile_Time_Known_Value (Type_Low_Bound (Ancest)) then
3322 Lo := Expr_Rep_Value (Type_Low_Bound (Ancest));
3329 if Compile_Time_Known_Value (Type_High_Bound (Ancest)) then
3330 Hi := Expr_Rep_Value (Type_High_Bound (Ancest));
3336 Ancest := Ancestor_Subtype (Ancest);
3339 Ancest := Base_Type (T);
3341 if Is_Generic_Type (Ancest) then
3347 -- Fixed-point types. We can't simply use Expr_Value to get the
3348 -- Corresponding_Integer_Value values of the bounds, since these
3349 -- do not get set till the type is frozen, and this routine can
3350 -- be called before the type is frozen. Similarly the test for
3351 -- bounds being static needs to include the case where we have
3352 -- unanalyzed real literals for the same reason.
3354 elsif Is_Fixed_Point_Type (T) then
3356 -- The following loop is looking for the nearest compile time
3357 -- known bounds following the ancestor subtype chain. The idea
3358 -- is to find the most restrictive known bounds information.
3362 if Ancest = Any_Type or else Etype (Ancest) = Any_Type then
3367 if Nkind (Type_Low_Bound (Ancest)) = N_Real_Literal
3368 or else Compile_Time_Known_Value (Type_Low_Bound (Ancest))
3370 LoR := Expr_Value_R (Type_Low_Bound (Ancest));
3377 if Nkind (Type_High_Bound (Ancest)) = N_Real_Literal
3378 or else Compile_Time_Known_Value (Type_High_Bound (Ancest))
3380 HiR := Expr_Value_R (Type_High_Bound (Ancest));
3386 Ancest := Ancestor_Subtype (Ancest);
3389 Ancest := Base_Type (T);
3391 if Is_Generic_Type (Ancest) then
3397 Lo := UR_To_Uint (LoR / Small_Value (T));
3398 Hi := UR_To_Uint (HiR / Small_Value (T));
3400 -- No other types allowed
3403 raise Program_Error;
3406 -- Fall through with Hi and Lo set. Deal with biased case
3408 if (Biased and then not Is_Fixed_Point_Type (T))
3409 or else Has_Biased_Representation (T)
3415 -- Signed case. Note that we consider types like range 1 .. -1 to be
3416 -- signed for the purpose of computing the size, since the bounds
3417 -- have to be accomodated in the base type.
3419 if Lo < 0 or else Hi < 0 then
3423 -- S = size, B = 2 ** (size - 1) (can accommodate -B .. +(B - 1))
3424 -- Note that we accommodate the case where the bounds cross. This
3425 -- can happen either because of the way the bounds are declared
3426 -- or because of the algorithm in Freeze_Fixed_Point_Type.
3440 -- If both bounds are positive, make sure that both are represen-
3441 -- table in the case where the bounds are crossed. This can happen
3442 -- either because of the way the bounds are declared, or because of
3443 -- the algorithm in Freeze_Fixed_Point_Type.
3449 -- S = size, (can accommodate 0 .. (2**size - 1))
3452 while Hi >= Uint_2 ** S loop
3460 ---------------------------
3461 -- New_Stream_Subprogram --
3462 ---------------------------
3464 procedure New_Stream_Subprogram
3468 Nam : TSS_Name_Type)
3470 Loc : constant Source_Ptr := Sloc (N);
3471 Sname : constant Name_Id := Make_TSS_Name (Base_Type (Ent), Nam);
3472 Subp_Id : Entity_Id;
3473 Subp_Decl : Node_Id;
3477 Defer_Declaration : constant Boolean :=
3478 Is_Tagged_Type (Ent) or else Is_Private_Type (Ent);
3479 -- For a tagged type, there is a declaration for each stream attribute
3480 -- at the freeze point, and we must generate only a completion of this
3481 -- declaration. We do the same for private types, because the full view
3482 -- might be tagged. Otherwise we generate a declaration at the point of
3483 -- the attribute definition clause.
3485 function Build_Spec return Node_Id;
3486 -- Used for declaration and renaming declaration, so that this is
3487 -- treated as a renaming_as_body.
3493 function Build_Spec return Node_Id is
3494 Out_P : constant Boolean := (Nam = TSS_Stream_Read);
3497 T_Ref : constant Node_Id := New_Reference_To (Etyp, Loc);
3500 Subp_Id := Make_Defining_Identifier (Loc, Sname);
3502 -- S : access Root_Stream_Type'Class
3504 Formals := New_List (
3505 Make_Parameter_Specification (Loc,
3506 Defining_Identifier =>
3507 Make_Defining_Identifier (Loc, Name_S),
3509 Make_Access_Definition (Loc,
3512 Designated_Type (Etype (F)), Loc))));
3514 if Nam = TSS_Stream_Input then
3515 Spec := Make_Function_Specification (Loc,
3516 Defining_Unit_Name => Subp_Id,
3517 Parameter_Specifications => Formals,
3518 Result_Definition => T_Ref);
3523 Make_Parameter_Specification (Loc,
3524 Defining_Identifier => Make_Defining_Identifier (Loc, Name_V),
3525 Out_Present => Out_P,
3526 Parameter_Type => T_Ref));
3528 Spec := Make_Procedure_Specification (Loc,
3529 Defining_Unit_Name => Subp_Id,
3530 Parameter_Specifications => Formals);
3536 -- Start of processing for New_Stream_Subprogram
3539 F := First_Formal (Subp);
3541 if Ekind (Subp) = E_Procedure then
3542 Etyp := Etype (Next_Formal (F));
3544 Etyp := Etype (Subp);
3547 -- Prepare subprogram declaration and insert it as an action on the
3548 -- clause node. The visibility for this entity is used to test for
3549 -- visibility of the attribute definition clause (in the sense of
3550 -- 8.3(23) as amended by AI-195).
3552 if not Defer_Declaration then
3554 Make_Subprogram_Declaration (Loc,
3555 Specification => Build_Spec);
3557 -- For a tagged type, there is always a visible declaration for each
3558 -- stream TSS (it is a predefined primitive operation), and the
3559 -- completion of this declaration occurs at the freeze point, which is
3560 -- not always visible at places where the attribute definition clause is
3561 -- visible. So, we create a dummy entity here for the purpose of
3562 -- tracking the visibility of the attribute definition clause itself.
3566 Make_Defining_Identifier (Loc,
3567 Chars => New_External_Name (Sname, 'V'));
3569 Make_Object_Declaration (Loc,
3570 Defining_Identifier => Subp_Id,
3571 Object_Definition => New_Occurrence_Of (Standard_Boolean, Loc));
3574 Insert_Action (N, Subp_Decl);
3575 Set_Entity (N, Subp_Id);
3578 Make_Subprogram_Renaming_Declaration (Loc,
3579 Specification => Build_Spec,
3580 Name => New_Reference_To (Subp, Loc));
3582 if Defer_Declaration then
3583 Set_TSS (Base_Type (Ent), Subp_Id);
3585 Insert_Action (N, Subp_Decl);
3586 Copy_TSS (Subp_Id, Base_Type (Ent));
3588 end New_Stream_Subprogram;
3590 ------------------------
3591 -- Rep_Item_Too_Early --
3592 ------------------------
3594 function Rep_Item_Too_Early (T : Entity_Id; N : Node_Id) return Boolean is
3596 -- Cannot apply non-operational rep items to generic types
3598 if Is_Operational_Item (N) then
3602 and then Is_Generic_Type (Root_Type (T))
3605 ("representation item not allowed for generic type", N);
3609 -- Otherwise check for incompleted type
3611 if Is_Incomplete_Or_Private_Type (T)
3612 and then No (Underlying_Type (T))
3615 ("representation item must be after full type declaration", N);
3618 -- If the type has incompleted components, a representation clause is
3619 -- illegal but stream attributes and Convention pragmas are correct.
3621 elsif Has_Private_Component (T) then
3622 if Nkind (N) = N_Pragma then
3626 ("representation item must appear after type is fully defined",
3633 end Rep_Item_Too_Early;
3635 -----------------------
3636 -- Rep_Item_Too_Late --
3637 -----------------------
3639 function Rep_Item_Too_Late
3642 FOnly : Boolean := False) return Boolean
3645 Parent_Type : Entity_Id;
3648 -- Output the too late message. Note that this is not considered a
3649 -- serious error, since the effect is simply that we ignore the
3650 -- representation clause in this case.
3656 procedure Too_Late is
3658 Error_Msg_N ("|representation item appears too late!", N);
3661 -- Start of processing for Rep_Item_Too_Late
3664 -- First make sure entity is not frozen (RM 13.1(9)). Exclude imported
3665 -- types, which may be frozen if they appear in a representation clause
3666 -- for a local type.
3669 and then not From_With_Type (T)
3672 S := First_Subtype (T);
3674 if Present (Freeze_Node (S)) then
3676 ("?no more representation items for }", Freeze_Node (S), S);
3681 -- Check for case of non-tagged derived type whose parent either has
3682 -- primitive operations, or is a by reference type (RM 13.1(10)).
3686 and then Is_Derived_Type (T)
3687 and then not Is_Tagged_Type (T)
3689 Parent_Type := Etype (Base_Type (T));
3691 if Has_Primitive_Operations (Parent_Type) then
3694 ("primitive operations already defined for&!", N, Parent_Type);
3697 elsif Is_By_Reference_Type (Parent_Type) then
3700 ("parent type & is a by reference type!", N, Parent_Type);
3705 -- No error, link item into head of chain of rep items for the entity
3707 Record_Rep_Item (T, N);
3709 end Rep_Item_Too_Late;
3711 -------------------------
3712 -- Same_Representation --
3713 -------------------------
3715 function Same_Representation (Typ1, Typ2 : Entity_Id) return Boolean is
3716 T1 : constant Entity_Id := Underlying_Type (Typ1);
3717 T2 : constant Entity_Id := Underlying_Type (Typ2);
3720 -- A quick check, if base types are the same, then we definitely have
3721 -- the same representation, because the subtype specific representation
3722 -- attributes (Size and Alignment) do not affect representation from
3723 -- the point of view of this test.
3725 if Base_Type (T1) = Base_Type (T2) then
3728 elsif Is_Private_Type (Base_Type (T2))
3729 and then Base_Type (T1) = Full_View (Base_Type (T2))
3734 -- Tagged types never have differing representations
3736 if Is_Tagged_Type (T1) then
3740 -- Representations are definitely different if conventions differ
3742 if Convention (T1) /= Convention (T2) then
3746 -- Representations are different if component alignments differ
3748 if (Is_Record_Type (T1) or else Is_Array_Type (T1))
3750 (Is_Record_Type (T2) or else Is_Array_Type (T2))
3751 and then Component_Alignment (T1) /= Component_Alignment (T2)
3756 -- For arrays, the only real issue is component size. If we know the
3757 -- component size for both arrays, and it is the same, then that's
3758 -- good enough to know we don't have a change of representation.
3760 if Is_Array_Type (T1) then
3761 if Known_Component_Size (T1)
3762 and then Known_Component_Size (T2)
3763 and then Component_Size (T1) = Component_Size (T2)
3769 -- Types definitely have same representation if neither has non-standard
3770 -- representation since default representations are always consistent.
3771 -- If only one has non-standard representation, and the other does not,
3772 -- then we consider that they do not have the same representation. They
3773 -- might, but there is no way of telling early enough.
3775 if Has_Non_Standard_Rep (T1) then
3776 if not Has_Non_Standard_Rep (T2) then
3780 return not Has_Non_Standard_Rep (T2);
3783 -- Here the two types both have non-standard representation, and we
3784 -- need to determine if they have the same non-standard representation
3786 -- For arrays, we simply need to test if the component sizes are the
3787 -- same. Pragma Pack is reflected in modified component sizes, so this
3788 -- check also deals with pragma Pack.
3790 if Is_Array_Type (T1) then
3791 return Component_Size (T1) = Component_Size (T2);
3793 -- Tagged types always have the same representation, because it is not
3794 -- possible to specify different representations for common fields.
3796 elsif Is_Tagged_Type (T1) then
3799 -- Case of record types
3801 elsif Is_Record_Type (T1) then
3803 -- Packed status must conform
3805 if Is_Packed (T1) /= Is_Packed (T2) then
3808 -- Otherwise we must check components. Typ2 maybe a constrained
3809 -- subtype with fewer components, so we compare the components
3810 -- of the base types.
3813 Record_Case : declare
3814 CD1, CD2 : Entity_Id;
3816 function Same_Rep return Boolean;
3817 -- CD1 and CD2 are either components or discriminants. This
3818 -- function tests whether the two have the same representation
3824 function Same_Rep return Boolean is
3826 if No (Component_Clause (CD1)) then
3827 return No (Component_Clause (CD2));
3831 Present (Component_Clause (CD2))
3833 Component_Bit_Offset (CD1) = Component_Bit_Offset (CD2)
3835 Esize (CD1) = Esize (CD2);
3839 -- Start processing for Record_Case
3842 if Has_Discriminants (T1) then
3843 CD1 := First_Discriminant (T1);
3844 CD2 := First_Discriminant (T2);
3846 -- The number of discriminants may be different if the
3847 -- derived type has fewer (constrained by values). The
3848 -- invisible discriminants retain the representation of
3849 -- the original, so the discrepancy does not per se
3850 -- indicate a different representation.
3853 and then Present (CD2)
3855 if not Same_Rep then
3858 Next_Discriminant (CD1);
3859 Next_Discriminant (CD2);
3864 CD1 := First_Component (Underlying_Type (Base_Type (T1)));
3865 CD2 := First_Component (Underlying_Type (Base_Type (T2)));
3867 while Present (CD1) loop
3868 if not Same_Rep then
3871 Next_Component (CD1);
3872 Next_Component (CD2);
3880 -- For enumeration types, we must check each literal to see if the
3881 -- representation is the same. Note that we do not permit enumeration
3882 -- reprsentation clauses for Character and Wide_Character, so these
3883 -- cases were already dealt with.
3885 elsif Is_Enumeration_Type (T1) then
3887 Enumeration_Case : declare
3891 L1 := First_Literal (T1);
3892 L2 := First_Literal (T2);
3894 while Present (L1) loop
3895 if Enumeration_Rep (L1) /= Enumeration_Rep (L2) then
3905 end Enumeration_Case;
3907 -- Any other types have the same representation for these purposes
3912 end Same_Representation;
3914 --------------------
3915 -- Set_Enum_Esize --
3916 --------------------
3918 procedure Set_Enum_Esize (T : Entity_Id) is
3926 -- Find the minimum standard size (8,16,32,64) that fits
3928 Lo := Enumeration_Rep (Entity (Type_Low_Bound (T)));
3929 Hi := Enumeration_Rep (Entity (Type_High_Bound (T)));
3932 if Lo >= -Uint_2**07 and then Hi < Uint_2**07 then
3933 Sz := Standard_Character_Size; -- May be > 8 on some targets
3935 elsif Lo >= -Uint_2**15 and then Hi < Uint_2**15 then
3938 elsif Lo >= -Uint_2**31 and then Hi < Uint_2**31 then
3941 else pragma Assert (Lo >= -Uint_2**63 and then Hi < Uint_2**63);
3946 if Hi < Uint_2**08 then
3947 Sz := Standard_Character_Size; -- May be > 8 on some targets
3949 elsif Hi < Uint_2**16 then
3952 elsif Hi < Uint_2**32 then
3955 else pragma Assert (Hi < Uint_2**63);
3960 -- That minimum is the proper size unless we have a foreign convention
3961 -- and the size required is 32 or less, in which case we bump the size
3962 -- up to 32. This is required for C and C++ and seems reasonable for
3963 -- all other foreign conventions.
3965 if Has_Foreign_Convention (T)
3966 and then Esize (T) < Standard_Integer_Size
3968 Init_Esize (T, Standard_Integer_Size);
3975 -----------------------------------
3976 -- Validate_Unchecked_Conversion --
3977 -----------------------------------
3979 procedure Validate_Unchecked_Conversion
3981 Act_Unit : Entity_Id)
3988 -- Obtain source and target types. Note that we call Ancestor_Subtype
3989 -- here because the processing for generic instantiation always makes
3990 -- subtypes, and we want the original frozen actual types.
3992 -- If we are dealing with private types, then do the check on their
3993 -- fully declared counterparts if the full declarations have been
3994 -- encountered (they don't have to be visible, but they must exist!)
3996 Source := Ancestor_Subtype (Etype (First_Formal (Act_Unit)));
3998 if Is_Private_Type (Source)
3999 and then Present (Underlying_Type (Source))
4001 Source := Underlying_Type (Source);
4004 Target := Ancestor_Subtype (Etype (Act_Unit));
4006 -- If either type is generic, the instantiation happens within a
4007 -- generic unit, and there is nothing to check. The proper check
4008 -- will happen when the enclosing generic is instantiated.
4010 if Is_Generic_Type (Source) or else Is_Generic_Type (Target) then
4014 if Is_Private_Type (Target)
4015 and then Present (Underlying_Type (Target))
4017 Target := Underlying_Type (Target);
4020 -- Source may be unconstrained array, but not target
4022 if Is_Array_Type (Target)
4023 and then not Is_Constrained (Target)
4026 ("unchecked conversion to unconstrained array not allowed", N);
4030 -- Warn if conversion between two different convention pointers
4032 if Is_Access_Type (Target)
4033 and then Is_Access_Type (Source)
4034 and then Convention (Target) /= Convention (Source)
4035 and then Warn_On_Unchecked_Conversion
4038 ("?conversion between pointers with different conventions!", N);
4041 -- Make entry in unchecked conversion table for later processing
4042 -- by Validate_Unchecked_Conversions, which will check sizes and
4043 -- alignments (using values set by the back-end where possible).
4044 -- This is only done if the appropriate warning is active
4046 if Warn_On_Unchecked_Conversion then
4047 Unchecked_Conversions.Append
4048 (New_Val => UC_Entry'
4053 -- If both sizes are known statically now, then back end annotation
4054 -- is not required to do a proper check but if either size is not
4055 -- known statically, then we need the annotation.
4057 if Known_Static_RM_Size (Source)
4058 and then Known_Static_RM_Size (Target)
4062 Back_Annotate_Rep_Info := True;
4066 -- If unchecked conversion to access type, and access type is
4067 -- declared in the same unit as the unchecked conversion, then
4068 -- set the No_Strict_Aliasing flag (no strict aliasing is
4069 -- implicit in this situation).
4071 if Is_Access_Type (Target) and then
4072 In_Same_Source_Unit (Target, N)
4074 Set_No_Strict_Aliasing (Implementation_Base_Type (Target));
4077 -- Generate N_Validate_Unchecked_Conversion node for back end in
4078 -- case the back end needs to perform special validation checks.
4080 -- Shouldn't this be in exp_ch13, since the check only gets done
4081 -- if we have full expansion and the back end is called ???
4084 Make_Validate_Unchecked_Conversion (Sloc (N));
4085 Set_Source_Type (Vnode, Source);
4086 Set_Target_Type (Vnode, Target);
4088 -- If the unchecked conversion node is in a list, just insert before
4089 -- it. If not we have some strange case, not worth bothering about.
4091 if Is_List_Member (N) then
4092 Insert_After (N, Vnode);
4094 end Validate_Unchecked_Conversion;
4096 ------------------------------------
4097 -- Validate_Unchecked_Conversions --
4098 ------------------------------------
4100 procedure Validate_Unchecked_Conversions is
4102 for N in Unchecked_Conversions.First .. Unchecked_Conversions.Last loop
4104 T : UC_Entry renames Unchecked_Conversions.Table (N);
4106 Enode : constant Node_Id := T.Enode;
4107 Source : constant Entity_Id := T.Source;
4108 Target : constant Entity_Id := T.Target;
4114 -- This validation check, which warns if we have unequal sizes
4115 -- for unchecked conversion, and thus potentially implementation
4116 -- dependent semantics, is one of the few occasions on which we
4117 -- use the official RM size instead of Esize. See description
4118 -- in Einfo "Handling of Type'Size Values" for details.
4120 if Serious_Errors_Detected = 0
4121 and then Known_Static_RM_Size (Source)
4122 and then Known_Static_RM_Size (Target)
4124 Source_Siz := RM_Size (Source);
4125 Target_Siz := RM_Size (Target);
4127 if Source_Siz /= Target_Siz then
4129 ("?types for unchecked conversion have different sizes!",
4132 if All_Errors_Mode then
4133 Error_Msg_Name_1 := Chars (Source);
4134 Error_Msg_Uint_1 := Source_Siz;
4135 Error_Msg_Name_2 := Chars (Target);
4136 Error_Msg_Uint_2 := Target_Siz;
4138 ("\size of % is ^, size of % is ^?", Enode);
4140 Error_Msg_Uint_1 := UI_Abs (Source_Siz - Target_Siz);
4142 if Is_Discrete_Type (Source)
4143 and then Is_Discrete_Type (Target)
4145 if Source_Siz > Target_Siz then
4147 ("\?^ high order bits of source will be ignored!",
4150 elsif Is_Unsigned_Type (Source) then
4152 ("\?source will be extended with ^ high order " &
4153 "zero bits?!", Enode);
4157 ("\?source will be extended with ^ high order " &
4162 elsif Source_Siz < Target_Siz then
4163 if Is_Discrete_Type (Target) then
4164 if Bytes_Big_Endian then
4166 ("\?target value will include ^ undefined " &
4171 ("\?target value will include ^ undefined " &
4178 ("\?^ trailing bits of target value will be " &
4179 "undefined!", Enode);
4182 else pragma Assert (Source_Siz > Target_Siz);
4184 ("\?^ trailing bits of source will be ignored!",
4191 -- If both types are access types, we need to check the alignment.
4192 -- If the alignment of both is specified, we can do it here.
4194 if Serious_Errors_Detected = 0
4195 and then Ekind (Source) in Access_Kind
4196 and then Ekind (Target) in Access_Kind
4197 and then Target_Strict_Alignment
4198 and then Present (Designated_Type (Source))
4199 and then Present (Designated_Type (Target))
4202 D_Source : constant Entity_Id := Designated_Type (Source);
4203 D_Target : constant Entity_Id := Designated_Type (Target);
4206 if Known_Alignment (D_Source)
4207 and then Known_Alignment (D_Target)
4210 Source_Align : constant Uint := Alignment (D_Source);
4211 Target_Align : constant Uint := Alignment (D_Target);
4214 if Source_Align < Target_Align
4215 and then not Is_Tagged_Type (D_Source)
4217 Error_Msg_Uint_1 := Target_Align;
4218 Error_Msg_Uint_2 := Source_Align;
4219 Error_Msg_Node_2 := D_Source;
4221 ("?alignment of & (^) is stricter than " &
4222 "alignment of & (^)!", Enode, D_Target);
4224 if All_Errors_Mode then
4226 ("\?resulting access value may have invalid " &
4227 "alignment!", Enode);
4236 end Validate_Unchecked_Conversions;