1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2007, Free Software Foundation, Inc. --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING3. If not, go to --
19 -- http://www.gnu.org/licenses for a complete copy of the license. --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
24 ------------------------------------------------------------------------------
26 with Atree
; use Atree
;
27 with Checks
; use Checks
;
28 with Einfo
; use Einfo
;
29 with Errout
; use Errout
;
30 with Exp_Tss
; use Exp_Tss
;
31 with Exp_Util
; use Exp_Util
;
33 with Lib
.Xref
; use Lib
.Xref
;
34 with Namet
; use Namet
;
35 with Nlists
; use Nlists
;
36 with Nmake
; use Nmake
;
38 with Restrict
; use Restrict
;
39 with Rident
; use Rident
;
40 with Rtsfind
; use Rtsfind
;
42 with Sem_Ch8
; use Sem_Ch8
;
43 with Sem_Eval
; use Sem_Eval
;
44 with Sem_Res
; use Sem_Res
;
45 with Sem_Type
; use Sem_Type
;
46 with Sem_Util
; use Sem_Util
;
47 with Sem_Warn
; use Sem_Warn
;
48 with Snames
; use Snames
;
49 with Stand
; use Stand
;
50 with Sinfo
; use Sinfo
;
52 with Targparm
; use Targparm
;
53 with Ttypes
; use Ttypes
;
54 with Tbuild
; use Tbuild
;
55 with Urealp
; use Urealp
;
57 with GNAT
.Heap_Sort_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 New_Stream_Subprogram
97 -- Create a subprogram renaming of a given stream attribute to the
98 -- designated subprogram and then in the tagged case, provide this as a
99 -- primitive operation, or in the non-tagged case make an appropriate TSS
100 -- entry. This is more properly an expansion activity than just semantics,
101 -- but the presence of user-defined stream functions for limited types is a
102 -- legality check, which is why this takes place here rather than in
103 -- exp_ch13, where it was previously. Nam indicates the name of the TSS
104 -- function to be generated.
106 -- To avoid elaboration anomalies with freeze nodes, for untagged types
107 -- we generate both a subprogram declaration and a subprogram renaming
108 -- declaration, so that the attribute specification is handled as a
109 -- renaming_as_body. For tagged types, the specification is one of the
112 ----------------------------------------------
113 -- Table for Validate_Unchecked_Conversions --
114 ----------------------------------------------
116 -- The following table collects unchecked conversions for validation.
117 -- Entries are made by Validate_Unchecked_Conversion and then the
118 -- call to Validate_Unchecked_Conversions does the actual error
119 -- checking and posting of warnings. The reason for this delayed
120 -- processing is to take advantage of back-annotations of size and
121 -- alignment values peformed by the back end.
123 type UC_Entry
is record
124 Enode
: Node_Id
; -- node used for posting warnings
125 Source
: Entity_Id
; -- source type for unchecked conversion
126 Target
: Entity_Id
; -- target type for unchecked conversion
129 package Unchecked_Conversions
is new Table
.Table
(
130 Table_Component_Type
=> UC_Entry
,
131 Table_Index_Type
=> Int
,
132 Table_Low_Bound
=> 1,
134 Table_Increment
=> 200,
135 Table_Name
=> "Unchecked_Conversions");
137 ----------------------------------------
138 -- Table for Validate_Address_Clauses --
139 ----------------------------------------
141 -- If an address clause has the form
143 -- for X'Address use Expr
145 -- where Expr is of the form Y'Address or recursively is a reference
146 -- to a constant of either of these forms, and X and Y are entities of
147 -- objects, then if Y has a smaller alignment than X, that merits a
148 -- warning about possible bad alignment. The following table collects
149 -- address clauses of this kind. We put these in a table so that they
150 -- can be checked after the back end has completed annotation of the
151 -- alignments of objects, since we can catch more cases that way.
153 type Address_Clause_Check_Record
is record
155 -- The address clause
158 -- The entity of the object overlaying Y
161 -- The entity of the object being overlaid
164 package Address_Clause_Checks
is new Table
.Table
(
165 Table_Component_Type
=> Address_Clause_Check_Record
,
166 Table_Index_Type
=> Int
,
167 Table_Low_Bound
=> 1,
169 Table_Increment
=> 200,
170 Table_Name
=> "Address_Clause_Checks");
172 ----------------------------
173 -- Address_Aliased_Entity --
174 ----------------------------
176 function Address_Aliased_Entity
(N
: Node_Id
) return Entity_Id
is
178 if Nkind
(N
) = N_Attribute_Reference
179 and then Attribute_Name
(N
) = Name_Address
182 Nam
: Node_Id
:= Prefix
(N
);
185 or else Nkind
(Nam
) = N_Selected_Component
186 or else Nkind
(Nam
) = N_Indexed_Component
191 if Is_Entity_Name
(Nam
) then
198 end Address_Aliased_Entity
;
200 -----------------------------------------
201 -- Adjust_Record_For_Reverse_Bit_Order --
202 -----------------------------------------
204 procedure Adjust_Record_For_Reverse_Bit_Order
(R
: Entity_Id
) is
205 Max_Machine_Scalar_Size
: constant Uint
:=
207 (Standard_Long_Long_Integer_Size
);
208 -- We use this as the maximum machine scalar size in the sense of AI-133
212 SSU
: constant Uint
:= UI_From_Int
(System_Storage_Unit
);
215 -- This first loop through components does two things. First it deals
216 -- with the case of components with component clauses whose length is
217 -- greater than the maximum machine scalar size (either accepting them
218 -- or rejecting as needed). Second, it counts the number of components
219 -- with component clauses whose length does not exceed this maximum for
223 Comp
:= First_Component_Or_Discriminant
(R
);
224 while Present
(Comp
) loop
226 CC
: constant Node_Id
:= Component_Clause
(Comp
);
227 Fbit
: constant Uint
:= Static_Integer
(First_Bit
(CC
));
232 -- Case of component with size > max machine scalar
234 if Esize
(Comp
) > Max_Machine_Scalar_Size
then
236 -- Must begin on byte boundary
238 if Fbit
mod SSU
/= 0 then
240 ("illegal first bit value for reverse bit order",
242 Error_Msg_Uint_1
:= SSU
;
243 Error_Msg_Uint_2
:= Max_Machine_Scalar_Size
;
246 ("\must be a multiple of ^ if size greater than ^",
249 -- Must end on byte boundary
251 elsif Esize
(Comp
) mod SSU
/= 0 then
253 ("illegal last bit value for reverse bit order",
255 Error_Msg_Uint_1
:= SSU
;
256 Error_Msg_Uint_2
:= Max_Machine_Scalar_Size
;
259 ("\must be a multiple of ^ if size greater than ^",
262 -- OK, give warning if enabled
264 elsif Warn_On_Reverse_Bit_Order
then
266 ("multi-byte field specified with non-standard"
267 & " Bit_Order?", CC
);
269 if Bytes_Big_Endian
then
271 ("\bytes are not reversed "
272 & "(component is big-endian)?", CC
);
275 ("\bytes are not reversed "
276 & "(component is little-endian)?", CC
);
280 -- Case where size is not greater than max machine scalar.
281 -- For now, we just count these.
284 Num_CC
:= Num_CC
+ 1;
289 Next_Component_Or_Discriminant
(Comp
);
292 -- We need to sort the component clauses on the basis of the Position
293 -- values in the clause, so we can group clauses with the same Position.
294 -- together to determine the relevant machine scalar size.
297 Comps
: array (0 .. Num_CC
) of Entity_Id
;
298 -- Array to collect component and discrimninant entities. The data
299 -- starts at index 1, the 0'th entry is for GNAT.Heap_Sort_A.
301 function CP_Lt
(Op1
, Op2
: Natural) return Boolean;
302 -- Compare routine for Sort (See GNAT.Heap_Sort_A)
304 procedure CP_Move
(From
: Natural; To
: Natural);
305 -- Move routine for Sort (see GNAT.Heap_Sort_A)
309 -- Start and stop positions in component list of set of components
310 -- with the same starting position (that constitute components in
311 -- a single machine scalar).
314 -- Maximum last bit value of any component in this set
317 -- Corresponding machine scalar size
323 function CP_Lt
(Op1
, Op2
: Natural) return Boolean is
325 return Position
(Component_Clause
(Comps
(Op1
))) <
326 Position
(Component_Clause
(Comps
(Op2
)));
333 procedure CP_Move
(From
: Natural; To
: Natural) is
335 Comps
(To
) := Comps
(From
);
339 -- Collect the component clauses
342 Comp
:= First_Component_Or_Discriminant
(R
);
343 while Present
(Comp
) loop
344 if Present
(Component_Clause
(Comp
))
345 and then Esize
(Comp
) <= Max_Machine_Scalar_Size
347 Num_CC
:= Num_CC
+ 1;
348 Comps
(Num_CC
) := Comp
;
351 Next_Component_Or_Discriminant
(Comp
);
354 -- Sort by ascending position number
356 Sort
(Num_CC
, CP_Move
'Unrestricted_Access, CP_Lt
'Unrestricted_Access);
358 -- We now have all the components whose size does not exceed the max
359 -- machine scalar value, sorted by starting position. In this loop
360 -- we gather groups of clauses starting at the same position, to
361 -- process them in accordance with Ada 2005 AI-133.
364 while Stop
< Num_CC
loop
368 Static_Integer
(Last_Bit
(Component_Clause
(Comps
(Start
))));
369 while Stop
< Num_CC
loop
371 (Position
(Component_Clause
(Comps
(Stop
+ 1)))) =
373 (Position
(Component_Clause
(Comps
(Stop
))))
380 (Last_Bit
(Component_Clause
(Comps
(Stop
)))));
386 -- Now we have a group of component clauses from Start to Stop
387 -- whose positions are identical, and MaxL is the maximum last bit
388 -- value of any of these components.
390 -- We need to determine the corresponding machine scalar size.
391 -- This loop assumes that machine scalar sizes are even, and that
392 -- each possible machine scalar has twice as many bits as the
395 MSS
:= Max_Machine_Scalar_Size
;
397 and then (MSS
/ 2) >= SSU
398 and then (MSS
/ 2) > MaxL
403 -- Here is where we fix up the Component_Bit_Offset value to
404 -- account for the reverse bit order. Some examples of what needs
405 -- to be done for the case of a machine scalar size of 8 are:
407 -- First_Bit .. Last_Bit Component_Bit_Offset
419 -- The general rule is that the first bit is is obtained by
420 -- subtracting the old ending bit from machine scalar size - 1.
422 for C
in Start
.. Stop
loop
424 Comp
: constant Entity_Id
:= Comps
(C
);
425 CC
: constant Node_Id
:= Component_Clause
(Comp
);
426 LB
: constant Uint
:= Static_Integer
(Last_Bit
(CC
));
427 NFB
: constant Uint
:= MSS
- Uint_1
- LB
;
428 NLB
: constant Uint
:= NFB
+ Esize
(Comp
) - 1;
429 Pos
: constant Uint
:= Static_Integer
(Position
(CC
));
432 if Warn_On_Reverse_Bit_Order
then
433 Error_Msg_Uint_1
:= MSS
;
435 ("?reverse bit order in machine " &
436 "scalar of length^", First_Bit
(CC
));
437 Error_Msg_Uint_1
:= NFB
;
438 Error_Msg_Uint_2
:= NLB
;
440 if Bytes_Big_Endian
then
442 ("?\big-endian range for component & is ^ .. ^",
443 First_Bit
(CC
), Comp
);
446 ("?\little-endian range for component & is ^ .. ^",
447 First_Bit
(CC
), Comp
);
451 Set_Component_Bit_Offset
(Comp
, Pos
* SSU
+ NFB
);
452 Set_Normalized_First_Bit
(Comp
, NFB
mod SSU
);
457 end Adjust_Record_For_Reverse_Bit_Order
;
459 --------------------------------------
460 -- Alignment_Check_For_Esize_Change --
461 --------------------------------------
463 procedure Alignment_Check_For_Esize_Change
(Typ
: Entity_Id
) is
465 -- If the alignment is known, and not set by a rep clause, and is
466 -- inconsistent with the size being set, then reset it to unknown,
467 -- we assume in this case that the size overrides the inherited
468 -- alignment, and that the alignment must be recomputed.
470 if Known_Alignment
(Typ
)
471 and then not Has_Alignment_Clause
(Typ
)
472 and then Esize
(Typ
) mod (Alignment
(Typ
) * SSU
) /= 0
474 Init_Alignment
(Typ
);
476 end Alignment_Check_For_Esize_Change
;
478 -----------------------
479 -- Analyze_At_Clause --
480 -----------------------
482 -- An at clause is replaced by the corresponding Address attribute
483 -- definition clause that is the preferred approach in Ada 95.
485 procedure Analyze_At_Clause
(N
: Node_Id
) is
487 Check_Restriction
(No_Obsolescent_Features
, N
);
489 if Warn_On_Obsolescent_Feature
then
491 ("at clause is an obsolescent feature (RM J.7(2))?", N
);
493 ("\use address attribute definition clause instead?", N
);
497 Make_Attribute_Definition_Clause
(Sloc
(N
),
498 Name
=> Identifier
(N
),
499 Chars
=> Name_Address
,
500 Expression
=> Expression
(N
)));
501 Analyze_Attribute_Definition_Clause
(N
);
502 end Analyze_At_Clause
;
504 -----------------------------------------
505 -- Analyze_Attribute_Definition_Clause --
506 -----------------------------------------
508 procedure Analyze_Attribute_Definition_Clause
(N
: Node_Id
) is
509 Loc
: constant Source_Ptr
:= Sloc
(N
);
510 Nam
: constant Node_Id
:= Name
(N
);
511 Attr
: constant Name_Id
:= Chars
(N
);
512 Expr
: constant Node_Id
:= Expression
(N
);
513 Id
: constant Attribute_Id
:= Get_Attribute_Id
(Attr
);
517 FOnly
: Boolean := False;
518 -- Reset to True for subtype specific attribute (Alignment, Size)
519 -- and for stream attributes, i.e. those cases where in the call
520 -- to Rep_Item_Too_Late, FOnly is set True so that only the freezing
521 -- rules are checked. Note that the case of stream attributes is not
522 -- clear from the RM, but see AI95-00137. Also, the RM seems to
523 -- disallow Storage_Size for derived task types, but that is also
524 -- clearly unintentional.
526 procedure Analyze_Stream_TSS_Definition
(TSS_Nam
: TSS_Name_Type
);
527 -- Common processing for 'Read, 'Write, 'Input and 'Output attribute
528 -- definition clauses.
530 procedure Analyze_Stream_TSS_Definition
(TSS_Nam
: TSS_Name_Type
) is
531 Subp
: Entity_Id
:= Empty
;
536 Is_Read
: constant Boolean := (TSS_Nam
= TSS_Stream_Read
);
538 function Has_Good_Profile
(Subp
: Entity_Id
) return Boolean;
539 -- Return true if the entity is a subprogram with an appropriate
540 -- profile for the attribute being defined.
542 ----------------------
543 -- Has_Good_Profile --
544 ----------------------
546 function Has_Good_Profile
(Subp
: Entity_Id
) return Boolean is
548 Is_Function
: constant Boolean := (TSS_Nam
= TSS_Stream_Input
);
549 Expected_Ekind
: constant array (Boolean) of Entity_Kind
:=
550 (False => E_Procedure
, True => E_Function
);
554 if Ekind
(Subp
) /= Expected_Ekind
(Is_Function
) then
558 F
:= First_Formal
(Subp
);
561 or else Ekind
(Etype
(F
)) /= E_Anonymous_Access_Type
562 or else Designated_Type
(Etype
(F
)) /=
563 Class_Wide_Type
(RTE
(RE_Root_Stream_Type
))
568 if not Is_Function
then
572 Expected_Mode
: constant array (Boolean) of Entity_Kind
:=
573 (False => E_In_Parameter
,
574 True => E_Out_Parameter
);
576 if Parameter_Mode
(F
) /= Expected_Mode
(Is_Read
) then
587 return Base_Type
(Typ
) = Base_Type
(Ent
)
588 and then No
(Next_Formal
(F
));
590 end Has_Good_Profile
;
592 -- Start of processing for Analyze_Stream_TSS_Definition
597 if not Is_Type
(U_Ent
) then
598 Error_Msg_N
("local name must be a subtype", Nam
);
602 Pnam
:= TSS
(Base_Type
(U_Ent
), TSS_Nam
);
604 -- If Pnam is present, it can be either inherited from an ancestor
605 -- type (in which case it is legal to redefine it for this type), or
606 -- be a previous definition of the attribute for the same type (in
607 -- which case it is illegal).
609 -- In the first case, it will have been analyzed already, and we
610 -- can check that its profile does not match the expected profile
611 -- for a stream attribute of U_Ent. In the second case, either Pnam
612 -- has been analyzed (and has the expected profile), or it has not
613 -- been analyzed yet (case of a type that has not been frozen yet
614 -- and for which the stream attribute has been set using Set_TSS).
617 and then (No
(First_Entity
(Pnam
)) or else Has_Good_Profile
(Pnam
))
619 Error_Msg_Sloc
:= Sloc
(Pnam
);
620 Error_Msg_Name_1
:= Attr
;
621 Error_Msg_N
("% attribute already defined #", Nam
);
627 if Is_Entity_Name
(Expr
) then
628 if not Is_Overloaded
(Expr
) then
629 if Has_Good_Profile
(Entity
(Expr
)) then
630 Subp
:= Entity
(Expr
);
634 Get_First_Interp
(Expr
, I
, It
);
635 while Present
(It
.Nam
) loop
636 if Has_Good_Profile
(It
.Nam
) then
641 Get_Next_Interp
(I
, It
);
646 if Present
(Subp
) then
647 if Is_Abstract_Subprogram
(Subp
) then
648 Error_Msg_N
("stream subprogram must not be abstract", Expr
);
652 Set_Entity
(Expr
, Subp
);
653 Set_Etype
(Expr
, Etype
(Subp
));
655 New_Stream_Subprogram
(N
, U_Ent
, Subp
, TSS_Nam
);
658 Error_Msg_Name_1
:= Attr
;
659 Error_Msg_N
("incorrect expression for% attribute", Expr
);
661 end Analyze_Stream_TSS_Definition
;
663 -- Start of processing for Analyze_Attribute_Definition_Clause
666 if Ignore_Rep_Clauses
then
667 Rewrite
(N
, Make_Null_Statement
(Sloc
(N
)));
674 if Rep_Item_Too_Early
(Ent
, N
) then
678 -- Rep clause applies to full view of incomplete type or private type if
679 -- we have one (if not, this is a premature use of the type). However,
680 -- certain semantic checks need to be done on the specified entity (i.e.
681 -- the private view), so we save it in Ent.
683 if Is_Private_Type
(Ent
)
684 and then Is_Derived_Type
(Ent
)
685 and then not Is_Tagged_Type
(Ent
)
686 and then No
(Full_View
(Ent
))
688 -- If this is a private type whose completion is a derivation from
689 -- another private type, there is no full view, and the attribute
690 -- belongs to the type itself, not its underlying parent.
694 elsif Ekind
(Ent
) = E_Incomplete_Type
then
696 -- The attribute applies to the full view, set the entity of the
697 -- attribute definition accordingly.
699 Ent
:= Underlying_Type
(Ent
);
701 Set_Entity
(Nam
, Ent
);
704 U_Ent
:= Underlying_Type
(Ent
);
707 -- Complete other routine error checks
709 if Etype
(Nam
) = Any_Type
then
712 elsif Scope
(Ent
) /= Current_Scope
then
713 Error_Msg_N
("entity must be declared in this scope", Nam
);
716 elsif No
(U_Ent
) then
719 elsif Is_Type
(U_Ent
)
720 and then not Is_First_Subtype
(U_Ent
)
721 and then Id
/= Attribute_Object_Size
722 and then Id
/= Attribute_Value_Size
723 and then not From_At_Mod
(N
)
725 Error_Msg_N
("cannot specify attribute for subtype", Nam
);
729 -- Switch on particular attribute
737 -- Address attribute definition clause
739 when Attribute_Address
=> Address
: begin
740 Analyze_And_Resolve
(Expr
, RTE
(RE_Address
));
742 if Present
(Address_Clause
(U_Ent
)) then
743 Error_Msg_N
("address already given for &", Nam
);
745 -- Case of address clause for subprogram
747 elsif Is_Subprogram
(U_Ent
) then
748 if Has_Homonym
(U_Ent
) then
750 ("address clause cannot be given " &
751 "for overloaded subprogram",
756 -- For subprograms, all address clauses are permitted, and we
757 -- mark the subprogram as having a deferred freeze so that Gigi
758 -- will not elaborate it too soon.
760 -- Above needs more comments, what is too soon about???
762 Set_Has_Delayed_Freeze
(U_Ent
);
764 -- Case of address clause for entry
766 elsif Ekind
(U_Ent
) = E_Entry
then
767 if Nkind
(Parent
(N
)) = N_Task_Body
then
769 ("entry address must be specified in task spec", Nam
);
773 -- For entries, we require a constant address
775 Check_Constant_Address_Clause
(Expr
, U_Ent
);
777 -- Special checks for task types
779 if Is_Task_Type
(Scope
(U_Ent
))
780 and then Comes_From_Source
(Scope
(U_Ent
))
783 ("?entry address declared for entry in task type", N
);
785 ("\?only one task can be declared of this type", N
);
788 -- Entry address clauses are obsolescent
790 Check_Restriction
(No_Obsolescent_Features
, N
);
792 if Warn_On_Obsolescent_Feature
then
794 ("attaching interrupt to task entry is an " &
795 "obsolescent feature (RM J.7.1)?", N
);
797 ("\use interrupt procedure instead?", N
);
800 -- Case of an address clause for a controlled object which we
801 -- consider to be erroneous.
803 elsif Is_Controlled
(Etype
(U_Ent
))
804 or else Has_Controlled_Component
(Etype
(U_Ent
))
807 ("?controlled object& must not be overlaid", Nam
, U_Ent
);
809 ("\?Program_Error will be raised at run time", Nam
);
810 Insert_Action
(Declaration_Node
(U_Ent
),
811 Make_Raise_Program_Error
(Loc
,
812 Reason
=> PE_Overlaid_Controlled_Object
));
815 -- Case of address clause for a (non-controlled) object
818 Ekind
(U_Ent
) = E_Variable
820 Ekind
(U_Ent
) = E_Constant
823 Expr
: constant Node_Id
:= Expression
(N
);
824 Aent
: constant Entity_Id
:= Address_Aliased_Entity
(Expr
);
825 Ent_Y
: constant Entity_Id
:= Find_Overlaid_Object
(N
);
828 -- Exported variables cannot have an address clause,
829 -- because this cancels the effect of the pragma Export
831 if Is_Exported
(U_Ent
) then
833 ("cannot export object with address clause", Nam
);
836 -- Overlaying controlled objects is erroneous
839 and then (Has_Controlled_Component
(Etype
(Aent
))
840 or else Is_Controlled
(Etype
(Aent
)))
843 ("?cannot overlay with controlled object", Expr
);
845 ("\?Program_Error will be raised at run time", Expr
);
846 Insert_Action
(Declaration_Node
(U_Ent
),
847 Make_Raise_Program_Error
(Loc
,
848 Reason
=> PE_Overlaid_Controlled_Object
));
852 and then Ekind
(U_Ent
) = E_Constant
853 and then Ekind
(Aent
) /= E_Constant
855 Error_Msg_N
("constant overlays a variable?", Expr
);
857 elsif Present
(Renamed_Object
(U_Ent
)) then
859 ("address clause not allowed"
860 & " for a renaming declaration (RM 13.1(6))", Nam
);
863 -- Imported variables can have an address clause, but then
864 -- the import is pretty meaningless except to suppress
865 -- initializations, so we do not need such variables to
866 -- be statically allocated (and in fact it causes trouble
867 -- if the address clause is a local value).
869 elsif Is_Imported
(U_Ent
) then
870 Set_Is_Statically_Allocated
(U_Ent
, False);
873 -- We mark a possible modification of a variable with an
874 -- address clause, since it is likely aliasing is occurring.
876 Note_Possible_Modification
(Nam
);
878 -- Here we are checking for explicit overlap of one variable
879 -- by another, and if we find this then mark the overlapped
880 -- variable as also being volatile to prevent unwanted
883 if Present
(Ent_Y
) then
884 Set_Treat_As_Volatile
(Ent_Y
);
887 -- Legality checks on the address clause for initialized
888 -- objects is deferred until the freeze point, because
889 -- a subsequent pragma might indicate that the object is
890 -- imported and thus not initialized.
892 Set_Has_Delayed_Freeze
(U_Ent
);
894 if Is_Exported
(U_Ent
) then
896 ("& cannot be exported if an address clause is given",
899 ("\define and export a variable " &
900 "that holds its address instead",
904 -- Entity has delayed freeze, so we will generate an
905 -- alignment check at the freeze point unless suppressed.
907 if not Range_Checks_Suppressed
(U_Ent
)
908 and then not Alignment_Checks_Suppressed
(U_Ent
)
910 Set_Check_Address_Alignment
(N
);
913 -- Kill the size check code, since we are not allocating
914 -- the variable, it is somewhere else.
916 Kill_Size_Check_Code
(U_Ent
);
919 -- If the address clause is of the form:
921 -- for X'Address use Y'Address
925 -- Const : constant Address := Y'Address;
927 -- for X'Address use Const;
929 -- then we make an entry in the table for checking the size and
930 -- alignment of the overlaying variable. We defer this check
931 -- till after code generation to take full advantage of the
932 -- annotation done by the back end. This entry is only made if
933 -- we have not already posted a warning about size/alignment
934 -- (some warnings of this type are posted in Checks).
936 if Address_Clause_Overlay_Warnings
then
938 Ent_X
: Entity_Id
:= Empty
;
939 Ent_Y
: Entity_Id
:= Empty
;
942 Ent_Y
:= Find_Overlaid_Object
(N
);
944 if Present
(Ent_Y
) and then Is_Entity_Name
(Name
(N
)) then
945 Ent_X
:= Entity
(Name
(N
));
946 Address_Clause_Checks
.Append
((N
, Ent_X
, Ent_Y
));
951 -- Not a valid entity for an address clause
954 Error_Msg_N
("address cannot be given for &", Nam
);
962 -- Alignment attribute definition clause
964 when Attribute_Alignment
=> Alignment_Block
: declare
965 Align
: constant Uint
:= Get_Alignment_Value
(Expr
);
970 if not Is_Type
(U_Ent
)
971 and then Ekind
(U_Ent
) /= E_Variable
972 and then Ekind
(U_Ent
) /= E_Constant
974 Error_Msg_N
("alignment cannot be given for &", Nam
);
976 elsif Has_Alignment_Clause
(U_Ent
) then
977 Error_Msg_Sloc
:= Sloc
(Alignment_Clause
(U_Ent
));
978 Error_Msg_N
("alignment clause previously given#", N
);
980 elsif Align
/= No_Uint
then
981 Set_Has_Alignment_Clause
(U_Ent
);
982 Set_Alignment
(U_Ent
, Align
);
990 -- Bit_Order attribute definition clause
992 when Attribute_Bit_Order
=> Bit_Order
: declare
994 if not Is_Record_Type
(U_Ent
) then
996 ("Bit_Order can only be defined for record type", Nam
);
999 Analyze_And_Resolve
(Expr
, RTE
(RE_Bit_Order
));
1001 if Etype
(Expr
) = Any_Type
then
1004 elsif not Is_Static_Expression
(Expr
) then
1005 Flag_Non_Static_Expr
1006 ("Bit_Order requires static expression!", Expr
);
1009 if (Expr_Value
(Expr
) = 0) /= Bytes_Big_Endian
then
1010 Set_Reverse_Bit_Order
(U_Ent
, True);
1016 --------------------
1017 -- Component_Size --
1018 --------------------
1020 -- Component_Size attribute definition clause
1022 when Attribute_Component_Size
=> Component_Size_Case
: declare
1023 Csize
: constant Uint
:= Static_Integer
(Expr
);
1026 New_Ctyp
: Entity_Id
;
1030 if not Is_Array_Type
(U_Ent
) then
1031 Error_Msg_N
("component size requires array type", Nam
);
1035 Btype
:= Base_Type
(U_Ent
);
1037 if Has_Component_Size_Clause
(Btype
) then
1039 ("component size clase for& previously given", Nam
);
1041 elsif Csize
/= No_Uint
then
1042 Check_Size
(Expr
, Component_Type
(Btype
), Csize
, Biased
);
1044 if Has_Aliased_Components
(Btype
)
1047 and then Csize
/= 16
1050 ("component size incorrect for aliased components", N
);
1054 -- For the biased case, build a declaration for a subtype
1055 -- that will be used to represent the biased subtype that
1056 -- reflects the biased representation of components. We need
1057 -- this subtype to get proper conversions on referencing
1058 -- elements of the array.
1062 Make_Defining_Identifier
(Loc
,
1063 Chars
=> New_External_Name
(Chars
(U_Ent
), 'C', 0, 'T'));
1066 Make_Subtype_Declaration
(Loc
,
1067 Defining_Identifier
=> New_Ctyp
,
1068 Subtype_Indication
=>
1069 New_Occurrence_Of
(Component_Type
(Btype
), Loc
));
1071 Set_Parent
(Decl
, N
);
1072 Analyze
(Decl
, Suppress
=> All_Checks
);
1074 Set_Has_Delayed_Freeze
(New_Ctyp
, False);
1075 Set_Esize
(New_Ctyp
, Csize
);
1076 Set_RM_Size
(New_Ctyp
, Csize
);
1077 Init_Alignment
(New_Ctyp
);
1078 Set_Has_Biased_Representation
(New_Ctyp
, True);
1079 Set_Is_Itype
(New_Ctyp
, True);
1080 Set_Associated_Node_For_Itype
(New_Ctyp
, U_Ent
);
1082 Set_Component_Type
(Btype
, New_Ctyp
);
1085 Set_Component_Size
(Btype
, Csize
);
1086 Set_Has_Component_Size_Clause
(Btype
, True);
1087 Set_Has_Non_Standard_Rep
(Btype
, True);
1089 end Component_Size_Case
;
1095 when Attribute_External_Tag
=> External_Tag
:
1097 if not Is_Tagged_Type
(U_Ent
) then
1098 Error_Msg_N
("should be a tagged type", Nam
);
1101 Analyze_And_Resolve
(Expr
, Standard_String
);
1103 if not Is_Static_Expression
(Expr
) then
1104 Flag_Non_Static_Expr
1105 ("static string required for tag name!", Nam
);
1108 if VM_Target
= No_VM
then
1109 Set_Has_External_Tag_Rep_Clause
(U_Ent
);
1111 Error_Msg_Name_1
:= Attr
;
1113 ("% attribute unsupported in this configuration", Nam
);
1116 if not Is_Library_Level_Entity
(U_Ent
) then
1118 ("?non-unique external tag supplied for &", N
, U_Ent
);
1120 ("?\same external tag applies to all subprogram calls", N
);
1122 ("?\corresponding internal tag cannot be obtained", N
);
1130 when Attribute_Input
=>
1131 Analyze_Stream_TSS_Definition
(TSS_Stream_Input
);
1132 Set_Has_Specified_Stream_Input
(Ent
);
1138 -- Machine radix attribute definition clause
1140 when Attribute_Machine_Radix
=> Machine_Radix
: declare
1141 Radix
: constant Uint
:= Static_Integer
(Expr
);
1144 if not Is_Decimal_Fixed_Point_Type
(U_Ent
) then
1145 Error_Msg_N
("decimal fixed-point type expected for &", Nam
);
1147 elsif Has_Machine_Radix_Clause
(U_Ent
) then
1148 Error_Msg_Sloc
:= Sloc
(Alignment_Clause
(U_Ent
));
1149 Error_Msg_N
("machine radix clause previously given#", N
);
1151 elsif Radix
/= No_Uint
then
1152 Set_Has_Machine_Radix_Clause
(U_Ent
);
1153 Set_Has_Non_Standard_Rep
(Base_Type
(U_Ent
));
1157 elsif Radix
= 10 then
1158 Set_Machine_Radix_10
(U_Ent
);
1160 Error_Msg_N
("machine radix value must be 2 or 10", Expr
);
1169 -- Object_Size attribute definition clause
1171 when Attribute_Object_Size
=> Object_Size
: declare
1172 Size
: constant Uint
:= Static_Integer
(Expr
);
1176 if not Is_Type
(U_Ent
) then
1177 Error_Msg_N
("Object_Size cannot be given for &", Nam
);
1179 elsif Has_Object_Size_Clause
(U_Ent
) then
1180 Error_Msg_N
("Object_Size already given for &", Nam
);
1183 Check_Size
(Expr
, U_Ent
, Size
, Biased
);
1191 UI_Mod
(Size
, 64) /= 0
1194 ("Object_Size must be 8, 16, 32, or multiple of 64",
1198 Set_Esize
(U_Ent
, Size
);
1199 Set_Has_Object_Size_Clause
(U_Ent
);
1200 Alignment_Check_For_Esize_Change
(U_Ent
);
1208 when Attribute_Output
=>
1209 Analyze_Stream_TSS_Definition
(TSS_Stream_Output
);
1210 Set_Has_Specified_Stream_Output
(Ent
);
1216 when Attribute_Read
=>
1217 Analyze_Stream_TSS_Definition
(TSS_Stream_Read
);
1218 Set_Has_Specified_Stream_Read
(Ent
);
1224 -- Size attribute definition clause
1226 when Attribute_Size
=> Size
: declare
1227 Size
: constant Uint
:= Static_Integer
(Expr
);
1234 if Has_Size_Clause
(U_Ent
) then
1235 Error_Msg_N
("size already given for &", Nam
);
1237 elsif not Is_Type
(U_Ent
)
1238 and then Ekind
(U_Ent
) /= E_Variable
1239 and then Ekind
(U_Ent
) /= E_Constant
1241 Error_Msg_N
("size cannot be given for &", Nam
);
1243 elsif Is_Array_Type
(U_Ent
)
1244 and then not Is_Constrained
(U_Ent
)
1247 ("size cannot be given for unconstrained array", Nam
);
1249 elsif Size
/= No_Uint
then
1250 if Is_Type
(U_Ent
) then
1253 Etyp
:= Etype
(U_Ent
);
1256 -- Check size, note that Gigi is in charge of checking that the
1257 -- size of an array or record type is OK. Also we do not check
1258 -- the size in the ordinary fixed-point case, since it is too
1259 -- early to do so (there may be subsequent small clause that
1260 -- affects the size). We can check the size if a small clause
1261 -- has already been given.
1263 if not Is_Ordinary_Fixed_Point_Type
(U_Ent
)
1264 or else Has_Small_Clause
(U_Ent
)
1266 Check_Size
(Expr
, Etyp
, Size
, Biased
);
1267 Set_Has_Biased_Representation
(U_Ent
, Biased
);
1270 -- For types set RM_Size and Esize if possible
1272 if Is_Type
(U_Ent
) then
1273 Set_RM_Size
(U_Ent
, Size
);
1275 -- For scalar types, increase Object_Size to power of 2, but
1276 -- not less than a storage unit in any case (i.e., normally
1277 -- this means it will be byte addressable).
1279 if Is_Scalar_Type
(U_Ent
) then
1280 if Size
<= System_Storage_Unit
then
1281 Init_Esize
(U_Ent
, System_Storage_Unit
);
1282 elsif Size
<= 16 then
1283 Init_Esize
(U_Ent
, 16);
1284 elsif Size
<= 32 then
1285 Init_Esize
(U_Ent
, 32);
1287 Set_Esize
(U_Ent
, (Size
+ 63) / 64 * 64);
1290 -- For all other types, object size = value size. The
1291 -- backend will adjust as needed.
1294 Set_Esize
(U_Ent
, Size
);
1297 Alignment_Check_For_Esize_Change
(U_Ent
);
1299 -- For objects, set Esize only
1302 if Is_Elementary_Type
(Etyp
) then
1303 if Size
/= System_Storage_Unit
1305 Size
/= System_Storage_Unit
* 2
1307 Size
/= System_Storage_Unit
* 4
1309 Size
/= System_Storage_Unit
* 8
1311 Error_Msg_Uint_1
:= UI_From_Int
(System_Storage_Unit
);
1312 Error_Msg_Uint_2
:= Error_Msg_Uint_1
* 8;
1314 ("size for primitive object must be a power of 2"
1315 & " in the range ^-^", N
);
1319 Set_Esize
(U_Ent
, Size
);
1322 Set_Has_Size_Clause
(U_Ent
);
1330 -- Small attribute definition clause
1332 when Attribute_Small
=> Small
: declare
1333 Implicit_Base
: constant Entity_Id
:= Base_Type
(U_Ent
);
1337 Analyze_And_Resolve
(Expr
, Any_Real
);
1339 if Etype
(Expr
) = Any_Type
then
1342 elsif not Is_Static_Expression
(Expr
) then
1343 Flag_Non_Static_Expr
1344 ("small requires static expression!", Expr
);
1348 Small
:= Expr_Value_R
(Expr
);
1350 if Small
<= Ureal_0
then
1351 Error_Msg_N
("small value must be greater than zero", Expr
);
1357 if not Is_Ordinary_Fixed_Point_Type
(U_Ent
) then
1359 ("small requires an ordinary fixed point type", Nam
);
1361 elsif Has_Small_Clause
(U_Ent
) then
1362 Error_Msg_N
("small already given for &", Nam
);
1364 elsif Small
> Delta_Value
(U_Ent
) then
1366 ("small value must not be greater then delta value", Nam
);
1369 Set_Small_Value
(U_Ent
, Small
);
1370 Set_Small_Value
(Implicit_Base
, Small
);
1371 Set_Has_Small_Clause
(U_Ent
);
1372 Set_Has_Small_Clause
(Implicit_Base
);
1373 Set_Has_Non_Standard_Rep
(Implicit_Base
);
1381 -- Storage_Pool attribute definition clause
1383 when Attribute_Storage_Pool
=> Storage_Pool
: declare
1388 if Ekind
(U_Ent
) = E_Access_Subprogram_Type
then
1390 ("storage pool cannot be given for access-to-subprogram type",
1394 elsif Ekind
(U_Ent
) /= E_Access_Type
1395 and then Ekind
(U_Ent
) /= E_General_Access_Type
1398 ("storage pool can only be given for access types", Nam
);
1401 elsif Is_Derived_Type
(U_Ent
) then
1403 ("storage pool cannot be given for a derived access type",
1406 elsif Has_Storage_Size_Clause
(U_Ent
) then
1407 Error_Msg_N
("storage size already given for &", Nam
);
1410 elsif Present
(Associated_Storage_Pool
(U_Ent
)) then
1411 Error_Msg_N
("storage pool already given for &", Nam
);
1416 (Expr
, Class_Wide_Type
(RTE
(RE_Root_Storage_Pool
)));
1418 if Nkind
(Expr
) = N_Type_Conversion
then
1419 T
:= Etype
(Expression
(Expr
));
1424 -- The Stack_Bounded_Pool is used internally for implementing
1425 -- access types with a Storage_Size. Since it only work
1426 -- properly when used on one specific type, we need to check
1427 -- that it is not highjacked improperly:
1428 -- type T is access Integer;
1429 -- for T'Storage_Size use n;
1430 -- type Q is access Float;
1431 -- for Q'Storage_Size use T'Storage_Size; -- incorrect
1433 if RTE_Available
(RE_Stack_Bounded_Pool
)
1434 and then Base_Type
(T
) = RTE
(RE_Stack_Bounded_Pool
)
1436 Error_Msg_N
("non-shareable internal Pool", Expr
);
1440 -- If the argument is a name that is not an entity name, then
1441 -- we construct a renaming operation to define an entity of
1442 -- type storage pool.
1444 if not Is_Entity_Name
(Expr
)
1445 and then Is_Object_Reference
(Expr
)
1448 Make_Defining_Identifier
(Loc
,
1449 Chars
=> New_Internal_Name
('P'));
1452 Rnode
: constant Node_Id
:=
1453 Make_Object_Renaming_Declaration
(Loc
,
1454 Defining_Identifier
=> Pool
,
1456 New_Occurrence_Of
(Etype
(Expr
), Loc
),
1460 Insert_Before
(N
, Rnode
);
1462 Set_Associated_Storage_Pool
(U_Ent
, Pool
);
1465 elsif Is_Entity_Name
(Expr
) then
1466 Pool
:= Entity
(Expr
);
1468 -- If pool is a renamed object, get original one. This can
1469 -- happen with an explicit renaming, and within instances.
1471 while Present
(Renamed_Object
(Pool
))
1472 and then Is_Entity_Name
(Renamed_Object
(Pool
))
1474 Pool
:= Entity
(Renamed_Object
(Pool
));
1477 if Present
(Renamed_Object
(Pool
))
1478 and then Nkind
(Renamed_Object
(Pool
)) = N_Type_Conversion
1479 and then Is_Entity_Name
(Expression
(Renamed_Object
(Pool
)))
1481 Pool
:= Entity
(Expression
(Renamed_Object
(Pool
)));
1484 Set_Associated_Storage_Pool
(U_Ent
, Pool
);
1486 elsif Nkind
(Expr
) = N_Type_Conversion
1487 and then Is_Entity_Name
(Expression
(Expr
))
1488 and then Nkind
(Original_Node
(Expr
)) = N_Attribute_Reference
1490 Pool
:= Entity
(Expression
(Expr
));
1491 Set_Associated_Storage_Pool
(U_Ent
, Pool
);
1494 Error_Msg_N
("incorrect reference to a Storage Pool", Expr
);
1503 -- Storage_Size attribute definition clause
1505 when Attribute_Storage_Size
=> Storage_Size
: declare
1506 Btype
: constant Entity_Id
:= Base_Type
(U_Ent
);
1510 if Is_Task_Type
(U_Ent
) then
1511 Check_Restriction
(No_Obsolescent_Features
, N
);
1513 if Warn_On_Obsolescent_Feature
then
1515 ("storage size clause for task is an " &
1516 "obsolescent feature (RM J.9)?", N
);
1518 ("\use Storage_Size pragma instead?", N
);
1524 if not Is_Access_Type
(U_Ent
)
1525 and then Ekind
(U_Ent
) /= E_Task_Type
1527 Error_Msg_N
("storage size cannot be given for &", Nam
);
1529 elsif Is_Access_Type
(U_Ent
) and Is_Derived_Type
(U_Ent
) then
1531 ("storage size cannot be given for a derived access type",
1534 elsif Has_Storage_Size_Clause
(Btype
) then
1535 Error_Msg_N
("storage size already given for &", Nam
);
1538 Analyze_And_Resolve
(Expr
, Any_Integer
);
1540 if Is_Access_Type
(U_Ent
) then
1541 if Present
(Associated_Storage_Pool
(U_Ent
)) then
1542 Error_Msg_N
("storage pool already given for &", Nam
);
1546 if Compile_Time_Known_Value
(Expr
)
1547 and then Expr_Value
(Expr
) = 0
1549 Set_No_Pool_Assigned
(Btype
);
1552 else -- Is_Task_Type (U_Ent)
1553 Sprag
:= Get_Rep_Pragma
(Btype
, Name_Storage_Size
);
1555 if Present
(Sprag
) then
1556 Error_Msg_Sloc
:= Sloc
(Sprag
);
1558 ("Storage_Size already specified#", Nam
);
1563 Set_Has_Storage_Size_Clause
(Btype
);
1571 when Attribute_Stream_Size
=> Stream_Size
: declare
1572 Size
: constant Uint
:= Static_Integer
(Expr
);
1575 if Ada_Version
<= Ada_95
then
1576 Check_Restriction
(No_Implementation_Attributes
, N
);
1579 if Has_Stream_Size_Clause
(U_Ent
) then
1580 Error_Msg_N
("Stream_Size already given for &", Nam
);
1582 elsif Is_Elementary_Type
(U_Ent
) then
1583 if Size
/= System_Storage_Unit
1585 Size
/= System_Storage_Unit
* 2
1587 Size
/= System_Storage_Unit
* 4
1589 Size
/= System_Storage_Unit
* 8
1591 Error_Msg_Uint_1
:= UI_From_Int
(System_Storage_Unit
);
1593 ("stream size for elementary type must be a"
1594 & " power of 2 and at least ^", N
);
1596 elsif RM_Size
(U_Ent
) > Size
then
1597 Error_Msg_Uint_1
:= RM_Size
(U_Ent
);
1599 ("stream size for elementary type must be a"
1600 & " power of 2 and at least ^", N
);
1603 Set_Has_Stream_Size_Clause
(U_Ent
);
1606 Error_Msg_N
("Stream_Size cannot be given for &", Nam
);
1614 -- Value_Size attribute definition clause
1616 when Attribute_Value_Size
=> Value_Size
: declare
1617 Size
: constant Uint
:= Static_Integer
(Expr
);
1621 if not Is_Type
(U_Ent
) then
1622 Error_Msg_N
("Value_Size cannot be given for &", Nam
);
1625 (Get_Attribute_Definition_Clause
1626 (U_Ent
, Attribute_Value_Size
))
1628 Error_Msg_N
("Value_Size already given for &", Nam
);
1630 elsif Is_Array_Type
(U_Ent
)
1631 and then not Is_Constrained
(U_Ent
)
1634 ("Value_Size cannot be given for unconstrained array", Nam
);
1637 if Is_Elementary_Type
(U_Ent
) then
1638 Check_Size
(Expr
, U_Ent
, Size
, Biased
);
1639 Set_Has_Biased_Representation
(U_Ent
, Biased
);
1642 Set_RM_Size
(U_Ent
, Size
);
1650 when Attribute_Write
=>
1651 Analyze_Stream_TSS_Definition
(TSS_Stream_Write
);
1652 Set_Has_Specified_Stream_Write
(Ent
);
1654 -- All other attributes cannot be set
1658 ("attribute& cannot be set with definition clause", N
);
1661 -- The test for the type being frozen must be performed after
1662 -- any expression the clause has been analyzed since the expression
1663 -- itself might cause freezing that makes the clause illegal.
1665 if Rep_Item_Too_Late
(U_Ent
, N
, FOnly
) then
1668 end Analyze_Attribute_Definition_Clause
;
1670 ----------------------------
1671 -- Analyze_Code_Statement --
1672 ----------------------------
1674 procedure Analyze_Code_Statement
(N
: Node_Id
) is
1675 HSS
: constant Node_Id
:= Parent
(N
);
1676 SBody
: constant Node_Id
:= Parent
(HSS
);
1677 Subp
: constant Entity_Id
:= Current_Scope
;
1684 -- Analyze and check we get right type, note that this implements the
1685 -- requirement (RM 13.8(1)) that Machine_Code be with'ed, since that
1686 -- is the only way that Asm_Insn could possibly be visible.
1688 Analyze_And_Resolve
(Expression
(N
));
1690 if Etype
(Expression
(N
)) = Any_Type
then
1692 elsif Etype
(Expression
(N
)) /= RTE
(RE_Asm_Insn
) then
1693 Error_Msg_N
("incorrect type for code statement", N
);
1697 Check_Code_Statement
(N
);
1699 -- Make sure we appear in the handled statement sequence of a
1700 -- subprogram (RM 13.8(3)).
1702 if Nkind
(HSS
) /= N_Handled_Sequence_Of_Statements
1703 or else Nkind
(SBody
) /= N_Subprogram_Body
1706 ("code statement can only appear in body of subprogram", N
);
1710 -- Do remaining checks (RM 13.8(3)) if not already done
1712 if not Is_Machine_Code_Subprogram
(Subp
) then
1713 Set_Is_Machine_Code_Subprogram
(Subp
);
1715 -- No exception handlers allowed
1717 if Present
(Exception_Handlers
(HSS
)) then
1719 ("exception handlers not permitted in machine code subprogram",
1720 First
(Exception_Handlers
(HSS
)));
1723 -- No declarations other than use clauses and pragmas (we allow
1724 -- certain internally generated declarations as well).
1726 Decl
:= First
(Declarations
(SBody
));
1727 while Present
(Decl
) loop
1728 DeclO
:= Original_Node
(Decl
);
1729 if Comes_From_Source
(DeclO
)
1730 and then Nkind
(DeclO
) /= N_Pragma
1731 and then Nkind
(DeclO
) /= N_Use_Package_Clause
1732 and then Nkind
(DeclO
) /= N_Use_Type_Clause
1733 and then Nkind
(DeclO
) /= N_Implicit_Label_Declaration
1736 ("this declaration not allowed in machine code subprogram",
1743 -- No statements other than code statements, pragmas, and labels.
1744 -- Again we allow certain internally generated statements.
1746 Stmt
:= First
(Statements
(HSS
));
1747 while Present
(Stmt
) loop
1748 StmtO
:= Original_Node
(Stmt
);
1749 if Comes_From_Source
(StmtO
)
1750 and then Nkind
(StmtO
) /= N_Pragma
1751 and then Nkind
(StmtO
) /= N_Label
1752 and then Nkind
(StmtO
) /= N_Code_Statement
1755 ("this statement is not allowed in machine code subprogram",
1762 end Analyze_Code_Statement
;
1764 -----------------------------------------------
1765 -- Analyze_Enumeration_Representation_Clause --
1766 -----------------------------------------------
1768 procedure Analyze_Enumeration_Representation_Clause
(N
: Node_Id
) is
1769 Ident
: constant Node_Id
:= Identifier
(N
);
1770 Aggr
: constant Node_Id
:= Array_Aggregate
(N
);
1771 Enumtype
: Entity_Id
;
1777 Err
: Boolean := False;
1779 Lo
: constant Uint
:= Expr_Value
(Type_Low_Bound
(Universal_Integer
));
1780 Hi
: constant Uint
:= Expr_Value
(Type_High_Bound
(Universal_Integer
));
1785 if Ignore_Rep_Clauses
then
1789 -- First some basic error checks
1792 Enumtype
:= Entity
(Ident
);
1794 if Enumtype
= Any_Type
1795 or else Rep_Item_Too_Early
(Enumtype
, N
)
1799 Enumtype
:= Underlying_Type
(Enumtype
);
1802 if not Is_Enumeration_Type
(Enumtype
) then
1804 ("enumeration type required, found}",
1805 Ident
, First_Subtype
(Enumtype
));
1809 -- Ignore rep clause on generic actual type. This will already have
1810 -- been flagged on the template as an error, and this is the safest
1811 -- way to ensure we don't get a junk cascaded message in the instance.
1813 if Is_Generic_Actual_Type
(Enumtype
) then
1816 -- Type must be in current scope
1818 elsif Scope
(Enumtype
) /= Current_Scope
then
1819 Error_Msg_N
("type must be declared in this scope", Ident
);
1822 -- Type must be a first subtype
1824 elsif not Is_First_Subtype
(Enumtype
) then
1825 Error_Msg_N
("cannot give enumeration rep clause for subtype", N
);
1828 -- Ignore duplicate rep clause
1830 elsif Has_Enumeration_Rep_Clause
(Enumtype
) then
1831 Error_Msg_N
("duplicate enumeration rep clause ignored", N
);
1834 -- Don't allow rep clause for standard [wide_[wide_]]character
1836 elsif Root_Type
(Enumtype
) = Standard_Character
1837 or else Root_Type
(Enumtype
) = Standard_Wide_Character
1838 or else Root_Type
(Enumtype
) = Standard_Wide_Wide_Character
1840 Error_Msg_N
("enumeration rep clause not allowed for this type", N
);
1843 -- Check that the expression is a proper aggregate (no parentheses)
1845 elsif Paren_Count
(Aggr
) /= 0 then
1847 ("extra parentheses surrounding aggregate not allowed",
1851 -- All tests passed, so set rep clause in place
1854 Set_Has_Enumeration_Rep_Clause
(Enumtype
);
1855 Set_Has_Enumeration_Rep_Clause
(Base_Type
(Enumtype
));
1858 -- Now we process the aggregate. Note that we don't use the normal
1859 -- aggregate code for this purpose, because we don't want any of the
1860 -- normal expansion activities, and a number of special semantic
1861 -- rules apply (including the component type being any integer type)
1863 Elit
:= First_Literal
(Enumtype
);
1865 -- First the positional entries if any
1867 if Present
(Expressions
(Aggr
)) then
1868 Expr
:= First
(Expressions
(Aggr
));
1869 while Present
(Expr
) loop
1871 Error_Msg_N
("too many entries in aggregate", Expr
);
1875 Val
:= Static_Integer
(Expr
);
1877 -- Err signals that we found some incorrect entries processing
1878 -- the list. The final checks for completeness and ordering are
1879 -- skipped in this case.
1881 if Val
= No_Uint
then
1883 elsif Val
< Lo
or else Hi
< Val
then
1884 Error_Msg_N
("value outside permitted range", Expr
);
1888 Set_Enumeration_Rep
(Elit
, Val
);
1889 Set_Enumeration_Rep_Expr
(Elit
, Expr
);
1895 -- Now process the named entries if present
1897 if Present
(Component_Associations
(Aggr
)) then
1898 Assoc
:= First
(Component_Associations
(Aggr
));
1899 while Present
(Assoc
) loop
1900 Choice
:= First
(Choices
(Assoc
));
1902 if Present
(Next
(Choice
)) then
1904 ("multiple choice not allowed here", Next
(Choice
));
1908 if Nkind
(Choice
) = N_Others_Choice
then
1909 Error_Msg_N
("others choice not allowed here", Choice
);
1912 elsif Nkind
(Choice
) = N_Range
then
1913 -- ??? should allow zero/one element range here
1914 Error_Msg_N
("range not allowed here", Choice
);
1918 Analyze_And_Resolve
(Choice
, Enumtype
);
1920 if Is_Entity_Name
(Choice
)
1921 and then Is_Type
(Entity
(Choice
))
1923 Error_Msg_N
("subtype name not allowed here", Choice
);
1925 -- ??? should allow static subtype with zero/one entry
1927 elsif Etype
(Choice
) = Base_Type
(Enumtype
) then
1928 if not Is_Static_Expression
(Choice
) then
1929 Flag_Non_Static_Expr
1930 ("non-static expression used for choice!", Choice
);
1934 Elit
:= Expr_Value_E
(Choice
);
1936 if Present
(Enumeration_Rep_Expr
(Elit
)) then
1937 Error_Msg_Sloc
:= Sloc
(Enumeration_Rep_Expr
(Elit
));
1939 ("representation for& previously given#",
1944 Set_Enumeration_Rep_Expr
(Elit
, Choice
);
1946 Expr
:= Expression
(Assoc
);
1947 Val
:= Static_Integer
(Expr
);
1949 if Val
= No_Uint
then
1952 elsif Val
< Lo
or else Hi
< Val
then
1953 Error_Msg_N
("value outside permitted range", Expr
);
1957 Set_Enumeration_Rep
(Elit
, Val
);
1966 -- Aggregate is fully processed. Now we check that a full set of
1967 -- representations was given, and that they are in range and in order.
1968 -- These checks are only done if no other errors occurred.
1974 Elit
:= First_Literal
(Enumtype
);
1975 while Present
(Elit
) loop
1976 if No
(Enumeration_Rep_Expr
(Elit
)) then
1977 Error_Msg_NE
("missing representation for&!", N
, Elit
);
1980 Val
:= Enumeration_Rep
(Elit
);
1982 if Min
= No_Uint
then
1986 if Val
/= No_Uint
then
1987 if Max
/= No_Uint
and then Val
<= Max
then
1989 ("enumeration value for& not ordered!",
1990 Enumeration_Rep_Expr
(Elit
), Elit
);
1996 -- If there is at least one literal whose representation
1997 -- is not equal to the Pos value, then note that this
1998 -- enumeration type has a non-standard representation.
2000 if Val
/= Enumeration_Pos
(Elit
) then
2001 Set_Has_Non_Standard_Rep
(Base_Type
(Enumtype
));
2008 -- Now set proper size information
2011 Minsize
: Uint
:= UI_From_Int
(Minimum_Size
(Enumtype
));
2014 if Has_Size_Clause
(Enumtype
) then
2015 if Esize
(Enumtype
) >= Minsize
then
2020 UI_From_Int
(Minimum_Size
(Enumtype
, Biased
=> True));
2022 if Esize
(Enumtype
) < Minsize
then
2023 Error_Msg_N
("previously given size is too small", N
);
2026 Set_Has_Biased_Representation
(Enumtype
);
2031 Set_RM_Size
(Enumtype
, Minsize
);
2032 Set_Enum_Esize
(Enumtype
);
2035 Set_RM_Size
(Base_Type
(Enumtype
), RM_Size
(Enumtype
));
2036 Set_Esize
(Base_Type
(Enumtype
), Esize
(Enumtype
));
2037 Set_Alignment
(Base_Type
(Enumtype
), Alignment
(Enumtype
));
2041 -- We repeat the too late test in case it froze itself!
2043 if Rep_Item_Too_Late
(Enumtype
, N
) then
2046 end Analyze_Enumeration_Representation_Clause
;
2048 ----------------------------
2049 -- Analyze_Free_Statement --
2050 ----------------------------
2052 procedure Analyze_Free_Statement
(N
: Node_Id
) is
2054 Analyze
(Expression
(N
));
2055 end Analyze_Free_Statement
;
2057 ------------------------------------------
2058 -- Analyze_Record_Representation_Clause --
2059 ------------------------------------------
2061 procedure Analyze_Record_Representation_Clause
(N
: Node_Id
) is
2062 Loc
: constant Source_Ptr
:= Sloc
(N
);
2063 Ident
: constant Node_Id
:= Identifier
(N
);
2064 Rectype
: Entity_Id
;
2070 Hbit
: Uint
:= Uint_0
;
2075 Max_Bit_So_Far
: Uint
;
2076 -- Records the maximum bit position so far. If all field positions
2077 -- are monotonically increasing, then we can skip the circuit for
2078 -- checking for overlap, since no overlap is possible.
2080 Overlap_Check_Required
: Boolean;
2081 -- Used to keep track of whether or not an overlap check is required
2083 Ccount
: Natural := 0;
2084 -- Number of component clauses in record rep clause
2086 CR_Pragma
: Node_Id
:= Empty
;
2087 -- Points to N_Pragma node if Complete_Representation pragma present
2090 if Ignore_Rep_Clauses
then
2095 Rectype
:= Entity
(Ident
);
2097 if Rectype
= Any_Type
2098 or else Rep_Item_Too_Early
(Rectype
, N
)
2102 Rectype
:= Underlying_Type
(Rectype
);
2105 -- First some basic error checks
2107 if not Is_Record_Type
(Rectype
) then
2109 ("record type required, found}", Ident
, First_Subtype
(Rectype
));
2112 elsif Is_Unchecked_Union
(Rectype
) then
2114 ("record rep clause not allowed for Unchecked_Union", N
);
2116 elsif Scope
(Rectype
) /= Current_Scope
then
2117 Error_Msg_N
("type must be declared in this scope", N
);
2120 elsif not Is_First_Subtype
(Rectype
) then
2121 Error_Msg_N
("cannot give record rep clause for subtype", N
);
2124 elsif Has_Record_Rep_Clause
(Rectype
) then
2125 Error_Msg_N
("duplicate record rep clause ignored", N
);
2128 elsif Rep_Item_Too_Late
(Rectype
, N
) then
2132 if Present
(Mod_Clause
(N
)) then
2134 Loc
: constant Source_Ptr
:= Sloc
(N
);
2135 M
: constant Node_Id
:= Mod_Clause
(N
);
2136 P
: constant List_Id
:= Pragmas_Before
(M
);
2140 pragma Warnings
(Off
, Mod_Val
);
2143 Check_Restriction
(No_Obsolescent_Features
, Mod_Clause
(N
));
2145 if Warn_On_Obsolescent_Feature
then
2147 ("mod clause is an obsolescent feature (RM J.8)?", N
);
2149 ("\use alignment attribute definition clause instead?", N
);
2156 -- In ASIS_Mode mode, expansion is disabled, but we must convert
2157 -- the Mod clause into an alignment clause anyway, so that the
2158 -- back-end can compute and back-annotate properly the size and
2159 -- alignment of types that may include this record.
2161 -- This seems dubious, this destroys the source tree in a manner
2162 -- not detectable by ASIS ???
2164 if Operating_Mode
= Check_Semantics
2168 Make_Attribute_Definition_Clause
(Loc
,
2169 Name
=> New_Reference_To
(Base_Type
(Rectype
), Loc
),
2170 Chars
=> Name_Alignment
,
2171 Expression
=> Relocate_Node
(Expression
(M
)));
2173 Set_From_At_Mod
(AtM_Nod
);
2174 Insert_After
(N
, AtM_Nod
);
2175 Mod_Val
:= Get_Alignment_Value
(Expression
(AtM_Nod
));
2176 Set_Mod_Clause
(N
, Empty
);
2179 -- Get the alignment value to perform error checking
2181 Mod_Val
:= Get_Alignment_Value
(Expression
(M
));
2187 -- Clear any existing component clauses for the type (this happens with
2188 -- derived types, where we are now overriding the original).
2190 Comp
:= First_Component_Or_Discriminant
(Rectype
);
2191 while Present
(Comp
) loop
2192 Set_Component_Clause
(Comp
, Empty
);
2193 Next_Component_Or_Discriminant
(Comp
);
2196 -- All done if no component clauses
2198 CC
:= First
(Component_Clauses
(N
));
2204 -- If a tag is present, then create a component clause that places it
2205 -- at the start of the record (otherwise gigi may place it after other
2206 -- fields that have rep clauses).
2208 Fent
:= First_Entity
(Rectype
);
2210 if Nkind
(Fent
) = N_Defining_Identifier
2211 and then Chars
(Fent
) = Name_uTag
2213 Set_Component_Bit_Offset
(Fent
, Uint_0
);
2214 Set_Normalized_Position
(Fent
, Uint_0
);
2215 Set_Normalized_First_Bit
(Fent
, Uint_0
);
2216 Set_Normalized_Position_Max
(Fent
, Uint_0
);
2217 Init_Esize
(Fent
, System_Address_Size
);
2219 Set_Component_Clause
(Fent
,
2220 Make_Component_Clause
(Loc
,
2222 Make_Identifier
(Loc
,
2223 Chars
=> Name_uTag
),
2226 Make_Integer_Literal
(Loc
,
2230 Make_Integer_Literal
(Loc
,
2234 Make_Integer_Literal
(Loc
,
2235 UI_From_Int
(System_Address_Size
))));
2237 Ccount
:= Ccount
+ 1;
2240 -- A representation like this applies to the base type
2242 Set_Has_Record_Rep_Clause
(Base_Type
(Rectype
));
2243 Set_Has_Non_Standard_Rep
(Base_Type
(Rectype
));
2244 Set_Has_Specified_Layout
(Base_Type
(Rectype
));
2246 Max_Bit_So_Far
:= Uint_Minus_1
;
2247 Overlap_Check_Required
:= False;
2249 -- Process the component clauses
2251 while Present
(CC
) loop
2255 if Nkind
(CC
) = N_Pragma
then
2258 -- The only pragma of interest is Complete_Representation
2260 if Chars
(CC
) = Name_Complete_Representation
then
2264 -- Processing for real component clause
2267 Ccount
:= Ccount
+ 1;
2268 Posit
:= Static_Integer
(Position
(CC
));
2269 Fbit
:= Static_Integer
(First_Bit
(CC
));
2270 Lbit
:= Static_Integer
(Last_Bit
(CC
));
2273 and then Fbit
/= No_Uint
2274 and then Lbit
/= No_Uint
2278 ("position cannot be negative", Position
(CC
));
2282 ("first bit cannot be negative", First_Bit
(CC
));
2284 -- Values look OK, so find the corresponding record component
2285 -- Even though the syntax allows an attribute reference for
2286 -- implementation-defined components, GNAT does not allow the
2287 -- tag to get an explicit position.
2289 elsif Nkind
(Component_Name
(CC
)) = N_Attribute_Reference
then
2290 if Attribute_Name
(Component_Name
(CC
)) = Name_Tag
then
2291 Error_Msg_N
("position of tag cannot be specified", CC
);
2293 Error_Msg_N
("illegal component name", CC
);
2297 Comp
:= First_Entity
(Rectype
);
2298 while Present
(Comp
) loop
2299 exit when Chars
(Comp
) = Chars
(Component_Name
(CC
));
2305 -- Maybe component of base type that is absent from
2306 -- statically constrained first subtype.
2308 Comp
:= First_Entity
(Base_Type
(Rectype
));
2309 while Present
(Comp
) loop
2310 exit when Chars
(Comp
) = Chars
(Component_Name
(CC
));
2317 ("component clause is for non-existent field", CC
);
2319 elsif Present
(Component_Clause
(Comp
)) then
2320 Error_Msg_Sloc
:= Sloc
(Component_Clause
(Comp
));
2322 ("component clause previously given#", CC
);
2325 -- Make reference for field in record rep clause and set
2326 -- appropriate entity field in the field identifier.
2329 (Comp
, Component_Name
(CC
), Set_Ref
=> False);
2330 Set_Entity
(Component_Name
(CC
), Comp
);
2332 -- Update Fbit and Lbit to the actual bit number
2334 Fbit
:= Fbit
+ UI_From_Int
(SSU
) * Posit
;
2335 Lbit
:= Lbit
+ UI_From_Int
(SSU
) * Posit
;
2337 if Fbit
<= Max_Bit_So_Far
then
2338 Overlap_Check_Required
:= True;
2340 Max_Bit_So_Far
:= Lbit
;
2343 if Has_Size_Clause
(Rectype
)
2344 and then Esize
(Rectype
) <= Lbit
2347 ("bit number out of range of specified size",
2350 Set_Component_Clause
(Comp
, CC
);
2351 Set_Component_Bit_Offset
(Comp
, Fbit
);
2352 Set_Esize
(Comp
, 1 + (Lbit
- Fbit
));
2353 Set_Normalized_First_Bit
(Comp
, Fbit
mod SSU
);
2354 Set_Normalized_Position
(Comp
, Fbit
/ SSU
);
2356 Set_Normalized_Position_Max
2357 (Fent
, Normalized_Position
(Fent
));
2359 if Is_Tagged_Type
(Rectype
)
2360 and then Fbit
< System_Address_Size
2363 ("component overlaps tag field of&",
2367 -- This information is also set in the corresponding
2368 -- component of the base type, found by accessing the
2369 -- Original_Record_Component link if it is present.
2371 Ocomp
:= Original_Record_Component
(Comp
);
2378 (Component_Name
(CC
),
2383 Set_Has_Biased_Representation
(Comp
, Biased
);
2385 if Present
(Ocomp
) then
2386 Set_Component_Clause
(Ocomp
, CC
);
2387 Set_Component_Bit_Offset
(Ocomp
, Fbit
);
2388 Set_Normalized_First_Bit
(Ocomp
, Fbit
mod SSU
);
2389 Set_Normalized_Position
(Ocomp
, Fbit
/ SSU
);
2390 Set_Esize
(Ocomp
, 1 + (Lbit
- Fbit
));
2392 Set_Normalized_Position_Max
2393 (Ocomp
, Normalized_Position
(Ocomp
));
2395 Set_Has_Biased_Representation
2396 (Ocomp
, Has_Biased_Representation
(Comp
));
2399 if Esize
(Comp
) < 0 then
2400 Error_Msg_N
("component size is negative", CC
);
2411 -- Now that we have processed all the component clauses, check for
2412 -- overlap. We have to leave this till last, since the components
2413 -- can appear in any arbitrary order in the representation clause.
2415 -- We do not need this check if all specified ranges were monotonic,
2416 -- as recorded by Overlap_Check_Required being False at this stage.
2418 -- This first section checks if there are any overlapping entries
2419 -- at all. It does this by sorting all entries and then seeing if
2420 -- there are any overlaps. If there are none, then that is decisive,
2421 -- but if there are overlaps, they may still be OK (they may result
2422 -- from fields in different variants).
2424 if Overlap_Check_Required
then
2425 Overlap_Check1
: declare
2427 OC_Fbit
: array (0 .. Ccount
) of Uint
;
2428 -- First-bit values for component clauses, the value is the
2429 -- offset of the first bit of the field from start of record.
2430 -- The zero entry is for use in sorting.
2432 OC_Lbit
: array (0 .. Ccount
) of Uint
;
2433 -- Last-bit values for component clauses, the value is the
2434 -- offset of the last bit of the field from start of record.
2435 -- The zero entry is for use in sorting.
2437 OC_Count
: Natural := 0;
2438 -- Count of entries in OC_Fbit and OC_Lbit
2440 function OC_Lt
(Op1
, Op2
: Natural) return Boolean;
2441 -- Compare routine for Sort (See GNAT.Heap_Sort_A)
2443 procedure OC_Move
(From
: Natural; To
: Natural);
2444 -- Move routine for Sort (see GNAT.Heap_Sort_A)
2446 function OC_Lt
(Op1
, Op2
: Natural) return Boolean is
2448 return OC_Fbit
(Op1
) < OC_Fbit
(Op2
);
2451 procedure OC_Move
(From
: Natural; To
: Natural) is
2453 OC_Fbit
(To
) := OC_Fbit
(From
);
2454 OC_Lbit
(To
) := OC_Lbit
(From
);
2458 CC
:= First
(Component_Clauses
(N
));
2459 while Present
(CC
) loop
2460 if Nkind
(CC
) /= N_Pragma
then
2461 Posit
:= Static_Integer
(Position
(CC
));
2462 Fbit
:= Static_Integer
(First_Bit
(CC
));
2463 Lbit
:= Static_Integer
(Last_Bit
(CC
));
2466 and then Fbit
/= No_Uint
2467 and then Lbit
/= No_Uint
2469 OC_Count
:= OC_Count
+ 1;
2470 Posit
:= Posit
* SSU
;
2471 OC_Fbit
(OC_Count
) := Fbit
+ Posit
;
2472 OC_Lbit
(OC_Count
) := Lbit
+ Posit
;
2481 OC_Move
'Unrestricted_Access,
2482 OC_Lt
'Unrestricted_Access);
2484 Overlap_Check_Required
:= False;
2485 for J
in 1 .. OC_Count
- 1 loop
2486 if OC_Lbit
(J
) >= OC_Fbit
(J
+ 1) then
2487 Overlap_Check_Required
:= True;
2494 -- If Overlap_Check_Required is still True, then we have to do
2495 -- the full scale overlap check, since we have at least two fields
2496 -- that do overlap, and we need to know if that is OK since they
2497 -- are in the same variant, or whether we have a definite problem
2499 if Overlap_Check_Required
then
2500 Overlap_Check2
: declare
2501 C1_Ent
, C2_Ent
: Entity_Id
;
2502 -- Entities of components being checked for overlap
2505 -- Component_List node whose Component_Items are being checked
2508 -- Component declaration for component being checked
2511 C1_Ent
:= First_Entity
(Base_Type
(Rectype
));
2513 -- Loop through all components in record. For each component check
2514 -- for overlap with any of the preceding elements on the component
2515 -- list containing the component, and also, if the component is in
2516 -- a variant, check against components outside the case structure.
2517 -- This latter test is repeated recursively up the variant tree.
2519 Main_Component_Loop
: while Present
(C1_Ent
) loop
2520 if Ekind
(C1_Ent
) /= E_Component
2521 and then Ekind
(C1_Ent
) /= E_Discriminant
2523 goto Continue_Main_Component_Loop
;
2526 -- Skip overlap check if entity has no declaration node. This
2527 -- happens with discriminants in constrained derived types.
2528 -- Probably we are missing some checks as a result, but that
2529 -- does not seem terribly serious ???
2531 if No
(Declaration_Node
(C1_Ent
)) then
2532 goto Continue_Main_Component_Loop
;
2535 Clist
:= Parent
(List_Containing
(Declaration_Node
(C1_Ent
)));
2537 -- Loop through component lists that need checking. Check the
2538 -- current component list and all lists in variants above us.
2540 Component_List_Loop
: loop
2542 -- If derived type definition, go to full declaration
2543 -- If at outer level, check discriminants if there are any
2545 if Nkind
(Clist
) = N_Derived_Type_Definition
then
2546 Clist
:= Parent
(Clist
);
2549 -- Outer level of record definition, check discriminants
2551 if Nkind
(Clist
) = N_Full_Type_Declaration
2552 or else Nkind
(Clist
) = N_Private_Type_Declaration
2554 if Has_Discriminants
(Defining_Identifier
(Clist
)) then
2556 First_Discriminant
(Defining_Identifier
(Clist
));
2558 while Present
(C2_Ent
) loop
2559 exit when C1_Ent
= C2_Ent
;
2560 Check_Component_Overlap
(C1_Ent
, C2_Ent
);
2561 Next_Discriminant
(C2_Ent
);
2565 -- Record extension case
2567 elsif Nkind
(Clist
) = N_Derived_Type_Definition
then
2570 -- Otherwise check one component list
2573 Citem
:= First
(Component_Items
(Clist
));
2575 while Present
(Citem
) loop
2576 if Nkind
(Citem
) = N_Component_Declaration
then
2577 C2_Ent
:= Defining_Identifier
(Citem
);
2578 exit when C1_Ent
= C2_Ent
;
2579 Check_Component_Overlap
(C1_Ent
, C2_Ent
);
2586 -- Check for variants above us (the parent of the Clist can
2587 -- be a variant, in which case its parent is a variant part,
2588 -- and the parent of the variant part is a component list
2589 -- whose components must all be checked against the current
2590 -- component for overlap.
2592 if Nkind
(Parent
(Clist
)) = N_Variant
then
2593 Clist
:= Parent
(Parent
(Parent
(Clist
)));
2595 -- Check for possible discriminant part in record, this is
2596 -- treated essentially as another level in the recursion.
2597 -- For this case we have the parent of the component list
2598 -- is the record definition, and its parent is the full
2599 -- type declaration which contains the discriminant
2602 elsif Nkind
(Parent
(Clist
)) = N_Record_Definition
then
2603 Clist
:= Parent
(Parent
((Clist
)));
2605 -- If neither of these two cases, we are at the top of
2609 exit Component_List_Loop
;
2611 end loop Component_List_Loop
;
2613 <<Continue_Main_Component_Loop
>>
2614 Next_Entity
(C1_Ent
);
2616 end loop Main_Component_Loop
;
2620 -- For records that have component clauses for all components, and
2621 -- whose size is less than or equal to 32, we need to know the size
2622 -- in the front end to activate possible packed array processing
2623 -- where the component type is a record.
2625 -- At this stage Hbit + 1 represents the first unused bit from all
2626 -- the component clauses processed, so if the component clauses are
2627 -- complete, then this is the length of the record.
2629 -- For records longer than System.Storage_Unit, and for those where
2630 -- not all components have component clauses, the back end determines
2631 -- the length (it may for example be appopriate to round up the size
2632 -- to some convenient boundary, based on alignment considerations etc).
2634 if Unknown_RM_Size
(Rectype
)
2635 and then Hbit
+ 1 <= 32
2637 -- Nothing to do if at least one component with no component clause
2639 Comp
:= First_Component_Or_Discriminant
(Rectype
);
2640 while Present
(Comp
) loop
2641 exit when No
(Component_Clause
(Comp
));
2642 Next_Component_Or_Discriminant
(Comp
);
2645 -- If we fall out of loop, all components have component clauses
2646 -- and so we can set the size to the maximum value.
2649 Set_RM_Size
(Rectype
, Hbit
+ 1);
2653 -- Check missing components if Complete_Representation pragma appeared
2655 if Present
(CR_Pragma
) then
2656 Comp
:= First_Component_Or_Discriminant
(Rectype
);
2657 while Present
(Comp
) loop
2658 if No
(Component_Clause
(Comp
)) then
2660 ("missing component clause for &", CR_Pragma
, Comp
);
2663 Next_Component_Or_Discriminant
(Comp
);
2666 -- If no Complete_Representation pragma, warn if missing components
2668 elsif Warn_On_Unrepped_Components
2669 and then not Warnings_Off
(Rectype
)
2672 Num_Repped_Components
: Nat
:= 0;
2673 Num_Unrepped_Components
: Nat
:= 0;
2676 -- First count number of repped and unrepped components
2678 Comp
:= First_Component_Or_Discriminant
(Rectype
);
2679 while Present
(Comp
) loop
2680 if Present
(Component_Clause
(Comp
)) then
2681 Num_Repped_Components
:= Num_Repped_Components
+ 1;
2683 Num_Unrepped_Components
:= Num_Unrepped_Components
+ 1;
2686 Next_Component_Or_Discriminant
(Comp
);
2689 -- We are only interested in the case where there is at least one
2690 -- unrepped component, and at least half the components have rep
2691 -- clauses. We figure that if less than half have them, then the
2692 -- partial rep clause is really intentional.
2694 if Num_Unrepped_Components
> 0
2695 and then Num_Unrepped_Components
< Num_Repped_Components
2697 Comp
:= First_Component_Or_Discriminant
(Rectype
);
2698 while Present
(Comp
) loop
2699 if No
(Component_Clause
(Comp
))
2700 and then (Is_Scalar_Type
(Underlying_Type
(Etype
(Comp
)))
2701 or else Size_Known_At_Compile_Time
2702 (Underlying_Type
(Etype
(Comp
))))
2704 Error_Msg_Sloc
:= Sloc
(Comp
);
2706 ("?no component clause given for & declared #",
2710 Next_Component_Or_Discriminant
(Comp
);
2715 end Analyze_Record_Representation_Clause
;
2717 -----------------------------
2718 -- Check_Component_Overlap --
2719 -----------------------------
2721 procedure Check_Component_Overlap
(C1_Ent
, C2_Ent
: Entity_Id
) is
2723 if Present
(Component_Clause
(C1_Ent
))
2724 and then Present
(Component_Clause
(C2_Ent
))
2726 -- Exclude odd case where we have two tag fields in the same
2727 -- record, both at location zero. This seems a bit strange,
2728 -- but it seems to happen in some circumstances ???
2730 if Chars
(C1_Ent
) = Name_uTag
2731 and then Chars
(C2_Ent
) = Name_uTag
2736 -- Here we check if the two fields overlap
2739 S1
: constant Uint
:= Component_Bit_Offset
(C1_Ent
);
2740 S2
: constant Uint
:= Component_Bit_Offset
(C2_Ent
);
2741 E1
: constant Uint
:= S1
+ Esize
(C1_Ent
);
2742 E2
: constant Uint
:= S2
+ Esize
(C2_Ent
);
2745 if E2
<= S1
or else E1
<= S2
then
2749 Component_Name
(Component_Clause
(C2_Ent
));
2750 Error_Msg_Sloc
:= Sloc
(Error_Msg_Node_2
);
2752 Component_Name
(Component_Clause
(C1_Ent
));
2754 ("component& overlaps & #",
2755 Component_Name
(Component_Clause
(C1_Ent
)));
2759 end Check_Component_Overlap
;
2761 -----------------------------------
2762 -- Check_Constant_Address_Clause --
2763 -----------------------------------
2765 procedure Check_Constant_Address_Clause
2769 procedure Check_At_Constant_Address
(Nod
: Node_Id
);
2770 -- Checks that the given node N represents a name whose 'Address
2771 -- is constant (in the same sense as OK_Constant_Address_Clause,
2772 -- i.e. the address value is the same at the point of declaration
2773 -- of U_Ent and at the time of elaboration of the address clause.
2775 procedure Check_Expr_Constants
(Nod
: Node_Id
);
2776 -- Checks that Nod meets the requirements for a constant address
2777 -- clause in the sense of the enclosing procedure.
2779 procedure Check_List_Constants
(Lst
: List_Id
);
2780 -- Check that all elements of list Lst meet the requirements for a
2781 -- constant address clause in the sense of the enclosing procedure.
2783 -------------------------------
2784 -- Check_At_Constant_Address --
2785 -------------------------------
2787 procedure Check_At_Constant_Address
(Nod
: Node_Id
) is
2789 if Is_Entity_Name
(Nod
) then
2790 if Present
(Address_Clause
(Entity
((Nod
)))) then
2792 ("invalid address clause for initialized object &!",
2795 ("address for& cannot" &
2796 " depend on another address clause! (RM 13.1(22))!",
2799 elsif In_Same_Source_Unit
(Entity
(Nod
), U_Ent
)
2800 and then Sloc
(U_Ent
) < Sloc
(Entity
(Nod
))
2803 ("invalid address clause for initialized object &!",
2805 Error_Msg_Name_1
:= Chars
(Entity
(Nod
));
2806 Error_Msg_Name_2
:= Chars
(U_Ent
);
2808 ("\% must be defined before % (RM 13.1(22))!",
2812 elsif Nkind
(Nod
) = N_Selected_Component
then
2814 T
: constant Entity_Id
:= Etype
(Prefix
(Nod
));
2817 if (Is_Record_Type
(T
)
2818 and then Has_Discriminants
(T
))
2821 and then Is_Record_Type
(Designated_Type
(T
))
2822 and then Has_Discriminants
(Designated_Type
(T
)))
2825 ("invalid address clause for initialized object &!",
2828 ("\address cannot depend on component" &
2829 " of discriminated record (RM 13.1(22))!",
2832 Check_At_Constant_Address
(Prefix
(Nod
));
2836 elsif Nkind
(Nod
) = N_Indexed_Component
then
2837 Check_At_Constant_Address
(Prefix
(Nod
));
2838 Check_List_Constants
(Expressions
(Nod
));
2841 Check_Expr_Constants
(Nod
);
2843 end Check_At_Constant_Address
;
2845 --------------------------
2846 -- Check_Expr_Constants --
2847 --------------------------
2849 procedure Check_Expr_Constants
(Nod
: Node_Id
) is
2850 Loc_U_Ent
: constant Source_Ptr
:= Sloc
(U_Ent
);
2851 Ent
: Entity_Id
:= Empty
;
2854 if Nkind
(Nod
) in N_Has_Etype
2855 and then Etype
(Nod
) = Any_Type
2861 when N_Empty | N_Error
=>
2864 when N_Identifier | N_Expanded_Name
=>
2865 Ent
:= Entity
(Nod
);
2867 -- We need to look at the original node if it is different
2868 -- from the node, since we may have rewritten things and
2869 -- substituted an identifier representing the rewrite.
2871 if Original_Node
(Nod
) /= Nod
then
2872 Check_Expr_Constants
(Original_Node
(Nod
));
2874 -- If the node is an object declaration without initial
2875 -- value, some code has been expanded, and the expression
2876 -- is not constant, even if the constituents might be
2877 -- acceptable, as in A'Address + offset.
2879 if Ekind
(Ent
) = E_Variable
2880 and then Nkind
(Declaration_Node
(Ent
))
2881 = N_Object_Declaration
2883 No
(Expression
(Declaration_Node
(Ent
)))
2886 ("invalid address clause for initialized object &!",
2889 -- If entity is constant, it may be the result of expanding
2890 -- a check. We must verify that its declaration appears
2891 -- before the object in question, else we also reject the
2894 elsif Ekind
(Ent
) = E_Constant
2895 and then In_Same_Source_Unit
(Ent
, U_Ent
)
2896 and then Sloc
(Ent
) > Loc_U_Ent
2899 ("invalid address clause for initialized object &!",
2906 -- Otherwise look at the identifier and see if it is OK
2908 if Ekind
(Ent
) = E_Named_Integer
2910 Ekind
(Ent
) = E_Named_Real
2917 Ekind
(Ent
) = E_Constant
2919 Ekind
(Ent
) = E_In_Parameter
2921 -- This is the case where we must have Ent defined
2922 -- before U_Ent. Clearly if they are in different
2923 -- units this requirement is met since the unit
2924 -- containing Ent is already processed.
2926 if not In_Same_Source_Unit
(Ent
, U_Ent
) then
2929 -- Otherwise location of Ent must be before the
2930 -- location of U_Ent, that's what prior defined means.
2932 elsif Sloc
(Ent
) < Loc_U_Ent
then
2937 ("invalid address clause for initialized object &!",
2939 Error_Msg_Name_1
:= Chars
(Ent
);
2940 Error_Msg_Name_2
:= Chars
(U_Ent
);
2942 ("\% must be defined before % (RM 13.1(22))!",
2946 elsif Nkind
(Original_Node
(Nod
)) = N_Function_Call
then
2947 Check_Expr_Constants
(Original_Node
(Nod
));
2951 ("invalid address clause for initialized object &!",
2954 if Comes_From_Source
(Ent
) then
2955 Error_Msg_Name_1
:= Chars
(Ent
);
2957 ("\reference to variable% not allowed"
2958 & " (RM 13.1(22))!", Nod
);
2961 ("non-static expression not allowed"
2962 & " (RM 13.1(22))!", Nod
);
2966 when N_Integer_Literal
=>
2968 -- If this is a rewritten unchecked conversion, in a system
2969 -- where Address is an integer type, always use the base type
2970 -- for a literal value. This is user-friendly and prevents
2971 -- order-of-elaboration issues with instances of unchecked
2974 if Nkind
(Original_Node
(Nod
)) = N_Function_Call
then
2975 Set_Etype
(Nod
, Base_Type
(Etype
(Nod
)));
2978 when N_Real_Literal |
2980 N_Character_Literal
=>
2984 Check_Expr_Constants
(Low_Bound
(Nod
));
2985 Check_Expr_Constants
(High_Bound
(Nod
));
2987 when N_Explicit_Dereference
=>
2988 Check_Expr_Constants
(Prefix
(Nod
));
2990 when N_Indexed_Component
=>
2991 Check_Expr_Constants
(Prefix
(Nod
));
2992 Check_List_Constants
(Expressions
(Nod
));
2995 Check_Expr_Constants
(Prefix
(Nod
));
2996 Check_Expr_Constants
(Discrete_Range
(Nod
));
2998 when N_Selected_Component
=>
2999 Check_Expr_Constants
(Prefix
(Nod
));
3001 when N_Attribute_Reference
=>
3002 if Attribute_Name
(Nod
) = Name_Address
3004 Attribute_Name
(Nod
) = Name_Access
3006 Attribute_Name
(Nod
) = Name_Unchecked_Access
3008 Attribute_Name
(Nod
) = Name_Unrestricted_Access
3010 Check_At_Constant_Address
(Prefix
(Nod
));
3013 Check_Expr_Constants
(Prefix
(Nod
));
3014 Check_List_Constants
(Expressions
(Nod
));
3018 Check_List_Constants
(Component_Associations
(Nod
));
3019 Check_List_Constants
(Expressions
(Nod
));
3021 when N_Component_Association
=>
3022 Check_Expr_Constants
(Expression
(Nod
));
3024 when N_Extension_Aggregate
=>
3025 Check_Expr_Constants
(Ancestor_Part
(Nod
));
3026 Check_List_Constants
(Component_Associations
(Nod
));
3027 Check_List_Constants
(Expressions
(Nod
));
3032 when N_Binary_Op | N_And_Then | N_Or_Else | N_Membership_Test
=>
3033 Check_Expr_Constants
(Left_Opnd
(Nod
));
3034 Check_Expr_Constants
(Right_Opnd
(Nod
));
3037 Check_Expr_Constants
(Right_Opnd
(Nod
));
3039 when N_Type_Conversion |
3040 N_Qualified_Expression |
3042 Check_Expr_Constants
(Expression
(Nod
));
3044 when N_Unchecked_Type_Conversion
=>
3045 Check_Expr_Constants
(Expression
(Nod
));
3047 -- If this is a rewritten unchecked conversion, subtypes
3048 -- in this node are those created within the instance.
3049 -- To avoid order of elaboration issues, replace them
3050 -- with their base types. Note that address clauses can
3051 -- cause order of elaboration problems because they are
3052 -- elaborated by the back-end at the point of definition,
3053 -- and may mention entities declared in between (as long
3054 -- as everything is static). It is user-friendly to allow
3055 -- unchecked conversions in this context.
3057 if Nkind
(Original_Node
(Nod
)) = N_Function_Call
then
3058 Set_Etype
(Expression
(Nod
),
3059 Base_Type
(Etype
(Expression
(Nod
))));
3060 Set_Etype
(Nod
, Base_Type
(Etype
(Nod
)));
3063 when N_Function_Call
=>
3064 if not Is_Pure
(Entity
(Name
(Nod
))) then
3066 ("invalid address clause for initialized object &!",
3070 ("\function & is not pure (RM 13.1(22))!",
3071 Nod
, Entity
(Name
(Nod
)));
3074 Check_List_Constants
(Parameter_Associations
(Nod
));
3077 when N_Parameter_Association
=>
3078 Check_Expr_Constants
(Explicit_Actual_Parameter
(Nod
));
3082 ("invalid address clause for initialized object &!",
3085 ("\must be constant defined before& (RM 13.1(22))!",
3088 end Check_Expr_Constants
;
3090 --------------------------
3091 -- Check_List_Constants --
3092 --------------------------
3094 procedure Check_List_Constants
(Lst
: List_Id
) is
3098 if Present
(Lst
) then
3099 Nod1
:= First
(Lst
);
3100 while Present
(Nod1
) loop
3101 Check_Expr_Constants
(Nod1
);
3105 end Check_List_Constants
;
3107 -- Start of processing for Check_Constant_Address_Clause
3110 Check_Expr_Constants
(Expr
);
3111 end Check_Constant_Address_Clause
;
3117 procedure Check_Size
3121 Biased
: out Boolean)
3123 UT
: constant Entity_Id
:= Underlying_Type
(T
);
3129 -- Dismiss cases for generic types or types with previous errors
3132 or else UT
= Any_Type
3133 or else Is_Generic_Type
(UT
)
3134 or else Is_Generic_Type
(Root_Type
(UT
))
3138 -- Check case of bit packed array
3140 elsif Is_Array_Type
(UT
)
3141 and then Known_Static_Component_Size
(UT
)
3142 and then Is_Bit_Packed_Array
(UT
)
3150 Asiz
:= Component_Size
(UT
);
3151 Indx
:= First_Index
(UT
);
3153 Ityp
:= Etype
(Indx
);
3155 -- If non-static bound, then we are not in the business of
3156 -- trying to check the length, and indeed an error will be
3157 -- issued elsewhere, since sizes of non-static array types
3158 -- cannot be set implicitly or explicitly.
3160 if not Is_Static_Subtype
(Ityp
) then
3164 -- Otherwise accumulate next dimension
3166 Asiz
:= Asiz
* (Expr_Value
(Type_High_Bound
(Ityp
)) -
3167 Expr_Value
(Type_Low_Bound
(Ityp
)) +
3171 exit when No
(Indx
);
3177 Error_Msg_Uint_1
:= Asiz
;
3179 ("size for& too small, minimum allowed is ^", N
, T
);
3180 Set_Esize
(T
, Asiz
);
3181 Set_RM_Size
(T
, Asiz
);
3185 -- All other composite types are ignored
3187 elsif Is_Composite_Type
(UT
) then
3190 -- For fixed-point types, don't check minimum if type is not frozen,
3191 -- since we don't know all the characteristics of the type that can
3192 -- affect the size (e.g. a specified small) till freeze time.
3194 elsif Is_Fixed_Point_Type
(UT
)
3195 and then not Is_Frozen
(UT
)
3199 -- Cases for which a minimum check is required
3202 -- Ignore if specified size is correct for the type
3204 if Known_Esize
(UT
) and then Siz
= Esize
(UT
) then
3208 -- Otherwise get minimum size
3210 M
:= UI_From_Int
(Minimum_Size
(UT
));
3214 -- Size is less than minimum size, but one possibility remains
3215 -- that we can manage with the new size if we bias the type
3217 M
:= UI_From_Int
(Minimum_Size
(UT
, Biased
=> True));
3220 Error_Msg_Uint_1
:= M
;
3222 ("size for& too small, minimum allowed is ^", N
, T
);
3232 -------------------------
3233 -- Get_Alignment_Value --
3234 -------------------------
3236 function Get_Alignment_Value
(Expr
: Node_Id
) return Uint
is
3237 Align
: constant Uint
:= Static_Integer
(Expr
);
3240 if Align
= No_Uint
then
3243 elsif Align
<= 0 then
3244 Error_Msg_N
("alignment value must be positive", Expr
);
3248 for J
in Int
range 0 .. 64 loop
3250 M
: constant Uint
:= Uint_2
** J
;
3253 exit when M
= Align
;
3257 ("alignment value must be power of 2", Expr
);
3265 end Get_Alignment_Value
;
3271 procedure Initialize
is
3273 Unchecked_Conversions
.Init
;
3276 -------------------------
3277 -- Is_Operational_Item --
3278 -------------------------
3280 function Is_Operational_Item
(N
: Node_Id
) return Boolean is
3282 if Nkind
(N
) /= N_Attribute_Definition_Clause
then
3286 Id
: constant Attribute_Id
:= Get_Attribute_Id
(Chars
(N
));
3289 return Id
= Attribute_Input
3290 or else Id
= Attribute_Output
3291 or else Id
= Attribute_Read
3292 or else Id
= Attribute_Write
3293 or else Id
= Attribute_External_Tag
;
3296 end Is_Operational_Item
;
3302 function Minimum_Size
3304 Biased
: Boolean := False) return Nat
3306 Lo
: Uint
:= No_Uint
;
3307 Hi
: Uint
:= No_Uint
;
3308 LoR
: Ureal
:= No_Ureal
;
3309 HiR
: Ureal
:= No_Ureal
;
3310 LoSet
: Boolean := False;
3311 HiSet
: Boolean := False;
3315 R_Typ
: constant Entity_Id
:= Root_Type
(T
);
3318 -- If bad type, return 0
3320 if T
= Any_Type
then
3323 -- For generic types, just return zero. There cannot be any legitimate
3324 -- need to know such a size, but this routine may be called with a
3325 -- generic type as part of normal processing.
3327 elsif Is_Generic_Type
(R_Typ
)
3328 or else R_Typ
= Any_Type
3332 -- Access types. Normally an access type cannot have a size smaller
3333 -- than the size of System.Address. The exception is on VMS, where
3334 -- we have short and long addresses, and it is possible for an access
3335 -- type to have a short address size (and thus be less than the size
3336 -- of System.Address itself). We simply skip the check for VMS, and
3337 -- leave the back end to do the check.
3339 elsif Is_Access_Type
(T
) then
3340 if OpenVMS_On_Target
then
3343 return System_Address_Size
;
3346 -- Floating-point types
3348 elsif Is_Floating_Point_Type
(T
) then
3349 return UI_To_Int
(Esize
(R_Typ
));
3353 elsif Is_Discrete_Type
(T
) then
3355 -- The following loop is looking for the nearest compile time
3356 -- known bounds following the ancestor subtype chain. The idea
3357 -- is to find the most restrictive known bounds information.
3361 if Ancest
= Any_Type
or else Etype
(Ancest
) = Any_Type
then
3366 if Compile_Time_Known_Value
(Type_Low_Bound
(Ancest
)) then
3367 Lo
:= Expr_Rep_Value
(Type_Low_Bound
(Ancest
));
3374 if Compile_Time_Known_Value
(Type_High_Bound
(Ancest
)) then
3375 Hi
:= Expr_Rep_Value
(Type_High_Bound
(Ancest
));
3381 Ancest
:= Ancestor_Subtype
(Ancest
);
3384 Ancest
:= Base_Type
(T
);
3386 if Is_Generic_Type
(Ancest
) then
3392 -- Fixed-point types. We can't simply use Expr_Value to get the
3393 -- Corresponding_Integer_Value values of the bounds, since these
3394 -- do not get set till the type is frozen, and this routine can
3395 -- be called before the type is frozen. Similarly the test for
3396 -- bounds being static needs to include the case where we have
3397 -- unanalyzed real literals for the same reason.
3399 elsif Is_Fixed_Point_Type
(T
) then
3401 -- The following loop is looking for the nearest compile time
3402 -- known bounds following the ancestor subtype chain. The idea
3403 -- is to find the most restrictive known bounds information.
3407 if Ancest
= Any_Type
or else Etype
(Ancest
) = Any_Type
then
3412 if Nkind
(Type_Low_Bound
(Ancest
)) = N_Real_Literal
3413 or else Compile_Time_Known_Value
(Type_Low_Bound
(Ancest
))
3415 LoR
:= Expr_Value_R
(Type_Low_Bound
(Ancest
));
3422 if Nkind
(Type_High_Bound
(Ancest
)) = N_Real_Literal
3423 or else Compile_Time_Known_Value
(Type_High_Bound
(Ancest
))
3425 HiR
:= Expr_Value_R
(Type_High_Bound
(Ancest
));
3431 Ancest
:= Ancestor_Subtype
(Ancest
);
3434 Ancest
:= Base_Type
(T
);
3436 if Is_Generic_Type
(Ancest
) then
3442 Lo
:= UR_To_Uint
(LoR
/ Small_Value
(T
));
3443 Hi
:= UR_To_Uint
(HiR
/ Small_Value
(T
));
3445 -- No other types allowed
3448 raise Program_Error
;
3451 -- Fall through with Hi and Lo set. Deal with biased case
3453 if (Biased
and then not Is_Fixed_Point_Type
(T
))
3454 or else Has_Biased_Representation
(T
)
3460 -- Signed case. Note that we consider types like range 1 .. -1 to be
3461 -- signed for the purpose of computing the size, since the bounds
3462 -- have to be accomodated in the base type.
3464 if Lo
< 0 or else Hi
< 0 then
3468 -- S = size, B = 2 ** (size - 1) (can accommodate -B .. +(B - 1))
3469 -- Note that we accommodate the case where the bounds cross. This
3470 -- can happen either because of the way the bounds are declared
3471 -- or because of the algorithm in Freeze_Fixed_Point_Type.
3485 -- If both bounds are positive, make sure that both are represen-
3486 -- table in the case where the bounds are crossed. This can happen
3487 -- either because of the way the bounds are declared, or because of
3488 -- the algorithm in Freeze_Fixed_Point_Type.
3494 -- S = size, (can accommodate 0 .. (2**size - 1))
3497 while Hi
>= Uint_2
** S
loop
3505 ---------------------------
3506 -- New_Stream_Subprogram --
3507 ---------------------------
3509 procedure New_Stream_Subprogram
3513 Nam
: TSS_Name_Type
)
3515 Loc
: constant Source_Ptr
:= Sloc
(N
);
3516 Sname
: constant Name_Id
:= Make_TSS_Name
(Base_Type
(Ent
), Nam
);
3517 Subp_Id
: Entity_Id
;
3518 Subp_Decl
: Node_Id
;
3522 Defer_Declaration
: constant Boolean :=
3523 Is_Tagged_Type
(Ent
) or else Is_Private_Type
(Ent
);
3524 -- For a tagged type, there is a declaration for each stream attribute
3525 -- at the freeze point, and we must generate only a completion of this
3526 -- declaration. We do the same for private types, because the full view
3527 -- might be tagged. Otherwise we generate a declaration at the point of
3528 -- the attribute definition clause.
3530 function Build_Spec
return Node_Id
;
3531 -- Used for declaration and renaming declaration, so that this is
3532 -- treated as a renaming_as_body.
3538 function Build_Spec
return Node_Id
is
3539 Out_P
: constant Boolean := (Nam
= TSS_Stream_Read
);
3542 T_Ref
: constant Node_Id
:= New_Reference_To
(Etyp
, Loc
);
3545 Subp_Id
:= Make_Defining_Identifier
(Loc
, Sname
);
3547 -- S : access Root_Stream_Type'Class
3549 Formals
:= New_List
(
3550 Make_Parameter_Specification
(Loc
,
3551 Defining_Identifier
=>
3552 Make_Defining_Identifier
(Loc
, Name_S
),
3554 Make_Access_Definition
(Loc
,
3557 Designated_Type
(Etype
(F
)), Loc
))));
3559 if Nam
= TSS_Stream_Input
then
3560 Spec
:= Make_Function_Specification
(Loc
,
3561 Defining_Unit_Name
=> Subp_Id
,
3562 Parameter_Specifications
=> Formals
,
3563 Result_Definition
=> T_Ref
);
3568 Make_Parameter_Specification
(Loc
,
3569 Defining_Identifier
=> Make_Defining_Identifier
(Loc
, Name_V
),
3570 Out_Present
=> Out_P
,
3571 Parameter_Type
=> T_Ref
));
3573 Spec
:= Make_Procedure_Specification
(Loc
,
3574 Defining_Unit_Name
=> Subp_Id
,
3575 Parameter_Specifications
=> Formals
);
3581 -- Start of processing for New_Stream_Subprogram
3584 F
:= First_Formal
(Subp
);
3586 if Ekind
(Subp
) = E_Procedure
then
3587 Etyp
:= Etype
(Next_Formal
(F
));
3589 Etyp
:= Etype
(Subp
);
3592 -- Prepare subprogram declaration and insert it as an action on the
3593 -- clause node. The visibility for this entity is used to test for
3594 -- visibility of the attribute definition clause (in the sense of
3595 -- 8.3(23) as amended by AI-195).
3597 if not Defer_Declaration
then
3599 Make_Subprogram_Declaration
(Loc
,
3600 Specification
=> Build_Spec
);
3602 -- For a tagged type, there is always a visible declaration for each
3603 -- stream TSS (it is a predefined primitive operation), and the
3604 -- completion of this declaration occurs at the freeze point, which is
3605 -- not always visible at places where the attribute definition clause is
3606 -- visible. So, we create a dummy entity here for the purpose of
3607 -- tracking the visibility of the attribute definition clause itself.
3611 Make_Defining_Identifier
(Loc
,
3612 Chars
=> New_External_Name
(Sname
, 'V'));
3614 Make_Object_Declaration
(Loc
,
3615 Defining_Identifier
=> Subp_Id
,
3616 Object_Definition
=> New_Occurrence_Of
(Standard_Boolean
, Loc
));
3619 Insert_Action
(N
, Subp_Decl
);
3620 Set_Entity
(N
, Subp_Id
);
3623 Make_Subprogram_Renaming_Declaration
(Loc
,
3624 Specification
=> Build_Spec
,
3625 Name
=> New_Reference_To
(Subp
, Loc
));
3627 if Defer_Declaration
then
3628 Set_TSS
(Base_Type
(Ent
), Subp_Id
);
3630 Insert_Action
(N
, Subp_Decl
);
3631 Copy_TSS
(Subp_Id
, Base_Type
(Ent
));
3633 end New_Stream_Subprogram
;
3635 ------------------------
3636 -- Rep_Item_Too_Early --
3637 ------------------------
3639 function Rep_Item_Too_Early
(T
: Entity_Id
; N
: Node_Id
) return Boolean is
3641 -- Cannot apply non-operational rep items to generic types
3643 if Is_Operational_Item
(N
) then
3647 and then Is_Generic_Type
(Root_Type
(T
))
3650 ("representation item not allowed for generic type", N
);
3654 -- Otherwise check for incompleted type
3656 if Is_Incomplete_Or_Private_Type
(T
)
3657 and then No
(Underlying_Type
(T
))
3660 ("representation item must be after full type declaration", N
);
3663 -- If the type has incompleted components, a representation clause is
3664 -- illegal but stream attributes and Convention pragmas are correct.
3666 elsif Has_Private_Component
(T
) then
3667 if Nkind
(N
) = N_Pragma
then
3671 ("representation item must appear after type is fully defined",
3678 end Rep_Item_Too_Early
;
3680 -----------------------
3681 -- Rep_Item_Too_Late --
3682 -----------------------
3684 function Rep_Item_Too_Late
3687 FOnly
: Boolean := False) return Boolean
3690 Parent_Type
: Entity_Id
;
3693 -- Output the too late message. Note that this is not considered a
3694 -- serious error, since the effect is simply that we ignore the
3695 -- representation clause in this case.
3701 procedure Too_Late
is
3703 Error_Msg_N
("|representation item appears too late!", N
);
3706 -- Start of processing for Rep_Item_Too_Late
3709 -- First make sure entity is not frozen (RM 13.1(9)). Exclude imported
3710 -- types, which may be frozen if they appear in a representation clause
3711 -- for a local type.
3714 and then not From_With_Type
(T
)
3717 S
:= First_Subtype
(T
);
3719 if Present
(Freeze_Node
(S
)) then
3721 ("?no more representation items for }", Freeze_Node
(S
), S
);
3726 -- Check for case of non-tagged derived type whose parent either has
3727 -- primitive operations, or is a by reference type (RM 13.1(10)).
3731 and then Is_Derived_Type
(T
)
3732 and then not Is_Tagged_Type
(T
)
3734 Parent_Type
:= Etype
(Base_Type
(T
));
3736 if Has_Primitive_Operations
(Parent_Type
) then
3739 ("primitive operations already defined for&!", N
, Parent_Type
);
3742 elsif Is_By_Reference_Type
(Parent_Type
) then
3745 ("parent type & is a by reference type!", N
, Parent_Type
);
3750 -- No error, link item into head of chain of rep items for the entity
3752 Record_Rep_Item
(T
, N
);
3754 end Rep_Item_Too_Late
;
3756 -------------------------
3757 -- Same_Representation --
3758 -------------------------
3760 function Same_Representation
(Typ1
, Typ2
: Entity_Id
) return Boolean is
3761 T1
: constant Entity_Id
:= Underlying_Type
(Typ1
);
3762 T2
: constant Entity_Id
:= Underlying_Type
(Typ2
);
3765 -- A quick check, if base types are the same, then we definitely have
3766 -- the same representation, because the subtype specific representation
3767 -- attributes (Size and Alignment) do not affect representation from
3768 -- the point of view of this test.
3770 if Base_Type
(T1
) = Base_Type
(T2
) then
3773 elsif Is_Private_Type
(Base_Type
(T2
))
3774 and then Base_Type
(T1
) = Full_View
(Base_Type
(T2
))
3779 -- Tagged types never have differing representations
3781 if Is_Tagged_Type
(T1
) then
3785 -- Representations are definitely different if conventions differ
3787 if Convention
(T1
) /= Convention
(T2
) then
3791 -- Representations are different if component alignments differ
3793 if (Is_Record_Type
(T1
) or else Is_Array_Type
(T1
))
3795 (Is_Record_Type
(T2
) or else Is_Array_Type
(T2
))
3796 and then Component_Alignment
(T1
) /= Component_Alignment
(T2
)
3801 -- For arrays, the only real issue is component size. If we know the
3802 -- component size for both arrays, and it is the same, then that's
3803 -- good enough to know we don't have a change of representation.
3805 if Is_Array_Type
(T1
) then
3806 if Known_Component_Size
(T1
)
3807 and then Known_Component_Size
(T2
)
3808 and then Component_Size
(T1
) = Component_Size
(T2
)
3814 -- Types definitely have same representation if neither has non-standard
3815 -- representation since default representations are always consistent.
3816 -- If only one has non-standard representation, and the other does not,
3817 -- then we consider that they do not have the same representation. They
3818 -- might, but there is no way of telling early enough.
3820 if Has_Non_Standard_Rep
(T1
) then
3821 if not Has_Non_Standard_Rep
(T2
) then
3825 return not Has_Non_Standard_Rep
(T2
);
3828 -- Here the two types both have non-standard representation, and we
3829 -- need to determine if they have the same non-standard representation
3831 -- For arrays, we simply need to test if the component sizes are the
3832 -- same. Pragma Pack is reflected in modified component sizes, so this
3833 -- check also deals with pragma Pack.
3835 if Is_Array_Type
(T1
) then
3836 return Component_Size
(T1
) = Component_Size
(T2
);
3838 -- Tagged types always have the same representation, because it is not
3839 -- possible to specify different representations for common fields.
3841 elsif Is_Tagged_Type
(T1
) then
3844 -- Case of record types
3846 elsif Is_Record_Type
(T1
) then
3848 -- Packed status must conform
3850 if Is_Packed
(T1
) /= Is_Packed
(T2
) then
3853 -- Otherwise we must check components. Typ2 maybe a constrained
3854 -- subtype with fewer components, so we compare the components
3855 -- of the base types.
3858 Record_Case
: declare
3859 CD1
, CD2
: Entity_Id
;
3861 function Same_Rep
return Boolean;
3862 -- CD1 and CD2 are either components or discriminants. This
3863 -- function tests whether the two have the same representation
3869 function Same_Rep
return Boolean is
3871 if No
(Component_Clause
(CD1
)) then
3872 return No
(Component_Clause
(CD2
));
3876 Present
(Component_Clause
(CD2
))
3878 Component_Bit_Offset
(CD1
) = Component_Bit_Offset
(CD2
)
3880 Esize
(CD1
) = Esize
(CD2
);
3884 -- Start processing for Record_Case
3887 if Has_Discriminants
(T1
) then
3888 CD1
:= First_Discriminant
(T1
);
3889 CD2
:= First_Discriminant
(T2
);
3891 -- The number of discriminants may be different if the
3892 -- derived type has fewer (constrained by values). The
3893 -- invisible discriminants retain the representation of
3894 -- the original, so the discrepancy does not per se
3895 -- indicate a different representation.
3898 and then Present
(CD2
)
3900 if not Same_Rep
then
3903 Next_Discriminant
(CD1
);
3904 Next_Discriminant
(CD2
);
3909 CD1
:= First_Component
(Underlying_Type
(Base_Type
(T1
)));
3910 CD2
:= First_Component
(Underlying_Type
(Base_Type
(T2
)));
3912 while Present
(CD1
) loop
3913 if not Same_Rep
then
3916 Next_Component
(CD1
);
3917 Next_Component
(CD2
);
3925 -- For enumeration types, we must check each literal to see if the
3926 -- representation is the same. Note that we do not permit enumeration
3927 -- reprsentation clauses for Character and Wide_Character, so these
3928 -- cases were already dealt with.
3930 elsif Is_Enumeration_Type
(T1
) then
3932 Enumeration_Case
: declare
3936 L1
:= First_Literal
(T1
);
3937 L2
:= First_Literal
(T2
);
3939 while Present
(L1
) loop
3940 if Enumeration_Rep
(L1
) /= Enumeration_Rep
(L2
) then
3950 end Enumeration_Case
;
3952 -- Any other types have the same representation for these purposes
3957 end Same_Representation
;
3959 --------------------
3960 -- Set_Enum_Esize --
3961 --------------------
3963 procedure Set_Enum_Esize
(T
: Entity_Id
) is
3971 -- Find the minimum standard size (8,16,32,64) that fits
3973 Lo
:= Enumeration_Rep
(Entity
(Type_Low_Bound
(T
)));
3974 Hi
:= Enumeration_Rep
(Entity
(Type_High_Bound
(T
)));
3977 if Lo
>= -Uint_2
**07 and then Hi
< Uint_2
**07 then
3978 Sz
:= Standard_Character_Size
; -- May be > 8 on some targets
3980 elsif Lo
>= -Uint_2
**15 and then Hi
< Uint_2
**15 then
3983 elsif Lo
>= -Uint_2
**31 and then Hi
< Uint_2
**31 then
3986 else pragma Assert
(Lo
>= -Uint_2
**63 and then Hi
< Uint_2
**63);
3991 if Hi
< Uint_2
**08 then
3992 Sz
:= Standard_Character_Size
; -- May be > 8 on some targets
3994 elsif Hi
< Uint_2
**16 then
3997 elsif Hi
< Uint_2
**32 then
4000 else pragma Assert
(Hi
< Uint_2
**63);
4005 -- That minimum is the proper size unless we have a foreign convention
4006 -- and the size required is 32 or less, in which case we bump the size
4007 -- up to 32. This is required for C and C++ and seems reasonable for
4008 -- all other foreign conventions.
4010 if Has_Foreign_Convention
(T
)
4011 and then Esize
(T
) < Standard_Integer_Size
4013 Init_Esize
(T
, Standard_Integer_Size
);
4019 ------------------------------
4020 -- Validate_Address_Clauses --
4021 ------------------------------
4023 procedure Validate_Address_Clauses
is
4025 for J
in Address_Clause_Checks
.First
.. Address_Clause_Checks
.Last
loop
4027 ACCR
: Address_Clause_Check_Record
4028 renames Address_Clause_Checks
.Table
(J
);
4037 -- Skip processing of this entry if warning already posted
4039 if not Address_Warning_Posted
(ACCR
.N
) then
4041 -- Get alignments. Really we should always have the alignment
4042 -- of the objects properly back annotated, but right now the
4043 -- back end fails to back annotate for address clauses???
4045 if Known_Alignment
(ACCR
.X
) then
4046 X_Alignment
:= Alignment
(ACCR
.X
);
4048 X_Alignment
:= Alignment
(Etype
(ACCR
.X
));
4051 if Known_Alignment
(ACCR
.Y
) then
4052 Y_Alignment
:= Alignment
(ACCR
.Y
);
4054 Y_Alignment
:= Alignment
(Etype
(ACCR
.Y
));
4057 -- Similarly obtain sizes
4059 if Known_Esize
(ACCR
.X
) then
4060 X_Size
:= Esize
(ACCR
.X
);
4062 X_Size
:= Esize
(Etype
(ACCR
.X
));
4065 if Known_Esize
(ACCR
.Y
) then
4066 Y_Size
:= Esize
(ACCR
.Y
);
4068 Y_Size
:= Esize
(Etype
(ACCR
.Y
));
4071 -- Check for large object overlaying smaller one
4074 and then X_Size
> Uint_0
4075 and then X_Size
> Y_Size
4078 ("?size for overlaid object is too small", ACCR
.N
);
4079 Error_Msg_Uint_1
:= X_Size
;
4081 ("\?size of & is ^", ACCR
.N
, ACCR
.X
);
4082 Error_Msg_Uint_1
:= Y_Size
;
4084 ("\?size of & is ^", ACCR
.N
, ACCR
.Y
);
4086 -- Check for inadequate alignment. Again the defensive check
4087 -- on Y_Alignment should not be needed, but because of the
4088 -- failure in back end annotation, we can have an alignment
4091 -- Note: we do not check alignments if we gave a size
4092 -- warning, since it would likely be redundant.
4094 elsif Y_Alignment
/= Uint_0
4095 and then Y_Alignment
< X_Alignment
4098 ("?specified address for& may be inconsistent "
4102 ("\?program execution may be erroneous (RM 13.3(27))",
4104 Error_Msg_Uint_1
:= X_Alignment
;
4106 ("\?alignment of & is ^",
4108 Error_Msg_Uint_1
:= Y_Alignment
;
4110 ("\?alignment of & is ^",
4116 end Validate_Address_Clauses
;
4118 -----------------------------------
4119 -- Validate_Unchecked_Conversion --
4120 -----------------------------------
4122 procedure Validate_Unchecked_Conversion
4124 Act_Unit
: Entity_Id
)
4131 -- Obtain source and target types. Note that we call Ancestor_Subtype
4132 -- here because the processing for generic instantiation always makes
4133 -- subtypes, and we want the original frozen actual types.
4135 -- If we are dealing with private types, then do the check on their
4136 -- fully declared counterparts if the full declarations have been
4137 -- encountered (they don't have to be visible, but they must exist!)
4139 Source
:= Ancestor_Subtype
(Etype
(First_Formal
(Act_Unit
)));
4141 if Is_Private_Type
(Source
)
4142 and then Present
(Underlying_Type
(Source
))
4144 Source
:= Underlying_Type
(Source
);
4147 Target
:= Ancestor_Subtype
(Etype
(Act_Unit
));
4149 -- If either type is generic, the instantiation happens within a
4150 -- generic unit, and there is nothing to check. The proper check
4151 -- will happen when the enclosing generic is instantiated.
4153 if Is_Generic_Type
(Source
) or else Is_Generic_Type
(Target
) then
4157 if Is_Private_Type
(Target
)
4158 and then Present
(Underlying_Type
(Target
))
4160 Target
:= Underlying_Type
(Target
);
4163 -- Source may be unconstrained array, but not target
4165 if Is_Array_Type
(Target
)
4166 and then not Is_Constrained
(Target
)
4169 ("unchecked conversion to unconstrained array not allowed", N
);
4173 -- Warn if conversion between two different convention pointers
4175 if Is_Access_Type
(Target
)
4176 and then Is_Access_Type
(Source
)
4177 and then Convention
(Target
) /= Convention
(Source
)
4178 and then Warn_On_Unchecked_Conversion
4181 ("?conversion between pointers with different conventions!", N
);
4184 -- Make entry in unchecked conversion table for later processing
4185 -- by Validate_Unchecked_Conversions, which will check sizes and
4186 -- alignments (using values set by the back-end where possible).
4187 -- This is only done if the appropriate warning is active
4189 if Warn_On_Unchecked_Conversion
then
4190 Unchecked_Conversions
.Append
4191 (New_Val
=> UC_Entry
'
4196 -- If both sizes are known statically now, then back end annotation
4197 -- is not required to do a proper check but if either size is not
4198 -- known statically, then we need the annotation.
4200 if Known_Static_RM_Size (Source)
4201 and then Known_Static_RM_Size (Target)
4205 Back_Annotate_Rep_Info := True;
4209 -- If unchecked conversion to access type, and access type is
4210 -- declared in the same unit as the unchecked conversion, then
4211 -- set the No_Strict_Aliasing flag (no strict aliasing is
4212 -- implicit in this situation).
4214 if Is_Access_Type (Target) and then
4215 In_Same_Source_Unit (Target, N)
4217 Set_No_Strict_Aliasing (Implementation_Base_Type (Target));
4220 -- Generate N_Validate_Unchecked_Conversion node for back end in
4221 -- case the back end needs to perform special validation checks.
4223 -- Shouldn't this be in exp_ch13, since the check only gets done
4224 -- if we have full expansion and the back end is called ???
4227 Make_Validate_Unchecked_Conversion (Sloc (N));
4228 Set_Source_Type (Vnode, Source);
4229 Set_Target_Type (Vnode, Target);
4231 -- If the unchecked conversion node is in a list, just insert before
4232 -- it. If not we have some strange case, not worth bothering about.
4234 if Is_List_Member (N) then
4235 Insert_After (N, Vnode);
4237 end Validate_Unchecked_Conversion;
4239 ------------------------------------
4240 -- Validate_Unchecked_Conversions --
4241 ------------------------------------
4243 procedure Validate_Unchecked_Conversions is
4245 for N in Unchecked_Conversions.First .. Unchecked_Conversions.Last loop
4247 T : UC_Entry renames Unchecked_Conversions.Table (N);
4249 Enode : constant Node_Id := T.Enode;
4250 Source : constant Entity_Id := T.Source;
4251 Target : constant Entity_Id := T.Target;
4257 -- This validation check, which warns if we have unequal sizes
4258 -- for unchecked conversion, and thus potentially implementation
4259 -- dependent semantics, is one of the few occasions on which we
4260 -- use the official RM size instead of Esize. See description
4261 -- in Einfo "Handling of Type'Size Values" for details.
4263 if Serious_Errors_Detected = 0
4264 and then Known_Static_RM_Size (Source)
4265 and then Known_Static_RM_Size (Target)
4267 Source_Siz := RM_Size (Source);
4268 Target_Siz := RM_Size (Target);
4270 if Source_Siz /= Target_Siz then
4272 ("?types for unchecked conversion have different sizes!",
4275 if All_Errors_Mode then
4276 Error_Msg_Name_1 := Chars (Source);
4277 Error_Msg_Uint_1 := Source_Siz;
4278 Error_Msg_Name_2 := Chars (Target);
4279 Error_Msg_Uint_2 := Target_Siz;
4281 ("\size of % is ^, size of % is ^?", Enode);
4283 Error_Msg_Uint_1 := UI_Abs (Source_Siz - Target_Siz);
4285 if Is_Discrete_Type (Source)
4286 and then Is_Discrete_Type (Target)
4288 if Source_Siz > Target_Siz then
4290 ("\?^ high order bits of source will be ignored!",
4293 elsif Is_Unsigned_Type (Source) then
4295 ("\?source will be extended with ^ high order " &
4296 "zero bits?!", Enode);
4300 ("\?source will be extended with ^ high order " &
4305 elsif Source_Siz < Target_Siz then
4306 if Is_Discrete_Type (Target) then
4307 if Bytes_Big_Endian then
4309 ("\?target value will include ^ undefined " &
4314 ("\?target value will include ^ undefined " &
4321 ("\?^ trailing bits of target value will be " &
4322 "undefined!", Enode);
4325 else pragma Assert (Source_Siz > Target_Siz);
4327 ("\?^ trailing bits of source will be ignored!",
4334 -- If both types are access types, we need to check the alignment.
4335 -- If the alignment of both is specified, we can do it here.
4337 if Serious_Errors_Detected = 0
4338 and then Ekind (Source) in Access_Kind
4339 and then Ekind (Target) in Access_Kind
4340 and then Target_Strict_Alignment
4341 and then Present (Designated_Type (Source))
4342 and then Present (Designated_Type (Target))
4345 D_Source : constant Entity_Id := Designated_Type (Source);
4346 D_Target : constant Entity_Id := Designated_Type (Target);
4349 if Known_Alignment (D_Source)
4350 and then Known_Alignment (D_Target)
4353 Source_Align : constant Uint := Alignment (D_Source);
4354 Target_Align : constant Uint := Alignment (D_Target);
4357 if Source_Align < Target_Align
4358 and then not Is_Tagged_Type (D_Source)
4360 Error_Msg_Uint_1 := Target_Align;
4361 Error_Msg_Uint_2 := Source_Align;
4362 Error_Msg_Node_2 := D_Source;
4364 ("?alignment of & (^) is stricter than " &
4365 "alignment of & (^)!", Enode, D_Target);
4367 if All_Errors_Mode then
4369 ("\?resulting access value may have invalid " &
4370 "alignment!", Enode);
4379 end Validate_Unchecked_Conversions;