1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2010, 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 Aspects; use Aspects;
27 with Atree; use Atree;
28 with Checks; use Checks;
29 with Einfo; use Einfo;
30 with Elists; use Elists;
31 with Errout; use Errout;
32 with Exp_Disp; use Exp_Disp;
33 with Exp_Tss; use Exp_Tss;
34 with Exp_Util; use Exp_Util;
36 with Lib.Xref; use Lib.Xref;
37 with Namet; use Namet;
38 with Nlists; use Nlists;
39 with Nmake; use Nmake;
41 with Restrict; use Restrict;
42 with Rident; use Rident;
43 with Rtsfind; use Rtsfind;
45 with Sem_Aux; use Sem_Aux;
46 with Sem_Ch3; use Sem_Ch3;
47 with Sem_Ch8; use Sem_Ch8;
48 with Sem_Eval; use Sem_Eval;
49 with Sem_Res; use Sem_Res;
50 with Sem_Type; use Sem_Type;
51 with Sem_Util; use Sem_Util;
52 with Sem_Warn; use Sem_Warn;
53 with Snames; use Snames;
54 with Stand; use Stand;
55 with Sinfo; use Sinfo;
56 with Targparm; use Targparm;
57 with Ttypes; use Ttypes;
58 with Tbuild; use Tbuild;
59 with Urealp; use Urealp;
61 with GNAT.Heap_Sort_G;
63 package body Sem_Ch13 is
65 SSU : constant Pos := System_Storage_Unit;
66 -- Convenient short hand for commonly used constant
68 -----------------------
69 -- Local Subprograms --
70 -----------------------
72 procedure Alignment_Check_For_Esize_Change (Typ : Entity_Id);
73 -- This routine is called after setting the Esize of type entity Typ.
74 -- The purpose is to deal with the situation where an alignment has been
75 -- inherited from a derived type that is no longer appropriate for the
76 -- new Esize value. In this case, we reset the Alignment to unknown.
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 procedure New_Stream_Subprogram
94 -- Create a subprogram renaming of a given stream attribute to the
95 -- designated subprogram and then in the tagged case, provide this as a
96 -- primitive operation, or in the non-tagged case make an appropriate TSS
97 -- entry. This is more properly an expansion activity than just semantics,
98 -- but the presence of user-defined stream functions for limited types is a
99 -- legality check, which is why this takes place here rather than in
100 -- exp_ch13, where it was previously. Nam indicates the name of the TSS
101 -- function to be generated.
103 -- To avoid elaboration anomalies with freeze nodes, for untagged types
104 -- we generate both a subprogram declaration and a subprogram renaming
105 -- declaration, so that the attribute specification is handled as a
106 -- renaming_as_body. For tagged types, the specification is one of the
113 Biased : Boolean := True);
114 -- If Biased is True, sets Has_Biased_Representation flag for E, and
115 -- outputs a warning message at node N if Warn_On_Biased_Representation is
116 -- is True. This warning inserts the string Msg to describe the construct
119 ----------------------------------------------
120 -- Table for Validate_Unchecked_Conversions --
121 ----------------------------------------------
123 -- The following table collects unchecked conversions for validation.
124 -- Entries are made by Validate_Unchecked_Conversion and then the
125 -- call to Validate_Unchecked_Conversions does the actual error
126 -- checking and posting of warnings. The reason for this delayed
127 -- processing is to take advantage of back-annotations of size and
128 -- alignment values performed by the back end.
130 -- Note: the reason we store a Source_Ptr value instead of a Node_Id
131 -- is that by the time Validate_Unchecked_Conversions is called, Sprint
132 -- will already have modified all Sloc values if the -gnatD option is set.
134 type UC_Entry is record
135 Eloc : Source_Ptr; -- node used for posting warnings
136 Source : Entity_Id; -- source type for unchecked conversion
137 Target : Entity_Id; -- target type for unchecked conversion
140 package Unchecked_Conversions is new Table.Table (
141 Table_Component_Type => UC_Entry,
142 Table_Index_Type => Int,
143 Table_Low_Bound => 1,
145 Table_Increment => 200,
146 Table_Name => "Unchecked_Conversions");
148 ----------------------------------------
149 -- Table for Validate_Address_Clauses --
150 ----------------------------------------
152 -- If an address clause has the form
154 -- for X'Address use Expr
156 -- where Expr is of the form Y'Address or recursively is a reference
157 -- to a constant of either of these forms, and X and Y are entities of
158 -- objects, then if Y has a smaller alignment than X, that merits a
159 -- warning about possible bad alignment. The following table collects
160 -- address clauses of this kind. We put these in a table so that they
161 -- can be checked after the back end has completed annotation of the
162 -- alignments of objects, since we can catch more cases that way.
164 type Address_Clause_Check_Record is record
166 -- The address clause
169 -- The entity of the object overlaying Y
172 -- The entity of the object being overlaid
175 -- Whether the address is offseted within Y
178 package Address_Clause_Checks is new Table.Table (
179 Table_Component_Type => Address_Clause_Check_Record,
180 Table_Index_Type => Int,
181 Table_Low_Bound => 1,
183 Table_Increment => 200,
184 Table_Name => "Address_Clause_Checks");
186 -----------------------------------------
187 -- Adjust_Record_For_Reverse_Bit_Order --
188 -----------------------------------------
190 procedure Adjust_Record_For_Reverse_Bit_Order (R : Entity_Id) is
195 -- Processing depends on version of Ada
197 -- For Ada 95, we just renumber bits within a storage unit. We do the
198 -- same for Ada 83 mode, since we recognize pragma Bit_Order in Ada 83,
199 -- and are free to add this extension.
201 if Ada_Version < Ada_2005 then
202 Comp := First_Component_Or_Discriminant (R);
203 while Present (Comp) loop
204 CC := Component_Clause (Comp);
206 -- If component clause is present, then deal with the non-default
207 -- bit order case for Ada 95 mode.
209 -- We only do this processing for the base type, and in fact that
210 -- is important, since otherwise if there are record subtypes, we
211 -- could reverse the bits once for each subtype, which is wrong.
214 and then Ekind (R) = E_Record_Type
217 CFB : constant Uint := Component_Bit_Offset (Comp);
218 CSZ : constant Uint := Esize (Comp);
219 CLC : constant Node_Id := Component_Clause (Comp);
220 Pos : constant Node_Id := Position (CLC);
221 FB : constant Node_Id := First_Bit (CLC);
223 Storage_Unit_Offset : constant Uint :=
224 CFB / System_Storage_Unit;
226 Start_Bit : constant Uint :=
227 CFB mod System_Storage_Unit;
230 -- Cases where field goes over storage unit boundary
232 if Start_Bit + CSZ > System_Storage_Unit then
234 -- Allow multi-byte field but generate warning
236 if Start_Bit mod System_Storage_Unit = 0
237 and then CSZ mod System_Storage_Unit = 0
240 ("multi-byte field specified with non-standard"
241 & " Bit_Order?", CLC);
243 if Bytes_Big_Endian then
245 ("bytes are not reversed "
246 & "(component is big-endian)?", CLC);
249 ("bytes are not reversed "
250 & "(component is little-endian)?", CLC);
253 -- Do not allow non-contiguous field
257 ("attempt to specify non-contiguous field "
258 & "not permitted", CLC);
260 ("\caused by non-standard Bit_Order "
263 ("\consider possibility of using "
264 & "Ada 2005 mode here", CLC);
267 -- Case where field fits in one storage unit
270 -- Give warning if suspicious component clause
272 if Intval (FB) >= System_Storage_Unit
273 and then Warn_On_Reverse_Bit_Order
276 ("?Bit_Order clause does not affect " &
277 "byte ordering", Pos);
279 Intval (Pos) + Intval (FB) /
282 ("?position normalized to ^ before bit " &
283 "order interpreted", Pos);
286 -- Here is where we fix up the Component_Bit_Offset value
287 -- to account for the reverse bit order. Some examples of
288 -- what needs to be done are:
290 -- First_Bit .. Last_Bit Component_Bit_Offset
302 -- The rule is that the first bit is is obtained by
303 -- subtracting the old ending bit from storage_unit - 1.
305 Set_Component_Bit_Offset
307 (Storage_Unit_Offset * System_Storage_Unit) +
308 (System_Storage_Unit - 1) -
309 (Start_Bit + CSZ - 1));
311 Set_Normalized_First_Bit
313 Component_Bit_Offset (Comp) mod
314 System_Storage_Unit);
319 Next_Component_Or_Discriminant (Comp);
322 -- For Ada 2005, we do machine scalar processing, as fully described In
323 -- AI-133. This involves gathering all components which start at the
324 -- same byte offset and processing them together. Same approach is still
325 -- valid in later versions including Ada 2012.
329 Max_Machine_Scalar_Size : constant Uint :=
331 (Standard_Long_Long_Integer_Size);
332 -- We use this as the maximum machine scalar size
335 SSU : constant Uint := UI_From_Int (System_Storage_Unit);
338 -- This first loop through components does two things. First it
339 -- deals with the case of components with component clauses whose
340 -- length is greater than the maximum machine scalar size (either
341 -- accepting them or rejecting as needed). Second, it counts the
342 -- number of components with component clauses whose length does
343 -- not exceed this maximum for later processing.
346 Comp := First_Component_Or_Discriminant (R);
347 while Present (Comp) loop
348 CC := Component_Clause (Comp);
352 Fbit : constant Uint :=
353 Static_Integer (First_Bit (CC));
356 -- Case of component with size > max machine scalar
358 if Esize (Comp) > Max_Machine_Scalar_Size then
360 -- Must begin on byte boundary
362 if Fbit mod SSU /= 0 then
364 ("illegal first bit value for "
365 & "reverse bit order",
367 Error_Msg_Uint_1 := SSU;
368 Error_Msg_Uint_2 := Max_Machine_Scalar_Size;
371 ("\must be a multiple of ^ "
372 & "if size greater than ^",
375 -- Must end on byte boundary
377 elsif Esize (Comp) mod SSU /= 0 then
379 ("illegal last bit value for "
380 & "reverse bit order",
382 Error_Msg_Uint_1 := SSU;
383 Error_Msg_Uint_2 := Max_Machine_Scalar_Size;
386 ("\must be a multiple of ^ if size "
390 -- OK, give warning if enabled
392 elsif Warn_On_Reverse_Bit_Order then
394 ("multi-byte field specified with "
395 & " non-standard Bit_Order?", CC);
397 if Bytes_Big_Endian then
399 ("\bytes are not reversed "
400 & "(component is big-endian)?", CC);
403 ("\bytes are not reversed "
404 & "(component is little-endian)?", CC);
408 -- Case where size is not greater than max machine
409 -- scalar. For now, we just count these.
412 Num_CC := Num_CC + 1;
417 Next_Component_Or_Discriminant (Comp);
420 -- We need to sort the component clauses on the basis of the
421 -- Position values in the clause, so we can group clauses with
422 -- the same Position. together to determine the relevant machine
426 Comps : array (0 .. Num_CC) of Entity_Id;
427 -- Array to collect component and discriminant entities. The
428 -- data starts at index 1, the 0'th entry is for the sort
431 function CP_Lt (Op1, Op2 : Natural) return Boolean;
432 -- Compare routine for Sort
434 procedure CP_Move (From : Natural; To : Natural);
435 -- Move routine for Sort
437 package Sorting is new GNAT.Heap_Sort_G (CP_Move, CP_Lt);
441 -- Start and stop positions in the component list of the set of
442 -- components with the same starting position (that constitute
443 -- components in a single machine scalar).
446 -- Maximum last bit value of any component in this set
449 -- Corresponding machine scalar size
455 function CP_Lt (Op1, Op2 : Natural) return Boolean is
457 return Position (Component_Clause (Comps (Op1))) <
458 Position (Component_Clause (Comps (Op2)));
465 procedure CP_Move (From : Natural; To : Natural) is
467 Comps (To) := Comps (From);
470 -- Start of processing for Sort_CC
473 -- Collect the component clauses
476 Comp := First_Component_Or_Discriminant (R);
477 while Present (Comp) loop
478 if Present (Component_Clause (Comp))
479 and then Esize (Comp) <= Max_Machine_Scalar_Size
481 Num_CC := Num_CC + 1;
482 Comps (Num_CC) := Comp;
485 Next_Component_Or_Discriminant (Comp);
488 -- Sort by ascending position number
490 Sorting.Sort (Num_CC);
492 -- We now have all the components whose size does not exceed
493 -- the max machine scalar value, sorted by starting position.
494 -- In this loop we gather groups of clauses starting at the
495 -- same position, to process them in accordance with AI-133.
498 while Stop < Num_CC loop
503 (Last_Bit (Component_Clause (Comps (Start))));
504 while Stop < Num_CC loop
506 (Position (Component_Clause (Comps (Stop + 1)))) =
508 (Position (Component_Clause (Comps (Stop))))
516 (Component_Clause (Comps (Stop)))));
522 -- Now we have a group of component clauses from Start to
523 -- Stop whose positions are identical, and MaxL is the
524 -- maximum last bit value of any of these components.
526 -- We need to determine the corresponding machine scalar
527 -- size. This loop assumes that machine scalar sizes are
528 -- even, and that each possible machine scalar has twice
529 -- as many bits as the next smaller one.
531 MSS := Max_Machine_Scalar_Size;
533 and then (MSS / 2) >= SSU
534 and then (MSS / 2) > MaxL
539 -- Here is where we fix up the Component_Bit_Offset value
540 -- to account for the reverse bit order. Some examples of
541 -- what needs to be done for the case of a machine scalar
544 -- First_Bit .. Last_Bit Component_Bit_Offset
556 -- The rule is that the first bit is obtained by subtracting
557 -- the old ending bit from machine scalar size - 1.
559 for C in Start .. Stop loop
561 Comp : constant Entity_Id := Comps (C);
562 CC : constant Node_Id :=
563 Component_Clause (Comp);
564 LB : constant Uint :=
565 Static_Integer (Last_Bit (CC));
566 NFB : constant Uint := MSS - Uint_1 - LB;
567 NLB : constant Uint := NFB + Esize (Comp) - 1;
568 Pos : constant Uint :=
569 Static_Integer (Position (CC));
572 if Warn_On_Reverse_Bit_Order then
573 Error_Msg_Uint_1 := MSS;
575 ("info: reverse bit order in machine " &
576 "scalar of length^?", First_Bit (CC));
577 Error_Msg_Uint_1 := NFB;
578 Error_Msg_Uint_2 := NLB;
580 if Bytes_Big_Endian then
582 ("?\info: big-endian range for "
583 & "component & is ^ .. ^",
584 First_Bit (CC), Comp);
587 ("?\info: little-endian range "
588 & "for component & is ^ .. ^",
589 First_Bit (CC), Comp);
593 Set_Component_Bit_Offset (Comp, Pos * SSU + NFB);
594 Set_Normalized_First_Bit (Comp, NFB mod SSU);
601 end Adjust_Record_For_Reverse_Bit_Order;
603 --------------------------------------
604 -- Alignment_Check_For_Esize_Change --
605 --------------------------------------
607 procedure Alignment_Check_For_Esize_Change (Typ : Entity_Id) is
609 -- If the alignment is known, and not set by a rep clause, and is
610 -- inconsistent with the size being set, then reset it to unknown,
611 -- we assume in this case that the size overrides the inherited
612 -- alignment, and that the alignment must be recomputed.
614 if Known_Alignment (Typ)
615 and then not Has_Alignment_Clause (Typ)
616 and then Esize (Typ) mod (Alignment (Typ) * SSU) /= 0
618 Init_Alignment (Typ);
620 end Alignment_Check_For_Esize_Change;
622 -----------------------------------
623 -- Analyze_Aspect_Specifications --
624 -----------------------------------
626 procedure Analyze_Aspect_Specifications
635 Ins_Node : Node_Id := N;
636 -- Insert pragmas (other than Pre/Post) after this node
638 -- The general processing involves building an attribute definition
639 -- clause or a pragma node that corresponds to the access type. Then
640 -- one of two things happens:
642 -- If we are required to delay the evaluation of this aspect to the
643 -- freeze point, we preanalyze the relevant argument, and then attach
644 -- the corresponding pragma/attribute definition clause to the aspect
645 -- specification node, which is then placed in the Rep Item chain.
646 -- In this case we mark the entity with the Has_Delayed_Aspects flag,
647 -- and we evaluate the rep item at the freeze point.
649 -- If no delay is required, we just insert the pragma or attribute
650 -- after the declaration, and it will get processed by the normal
651 -- circuit. The From_Aspect_Specification flag is set on the pragma
652 -- or attribute definition node in either case to activate special
653 -- processing (e.g. not traversing the list of homonyms for inline).
655 Delay_Required : Boolean;
656 -- Set True if delay is required
664 while Present (Aspect) loop
666 Id : constant Node_Id := Identifier (Aspect);
667 Expr : constant Node_Id := Expression (Aspect);
668 Nam : constant Name_Id := Chars (Id);
669 A_Id : constant Aspect_Id := Get_Aspect_Id (Nam);
674 Set_Entity (Aspect, E);
675 Ent := New_Occurrence_Of (E, Sloc (Id));
677 -- Check for duplicate aspect
680 while Anod /= Aspect loop
681 if Nam = Chars (Identifier (Anod)) then
682 Error_Msg_Name_1 := Nam;
683 Error_Msg_Sloc := Sloc (Anod);
685 ("aspect% for & ignored, already given at#", Id, E);
692 -- Processing based on specific aspect
696 -- No_Aspect should be impossible
701 -- Aspects taking an optional boolean argument. For all of
702 -- these we just create a matching pragma and insert it,
703 -- setting flag Cancel_Aspect if the expression is False.
705 when Aspect_Ada_2005 |
708 Aspect_Atomic_Components |
709 Aspect_Discard_Names |
710 Aspect_Favor_Top_Level |
712 Aspect_Inline_Always |
715 Aspect_Persistent_BSS |
716 Aspect_Preelaborable_Initialization |
717 Aspect_Pure_Function |
719 Aspect_Suppress_Debug_Info |
720 Aspect_Unchecked_Union |
721 Aspect_Universal_Aliasing |
723 Aspect_Unreferenced |
724 Aspect_Unreferenced_Objects |
726 Aspect_Volatile_Components =>
728 -- Build corresponding pragma node
731 Make_Pragma (Sloc (Aspect),
732 Pragma_Argument_Associations => New_List (Ent),
734 Make_Identifier (Sloc (Id), Chars (Id)));
736 -- Deal with missing expression case, delay never needed
739 Delay_Required := False;
741 -- Expression is present
744 Preanalyze_Spec_Expression (Expr, Standard_Boolean);
746 -- If preanalysis gives a static expression, we don't
747 -- need to delay (this will happen often in practice).
749 if Is_OK_Static_Expression (Expr) then
750 Delay_Required := False;
752 if Is_False (Expr_Value (Expr)) then
753 Set_Aspect_Cancel (Aitem);
756 -- If we don't get a static expression, then delay, the
757 -- expression may turn out static by freeze time.
760 Delay_Required := True;
764 -- Aspects corresponding to attribute definition clauses with
765 -- the exception of Address which is treated specially.
767 when Aspect_Address |
770 Aspect_Component_Size |
771 Aspect_External_Tag |
772 Aspect_Machine_Radix |
775 Aspect_Storage_Pool |
776 Aspect_Storage_Size |
780 -- Preanalyze the expression with the appropriate type
783 when Aspect_Address =>
784 T := RTE (RE_Address);
785 when Aspect_Bit_Order =>
786 T := RTE (RE_Bit_Order);
787 when Aspect_External_Tag =>
788 T := Standard_String;
789 when Aspect_Storage_Pool =>
790 T := Class_Wide_Type (RTE (RE_Root_Storage_Pool));
795 Preanalyze_Spec_Expression (Expr, T);
797 -- Construct the attribute definition clause
800 Make_Attribute_Definition_Clause (Sloc (Aspect),
803 Expression => Relocate_Node (Expr));
805 -- We do not need a delay if we have a static expression
807 if Is_OK_Static_Expression (Expression (Aitem)) then
808 Delay_Required := False;
810 -- Here a delay is required
813 Delay_Required := True;
816 -- Aspects corresponding to pragmas with two arguments, where
817 -- the first argument is a local name referring to the entity,
818 -- and the second argument is the aspect definition expression.
820 when Aspect_Suppress |
823 -- Construct the pragma
826 Make_Pragma (Sloc (Aspect),
827 Pragma_Argument_Associations => New_List (
828 New_Occurrence_Of (E, Sloc (Expr)),
829 Relocate_Node (Expr)),
831 Make_Identifier (Sloc (Id), Chars (Id)));
833 -- We don't have to play the delay game here, since the only
834 -- values are check names which don't get analyzed anyway.
836 Delay_Required := False;
838 -- Aspects corresponding to pragmas with two arguments, where
839 -- the second argument is a local name referring to the entity,
840 -- and the first argument is the aspect definition expression.
842 when Aspect_Warnings =>
844 -- Construct the pragma
847 Make_Pragma (Sloc (Aspect),
848 Pragma_Argument_Associations => New_List (
849 Relocate_Node (Expr),
850 New_Occurrence_Of (E, Sloc (Expr))),
852 Make_Identifier (Sloc (Id), Chars (Id)));
854 -- We don't have to play the delay game here, since the only
855 -- values are check names which don't get analyzed anyway.
857 Delay_Required := False;
859 -- Aspect Post corresponds to pragma Postcondition with single
860 -- argument that is the expression (we never give a message
861 -- argument. This is inserted right after the declaration,
862 -- to get the required pragma placement.
866 -- Construct the pragma
869 Make_Pragma (Sloc (Expr),
870 Pragma_Argument_Associations => New_List (
871 Relocate_Node (Expr)),
873 Make_Identifier (Sloc (Id), Name_Postcondition));
875 -- We don't have to play the delay game here. The required
876 -- delay in this case is already implemented by the pragma.
878 Delay_Required := False;
880 -- Aspect Pre corresponds to pragma Precondition with single
881 -- argument that is the expression (we never give a message
882 -- argument). This is inserted right after the declaration,
883 -- to get the required pragma placement.
887 -- Construct the pragma
890 Make_Pragma (Sloc (Expr),
891 Pragma_Argument_Associations => New_List (
892 Relocate_Node (Expr)),
894 Make_Identifier (Sloc (Id), Name_Precondition));
896 -- We don't have to play the delay game here. The required
897 -- delay in this case is already implemented by the pragma.
899 Delay_Required := False;
901 -- Aspects currently unimplemented
903 when Aspect_Invariant |
906 Error_Msg_N ("aspect& not implemented", Identifier (Aspect));
910 Set_From_Aspect_Specification (Aitem, True);
912 -- If a delay is required, we delay the freeze (not much point in
913 -- delaying the aspect if we don't delay the freeze!). The pragma
914 -- or clause is then attached to the aspect specification which
915 -- is placed in the rep item list.
917 if Delay_Required then
918 Ensure_Freeze_Node (E);
919 Set_Is_Delayed_Aspect (Aitem);
920 Set_Has_Delayed_Aspects (E);
921 Set_Aspect_Rep_Item (Aspect, Aitem);
922 Record_Rep_Item (E, Aspect);
924 -- If no delay required, insert the pragma/clause in the tree
927 -- For Pre/Post cases, insert immediately after the entity
928 -- declaration, since that is the required pragma placement.
930 if A_Id = Aspect_Pre or else A_Id = Aspect_Post then
931 Insert_After (N, Aitem);
933 -- For all other cases, insert in sequence
936 Insert_After (Ins_Node, Aitem);
945 end Analyze_Aspect_Specifications;
947 -----------------------
948 -- Analyze_At_Clause --
949 -----------------------
951 -- An at clause is replaced by the corresponding Address attribute
952 -- definition clause that is the preferred approach in Ada 95.
954 procedure Analyze_At_Clause (N : Node_Id) is
955 CS : constant Boolean := Comes_From_Source (N);
958 -- This is an obsolescent feature
960 Check_Restriction (No_Obsolescent_Features, N);
962 if Warn_On_Obsolescent_Feature then
964 ("at clause is an obsolescent feature (RM J.7(2))?", N);
966 ("\use address attribute definition clause instead?", N);
969 -- Rewrite as address clause
972 Make_Attribute_Definition_Clause (Sloc (N),
973 Name => Identifier (N),
974 Chars => Name_Address,
975 Expression => Expression (N)));
977 -- We preserve Comes_From_Source, since logically the clause still
978 -- comes from the source program even though it is changed in form.
980 Set_Comes_From_Source (N, CS);
982 -- Analyze rewritten clause
984 Analyze_Attribute_Definition_Clause (N);
985 end Analyze_At_Clause;
987 -----------------------------------------
988 -- Analyze_Attribute_Definition_Clause --
989 -----------------------------------------
991 procedure Analyze_Attribute_Definition_Clause (N : Node_Id) is
992 Loc : constant Source_Ptr := Sloc (N);
993 Nam : constant Node_Id := Name (N);
994 Attr : constant Name_Id := Chars (N);
995 Expr : constant Node_Id := Expression (N);
996 Id : constant Attribute_Id := Get_Attribute_Id (Attr);
1000 FOnly : Boolean := False;
1001 -- Reset to True for subtype specific attribute (Alignment, Size)
1002 -- and for stream attributes, i.e. those cases where in the call
1003 -- to Rep_Item_Too_Late, FOnly is set True so that only the freezing
1004 -- rules are checked. Note that the case of stream attributes is not
1005 -- clear from the RM, but see AI95-00137. Also, the RM seems to
1006 -- disallow Storage_Size for derived task types, but that is also
1007 -- clearly unintentional.
1009 procedure Analyze_Stream_TSS_Definition (TSS_Nam : TSS_Name_Type);
1010 -- Common processing for 'Read, 'Write, 'Input and 'Output attribute
1011 -- definition clauses.
1013 function Duplicate_Clause return Boolean;
1014 -- This routine checks if the aspect for U_Ent being given by attribute
1015 -- definition clause N is for an aspect that has already been specified,
1016 -- and if so gives an error message. If there is a duplicate, True is
1017 -- returned, otherwise if there is no error, False is returned.
1019 -----------------------------------
1020 -- Analyze_Stream_TSS_Definition --
1021 -----------------------------------
1023 procedure Analyze_Stream_TSS_Definition (TSS_Nam : TSS_Name_Type) is
1024 Subp : Entity_Id := Empty;
1029 Is_Read : constant Boolean := (TSS_Nam = TSS_Stream_Read);
1031 function Has_Good_Profile (Subp : Entity_Id) return Boolean;
1032 -- Return true if the entity is a subprogram with an appropriate
1033 -- profile for the attribute being defined.
1035 ----------------------
1036 -- Has_Good_Profile --
1037 ----------------------
1039 function Has_Good_Profile (Subp : Entity_Id) return Boolean is
1041 Is_Function : constant Boolean := (TSS_Nam = TSS_Stream_Input);
1042 Expected_Ekind : constant array (Boolean) of Entity_Kind :=
1043 (False => E_Procedure, True => E_Function);
1047 if Ekind (Subp) /= Expected_Ekind (Is_Function) then
1051 F := First_Formal (Subp);
1054 or else Ekind (Etype (F)) /= E_Anonymous_Access_Type
1055 or else Designated_Type (Etype (F)) /=
1056 Class_Wide_Type (RTE (RE_Root_Stream_Type))
1061 if not Is_Function then
1065 Expected_Mode : constant array (Boolean) of Entity_Kind :=
1066 (False => E_In_Parameter,
1067 True => E_Out_Parameter);
1069 if Parameter_Mode (F) /= Expected_Mode (Is_Read) then
1077 Typ := Etype (Subp);
1080 return Base_Type (Typ) = Base_Type (Ent)
1081 and then No (Next_Formal (F));
1082 end Has_Good_Profile;
1084 -- Start of processing for Analyze_Stream_TSS_Definition
1089 if not Is_Type (U_Ent) then
1090 Error_Msg_N ("local name must be a subtype", Nam);
1094 Pnam := TSS (Base_Type (U_Ent), TSS_Nam);
1096 -- If Pnam is present, it can be either inherited from an ancestor
1097 -- type (in which case it is legal to redefine it for this type), or
1098 -- be a previous definition of the attribute for the same type (in
1099 -- which case it is illegal).
1101 -- In the first case, it will have been analyzed already, and we
1102 -- can check that its profile does not match the expected profile
1103 -- for a stream attribute of U_Ent. In the second case, either Pnam
1104 -- has been analyzed (and has the expected profile), or it has not
1105 -- been analyzed yet (case of a type that has not been frozen yet
1106 -- and for which the stream attribute has been set using Set_TSS).
1109 and then (No (First_Entity (Pnam)) or else Has_Good_Profile (Pnam))
1111 Error_Msg_Sloc := Sloc (Pnam);
1112 Error_Msg_Name_1 := Attr;
1113 Error_Msg_N ("% attribute already defined #", Nam);
1119 if Is_Entity_Name (Expr) then
1120 if not Is_Overloaded (Expr) then
1121 if Has_Good_Profile (Entity (Expr)) then
1122 Subp := Entity (Expr);
1126 Get_First_Interp (Expr, I, It);
1127 while Present (It.Nam) loop
1128 if Has_Good_Profile (It.Nam) then
1133 Get_Next_Interp (I, It);
1138 if Present (Subp) then
1139 if Is_Abstract_Subprogram (Subp) then
1140 Error_Msg_N ("stream subprogram must not be abstract", Expr);
1144 Set_Entity (Expr, Subp);
1145 Set_Etype (Expr, Etype (Subp));
1147 New_Stream_Subprogram (N, U_Ent, Subp, TSS_Nam);
1150 Error_Msg_Name_1 := Attr;
1151 Error_Msg_N ("incorrect expression for% attribute", Expr);
1153 end Analyze_Stream_TSS_Definition;
1155 ----------------------
1156 -- Duplicate_Clause --
1157 ----------------------
1159 function Duplicate_Clause return Boolean is
1163 -- Nothing to do if this attribute definition clause comes from
1164 -- an aspect specification, since we could not be duplicating an
1165 -- explicit clause, and we dealt with the case of duplicated aspects
1166 -- in Analyze_Aspect_Specifications.
1168 if From_Aspect_Specification (N) then
1172 -- Otherwise current clause may duplicate previous clause or a
1173 -- previously given aspect specification for the same aspect.
1175 A := Get_Rep_Item_For_Entity (U_Ent, Chars (N));
1178 if Entity (A) = U_Ent then
1179 Error_Msg_Name_1 := Chars (N);
1180 Error_Msg_Sloc := Sloc (A);
1181 Error_Msg_NE ("aspect% for & previously specified#", N, U_Ent);
1187 end Duplicate_Clause;
1189 -- Start of processing for Analyze_Attribute_Definition_Clause
1192 -- Process Ignore_Rep_Clauses option
1194 if Ignore_Rep_Clauses then
1197 -- The following should be ignored. They do not affect legality
1198 -- and may be target dependent. The basic idea of -gnatI is to
1199 -- ignore any rep clauses that may be target dependent but do not
1200 -- affect legality (except possibly to be rejected because they
1201 -- are incompatible with the compilation target).
1203 when Attribute_Alignment |
1204 Attribute_Bit_Order |
1205 Attribute_Component_Size |
1206 Attribute_Machine_Radix |
1207 Attribute_Object_Size |
1210 Attribute_Stream_Size |
1211 Attribute_Value_Size =>
1213 Rewrite (N, Make_Null_Statement (Sloc (N)));
1216 -- The following should not be ignored, because in the first place
1217 -- they are reasonably portable, and should not cause problems in
1218 -- compiling code from another target, and also they do affect
1219 -- legality, e.g. failing to provide a stream attribute for a
1220 -- type may make a program illegal.
1222 when Attribute_External_Tag |
1226 Attribute_Storage_Pool |
1227 Attribute_Storage_Size |
1231 -- Other cases are errors ("attribute& cannot be set with
1232 -- definition clause"), which will be caught below.
1240 Ent := Entity (Nam);
1242 if Rep_Item_Too_Early (Ent, N) then
1246 -- Rep clause applies to full view of incomplete type or private type if
1247 -- we have one (if not, this is a premature use of the type). However,
1248 -- certain semantic checks need to be done on the specified entity (i.e.
1249 -- the private view), so we save it in Ent.
1251 if Is_Private_Type (Ent)
1252 and then Is_Derived_Type (Ent)
1253 and then not Is_Tagged_Type (Ent)
1254 and then No (Full_View (Ent))
1256 -- If this is a private type whose completion is a derivation from
1257 -- another private type, there is no full view, and the attribute
1258 -- belongs to the type itself, not its underlying parent.
1262 elsif Ekind (Ent) = E_Incomplete_Type then
1264 -- The attribute applies to the full view, set the entity of the
1265 -- attribute definition accordingly.
1267 Ent := Underlying_Type (Ent);
1269 Set_Entity (Nam, Ent);
1272 U_Ent := Underlying_Type (Ent);
1275 -- Complete other routine error checks
1277 if Etype (Nam) = Any_Type then
1280 elsif Scope (Ent) /= Current_Scope then
1281 Error_Msg_N ("entity must be declared in this scope", Nam);
1284 elsif No (U_Ent) then
1287 elsif Is_Type (U_Ent)
1288 and then not Is_First_Subtype (U_Ent)
1289 and then Id /= Attribute_Object_Size
1290 and then Id /= Attribute_Value_Size
1291 and then not From_At_Mod (N)
1293 Error_Msg_N ("cannot specify attribute for subtype", Nam);
1297 Set_Entity (N, U_Ent);
1299 -- Switch on particular attribute
1307 -- Address attribute definition clause
1309 when Attribute_Address => Address : begin
1311 -- A little error check, catch for X'Address use X'Address;
1313 if Nkind (Nam) = N_Identifier
1314 and then Nkind (Expr) = N_Attribute_Reference
1315 and then Attribute_Name (Expr) = Name_Address
1316 and then Nkind (Prefix (Expr)) = N_Identifier
1317 and then Chars (Nam) = Chars (Prefix (Expr))
1320 ("address for & is self-referencing", Prefix (Expr), Ent);
1324 -- Not that special case, carry on with analysis of expression
1326 Analyze_And_Resolve (Expr, RTE (RE_Address));
1328 -- Even when ignoring rep clauses we need to indicate that the
1329 -- entity has an address clause and thus it is legal to declare
1332 if Ignore_Rep_Clauses then
1333 if Ekind_In (U_Ent, E_Variable, E_Constant) then
1334 Record_Rep_Item (U_Ent, N);
1340 if Duplicate_Clause then
1343 -- Case of address clause for subprogram
1345 elsif Is_Subprogram (U_Ent) then
1346 if Has_Homonym (U_Ent) then
1348 ("address clause cannot be given " &
1349 "for overloaded subprogram",
1354 -- For subprograms, all address clauses are permitted, and we
1355 -- mark the subprogram as having a deferred freeze so that Gigi
1356 -- will not elaborate it too soon.
1358 -- Above needs more comments, what is too soon about???
1360 Set_Has_Delayed_Freeze (U_Ent);
1362 -- Case of address clause for entry
1364 elsif Ekind (U_Ent) = E_Entry then
1365 if Nkind (Parent (N)) = N_Task_Body then
1367 ("entry address must be specified in task spec", Nam);
1371 -- For entries, we require a constant address
1373 Check_Constant_Address_Clause (Expr, U_Ent);
1375 -- Special checks for task types
1377 if Is_Task_Type (Scope (U_Ent))
1378 and then Comes_From_Source (Scope (U_Ent))
1381 ("?entry address declared for entry in task type", N);
1383 ("\?only one task can be declared of this type", N);
1386 -- Entry address clauses are obsolescent
1388 Check_Restriction (No_Obsolescent_Features, N);
1390 if Warn_On_Obsolescent_Feature then
1392 ("attaching interrupt to task entry is an " &
1393 "obsolescent feature (RM J.7.1)?", N);
1395 ("\use interrupt procedure instead?", N);
1398 -- Case of an address clause for a controlled object which we
1399 -- consider to be erroneous.
1401 elsif Is_Controlled (Etype (U_Ent))
1402 or else Has_Controlled_Component (Etype (U_Ent))
1405 ("?controlled object& must not be overlaid", Nam, U_Ent);
1407 ("\?Program_Error will be raised at run time", Nam);
1408 Insert_Action (Declaration_Node (U_Ent),
1409 Make_Raise_Program_Error (Loc,
1410 Reason => PE_Overlaid_Controlled_Object));
1413 -- Case of address clause for a (non-controlled) object
1416 Ekind (U_Ent) = E_Variable
1418 Ekind (U_Ent) = E_Constant
1421 Expr : constant Node_Id := Expression (N);
1426 -- Exported variables cannot have an address clause, because
1427 -- this cancels the effect of the pragma Export.
1429 if Is_Exported (U_Ent) then
1431 ("cannot export object with address clause", Nam);
1435 Find_Overlaid_Entity (N, O_Ent, Off);
1437 -- Overlaying controlled objects is erroneous
1440 and then (Has_Controlled_Component (Etype (O_Ent))
1441 or else Is_Controlled (Etype (O_Ent)))
1444 ("?cannot overlay with controlled object", Expr);
1446 ("\?Program_Error will be raised at run time", Expr);
1447 Insert_Action (Declaration_Node (U_Ent),
1448 Make_Raise_Program_Error (Loc,
1449 Reason => PE_Overlaid_Controlled_Object));
1452 elsif Present (O_Ent)
1453 and then Ekind (U_Ent) = E_Constant
1454 and then not Is_Constant_Object (O_Ent)
1456 Error_Msg_N ("constant overlays a variable?", Expr);
1458 elsif Present (Renamed_Object (U_Ent)) then
1460 ("address clause not allowed"
1461 & " for a renaming declaration (RM 13.1(6))", Nam);
1464 -- Imported variables can have an address clause, but then
1465 -- the import is pretty meaningless except to suppress
1466 -- initializations, so we do not need such variables to
1467 -- be statically allocated (and in fact it causes trouble
1468 -- if the address clause is a local value).
1470 elsif Is_Imported (U_Ent) then
1471 Set_Is_Statically_Allocated (U_Ent, False);
1474 -- We mark a possible modification of a variable with an
1475 -- address clause, since it is likely aliasing is occurring.
1477 Note_Possible_Modification (Nam, Sure => False);
1479 -- Here we are checking for explicit overlap of one variable
1480 -- by another, and if we find this then mark the overlapped
1481 -- variable as also being volatile to prevent unwanted
1482 -- optimizations. This is a significant pessimization so
1483 -- avoid it when there is an offset, i.e. when the object
1484 -- is composite; they cannot be optimized easily anyway.
1487 and then Is_Object (O_Ent)
1490 Set_Treat_As_Volatile (O_Ent);
1493 -- Legality checks on the address clause for initialized
1494 -- objects is deferred until the freeze point, because
1495 -- a subsequent pragma might indicate that the object is
1496 -- imported and thus not initialized.
1498 Set_Has_Delayed_Freeze (U_Ent);
1500 -- If an initialization call has been generated for this
1501 -- object, it needs to be deferred to after the freeze node
1502 -- we have just now added, otherwise GIGI will see a
1503 -- reference to the variable (as actual to the IP call)
1504 -- before its definition.
1507 Init_Call : constant Node_Id := Find_Init_Call (U_Ent, N);
1509 if Present (Init_Call) then
1511 Append_Freeze_Action (U_Ent, Init_Call);
1515 if Is_Exported (U_Ent) then
1517 ("& cannot be exported if an address clause is given",
1520 ("\define and export a variable " &
1521 "that holds its address instead",
1525 -- Entity has delayed freeze, so we will generate an
1526 -- alignment check at the freeze point unless suppressed.
1528 if not Range_Checks_Suppressed (U_Ent)
1529 and then not Alignment_Checks_Suppressed (U_Ent)
1531 Set_Check_Address_Alignment (N);
1534 -- Kill the size check code, since we are not allocating
1535 -- the variable, it is somewhere else.
1537 Kill_Size_Check_Code (U_Ent);
1539 -- If the address clause is of the form:
1541 -- for Y'Address use X'Address
1545 -- Const : constant Address := X'Address;
1547 -- for Y'Address use Const;
1549 -- then we make an entry in the table for checking the size
1550 -- and alignment of the overlaying variable. We defer this
1551 -- check till after code generation to take full advantage
1552 -- of the annotation done by the back end. This entry is
1553 -- only made if the address clause comes from source.
1554 -- If the entity has a generic type, the check will be
1555 -- performed in the instance if the actual type justifies
1556 -- it, and we do not insert the clause in the table to
1557 -- prevent spurious warnings.
1559 if Address_Clause_Overlay_Warnings
1560 and then Comes_From_Source (N)
1561 and then Present (O_Ent)
1562 and then Is_Object (O_Ent)
1564 if not Is_Generic_Type (Etype (U_Ent)) then
1565 Address_Clause_Checks.Append ((N, U_Ent, O_Ent, Off));
1568 -- If variable overlays a constant view, and we are
1569 -- warning on overlays, then mark the variable as
1570 -- overlaying a constant (we will give warnings later
1571 -- if this variable is assigned).
1573 if Is_Constant_Object (O_Ent)
1574 and then Ekind (U_Ent) = E_Variable
1576 Set_Overlays_Constant (U_Ent);
1581 -- Not a valid entity for an address clause
1584 Error_Msg_N ("address cannot be given for &", Nam);
1592 -- Alignment attribute definition clause
1594 when Attribute_Alignment => Alignment : declare
1595 Align : constant Uint := Get_Alignment_Value (Expr);
1600 if not Is_Type (U_Ent)
1601 and then Ekind (U_Ent) /= E_Variable
1602 and then Ekind (U_Ent) /= E_Constant
1604 Error_Msg_N ("alignment cannot be given for &", Nam);
1606 elsif Duplicate_Clause then
1609 elsif Align /= No_Uint then
1610 Set_Has_Alignment_Clause (U_Ent);
1611 Set_Alignment (U_Ent, Align);
1613 -- For an array type, U_Ent is the first subtype. In that case,
1614 -- also set the alignment of the anonymous base type so that
1615 -- other subtypes (such as the itypes for aggregates of the
1616 -- type) also receive the expected alignment.
1618 if Is_Array_Type (U_Ent) then
1619 Set_Alignment (Base_Type (U_Ent), Align);
1628 -- Bit_Order attribute definition clause
1630 when Attribute_Bit_Order => Bit_Order : declare
1632 if not Is_Record_Type (U_Ent) then
1634 ("Bit_Order can only be defined for record type", Nam);
1636 elsif Duplicate_Clause then
1640 Analyze_And_Resolve (Expr, RTE (RE_Bit_Order));
1642 if Etype (Expr) = Any_Type then
1645 elsif not Is_Static_Expression (Expr) then
1646 Flag_Non_Static_Expr
1647 ("Bit_Order requires static expression!", Expr);
1650 if (Expr_Value (Expr) = 0) /= Bytes_Big_Endian then
1651 Set_Reverse_Bit_Order (U_Ent, True);
1657 --------------------
1658 -- Component_Size --
1659 --------------------
1661 -- Component_Size attribute definition clause
1663 when Attribute_Component_Size => Component_Size_Case : declare
1664 Csize : constant Uint := Static_Integer (Expr);
1668 New_Ctyp : Entity_Id;
1672 if not Is_Array_Type (U_Ent) then
1673 Error_Msg_N ("component size requires array type", Nam);
1677 Btype := Base_Type (U_Ent);
1678 Ctyp := Component_Type (Btype);
1680 if Duplicate_Clause then
1683 elsif Rep_Item_Too_Early (Btype, N) then
1686 elsif Csize /= No_Uint then
1687 Check_Size (Expr, Ctyp, Csize, Biased);
1689 -- For the biased case, build a declaration for a subtype that
1690 -- will be used to represent the biased subtype that reflects
1691 -- the biased representation of components. We need the subtype
1692 -- to get proper conversions on referencing elements of the
1693 -- array. Note: component size clauses are ignored in VM mode.
1695 if VM_Target = No_VM then
1698 Make_Defining_Identifier (Loc,
1700 New_External_Name (Chars (U_Ent), 'C', 0, 'T'));
1703 Make_Subtype_Declaration (Loc,
1704 Defining_Identifier => New_Ctyp,
1705 Subtype_Indication =>
1706 New_Occurrence_Of (Component_Type (Btype), Loc));
1708 Set_Parent (Decl, N);
1709 Analyze (Decl, Suppress => All_Checks);
1711 Set_Has_Delayed_Freeze (New_Ctyp, False);
1712 Set_Esize (New_Ctyp, Csize);
1713 Set_RM_Size (New_Ctyp, Csize);
1714 Init_Alignment (New_Ctyp);
1715 Set_Is_Itype (New_Ctyp, True);
1716 Set_Associated_Node_For_Itype (New_Ctyp, U_Ent);
1718 Set_Component_Type (Btype, New_Ctyp);
1719 Set_Biased (New_Ctyp, N, "component size clause");
1722 Set_Component_Size (Btype, Csize);
1724 -- For VM case, we ignore component size clauses
1727 -- Give a warning unless we are in GNAT mode, in which case
1728 -- the warning is suppressed since it is not useful.
1730 if not GNAT_Mode then
1732 ("?component size ignored in this configuration", N);
1736 -- Deal with warning on overridden size
1738 if Warn_On_Overridden_Size
1739 and then Has_Size_Clause (Ctyp)
1740 and then RM_Size (Ctyp) /= Csize
1743 ("?component size overrides size clause for&",
1747 Set_Has_Component_Size_Clause (Btype, True);
1748 Set_Has_Non_Standard_Rep (Btype, True);
1750 end Component_Size_Case;
1756 when Attribute_External_Tag => External_Tag :
1758 if not Is_Tagged_Type (U_Ent) then
1759 Error_Msg_N ("should be a tagged type", Nam);
1762 if Duplicate_Clause then
1766 Analyze_And_Resolve (Expr, Standard_String);
1768 if not Is_Static_Expression (Expr) then
1769 Flag_Non_Static_Expr
1770 ("static string required for tag name!", Nam);
1773 if VM_Target = No_VM then
1774 Set_Has_External_Tag_Rep_Clause (U_Ent);
1776 Error_Msg_Name_1 := Attr;
1778 ("% attribute unsupported in this configuration", Nam);
1781 if not Is_Library_Level_Entity (U_Ent) then
1783 ("?non-unique external tag supplied for &", N, U_Ent);
1785 ("?\same external tag applies to all subprogram calls", N);
1787 ("?\corresponding internal tag cannot be obtained", N);
1796 when Attribute_Input =>
1797 Analyze_Stream_TSS_Definition (TSS_Stream_Input);
1798 Set_Has_Specified_Stream_Input (Ent);
1804 -- Machine radix attribute definition clause
1806 when Attribute_Machine_Radix => Machine_Radix : declare
1807 Radix : constant Uint := Static_Integer (Expr);
1810 if not Is_Decimal_Fixed_Point_Type (U_Ent) then
1811 Error_Msg_N ("decimal fixed-point type expected for &", Nam);
1813 elsif Duplicate_Clause then
1816 elsif Radix /= No_Uint then
1817 Set_Has_Machine_Radix_Clause (U_Ent);
1818 Set_Has_Non_Standard_Rep (Base_Type (U_Ent));
1822 elsif Radix = 10 then
1823 Set_Machine_Radix_10 (U_Ent);
1825 Error_Msg_N ("machine radix value must be 2 or 10", Expr);
1834 -- Object_Size attribute definition clause
1836 when Attribute_Object_Size => Object_Size : declare
1837 Size : constant Uint := Static_Integer (Expr);
1840 pragma Warnings (Off, Biased);
1843 if not Is_Type (U_Ent) then
1844 Error_Msg_N ("Object_Size cannot be given for &", Nam);
1846 elsif Duplicate_Clause then
1850 Check_Size (Expr, U_Ent, Size, Biased);
1858 UI_Mod (Size, 64) /= 0
1861 ("Object_Size must be 8, 16, 32, or multiple of 64",
1865 Set_Esize (U_Ent, Size);
1866 Set_Has_Object_Size_Clause (U_Ent);
1867 Alignment_Check_For_Esize_Change (U_Ent);
1875 when Attribute_Output =>
1876 Analyze_Stream_TSS_Definition (TSS_Stream_Output);
1877 Set_Has_Specified_Stream_Output (Ent);
1883 when Attribute_Read =>
1884 Analyze_Stream_TSS_Definition (TSS_Stream_Read);
1885 Set_Has_Specified_Stream_Read (Ent);
1891 -- Size attribute definition clause
1893 when Attribute_Size => Size : declare
1894 Size : constant Uint := Static_Integer (Expr);
1901 if Duplicate_Clause then
1904 elsif not Is_Type (U_Ent)
1905 and then Ekind (U_Ent) /= E_Variable
1906 and then Ekind (U_Ent) /= E_Constant
1908 Error_Msg_N ("size cannot be given for &", Nam);
1910 elsif Is_Array_Type (U_Ent)
1911 and then not Is_Constrained (U_Ent)
1914 ("size cannot be given for unconstrained array", Nam);
1916 elsif Size /= No_Uint then
1918 if VM_Target /= No_VM and then not GNAT_Mode then
1920 -- Size clause is not handled properly on VM targets.
1921 -- Display a warning unless we are in GNAT mode, in which
1922 -- case this is useless.
1925 ("?size clauses are ignored in this configuration", N);
1928 if Is_Type (U_Ent) then
1931 Etyp := Etype (U_Ent);
1934 -- Check size, note that Gigi is in charge of checking that the
1935 -- size of an array or record type is OK. Also we do not check
1936 -- the size in the ordinary fixed-point case, since it is too
1937 -- early to do so (there may be subsequent small clause that
1938 -- affects the size). We can check the size if a small clause
1939 -- has already been given.
1941 if not Is_Ordinary_Fixed_Point_Type (U_Ent)
1942 or else Has_Small_Clause (U_Ent)
1944 Check_Size (Expr, Etyp, Size, Biased);
1945 Set_Biased (U_Ent, N, "size clause", Biased);
1948 -- For types set RM_Size and Esize if possible
1950 if Is_Type (U_Ent) then
1951 Set_RM_Size (U_Ent, Size);
1953 -- For scalar types, increase Object_Size to power of 2, but
1954 -- not less than a storage unit in any case (i.e., normally
1955 -- this means it will be byte addressable).
1957 if Is_Scalar_Type (U_Ent) then
1958 if Size <= System_Storage_Unit then
1959 Init_Esize (U_Ent, System_Storage_Unit);
1960 elsif Size <= 16 then
1961 Init_Esize (U_Ent, 16);
1962 elsif Size <= 32 then
1963 Init_Esize (U_Ent, 32);
1965 Set_Esize (U_Ent, (Size + 63) / 64 * 64);
1968 -- For all other types, object size = value size. The
1969 -- backend will adjust as needed.
1972 Set_Esize (U_Ent, Size);
1975 Alignment_Check_For_Esize_Change (U_Ent);
1977 -- For objects, set Esize only
1980 if Is_Elementary_Type (Etyp) then
1981 if Size /= System_Storage_Unit
1983 Size /= System_Storage_Unit * 2
1985 Size /= System_Storage_Unit * 4
1987 Size /= System_Storage_Unit * 8
1989 Error_Msg_Uint_1 := UI_From_Int (System_Storage_Unit);
1990 Error_Msg_Uint_2 := Error_Msg_Uint_1 * 8;
1992 ("size for primitive object must be a power of 2"
1993 & " in the range ^-^", N);
1997 Set_Esize (U_Ent, Size);
2000 Set_Has_Size_Clause (U_Ent);
2008 -- Small attribute definition clause
2010 when Attribute_Small => Small : declare
2011 Implicit_Base : constant Entity_Id := Base_Type (U_Ent);
2015 Analyze_And_Resolve (Expr, Any_Real);
2017 if Etype (Expr) = Any_Type then
2020 elsif not Is_Static_Expression (Expr) then
2021 Flag_Non_Static_Expr
2022 ("small requires static expression!", Expr);
2026 Small := Expr_Value_R (Expr);
2028 if Small <= Ureal_0 then
2029 Error_Msg_N ("small value must be greater than zero", Expr);
2035 if not Is_Ordinary_Fixed_Point_Type (U_Ent) then
2037 ("small requires an ordinary fixed point type", Nam);
2039 elsif Has_Small_Clause (U_Ent) then
2040 Error_Msg_N ("small already given for &", Nam);
2042 elsif Small > Delta_Value (U_Ent) then
2044 ("small value must not be greater then delta value", Nam);
2047 Set_Small_Value (U_Ent, Small);
2048 Set_Small_Value (Implicit_Base, Small);
2049 Set_Has_Small_Clause (U_Ent);
2050 Set_Has_Small_Clause (Implicit_Base);
2051 Set_Has_Non_Standard_Rep (Implicit_Base);
2059 -- Storage_Pool attribute definition clause
2061 when Attribute_Storage_Pool => Storage_Pool : declare
2066 if Ekind (U_Ent) = E_Access_Subprogram_Type then
2068 ("storage pool cannot be given for access-to-subprogram type",
2073 Ekind_In (U_Ent, E_Access_Type, E_General_Access_Type)
2076 ("storage pool can only be given for access types", Nam);
2079 elsif Is_Derived_Type (U_Ent) then
2081 ("storage pool cannot be given for a derived access type",
2084 elsif Duplicate_Clause then
2087 elsif Present (Associated_Storage_Pool (U_Ent)) then
2088 Error_Msg_N ("storage pool already given for &", Nam);
2093 (Expr, Class_Wide_Type (RTE (RE_Root_Storage_Pool)));
2095 if not Denotes_Variable (Expr) then
2096 Error_Msg_N ("storage pool must be a variable", Expr);
2100 if Nkind (Expr) = N_Type_Conversion then
2101 T := Etype (Expression (Expr));
2106 -- The Stack_Bounded_Pool is used internally for implementing
2107 -- access types with a Storage_Size. Since it only work
2108 -- properly when used on one specific type, we need to check
2109 -- that it is not hijacked improperly:
2110 -- type T is access Integer;
2111 -- for T'Storage_Size use n;
2112 -- type Q is access Float;
2113 -- for Q'Storage_Size use T'Storage_Size; -- incorrect
2115 if RTE_Available (RE_Stack_Bounded_Pool)
2116 and then Base_Type (T) = RTE (RE_Stack_Bounded_Pool)
2118 Error_Msg_N ("non-shareable internal Pool", Expr);
2122 -- If the argument is a name that is not an entity name, then
2123 -- we construct a renaming operation to define an entity of
2124 -- type storage pool.
2126 if not Is_Entity_Name (Expr)
2127 and then Is_Object_Reference (Expr)
2129 Pool := Make_Temporary (Loc, 'P', Expr);
2132 Rnode : constant Node_Id :=
2133 Make_Object_Renaming_Declaration (Loc,
2134 Defining_Identifier => Pool,
2136 New_Occurrence_Of (Etype (Expr), Loc),
2140 Insert_Before (N, Rnode);
2142 Set_Associated_Storage_Pool (U_Ent, Pool);
2145 elsif Is_Entity_Name (Expr) then
2146 Pool := Entity (Expr);
2148 -- If pool is a renamed object, get original one. This can
2149 -- happen with an explicit renaming, and within instances.
2151 while Present (Renamed_Object (Pool))
2152 and then Is_Entity_Name (Renamed_Object (Pool))
2154 Pool := Entity (Renamed_Object (Pool));
2157 if Present (Renamed_Object (Pool))
2158 and then Nkind (Renamed_Object (Pool)) = N_Type_Conversion
2159 and then Is_Entity_Name (Expression (Renamed_Object (Pool)))
2161 Pool := Entity (Expression (Renamed_Object (Pool)));
2164 Set_Associated_Storage_Pool (U_Ent, Pool);
2166 elsif Nkind (Expr) = N_Type_Conversion
2167 and then Is_Entity_Name (Expression (Expr))
2168 and then Nkind (Original_Node (Expr)) = N_Attribute_Reference
2170 Pool := Entity (Expression (Expr));
2171 Set_Associated_Storage_Pool (U_Ent, Pool);
2174 Error_Msg_N ("incorrect reference to a Storage Pool", Expr);
2183 -- Storage_Size attribute definition clause
2185 when Attribute_Storage_Size => Storage_Size : declare
2186 Btype : constant Entity_Id := Base_Type (U_Ent);
2190 if Is_Task_Type (U_Ent) then
2191 Check_Restriction (No_Obsolescent_Features, N);
2193 if Warn_On_Obsolescent_Feature then
2195 ("storage size clause for task is an " &
2196 "obsolescent feature (RM J.9)?", N);
2197 Error_Msg_N ("\use Storage_Size pragma instead?", N);
2203 if not Is_Access_Type (U_Ent)
2204 and then Ekind (U_Ent) /= E_Task_Type
2206 Error_Msg_N ("storage size cannot be given for &", Nam);
2208 elsif Is_Access_Type (U_Ent) and Is_Derived_Type (U_Ent) then
2210 ("storage size cannot be given for a derived access type",
2213 elsif Duplicate_Clause then
2217 Analyze_And_Resolve (Expr, Any_Integer);
2219 if Is_Access_Type (U_Ent) then
2220 if Present (Associated_Storage_Pool (U_Ent)) then
2221 Error_Msg_N ("storage pool already given for &", Nam);
2225 if Is_OK_Static_Expression (Expr)
2226 and then Expr_Value (Expr) = 0
2228 Set_No_Pool_Assigned (Btype);
2231 else -- Is_Task_Type (U_Ent)
2232 Sprag := Get_Rep_Pragma (Btype, Name_Storage_Size);
2234 if Present (Sprag) then
2235 Error_Msg_Sloc := Sloc (Sprag);
2237 ("Storage_Size already specified#", Nam);
2242 Set_Has_Storage_Size_Clause (Btype);
2250 when Attribute_Stream_Size => Stream_Size : declare
2251 Size : constant Uint := Static_Integer (Expr);
2254 if Ada_Version <= Ada_95 then
2255 Check_Restriction (No_Implementation_Attributes, N);
2258 if Duplicate_Clause then
2261 elsif Is_Elementary_Type (U_Ent) then
2262 if Size /= System_Storage_Unit
2264 Size /= System_Storage_Unit * 2
2266 Size /= System_Storage_Unit * 4
2268 Size /= System_Storage_Unit * 8
2270 Error_Msg_Uint_1 := UI_From_Int (System_Storage_Unit);
2272 ("stream size for elementary type must be a"
2273 & " power of 2 and at least ^", N);
2275 elsif RM_Size (U_Ent) > Size then
2276 Error_Msg_Uint_1 := RM_Size (U_Ent);
2278 ("stream size for elementary type must be a"
2279 & " power of 2 and at least ^", N);
2282 Set_Has_Stream_Size_Clause (U_Ent);
2285 Error_Msg_N ("Stream_Size cannot be given for &", Nam);
2293 -- Value_Size attribute definition clause
2295 when Attribute_Value_Size => Value_Size : declare
2296 Size : constant Uint := Static_Integer (Expr);
2300 if not Is_Type (U_Ent) then
2301 Error_Msg_N ("Value_Size cannot be given for &", Nam);
2303 elsif Duplicate_Clause then
2306 elsif Is_Array_Type (U_Ent)
2307 and then not Is_Constrained (U_Ent)
2310 ("Value_Size cannot be given for unconstrained array", Nam);
2313 if Is_Elementary_Type (U_Ent) then
2314 Check_Size (Expr, U_Ent, Size, Biased);
2315 Set_Biased (U_Ent, N, "value size clause", Biased);
2318 Set_RM_Size (U_Ent, Size);
2326 when Attribute_Write =>
2327 Analyze_Stream_TSS_Definition (TSS_Stream_Write);
2328 Set_Has_Specified_Stream_Write (Ent);
2330 -- All other attributes cannot be set
2334 ("attribute& cannot be set with definition clause", N);
2337 -- The test for the type being frozen must be performed after
2338 -- any expression the clause has been analyzed since the expression
2339 -- itself might cause freezing that makes the clause illegal.
2341 if Rep_Item_Too_Late (U_Ent, N, FOnly) then
2344 end Analyze_Attribute_Definition_Clause;
2346 ----------------------------
2347 -- Analyze_Code_Statement --
2348 ----------------------------
2350 procedure Analyze_Code_Statement (N : Node_Id) is
2351 HSS : constant Node_Id := Parent (N);
2352 SBody : constant Node_Id := Parent (HSS);
2353 Subp : constant Entity_Id := Current_Scope;
2360 -- Analyze and check we get right type, note that this implements the
2361 -- requirement (RM 13.8(1)) that Machine_Code be with'ed, since that
2362 -- is the only way that Asm_Insn could possibly be visible.
2364 Analyze_And_Resolve (Expression (N));
2366 if Etype (Expression (N)) = Any_Type then
2368 elsif Etype (Expression (N)) /= RTE (RE_Asm_Insn) then
2369 Error_Msg_N ("incorrect type for code statement", N);
2373 Check_Code_Statement (N);
2375 -- Make sure we appear in the handled statement sequence of a
2376 -- subprogram (RM 13.8(3)).
2378 if Nkind (HSS) /= N_Handled_Sequence_Of_Statements
2379 or else Nkind (SBody) /= N_Subprogram_Body
2382 ("code statement can only appear in body of subprogram", N);
2386 -- Do remaining checks (RM 13.8(3)) if not already done
2388 if not Is_Machine_Code_Subprogram (Subp) then
2389 Set_Is_Machine_Code_Subprogram (Subp);
2391 -- No exception handlers allowed
2393 if Present (Exception_Handlers (HSS)) then
2395 ("exception handlers not permitted in machine code subprogram",
2396 First (Exception_Handlers (HSS)));
2399 -- No declarations other than use clauses and pragmas (we allow
2400 -- certain internally generated declarations as well).
2402 Decl := First (Declarations (SBody));
2403 while Present (Decl) loop
2404 DeclO := Original_Node (Decl);
2405 if Comes_From_Source (DeclO)
2406 and not Nkind_In (DeclO, N_Pragma,
2407 N_Use_Package_Clause,
2409 N_Implicit_Label_Declaration)
2412 ("this declaration not allowed in machine code subprogram",
2419 -- No statements other than code statements, pragmas, and labels.
2420 -- Again we allow certain internally generated statements.
2422 Stmt := First (Statements (HSS));
2423 while Present (Stmt) loop
2424 StmtO := Original_Node (Stmt);
2425 if Comes_From_Source (StmtO)
2426 and then not Nkind_In (StmtO, N_Pragma,
2431 ("this statement is not allowed in machine code subprogram",
2438 end Analyze_Code_Statement;
2440 -----------------------------------------------
2441 -- Analyze_Enumeration_Representation_Clause --
2442 -----------------------------------------------
2444 procedure Analyze_Enumeration_Representation_Clause (N : Node_Id) is
2445 Ident : constant Node_Id := Identifier (N);
2446 Aggr : constant Node_Id := Array_Aggregate (N);
2447 Enumtype : Entity_Id;
2453 Err : Boolean := False;
2455 Lo : constant Uint := Expr_Value (Type_Low_Bound (Universal_Integer));
2456 Hi : constant Uint := Expr_Value (Type_High_Bound (Universal_Integer));
2457 -- Allowed range of universal integer (= allowed range of enum lit vals)
2461 -- Minimum and maximum values of entries
2464 -- Pointer to node for literal providing max value
2467 if Ignore_Rep_Clauses then
2471 -- First some basic error checks
2474 Enumtype := Entity (Ident);
2476 if Enumtype = Any_Type
2477 or else Rep_Item_Too_Early (Enumtype, N)
2481 Enumtype := Underlying_Type (Enumtype);
2484 if not Is_Enumeration_Type (Enumtype) then
2486 ("enumeration type required, found}",
2487 Ident, First_Subtype (Enumtype));
2491 -- Ignore rep clause on generic actual type. This will already have
2492 -- been flagged on the template as an error, and this is the safest
2493 -- way to ensure we don't get a junk cascaded message in the instance.
2495 if Is_Generic_Actual_Type (Enumtype) then
2498 -- Type must be in current scope
2500 elsif Scope (Enumtype) /= Current_Scope then
2501 Error_Msg_N ("type must be declared in this scope", Ident);
2504 -- Type must be a first subtype
2506 elsif not Is_First_Subtype (Enumtype) then
2507 Error_Msg_N ("cannot give enumeration rep clause for subtype", N);
2510 -- Ignore duplicate rep clause
2512 elsif Has_Enumeration_Rep_Clause (Enumtype) then
2513 Error_Msg_N ("duplicate enumeration rep clause ignored", N);
2516 -- Don't allow rep clause for standard [wide_[wide_]]character
2518 elsif Is_Standard_Character_Type (Enumtype) then
2519 Error_Msg_N ("enumeration rep clause not allowed for this type", N);
2522 -- Check that the expression is a proper aggregate (no parentheses)
2524 elsif Paren_Count (Aggr) /= 0 then
2526 ("extra parentheses surrounding aggregate not allowed",
2530 -- All tests passed, so set rep clause in place
2533 Set_Has_Enumeration_Rep_Clause (Enumtype);
2534 Set_Has_Enumeration_Rep_Clause (Base_Type (Enumtype));
2537 -- Now we process the aggregate. Note that we don't use the normal
2538 -- aggregate code for this purpose, because we don't want any of the
2539 -- normal expansion activities, and a number of special semantic
2540 -- rules apply (including the component type being any integer type)
2542 Elit := First_Literal (Enumtype);
2544 -- First the positional entries if any
2546 if Present (Expressions (Aggr)) then
2547 Expr := First (Expressions (Aggr));
2548 while Present (Expr) loop
2550 Error_Msg_N ("too many entries in aggregate", Expr);
2554 Val := Static_Integer (Expr);
2556 -- Err signals that we found some incorrect entries processing
2557 -- the list. The final checks for completeness and ordering are
2558 -- skipped in this case.
2560 if Val = No_Uint then
2562 elsif Val < Lo or else Hi < Val then
2563 Error_Msg_N ("value outside permitted range", Expr);
2567 Set_Enumeration_Rep (Elit, Val);
2568 Set_Enumeration_Rep_Expr (Elit, Expr);
2574 -- Now process the named entries if present
2576 if Present (Component_Associations (Aggr)) then
2577 Assoc := First (Component_Associations (Aggr));
2578 while Present (Assoc) loop
2579 Choice := First (Choices (Assoc));
2581 if Present (Next (Choice)) then
2583 ("multiple choice not allowed here", Next (Choice));
2587 if Nkind (Choice) = N_Others_Choice then
2588 Error_Msg_N ("others choice not allowed here", Choice);
2591 elsif Nkind (Choice) = N_Range then
2592 -- ??? should allow zero/one element range here
2593 Error_Msg_N ("range not allowed here", Choice);
2597 Analyze_And_Resolve (Choice, Enumtype);
2599 if Is_Entity_Name (Choice)
2600 and then Is_Type (Entity (Choice))
2602 Error_Msg_N ("subtype name not allowed here", Choice);
2604 -- ??? should allow static subtype with zero/one entry
2606 elsif Etype (Choice) = Base_Type (Enumtype) then
2607 if not Is_Static_Expression (Choice) then
2608 Flag_Non_Static_Expr
2609 ("non-static expression used for choice!", Choice);
2613 Elit := Expr_Value_E (Choice);
2615 if Present (Enumeration_Rep_Expr (Elit)) then
2616 Error_Msg_Sloc := Sloc (Enumeration_Rep_Expr (Elit));
2618 ("representation for& previously given#",
2623 Set_Enumeration_Rep_Expr (Elit, Expression (Assoc));
2625 Expr := Expression (Assoc);
2626 Val := Static_Integer (Expr);
2628 if Val = No_Uint then
2631 elsif Val < Lo or else Hi < Val then
2632 Error_Msg_N ("value outside permitted range", Expr);
2636 Set_Enumeration_Rep (Elit, Val);
2645 -- Aggregate is fully processed. Now we check that a full set of
2646 -- representations was given, and that they are in range and in order.
2647 -- These checks are only done if no other errors occurred.
2653 Elit := First_Literal (Enumtype);
2654 while Present (Elit) loop
2655 if No (Enumeration_Rep_Expr (Elit)) then
2656 Error_Msg_NE ("missing representation for&!", N, Elit);
2659 Val := Enumeration_Rep (Elit);
2661 if Min = No_Uint then
2665 if Val /= No_Uint then
2666 if Max /= No_Uint and then Val <= Max then
2668 ("enumeration value for& not ordered!",
2669 Enumeration_Rep_Expr (Elit), Elit);
2672 Max_Node := Enumeration_Rep_Expr (Elit);
2676 -- If there is at least one literal whose representation is not
2677 -- equal to the Pos value, then note that this enumeration type
2678 -- has a non-standard representation.
2680 if Val /= Enumeration_Pos (Elit) then
2681 Set_Has_Non_Standard_Rep (Base_Type (Enumtype));
2688 -- Now set proper size information
2691 Minsize : Uint := UI_From_Int (Minimum_Size (Enumtype));
2694 if Has_Size_Clause (Enumtype) then
2696 -- All OK, if size is OK now
2698 if RM_Size (Enumtype) >= Minsize then
2702 -- Try if we can get by with biasing
2705 UI_From_Int (Minimum_Size (Enumtype, Biased => True));
2707 -- Error message if even biasing does not work
2709 if RM_Size (Enumtype) < Minsize then
2710 Error_Msg_Uint_1 := RM_Size (Enumtype);
2711 Error_Msg_Uint_2 := Max;
2713 ("previously given size (^) is too small "
2714 & "for this value (^)", Max_Node);
2716 -- If biasing worked, indicate that we now have biased rep
2720 (Enumtype, Size_Clause (Enumtype), "size clause");
2725 Set_RM_Size (Enumtype, Minsize);
2726 Set_Enum_Esize (Enumtype);
2729 Set_RM_Size (Base_Type (Enumtype), RM_Size (Enumtype));
2730 Set_Esize (Base_Type (Enumtype), Esize (Enumtype));
2731 Set_Alignment (Base_Type (Enumtype), Alignment (Enumtype));
2735 -- We repeat the too late test in case it froze itself!
2737 if Rep_Item_Too_Late (Enumtype, N) then
2740 end Analyze_Enumeration_Representation_Clause;
2742 ----------------------------
2743 -- Analyze_Free_Statement --
2744 ----------------------------
2746 procedure Analyze_Free_Statement (N : Node_Id) is
2748 Analyze (Expression (N));
2749 end Analyze_Free_Statement;
2751 ---------------------------
2752 -- Analyze_Freeze_Entity --
2753 ---------------------------
2755 procedure Analyze_Freeze_Entity (N : Node_Id) is
2756 E : constant Entity_Id := Entity (N);
2759 -- Remember that we are processing a freezing entity. Required to
2760 -- ensure correct decoration of internal entities associated with
2761 -- interfaces (see New_Overloaded_Entity).
2763 Inside_Freezing_Actions := Inside_Freezing_Actions + 1;
2765 -- For tagged types covering interfaces add internal entities that link
2766 -- the primitives of the interfaces with the primitives that cover them.
2767 -- Note: These entities were originally generated only when generating
2768 -- code because their main purpose was to provide support to initialize
2769 -- the secondary dispatch tables. They are now generated also when
2770 -- compiling with no code generation to provide ASIS the relationship
2771 -- between interface primitives and tagged type primitives. They are
2772 -- also used to locate primitives covering interfaces when processing
2773 -- generics (see Derive_Subprograms).
2775 if Ada_Version >= Ada_2005
2776 and then Ekind (E) = E_Record_Type
2777 and then Is_Tagged_Type (E)
2778 and then not Is_Interface (E)
2779 and then Has_Interfaces (E)
2781 -- This would be a good common place to call the routine that checks
2782 -- overriding of interface primitives (and thus factorize calls to
2783 -- Check_Abstract_Overriding located at different contexts in the
2784 -- compiler). However, this is not possible because it causes
2785 -- spurious errors in case of late overriding.
2787 Add_Internal_Interface_Entities (E);
2792 if Ekind (E) = E_Record_Type
2793 and then Is_CPP_Class (E)
2794 and then Is_Tagged_Type (E)
2795 and then Tagged_Type_Expansion
2796 and then Expander_Active
2798 if CPP_Num_Prims (E) = 0 then
2800 -- If the CPP type has user defined components then it must import
2801 -- primitives from C++. This is required because if the C++ class
2802 -- has no primitives then the C++ compiler does not added the _tag
2803 -- component to the type.
2805 pragma Assert (Chars (First_Entity (E)) = Name_uTag);
2807 if First_Entity (E) /= Last_Entity (E) then
2809 ("?'C'P'P type must import at least one primitive from C++",
2814 -- Check that all its primitives are abstract or imported from C++.
2815 -- Check also availability of the C++ constructor.
2818 Has_Constructors : constant Boolean := Has_CPP_Constructors (E);
2820 Error_Reported : Boolean := False;
2824 Elmt := First_Elmt (Primitive_Operations (E));
2825 while Present (Elmt) loop
2826 Prim := Node (Elmt);
2828 if Comes_From_Source (Prim) then
2829 if Is_Abstract_Subprogram (Prim) then
2832 elsif not Is_Imported (Prim)
2833 or else Convention (Prim) /= Convention_CPP
2836 ("?primitives of 'C'P'P types must be imported from C++"
2837 & " or abstract", Prim);
2839 elsif not Has_Constructors
2840 and then not Error_Reported
2842 Error_Msg_Name_1 := Chars (E);
2844 ("?'C'P'P constructor required for type %", Prim);
2845 Error_Reported := True;
2854 Inside_Freezing_Actions := Inside_Freezing_Actions - 1;
2855 end Analyze_Freeze_Entity;
2857 ------------------------------------------
2858 -- Analyze_Record_Representation_Clause --
2859 ------------------------------------------
2861 -- Note: we check as much as we can here, but we can't do any checks
2862 -- based on the position values (e.g. overlap checks) until freeze time
2863 -- because especially in Ada 2005 (machine scalar mode), the processing
2864 -- for non-standard bit order can substantially change the positions.
2865 -- See procedure Check_Record_Representation_Clause (called from Freeze)
2866 -- for the remainder of this processing.
2868 procedure Analyze_Record_Representation_Clause (N : Node_Id) is
2869 Ident : constant Node_Id := Identifier (N);
2874 Hbit : Uint := Uint_0;
2878 Rectype : Entity_Id;
2880 CR_Pragma : Node_Id := Empty;
2881 -- Points to N_Pragma node if Complete_Representation pragma present
2884 if Ignore_Rep_Clauses then
2889 Rectype := Entity (Ident);
2891 if Rectype = Any_Type
2892 or else Rep_Item_Too_Early (Rectype, N)
2896 Rectype := Underlying_Type (Rectype);
2899 -- First some basic error checks
2901 if not Is_Record_Type (Rectype) then
2903 ("record type required, found}", Ident, First_Subtype (Rectype));
2906 elsif Scope (Rectype) /= Current_Scope then
2907 Error_Msg_N ("type must be declared in this scope", N);
2910 elsif not Is_First_Subtype (Rectype) then
2911 Error_Msg_N ("cannot give record rep clause for subtype", N);
2914 elsif Has_Record_Rep_Clause (Rectype) then
2915 Error_Msg_N ("duplicate record rep clause ignored", N);
2918 elsif Rep_Item_Too_Late (Rectype, N) then
2922 if Present (Mod_Clause (N)) then
2924 Loc : constant Source_Ptr := Sloc (N);
2925 M : constant Node_Id := Mod_Clause (N);
2926 P : constant List_Id := Pragmas_Before (M);
2930 pragma Warnings (Off, Mod_Val);
2933 Check_Restriction (No_Obsolescent_Features, Mod_Clause (N));
2935 if Warn_On_Obsolescent_Feature then
2937 ("mod clause is an obsolescent feature (RM J.8)?", N);
2939 ("\use alignment attribute definition clause instead?", N);
2946 -- In ASIS_Mode mode, expansion is disabled, but we must convert
2947 -- the Mod clause into an alignment clause anyway, so that the
2948 -- back-end can compute and back-annotate properly the size and
2949 -- alignment of types that may include this record.
2951 -- This seems dubious, this destroys the source tree in a manner
2952 -- not detectable by ASIS ???
2954 if Operating_Mode = Check_Semantics
2958 Make_Attribute_Definition_Clause (Loc,
2959 Name => New_Reference_To (Base_Type (Rectype), Loc),
2960 Chars => Name_Alignment,
2961 Expression => Relocate_Node (Expression (M)));
2963 Set_From_At_Mod (AtM_Nod);
2964 Insert_After (N, AtM_Nod);
2965 Mod_Val := Get_Alignment_Value (Expression (AtM_Nod));
2966 Set_Mod_Clause (N, Empty);
2969 -- Get the alignment value to perform error checking
2971 Mod_Val := Get_Alignment_Value (Expression (M));
2976 -- For untagged types, clear any existing component clauses for the
2977 -- type. If the type is derived, this is what allows us to override
2978 -- a rep clause for the parent. For type extensions, the representation
2979 -- of the inherited components is inherited, so we want to keep previous
2980 -- component clauses for completeness.
2982 if not Is_Tagged_Type (Rectype) then
2983 Comp := First_Component_Or_Discriminant (Rectype);
2984 while Present (Comp) loop
2985 Set_Component_Clause (Comp, Empty);
2986 Next_Component_Or_Discriminant (Comp);
2990 -- All done if no component clauses
2992 CC := First (Component_Clauses (N));
2998 -- A representation like this applies to the base type
3000 Set_Has_Record_Rep_Clause (Base_Type (Rectype));
3001 Set_Has_Non_Standard_Rep (Base_Type (Rectype));
3002 Set_Has_Specified_Layout (Base_Type (Rectype));
3004 -- Process the component clauses
3006 while Present (CC) loop
3010 if Nkind (CC) = N_Pragma then
3013 -- The only pragma of interest is Complete_Representation
3015 if Pragma_Name (CC) = Name_Complete_Representation then
3019 -- Processing for real component clause
3022 Posit := Static_Integer (Position (CC));
3023 Fbit := Static_Integer (First_Bit (CC));
3024 Lbit := Static_Integer (Last_Bit (CC));
3027 and then Fbit /= No_Uint
3028 and then Lbit /= No_Uint
3032 ("position cannot be negative", Position (CC));
3036 ("first bit cannot be negative", First_Bit (CC));
3038 -- The Last_Bit specified in a component clause must not be
3039 -- less than the First_Bit minus one (RM-13.5.1(10)).
3041 elsif Lbit < Fbit - 1 then
3043 ("last bit cannot be less than first bit minus one",
3046 -- Values look OK, so find the corresponding record component
3047 -- Even though the syntax allows an attribute reference for
3048 -- implementation-defined components, GNAT does not allow the
3049 -- tag to get an explicit position.
3051 elsif Nkind (Component_Name (CC)) = N_Attribute_Reference then
3052 if Attribute_Name (Component_Name (CC)) = Name_Tag then
3053 Error_Msg_N ("position of tag cannot be specified", CC);
3055 Error_Msg_N ("illegal component name", CC);
3059 Comp := First_Entity (Rectype);
3060 while Present (Comp) loop
3061 exit when Chars (Comp) = Chars (Component_Name (CC));
3067 -- Maybe component of base type that is absent from
3068 -- statically constrained first subtype.
3070 Comp := First_Entity (Base_Type (Rectype));
3071 while Present (Comp) loop
3072 exit when Chars (Comp) = Chars (Component_Name (CC));
3079 ("component clause is for non-existent field", CC);
3081 -- Ada 2012 (AI05-0026): Any name that denotes a
3082 -- discriminant of an object of an unchecked union type
3083 -- shall not occur within a record_representation_clause.
3085 -- The general restriction of using record rep clauses on
3086 -- Unchecked_Union types has now been lifted. Since it is
3087 -- possible to introduce a record rep clause which mentions
3088 -- the discriminant of an Unchecked_Union in non-Ada 2012
3089 -- code, this check is applied to all versions of the
3092 elsif Ekind (Comp) = E_Discriminant
3093 and then Is_Unchecked_Union (Rectype)
3096 ("cannot reference discriminant of Unchecked_Union",
3097 Component_Name (CC));
3099 elsif Present (Component_Clause (Comp)) then
3101 -- Diagnose duplicate rep clause, or check consistency
3102 -- if this is an inherited component. In a double fault,
3103 -- there may be a duplicate inconsistent clause for an
3104 -- inherited component.
3106 if Scope (Original_Record_Component (Comp)) = Rectype
3107 or else Parent (Component_Clause (Comp)) = N
3109 Error_Msg_Sloc := Sloc (Component_Clause (Comp));
3110 Error_Msg_N ("component clause previously given#", CC);
3114 Rep1 : constant Node_Id := Component_Clause (Comp);
3116 if Intval (Position (Rep1)) /=
3117 Intval (Position (CC))
3118 or else Intval (First_Bit (Rep1)) /=
3119 Intval (First_Bit (CC))
3120 or else Intval (Last_Bit (Rep1)) /=
3121 Intval (Last_Bit (CC))
3123 Error_Msg_N ("component clause inconsistent "
3124 & "with representation of ancestor", CC);
3125 elsif Warn_On_Redundant_Constructs then
3126 Error_Msg_N ("?redundant component clause "
3127 & "for inherited component!", CC);
3132 -- Normal case where this is the first component clause we
3133 -- have seen for this entity, so set it up properly.
3136 -- Make reference for field in record rep clause and set
3137 -- appropriate entity field in the field identifier.
3140 (Comp, Component_Name (CC), Set_Ref => False);
3141 Set_Entity (Component_Name (CC), Comp);
3143 -- Update Fbit and Lbit to the actual bit number
3145 Fbit := Fbit + UI_From_Int (SSU) * Posit;
3146 Lbit := Lbit + UI_From_Int (SSU) * Posit;
3148 if Has_Size_Clause (Rectype)
3149 and then Esize (Rectype) <= Lbit
3152 ("bit number out of range of specified size",
3155 Set_Component_Clause (Comp, CC);
3156 Set_Component_Bit_Offset (Comp, Fbit);
3157 Set_Esize (Comp, 1 + (Lbit - Fbit));
3158 Set_Normalized_First_Bit (Comp, Fbit mod SSU);
3159 Set_Normalized_Position (Comp, Fbit / SSU);
3161 if Warn_On_Overridden_Size
3162 and then Has_Size_Clause (Etype (Comp))
3163 and then RM_Size (Etype (Comp)) /= Esize (Comp)
3166 ("?component size overrides size clause for&",
3167 Component_Name (CC), Etype (Comp));
3170 -- This information is also set in the corresponding
3171 -- component of the base type, found by accessing the
3172 -- Original_Record_Component link if it is present.
3174 Ocomp := Original_Record_Component (Comp);
3181 (Component_Name (CC),
3187 (Comp, First_Node (CC), "component clause", Biased);
3189 if Present (Ocomp) then
3190 Set_Component_Clause (Ocomp, CC);
3191 Set_Component_Bit_Offset (Ocomp, Fbit);
3192 Set_Normalized_First_Bit (Ocomp, Fbit mod SSU);
3193 Set_Normalized_Position (Ocomp, Fbit / SSU);
3194 Set_Esize (Ocomp, 1 + (Lbit - Fbit));
3196 Set_Normalized_Position_Max
3197 (Ocomp, Normalized_Position (Ocomp));
3199 -- Note: we don't use Set_Biased here, because we
3200 -- already gave a warning above if needed, and we
3201 -- would get a duplicate for the same name here.
3203 Set_Has_Biased_Representation
3204 (Ocomp, Has_Biased_Representation (Comp));
3207 if Esize (Comp) < 0 then
3208 Error_Msg_N ("component size is negative", CC);
3219 -- Check missing components if Complete_Representation pragma appeared
3221 if Present (CR_Pragma) then
3222 Comp := First_Component_Or_Discriminant (Rectype);
3223 while Present (Comp) loop
3224 if No (Component_Clause (Comp)) then
3226 ("missing component clause for &", CR_Pragma, Comp);
3229 Next_Component_Or_Discriminant (Comp);
3232 -- If no Complete_Representation pragma, warn if missing components
3234 elsif Warn_On_Unrepped_Components then
3236 Num_Repped_Components : Nat := 0;
3237 Num_Unrepped_Components : Nat := 0;
3240 -- First count number of repped and unrepped components
3242 Comp := First_Component_Or_Discriminant (Rectype);
3243 while Present (Comp) loop
3244 if Present (Component_Clause (Comp)) then
3245 Num_Repped_Components := Num_Repped_Components + 1;
3247 Num_Unrepped_Components := Num_Unrepped_Components + 1;
3250 Next_Component_Or_Discriminant (Comp);
3253 -- We are only interested in the case where there is at least one
3254 -- unrepped component, and at least half the components have rep
3255 -- clauses. We figure that if less than half have them, then the
3256 -- partial rep clause is really intentional. If the component
3257 -- type has no underlying type set at this point (as for a generic
3258 -- formal type), we don't know enough to give a warning on the
3261 if Num_Unrepped_Components > 0
3262 and then Num_Unrepped_Components < Num_Repped_Components
3264 Comp := First_Component_Or_Discriminant (Rectype);
3265 while Present (Comp) loop
3266 if No (Component_Clause (Comp))
3267 and then Comes_From_Source (Comp)
3268 and then Present (Underlying_Type (Etype (Comp)))
3269 and then (Is_Scalar_Type (Underlying_Type (Etype (Comp)))
3270 or else Size_Known_At_Compile_Time
3271 (Underlying_Type (Etype (Comp))))
3272 and then not Has_Warnings_Off (Rectype)
3274 Error_Msg_Sloc := Sloc (Comp);
3276 ("?no component clause given for & declared #",
3280 Next_Component_Or_Discriminant (Comp);
3285 end Analyze_Record_Representation_Clause;
3287 -----------------------------------
3288 -- Check_Constant_Address_Clause --
3289 -----------------------------------
3291 procedure Check_Constant_Address_Clause
3295 procedure Check_At_Constant_Address (Nod : Node_Id);
3296 -- Checks that the given node N represents a name whose 'Address is
3297 -- constant (in the same sense as OK_Constant_Address_Clause, i.e. the
3298 -- address value is the same at the point of declaration of U_Ent and at
3299 -- the time of elaboration of the address clause.
3301 procedure Check_Expr_Constants (Nod : Node_Id);
3302 -- Checks that Nod meets the requirements for a constant address clause
3303 -- in the sense of the enclosing procedure.
3305 procedure Check_List_Constants (Lst : List_Id);
3306 -- Check that all elements of list Lst meet the requirements for a
3307 -- constant address clause in the sense of the enclosing procedure.
3309 -------------------------------
3310 -- Check_At_Constant_Address --
3311 -------------------------------
3313 procedure Check_At_Constant_Address (Nod : Node_Id) is
3315 if Is_Entity_Name (Nod) then
3316 if Present (Address_Clause (Entity ((Nod)))) then
3318 ("invalid address clause for initialized object &!",
3321 ("address for& cannot" &
3322 " depend on another address clause! (RM 13.1(22))!",
3325 elsif In_Same_Source_Unit (Entity (Nod), U_Ent)
3326 and then Sloc (U_Ent) < Sloc (Entity (Nod))
3329 ("invalid address clause for initialized object &!",
3331 Error_Msg_Node_2 := U_Ent;
3333 ("\& must be defined before & (RM 13.1(22))!",
3337 elsif Nkind (Nod) = N_Selected_Component then
3339 T : constant Entity_Id := Etype (Prefix (Nod));
3342 if (Is_Record_Type (T)
3343 and then Has_Discriminants (T))
3346 and then Is_Record_Type (Designated_Type (T))
3347 and then Has_Discriminants (Designated_Type (T)))
3350 ("invalid address clause for initialized object &!",
3353 ("\address cannot depend on component" &
3354 " of discriminated record (RM 13.1(22))!",
3357 Check_At_Constant_Address (Prefix (Nod));
3361 elsif Nkind (Nod) = N_Indexed_Component then
3362 Check_At_Constant_Address (Prefix (Nod));
3363 Check_List_Constants (Expressions (Nod));
3366 Check_Expr_Constants (Nod);
3368 end Check_At_Constant_Address;
3370 --------------------------
3371 -- Check_Expr_Constants --
3372 --------------------------
3374 procedure Check_Expr_Constants (Nod : Node_Id) is
3375 Loc_U_Ent : constant Source_Ptr := Sloc (U_Ent);
3376 Ent : Entity_Id := Empty;
3379 if Nkind (Nod) in N_Has_Etype
3380 and then Etype (Nod) = Any_Type
3386 when N_Empty | N_Error =>
3389 when N_Identifier | N_Expanded_Name =>
3390 Ent := Entity (Nod);
3392 -- We need to look at the original node if it is different
3393 -- from the node, since we may have rewritten things and
3394 -- substituted an identifier representing the rewrite.
3396 if Original_Node (Nod) /= Nod then
3397 Check_Expr_Constants (Original_Node (Nod));
3399 -- If the node is an object declaration without initial
3400 -- value, some code has been expanded, and the expression
3401 -- is not constant, even if the constituents might be
3402 -- acceptable, as in A'Address + offset.
3404 if Ekind (Ent) = E_Variable
3406 Nkind (Declaration_Node (Ent)) = N_Object_Declaration
3408 No (Expression (Declaration_Node (Ent)))
3411 ("invalid address clause for initialized object &!",
3414 -- If entity is constant, it may be the result of expanding
3415 -- a check. We must verify that its declaration appears
3416 -- before the object in question, else we also reject the
3419 elsif Ekind (Ent) = E_Constant
3420 and then In_Same_Source_Unit (Ent, U_Ent)
3421 and then Sloc (Ent) > Loc_U_Ent
3424 ("invalid address clause for initialized object &!",
3431 -- Otherwise look at the identifier and see if it is OK
3433 if Ekind_In (Ent, E_Named_Integer, E_Named_Real)
3434 or else Is_Type (Ent)
3439 Ekind (Ent) = E_Constant
3441 Ekind (Ent) = E_In_Parameter
3443 -- This is the case where we must have Ent defined before
3444 -- U_Ent. Clearly if they are in different units this
3445 -- requirement is met since the unit containing Ent is
3446 -- already processed.
3448 if not In_Same_Source_Unit (Ent, U_Ent) then
3451 -- Otherwise location of Ent must be before the location
3452 -- of U_Ent, that's what prior defined means.
3454 elsif Sloc (Ent) < Loc_U_Ent then
3459 ("invalid address clause for initialized object &!",
3461 Error_Msg_Node_2 := U_Ent;
3463 ("\& must be defined before & (RM 13.1(22))!",
3467 elsif Nkind (Original_Node (Nod)) = N_Function_Call then
3468 Check_Expr_Constants (Original_Node (Nod));
3472 ("invalid address clause for initialized object &!",
3475 if Comes_From_Source (Ent) then
3477 ("\reference to variable& not allowed"
3478 & " (RM 13.1(22))!", Nod, Ent);
3481 ("non-static expression not allowed"
3482 & " (RM 13.1(22))!", Nod);
3486 when N_Integer_Literal =>
3488 -- If this is a rewritten unchecked conversion, in a system
3489 -- where Address is an integer type, always use the base type
3490 -- for a literal value. This is user-friendly and prevents
3491 -- order-of-elaboration issues with instances of unchecked
3494 if Nkind (Original_Node (Nod)) = N_Function_Call then
3495 Set_Etype (Nod, Base_Type (Etype (Nod)));
3498 when N_Real_Literal |
3500 N_Character_Literal =>
3504 Check_Expr_Constants (Low_Bound (Nod));
3505 Check_Expr_Constants (High_Bound (Nod));
3507 when N_Explicit_Dereference =>
3508 Check_Expr_Constants (Prefix (Nod));
3510 when N_Indexed_Component =>
3511 Check_Expr_Constants (Prefix (Nod));
3512 Check_List_Constants (Expressions (Nod));
3515 Check_Expr_Constants (Prefix (Nod));
3516 Check_Expr_Constants (Discrete_Range (Nod));
3518 when N_Selected_Component =>
3519 Check_Expr_Constants (Prefix (Nod));
3521 when N_Attribute_Reference =>
3522 if Attribute_Name (Nod) = Name_Address
3524 Attribute_Name (Nod) = Name_Access
3526 Attribute_Name (Nod) = Name_Unchecked_Access
3528 Attribute_Name (Nod) = Name_Unrestricted_Access
3530 Check_At_Constant_Address (Prefix (Nod));
3533 Check_Expr_Constants (Prefix (Nod));
3534 Check_List_Constants (Expressions (Nod));
3538 Check_List_Constants (Component_Associations (Nod));
3539 Check_List_Constants (Expressions (Nod));
3541 when N_Component_Association =>
3542 Check_Expr_Constants (Expression (Nod));
3544 when N_Extension_Aggregate =>
3545 Check_Expr_Constants (Ancestor_Part (Nod));
3546 Check_List_Constants (Component_Associations (Nod));
3547 Check_List_Constants (Expressions (Nod));
3552 when N_Binary_Op | N_Short_Circuit | N_Membership_Test =>
3553 Check_Expr_Constants (Left_Opnd (Nod));
3554 Check_Expr_Constants (Right_Opnd (Nod));
3557 Check_Expr_Constants (Right_Opnd (Nod));
3559 when N_Type_Conversion |
3560 N_Qualified_Expression |
3562 Check_Expr_Constants (Expression (Nod));
3564 when N_Unchecked_Type_Conversion =>
3565 Check_Expr_Constants (Expression (Nod));
3567 -- If this is a rewritten unchecked conversion, subtypes in
3568 -- this node are those created within the instance. To avoid
3569 -- order of elaboration issues, replace them with their base
3570 -- types. Note that address clauses can cause order of
3571 -- elaboration problems because they are elaborated by the
3572 -- back-end at the point of definition, and may mention
3573 -- entities declared in between (as long as everything is
3574 -- static). It is user-friendly to allow unchecked conversions
3577 if Nkind (Original_Node (Nod)) = N_Function_Call then
3578 Set_Etype (Expression (Nod),
3579 Base_Type (Etype (Expression (Nod))));
3580 Set_Etype (Nod, Base_Type (Etype (Nod)));
3583 when N_Function_Call =>
3584 if not Is_Pure (Entity (Name (Nod))) then
3586 ("invalid address clause for initialized object &!",
3590 ("\function & is not pure (RM 13.1(22))!",
3591 Nod, Entity (Name (Nod)));
3594 Check_List_Constants (Parameter_Associations (Nod));
3597 when N_Parameter_Association =>
3598 Check_Expr_Constants (Explicit_Actual_Parameter (Nod));
3602 ("invalid address clause for initialized object &!",
3605 ("\must be constant defined before& (RM 13.1(22))!",
3608 end Check_Expr_Constants;
3610 --------------------------
3611 -- Check_List_Constants --
3612 --------------------------
3614 procedure Check_List_Constants (Lst : List_Id) is
3618 if Present (Lst) then
3619 Nod1 := First (Lst);
3620 while Present (Nod1) loop
3621 Check_Expr_Constants (Nod1);
3625 end Check_List_Constants;
3627 -- Start of processing for Check_Constant_Address_Clause
3630 -- If rep_clauses are to be ignored, no need for legality checks. In
3631 -- particular, no need to pester user about rep clauses that violate
3632 -- the rule on constant addresses, given that these clauses will be
3633 -- removed by Freeze before they reach the back end.
3635 if not Ignore_Rep_Clauses then
3636 Check_Expr_Constants (Expr);
3638 end Check_Constant_Address_Clause;
3640 ----------------------------------------
3641 -- Check_Record_Representation_Clause --
3642 ----------------------------------------
3644 procedure Check_Record_Representation_Clause (N : Node_Id) is
3645 Loc : constant Source_Ptr := Sloc (N);
3646 Ident : constant Node_Id := Identifier (N);
3647 Rectype : Entity_Id;
3652 Hbit : Uint := Uint_0;
3656 Max_Bit_So_Far : Uint;
3657 -- Records the maximum bit position so far. If all field positions
3658 -- are monotonically increasing, then we can skip the circuit for
3659 -- checking for overlap, since no overlap is possible.
3661 Tagged_Parent : Entity_Id := Empty;
3662 -- This is set in the case of a derived tagged type for which we have
3663 -- Is_Fully_Repped_Tagged_Type True (indicating that all components are
3664 -- positioned by record representation clauses). In this case we must
3665 -- check for overlap between components of this tagged type, and the
3666 -- components of its parent. Tagged_Parent will point to this parent
3667 -- type. For all other cases Tagged_Parent is left set to Empty.
3669 Parent_Last_Bit : Uint;
3670 -- Relevant only if Tagged_Parent is set, Parent_Last_Bit indicates the
3671 -- last bit position for any field in the parent type. We only need to
3672 -- check overlap for fields starting below this point.
3674 Overlap_Check_Required : Boolean;
3675 -- Used to keep track of whether or not an overlap check is required
3677 Overlap_Detected : Boolean := False;
3678 -- Set True if an overlap is detected
3680 Ccount : Natural := 0;
3681 -- Number of component clauses in record rep clause
3683 procedure Check_Component_Overlap (C1_Ent, C2_Ent : Entity_Id);
3684 -- Given two entities for record components or discriminants, checks
3685 -- if they have overlapping component clauses and issues errors if so.
3687 procedure Find_Component;
3688 -- Finds component entity corresponding to current component clause (in
3689 -- CC), and sets Comp to the entity, and Fbit/Lbit to the zero origin
3690 -- start/stop bits for the field. If there is no matching component or
3691 -- if the matching component does not have a component clause, then
3692 -- that's an error and Comp is set to Empty, but no error message is
3693 -- issued, since the message was already given. Comp is also set to
3694 -- Empty if the current "component clause" is in fact a pragma.
3696 -----------------------------
3697 -- Check_Component_Overlap --
3698 -----------------------------
3700 procedure Check_Component_Overlap (C1_Ent, C2_Ent : Entity_Id) is
3701 CC1 : constant Node_Id := Component_Clause (C1_Ent);
3702 CC2 : constant Node_Id := Component_Clause (C2_Ent);
3705 if Present (CC1) and then Present (CC2) then
3707 -- Exclude odd case where we have two tag fields in the same
3708 -- record, both at location zero. This seems a bit strange, but
3709 -- it seems to happen in some circumstances, perhaps on an error.
3711 if Chars (C1_Ent) = Name_uTag
3713 Chars (C2_Ent) = Name_uTag
3718 -- Here we check if the two fields overlap
3721 S1 : constant Uint := Component_Bit_Offset (C1_Ent);
3722 S2 : constant Uint := Component_Bit_Offset (C2_Ent);
3723 E1 : constant Uint := S1 + Esize (C1_Ent);
3724 E2 : constant Uint := S2 + Esize (C2_Ent);
3727 if E2 <= S1 or else E1 <= S2 then
3730 Error_Msg_Node_2 := Component_Name (CC2);
3731 Error_Msg_Sloc := Sloc (Error_Msg_Node_2);
3732 Error_Msg_Node_1 := Component_Name (CC1);
3734 ("component& overlaps & #", Component_Name (CC1));
3735 Overlap_Detected := True;
3739 end Check_Component_Overlap;
3741 --------------------
3742 -- Find_Component --
3743 --------------------
3745 procedure Find_Component is
3747 procedure Search_Component (R : Entity_Id);
3748 -- Search components of R for a match. If found, Comp is set.
3750 ----------------------
3751 -- Search_Component --
3752 ----------------------
3754 procedure Search_Component (R : Entity_Id) is
3756 Comp := First_Component_Or_Discriminant (R);
3757 while Present (Comp) loop
3759 -- Ignore error of attribute name for component name (we
3760 -- already gave an error message for this, so no need to
3763 if Nkind (Component_Name (CC)) = N_Attribute_Reference then
3766 exit when Chars (Comp) = Chars (Component_Name (CC));
3769 Next_Component_Or_Discriminant (Comp);
3771 end Search_Component;
3773 -- Start of processing for Find_Component
3776 -- Return with Comp set to Empty if we have a pragma
3778 if Nkind (CC) = N_Pragma then
3783 -- Search current record for matching component
3785 Search_Component (Rectype);
3787 -- If not found, maybe component of base type that is absent from
3788 -- statically constrained first subtype.
3791 Search_Component (Base_Type (Rectype));
3794 -- If no component, or the component does not reference the component
3795 -- clause in question, then there was some previous error for which
3796 -- we already gave a message, so just return with Comp Empty.
3799 or else Component_Clause (Comp) /= CC
3803 -- Normal case where we have a component clause
3806 Fbit := Component_Bit_Offset (Comp);
3807 Lbit := Fbit + Esize (Comp) - 1;
3811 -- Start of processing for Check_Record_Representation_Clause
3815 Rectype := Entity (Ident);
3817 if Rectype = Any_Type then
3820 Rectype := Underlying_Type (Rectype);
3823 -- See if we have a fully repped derived tagged type
3826 PS : constant Entity_Id := Parent_Subtype (Rectype);
3829 if Present (PS) and then Is_Fully_Repped_Tagged_Type (PS) then
3830 Tagged_Parent := PS;
3832 -- Find maximum bit of any component of the parent type
3834 Parent_Last_Bit := UI_From_Int (System_Address_Size - 1);
3835 Pcomp := First_Entity (Tagged_Parent);
3836 while Present (Pcomp) loop
3837 if Ekind_In (Pcomp, E_Discriminant, E_Component) then
3838 if Component_Bit_Offset (Pcomp) /= No_Uint
3839 and then Known_Static_Esize (Pcomp)
3844 Component_Bit_Offset (Pcomp) + Esize (Pcomp) - 1);
3847 Next_Entity (Pcomp);
3853 -- All done if no component clauses
3855 CC := First (Component_Clauses (N));
3861 -- If a tag is present, then create a component clause that places it
3862 -- at the start of the record (otherwise gigi may place it after other
3863 -- fields that have rep clauses).
3865 Fent := First_Entity (Rectype);
3867 if Nkind (Fent) = N_Defining_Identifier
3868 and then Chars (Fent) = Name_uTag
3870 Set_Component_Bit_Offset (Fent, Uint_0);
3871 Set_Normalized_Position (Fent, Uint_0);
3872 Set_Normalized_First_Bit (Fent, Uint_0);
3873 Set_Normalized_Position_Max (Fent, Uint_0);
3874 Init_Esize (Fent, System_Address_Size);
3876 Set_Component_Clause (Fent,
3877 Make_Component_Clause (Loc,
3879 Make_Identifier (Loc,
3880 Chars => Name_uTag),
3883 Make_Integer_Literal (Loc,
3887 Make_Integer_Literal (Loc,
3891 Make_Integer_Literal (Loc,
3892 UI_From_Int (System_Address_Size))));
3894 Ccount := Ccount + 1;
3897 Max_Bit_So_Far := Uint_Minus_1;
3898 Overlap_Check_Required := False;
3900 -- Process the component clauses
3902 while Present (CC) loop
3905 if Present (Comp) then
3906 Ccount := Ccount + 1;
3908 -- We need a full overlap check if record positions non-monotonic
3910 if Fbit <= Max_Bit_So_Far then
3911 Overlap_Check_Required := True;
3914 Max_Bit_So_Far := Lbit;
3916 -- Check bit position out of range of specified size
3918 if Has_Size_Clause (Rectype)
3919 and then Esize (Rectype) <= Lbit
3922 ("bit number out of range of specified size",
3925 -- Check for overlap with tag field
3928 if Is_Tagged_Type (Rectype)
3929 and then Fbit < System_Address_Size
3932 ("component overlaps tag field of&",
3933 Component_Name (CC), Rectype);
3934 Overlap_Detected := True;
3942 -- Check parent overlap if component might overlap parent field
3944 if Present (Tagged_Parent)
3945 and then Fbit <= Parent_Last_Bit
3947 Pcomp := First_Component_Or_Discriminant (Tagged_Parent);
3948 while Present (Pcomp) loop
3949 if not Is_Tag (Pcomp)
3950 and then Chars (Pcomp) /= Name_uParent
3952 Check_Component_Overlap (Comp, Pcomp);
3955 Next_Component_Or_Discriminant (Pcomp);
3963 -- Now that we have processed all the component clauses, check for
3964 -- overlap. We have to leave this till last, since the components can
3965 -- appear in any arbitrary order in the representation clause.
3967 -- We do not need this check if all specified ranges were monotonic,
3968 -- as recorded by Overlap_Check_Required being False at this stage.
3970 -- This first section checks if there are any overlapping entries at
3971 -- all. It does this by sorting all entries and then seeing if there are
3972 -- any overlaps. If there are none, then that is decisive, but if there
3973 -- are overlaps, they may still be OK (they may result from fields in
3974 -- different variants).
3976 if Overlap_Check_Required then
3977 Overlap_Check1 : declare
3979 OC_Fbit : array (0 .. Ccount) of Uint;
3980 -- First-bit values for component clauses, the value is the offset
3981 -- of the first bit of the field from start of record. The zero
3982 -- entry is for use in sorting.
3984 OC_Lbit : array (0 .. Ccount) of Uint;
3985 -- Last-bit values for component clauses, the value is the offset
3986 -- of the last bit of the field from start of record. The zero
3987 -- entry is for use in sorting.
3989 OC_Count : Natural := 0;
3990 -- Count of entries in OC_Fbit and OC_Lbit
3992 function OC_Lt (Op1, Op2 : Natural) return Boolean;
3993 -- Compare routine for Sort
3995 procedure OC_Move (From : Natural; To : Natural);
3996 -- Move routine for Sort
3998 package Sorting is new GNAT.Heap_Sort_G (OC_Move, OC_Lt);
4004 function OC_Lt (Op1, Op2 : Natural) return Boolean is
4006 return OC_Fbit (Op1) < OC_Fbit (Op2);
4013 procedure OC_Move (From : Natural; To : Natural) is
4015 OC_Fbit (To) := OC_Fbit (From);
4016 OC_Lbit (To) := OC_Lbit (From);
4019 -- Start of processing for Overlap_Check
4022 CC := First (Component_Clauses (N));
4023 while Present (CC) loop
4025 -- Exclude component clause already marked in error
4027 if not Error_Posted (CC) then
4030 if Present (Comp) then
4031 OC_Count := OC_Count + 1;
4032 OC_Fbit (OC_Count) := Fbit;
4033 OC_Lbit (OC_Count) := Lbit;
4040 Sorting.Sort (OC_Count);
4042 Overlap_Check_Required := False;
4043 for J in 1 .. OC_Count - 1 loop
4044 if OC_Lbit (J) >= OC_Fbit (J + 1) then
4045 Overlap_Check_Required := True;
4052 -- If Overlap_Check_Required is still True, then we have to do the full
4053 -- scale overlap check, since we have at least two fields that do
4054 -- overlap, and we need to know if that is OK since they are in
4055 -- different variant, or whether we have a definite problem.
4057 if Overlap_Check_Required then
4058 Overlap_Check2 : declare
4059 C1_Ent, C2_Ent : Entity_Id;
4060 -- Entities of components being checked for overlap
4063 -- Component_List node whose Component_Items are being checked
4066 -- Component declaration for component being checked
4069 C1_Ent := First_Entity (Base_Type (Rectype));
4071 -- Loop through all components in record. For each component check
4072 -- for overlap with any of the preceding elements on the component
4073 -- list containing the component and also, if the component is in
4074 -- a variant, check against components outside the case structure.
4075 -- This latter test is repeated recursively up the variant tree.
4077 Main_Component_Loop : while Present (C1_Ent) loop
4078 if not Ekind_In (C1_Ent, E_Component, E_Discriminant) then
4079 goto Continue_Main_Component_Loop;
4082 -- Skip overlap check if entity has no declaration node. This
4083 -- happens with discriminants in constrained derived types.
4084 -- Possibly we are missing some checks as a result, but that
4085 -- does not seem terribly serious.
4087 if No (Declaration_Node (C1_Ent)) then
4088 goto Continue_Main_Component_Loop;
4091 Clist := Parent (List_Containing (Declaration_Node (C1_Ent)));
4093 -- Loop through component lists that need checking. Check the
4094 -- current component list and all lists in variants above us.
4096 Component_List_Loop : loop
4098 -- If derived type definition, go to full declaration
4099 -- If at outer level, check discriminants if there are any.
4101 if Nkind (Clist) = N_Derived_Type_Definition then
4102 Clist := Parent (Clist);
4105 -- Outer level of record definition, check discriminants
4107 if Nkind_In (Clist, N_Full_Type_Declaration,
4108 N_Private_Type_Declaration)
4110 if Has_Discriminants (Defining_Identifier (Clist)) then
4112 First_Discriminant (Defining_Identifier (Clist));
4113 while Present (C2_Ent) loop
4114 exit when C1_Ent = C2_Ent;
4115 Check_Component_Overlap (C1_Ent, C2_Ent);
4116 Next_Discriminant (C2_Ent);
4120 -- Record extension case
4122 elsif Nkind (Clist) = N_Derived_Type_Definition then
4125 -- Otherwise check one component list
4128 Citem := First (Component_Items (Clist));
4129 while Present (Citem) loop
4130 if Nkind (Citem) = N_Component_Declaration then
4131 C2_Ent := Defining_Identifier (Citem);
4132 exit when C1_Ent = C2_Ent;
4133 Check_Component_Overlap (C1_Ent, C2_Ent);
4140 -- Check for variants above us (the parent of the Clist can
4141 -- be a variant, in which case its parent is a variant part,
4142 -- and the parent of the variant part is a component list
4143 -- whose components must all be checked against the current
4144 -- component for overlap).
4146 if Nkind (Parent (Clist)) = N_Variant then
4147 Clist := Parent (Parent (Parent (Clist)));
4149 -- Check for possible discriminant part in record, this
4150 -- is treated essentially as another level in the
4151 -- recursion. For this case the parent of the component
4152 -- list is the record definition, and its parent is the
4153 -- full type declaration containing the discriminant
4156 elsif Nkind (Parent (Clist)) = N_Record_Definition then
4157 Clist := Parent (Parent ((Clist)));
4159 -- If neither of these two cases, we are at the top of
4163 exit Component_List_Loop;
4165 end loop Component_List_Loop;
4167 <<Continue_Main_Component_Loop>>
4168 Next_Entity (C1_Ent);
4170 end loop Main_Component_Loop;
4174 -- The following circuit deals with warning on record holes (gaps). We
4175 -- skip this check if overlap was detected, since it makes sense for the
4176 -- programmer to fix this illegality before worrying about warnings.
4178 if not Overlap_Detected and Warn_On_Record_Holes then
4179 Record_Hole_Check : declare
4180 Decl : constant Node_Id := Declaration_Node (Base_Type (Rectype));
4181 -- Full declaration of record type
4183 procedure Check_Component_List
4187 -- Check component list CL for holes. The starting bit should be
4188 -- Sbit. which is zero for the main record component list and set
4189 -- appropriately for recursive calls for variants. DS is set to
4190 -- a list of discriminant specifications to be included in the
4191 -- consideration of components. It is No_List if none to consider.
4193 --------------------------
4194 -- Check_Component_List --
4195 --------------------------
4197 procedure Check_Component_List
4205 Compl := Integer (List_Length (Component_Items (CL)));
4207 if DS /= No_List then
4208 Compl := Compl + Integer (List_Length (DS));
4212 Comps : array (Natural range 0 .. Compl) of Entity_Id;
4213 -- Gather components (zero entry is for sort routine)
4215 Ncomps : Natural := 0;
4216 -- Number of entries stored in Comps (starting at Comps (1))
4219 -- One component item or discriminant specification
4222 -- Starting bit for next component
4230 function Lt (Op1, Op2 : Natural) return Boolean;
4231 -- Compare routine for Sort
4233 procedure Move (From : Natural; To : Natural);
4234 -- Move routine for Sort
4236 package Sorting is new GNAT.Heap_Sort_G (Move, Lt);
4242 function Lt (Op1, Op2 : Natural) return Boolean is
4244 return Component_Bit_Offset (Comps (Op1))
4246 Component_Bit_Offset (Comps (Op2));
4253 procedure Move (From : Natural; To : Natural) is
4255 Comps (To) := Comps (From);
4259 -- Gather discriminants into Comp
4261 if DS /= No_List then
4262 Citem := First (DS);
4263 while Present (Citem) loop
4264 if Nkind (Citem) = N_Discriminant_Specification then
4266 Ent : constant Entity_Id :=
4267 Defining_Identifier (Citem);
4269 if Ekind (Ent) = E_Discriminant then
4270 Ncomps := Ncomps + 1;
4271 Comps (Ncomps) := Ent;
4280 -- Gather component entities into Comp
4282 Citem := First (Component_Items (CL));
4283 while Present (Citem) loop
4284 if Nkind (Citem) = N_Component_Declaration then
4285 Ncomps := Ncomps + 1;
4286 Comps (Ncomps) := Defining_Identifier (Citem);
4292 -- Now sort the component entities based on the first bit.
4293 -- Note we already know there are no overlapping components.
4295 Sorting.Sort (Ncomps);
4297 -- Loop through entries checking for holes
4300 for J in 1 .. Ncomps loop
4302 Error_Msg_Uint_1 := Component_Bit_Offset (CEnt) - Nbit;
4304 if Error_Msg_Uint_1 > 0 then
4306 ("?^-bit gap before component&",
4307 Component_Name (Component_Clause (CEnt)), CEnt);
4310 Nbit := Component_Bit_Offset (CEnt) + Esize (CEnt);
4313 -- Process variant parts recursively if present
4315 if Present (Variant_Part (CL)) then
4316 Variant := First (Variants (Variant_Part (CL)));
4317 while Present (Variant) loop
4318 Check_Component_List
4319 (Component_List (Variant), Nbit, No_List);
4324 end Check_Component_List;
4326 -- Start of processing for Record_Hole_Check
4333 if Is_Tagged_Type (Rectype) then
4334 Sbit := UI_From_Int (System_Address_Size);
4339 if Nkind (Decl) = N_Full_Type_Declaration
4340 and then Nkind (Type_Definition (Decl)) = N_Record_Definition
4342 Check_Component_List
4343 (Component_List (Type_Definition (Decl)),
4345 Discriminant_Specifications (Decl));
4348 end Record_Hole_Check;
4351 -- For records that have component clauses for all components, and whose
4352 -- size is less than or equal to 32, we need to know the size in the
4353 -- front end to activate possible packed array processing where the
4354 -- component type is a record.
4356 -- At this stage Hbit + 1 represents the first unused bit from all the
4357 -- component clauses processed, so if the component clauses are
4358 -- complete, then this is the length of the record.
4360 -- For records longer than System.Storage_Unit, and for those where not
4361 -- all components have component clauses, the back end determines the
4362 -- length (it may for example be appropriate to round up the size
4363 -- to some convenient boundary, based on alignment considerations, etc).
4365 if Unknown_RM_Size (Rectype) and then Hbit + 1 <= 32 then
4367 -- Nothing to do if at least one component has no component clause
4369 Comp := First_Component_Or_Discriminant (Rectype);
4370 while Present (Comp) loop
4371 exit when No (Component_Clause (Comp));
4372 Next_Component_Or_Discriminant (Comp);
4375 -- If we fall out of loop, all components have component clauses
4376 -- and so we can set the size to the maximum value.
4379 Set_RM_Size (Rectype, Hbit + 1);
4382 end Check_Record_Representation_Clause;
4388 procedure Check_Size
4392 Biased : out Boolean)
4394 UT : constant Entity_Id := Underlying_Type (T);
4400 -- Dismiss cases for generic types or types with previous errors
4403 or else UT = Any_Type
4404 or else Is_Generic_Type (UT)
4405 or else Is_Generic_Type (Root_Type (UT))
4409 -- Check case of bit packed array
4411 elsif Is_Array_Type (UT)
4412 and then Known_Static_Component_Size (UT)
4413 and then Is_Bit_Packed_Array (UT)
4421 Asiz := Component_Size (UT);
4422 Indx := First_Index (UT);
4424 Ityp := Etype (Indx);
4426 -- If non-static bound, then we are not in the business of
4427 -- trying to check the length, and indeed an error will be
4428 -- issued elsewhere, since sizes of non-static array types
4429 -- cannot be set implicitly or explicitly.
4431 if not Is_Static_Subtype (Ityp) then
4435 -- Otherwise accumulate next dimension
4437 Asiz := Asiz * (Expr_Value (Type_High_Bound (Ityp)) -
4438 Expr_Value (Type_Low_Bound (Ityp)) +
4442 exit when No (Indx);
4448 Error_Msg_Uint_1 := Asiz;
4450 ("size for& too small, minimum allowed is ^", N, T);
4451 Set_Esize (T, Asiz);
4452 Set_RM_Size (T, Asiz);
4456 -- All other composite types are ignored
4458 elsif Is_Composite_Type (UT) then
4461 -- For fixed-point types, don't check minimum if type is not frozen,
4462 -- since we don't know all the characteristics of the type that can
4463 -- affect the size (e.g. a specified small) till freeze time.
4465 elsif Is_Fixed_Point_Type (UT)
4466 and then not Is_Frozen (UT)
4470 -- Cases for which a minimum check is required
4473 -- Ignore if specified size is correct for the type
4475 if Known_Esize (UT) and then Siz = Esize (UT) then
4479 -- Otherwise get minimum size
4481 M := UI_From_Int (Minimum_Size (UT));
4485 -- Size is less than minimum size, but one possibility remains
4486 -- that we can manage with the new size if we bias the type.
4488 M := UI_From_Int (Minimum_Size (UT, Biased => True));
4491 Error_Msg_Uint_1 := M;
4493 ("size for& too small, minimum allowed is ^", N, T);
4503 -------------------------
4504 -- Get_Alignment_Value --
4505 -------------------------
4507 function Get_Alignment_Value (Expr : Node_Id) return Uint is
4508 Align : constant Uint := Static_Integer (Expr);
4511 if Align = No_Uint then
4514 elsif Align <= 0 then
4515 Error_Msg_N ("alignment value must be positive", Expr);
4519 for J in Int range 0 .. 64 loop
4521 M : constant Uint := Uint_2 ** J;
4524 exit when M = Align;
4528 ("alignment value must be power of 2", Expr);
4536 end Get_Alignment_Value;
4542 procedure Initialize is
4544 Address_Clause_Checks.Init;
4545 Independence_Checks.Init;
4546 Unchecked_Conversions.Init;
4549 -------------------------
4550 -- Is_Operational_Item --
4551 -------------------------
4553 function Is_Operational_Item (N : Node_Id) return Boolean is
4555 if Nkind (N) /= N_Attribute_Definition_Clause then
4559 Id : constant Attribute_Id := Get_Attribute_Id (Chars (N));
4561 return Id = Attribute_Input
4562 or else Id = Attribute_Output
4563 or else Id = Attribute_Read
4564 or else Id = Attribute_Write
4565 or else Id = Attribute_External_Tag;
4568 end Is_Operational_Item;
4574 function Minimum_Size
4576 Biased : Boolean := False) return Nat
4578 Lo : Uint := No_Uint;
4579 Hi : Uint := No_Uint;
4580 LoR : Ureal := No_Ureal;
4581 HiR : Ureal := No_Ureal;
4582 LoSet : Boolean := False;
4583 HiSet : Boolean := False;
4587 R_Typ : constant Entity_Id := Root_Type (T);
4590 -- If bad type, return 0
4592 if T = Any_Type then
4595 -- For generic types, just return zero. There cannot be any legitimate
4596 -- need to know such a size, but this routine may be called with a
4597 -- generic type as part of normal processing.
4599 elsif Is_Generic_Type (R_Typ)
4600 or else R_Typ = Any_Type
4604 -- Access types. Normally an access type cannot have a size smaller
4605 -- than the size of System.Address. The exception is on VMS, where
4606 -- we have short and long addresses, and it is possible for an access
4607 -- type to have a short address size (and thus be less than the size
4608 -- of System.Address itself). We simply skip the check for VMS, and
4609 -- leave it to the back end to do the check.
4611 elsif Is_Access_Type (T) then
4612 if OpenVMS_On_Target then
4615 return System_Address_Size;
4618 -- Floating-point types
4620 elsif Is_Floating_Point_Type (T) then
4621 return UI_To_Int (Esize (R_Typ));
4625 elsif Is_Discrete_Type (T) then
4627 -- The following loop is looking for the nearest compile time known
4628 -- bounds following the ancestor subtype chain. The idea is to find
4629 -- the most restrictive known bounds information.
4633 if Ancest = Any_Type or else Etype (Ancest) = Any_Type then
4638 if Compile_Time_Known_Value (Type_Low_Bound (Ancest)) then
4639 Lo := Expr_Rep_Value (Type_Low_Bound (Ancest));
4646 if Compile_Time_Known_Value (Type_High_Bound (Ancest)) then
4647 Hi := Expr_Rep_Value (Type_High_Bound (Ancest));
4653 Ancest := Ancestor_Subtype (Ancest);
4656 Ancest := Base_Type (T);
4658 if Is_Generic_Type (Ancest) then
4664 -- Fixed-point types. We can't simply use Expr_Value to get the
4665 -- Corresponding_Integer_Value values of the bounds, since these do not
4666 -- get set till the type is frozen, and this routine can be called
4667 -- before the type is frozen. Similarly the test for bounds being static
4668 -- needs to include the case where we have unanalyzed real literals for
4671 elsif Is_Fixed_Point_Type (T) then
4673 -- The following loop is looking for the nearest compile time known
4674 -- bounds following the ancestor subtype chain. The idea is to find
4675 -- the most restrictive known bounds information.
4679 if Ancest = Any_Type or else Etype (Ancest) = Any_Type then
4683 -- Note: In the following two tests for LoSet and HiSet, it may
4684 -- seem redundant to test for N_Real_Literal here since normally
4685 -- one would assume that the test for the value being known at
4686 -- compile time includes this case. However, there is a glitch.
4687 -- If the real literal comes from folding a non-static expression,
4688 -- then we don't consider any non- static expression to be known
4689 -- at compile time if we are in configurable run time mode (needed
4690 -- in some cases to give a clearer definition of what is and what
4691 -- is not accepted). So the test is indeed needed. Without it, we
4692 -- would set neither Lo_Set nor Hi_Set and get an infinite loop.
4695 if Nkind (Type_Low_Bound (Ancest)) = N_Real_Literal
4696 or else Compile_Time_Known_Value (Type_Low_Bound (Ancest))
4698 LoR := Expr_Value_R (Type_Low_Bound (Ancest));
4705 if Nkind (Type_High_Bound (Ancest)) = N_Real_Literal
4706 or else Compile_Time_Known_Value (Type_High_Bound (Ancest))
4708 HiR := Expr_Value_R (Type_High_Bound (Ancest));
4714 Ancest := Ancestor_Subtype (Ancest);
4717 Ancest := Base_Type (T);
4719 if Is_Generic_Type (Ancest) then
4725 Lo := UR_To_Uint (LoR / Small_Value (T));
4726 Hi := UR_To_Uint (HiR / Small_Value (T));
4728 -- No other types allowed
4731 raise Program_Error;
4734 -- Fall through with Hi and Lo set. Deal with biased case
4737 and then not Is_Fixed_Point_Type (T)
4738 and then not (Is_Enumeration_Type (T)
4739 and then Has_Non_Standard_Rep (T)))
4740 or else Has_Biased_Representation (T)
4746 -- Signed case. Note that we consider types like range 1 .. -1 to be
4747 -- signed for the purpose of computing the size, since the bounds have
4748 -- to be accommodated in the base type.
4750 if Lo < 0 or else Hi < 0 then
4754 -- S = size, B = 2 ** (size - 1) (can accommodate -B .. +(B - 1))
4755 -- Note that we accommodate the case where the bounds cross. This
4756 -- can happen either because of the way the bounds are declared
4757 -- or because of the algorithm in Freeze_Fixed_Point_Type.
4771 -- If both bounds are positive, make sure that both are represen-
4772 -- table in the case where the bounds are crossed. This can happen
4773 -- either because of the way the bounds are declared, or because of
4774 -- the algorithm in Freeze_Fixed_Point_Type.
4780 -- S = size, (can accommodate 0 .. (2**size - 1))
4783 while Hi >= Uint_2 ** S loop
4791 ---------------------------
4792 -- New_Stream_Subprogram --
4793 ---------------------------
4795 procedure New_Stream_Subprogram
4799 Nam : TSS_Name_Type)
4801 Loc : constant Source_Ptr := Sloc (N);
4802 Sname : constant Name_Id := Make_TSS_Name (Base_Type (Ent), Nam);
4803 Subp_Id : Entity_Id;
4804 Subp_Decl : Node_Id;
4808 Defer_Declaration : constant Boolean :=
4809 Is_Tagged_Type (Ent) or else Is_Private_Type (Ent);
4810 -- For a tagged type, there is a declaration for each stream attribute
4811 -- at the freeze point, and we must generate only a completion of this
4812 -- declaration. We do the same for private types, because the full view
4813 -- might be tagged. Otherwise we generate a declaration at the point of
4814 -- the attribute definition clause.
4816 function Build_Spec return Node_Id;
4817 -- Used for declaration and renaming declaration, so that this is
4818 -- treated as a renaming_as_body.
4824 function Build_Spec return Node_Id is
4825 Out_P : constant Boolean := (Nam = TSS_Stream_Read);
4828 T_Ref : constant Node_Id := New_Reference_To (Etyp, Loc);
4831 Subp_Id := Make_Defining_Identifier (Loc, Sname);
4833 -- S : access Root_Stream_Type'Class
4835 Formals := New_List (
4836 Make_Parameter_Specification (Loc,
4837 Defining_Identifier =>
4838 Make_Defining_Identifier (Loc, Name_S),
4840 Make_Access_Definition (Loc,
4843 Designated_Type (Etype (F)), Loc))));
4845 if Nam = TSS_Stream_Input then
4846 Spec := Make_Function_Specification (Loc,
4847 Defining_Unit_Name => Subp_Id,
4848 Parameter_Specifications => Formals,
4849 Result_Definition => T_Ref);
4854 Make_Parameter_Specification (Loc,
4855 Defining_Identifier => Make_Defining_Identifier (Loc, Name_V),
4856 Out_Present => Out_P,
4857 Parameter_Type => T_Ref));
4860 Make_Procedure_Specification (Loc,
4861 Defining_Unit_Name => Subp_Id,
4862 Parameter_Specifications => Formals);
4868 -- Start of processing for New_Stream_Subprogram
4871 F := First_Formal (Subp);
4873 if Ekind (Subp) = E_Procedure then
4874 Etyp := Etype (Next_Formal (F));
4876 Etyp := Etype (Subp);
4879 -- Prepare subprogram declaration and insert it as an action on the
4880 -- clause node. The visibility for this entity is used to test for
4881 -- visibility of the attribute definition clause (in the sense of
4882 -- 8.3(23) as amended by AI-195).
4884 if not Defer_Declaration then
4886 Make_Subprogram_Declaration (Loc,
4887 Specification => Build_Spec);
4889 -- For a tagged type, there is always a visible declaration for each
4890 -- stream TSS (it is a predefined primitive operation), and the
4891 -- completion of this declaration occurs at the freeze point, which is
4892 -- not always visible at places where the attribute definition clause is
4893 -- visible. So, we create a dummy entity here for the purpose of
4894 -- tracking the visibility of the attribute definition clause itself.
4898 Make_Defining_Identifier (Loc,
4899 Chars => New_External_Name (Sname, 'V'));
4901 Make_Object_Declaration (Loc,
4902 Defining_Identifier => Subp_Id,
4903 Object_Definition => New_Occurrence_Of (Standard_Boolean, Loc));
4906 Insert_Action (N, Subp_Decl);
4907 Set_Entity (N, Subp_Id);
4910 Make_Subprogram_Renaming_Declaration (Loc,
4911 Specification => Build_Spec,
4912 Name => New_Reference_To (Subp, Loc));
4914 if Defer_Declaration then
4915 Set_TSS (Base_Type (Ent), Subp_Id);
4917 Insert_Action (N, Subp_Decl);
4918 Copy_TSS (Subp_Id, Base_Type (Ent));
4920 end New_Stream_Subprogram;
4922 ------------------------
4923 -- Rep_Item_Too_Early --
4924 ------------------------
4926 function Rep_Item_Too_Early (T : Entity_Id; N : Node_Id) return Boolean is
4928 -- Cannot apply non-operational rep items to generic types
4930 if Is_Operational_Item (N) then
4934 and then Is_Generic_Type (Root_Type (T))
4936 Error_Msg_N ("representation item not allowed for generic type", N);
4940 -- Otherwise check for incomplete type
4942 if Is_Incomplete_Or_Private_Type (T)
4943 and then No (Underlying_Type (T))
4946 ("representation item must be after full type declaration", N);
4949 -- If the type has incomplete components, a representation clause is
4950 -- illegal but stream attributes and Convention pragmas are correct.
4952 elsif Has_Private_Component (T) then
4953 if Nkind (N) = N_Pragma then
4957 ("representation item must appear after type is fully defined",
4964 end Rep_Item_Too_Early;
4966 -----------------------
4967 -- Rep_Item_Too_Late --
4968 -----------------------
4970 function Rep_Item_Too_Late
4973 FOnly : Boolean := False) return Boolean
4976 Parent_Type : Entity_Id;
4979 -- Output the too late message. Note that this is not considered a
4980 -- serious error, since the effect is simply that we ignore the
4981 -- representation clause in this case.
4987 procedure Too_Late is
4989 Error_Msg_N ("|representation item appears too late!", N);
4992 -- Start of processing for Rep_Item_Too_Late
4995 -- First make sure entity is not frozen (RM 13.1(9)). Exclude imported
4996 -- types, which may be frozen if they appear in a representation clause
4997 -- for a local type.
5000 and then not From_With_Type (T)
5003 S := First_Subtype (T);
5005 if Present (Freeze_Node (S)) then
5007 ("?no more representation items for }", Freeze_Node (S), S);
5012 -- Check for case of non-tagged derived type whose parent either has
5013 -- primitive operations, or is a by reference type (RM 13.1(10)).
5017 and then Is_Derived_Type (T)
5018 and then not Is_Tagged_Type (T)
5020 Parent_Type := Etype (Base_Type (T));
5022 if Has_Primitive_Operations (Parent_Type) then
5025 ("primitive operations already defined for&!", N, Parent_Type);
5028 elsif Is_By_Reference_Type (Parent_Type) then
5031 ("parent type & is a by reference type!", N, Parent_Type);
5036 -- No error, link item into head of chain of rep items for the entity,
5037 -- but avoid chaining if we have an overloadable entity, and the pragma
5038 -- is one that can apply to multiple overloaded entities.
5040 if Is_Overloadable (T)
5041 and then Nkind (N) = N_Pragma
5044 Pname : constant Name_Id := Pragma_Name (N);
5046 if Pname = Name_Convention or else
5047 Pname = Name_Import or else
5048 Pname = Name_Export or else
5049 Pname = Name_External or else
5050 Pname = Name_Interface
5057 Record_Rep_Item (T, N);
5059 end Rep_Item_Too_Late;
5061 -------------------------
5062 -- Same_Representation --
5063 -------------------------
5065 function Same_Representation (Typ1, Typ2 : Entity_Id) return Boolean is
5066 T1 : constant Entity_Id := Underlying_Type (Typ1);
5067 T2 : constant Entity_Id := Underlying_Type (Typ2);
5070 -- A quick check, if base types are the same, then we definitely have
5071 -- the same representation, because the subtype specific representation
5072 -- attributes (Size and Alignment) do not affect representation from
5073 -- the point of view of this test.
5075 if Base_Type (T1) = Base_Type (T2) then
5078 elsif Is_Private_Type (Base_Type (T2))
5079 and then Base_Type (T1) = Full_View (Base_Type (T2))
5084 -- Tagged types never have differing representations
5086 if Is_Tagged_Type (T1) then
5090 -- Representations are definitely different if conventions differ
5092 if Convention (T1) /= Convention (T2) then
5096 -- Representations are different if component alignments differ
5098 if (Is_Record_Type (T1) or else Is_Array_Type (T1))
5100 (Is_Record_Type (T2) or else Is_Array_Type (T2))
5101 and then Component_Alignment (T1) /= Component_Alignment (T2)
5106 -- For arrays, the only real issue is component size. If we know the
5107 -- component size for both arrays, and it is the same, then that's
5108 -- good enough to know we don't have a change of representation.
5110 if Is_Array_Type (T1) then
5111 if Known_Component_Size (T1)
5112 and then Known_Component_Size (T2)
5113 and then Component_Size (T1) = Component_Size (T2)
5119 -- Types definitely have same representation if neither has non-standard
5120 -- representation since default representations are always consistent.
5121 -- If only one has non-standard representation, and the other does not,
5122 -- then we consider that they do not have the same representation. They
5123 -- might, but there is no way of telling early enough.
5125 if Has_Non_Standard_Rep (T1) then
5126 if not Has_Non_Standard_Rep (T2) then
5130 return not Has_Non_Standard_Rep (T2);
5133 -- Here the two types both have non-standard representation, and we need
5134 -- to determine if they have the same non-standard representation.
5136 -- For arrays, we simply need to test if the component sizes are the
5137 -- same. Pragma Pack is reflected in modified component sizes, so this
5138 -- check also deals with pragma Pack.
5140 if Is_Array_Type (T1) then
5141 return Component_Size (T1) = Component_Size (T2);
5143 -- Tagged types always have the same representation, because it is not
5144 -- possible to specify different representations for common fields.
5146 elsif Is_Tagged_Type (T1) then
5149 -- Case of record types
5151 elsif Is_Record_Type (T1) then
5153 -- Packed status must conform
5155 if Is_Packed (T1) /= Is_Packed (T2) then
5158 -- Otherwise we must check components. Typ2 maybe a constrained
5159 -- subtype with fewer components, so we compare the components
5160 -- of the base types.
5163 Record_Case : declare
5164 CD1, CD2 : Entity_Id;
5166 function Same_Rep return Boolean;
5167 -- CD1 and CD2 are either components or discriminants. This
5168 -- function tests whether the two have the same representation
5174 function Same_Rep return Boolean is
5176 if No (Component_Clause (CD1)) then
5177 return No (Component_Clause (CD2));
5181 Present (Component_Clause (CD2))
5183 Component_Bit_Offset (CD1) = Component_Bit_Offset (CD2)
5185 Esize (CD1) = Esize (CD2);
5189 -- Start of processing for Record_Case
5192 if Has_Discriminants (T1) then
5193 CD1 := First_Discriminant (T1);
5194 CD2 := First_Discriminant (T2);
5196 -- The number of discriminants may be different if the
5197 -- derived type has fewer (constrained by values). The
5198 -- invisible discriminants retain the representation of
5199 -- the original, so the discrepancy does not per se
5200 -- indicate a different representation.
5203 and then Present (CD2)
5205 if not Same_Rep then
5208 Next_Discriminant (CD1);
5209 Next_Discriminant (CD2);
5214 CD1 := First_Component (Underlying_Type (Base_Type (T1)));
5215 CD2 := First_Component (Underlying_Type (Base_Type (T2)));
5217 while Present (CD1) loop
5218 if not Same_Rep then
5221 Next_Component (CD1);
5222 Next_Component (CD2);
5230 -- For enumeration types, we must check each literal to see if the
5231 -- representation is the same. Note that we do not permit enumeration
5232 -- representation clauses for Character and Wide_Character, so these
5233 -- cases were already dealt with.
5235 elsif Is_Enumeration_Type (T1) then
5236 Enumeration_Case : declare
5240 L1 := First_Literal (T1);
5241 L2 := First_Literal (T2);
5243 while Present (L1) loop
5244 if Enumeration_Rep (L1) /= Enumeration_Rep (L2) then
5254 end Enumeration_Case;
5256 -- Any other types have the same representation for these purposes
5261 end Same_Representation;
5267 procedure Set_Biased
5271 Biased : Boolean := True)
5275 Set_Has_Biased_Representation (E);
5277 if Warn_On_Biased_Representation then
5279 ("?" & Msg & " forces biased representation for&", N, E);
5284 --------------------
5285 -- Set_Enum_Esize --
5286 --------------------
5288 procedure Set_Enum_Esize (T : Entity_Id) is
5296 -- Find the minimum standard size (8,16,32,64) that fits
5298 Lo := Enumeration_Rep (Entity (Type_Low_Bound (T)));
5299 Hi := Enumeration_Rep (Entity (Type_High_Bound (T)));
5302 if Lo >= -Uint_2**07 and then Hi < Uint_2**07 then
5303 Sz := Standard_Character_Size; -- May be > 8 on some targets
5305 elsif Lo >= -Uint_2**15 and then Hi < Uint_2**15 then
5308 elsif Lo >= -Uint_2**31 and then Hi < Uint_2**31 then
5311 else pragma Assert (Lo >= -Uint_2**63 and then Hi < Uint_2**63);
5316 if Hi < Uint_2**08 then
5317 Sz := Standard_Character_Size; -- May be > 8 on some targets
5319 elsif Hi < Uint_2**16 then
5322 elsif Hi < Uint_2**32 then
5325 else pragma Assert (Hi < Uint_2**63);
5330 -- That minimum is the proper size unless we have a foreign convention
5331 -- and the size required is 32 or less, in which case we bump the size
5332 -- up to 32. This is required for C and C++ and seems reasonable for
5333 -- all other foreign conventions.
5335 if Has_Foreign_Convention (T)
5336 and then Esize (T) < Standard_Integer_Size
5338 Init_Esize (T, Standard_Integer_Size);
5344 ------------------------------
5345 -- Validate_Address_Clauses --
5346 ------------------------------
5348 procedure Validate_Address_Clauses is
5350 for J in Address_Clause_Checks.First .. Address_Clause_Checks.Last loop
5352 ACCR : Address_Clause_Check_Record
5353 renames Address_Clause_Checks.Table (J);
5364 -- Skip processing of this entry if warning already posted
5366 if not Address_Warning_Posted (ACCR.N) then
5368 Expr := Original_Node (Expression (ACCR.N));
5372 X_Alignment := Alignment (ACCR.X);
5373 Y_Alignment := Alignment (ACCR.Y);
5375 -- Similarly obtain sizes
5377 X_Size := Esize (ACCR.X);
5378 Y_Size := Esize (ACCR.Y);
5380 -- Check for large object overlaying smaller one
5383 and then X_Size > Uint_0
5384 and then X_Size > Y_Size
5387 ("?& overlays smaller object", ACCR.N, ACCR.X);
5389 ("\?program execution may be erroneous", ACCR.N);
5390 Error_Msg_Uint_1 := X_Size;
5392 ("\?size of & is ^", ACCR.N, ACCR.X);
5393 Error_Msg_Uint_1 := Y_Size;
5395 ("\?size of & is ^", ACCR.N, ACCR.Y);
5397 -- Check for inadequate alignment, both of the base object
5398 -- and of the offset, if any.
5400 -- Note: we do not check the alignment if we gave a size
5401 -- warning, since it would likely be redundant.
5403 elsif Y_Alignment /= Uint_0
5404 and then (Y_Alignment < X_Alignment
5407 Nkind (Expr) = N_Attribute_Reference
5409 Attribute_Name (Expr) = Name_Address
5411 Has_Compatible_Alignment
5412 (ACCR.X, Prefix (Expr))
5413 /= Known_Compatible))
5416 ("?specified address for& may be inconsistent "
5420 ("\?program execution may be erroneous (RM 13.3(27))",
5422 Error_Msg_Uint_1 := X_Alignment;
5424 ("\?alignment of & is ^",
5426 Error_Msg_Uint_1 := Y_Alignment;
5428 ("\?alignment of & is ^",
5430 if Y_Alignment >= X_Alignment then
5432 ("\?but offset is not multiple of alignment",
5439 end Validate_Address_Clauses;
5441 ---------------------------
5442 -- Validate_Independence --
5443 ---------------------------
5445 procedure Validate_Independence is
5446 SU : constant Uint := UI_From_Int (System_Storage_Unit);
5454 procedure Check_Array_Type (Atyp : Entity_Id);
5455 -- Checks if the array type Atyp has independent components, and
5456 -- if not, outputs an appropriate set of error messages.
5458 procedure No_Independence;
5459 -- Output message that independence cannot be guaranteed
5461 function OK_Component (C : Entity_Id) return Boolean;
5462 -- Checks one component to see if it is independently accessible, and
5463 -- if so yields True, otherwise yields False if independent access
5464 -- cannot be guaranteed. This is a conservative routine, it only
5465 -- returns True if it knows for sure, it returns False if it knows
5466 -- there is a problem, or it cannot be sure there is no problem.
5468 procedure Reason_Bad_Component (C : Entity_Id);
5469 -- Outputs continuation message if a reason can be determined for
5470 -- the component C being bad.
5472 ----------------------
5473 -- Check_Array_Type --
5474 ----------------------
5476 procedure Check_Array_Type (Atyp : Entity_Id) is
5477 Ctyp : constant Entity_Id := Component_Type (Atyp);
5480 -- OK if no alignment clause, no pack, and no component size
5482 if not Has_Component_Size_Clause (Atyp)
5483 and then not Has_Alignment_Clause (Atyp)
5484 and then not Is_Packed (Atyp)
5489 -- Check actual component size
5491 if not Known_Component_Size (Atyp)
5492 or else not (Addressable (Component_Size (Atyp))
5493 and then Component_Size (Atyp) < 64)
5494 or else Component_Size (Atyp) mod Esize (Ctyp) /= 0
5498 -- Bad component size, check reason
5500 if Has_Component_Size_Clause (Atyp) then
5502 Get_Attribute_Definition_Clause
5503 (Atyp, Attribute_Component_Size);
5506 Error_Msg_Sloc := Sloc (P);
5507 Error_Msg_N ("\because of Component_Size clause#", N);
5512 if Is_Packed (Atyp) then
5513 P := Get_Rep_Pragma (Atyp, Name_Pack);
5516 Error_Msg_Sloc := Sloc (P);
5517 Error_Msg_N ("\because of pragma Pack#", N);
5522 -- No reason found, just return
5527 -- Array type is OK independence-wise
5530 end Check_Array_Type;
5532 ---------------------
5533 -- No_Independence --
5534 ---------------------
5536 procedure No_Independence is
5538 if Pragma_Name (N) = Name_Independent then
5540 ("independence cannot be guaranteed for&", N, E);
5543 ("independent components cannot be guaranteed for&", N, E);
5545 end No_Independence;
5551 function OK_Component (C : Entity_Id) return Boolean is
5552 Rec : constant Entity_Id := Scope (C);
5553 Ctyp : constant Entity_Id := Etype (C);
5556 -- OK if no component clause, no Pack, and no alignment clause
5558 if No (Component_Clause (C))
5559 and then not Is_Packed (Rec)
5560 and then not Has_Alignment_Clause (Rec)
5565 -- Here we look at the actual component layout. A component is
5566 -- addressable if its size is a multiple of the Esize of the
5567 -- component type, and its starting position in the record has
5568 -- appropriate alignment, and the record itself has appropriate
5569 -- alignment to guarantee the component alignment.
5571 -- Make sure sizes are static, always assume the worst for any
5572 -- cases where we cannot check static values.
5574 if not (Known_Static_Esize (C)
5575 and then Known_Static_Esize (Ctyp))
5580 -- Size of component must be addressable or greater than 64 bits
5581 -- and a multiple of bytes.
5583 if not Addressable (Esize (C))
5584 and then Esize (C) < Uint_64
5589 -- Check size is proper multiple
5591 if Esize (C) mod Esize (Ctyp) /= 0 then
5595 -- Check alignment of component is OK
5597 if not Known_Component_Bit_Offset (C)
5598 or else Component_Bit_Offset (C) < Uint_0
5599 or else Component_Bit_Offset (C) mod Esize (Ctyp) /= 0
5604 -- Check alignment of record type is OK
5606 if not Known_Alignment (Rec)
5607 or else (Alignment (Rec) * SU) mod Esize (Ctyp) /= 0
5612 -- All tests passed, component is addressable
5617 --------------------------
5618 -- Reason_Bad_Component --
5619 --------------------------
5621 procedure Reason_Bad_Component (C : Entity_Id) is
5622 Rec : constant Entity_Id := Scope (C);
5623 Ctyp : constant Entity_Id := Etype (C);
5626 -- If component clause present assume that's the problem
5628 if Present (Component_Clause (C)) then
5629 Error_Msg_Sloc := Sloc (Component_Clause (C));
5630 Error_Msg_N ("\because of Component_Clause#", N);
5634 -- If pragma Pack clause present, assume that's the problem
5636 if Is_Packed (Rec) then
5637 P := Get_Rep_Pragma (Rec, Name_Pack);
5640 Error_Msg_Sloc := Sloc (P);
5641 Error_Msg_N ("\because of pragma Pack#", N);
5646 -- See if record has bad alignment clause
5648 if Has_Alignment_Clause (Rec)
5649 and then Known_Alignment (Rec)
5650 and then (Alignment (Rec) * SU) mod Esize (Ctyp) /= 0
5652 P := Get_Attribute_Definition_Clause (Rec, Attribute_Alignment);
5655 Error_Msg_Sloc := Sloc (P);
5656 Error_Msg_N ("\because of Alignment clause#", N);
5660 -- Couldn't find a reason, so return without a message
5663 end Reason_Bad_Component;
5665 -- Start of processing for Validate_Independence
5668 for J in Independence_Checks.First .. Independence_Checks.Last loop
5669 N := Independence_Checks.Table (J).N;
5670 E := Independence_Checks.Table (J).E;
5671 IC := Pragma_Name (N) = Name_Independent_Components;
5673 -- Deal with component case
5675 if Ekind (E) = E_Discriminant or else Ekind (E) = E_Component then
5676 if not OK_Component (E) then
5678 Reason_Bad_Component (E);
5683 -- Deal with record with Independent_Components
5685 if IC and then Is_Record_Type (E) then
5686 Comp := First_Component_Or_Discriminant (E);
5687 while Present (Comp) loop
5688 if not OK_Component (Comp) then
5690 Reason_Bad_Component (Comp);
5694 Next_Component_Or_Discriminant (Comp);
5698 -- Deal with address clause case
5700 if Is_Object (E) then
5701 Addr := Address_Clause (E);
5703 if Present (Addr) then
5705 Error_Msg_Sloc := Sloc (Addr);
5706 Error_Msg_N ("\because of Address clause#", N);
5711 -- Deal with independent components for array type
5713 if IC and then Is_Array_Type (E) then
5714 Check_Array_Type (E);
5717 -- Deal with independent components for array object
5719 if IC and then Is_Object (E) and then Is_Array_Type (Etype (E)) then
5720 Check_Array_Type (Etype (E));
5725 end Validate_Independence;
5727 -----------------------------------
5728 -- Validate_Unchecked_Conversion --
5729 -----------------------------------
5731 procedure Validate_Unchecked_Conversion
5733 Act_Unit : Entity_Id)
5740 -- Obtain source and target types. Note that we call Ancestor_Subtype
5741 -- here because the processing for generic instantiation always makes
5742 -- subtypes, and we want the original frozen actual types.
5744 -- If we are dealing with private types, then do the check on their
5745 -- fully declared counterparts if the full declarations have been
5746 -- encountered (they don't have to be visible, but they must exist!)
5748 Source := Ancestor_Subtype (Etype (First_Formal (Act_Unit)));
5750 if Is_Private_Type (Source)
5751 and then Present (Underlying_Type (Source))
5753 Source := Underlying_Type (Source);
5756 Target := Ancestor_Subtype (Etype (Act_Unit));
5758 -- If either type is generic, the instantiation happens within a generic
5759 -- unit, and there is nothing to check. The proper check
5760 -- will happen when the enclosing generic is instantiated.
5762 if Is_Generic_Type (Source) or else Is_Generic_Type (Target) then
5766 if Is_Private_Type (Target)
5767 and then Present (Underlying_Type (Target))
5769 Target := Underlying_Type (Target);
5772 -- Source may be unconstrained array, but not target
5774 if Is_Array_Type (Target)
5775 and then not Is_Constrained (Target)
5778 ("unchecked conversion to unconstrained array not allowed", N);
5782 -- Warn if conversion between two different convention pointers
5784 if Is_Access_Type (Target)
5785 and then Is_Access_Type (Source)
5786 and then Convention (Target) /= Convention (Source)
5787 and then Warn_On_Unchecked_Conversion
5789 -- Give warnings for subprogram pointers only on most targets. The
5790 -- exception is VMS, where data pointers can have different lengths
5791 -- depending on the pointer convention.
5793 if Is_Access_Subprogram_Type (Target)
5794 or else Is_Access_Subprogram_Type (Source)
5795 or else OpenVMS_On_Target
5798 ("?conversion between pointers with different conventions!", N);
5802 -- Warn if one of the operands is Ada.Calendar.Time. Do not emit a
5803 -- warning when compiling GNAT-related sources.
5805 if Warn_On_Unchecked_Conversion
5806 and then not In_Predefined_Unit (N)
5807 and then RTU_Loaded (Ada_Calendar)
5809 (Chars (Source) = Name_Time
5811 Chars (Target) = Name_Time)
5813 -- If Ada.Calendar is loaded and the name of one of the operands is
5814 -- Time, there is a good chance that this is Ada.Calendar.Time.
5817 Calendar_Time : constant Entity_Id :=
5818 Full_View (RTE (RO_CA_Time));
5820 pragma Assert (Present (Calendar_Time));
5822 if Source = Calendar_Time
5823 or else Target = Calendar_Time
5826 ("?representation of 'Time values may change between " &
5827 "'G'N'A'T versions", N);
5832 -- Make entry in unchecked conversion table for later processing by
5833 -- Validate_Unchecked_Conversions, which will check sizes and alignments
5834 -- (using values set by the back-end where possible). This is only done
5835 -- if the appropriate warning is active.
5837 if Warn_On_Unchecked_Conversion then
5838 Unchecked_Conversions.Append
5839 (New_Val => UC_Entry'
5844 -- If both sizes are known statically now, then back end annotation
5845 -- is not required to do a proper check but if either size is not
5846 -- known statically, then we need the annotation.
5848 if Known_Static_RM_Size (Source)
5849 and then Known_Static_RM_Size (Target)
5853 Back_Annotate_Rep_Info := True;
5857 -- If unchecked conversion to access type, and access type is declared
5858 -- in the same unit as the unchecked conversion, then set the
5859 -- No_Strict_Aliasing flag (no strict aliasing is implicit in this
5862 if Is_Access_Type (Target) and then
5863 In_Same_Source_Unit (Target, N)
5865 Set_No_Strict_Aliasing (Implementation_Base_Type (Target));
5868 -- Generate N_Validate_Unchecked_Conversion node for back end in
5869 -- case the back end needs to perform special validation checks.
5871 -- Shouldn't this be in Exp_Ch13, since the check only gets done
5872 -- if we have full expansion and the back end is called ???
5875 Make_Validate_Unchecked_Conversion (Sloc (N));
5876 Set_Source_Type (Vnode, Source);
5877 Set_Target_Type (Vnode, Target);
5879 -- If the unchecked conversion node is in a list, just insert before it.
5880 -- If not we have some strange case, not worth bothering about.
5882 if Is_List_Member (N) then
5883 Insert_After (N, Vnode);
5885 end Validate_Unchecked_Conversion;
5887 ------------------------------------
5888 -- Validate_Unchecked_Conversions --
5889 ------------------------------------
5891 procedure Validate_Unchecked_Conversions is
5893 for N in Unchecked_Conversions.First .. Unchecked_Conversions.Last loop
5895 T : UC_Entry renames Unchecked_Conversions.Table (N);
5897 Eloc : constant Source_Ptr := T.Eloc;
5898 Source : constant Entity_Id := T.Source;
5899 Target : constant Entity_Id := T.Target;
5905 -- This validation check, which warns if we have unequal sizes for
5906 -- unchecked conversion, and thus potentially implementation
5907 -- dependent semantics, is one of the few occasions on which we
5908 -- use the official RM size instead of Esize. See description in
5909 -- Einfo "Handling of Type'Size Values" for details.
5911 if Serious_Errors_Detected = 0
5912 and then Known_Static_RM_Size (Source)
5913 and then Known_Static_RM_Size (Target)
5915 -- Don't do the check if warnings off for either type, note the
5916 -- deliberate use of OR here instead of OR ELSE to get the flag
5917 -- Warnings_Off_Used set for both types if appropriate.
5919 and then not (Has_Warnings_Off (Source)
5921 Has_Warnings_Off (Target))
5923 Source_Siz := RM_Size (Source);
5924 Target_Siz := RM_Size (Target);
5926 if Source_Siz /= Target_Siz then
5928 ("?types for unchecked conversion have different sizes!",
5931 if All_Errors_Mode then
5932 Error_Msg_Name_1 := Chars (Source);
5933 Error_Msg_Uint_1 := Source_Siz;
5934 Error_Msg_Name_2 := Chars (Target);
5935 Error_Msg_Uint_2 := Target_Siz;
5936 Error_Msg ("\size of % is ^, size of % is ^?", Eloc);
5938 Error_Msg_Uint_1 := UI_Abs (Source_Siz - Target_Siz);
5940 if Is_Discrete_Type (Source)
5941 and then Is_Discrete_Type (Target)
5943 if Source_Siz > Target_Siz then
5945 ("\?^ high order bits of source will be ignored!",
5948 elsif Is_Unsigned_Type (Source) then
5950 ("\?source will be extended with ^ high order " &
5951 "zero bits?!", Eloc);
5955 ("\?source will be extended with ^ high order " &
5960 elsif Source_Siz < Target_Siz then
5961 if Is_Discrete_Type (Target) then
5962 if Bytes_Big_Endian then
5964 ("\?target value will include ^ undefined " &
5969 ("\?target value will include ^ undefined " &
5976 ("\?^ trailing bits of target value will be " &
5977 "undefined!", Eloc);
5980 else pragma Assert (Source_Siz > Target_Siz);
5982 ("\?^ trailing bits of source will be ignored!",
5989 -- If both types are access types, we need to check the alignment.
5990 -- If the alignment of both is specified, we can do it here.
5992 if Serious_Errors_Detected = 0
5993 and then Ekind (Source) in Access_Kind
5994 and then Ekind (Target) in Access_Kind
5995 and then Target_Strict_Alignment
5996 and then Present (Designated_Type (Source))
5997 and then Present (Designated_Type (Target))
6000 D_Source : constant Entity_Id := Designated_Type (Source);
6001 D_Target : constant Entity_Id := Designated_Type (Target);
6004 if Known_Alignment (D_Source)
6005 and then Known_Alignment (D_Target)
6008 Source_Align : constant Uint := Alignment (D_Source);
6009 Target_Align : constant Uint := Alignment (D_Target);
6012 if Source_Align < Target_Align
6013 and then not Is_Tagged_Type (D_Source)
6015 -- Suppress warning if warnings suppressed on either
6016 -- type or either designated type. Note the use of
6017 -- OR here instead of OR ELSE. That is intentional,
6018 -- we would like to set flag Warnings_Off_Used in
6019 -- all types for which warnings are suppressed.
6021 and then not (Has_Warnings_Off (D_Source)
6023 Has_Warnings_Off (D_Target)
6025 Has_Warnings_Off (Source)
6027 Has_Warnings_Off (Target))
6029 Error_Msg_Uint_1 := Target_Align;
6030 Error_Msg_Uint_2 := Source_Align;
6031 Error_Msg_Node_1 := D_Target;
6032 Error_Msg_Node_2 := D_Source;
6034 ("?alignment of & (^) is stricter than " &
6035 "alignment of & (^)!", Eloc);
6037 ("\?resulting access value may have invalid " &
6038 "alignment!", Eloc);
6046 end Validate_Unchecked_Conversions;