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 Sinput; use Sinput;
54 with Snames; use Snames;
55 with Stand; use Stand;
56 with Sinfo; use Sinfo;
57 with Stringt; use Stringt;
58 with Targparm; use Targparm;
59 with Ttypes; use Ttypes;
60 with Tbuild; use Tbuild;
61 with Urealp; use Urealp;
63 with GNAT.Heap_Sort_G;
65 package body Sem_Ch13 is
67 SSU : constant Pos := System_Storage_Unit;
68 -- Convenient short hand for commonly used constant
70 -----------------------
71 -- Local Subprograms --
72 -----------------------
74 procedure Alignment_Check_For_Esize_Change (Typ : Entity_Id);
75 -- This routine is called after setting the Esize of type entity Typ.
76 -- The purpose is to deal with the situation where an alignment has been
77 -- inherited from a derived type that is no longer appropriate for the
78 -- new Esize value. In this case, we reset the Alignment to unknown.
80 -----------------------
81 -- Local Subprograms --
82 -----------------------
84 procedure Build_Predicate_Function
88 -- If Typ has predicates (indicated by Has_Predicates being set for Typ,
89 -- then either there are pragma Invariant entries on the rep chain for the
90 -- type (note that Predicate aspects are converted to pragam Predicate), or
91 -- there are inherited aspects from a parent type, or ancestor subtypes,
92 -- or interfaces. This procedure builds the spec and body for the Predicate
93 -- function that tests these predicates, returning them in PDecl and Pbody
94 -- and setting Predicate_Procedure for Typ. In some error situations no
95 -- procedure is built, in which case PDecl/PBody are empty on return.
97 function Get_Alignment_Value (Expr : Node_Id) return Uint;
98 -- Given the expression for an alignment value, returns the corresponding
99 -- Uint value. If the value is inappropriate, then error messages are
100 -- posted as required, and a value of No_Uint is returned.
102 function Is_Operational_Item (N : Node_Id) return Boolean;
103 -- A specification for a stream attribute is allowed before the full type
104 -- is declared, as explained in AI-00137 and the corrigendum. Attributes
105 -- that do not specify a representation characteristic are operational
108 procedure New_Stream_Subprogram
112 Nam : TSS_Name_Type);
113 -- Create a subprogram renaming of a given stream attribute to the
114 -- designated subprogram and then in the tagged case, provide this as a
115 -- primitive operation, or in the non-tagged case make an appropriate TSS
116 -- entry. This is more properly an expansion activity than just semantics,
117 -- but the presence of user-defined stream functions for limited types is a
118 -- legality check, which is why this takes place here rather than in
119 -- exp_ch13, where it was previously. Nam indicates the name of the TSS
120 -- function to be generated.
122 -- To avoid elaboration anomalies with freeze nodes, for untagged types
123 -- we generate both a subprogram declaration and a subprogram renaming
124 -- declaration, so that the attribute specification is handled as a
125 -- renaming_as_body. For tagged types, the specification is one of the
132 Biased : Boolean := True);
133 -- If Biased is True, sets Has_Biased_Representation flag for E, and
134 -- outputs a warning message at node N if Warn_On_Biased_Representation is
135 -- is True. This warning inserts the string Msg to describe the construct
138 ----------------------------------------------
139 -- Table for Validate_Unchecked_Conversions --
140 ----------------------------------------------
142 -- The following table collects unchecked conversions for validation.
143 -- Entries are made by Validate_Unchecked_Conversion and then the
144 -- call to Validate_Unchecked_Conversions does the actual error
145 -- checking and posting of warnings. The reason for this delayed
146 -- processing is to take advantage of back-annotations of size and
147 -- alignment values performed by the back end.
149 -- Note: the reason we store a Source_Ptr value instead of a Node_Id
150 -- is that by the time Validate_Unchecked_Conversions is called, Sprint
151 -- will already have modified all Sloc values if the -gnatD option is set.
153 type UC_Entry is record
154 Eloc : Source_Ptr; -- node used for posting warnings
155 Source : Entity_Id; -- source type for unchecked conversion
156 Target : Entity_Id; -- target type for unchecked conversion
159 package Unchecked_Conversions is new Table.Table (
160 Table_Component_Type => UC_Entry,
161 Table_Index_Type => Int,
162 Table_Low_Bound => 1,
164 Table_Increment => 200,
165 Table_Name => "Unchecked_Conversions");
167 ----------------------------------------
168 -- Table for Validate_Address_Clauses --
169 ----------------------------------------
171 -- If an address clause has the form
173 -- for X'Address use Expr
175 -- where Expr is of the form Y'Address or recursively is a reference
176 -- to a constant of either of these forms, and X and Y are entities of
177 -- objects, then if Y has a smaller alignment than X, that merits a
178 -- warning about possible bad alignment. The following table collects
179 -- address clauses of this kind. We put these in a table so that they
180 -- can be checked after the back end has completed annotation of the
181 -- alignments of objects, since we can catch more cases that way.
183 type Address_Clause_Check_Record is record
185 -- The address clause
188 -- The entity of the object overlaying Y
191 -- The entity of the object being overlaid
194 -- Whether the address is offseted within Y
197 package Address_Clause_Checks is new Table.Table (
198 Table_Component_Type => Address_Clause_Check_Record,
199 Table_Index_Type => Int,
200 Table_Low_Bound => 1,
202 Table_Increment => 200,
203 Table_Name => "Address_Clause_Checks");
205 -----------------------------------------
206 -- Adjust_Record_For_Reverse_Bit_Order --
207 -----------------------------------------
209 procedure Adjust_Record_For_Reverse_Bit_Order (R : Entity_Id) is
214 -- Processing depends on version of Ada
216 -- For Ada 95, we just renumber bits within a storage unit. We do the
217 -- same for Ada 83 mode, since we recognize pragma Bit_Order in Ada 83,
218 -- and are free to add this extension.
220 if Ada_Version < Ada_2005 then
221 Comp := First_Component_Or_Discriminant (R);
222 while Present (Comp) loop
223 CC := Component_Clause (Comp);
225 -- If component clause is present, then deal with the non-default
226 -- bit order case for Ada 95 mode.
228 -- We only do this processing for the base type, and in fact that
229 -- is important, since otherwise if there are record subtypes, we
230 -- could reverse the bits once for each subtype, which is wrong.
233 and then Ekind (R) = E_Record_Type
236 CFB : constant Uint := Component_Bit_Offset (Comp);
237 CSZ : constant Uint := Esize (Comp);
238 CLC : constant Node_Id := Component_Clause (Comp);
239 Pos : constant Node_Id := Position (CLC);
240 FB : constant Node_Id := First_Bit (CLC);
242 Storage_Unit_Offset : constant Uint :=
243 CFB / System_Storage_Unit;
245 Start_Bit : constant Uint :=
246 CFB mod System_Storage_Unit;
249 -- Cases where field goes over storage unit boundary
251 if Start_Bit + CSZ > System_Storage_Unit then
253 -- Allow multi-byte field but generate warning
255 if Start_Bit mod System_Storage_Unit = 0
256 and then CSZ mod System_Storage_Unit = 0
259 ("multi-byte field specified with non-standard"
260 & " Bit_Order?", CLC);
262 if Bytes_Big_Endian then
264 ("bytes are not reversed "
265 & "(component is big-endian)?", CLC);
268 ("bytes are not reversed "
269 & "(component is little-endian)?", CLC);
272 -- Do not allow non-contiguous field
276 ("attempt to specify non-contiguous field "
277 & "not permitted", CLC);
279 ("\caused by non-standard Bit_Order "
282 ("\consider possibility of using "
283 & "Ada 2005 mode here", CLC);
286 -- Case where field fits in one storage unit
289 -- Give warning if suspicious component clause
291 if Intval (FB) >= System_Storage_Unit
292 and then Warn_On_Reverse_Bit_Order
295 ("?Bit_Order clause does not affect " &
296 "byte ordering", Pos);
298 Intval (Pos) + Intval (FB) /
301 ("?position normalized to ^ before bit " &
302 "order interpreted", Pos);
305 -- Here is where we fix up the Component_Bit_Offset value
306 -- to account for the reverse bit order. Some examples of
307 -- what needs to be done are:
309 -- First_Bit .. Last_Bit Component_Bit_Offset
321 -- The rule is that the first bit is is obtained by
322 -- subtracting the old ending bit from storage_unit - 1.
324 Set_Component_Bit_Offset
326 (Storage_Unit_Offset * System_Storage_Unit) +
327 (System_Storage_Unit - 1) -
328 (Start_Bit + CSZ - 1));
330 Set_Normalized_First_Bit
332 Component_Bit_Offset (Comp) mod
333 System_Storage_Unit);
338 Next_Component_Or_Discriminant (Comp);
341 -- For Ada 2005, we do machine scalar processing, as fully described In
342 -- AI-133. This involves gathering all components which start at the
343 -- same byte offset and processing them together. Same approach is still
344 -- valid in later versions including Ada 2012.
348 Max_Machine_Scalar_Size : constant Uint :=
350 (Standard_Long_Long_Integer_Size);
351 -- We use this as the maximum machine scalar size
354 SSU : constant Uint := UI_From_Int (System_Storage_Unit);
357 -- This first loop through components does two things. First it
358 -- deals with the case of components with component clauses whose
359 -- length is greater than the maximum machine scalar size (either
360 -- accepting them or rejecting as needed). Second, it counts the
361 -- number of components with component clauses whose length does
362 -- not exceed this maximum for later processing.
365 Comp := First_Component_Or_Discriminant (R);
366 while Present (Comp) loop
367 CC := Component_Clause (Comp);
371 Fbit : constant Uint :=
372 Static_Integer (First_Bit (CC));
375 -- Case of component with size > max machine scalar
377 if Esize (Comp) > Max_Machine_Scalar_Size then
379 -- Must begin on byte boundary
381 if Fbit mod SSU /= 0 then
383 ("illegal first bit value for "
384 & "reverse bit order",
386 Error_Msg_Uint_1 := SSU;
387 Error_Msg_Uint_2 := Max_Machine_Scalar_Size;
390 ("\must be a multiple of ^ "
391 & "if size greater than ^",
394 -- Must end on byte boundary
396 elsif Esize (Comp) mod SSU /= 0 then
398 ("illegal last bit value for "
399 & "reverse bit order",
401 Error_Msg_Uint_1 := SSU;
402 Error_Msg_Uint_2 := Max_Machine_Scalar_Size;
405 ("\must be a multiple of ^ if size "
409 -- OK, give warning if enabled
411 elsif Warn_On_Reverse_Bit_Order then
413 ("multi-byte field specified with "
414 & " non-standard Bit_Order?", CC);
416 if Bytes_Big_Endian then
418 ("\bytes are not reversed "
419 & "(component is big-endian)?", CC);
422 ("\bytes are not reversed "
423 & "(component is little-endian)?", CC);
427 -- Case where size is not greater than max machine
428 -- scalar. For now, we just count these.
431 Num_CC := Num_CC + 1;
436 Next_Component_Or_Discriminant (Comp);
439 -- We need to sort the component clauses on the basis of the
440 -- Position values in the clause, so we can group clauses with
441 -- the same Position. together to determine the relevant machine
445 Comps : array (0 .. Num_CC) of Entity_Id;
446 -- Array to collect component and discriminant entities. The
447 -- data starts at index 1, the 0'th entry is for the sort
450 function CP_Lt (Op1, Op2 : Natural) return Boolean;
451 -- Compare routine for Sort
453 procedure CP_Move (From : Natural; To : Natural);
454 -- Move routine for Sort
456 package Sorting is new GNAT.Heap_Sort_G (CP_Move, CP_Lt);
460 -- Start and stop positions in the component list of the set of
461 -- components with the same starting position (that constitute
462 -- components in a single machine scalar).
465 -- Maximum last bit value of any component in this set
468 -- Corresponding machine scalar size
474 function CP_Lt (Op1, Op2 : Natural) return Boolean is
476 return Position (Component_Clause (Comps (Op1))) <
477 Position (Component_Clause (Comps (Op2)));
484 procedure CP_Move (From : Natural; To : Natural) is
486 Comps (To) := Comps (From);
489 -- Start of processing for Sort_CC
492 -- Collect the component clauses
495 Comp := First_Component_Or_Discriminant (R);
496 while Present (Comp) loop
497 if Present (Component_Clause (Comp))
498 and then Esize (Comp) <= Max_Machine_Scalar_Size
500 Num_CC := Num_CC + 1;
501 Comps (Num_CC) := Comp;
504 Next_Component_Or_Discriminant (Comp);
507 -- Sort by ascending position number
509 Sorting.Sort (Num_CC);
511 -- We now have all the components whose size does not exceed
512 -- the max machine scalar value, sorted by starting position.
513 -- In this loop we gather groups of clauses starting at the
514 -- same position, to process them in accordance with AI-133.
517 while Stop < Num_CC loop
522 (Last_Bit (Component_Clause (Comps (Start))));
523 while Stop < Num_CC loop
525 (Position (Component_Clause (Comps (Stop + 1)))) =
527 (Position (Component_Clause (Comps (Stop))))
535 (Component_Clause (Comps (Stop)))));
541 -- Now we have a group of component clauses from Start to
542 -- Stop whose positions are identical, and MaxL is the
543 -- maximum last bit value of any of these components.
545 -- We need to determine the corresponding machine scalar
546 -- size. This loop assumes that machine scalar sizes are
547 -- even, and that each possible machine scalar has twice
548 -- as many bits as the next smaller one.
550 MSS := Max_Machine_Scalar_Size;
552 and then (MSS / 2) >= SSU
553 and then (MSS / 2) > MaxL
558 -- Here is where we fix up the Component_Bit_Offset value
559 -- to account for the reverse bit order. Some examples of
560 -- what needs to be done for the case of a machine scalar
563 -- First_Bit .. Last_Bit Component_Bit_Offset
575 -- The rule is that the first bit is obtained by subtracting
576 -- the old ending bit from machine scalar size - 1.
578 for C in Start .. Stop loop
580 Comp : constant Entity_Id := Comps (C);
581 CC : constant Node_Id :=
582 Component_Clause (Comp);
583 LB : constant Uint :=
584 Static_Integer (Last_Bit (CC));
585 NFB : constant Uint := MSS - Uint_1 - LB;
586 NLB : constant Uint := NFB + Esize (Comp) - 1;
587 Pos : constant Uint :=
588 Static_Integer (Position (CC));
591 if Warn_On_Reverse_Bit_Order then
592 Error_Msg_Uint_1 := MSS;
594 ("info: reverse bit order in machine " &
595 "scalar of length^?", First_Bit (CC));
596 Error_Msg_Uint_1 := NFB;
597 Error_Msg_Uint_2 := NLB;
599 if Bytes_Big_Endian then
601 ("?\info: big-endian range for "
602 & "component & is ^ .. ^",
603 First_Bit (CC), Comp);
606 ("?\info: little-endian range "
607 & "for component & is ^ .. ^",
608 First_Bit (CC), Comp);
612 Set_Component_Bit_Offset (Comp, Pos * SSU + NFB);
613 Set_Normalized_First_Bit (Comp, NFB mod SSU);
620 end Adjust_Record_For_Reverse_Bit_Order;
622 --------------------------------------
623 -- Alignment_Check_For_Esize_Change --
624 --------------------------------------
626 procedure Alignment_Check_For_Esize_Change (Typ : Entity_Id) is
628 -- If the alignment is known, and not set by a rep clause, and is
629 -- inconsistent with the size being set, then reset it to unknown,
630 -- we assume in this case that the size overrides the inherited
631 -- alignment, and that the alignment must be recomputed.
633 if Known_Alignment (Typ)
634 and then not Has_Alignment_Clause (Typ)
635 and then Esize (Typ) mod (Alignment (Typ) * SSU) /= 0
637 Init_Alignment (Typ);
639 end Alignment_Check_For_Esize_Change;
641 -----------------------------------
642 -- Analyze_Aspect_Specifications --
643 -----------------------------------
645 procedure Analyze_Aspect_Specifications
654 Ins_Node : Node_Id := N;
655 -- Insert pragmas (except Pre/Post/Invariant/Predicate) after this node
657 -- The general processing involves building an attribute definition
658 -- clause or a pragma node that corresponds to the access type. Then
659 -- one of two things happens:
661 -- If we are required to delay the evaluation of this aspect to the
662 -- freeze point, we preanalyze the relevant argument, and then attach
663 -- the corresponding pragma/attribute definition clause to the aspect
664 -- specification node, which is then placed in the Rep Item chain.
665 -- In this case we mark the entity with the Has_Delayed_Aspects flag,
666 -- and we evaluate the rep item at the freeze point.
668 -- If no delay is required, we just insert the pragma or attribute
669 -- after the declaration, and it will get processed by the normal
670 -- circuit. The From_Aspect_Specification flag is set on the pragma
671 -- or attribute definition node in either case to activate special
672 -- processing (e.g. not traversing the list of homonyms for inline).
674 Delay_Required : Boolean;
675 -- Set True if delay is required
678 -- Return if no aspects
684 -- Return if already analyzed (avoids duplicate calls in some cases
685 -- where type declarations get rewritten and proessed twice).
691 -- Loop through apsects
694 while Present (Aspect) loop
696 Loc : constant Source_Ptr := Sloc (Aspect);
697 Id : constant Node_Id := Identifier (Aspect);
698 Expr : constant Node_Id := Expression (Aspect);
699 Nam : constant Name_Id := Chars (Id);
700 A_Id : constant Aspect_Id := Get_Aspect_Id (Nam);
704 Eloc : Source_Ptr := Sloc (Expr);
705 -- Source location of expression, modified when we split PPC's
708 Set_Entity (Aspect, E);
709 Ent := New_Occurrence_Of (E, Sloc (Id));
711 -- Check for duplicate aspect. Note that the Comes_From_Source
712 -- test allows duplicate Pre/Post's that we generate internally
713 -- to escape being flagged here.
716 while Anod /= Aspect loop
717 if Nam = Chars (Identifier (Anod))
718 and then Comes_From_Source (Aspect)
720 Error_Msg_Name_1 := Nam;
721 Error_Msg_Sloc := Sloc (Anod);
723 -- Case of same aspect specified twice
725 if Class_Present (Anod) = Class_Present (Aspect) then
726 if not Class_Present (Anod) then
728 ("aspect% for & previously given#",
732 ("aspect `%''Class` for & previously given#",
736 -- Case of Pre and Pre'Class both specified
738 elsif Nam = Name_Pre then
739 if Class_Present (Aspect) then
741 ("aspect `Pre''Class` for & is not allowed here",
744 ("\since aspect `Pre` previously given#",
749 ("aspect `Pre` for & is not allowed here",
752 ("\since aspect `Pre''Class` previously given#",
763 -- Processing based on specific aspect
767 -- No_Aspect should be impossible
772 -- Aspects taking an optional boolean argument. For all of
773 -- these we just create a matching pragma and insert it,
774 -- setting flag Cancel_Aspect if the expression is False.
776 when Aspect_Ada_2005 |
779 Aspect_Atomic_Components |
780 Aspect_Discard_Names |
781 Aspect_Favor_Top_Level |
783 Aspect_Inline_Always |
786 Aspect_Persistent_BSS |
787 Aspect_Preelaborable_Initialization |
788 Aspect_Pure_Function |
790 Aspect_Suppress_Debug_Info |
791 Aspect_Unchecked_Union |
792 Aspect_Universal_Aliasing |
794 Aspect_Unreferenced |
795 Aspect_Unreferenced_Objects |
797 Aspect_Volatile_Components =>
799 -- Build corresponding pragma node
803 Pragma_Argument_Associations => New_List (Ent),
805 Make_Identifier (Sloc (Id), Chars (Id)));
807 -- Deal with missing expression case, delay never needed
810 Delay_Required := False;
812 -- Expression is present
815 Preanalyze_Spec_Expression (Expr, Standard_Boolean);
817 -- If preanalysis gives a static expression, we don't
818 -- need to delay (this will happen often in practice).
820 if Is_OK_Static_Expression (Expr) then
821 Delay_Required := False;
823 if Is_False (Expr_Value (Expr)) then
824 Set_Aspect_Cancel (Aitem);
827 -- If we don't get a static expression, then delay, the
828 -- expression may turn out static by freeze time.
831 Delay_Required := True;
835 -- Aspects corresponding to attribute definition clauses
837 when Aspect_Address |
840 Aspect_Component_Size |
841 Aspect_External_Tag |
842 Aspect_Machine_Radix |
845 Aspect_Storage_Pool |
846 Aspect_Storage_Size |
850 -- Preanalyze the expression with the appropriate type
853 when Aspect_Address =>
854 T := RTE (RE_Address);
855 when Aspect_Bit_Order =>
856 T := RTE (RE_Bit_Order);
857 when Aspect_External_Tag =>
858 T := Standard_String;
859 when Aspect_Storage_Pool =>
860 T := Class_Wide_Type (RTE (RE_Root_Storage_Pool));
865 Preanalyze_Spec_Expression (Expr, T);
867 -- Construct the attribute definition clause
870 Make_Attribute_Definition_Clause (Loc,
873 Expression => Relocate_Node (Expr));
875 -- We do not need a delay if we have a static expression
877 if Is_OK_Static_Expression (Expression (Aitem)) then
878 Delay_Required := False;
880 -- Here a delay is required
883 Delay_Required := True;
886 -- Aspects corresponding to pragmas with two arguments, where
887 -- the first argument is a local name referring to the entity,
888 -- and the second argument is the aspect definition expression.
890 when Aspect_Suppress |
893 -- Construct the pragma
897 Pragma_Argument_Associations => New_List (
898 New_Occurrence_Of (E, Eloc),
899 Relocate_Node (Expr)),
901 Make_Identifier (Sloc (Id), Chars (Id)));
903 -- We don't have to play the delay game here, since the only
904 -- values are check names which don't get analyzed anyway.
906 Delay_Required := False;
908 -- Aspects corresponding to stream routines
915 -- Construct the attribute definition clause
918 Make_Attribute_Definition_Clause (Loc,
921 Expression => Relocate_Node (Expr));
923 -- These are always delayed (typically the subprogram that
924 -- is referenced cannot have been declared yet, since it has
925 -- a reference to the type for which this aspect is defined.
927 Delay_Required := True;
929 -- Aspects corresponding to pragmas with two arguments, where
930 -- the second argument is a local name referring to the entity,
931 -- and the first argument is the aspect definition expression.
933 when Aspect_Warnings =>
935 -- Construct the pragma
939 Pragma_Argument_Associations => New_List (
940 Relocate_Node (Expr),
941 New_Occurrence_Of (E, Eloc)),
943 Make_Identifier (Sloc (Id), Chars (Id)),
944 Class_Present => Class_Present (Aspect));
946 -- We don't have to play the delay game here, since the only
947 -- values are check names which don't get analyzed anyway.
949 Delay_Required := False;
951 -- Aspects Pre/Post generate Precondition/Postcondition pragmas
952 -- with a first argument that is the expression, and a second
953 -- argument that is an informative message if the test fails.
954 -- This is inserted right after the declaration, to get the
955 -- required pragma placement. The processing for the pragmas
956 -- takes care of the required delay.
958 when Aspect_Pre | Aspect_Post => declare
962 if A_Id = Aspect_Pre then
963 Pname := Name_Precondition;
965 Pname := Name_Postcondition;
968 -- If the expressions is of the form A and then B, then
969 -- we generate separate Pre/Post aspects for the separate
970 -- clauses. Since we allow multiple pragmas, there is no
971 -- problem in allowing multiple Pre/Post aspects internally.
973 -- We do not do this for Pre'Class, since we have to put
974 -- these conditions together in a complex OR expression
976 if Pname = Name_Postcondition
977 or else not Class_Present (Aspect)
979 while Nkind (Expr) = N_And_Then loop
980 Insert_After (Aspect,
981 Make_Aspect_Specification (Sloc (Right_Opnd (Expr)),
982 Identifier => Identifier (Aspect),
983 Expression => Relocate_Node (Right_Opnd (Expr)),
984 Class_Present => Class_Present (Aspect),
986 Rewrite (Expr, Relocate_Node (Left_Opnd (Expr)));
991 -- Build the precondition/postcondition pragma
996 Make_Identifier (Sloc (Id),
998 Class_Present => Class_Present (Aspect),
999 Split_PPC => Split_PPC (Aspect),
1000 Pragma_Argument_Associations => New_List (
1001 Make_Pragma_Argument_Association (Eloc,
1002 Chars => Name_Check,
1003 Expression => Relocate_Node (Expr))));
1005 -- Add message unless exception messages are suppressed
1007 if not Opt.Exception_Locations_Suppressed then
1008 Append_To (Pragma_Argument_Associations (Aitem),
1009 Make_Pragma_Argument_Association (Eloc,
1010 Chars => Name_Message,
1012 Make_String_Literal (Eloc,
1014 & Get_Name_String (Pname)
1016 & Build_Location_String (Eloc))));
1019 Set_From_Aspect_Specification (Aitem, True);
1021 -- For Pre/Post cases, insert immediately after the entity
1022 -- declaration, since that is the required pragma placement.
1023 -- Note that for these aspects, we do not have to worry
1024 -- about delay issues, since the pragmas themselves deal
1025 -- with delay of visibility for the expression analysis.
1027 -- If the entity is a library-level subprogram, the pre/
1028 -- postconditions must be treated as late pragmas.
1030 if Nkind (Parent (N)) = N_Compilation_Unit then
1031 Add_Global_Declaration (Aitem);
1033 Insert_After (N, Aitem);
1039 -- Invariant aspects generate a corresponding pragma with a
1040 -- first argument that is the entity, and the second argument
1041 -- is the expression and anthird argument with an appropriate
1042 -- message. This is inserted right after the declaration, to
1043 -- get the required pragma placement. The pragma processing
1044 -- takes care of the required delay.
1046 when Aspect_Invariant =>
1048 -- Construct the pragma
1052 Pragma_Argument_Associations =>
1053 New_List (Ent, Relocate_Node (Expr)),
1054 Class_Present => Class_Present (Aspect),
1055 Pragma_Identifier =>
1056 Make_Identifier (Sloc (Id), Name_Invariant));
1058 -- Add message unless exception messages are suppressed
1060 if not Opt.Exception_Locations_Suppressed then
1061 Append_To (Pragma_Argument_Associations (Aitem),
1062 Make_Pragma_Argument_Association (Eloc,
1063 Chars => Name_Message,
1065 Make_String_Literal (Eloc,
1066 Strval => "failed invariant from "
1067 & Build_Location_String (Eloc))));
1070 Set_From_Aspect_Specification (Aitem, True);
1072 -- For Invariant case, insert immediately after the entity
1073 -- declaration. We do not have to worry about delay issues
1074 -- since the pragma processing takes care of this.
1076 Insert_After (N, Aitem);
1079 -- Predicate aspects generate a corresponding pragma with a
1080 -- first argument that is the entity, and the second argument
1081 -- is the expression. This is inserted immediately after the
1082 -- declaration, to get the required pragma placement. The
1083 -- pragma processing takes care of the required delay.
1085 when Aspect_Predicate =>
1087 -- Construct the pragma
1091 Pragma_Argument_Associations =>
1092 New_List (Ent, Relocate_Node (Expr)),
1093 Class_Present => Class_Present (Aspect),
1094 Pragma_Identifier =>
1095 Make_Identifier (Sloc (Id), Name_Predicate));
1097 Set_From_Aspect_Specification (Aitem, True);
1099 -- Make sure we have a freeze node (it might otherwise be
1100 -- missing in cases like subtype X is Y, and we would not
1101 -- have a place to build the predicate function).
1103 Ensure_Freeze_Node (E);
1105 -- For Predicate case, insert immediately after the entity
1106 -- declaration. We do not have to worry about delay issues
1107 -- since the pragma processing takes care of this.
1109 Insert_After (N, Aitem);
1113 Set_From_Aspect_Specification (Aitem, True);
1115 -- If a delay is required, we delay the freeze (not much point in
1116 -- delaying the aspect if we don't delay the freeze!). The pragma
1117 -- or clause is then attached to the aspect specification which
1118 -- is placed in the rep item list.
1120 if Delay_Required then
1121 Ensure_Freeze_Node (E);
1122 Set_Is_Delayed_Aspect (Aitem);
1123 Set_Has_Delayed_Aspects (E);
1124 Set_Aspect_Rep_Item (Aspect, Aitem);
1125 Record_Rep_Item (E, Aspect);
1127 -- If no delay required, insert the pragma/clause in the tree
1130 -- For Pre/Post cases, insert immediately after the entity
1131 -- declaration, since that is the required pragma placement.
1133 if A_Id = Aspect_Pre or else A_Id = Aspect_Post then
1134 Insert_After (N, Aitem);
1136 -- For all other cases, insert in sequence
1139 Insert_After (Ins_Node, Aitem);
1148 end Analyze_Aspect_Specifications;
1150 -----------------------
1151 -- Analyze_At_Clause --
1152 -----------------------
1154 -- An at clause is replaced by the corresponding Address attribute
1155 -- definition clause that is the preferred approach in Ada 95.
1157 procedure Analyze_At_Clause (N : Node_Id) is
1158 CS : constant Boolean := Comes_From_Source (N);
1161 -- This is an obsolescent feature
1163 Check_Restriction (No_Obsolescent_Features, N);
1165 if Warn_On_Obsolescent_Feature then
1167 ("at clause is an obsolescent feature (RM J.7(2))?", N);
1169 ("\use address attribute definition clause instead?", N);
1172 -- Rewrite as address clause
1175 Make_Attribute_Definition_Clause (Sloc (N),
1176 Name => Identifier (N),
1177 Chars => Name_Address,
1178 Expression => Expression (N)));
1180 -- We preserve Comes_From_Source, since logically the clause still
1181 -- comes from the source program even though it is changed in form.
1183 Set_Comes_From_Source (N, CS);
1185 -- Analyze rewritten clause
1187 Analyze_Attribute_Definition_Clause (N);
1188 end Analyze_At_Clause;
1190 -----------------------------------------
1191 -- Analyze_Attribute_Definition_Clause --
1192 -----------------------------------------
1194 procedure Analyze_Attribute_Definition_Clause (N : Node_Id) is
1195 Loc : constant Source_Ptr := Sloc (N);
1196 Nam : constant Node_Id := Name (N);
1197 Attr : constant Name_Id := Chars (N);
1198 Expr : constant Node_Id := Expression (N);
1199 Id : constant Attribute_Id := Get_Attribute_Id (Attr);
1203 FOnly : Boolean := False;
1204 -- Reset to True for subtype specific attribute (Alignment, Size)
1205 -- and for stream attributes, i.e. those cases where in the call
1206 -- to Rep_Item_Too_Late, FOnly is set True so that only the freezing
1207 -- rules are checked. Note that the case of stream attributes is not
1208 -- clear from the RM, but see AI95-00137. Also, the RM seems to
1209 -- disallow Storage_Size for derived task types, but that is also
1210 -- clearly unintentional.
1212 procedure Analyze_Stream_TSS_Definition (TSS_Nam : TSS_Name_Type);
1213 -- Common processing for 'Read, 'Write, 'Input and 'Output attribute
1214 -- definition clauses.
1216 function Duplicate_Clause return Boolean;
1217 -- This routine checks if the aspect for U_Ent being given by attribute
1218 -- definition clause N is for an aspect that has already been specified,
1219 -- and if so gives an error message. If there is a duplicate, True is
1220 -- returned, otherwise if there is no error, False is returned.
1222 -----------------------------------
1223 -- Analyze_Stream_TSS_Definition --
1224 -----------------------------------
1226 procedure Analyze_Stream_TSS_Definition (TSS_Nam : TSS_Name_Type) is
1227 Subp : Entity_Id := Empty;
1232 Is_Read : constant Boolean := (TSS_Nam = TSS_Stream_Read);
1234 function Has_Good_Profile (Subp : Entity_Id) return Boolean;
1235 -- Return true if the entity is a subprogram with an appropriate
1236 -- profile for the attribute being defined.
1238 ----------------------
1239 -- Has_Good_Profile --
1240 ----------------------
1242 function Has_Good_Profile (Subp : Entity_Id) return Boolean is
1244 Is_Function : constant Boolean := (TSS_Nam = TSS_Stream_Input);
1245 Expected_Ekind : constant array (Boolean) of Entity_Kind :=
1246 (False => E_Procedure, True => E_Function);
1250 if Ekind (Subp) /= Expected_Ekind (Is_Function) then
1254 F := First_Formal (Subp);
1257 or else Ekind (Etype (F)) /= E_Anonymous_Access_Type
1258 or else Designated_Type (Etype (F)) /=
1259 Class_Wide_Type (RTE (RE_Root_Stream_Type))
1264 if not Is_Function then
1268 Expected_Mode : constant array (Boolean) of Entity_Kind :=
1269 (False => E_In_Parameter,
1270 True => E_Out_Parameter);
1272 if Parameter_Mode (F) /= Expected_Mode (Is_Read) then
1280 Typ := Etype (Subp);
1283 return Base_Type (Typ) = Base_Type (Ent)
1284 and then No (Next_Formal (F));
1285 end Has_Good_Profile;
1287 -- Start of processing for Analyze_Stream_TSS_Definition
1292 if not Is_Type (U_Ent) then
1293 Error_Msg_N ("local name must be a subtype", Nam);
1297 Pnam := TSS (Base_Type (U_Ent), TSS_Nam);
1299 -- If Pnam is present, it can be either inherited from an ancestor
1300 -- type (in which case it is legal to redefine it for this type), or
1301 -- be a previous definition of the attribute for the same type (in
1302 -- which case it is illegal).
1304 -- In the first case, it will have been analyzed already, and we
1305 -- can check that its profile does not match the expected profile
1306 -- for a stream attribute of U_Ent. In the second case, either Pnam
1307 -- has been analyzed (and has the expected profile), or it has not
1308 -- been analyzed yet (case of a type that has not been frozen yet
1309 -- and for which the stream attribute has been set using Set_TSS).
1312 and then (No (First_Entity (Pnam)) or else Has_Good_Profile (Pnam))
1314 Error_Msg_Sloc := Sloc (Pnam);
1315 Error_Msg_Name_1 := Attr;
1316 Error_Msg_N ("% attribute already defined #", Nam);
1322 if Is_Entity_Name (Expr) then
1323 if not Is_Overloaded (Expr) then
1324 if Has_Good_Profile (Entity (Expr)) then
1325 Subp := Entity (Expr);
1329 Get_First_Interp (Expr, I, It);
1330 while Present (It.Nam) loop
1331 if Has_Good_Profile (It.Nam) then
1336 Get_Next_Interp (I, It);
1341 if Present (Subp) then
1342 if Is_Abstract_Subprogram (Subp) then
1343 Error_Msg_N ("stream subprogram must not be abstract", Expr);
1347 Set_Entity (Expr, Subp);
1348 Set_Etype (Expr, Etype (Subp));
1350 New_Stream_Subprogram (N, U_Ent, Subp, TSS_Nam);
1353 Error_Msg_Name_1 := Attr;
1354 Error_Msg_N ("incorrect expression for% attribute", Expr);
1356 end Analyze_Stream_TSS_Definition;
1358 ----------------------
1359 -- Duplicate_Clause --
1360 ----------------------
1362 function Duplicate_Clause return Boolean is
1366 -- Nothing to do if this attribute definition clause comes from
1367 -- an aspect specification, since we could not be duplicating an
1368 -- explicit clause, and we dealt with the case of duplicated aspects
1369 -- in Analyze_Aspect_Specifications.
1371 if From_Aspect_Specification (N) then
1375 -- Otherwise current clause may duplicate previous clause or a
1376 -- previously given aspect specification for the same aspect.
1378 A := Get_Rep_Item_For_Entity (U_Ent, Chars (N));
1381 if Entity (A) = U_Ent then
1382 Error_Msg_Name_1 := Chars (N);
1383 Error_Msg_Sloc := Sloc (A);
1384 Error_Msg_NE ("aspect% for & previously given#", N, U_Ent);
1390 end Duplicate_Clause;
1392 -- Start of processing for Analyze_Attribute_Definition_Clause
1395 -- Process Ignore_Rep_Clauses option
1397 if Ignore_Rep_Clauses then
1400 -- The following should be ignored. They do not affect legality
1401 -- and may be target dependent. The basic idea of -gnatI is to
1402 -- ignore any rep clauses that may be target dependent but do not
1403 -- affect legality (except possibly to be rejected because they
1404 -- are incompatible with the compilation target).
1406 when Attribute_Alignment |
1407 Attribute_Bit_Order |
1408 Attribute_Component_Size |
1409 Attribute_Machine_Radix |
1410 Attribute_Object_Size |
1413 Attribute_Stream_Size |
1414 Attribute_Value_Size =>
1416 Rewrite (N, Make_Null_Statement (Sloc (N)));
1419 -- The following should not be ignored, because in the first place
1420 -- they are reasonably portable, and should not cause problems in
1421 -- compiling code from another target, and also they do affect
1422 -- legality, e.g. failing to provide a stream attribute for a
1423 -- type may make a program illegal.
1425 when Attribute_External_Tag |
1429 Attribute_Storage_Pool |
1430 Attribute_Storage_Size |
1434 -- Other cases are errors ("attribute& cannot be set with
1435 -- definition clause"), which will be caught below.
1443 Ent := Entity (Nam);
1445 if Rep_Item_Too_Early (Ent, N) then
1449 -- Rep clause applies to full view of incomplete type or private type if
1450 -- we have one (if not, this is a premature use of the type). However,
1451 -- certain semantic checks need to be done on the specified entity (i.e.
1452 -- the private view), so we save it in Ent.
1454 if Is_Private_Type (Ent)
1455 and then Is_Derived_Type (Ent)
1456 and then not Is_Tagged_Type (Ent)
1457 and then No (Full_View (Ent))
1459 -- If this is a private type whose completion is a derivation from
1460 -- another private type, there is no full view, and the attribute
1461 -- belongs to the type itself, not its underlying parent.
1465 elsif Ekind (Ent) = E_Incomplete_Type then
1467 -- The attribute applies to the full view, set the entity of the
1468 -- attribute definition accordingly.
1470 Ent := Underlying_Type (Ent);
1472 Set_Entity (Nam, Ent);
1475 U_Ent := Underlying_Type (Ent);
1478 -- Complete other routine error checks
1480 if Etype (Nam) = Any_Type then
1483 elsif Scope (Ent) /= Current_Scope then
1484 Error_Msg_N ("entity must be declared in this scope", Nam);
1487 elsif No (U_Ent) then
1490 elsif Is_Type (U_Ent)
1491 and then not Is_First_Subtype (U_Ent)
1492 and then Id /= Attribute_Object_Size
1493 and then Id /= Attribute_Value_Size
1494 and then not From_At_Mod (N)
1496 Error_Msg_N ("cannot specify attribute for subtype", Nam);
1500 Set_Entity (N, U_Ent);
1502 -- Switch on particular attribute
1510 -- Address attribute definition clause
1512 when Attribute_Address => Address : begin
1514 -- A little error check, catch for X'Address use X'Address;
1516 if Nkind (Nam) = N_Identifier
1517 and then Nkind (Expr) = N_Attribute_Reference
1518 and then Attribute_Name (Expr) = Name_Address
1519 and then Nkind (Prefix (Expr)) = N_Identifier
1520 and then Chars (Nam) = Chars (Prefix (Expr))
1523 ("address for & is self-referencing", Prefix (Expr), Ent);
1527 -- Not that special case, carry on with analysis of expression
1529 Analyze_And_Resolve (Expr, RTE (RE_Address));
1531 -- Even when ignoring rep clauses we need to indicate that the
1532 -- entity has an address clause and thus it is legal to declare
1535 if Ignore_Rep_Clauses then
1536 if Ekind_In (U_Ent, E_Variable, E_Constant) then
1537 Record_Rep_Item (U_Ent, N);
1543 if Duplicate_Clause then
1546 -- Case of address clause for subprogram
1548 elsif Is_Subprogram (U_Ent) then
1549 if Has_Homonym (U_Ent) then
1551 ("address clause cannot be given " &
1552 "for overloaded subprogram",
1557 -- For subprograms, all address clauses are permitted, and we
1558 -- mark the subprogram as having a deferred freeze so that Gigi
1559 -- will not elaborate it too soon.
1561 -- Above needs more comments, what is too soon about???
1563 Set_Has_Delayed_Freeze (U_Ent);
1565 -- Case of address clause for entry
1567 elsif Ekind (U_Ent) = E_Entry then
1568 if Nkind (Parent (N)) = N_Task_Body then
1570 ("entry address must be specified in task spec", Nam);
1574 -- For entries, we require a constant address
1576 Check_Constant_Address_Clause (Expr, U_Ent);
1578 -- Special checks for task types
1580 if Is_Task_Type (Scope (U_Ent))
1581 and then Comes_From_Source (Scope (U_Ent))
1584 ("?entry address declared for entry in task type", N);
1586 ("\?only one task can be declared of this type", N);
1589 -- Entry address clauses are obsolescent
1591 Check_Restriction (No_Obsolescent_Features, N);
1593 if Warn_On_Obsolescent_Feature then
1595 ("attaching interrupt to task entry is an " &
1596 "obsolescent feature (RM J.7.1)?", N);
1598 ("\use interrupt procedure instead?", N);
1601 -- Case of an address clause for a controlled object which we
1602 -- consider to be erroneous.
1604 elsif Is_Controlled (Etype (U_Ent))
1605 or else Has_Controlled_Component (Etype (U_Ent))
1608 ("?controlled object& must not be overlaid", Nam, U_Ent);
1610 ("\?Program_Error will be raised at run time", Nam);
1611 Insert_Action (Declaration_Node (U_Ent),
1612 Make_Raise_Program_Error (Loc,
1613 Reason => PE_Overlaid_Controlled_Object));
1616 -- Case of address clause for a (non-controlled) object
1619 Ekind (U_Ent) = E_Variable
1621 Ekind (U_Ent) = E_Constant
1624 Expr : constant Node_Id := Expression (N);
1629 -- Exported variables cannot have an address clause, because
1630 -- this cancels the effect of the pragma Export.
1632 if Is_Exported (U_Ent) then
1634 ("cannot export object with address clause", Nam);
1638 Find_Overlaid_Entity (N, O_Ent, Off);
1640 -- Overlaying controlled objects is erroneous
1643 and then (Has_Controlled_Component (Etype (O_Ent))
1644 or else Is_Controlled (Etype (O_Ent)))
1647 ("?cannot overlay with controlled object", Expr);
1649 ("\?Program_Error will be raised at run time", Expr);
1650 Insert_Action (Declaration_Node (U_Ent),
1651 Make_Raise_Program_Error (Loc,
1652 Reason => PE_Overlaid_Controlled_Object));
1655 elsif Present (O_Ent)
1656 and then Ekind (U_Ent) = E_Constant
1657 and then not Is_Constant_Object (O_Ent)
1659 Error_Msg_N ("constant overlays a variable?", Expr);
1661 elsif Present (Renamed_Object (U_Ent)) then
1663 ("address clause not allowed"
1664 & " for a renaming declaration (RM 13.1(6))", Nam);
1667 -- Imported variables can have an address clause, but then
1668 -- the import is pretty meaningless except to suppress
1669 -- initializations, so we do not need such variables to
1670 -- be statically allocated (and in fact it causes trouble
1671 -- if the address clause is a local value).
1673 elsif Is_Imported (U_Ent) then
1674 Set_Is_Statically_Allocated (U_Ent, False);
1677 -- We mark a possible modification of a variable with an
1678 -- address clause, since it is likely aliasing is occurring.
1680 Note_Possible_Modification (Nam, Sure => False);
1682 -- Here we are checking for explicit overlap of one variable
1683 -- by another, and if we find this then mark the overlapped
1684 -- variable as also being volatile to prevent unwanted
1685 -- optimizations. This is a significant pessimization so
1686 -- avoid it when there is an offset, i.e. when the object
1687 -- is composite; they cannot be optimized easily anyway.
1690 and then Is_Object (O_Ent)
1693 Set_Treat_As_Volatile (O_Ent);
1696 -- Legality checks on the address clause for initialized
1697 -- objects is deferred until the freeze point, because
1698 -- a subsequent pragma might indicate that the object is
1699 -- imported and thus not initialized.
1701 Set_Has_Delayed_Freeze (U_Ent);
1703 -- If an initialization call has been generated for this
1704 -- object, it needs to be deferred to after the freeze node
1705 -- we have just now added, otherwise GIGI will see a
1706 -- reference to the variable (as actual to the IP call)
1707 -- before its definition.
1710 Init_Call : constant Node_Id := Find_Init_Call (U_Ent, N);
1712 if Present (Init_Call) then
1714 Append_Freeze_Action (U_Ent, Init_Call);
1718 if Is_Exported (U_Ent) then
1720 ("& cannot be exported if an address clause is given",
1723 ("\define and export a variable " &
1724 "that holds its address instead",
1728 -- Entity has delayed freeze, so we will generate an
1729 -- alignment check at the freeze point unless suppressed.
1731 if not Range_Checks_Suppressed (U_Ent)
1732 and then not Alignment_Checks_Suppressed (U_Ent)
1734 Set_Check_Address_Alignment (N);
1737 -- Kill the size check code, since we are not allocating
1738 -- the variable, it is somewhere else.
1740 Kill_Size_Check_Code (U_Ent);
1742 -- If the address clause is of the form:
1744 -- for Y'Address use X'Address
1748 -- Const : constant Address := X'Address;
1750 -- for Y'Address use Const;
1752 -- then we make an entry in the table for checking the size
1753 -- and alignment of the overlaying variable. We defer this
1754 -- check till after code generation to take full advantage
1755 -- of the annotation done by the back end. This entry is
1756 -- only made if the address clause comes from source.
1757 -- If the entity has a generic type, the check will be
1758 -- performed in the instance if the actual type justifies
1759 -- it, and we do not insert the clause in the table to
1760 -- prevent spurious warnings.
1762 if Address_Clause_Overlay_Warnings
1763 and then Comes_From_Source (N)
1764 and then Present (O_Ent)
1765 and then Is_Object (O_Ent)
1767 if not Is_Generic_Type (Etype (U_Ent)) then
1768 Address_Clause_Checks.Append ((N, U_Ent, O_Ent, Off));
1771 -- If variable overlays a constant view, and we are
1772 -- warning on overlays, then mark the variable as
1773 -- overlaying a constant (we will give warnings later
1774 -- if this variable is assigned).
1776 if Is_Constant_Object (O_Ent)
1777 and then Ekind (U_Ent) = E_Variable
1779 Set_Overlays_Constant (U_Ent);
1784 -- Not a valid entity for an address clause
1787 Error_Msg_N ("address cannot be given for &", Nam);
1795 -- Alignment attribute definition clause
1797 when Attribute_Alignment => Alignment : declare
1798 Align : constant Uint := Get_Alignment_Value (Expr);
1803 if not Is_Type (U_Ent)
1804 and then Ekind (U_Ent) /= E_Variable
1805 and then Ekind (U_Ent) /= E_Constant
1807 Error_Msg_N ("alignment cannot be given for &", Nam);
1809 elsif Duplicate_Clause then
1812 elsif Align /= No_Uint then
1813 Set_Has_Alignment_Clause (U_Ent);
1814 Set_Alignment (U_Ent, Align);
1816 -- For an array type, U_Ent is the first subtype. In that case,
1817 -- also set the alignment of the anonymous base type so that
1818 -- other subtypes (such as the itypes for aggregates of the
1819 -- type) also receive the expected alignment.
1821 if Is_Array_Type (U_Ent) then
1822 Set_Alignment (Base_Type (U_Ent), Align);
1831 -- Bit_Order attribute definition clause
1833 when Attribute_Bit_Order => Bit_Order : declare
1835 if not Is_Record_Type (U_Ent) then
1837 ("Bit_Order can only be defined for record type", Nam);
1839 elsif Duplicate_Clause then
1843 Analyze_And_Resolve (Expr, RTE (RE_Bit_Order));
1845 if Etype (Expr) = Any_Type then
1848 elsif not Is_Static_Expression (Expr) then
1849 Flag_Non_Static_Expr
1850 ("Bit_Order requires static expression!", Expr);
1853 if (Expr_Value (Expr) = 0) /= Bytes_Big_Endian then
1854 Set_Reverse_Bit_Order (U_Ent, True);
1860 --------------------
1861 -- Component_Size --
1862 --------------------
1864 -- Component_Size attribute definition clause
1866 when Attribute_Component_Size => Component_Size_Case : declare
1867 Csize : constant Uint := Static_Integer (Expr);
1871 New_Ctyp : Entity_Id;
1875 if not Is_Array_Type (U_Ent) then
1876 Error_Msg_N ("component size requires array type", Nam);
1880 Btype := Base_Type (U_Ent);
1881 Ctyp := Component_Type (Btype);
1883 if Duplicate_Clause then
1886 elsif Rep_Item_Too_Early (Btype, N) then
1889 elsif Csize /= No_Uint then
1890 Check_Size (Expr, Ctyp, Csize, Biased);
1892 -- For the biased case, build a declaration for a subtype that
1893 -- will be used to represent the biased subtype that reflects
1894 -- the biased representation of components. We need the subtype
1895 -- to get proper conversions on referencing elements of the
1896 -- array. Note: component size clauses are ignored in VM mode.
1898 if VM_Target = No_VM then
1901 Make_Defining_Identifier (Loc,
1903 New_External_Name (Chars (U_Ent), 'C', 0, 'T'));
1906 Make_Subtype_Declaration (Loc,
1907 Defining_Identifier => New_Ctyp,
1908 Subtype_Indication =>
1909 New_Occurrence_Of (Component_Type (Btype), Loc));
1911 Set_Parent (Decl, N);
1912 Analyze (Decl, Suppress => All_Checks);
1914 Set_Has_Delayed_Freeze (New_Ctyp, False);
1915 Set_Esize (New_Ctyp, Csize);
1916 Set_RM_Size (New_Ctyp, Csize);
1917 Init_Alignment (New_Ctyp);
1918 Set_Is_Itype (New_Ctyp, True);
1919 Set_Associated_Node_For_Itype (New_Ctyp, U_Ent);
1921 Set_Component_Type (Btype, New_Ctyp);
1922 Set_Biased (New_Ctyp, N, "component size clause");
1925 Set_Component_Size (Btype, Csize);
1927 -- For VM case, we ignore component size clauses
1930 -- Give a warning unless we are in GNAT mode, in which case
1931 -- the warning is suppressed since it is not useful.
1933 if not GNAT_Mode then
1935 ("?component size ignored in this configuration", N);
1939 -- Deal with warning on overridden size
1941 if Warn_On_Overridden_Size
1942 and then Has_Size_Clause (Ctyp)
1943 and then RM_Size (Ctyp) /= Csize
1946 ("?component size overrides size clause for&",
1950 Set_Has_Component_Size_Clause (Btype, True);
1951 Set_Has_Non_Standard_Rep (Btype, True);
1953 end Component_Size_Case;
1959 when Attribute_External_Tag => External_Tag :
1961 if not Is_Tagged_Type (U_Ent) then
1962 Error_Msg_N ("should be a tagged type", Nam);
1965 if Duplicate_Clause then
1969 Analyze_And_Resolve (Expr, Standard_String);
1971 if not Is_Static_Expression (Expr) then
1972 Flag_Non_Static_Expr
1973 ("static string required for tag name!", Nam);
1976 if VM_Target = No_VM then
1977 Set_Has_External_Tag_Rep_Clause (U_Ent);
1979 Error_Msg_Name_1 := Attr;
1981 ("% attribute unsupported in this configuration", Nam);
1984 if not Is_Library_Level_Entity (U_Ent) then
1986 ("?non-unique external tag supplied for &", N, U_Ent);
1988 ("?\same external tag applies to all subprogram calls", N);
1990 ("?\corresponding internal tag cannot be obtained", N);
1999 when Attribute_Input =>
2000 Analyze_Stream_TSS_Definition (TSS_Stream_Input);
2001 Set_Has_Specified_Stream_Input (Ent);
2007 -- Machine radix attribute definition clause
2009 when Attribute_Machine_Radix => Machine_Radix : declare
2010 Radix : constant Uint := Static_Integer (Expr);
2013 if not Is_Decimal_Fixed_Point_Type (U_Ent) then
2014 Error_Msg_N ("decimal fixed-point type expected for &", Nam);
2016 elsif Duplicate_Clause then
2019 elsif Radix /= No_Uint then
2020 Set_Has_Machine_Radix_Clause (U_Ent);
2021 Set_Has_Non_Standard_Rep (Base_Type (U_Ent));
2025 elsif Radix = 10 then
2026 Set_Machine_Radix_10 (U_Ent);
2028 Error_Msg_N ("machine radix value must be 2 or 10", Expr);
2037 -- Object_Size attribute definition clause
2039 when Attribute_Object_Size => Object_Size : declare
2040 Size : constant Uint := Static_Integer (Expr);
2043 pragma Warnings (Off, Biased);
2046 if not Is_Type (U_Ent) then
2047 Error_Msg_N ("Object_Size cannot be given for &", Nam);
2049 elsif Duplicate_Clause then
2053 Check_Size (Expr, U_Ent, Size, Biased);
2061 UI_Mod (Size, 64) /= 0
2064 ("Object_Size must be 8, 16, 32, or multiple of 64",
2068 Set_Esize (U_Ent, Size);
2069 Set_Has_Object_Size_Clause (U_Ent);
2070 Alignment_Check_For_Esize_Change (U_Ent);
2078 when Attribute_Output =>
2079 Analyze_Stream_TSS_Definition (TSS_Stream_Output);
2080 Set_Has_Specified_Stream_Output (Ent);
2086 when Attribute_Read =>
2087 Analyze_Stream_TSS_Definition (TSS_Stream_Read);
2088 Set_Has_Specified_Stream_Read (Ent);
2094 -- Size attribute definition clause
2096 when Attribute_Size => Size : declare
2097 Size : constant Uint := Static_Integer (Expr);
2104 if Duplicate_Clause then
2107 elsif not Is_Type (U_Ent)
2108 and then Ekind (U_Ent) /= E_Variable
2109 and then Ekind (U_Ent) /= E_Constant
2111 Error_Msg_N ("size cannot be given for &", Nam);
2113 elsif Is_Array_Type (U_Ent)
2114 and then not Is_Constrained (U_Ent)
2117 ("size cannot be given for unconstrained array", Nam);
2119 elsif Size /= No_Uint then
2121 if VM_Target /= No_VM and then not GNAT_Mode then
2123 -- Size clause is not handled properly on VM targets.
2124 -- Display a warning unless we are in GNAT mode, in which
2125 -- case this is useless.
2128 ("?size clauses are ignored in this configuration", N);
2131 if Is_Type (U_Ent) then
2134 Etyp := Etype (U_Ent);
2137 -- Check size, note that Gigi is in charge of checking that the
2138 -- size of an array or record type is OK. Also we do not check
2139 -- the size in the ordinary fixed-point case, since it is too
2140 -- early to do so (there may be subsequent small clause that
2141 -- affects the size). We can check the size if a small clause
2142 -- has already been given.
2144 if not Is_Ordinary_Fixed_Point_Type (U_Ent)
2145 or else Has_Small_Clause (U_Ent)
2147 Check_Size (Expr, Etyp, Size, Biased);
2148 Set_Biased (U_Ent, N, "size clause", Biased);
2151 -- For types set RM_Size and Esize if possible
2153 if Is_Type (U_Ent) then
2154 Set_RM_Size (U_Ent, Size);
2156 -- For scalar types, increase Object_Size to power of 2, but
2157 -- not less than a storage unit in any case (i.e., normally
2158 -- this means it will be byte addressable).
2160 if Is_Scalar_Type (U_Ent) then
2161 if Size <= System_Storage_Unit then
2162 Init_Esize (U_Ent, System_Storage_Unit);
2163 elsif Size <= 16 then
2164 Init_Esize (U_Ent, 16);
2165 elsif Size <= 32 then
2166 Init_Esize (U_Ent, 32);
2168 Set_Esize (U_Ent, (Size + 63) / 64 * 64);
2171 -- For all other types, object size = value size. The
2172 -- backend will adjust as needed.
2175 Set_Esize (U_Ent, Size);
2178 Alignment_Check_For_Esize_Change (U_Ent);
2180 -- For objects, set Esize only
2183 if Is_Elementary_Type (Etyp) then
2184 if Size /= System_Storage_Unit
2186 Size /= System_Storage_Unit * 2
2188 Size /= System_Storage_Unit * 4
2190 Size /= System_Storage_Unit * 8
2192 Error_Msg_Uint_1 := UI_From_Int (System_Storage_Unit);
2193 Error_Msg_Uint_2 := Error_Msg_Uint_1 * 8;
2195 ("size for primitive object must be a power of 2"
2196 & " in the range ^-^", N);
2200 Set_Esize (U_Ent, Size);
2203 Set_Has_Size_Clause (U_Ent);
2211 -- Small attribute definition clause
2213 when Attribute_Small => Small : declare
2214 Implicit_Base : constant Entity_Id := Base_Type (U_Ent);
2218 Analyze_And_Resolve (Expr, Any_Real);
2220 if Etype (Expr) = Any_Type then
2223 elsif not Is_Static_Expression (Expr) then
2224 Flag_Non_Static_Expr
2225 ("small requires static expression!", Expr);
2229 Small := Expr_Value_R (Expr);
2231 if Small <= Ureal_0 then
2232 Error_Msg_N ("small value must be greater than zero", Expr);
2238 if not Is_Ordinary_Fixed_Point_Type (U_Ent) then
2240 ("small requires an ordinary fixed point type", Nam);
2242 elsif Has_Small_Clause (U_Ent) then
2243 Error_Msg_N ("small already given for &", Nam);
2245 elsif Small > Delta_Value (U_Ent) then
2247 ("small value must not be greater then delta value", Nam);
2250 Set_Small_Value (U_Ent, Small);
2251 Set_Small_Value (Implicit_Base, Small);
2252 Set_Has_Small_Clause (U_Ent);
2253 Set_Has_Small_Clause (Implicit_Base);
2254 Set_Has_Non_Standard_Rep (Implicit_Base);
2262 -- Storage_Pool attribute definition clause
2264 when Attribute_Storage_Pool => Storage_Pool : declare
2269 if Ekind (U_Ent) = E_Access_Subprogram_Type then
2271 ("storage pool cannot be given for access-to-subprogram type",
2276 Ekind_In (U_Ent, E_Access_Type, E_General_Access_Type)
2279 ("storage pool can only be given for access types", Nam);
2282 elsif Is_Derived_Type (U_Ent) then
2284 ("storage pool cannot be given for a derived access type",
2287 elsif Duplicate_Clause then
2290 elsif Present (Associated_Storage_Pool (U_Ent)) then
2291 Error_Msg_N ("storage pool already given for &", Nam);
2296 (Expr, Class_Wide_Type (RTE (RE_Root_Storage_Pool)));
2298 if not Denotes_Variable (Expr) then
2299 Error_Msg_N ("storage pool must be a variable", Expr);
2303 if Nkind (Expr) = N_Type_Conversion then
2304 T := Etype (Expression (Expr));
2309 -- The Stack_Bounded_Pool is used internally for implementing
2310 -- access types with a Storage_Size. Since it only work
2311 -- properly when used on one specific type, we need to check
2312 -- that it is not hijacked improperly:
2313 -- type T is access Integer;
2314 -- for T'Storage_Size use n;
2315 -- type Q is access Float;
2316 -- for Q'Storage_Size use T'Storage_Size; -- incorrect
2318 if RTE_Available (RE_Stack_Bounded_Pool)
2319 and then Base_Type (T) = RTE (RE_Stack_Bounded_Pool)
2321 Error_Msg_N ("non-shareable internal Pool", Expr);
2325 -- If the argument is a name that is not an entity name, then
2326 -- we construct a renaming operation to define an entity of
2327 -- type storage pool.
2329 if not Is_Entity_Name (Expr)
2330 and then Is_Object_Reference (Expr)
2332 Pool := Make_Temporary (Loc, 'P', Expr);
2335 Rnode : constant Node_Id :=
2336 Make_Object_Renaming_Declaration (Loc,
2337 Defining_Identifier => Pool,
2339 New_Occurrence_Of (Etype (Expr), Loc),
2343 Insert_Before (N, Rnode);
2345 Set_Associated_Storage_Pool (U_Ent, Pool);
2348 elsif Is_Entity_Name (Expr) then
2349 Pool := Entity (Expr);
2351 -- If pool is a renamed object, get original one. This can
2352 -- happen with an explicit renaming, and within instances.
2354 while Present (Renamed_Object (Pool))
2355 and then Is_Entity_Name (Renamed_Object (Pool))
2357 Pool := Entity (Renamed_Object (Pool));
2360 if Present (Renamed_Object (Pool))
2361 and then Nkind (Renamed_Object (Pool)) = N_Type_Conversion
2362 and then Is_Entity_Name (Expression (Renamed_Object (Pool)))
2364 Pool := Entity (Expression (Renamed_Object (Pool)));
2367 Set_Associated_Storage_Pool (U_Ent, Pool);
2369 elsif Nkind (Expr) = N_Type_Conversion
2370 and then Is_Entity_Name (Expression (Expr))
2371 and then Nkind (Original_Node (Expr)) = N_Attribute_Reference
2373 Pool := Entity (Expression (Expr));
2374 Set_Associated_Storage_Pool (U_Ent, Pool);
2377 Error_Msg_N ("incorrect reference to a Storage Pool", Expr);
2386 -- Storage_Size attribute definition clause
2388 when Attribute_Storage_Size => Storage_Size : declare
2389 Btype : constant Entity_Id := Base_Type (U_Ent);
2393 if Is_Task_Type (U_Ent) then
2394 Check_Restriction (No_Obsolescent_Features, N);
2396 if Warn_On_Obsolescent_Feature then
2398 ("storage size clause for task is an " &
2399 "obsolescent feature (RM J.9)?", N);
2400 Error_Msg_N ("\use Storage_Size pragma instead?", N);
2406 if not Is_Access_Type (U_Ent)
2407 and then Ekind (U_Ent) /= E_Task_Type
2409 Error_Msg_N ("storage size cannot be given for &", Nam);
2411 elsif Is_Access_Type (U_Ent) and Is_Derived_Type (U_Ent) then
2413 ("storage size cannot be given for a derived access type",
2416 elsif Duplicate_Clause then
2420 Analyze_And_Resolve (Expr, Any_Integer);
2422 if Is_Access_Type (U_Ent) then
2423 if Present (Associated_Storage_Pool (U_Ent)) then
2424 Error_Msg_N ("storage pool already given for &", Nam);
2428 if Is_OK_Static_Expression (Expr)
2429 and then Expr_Value (Expr) = 0
2431 Set_No_Pool_Assigned (Btype);
2434 else -- Is_Task_Type (U_Ent)
2435 Sprag := Get_Rep_Pragma (Btype, Name_Storage_Size);
2437 if Present (Sprag) then
2438 Error_Msg_Sloc := Sloc (Sprag);
2440 ("Storage_Size already specified#", Nam);
2445 Set_Has_Storage_Size_Clause (Btype);
2453 when Attribute_Stream_Size => Stream_Size : declare
2454 Size : constant Uint := Static_Integer (Expr);
2457 if Ada_Version <= Ada_95 then
2458 Check_Restriction (No_Implementation_Attributes, N);
2461 if Duplicate_Clause then
2464 elsif Is_Elementary_Type (U_Ent) then
2465 if Size /= System_Storage_Unit
2467 Size /= System_Storage_Unit * 2
2469 Size /= System_Storage_Unit * 4
2471 Size /= System_Storage_Unit * 8
2473 Error_Msg_Uint_1 := UI_From_Int (System_Storage_Unit);
2475 ("stream size for elementary type must be a"
2476 & " power of 2 and at least ^", N);
2478 elsif RM_Size (U_Ent) > Size then
2479 Error_Msg_Uint_1 := RM_Size (U_Ent);
2481 ("stream size for elementary type must be a"
2482 & " power of 2 and at least ^", N);
2485 Set_Has_Stream_Size_Clause (U_Ent);
2488 Error_Msg_N ("Stream_Size cannot be given for &", Nam);
2496 -- Value_Size attribute definition clause
2498 when Attribute_Value_Size => Value_Size : declare
2499 Size : constant Uint := Static_Integer (Expr);
2503 if not Is_Type (U_Ent) then
2504 Error_Msg_N ("Value_Size cannot be given for &", Nam);
2506 elsif Duplicate_Clause then
2509 elsif Is_Array_Type (U_Ent)
2510 and then not Is_Constrained (U_Ent)
2513 ("Value_Size cannot be given for unconstrained array", Nam);
2516 if Is_Elementary_Type (U_Ent) then
2517 Check_Size (Expr, U_Ent, Size, Biased);
2518 Set_Biased (U_Ent, N, "value size clause", Biased);
2521 Set_RM_Size (U_Ent, Size);
2529 when Attribute_Write =>
2530 Analyze_Stream_TSS_Definition (TSS_Stream_Write);
2531 Set_Has_Specified_Stream_Write (Ent);
2533 -- All other attributes cannot be set
2537 ("attribute& cannot be set with definition clause", N);
2540 -- The test for the type being frozen must be performed after
2541 -- any expression the clause has been analyzed since the expression
2542 -- itself might cause freezing that makes the clause illegal.
2544 if Rep_Item_Too_Late (U_Ent, N, FOnly) then
2547 end Analyze_Attribute_Definition_Clause;
2549 ----------------------------
2550 -- Analyze_Code_Statement --
2551 ----------------------------
2553 procedure Analyze_Code_Statement (N : Node_Id) is
2554 HSS : constant Node_Id := Parent (N);
2555 SBody : constant Node_Id := Parent (HSS);
2556 Subp : constant Entity_Id := Current_Scope;
2563 -- Analyze and check we get right type, note that this implements the
2564 -- requirement (RM 13.8(1)) that Machine_Code be with'ed, since that
2565 -- is the only way that Asm_Insn could possibly be visible.
2567 Analyze_And_Resolve (Expression (N));
2569 if Etype (Expression (N)) = Any_Type then
2571 elsif Etype (Expression (N)) /= RTE (RE_Asm_Insn) then
2572 Error_Msg_N ("incorrect type for code statement", N);
2576 Check_Code_Statement (N);
2578 -- Make sure we appear in the handled statement sequence of a
2579 -- subprogram (RM 13.8(3)).
2581 if Nkind (HSS) /= N_Handled_Sequence_Of_Statements
2582 or else Nkind (SBody) /= N_Subprogram_Body
2585 ("code statement can only appear in body of subprogram", N);
2589 -- Do remaining checks (RM 13.8(3)) if not already done
2591 if not Is_Machine_Code_Subprogram (Subp) then
2592 Set_Is_Machine_Code_Subprogram (Subp);
2594 -- No exception handlers allowed
2596 if Present (Exception_Handlers (HSS)) then
2598 ("exception handlers not permitted in machine code subprogram",
2599 First (Exception_Handlers (HSS)));
2602 -- No declarations other than use clauses and pragmas (we allow
2603 -- certain internally generated declarations as well).
2605 Decl := First (Declarations (SBody));
2606 while Present (Decl) loop
2607 DeclO := Original_Node (Decl);
2608 if Comes_From_Source (DeclO)
2609 and not Nkind_In (DeclO, N_Pragma,
2610 N_Use_Package_Clause,
2612 N_Implicit_Label_Declaration)
2615 ("this declaration not allowed in machine code subprogram",
2622 -- No statements other than code statements, pragmas, and labels.
2623 -- Again we allow certain internally generated statements.
2625 Stmt := First (Statements (HSS));
2626 while Present (Stmt) loop
2627 StmtO := Original_Node (Stmt);
2628 if Comes_From_Source (StmtO)
2629 and then not Nkind_In (StmtO, N_Pragma,
2634 ("this statement is not allowed in machine code subprogram",
2641 end Analyze_Code_Statement;
2643 -----------------------------------------------
2644 -- Analyze_Enumeration_Representation_Clause --
2645 -----------------------------------------------
2647 procedure Analyze_Enumeration_Representation_Clause (N : Node_Id) is
2648 Ident : constant Node_Id := Identifier (N);
2649 Aggr : constant Node_Id := Array_Aggregate (N);
2650 Enumtype : Entity_Id;
2656 Err : Boolean := False;
2658 Lo : constant Uint := Expr_Value (Type_Low_Bound (Universal_Integer));
2659 Hi : constant Uint := Expr_Value (Type_High_Bound (Universal_Integer));
2660 -- Allowed range of universal integer (= allowed range of enum lit vals)
2664 -- Minimum and maximum values of entries
2667 -- Pointer to node for literal providing max value
2670 if Ignore_Rep_Clauses then
2674 -- First some basic error checks
2677 Enumtype := Entity (Ident);
2679 if Enumtype = Any_Type
2680 or else Rep_Item_Too_Early (Enumtype, N)
2684 Enumtype := Underlying_Type (Enumtype);
2687 if not Is_Enumeration_Type (Enumtype) then
2689 ("enumeration type required, found}",
2690 Ident, First_Subtype (Enumtype));
2694 -- Ignore rep clause on generic actual type. This will already have
2695 -- been flagged on the template as an error, and this is the safest
2696 -- way to ensure we don't get a junk cascaded message in the instance.
2698 if Is_Generic_Actual_Type (Enumtype) then
2701 -- Type must be in current scope
2703 elsif Scope (Enumtype) /= Current_Scope then
2704 Error_Msg_N ("type must be declared in this scope", Ident);
2707 -- Type must be a first subtype
2709 elsif not Is_First_Subtype (Enumtype) then
2710 Error_Msg_N ("cannot give enumeration rep clause for subtype", N);
2713 -- Ignore duplicate rep clause
2715 elsif Has_Enumeration_Rep_Clause (Enumtype) then
2716 Error_Msg_N ("duplicate enumeration rep clause ignored", N);
2719 -- Don't allow rep clause for standard [wide_[wide_]]character
2721 elsif Is_Standard_Character_Type (Enumtype) then
2722 Error_Msg_N ("enumeration rep clause not allowed for this type", N);
2725 -- Check that the expression is a proper aggregate (no parentheses)
2727 elsif Paren_Count (Aggr) /= 0 then
2729 ("extra parentheses surrounding aggregate not allowed",
2733 -- All tests passed, so set rep clause in place
2736 Set_Has_Enumeration_Rep_Clause (Enumtype);
2737 Set_Has_Enumeration_Rep_Clause (Base_Type (Enumtype));
2740 -- Now we process the aggregate. Note that we don't use the normal
2741 -- aggregate code for this purpose, because we don't want any of the
2742 -- normal expansion activities, and a number of special semantic
2743 -- rules apply (including the component type being any integer type)
2745 Elit := First_Literal (Enumtype);
2747 -- First the positional entries if any
2749 if Present (Expressions (Aggr)) then
2750 Expr := First (Expressions (Aggr));
2751 while Present (Expr) loop
2753 Error_Msg_N ("too many entries in aggregate", Expr);
2757 Val := Static_Integer (Expr);
2759 -- Err signals that we found some incorrect entries processing
2760 -- the list. The final checks for completeness and ordering are
2761 -- skipped in this case.
2763 if Val = No_Uint then
2765 elsif Val < Lo or else Hi < Val then
2766 Error_Msg_N ("value outside permitted range", Expr);
2770 Set_Enumeration_Rep (Elit, Val);
2771 Set_Enumeration_Rep_Expr (Elit, Expr);
2777 -- Now process the named entries if present
2779 if Present (Component_Associations (Aggr)) then
2780 Assoc := First (Component_Associations (Aggr));
2781 while Present (Assoc) loop
2782 Choice := First (Choices (Assoc));
2784 if Present (Next (Choice)) then
2786 ("multiple choice not allowed here", Next (Choice));
2790 if Nkind (Choice) = N_Others_Choice then
2791 Error_Msg_N ("others choice not allowed here", Choice);
2794 elsif Nkind (Choice) = N_Range then
2795 -- ??? should allow zero/one element range here
2796 Error_Msg_N ("range not allowed here", Choice);
2800 Analyze_And_Resolve (Choice, Enumtype);
2802 if Is_Entity_Name (Choice)
2803 and then Is_Type (Entity (Choice))
2805 Error_Msg_N ("subtype name not allowed here", Choice);
2807 -- ??? should allow static subtype with zero/one entry
2809 elsif Etype (Choice) = Base_Type (Enumtype) then
2810 if not Is_Static_Expression (Choice) then
2811 Flag_Non_Static_Expr
2812 ("non-static expression used for choice!", Choice);
2816 Elit := Expr_Value_E (Choice);
2818 if Present (Enumeration_Rep_Expr (Elit)) then
2819 Error_Msg_Sloc := Sloc (Enumeration_Rep_Expr (Elit));
2821 ("representation for& previously given#",
2826 Set_Enumeration_Rep_Expr (Elit, Expression (Assoc));
2828 Expr := Expression (Assoc);
2829 Val := Static_Integer (Expr);
2831 if Val = No_Uint then
2834 elsif Val < Lo or else Hi < Val then
2835 Error_Msg_N ("value outside permitted range", Expr);
2839 Set_Enumeration_Rep (Elit, Val);
2848 -- Aggregate is fully processed. Now we check that a full set of
2849 -- representations was given, and that they are in range and in order.
2850 -- These checks are only done if no other errors occurred.
2856 Elit := First_Literal (Enumtype);
2857 while Present (Elit) loop
2858 if No (Enumeration_Rep_Expr (Elit)) then
2859 Error_Msg_NE ("missing representation for&!", N, Elit);
2862 Val := Enumeration_Rep (Elit);
2864 if Min = No_Uint then
2868 if Val /= No_Uint then
2869 if Max /= No_Uint and then Val <= Max then
2871 ("enumeration value for& not ordered!",
2872 Enumeration_Rep_Expr (Elit), Elit);
2875 Max_Node := Enumeration_Rep_Expr (Elit);
2879 -- If there is at least one literal whose representation is not
2880 -- equal to the Pos value, then note that this enumeration type
2881 -- has a non-standard representation.
2883 if Val /= Enumeration_Pos (Elit) then
2884 Set_Has_Non_Standard_Rep (Base_Type (Enumtype));
2891 -- Now set proper size information
2894 Minsize : Uint := UI_From_Int (Minimum_Size (Enumtype));
2897 if Has_Size_Clause (Enumtype) then
2899 -- All OK, if size is OK now
2901 if RM_Size (Enumtype) >= Minsize then
2905 -- Try if we can get by with biasing
2908 UI_From_Int (Minimum_Size (Enumtype, Biased => True));
2910 -- Error message if even biasing does not work
2912 if RM_Size (Enumtype) < Minsize then
2913 Error_Msg_Uint_1 := RM_Size (Enumtype);
2914 Error_Msg_Uint_2 := Max;
2916 ("previously given size (^) is too small "
2917 & "for this value (^)", Max_Node);
2919 -- If biasing worked, indicate that we now have biased rep
2923 (Enumtype, Size_Clause (Enumtype), "size clause");
2928 Set_RM_Size (Enumtype, Minsize);
2929 Set_Enum_Esize (Enumtype);
2932 Set_RM_Size (Base_Type (Enumtype), RM_Size (Enumtype));
2933 Set_Esize (Base_Type (Enumtype), Esize (Enumtype));
2934 Set_Alignment (Base_Type (Enumtype), Alignment (Enumtype));
2938 -- We repeat the too late test in case it froze itself!
2940 if Rep_Item_Too_Late (Enumtype, N) then
2943 end Analyze_Enumeration_Representation_Clause;
2945 ----------------------------
2946 -- Analyze_Free_Statement --
2947 ----------------------------
2949 procedure Analyze_Free_Statement (N : Node_Id) is
2951 Analyze (Expression (N));
2952 end Analyze_Free_Statement;
2954 ---------------------------
2955 -- Analyze_Freeze_Entity --
2956 ---------------------------
2958 procedure Analyze_Freeze_Entity (N : Node_Id) is
2959 E : constant Entity_Id := Entity (N);
2962 -- Remember that we are processing a freezing entity. Required to
2963 -- ensure correct decoration of internal entities associated with
2964 -- interfaces (see New_Overloaded_Entity).
2966 Inside_Freezing_Actions := Inside_Freezing_Actions + 1;
2968 -- For tagged types covering interfaces add internal entities that link
2969 -- the primitives of the interfaces with the primitives that cover them.
2970 -- Note: These entities were originally generated only when generating
2971 -- code because their main purpose was to provide support to initialize
2972 -- the secondary dispatch tables. They are now generated also when
2973 -- compiling with no code generation to provide ASIS the relationship
2974 -- between interface primitives and tagged type primitives. They are
2975 -- also used to locate primitives covering interfaces when processing
2976 -- generics (see Derive_Subprograms).
2978 if Ada_Version >= Ada_2005
2979 and then Ekind (E) = E_Record_Type
2980 and then Is_Tagged_Type (E)
2981 and then not Is_Interface (E)
2982 and then Has_Interfaces (E)
2984 -- This would be a good common place to call the routine that checks
2985 -- overriding of interface primitives (and thus factorize calls to
2986 -- Check_Abstract_Overriding located at different contexts in the
2987 -- compiler). However, this is not possible because it causes
2988 -- spurious errors in case of late overriding.
2990 Add_Internal_Interface_Entities (E);
2995 if Ekind (E) = E_Record_Type
2996 and then Is_CPP_Class (E)
2997 and then Is_Tagged_Type (E)
2998 and then Tagged_Type_Expansion
2999 and then Expander_Active
3001 if CPP_Num_Prims (E) = 0 then
3003 -- If the CPP type has user defined components then it must import
3004 -- primitives from C++. This is required because if the C++ class
3005 -- has no primitives then the C++ compiler does not added the _tag
3006 -- component to the type.
3008 pragma Assert (Chars (First_Entity (E)) = Name_uTag);
3010 if First_Entity (E) /= Last_Entity (E) then
3012 ("?'C'P'P type must import at least one primitive from C++",
3017 -- Check that all its primitives are abstract or imported from C++.
3018 -- Check also availability of the C++ constructor.
3021 Has_Constructors : constant Boolean := Has_CPP_Constructors (E);
3023 Error_Reported : Boolean := False;
3027 Elmt := First_Elmt (Primitive_Operations (E));
3028 while Present (Elmt) loop
3029 Prim := Node (Elmt);
3031 if Comes_From_Source (Prim) then
3032 if Is_Abstract_Subprogram (Prim) then
3035 elsif not Is_Imported (Prim)
3036 or else Convention (Prim) /= Convention_CPP
3039 ("?primitives of 'C'P'P types must be imported from C++"
3040 & " or abstract", Prim);
3042 elsif not Has_Constructors
3043 and then not Error_Reported
3045 Error_Msg_Name_1 := Chars (E);
3047 ("?'C'P'P constructor required for type %", Prim);
3048 Error_Reported := True;
3057 Inside_Freezing_Actions := Inside_Freezing_Actions - 1;
3059 -- If we have a type with predicates, build predicate function
3061 if Is_Type (E) and then Has_Predicates (E) then
3067 Build_Predicate_Function (E, FDecl, FBody);
3069 if Present (FDecl) then
3070 Insert_After (N, FBody);
3071 Insert_After (N, FDecl);
3075 end Analyze_Freeze_Entity;
3077 ------------------------------------------
3078 -- Analyze_Record_Representation_Clause --
3079 ------------------------------------------
3081 -- Note: we check as much as we can here, but we can't do any checks
3082 -- based on the position values (e.g. overlap checks) until freeze time
3083 -- because especially in Ada 2005 (machine scalar mode), the processing
3084 -- for non-standard bit order can substantially change the positions.
3085 -- See procedure Check_Record_Representation_Clause (called from Freeze)
3086 -- for the remainder of this processing.
3088 procedure Analyze_Record_Representation_Clause (N : Node_Id) is
3089 Ident : constant Node_Id := Identifier (N);
3094 Hbit : Uint := Uint_0;
3098 Rectype : Entity_Id;
3100 CR_Pragma : Node_Id := Empty;
3101 -- Points to N_Pragma node if Complete_Representation pragma present
3104 if Ignore_Rep_Clauses then
3109 Rectype := Entity (Ident);
3111 if Rectype = Any_Type
3112 or else Rep_Item_Too_Early (Rectype, N)
3116 Rectype := Underlying_Type (Rectype);
3119 -- First some basic error checks
3121 if not Is_Record_Type (Rectype) then
3123 ("record type required, found}", Ident, First_Subtype (Rectype));
3126 elsif Scope (Rectype) /= Current_Scope then
3127 Error_Msg_N ("type must be declared in this scope", N);
3130 elsif not Is_First_Subtype (Rectype) then
3131 Error_Msg_N ("cannot give record rep clause for subtype", N);
3134 elsif Has_Record_Rep_Clause (Rectype) then
3135 Error_Msg_N ("duplicate record rep clause ignored", N);
3138 elsif Rep_Item_Too_Late (Rectype, N) then
3142 if Present (Mod_Clause (N)) then
3144 Loc : constant Source_Ptr := Sloc (N);
3145 M : constant Node_Id := Mod_Clause (N);
3146 P : constant List_Id := Pragmas_Before (M);
3150 pragma Warnings (Off, Mod_Val);
3153 Check_Restriction (No_Obsolescent_Features, Mod_Clause (N));
3155 if Warn_On_Obsolescent_Feature then
3157 ("mod clause is an obsolescent feature (RM J.8)?", N);
3159 ("\use alignment attribute definition clause instead?", N);
3166 -- In ASIS_Mode mode, expansion is disabled, but we must convert
3167 -- the Mod clause into an alignment clause anyway, so that the
3168 -- back-end can compute and back-annotate properly the size and
3169 -- alignment of types that may include this record.
3171 -- This seems dubious, this destroys the source tree in a manner
3172 -- not detectable by ASIS ???
3174 if Operating_Mode = Check_Semantics
3178 Make_Attribute_Definition_Clause (Loc,
3179 Name => New_Reference_To (Base_Type (Rectype), Loc),
3180 Chars => Name_Alignment,
3181 Expression => Relocate_Node (Expression (M)));
3183 Set_From_At_Mod (AtM_Nod);
3184 Insert_After (N, AtM_Nod);
3185 Mod_Val := Get_Alignment_Value (Expression (AtM_Nod));
3186 Set_Mod_Clause (N, Empty);
3189 -- Get the alignment value to perform error checking
3191 Mod_Val := Get_Alignment_Value (Expression (M));
3196 -- For untagged types, clear any existing component clauses for the
3197 -- type. If the type is derived, this is what allows us to override
3198 -- a rep clause for the parent. For type extensions, the representation
3199 -- of the inherited components is inherited, so we want to keep previous
3200 -- component clauses for completeness.
3202 if not Is_Tagged_Type (Rectype) then
3203 Comp := First_Component_Or_Discriminant (Rectype);
3204 while Present (Comp) loop
3205 Set_Component_Clause (Comp, Empty);
3206 Next_Component_Or_Discriminant (Comp);
3210 -- All done if no component clauses
3212 CC := First (Component_Clauses (N));
3218 -- A representation like this applies to the base type
3220 Set_Has_Record_Rep_Clause (Base_Type (Rectype));
3221 Set_Has_Non_Standard_Rep (Base_Type (Rectype));
3222 Set_Has_Specified_Layout (Base_Type (Rectype));
3224 -- Process the component clauses
3226 while Present (CC) loop
3230 if Nkind (CC) = N_Pragma then
3233 -- The only pragma of interest is Complete_Representation
3235 if Pragma_Name (CC) = Name_Complete_Representation then
3239 -- Processing for real component clause
3242 Posit := Static_Integer (Position (CC));
3243 Fbit := Static_Integer (First_Bit (CC));
3244 Lbit := Static_Integer (Last_Bit (CC));
3247 and then Fbit /= No_Uint
3248 and then Lbit /= No_Uint
3252 ("position cannot be negative", Position (CC));
3256 ("first bit cannot be negative", First_Bit (CC));
3258 -- The Last_Bit specified in a component clause must not be
3259 -- less than the First_Bit minus one (RM-13.5.1(10)).
3261 elsif Lbit < Fbit - 1 then
3263 ("last bit cannot be less than first bit minus one",
3266 -- Values look OK, so find the corresponding record component
3267 -- Even though the syntax allows an attribute reference for
3268 -- implementation-defined components, GNAT does not allow the
3269 -- tag to get an explicit position.
3271 elsif Nkind (Component_Name (CC)) = N_Attribute_Reference then
3272 if Attribute_Name (Component_Name (CC)) = Name_Tag then
3273 Error_Msg_N ("position of tag cannot be specified", CC);
3275 Error_Msg_N ("illegal component name", CC);
3279 Comp := First_Entity (Rectype);
3280 while Present (Comp) loop
3281 exit when Chars (Comp) = Chars (Component_Name (CC));
3287 -- Maybe component of base type that is absent from
3288 -- statically constrained first subtype.
3290 Comp := First_Entity (Base_Type (Rectype));
3291 while Present (Comp) loop
3292 exit when Chars (Comp) = Chars (Component_Name (CC));
3299 ("component clause is for non-existent field", CC);
3301 -- Ada 2012 (AI05-0026): Any name that denotes a
3302 -- discriminant of an object of an unchecked union type
3303 -- shall not occur within a record_representation_clause.
3305 -- The general restriction of using record rep clauses on
3306 -- Unchecked_Union types has now been lifted. Since it is
3307 -- possible to introduce a record rep clause which mentions
3308 -- the discriminant of an Unchecked_Union in non-Ada 2012
3309 -- code, this check is applied to all versions of the
3312 elsif Ekind (Comp) = E_Discriminant
3313 and then Is_Unchecked_Union (Rectype)
3316 ("cannot reference discriminant of Unchecked_Union",
3317 Component_Name (CC));
3319 elsif Present (Component_Clause (Comp)) then
3321 -- Diagnose duplicate rep clause, or check consistency
3322 -- if this is an inherited component. In a double fault,
3323 -- there may be a duplicate inconsistent clause for an
3324 -- inherited component.
3326 if Scope (Original_Record_Component (Comp)) = Rectype
3327 or else Parent (Component_Clause (Comp)) = N
3329 Error_Msg_Sloc := Sloc (Component_Clause (Comp));
3330 Error_Msg_N ("component clause previously given#", CC);
3334 Rep1 : constant Node_Id := Component_Clause (Comp);
3336 if Intval (Position (Rep1)) /=
3337 Intval (Position (CC))
3338 or else Intval (First_Bit (Rep1)) /=
3339 Intval (First_Bit (CC))
3340 or else Intval (Last_Bit (Rep1)) /=
3341 Intval (Last_Bit (CC))
3343 Error_Msg_N ("component clause inconsistent "
3344 & "with representation of ancestor", CC);
3345 elsif Warn_On_Redundant_Constructs then
3346 Error_Msg_N ("?redundant component clause "
3347 & "for inherited component!", CC);
3352 -- Normal case where this is the first component clause we
3353 -- have seen for this entity, so set it up properly.
3356 -- Make reference for field in record rep clause and set
3357 -- appropriate entity field in the field identifier.
3360 (Comp, Component_Name (CC), Set_Ref => False);
3361 Set_Entity (Component_Name (CC), Comp);
3363 -- Update Fbit and Lbit to the actual bit number
3365 Fbit := Fbit + UI_From_Int (SSU) * Posit;
3366 Lbit := Lbit + UI_From_Int (SSU) * Posit;
3368 if Has_Size_Clause (Rectype)
3369 and then Esize (Rectype) <= Lbit
3372 ("bit number out of range of specified size",
3375 Set_Component_Clause (Comp, CC);
3376 Set_Component_Bit_Offset (Comp, Fbit);
3377 Set_Esize (Comp, 1 + (Lbit - Fbit));
3378 Set_Normalized_First_Bit (Comp, Fbit mod SSU);
3379 Set_Normalized_Position (Comp, Fbit / SSU);
3381 if Warn_On_Overridden_Size
3382 and then Has_Size_Clause (Etype (Comp))
3383 and then RM_Size (Etype (Comp)) /= Esize (Comp)
3386 ("?component size overrides size clause for&",
3387 Component_Name (CC), Etype (Comp));
3390 -- This information is also set in the corresponding
3391 -- component of the base type, found by accessing the
3392 -- Original_Record_Component link if it is present.
3394 Ocomp := Original_Record_Component (Comp);
3401 (Component_Name (CC),
3407 (Comp, First_Node (CC), "component clause", Biased);
3409 if Present (Ocomp) then
3410 Set_Component_Clause (Ocomp, CC);
3411 Set_Component_Bit_Offset (Ocomp, Fbit);
3412 Set_Normalized_First_Bit (Ocomp, Fbit mod SSU);
3413 Set_Normalized_Position (Ocomp, Fbit / SSU);
3414 Set_Esize (Ocomp, 1 + (Lbit - Fbit));
3416 Set_Normalized_Position_Max
3417 (Ocomp, Normalized_Position (Ocomp));
3419 -- Note: we don't use Set_Biased here, because we
3420 -- already gave a warning above if needed, and we
3421 -- would get a duplicate for the same name here.
3423 Set_Has_Biased_Representation
3424 (Ocomp, Has_Biased_Representation (Comp));
3427 if Esize (Comp) < 0 then
3428 Error_Msg_N ("component size is negative", CC);
3439 -- Check missing components if Complete_Representation pragma appeared
3441 if Present (CR_Pragma) then
3442 Comp := First_Component_Or_Discriminant (Rectype);
3443 while Present (Comp) loop
3444 if No (Component_Clause (Comp)) then
3446 ("missing component clause for &", CR_Pragma, Comp);
3449 Next_Component_Or_Discriminant (Comp);
3452 -- If no Complete_Representation pragma, warn if missing components
3454 elsif Warn_On_Unrepped_Components then
3456 Num_Repped_Components : Nat := 0;
3457 Num_Unrepped_Components : Nat := 0;
3460 -- First count number of repped and unrepped components
3462 Comp := First_Component_Or_Discriminant (Rectype);
3463 while Present (Comp) loop
3464 if Present (Component_Clause (Comp)) then
3465 Num_Repped_Components := Num_Repped_Components + 1;
3467 Num_Unrepped_Components := Num_Unrepped_Components + 1;
3470 Next_Component_Or_Discriminant (Comp);
3473 -- We are only interested in the case where there is at least one
3474 -- unrepped component, and at least half the components have rep
3475 -- clauses. We figure that if less than half have them, then the
3476 -- partial rep clause is really intentional. If the component
3477 -- type has no underlying type set at this point (as for a generic
3478 -- formal type), we don't know enough to give a warning on the
3481 if Num_Unrepped_Components > 0
3482 and then Num_Unrepped_Components < Num_Repped_Components
3484 Comp := First_Component_Or_Discriminant (Rectype);
3485 while Present (Comp) loop
3486 if No (Component_Clause (Comp))
3487 and then Comes_From_Source (Comp)
3488 and then Present (Underlying_Type (Etype (Comp)))
3489 and then (Is_Scalar_Type (Underlying_Type (Etype (Comp)))
3490 or else Size_Known_At_Compile_Time
3491 (Underlying_Type (Etype (Comp))))
3492 and then not Has_Warnings_Off (Rectype)
3494 Error_Msg_Sloc := Sloc (Comp);
3496 ("?no component clause given for & declared #",
3500 Next_Component_Or_Discriminant (Comp);
3505 end Analyze_Record_Representation_Clause;
3507 -------------------------------
3508 -- Build_Invariant_Procedure --
3509 -------------------------------
3511 -- The procedure that is constructed here has the form
3513 -- procedure typInvariant (Ixxx : typ) is
3515 -- pragma Check (Invariant, exp, "failed invariant from xxx");
3516 -- pragma Check (Invariant, exp, "failed invariant from xxx");
3518 -- pragma Check (Invariant, exp, "failed inherited invariant from xxx");
3520 -- end typInvariant;
3522 procedure Build_Invariant_Procedure
3524 PDecl : out Node_Id;
3525 PBody : out Node_Id)
3527 Loc : constant Source_Ptr := Sloc (Typ);
3532 procedure Add_Invariants (T : Entity_Id; Inherit : Boolean);
3533 -- Appends statements to Stmts for any invariants in the rep item chain
3534 -- of the given type. If Inherit is False, then we only process entries
3535 -- on the chain for the type Typ. If Inherit is True, then we ignore any
3536 -- Invariant aspects, but we process all Invariant'Class aspects, adding
3537 -- "inherited" to the exception message and generating an informational
3538 -- message about the inheritance of an invariant.
3540 Object_Name : constant Name_Id := New_Internal_Name ('I');
3541 -- Name for argument of invariant procedure
3543 --------------------
3544 -- Add_Invariants --
3545 --------------------
3547 procedure Add_Invariants (T : Entity_Id; Inherit : Boolean) is
3557 function Replace_Node (N : Node_Id) return Traverse_Result;
3558 -- Process single node for traversal to replace type references
3560 procedure Replace_Type is new Traverse_Proc (Replace_Node);
3561 -- Traverse an expression changing every occurrence of an entity
3562 -- reference to type T with a reference to the object argument.
3568 function Replace_Node (N : Node_Id) return Traverse_Result is
3570 -- Case of entity name referencing the type
3572 if Is_Entity_Name (N)
3573 and then Entity (N) = T
3575 -- Invariant'Class, replace with T'Class (obj)
3577 if Class_Present (Ritem) then
3579 Make_Type_Conversion (Loc,
3581 Make_Attribute_Reference (Loc,
3583 New_Occurrence_Of (T, Loc),
3584 Attribute_Name => Name_Class),
3586 Make_Identifier (Loc,
3587 Chars => Object_Name)));
3589 -- Invariant, replace with obj
3593 Make_Identifier (Loc,
3594 Chars => Object_Name));
3597 -- All done with this node
3601 -- Not an instance of the type entity, keep going
3608 -- Start of processing for Add_Invariants
3611 Ritem := First_Rep_Item (T);
3612 while Present (Ritem) loop
3613 if Nkind (Ritem) = N_Pragma
3614 and then Pragma_Name (Ritem) = Name_Invariant
3616 Arg1 := First (Pragma_Argument_Associations (Ritem));
3617 Arg2 := Next (Arg1);
3618 Arg3 := Next (Arg2);
3620 Arg1 := Get_Pragma_Arg (Arg1);
3621 Arg2 := Get_Pragma_Arg (Arg2);
3623 -- For Inherit case, ignore Invariant, process only Class case
3626 if not Class_Present (Ritem) then
3630 -- For Inherit false, process only item for right type
3633 if Entity (Arg1) /= Typ then
3639 Stmts := Empty_List;
3642 Exp := New_Copy_Tree (Arg2);
3645 -- We need to replace any occurrences of the name of the type
3646 -- with references to the object, converted to type'Class in
3647 -- the case of Invariant'Class aspects. We do this by first
3648 -- doing a preanalysis, to identify all the entities, then
3649 -- we traverse looking for the type entity, and doing the
3650 -- necessary substitution. The preanalysis is done with the
3651 -- special OK_To_Reference flag set on the type, so that if
3652 -- we get an occurrence of this type, it will be reognized
3655 Set_OK_To_Reference (T, True);
3656 Preanalyze_Spec_Expression (Exp, Standard_Boolean);
3657 Set_OK_To_Reference (T, False);
3663 -- Build first two arguments for Check pragma
3666 Make_Pragma_Argument_Association (Loc,
3668 Make_Identifier (Loc,
3669 Chars => Name_Invariant)),
3670 Make_Pragma_Argument_Association (Loc,
3671 Expression => Exp));
3673 -- Add message if present in Invariant pragma
3675 if Present (Arg3) then
3676 Str := Strval (Get_Pragma_Arg (Arg3));
3678 -- If inherited case, and message starts "failed invariant",
3679 -- change it to be "failed inherited invariant".
3682 String_To_Name_Buffer (Str);
3684 if Name_Buffer (1 .. 16) = "failed invariant" then
3685 Insert_Str_In_Name_Buffer ("inherited ", 8);
3686 Str := String_From_Name_Buffer;
3691 Make_Pragma_Argument_Association (Loc,
3692 Expression => Make_String_Literal (Loc, Str)));
3695 -- Add Check pragma to list of statements
3699 Pragma_Identifier =>
3700 Make_Identifier (Loc,
3701 Chars => Name_Check),
3702 Pragma_Argument_Associations => Assoc));
3704 -- If Inherited case and option enabled, output info msg. Note
3705 -- that we know this is a case of Invariant'Class.
3707 if Inherit and Opt.List_Inherited_Aspects then
3708 Error_Msg_Sloc := Sloc (Ritem);
3710 ("?info: & inherits `Invariant''Class` aspect from #",
3716 Next_Rep_Item (Ritem);
3720 -- Start of processing for Build_Invariant_Procedure
3727 -- Add invariants for the current type
3729 Add_Invariants (Typ, Inherit => False);
3731 -- Add invariants for parent types
3734 Current_Typ : Entity_Id;
3735 Parent_Typ : Entity_Id;
3740 Parent_Typ := Etype (Current_Typ);
3742 if Is_Private_Type (Parent_Typ)
3743 and then Present (Full_View (Base_Type (Parent_Typ)))
3745 Parent_Typ := Full_View (Base_Type (Parent_Typ));
3748 exit when Parent_Typ = Current_Typ;
3750 Current_Typ := Parent_Typ;
3751 Add_Invariants (Current_Typ, Inherit => True);
3755 -- Build the procedure if we generated at least one Check pragma
3757 if Stmts /= No_List then
3759 -- Build procedure declaration
3761 pragma Assert (Has_Invariants (Typ));
3763 Make_Defining_Identifier (Loc,
3764 Chars => New_External_Name (Chars (Typ), "Invariant"));
3765 Set_Has_Invariants (SId);
3766 Set_Invariant_Procedure (Typ, SId);
3769 Make_Procedure_Specification (Loc,
3770 Defining_Unit_Name => SId,
3771 Parameter_Specifications => New_List (
3772 Make_Parameter_Specification (Loc,
3773 Defining_Identifier =>
3774 Make_Defining_Identifier (Loc,
3775 Chars => Object_Name),
3777 New_Occurrence_Of (Typ, Loc))));
3780 Make_Subprogram_Declaration (Loc,
3781 Specification => Spec);
3783 -- Build procedure body
3786 Make_Defining_Identifier (Loc,
3787 Chars => New_External_Name (Chars (Typ), "Invariant"));
3790 Make_Procedure_Specification (Loc,
3791 Defining_Unit_Name => SId,
3792 Parameter_Specifications => New_List (
3793 Make_Parameter_Specification (Loc,
3794 Defining_Identifier =>
3795 Make_Defining_Identifier (Loc,
3796 Chars => Object_Name),
3798 New_Occurrence_Of (Typ, Loc))));
3801 Make_Subprogram_Body (Loc,
3802 Specification => Spec,
3803 Declarations => Empty_List,
3804 Handled_Statement_Sequence =>
3805 Make_Handled_Sequence_Of_Statements (Loc,
3806 Statements => Stmts));
3808 end Build_Invariant_Procedure;
3810 ------------------------------
3811 -- Build_Predicate_Function --
3812 ------------------------------
3814 -- The procedure that is constructed here has the form
3816 -- function typPredicate (Ixxx : typ) return Boolean is
3819 -- exp1 and then exp2 and then ...
3820 -- and then typ1Predicate (typ1 (Ixxx))
3821 -- and then typ2Predicate (typ2 (Ixxx))
3823 -- end typPredicate;
3825 -- Here exp1, and exp2 are expressions from Predicate pragmas. Note that
3826 -- this is the point at which these expressions get analyzed, providing the
3827 -- required delay, and typ1, typ2, are entities from which predicates are
3828 -- inherited. Note that we do NOT generate Check pragmas, that's because we
3829 -- use this function even if checks are off, e.g. for membership tests.
3831 procedure Build_Predicate_Function
3833 FDecl : out Node_Id;
3834 FBody : out Node_Id)
3836 Loc : constant Source_Ptr := Sloc (Typ);
3841 -- This is the expression for the return statement in the function. It
3842 -- is build by connecting the component predicates with AND THEN.
3844 procedure Add_Call (T : Entity_Id);
3845 -- Includes a call to the predicate function for type T in Expr if T
3846 -- has predicates and Predicate_Function (T) is non-empty.
3848 procedure Add_Predicates;
3849 -- Appends expressions for any Predicate pragmas in the rep item chain
3850 -- Typ to Expr. Note that we look only at items for this exact entity.
3851 -- Inheritance of predicates for the parent type is done by calling the
3852 -- Predicate_Function of the parent type, using Add_Call above.
3854 procedure Build_Static_Predicate;
3855 -- This function is called to process a static predicate, and put it in
3856 -- canonical form and store it in Static_Predicate (Typ).
3858 Object_Name : constant Name_Id := New_Internal_Name ('I');
3859 -- Name for argument of Predicate procedure
3865 procedure Add_Call (T : Entity_Id) is
3869 if Present (T) and then Present (Predicate_Function (T)) then
3870 Set_Has_Predicates (Typ);
3872 -- Build the call to the predicate function of T
3878 Make_Identifier (Loc, Chars => Object_Name)));
3880 -- Add call to evolving expression, using AND THEN if needed
3887 Left_Opnd => Relocate_Node (Expr),
3891 -- Output info message on inheritance if required
3893 if Opt.List_Inherited_Aspects then
3894 Error_Msg_Sloc := Sloc (Predicate_Function (T));
3895 Error_Msg_Node_2 := T;
3896 Error_Msg_N ("?info: & inherits predicate from & #", Typ);
3901 --------------------
3902 -- Add_Predicates --
3903 --------------------
3905 procedure Add_Predicates is
3910 function Replace_Node (N : Node_Id) return Traverse_Result;
3911 -- Process single node for traversal to replace type references
3913 procedure Replace_Type is new Traverse_Proc (Replace_Node);
3914 -- Traverse an expression changing every occurrence of an entity
3915 -- reference to type T with a reference to the object argument.
3921 function Replace_Node (N : Node_Id) return Traverse_Result is
3923 -- Case of entity name referencing the type
3925 if Is_Entity_Name (N) and then Entity (N) = Typ then
3927 -- Replace with object
3930 Make_Identifier (Loc,
3931 Chars => Object_Name));
3933 -- All done with this node
3937 -- Not an occurrence of the type entity, keep going
3944 -- Start of processing for Add_Predicates
3947 Ritem := First_Rep_Item (Typ);
3948 while Present (Ritem) loop
3949 if Nkind (Ritem) = N_Pragma
3950 and then Pragma_Name (Ritem) = Name_Predicate
3952 Arg1 := First (Pragma_Argument_Associations (Ritem));
3953 Arg2 := Next (Arg1);
3955 Arg1 := Get_Pragma_Arg (Arg1);
3956 Arg2 := Get_Pragma_Arg (Arg2);
3958 -- See if this predicate pragma is for the current type
3960 if Entity (Arg1) = Typ then
3962 -- We have a match, this entry is for our subtype
3964 -- First We need to replace any occurrences of the name of
3965 -- the type with references to the object. We do this by
3966 -- first doing a preanalysis, to identify all the entities,
3967 -- then we traverse looking for the type entity, doing the
3968 -- needed substitution. The preanalysis is done with the
3969 -- special OK_To_Reference flag set on the type, so that if
3970 -- we get an occurrence of this type, it will be recognized
3973 Set_OK_To_Reference (Typ, True);
3974 Preanalyze_Spec_Expression (Arg2, Standard_Boolean);
3975 Set_OK_To_Reference (Typ, False);
3976 Replace_Type (Arg2);
3978 -- OK, replacement complete, now we can add the expression
3981 Expr := Relocate_Node (Arg2);
3983 -- There already was a predicate, so add to it
3988 Left_Opnd => Relocate_Node (Expr),
3989 Right_Opnd => Relocate_Node (Arg2));
3994 Next_Rep_Item (Ritem);
3998 ----------------------------
3999 -- Build_Static_Predicate --
4000 ----------------------------
4002 procedure Build_Static_Predicate is
4006 Non_Static : Boolean := False;
4007 -- Set True if something non-static is found
4009 Plist : List_Id := No_List;
4010 -- The entries in Plist are either static expressions which represent
4011 -- a possible value, or ranges of values. Subtype marks don't appear,
4012 -- since we expand them out.
4015 -- Low bound and high bound values of static subtype of Typ
4017 procedure Process_Entry (N : Node_Id);
4018 -- Process one entry (range or value or subtype mark)
4024 procedure Process_Entry (N : Node_Id) is
4026 -- Low and high bounds of range in list
4030 function Build_Val (V : Uint) return Node_Id;
4031 -- Return an analyzed N_Identifier node referencing this value
4033 function Build_Range (Lo, Hi : Uint) return Node_Id;
4034 -- Return an analyzed N_Range node referencing this range
4036 function Lo_Val (N : Node_Id) return Uint;
4037 -- Given static expression or static range, gets expression value
4038 -- or low bound of range.
4040 function Hi_Val (N : Node_Id) return Uint;
4041 -- Given static expression or static range, gets expression value
4042 -- of high bound of range.
4048 function Build_Range (Lo, Hi : Uint) return Node_Id is
4052 return Build_Val (Hi);
4055 Make_Range (Sloc (N),
4056 Low_Bound => Build_Val (Lo),
4057 High_Bound => Build_Val (Hi));
4058 Set_Etype (Result, Typ);
4059 Set_Analyzed (Result);
4068 function Build_Val (V : Uint) return Node_Id is
4072 if Is_Enumeration_Type (Typ) then
4073 Result := Get_Enum_Lit_From_Pos (Typ, V, Sloc (N));
4075 Result := Make_Integer_Literal (Sloc (N), Intval => V);
4078 Set_Etype (Result, Typ);
4079 Set_Is_Static_Expression (Result);
4080 Set_Analyzed (Result);
4088 function Hi_Val (N : Node_Id) return Uint is
4090 if Nkind (N) = N_Identifier then
4091 return Expr_Value (N);
4093 return Expr_Value (High_Bound (N));
4101 function Lo_Val (N : Node_Id) return Uint is
4103 if Nkind (N) = N_Identifier then
4104 return Expr_Value (N);
4106 return Expr_Value (Low_Bound (N));
4110 -- Start of processing for Process_Entry
4115 if Nkind (N) = N_Range then
4116 if not Is_Static_Expression (Low_Bound (N))
4118 not Is_Static_Expression (High_Bound (N))
4129 else pragma Assert (Nkind (N) = N_Identifier);
4131 -- Static expression case
4133 if Is_Static_Expression (N) then
4139 elsif Is_Type (Entity (N)) then
4141 -- If type has static predicates, process them recursively
4143 if Present (Static_Predicate (Entity (N))) then
4144 P := First (Static_Predicate (Entity (N)));
4145 while Present (P) loop
4157 -- For static subtype without predicates, get range
4159 elsif Is_Static_Subtype (Entity (N))
4160 and then not Has_Predicates (Entity (N))
4162 SLo := Expr_Value (Type_Low_Bound (Entity (N)));
4163 SHi := Expr_Value (Type_High_Bound (Entity (N)));
4165 -- Any other type makes us non-static
4172 -- Any other kind of identifier in predicate (e.g. a non-static
4173 -- expression value) means this is not a static predicate.
4181 -- Here with SLo and SHi set for (possibly single element) range
4182 -- of entry to insert in Plist. Non-static if out of range.
4184 if SLo < Lo or else SHi > Hi then
4189 -- If no Plist currently, create it
4192 Plist := New_List (Build_Range (SLo, SHi));
4195 -- Otherwise search Plist for insertion point
4200 -- Case of inserting before current entry
4202 if SHi < Lo_Val (P) - 1 then
4203 Insert_Before (P, Build_Range (SLo, SHi));
4206 -- Case of belongs past current entry
4208 elsif SLo > Hi_Val (P) + 1 then
4212 if No (Next (P)) then
4213 Append_To (Plist, Build_Range (SLo, SHi));
4216 -- Else just move to next item on list
4222 -- Case of extending current entyr, and in overlap cases
4223 -- may also eat up entries past this one.
4227 New_Lo : constant Uint := UI_Min (Lo_Val (P), SLo);
4228 New_Hi : Uint := UI_Max (Hi_Val (P), SHi);
4231 -- See if there are entries past us that we eat up
4233 while Present (Next (P))
4234 and then Lo_Val (Next (P)) <= New_Hi + 1
4236 New_Hi := Hi_Val (Next (P));
4240 -- We now need to replace the current node P with
4241 -- a new entry New_Lo .. New_Hi.
4243 Insert_After (P, Build_Range (New_Lo, New_Hi));
4252 -- Start of processing for Build_Static_Predicate
4255 -- Immediately non-static if our subtype is non static, or we
4256 -- do not have an appropriate discrete subtype in the first place.
4258 if not Ekind_In (Typ, E_Enumeration_Subtype,
4259 E_Modular_Integer_Subtype,
4260 E_Signed_Integer_Subtype)
4261 or else not Is_Static_Subtype (Typ)
4266 Lo := Expr_Value (Type_Low_Bound (Typ));
4267 Hi := Expr_Value (Type_High_Bound (Typ));
4269 -- Check if we have membership predicate
4271 if Nkind (Expr) = N_In then
4274 -- Allow qualified expression with membership predicate inside
4276 elsif Nkind (Expr) = N_Qualified_Expression
4277 and then Nkind (Expression (Expr)) = N_In
4279 Exp := Expression (Expr);
4281 -- Anything else cannot be a static predicate
4287 -- We have a membership operation, so we have a potentially static
4288 -- predicate, collect and canonicalize the entries in the list.
4290 if Present (Right_Opnd (Exp)) then
4291 Process_Entry (Right_Opnd (Exp));
4298 Alt := First (Alternatives (Exp));
4299 while Present (Alt) loop
4300 Process_Entry (Alt);
4310 -- Processing was successful and all entries were static, so
4311 -- now we can store the result as the predicate list.
4313 Set_Static_Predicate (Typ, Plist);
4314 end Build_Static_Predicate;
4316 -- Start of processing for Build_Predicate_Function
4319 -- Initialize for construction of statement list
4325 -- Return if already built or if type does not have predicates
4327 if not Has_Predicates (Typ)
4328 or else Present (Predicate_Function (Typ))
4333 -- Add Predicates for the current type
4337 -- Add predicates for ancestor if present
4340 Atyp : constant Entity_Id := Nearest_Ancestor (Typ);
4342 if Present (Atyp) then
4347 -- If we have predicates, build the function
4349 if Present (Expr) then
4351 -- Deal with static predicate case
4353 Build_Static_Predicate;
4355 -- Build function declaration
4357 pragma Assert (Has_Predicates (Typ));
4359 Make_Defining_Identifier (Loc,
4360 Chars => New_External_Name (Chars (Typ), "Predicate"));
4361 Set_Has_Predicates (SId);
4362 Set_Predicate_Function (Typ, SId);
4365 Make_Function_Specification (Loc,
4366 Defining_Unit_Name => SId,
4367 Parameter_Specifications => New_List (
4368 Make_Parameter_Specification (Loc,
4369 Defining_Identifier =>
4370 Make_Defining_Identifier (Loc, Chars => Object_Name),
4371 Parameter_Type => New_Occurrence_Of (Typ, Loc))),
4372 Result_Definition =>
4373 New_Occurrence_Of (Standard_Boolean, Loc));
4376 Make_Subprogram_Declaration (Loc,
4377 Specification => Spec);
4379 -- Build function body
4382 Make_Defining_Identifier (Loc,
4383 Chars => New_External_Name (Chars (Typ), "Predicate"));
4386 Make_Function_Specification (Loc,
4387 Defining_Unit_Name => SId,
4388 Parameter_Specifications => New_List (
4389 Make_Parameter_Specification (Loc,
4390 Defining_Identifier =>
4391 Make_Defining_Identifier (Loc, Chars => Object_Name),
4393 New_Occurrence_Of (Typ, Loc))),
4394 Result_Definition =>
4395 New_Occurrence_Of (Standard_Boolean, Loc));
4398 Make_Subprogram_Body (Loc,
4399 Specification => Spec,
4400 Declarations => Empty_List,
4401 Handled_Statement_Sequence =>
4402 Make_Handled_Sequence_Of_Statements (Loc,
4403 Statements => New_List (
4404 Make_Simple_Return_Statement (Loc,
4405 Expression => Expr))));
4407 end Build_Predicate_Function;
4409 -----------------------------------
4410 -- Check_Constant_Address_Clause --
4411 -----------------------------------
4413 procedure Check_Constant_Address_Clause
4417 procedure Check_At_Constant_Address (Nod : Node_Id);
4418 -- Checks that the given node N represents a name whose 'Address is
4419 -- constant (in the same sense as OK_Constant_Address_Clause, i.e. the
4420 -- address value is the same at the point of declaration of U_Ent and at
4421 -- the time of elaboration of the address clause.
4423 procedure Check_Expr_Constants (Nod : Node_Id);
4424 -- Checks that Nod meets the requirements for a constant address clause
4425 -- in the sense of the enclosing procedure.
4427 procedure Check_List_Constants (Lst : List_Id);
4428 -- Check that all elements of list Lst meet the requirements for a
4429 -- constant address clause in the sense of the enclosing procedure.
4431 -------------------------------
4432 -- Check_At_Constant_Address --
4433 -------------------------------
4435 procedure Check_At_Constant_Address (Nod : Node_Id) is
4437 if Is_Entity_Name (Nod) then
4438 if Present (Address_Clause (Entity ((Nod)))) then
4440 ("invalid address clause for initialized object &!",
4443 ("address for& cannot" &
4444 " depend on another address clause! (RM 13.1(22))!",
4447 elsif In_Same_Source_Unit (Entity (Nod), U_Ent)
4448 and then Sloc (U_Ent) < Sloc (Entity (Nod))
4451 ("invalid address clause for initialized object &!",
4453 Error_Msg_Node_2 := U_Ent;
4455 ("\& must be defined before & (RM 13.1(22))!",
4459 elsif Nkind (Nod) = N_Selected_Component then
4461 T : constant Entity_Id := Etype (Prefix (Nod));
4464 if (Is_Record_Type (T)
4465 and then Has_Discriminants (T))
4468 and then Is_Record_Type (Designated_Type (T))
4469 and then Has_Discriminants (Designated_Type (T)))
4472 ("invalid address clause for initialized object &!",
4475 ("\address cannot depend on component" &
4476 " of discriminated record (RM 13.1(22))!",
4479 Check_At_Constant_Address (Prefix (Nod));
4483 elsif Nkind (Nod) = N_Indexed_Component then
4484 Check_At_Constant_Address (Prefix (Nod));
4485 Check_List_Constants (Expressions (Nod));
4488 Check_Expr_Constants (Nod);
4490 end Check_At_Constant_Address;
4492 --------------------------
4493 -- Check_Expr_Constants --
4494 --------------------------
4496 procedure Check_Expr_Constants (Nod : Node_Id) is
4497 Loc_U_Ent : constant Source_Ptr := Sloc (U_Ent);
4498 Ent : Entity_Id := Empty;
4501 if Nkind (Nod) in N_Has_Etype
4502 and then Etype (Nod) = Any_Type
4508 when N_Empty | N_Error =>
4511 when N_Identifier | N_Expanded_Name =>
4512 Ent := Entity (Nod);
4514 -- We need to look at the original node if it is different
4515 -- from the node, since we may have rewritten things and
4516 -- substituted an identifier representing the rewrite.
4518 if Original_Node (Nod) /= Nod then
4519 Check_Expr_Constants (Original_Node (Nod));
4521 -- If the node is an object declaration without initial
4522 -- value, some code has been expanded, and the expression
4523 -- is not constant, even if the constituents might be
4524 -- acceptable, as in A'Address + offset.
4526 if Ekind (Ent) = E_Variable
4528 Nkind (Declaration_Node (Ent)) = N_Object_Declaration
4530 No (Expression (Declaration_Node (Ent)))
4533 ("invalid address clause for initialized object &!",
4536 -- If entity is constant, it may be the result of expanding
4537 -- a check. We must verify that its declaration appears
4538 -- before the object in question, else we also reject the
4541 elsif Ekind (Ent) = E_Constant
4542 and then In_Same_Source_Unit (Ent, U_Ent)
4543 and then Sloc (Ent) > Loc_U_Ent
4546 ("invalid address clause for initialized object &!",
4553 -- Otherwise look at the identifier and see if it is OK
4555 if Ekind_In (Ent, E_Named_Integer, E_Named_Real)
4556 or else Is_Type (Ent)
4561 Ekind (Ent) = E_Constant
4563 Ekind (Ent) = E_In_Parameter
4565 -- This is the case where we must have Ent defined before
4566 -- U_Ent. Clearly if they are in different units this
4567 -- requirement is met since the unit containing Ent is
4568 -- already processed.
4570 if not In_Same_Source_Unit (Ent, U_Ent) then
4573 -- Otherwise location of Ent must be before the location
4574 -- of U_Ent, that's what prior defined means.
4576 elsif Sloc (Ent) < Loc_U_Ent then
4581 ("invalid address clause for initialized object &!",
4583 Error_Msg_Node_2 := U_Ent;
4585 ("\& must be defined before & (RM 13.1(22))!",
4589 elsif Nkind (Original_Node (Nod)) = N_Function_Call then
4590 Check_Expr_Constants (Original_Node (Nod));
4594 ("invalid address clause for initialized object &!",
4597 if Comes_From_Source (Ent) then
4599 ("\reference to variable& not allowed"
4600 & " (RM 13.1(22))!", Nod, Ent);
4603 ("non-static expression not allowed"
4604 & " (RM 13.1(22))!", Nod);
4608 when N_Integer_Literal =>
4610 -- If this is a rewritten unchecked conversion, in a system
4611 -- where Address is an integer type, always use the base type
4612 -- for a literal value. This is user-friendly and prevents
4613 -- order-of-elaboration issues with instances of unchecked
4616 if Nkind (Original_Node (Nod)) = N_Function_Call then
4617 Set_Etype (Nod, Base_Type (Etype (Nod)));
4620 when N_Real_Literal |
4622 N_Character_Literal =>
4626 Check_Expr_Constants (Low_Bound (Nod));
4627 Check_Expr_Constants (High_Bound (Nod));
4629 when N_Explicit_Dereference =>
4630 Check_Expr_Constants (Prefix (Nod));
4632 when N_Indexed_Component =>
4633 Check_Expr_Constants (Prefix (Nod));
4634 Check_List_Constants (Expressions (Nod));
4637 Check_Expr_Constants (Prefix (Nod));
4638 Check_Expr_Constants (Discrete_Range (Nod));
4640 when N_Selected_Component =>
4641 Check_Expr_Constants (Prefix (Nod));
4643 when N_Attribute_Reference =>
4644 if Attribute_Name (Nod) = Name_Address
4646 Attribute_Name (Nod) = Name_Access
4648 Attribute_Name (Nod) = Name_Unchecked_Access
4650 Attribute_Name (Nod) = Name_Unrestricted_Access
4652 Check_At_Constant_Address (Prefix (Nod));
4655 Check_Expr_Constants (Prefix (Nod));
4656 Check_List_Constants (Expressions (Nod));
4660 Check_List_Constants (Component_Associations (Nod));
4661 Check_List_Constants (Expressions (Nod));
4663 when N_Component_Association =>
4664 Check_Expr_Constants (Expression (Nod));
4666 when N_Extension_Aggregate =>
4667 Check_Expr_Constants (Ancestor_Part (Nod));
4668 Check_List_Constants (Component_Associations (Nod));
4669 Check_List_Constants (Expressions (Nod));
4674 when N_Binary_Op | N_Short_Circuit | N_Membership_Test =>
4675 Check_Expr_Constants (Left_Opnd (Nod));
4676 Check_Expr_Constants (Right_Opnd (Nod));
4679 Check_Expr_Constants (Right_Opnd (Nod));
4681 when N_Type_Conversion |
4682 N_Qualified_Expression |
4684 Check_Expr_Constants (Expression (Nod));
4686 when N_Unchecked_Type_Conversion =>
4687 Check_Expr_Constants (Expression (Nod));
4689 -- If this is a rewritten unchecked conversion, subtypes in
4690 -- this node are those created within the instance. To avoid
4691 -- order of elaboration issues, replace them with their base
4692 -- types. Note that address clauses can cause order of
4693 -- elaboration problems because they are elaborated by the
4694 -- back-end at the point of definition, and may mention
4695 -- entities declared in between (as long as everything is
4696 -- static). It is user-friendly to allow unchecked conversions
4699 if Nkind (Original_Node (Nod)) = N_Function_Call then
4700 Set_Etype (Expression (Nod),
4701 Base_Type (Etype (Expression (Nod))));
4702 Set_Etype (Nod, Base_Type (Etype (Nod)));
4705 when N_Function_Call =>
4706 if not Is_Pure (Entity (Name (Nod))) then
4708 ("invalid address clause for initialized object &!",
4712 ("\function & is not pure (RM 13.1(22))!",
4713 Nod, Entity (Name (Nod)));
4716 Check_List_Constants (Parameter_Associations (Nod));
4719 when N_Parameter_Association =>
4720 Check_Expr_Constants (Explicit_Actual_Parameter (Nod));
4724 ("invalid address clause for initialized object &!",
4727 ("\must be constant defined before& (RM 13.1(22))!",
4730 end Check_Expr_Constants;
4732 --------------------------
4733 -- Check_List_Constants --
4734 --------------------------
4736 procedure Check_List_Constants (Lst : List_Id) is
4740 if Present (Lst) then
4741 Nod1 := First (Lst);
4742 while Present (Nod1) loop
4743 Check_Expr_Constants (Nod1);
4747 end Check_List_Constants;
4749 -- Start of processing for Check_Constant_Address_Clause
4752 -- If rep_clauses are to be ignored, no need for legality checks. In
4753 -- particular, no need to pester user about rep clauses that violate
4754 -- the rule on constant addresses, given that these clauses will be
4755 -- removed by Freeze before they reach the back end.
4757 if not Ignore_Rep_Clauses then
4758 Check_Expr_Constants (Expr);
4760 end Check_Constant_Address_Clause;
4762 ----------------------------------------
4763 -- Check_Record_Representation_Clause --
4764 ----------------------------------------
4766 procedure Check_Record_Representation_Clause (N : Node_Id) is
4767 Loc : constant Source_Ptr := Sloc (N);
4768 Ident : constant Node_Id := Identifier (N);
4769 Rectype : Entity_Id;
4774 Hbit : Uint := Uint_0;
4778 Max_Bit_So_Far : Uint;
4779 -- Records the maximum bit position so far. If all field positions
4780 -- are monotonically increasing, then we can skip the circuit for
4781 -- checking for overlap, since no overlap is possible.
4783 Tagged_Parent : Entity_Id := Empty;
4784 -- This is set in the case of a derived tagged type for which we have
4785 -- Is_Fully_Repped_Tagged_Type True (indicating that all components are
4786 -- positioned by record representation clauses). In this case we must
4787 -- check for overlap between components of this tagged type, and the
4788 -- components of its parent. Tagged_Parent will point to this parent
4789 -- type. For all other cases Tagged_Parent is left set to Empty.
4791 Parent_Last_Bit : Uint;
4792 -- Relevant only if Tagged_Parent is set, Parent_Last_Bit indicates the
4793 -- last bit position for any field in the parent type. We only need to
4794 -- check overlap for fields starting below this point.
4796 Overlap_Check_Required : Boolean;
4797 -- Used to keep track of whether or not an overlap check is required
4799 Overlap_Detected : Boolean := False;
4800 -- Set True if an overlap is detected
4802 Ccount : Natural := 0;
4803 -- Number of component clauses in record rep clause
4805 procedure Check_Component_Overlap (C1_Ent, C2_Ent : Entity_Id);
4806 -- Given two entities for record components or discriminants, checks
4807 -- if they have overlapping component clauses and issues errors if so.
4809 procedure Find_Component;
4810 -- Finds component entity corresponding to current component clause (in
4811 -- CC), and sets Comp to the entity, and Fbit/Lbit to the zero origin
4812 -- start/stop bits for the field. If there is no matching component or
4813 -- if the matching component does not have a component clause, then
4814 -- that's an error and Comp is set to Empty, but no error message is
4815 -- issued, since the message was already given. Comp is also set to
4816 -- Empty if the current "component clause" is in fact a pragma.
4818 -----------------------------
4819 -- Check_Component_Overlap --
4820 -----------------------------
4822 procedure Check_Component_Overlap (C1_Ent, C2_Ent : Entity_Id) is
4823 CC1 : constant Node_Id := Component_Clause (C1_Ent);
4824 CC2 : constant Node_Id := Component_Clause (C2_Ent);
4827 if Present (CC1) and then Present (CC2) then
4829 -- Exclude odd case where we have two tag fields in the same
4830 -- record, both at location zero. This seems a bit strange, but
4831 -- it seems to happen in some circumstances, perhaps on an error.
4833 if Chars (C1_Ent) = Name_uTag
4835 Chars (C2_Ent) = Name_uTag
4840 -- Here we check if the two fields overlap
4843 S1 : constant Uint := Component_Bit_Offset (C1_Ent);
4844 S2 : constant Uint := Component_Bit_Offset (C2_Ent);
4845 E1 : constant Uint := S1 + Esize (C1_Ent);
4846 E2 : constant Uint := S2 + Esize (C2_Ent);
4849 if E2 <= S1 or else E1 <= S2 then
4852 Error_Msg_Node_2 := Component_Name (CC2);
4853 Error_Msg_Sloc := Sloc (Error_Msg_Node_2);
4854 Error_Msg_Node_1 := Component_Name (CC1);
4856 ("component& overlaps & #", Component_Name (CC1));
4857 Overlap_Detected := True;
4861 end Check_Component_Overlap;
4863 --------------------
4864 -- Find_Component --
4865 --------------------
4867 procedure Find_Component is
4869 procedure Search_Component (R : Entity_Id);
4870 -- Search components of R for a match. If found, Comp is set.
4872 ----------------------
4873 -- Search_Component --
4874 ----------------------
4876 procedure Search_Component (R : Entity_Id) is
4878 Comp := First_Component_Or_Discriminant (R);
4879 while Present (Comp) loop
4881 -- Ignore error of attribute name for component name (we
4882 -- already gave an error message for this, so no need to
4885 if Nkind (Component_Name (CC)) = N_Attribute_Reference then
4888 exit when Chars (Comp) = Chars (Component_Name (CC));
4891 Next_Component_Or_Discriminant (Comp);
4893 end Search_Component;
4895 -- Start of processing for Find_Component
4898 -- Return with Comp set to Empty if we have a pragma
4900 if Nkind (CC) = N_Pragma then
4905 -- Search current record for matching component
4907 Search_Component (Rectype);
4909 -- If not found, maybe component of base type that is absent from
4910 -- statically constrained first subtype.
4913 Search_Component (Base_Type (Rectype));
4916 -- If no component, or the component does not reference the component
4917 -- clause in question, then there was some previous error for which
4918 -- we already gave a message, so just return with Comp Empty.
4921 or else Component_Clause (Comp) /= CC
4925 -- Normal case where we have a component clause
4928 Fbit := Component_Bit_Offset (Comp);
4929 Lbit := Fbit + Esize (Comp) - 1;
4933 -- Start of processing for Check_Record_Representation_Clause
4937 Rectype := Entity (Ident);
4939 if Rectype = Any_Type then
4942 Rectype := Underlying_Type (Rectype);
4945 -- See if we have a fully repped derived tagged type
4948 PS : constant Entity_Id := Parent_Subtype (Rectype);
4951 if Present (PS) and then Is_Fully_Repped_Tagged_Type (PS) then
4952 Tagged_Parent := PS;
4954 -- Find maximum bit of any component of the parent type
4956 Parent_Last_Bit := UI_From_Int (System_Address_Size - 1);
4957 Pcomp := First_Entity (Tagged_Parent);
4958 while Present (Pcomp) loop
4959 if Ekind_In (Pcomp, E_Discriminant, E_Component) then
4960 if Component_Bit_Offset (Pcomp) /= No_Uint
4961 and then Known_Static_Esize (Pcomp)
4966 Component_Bit_Offset (Pcomp) + Esize (Pcomp) - 1);
4969 Next_Entity (Pcomp);
4975 -- All done if no component clauses
4977 CC := First (Component_Clauses (N));
4983 -- If a tag is present, then create a component clause that places it
4984 -- at the start of the record (otherwise gigi may place it after other
4985 -- fields that have rep clauses).
4987 Fent := First_Entity (Rectype);
4989 if Nkind (Fent) = N_Defining_Identifier
4990 and then Chars (Fent) = Name_uTag
4992 Set_Component_Bit_Offset (Fent, Uint_0);
4993 Set_Normalized_Position (Fent, Uint_0);
4994 Set_Normalized_First_Bit (Fent, Uint_0);
4995 Set_Normalized_Position_Max (Fent, Uint_0);
4996 Init_Esize (Fent, System_Address_Size);
4998 Set_Component_Clause (Fent,
4999 Make_Component_Clause (Loc,
5001 Make_Identifier (Loc,
5002 Chars => Name_uTag),
5005 Make_Integer_Literal (Loc,
5009 Make_Integer_Literal (Loc,
5013 Make_Integer_Literal (Loc,
5014 UI_From_Int (System_Address_Size))));
5016 Ccount := Ccount + 1;
5019 Max_Bit_So_Far := Uint_Minus_1;
5020 Overlap_Check_Required := False;
5022 -- Process the component clauses
5024 while Present (CC) loop
5027 if Present (Comp) then
5028 Ccount := Ccount + 1;
5030 -- We need a full overlap check if record positions non-monotonic
5032 if Fbit <= Max_Bit_So_Far then
5033 Overlap_Check_Required := True;
5036 Max_Bit_So_Far := Lbit;
5038 -- Check bit position out of range of specified size
5040 if Has_Size_Clause (Rectype)
5041 and then Esize (Rectype) <= Lbit
5044 ("bit number out of range of specified size",
5047 -- Check for overlap with tag field
5050 if Is_Tagged_Type (Rectype)
5051 and then Fbit < System_Address_Size
5054 ("component overlaps tag field of&",
5055 Component_Name (CC), Rectype);
5056 Overlap_Detected := True;
5064 -- Check parent overlap if component might overlap parent field
5066 if Present (Tagged_Parent)
5067 and then Fbit <= Parent_Last_Bit
5069 Pcomp := First_Component_Or_Discriminant (Tagged_Parent);
5070 while Present (Pcomp) loop
5071 if not Is_Tag (Pcomp)
5072 and then Chars (Pcomp) /= Name_uParent
5074 Check_Component_Overlap (Comp, Pcomp);
5077 Next_Component_Or_Discriminant (Pcomp);
5085 -- Now that we have processed all the component clauses, check for
5086 -- overlap. We have to leave this till last, since the components can
5087 -- appear in any arbitrary order in the representation clause.
5089 -- We do not need this check if all specified ranges were monotonic,
5090 -- as recorded by Overlap_Check_Required being False at this stage.
5092 -- This first section checks if there are any overlapping entries at
5093 -- all. It does this by sorting all entries and then seeing if there are
5094 -- any overlaps. If there are none, then that is decisive, but if there
5095 -- are overlaps, they may still be OK (they may result from fields in
5096 -- different variants).
5098 if Overlap_Check_Required then
5099 Overlap_Check1 : declare
5101 OC_Fbit : array (0 .. Ccount) of Uint;
5102 -- First-bit values for component clauses, the value is the offset
5103 -- of the first bit of the field from start of record. The zero
5104 -- entry is for use in sorting.
5106 OC_Lbit : array (0 .. Ccount) of Uint;
5107 -- Last-bit values for component clauses, the value is the offset
5108 -- of the last bit of the field from start of record. The zero
5109 -- entry is for use in sorting.
5111 OC_Count : Natural := 0;
5112 -- Count of entries in OC_Fbit and OC_Lbit
5114 function OC_Lt (Op1, Op2 : Natural) return Boolean;
5115 -- Compare routine for Sort
5117 procedure OC_Move (From : Natural; To : Natural);
5118 -- Move routine for Sort
5120 package Sorting is new GNAT.Heap_Sort_G (OC_Move, OC_Lt);
5126 function OC_Lt (Op1, Op2 : Natural) return Boolean is
5128 return OC_Fbit (Op1) < OC_Fbit (Op2);
5135 procedure OC_Move (From : Natural; To : Natural) is
5137 OC_Fbit (To) := OC_Fbit (From);
5138 OC_Lbit (To) := OC_Lbit (From);
5141 -- Start of processing for Overlap_Check
5144 CC := First (Component_Clauses (N));
5145 while Present (CC) loop
5147 -- Exclude component clause already marked in error
5149 if not Error_Posted (CC) then
5152 if Present (Comp) then
5153 OC_Count := OC_Count + 1;
5154 OC_Fbit (OC_Count) := Fbit;
5155 OC_Lbit (OC_Count) := Lbit;
5162 Sorting.Sort (OC_Count);
5164 Overlap_Check_Required := False;
5165 for J in 1 .. OC_Count - 1 loop
5166 if OC_Lbit (J) >= OC_Fbit (J + 1) then
5167 Overlap_Check_Required := True;
5174 -- If Overlap_Check_Required is still True, then we have to do the full
5175 -- scale overlap check, since we have at least two fields that do
5176 -- overlap, and we need to know if that is OK since they are in
5177 -- different variant, or whether we have a definite problem.
5179 if Overlap_Check_Required then
5180 Overlap_Check2 : declare
5181 C1_Ent, C2_Ent : Entity_Id;
5182 -- Entities of components being checked for overlap
5185 -- Component_List node whose Component_Items are being checked
5188 -- Component declaration for component being checked
5191 C1_Ent := First_Entity (Base_Type (Rectype));
5193 -- Loop through all components in record. For each component check
5194 -- for overlap with any of the preceding elements on the component
5195 -- list containing the component and also, if the component is in
5196 -- a variant, check against components outside the case structure.
5197 -- This latter test is repeated recursively up the variant tree.
5199 Main_Component_Loop : while Present (C1_Ent) loop
5200 if not Ekind_In (C1_Ent, E_Component, E_Discriminant) then
5201 goto Continue_Main_Component_Loop;
5204 -- Skip overlap check if entity has no declaration node. This
5205 -- happens with discriminants in constrained derived types.
5206 -- Possibly we are missing some checks as a result, but that
5207 -- does not seem terribly serious.
5209 if No (Declaration_Node (C1_Ent)) then
5210 goto Continue_Main_Component_Loop;
5213 Clist := Parent (List_Containing (Declaration_Node (C1_Ent)));
5215 -- Loop through component lists that need checking. Check the
5216 -- current component list and all lists in variants above us.
5218 Component_List_Loop : loop
5220 -- If derived type definition, go to full declaration
5221 -- If at outer level, check discriminants if there are any.
5223 if Nkind (Clist) = N_Derived_Type_Definition then
5224 Clist := Parent (Clist);
5227 -- Outer level of record definition, check discriminants
5229 if Nkind_In (Clist, N_Full_Type_Declaration,
5230 N_Private_Type_Declaration)
5232 if Has_Discriminants (Defining_Identifier (Clist)) then
5234 First_Discriminant (Defining_Identifier (Clist));
5235 while Present (C2_Ent) loop
5236 exit when C1_Ent = C2_Ent;
5237 Check_Component_Overlap (C1_Ent, C2_Ent);
5238 Next_Discriminant (C2_Ent);
5242 -- Record extension case
5244 elsif Nkind (Clist) = N_Derived_Type_Definition then
5247 -- Otherwise check one component list
5250 Citem := First (Component_Items (Clist));
5251 while Present (Citem) loop
5252 if Nkind (Citem) = N_Component_Declaration then
5253 C2_Ent := Defining_Identifier (Citem);
5254 exit when C1_Ent = C2_Ent;
5255 Check_Component_Overlap (C1_Ent, C2_Ent);
5262 -- Check for variants above us (the parent of the Clist can
5263 -- be a variant, in which case its parent is a variant part,
5264 -- and the parent of the variant part is a component list
5265 -- whose components must all be checked against the current
5266 -- component for overlap).
5268 if Nkind (Parent (Clist)) = N_Variant then
5269 Clist := Parent (Parent (Parent (Clist)));
5271 -- Check for possible discriminant part in record, this
5272 -- is treated essentially as another level in the
5273 -- recursion. For this case the parent of the component
5274 -- list is the record definition, and its parent is the
5275 -- full type declaration containing the discriminant
5278 elsif Nkind (Parent (Clist)) = N_Record_Definition then
5279 Clist := Parent (Parent ((Clist)));
5281 -- If neither of these two cases, we are at the top of
5285 exit Component_List_Loop;
5287 end loop Component_List_Loop;
5289 <<Continue_Main_Component_Loop>>
5290 Next_Entity (C1_Ent);
5292 end loop Main_Component_Loop;
5296 -- The following circuit deals with warning on record holes (gaps). We
5297 -- skip this check if overlap was detected, since it makes sense for the
5298 -- programmer to fix this illegality before worrying about warnings.
5300 if not Overlap_Detected and Warn_On_Record_Holes then
5301 Record_Hole_Check : declare
5302 Decl : constant Node_Id := Declaration_Node (Base_Type (Rectype));
5303 -- Full declaration of record type
5305 procedure Check_Component_List
5309 -- Check component list CL for holes. The starting bit should be
5310 -- Sbit. which is zero for the main record component list and set
5311 -- appropriately for recursive calls for variants. DS is set to
5312 -- a list of discriminant specifications to be included in the
5313 -- consideration of components. It is No_List if none to consider.
5315 --------------------------
5316 -- Check_Component_List --
5317 --------------------------
5319 procedure Check_Component_List
5327 Compl := Integer (List_Length (Component_Items (CL)));
5329 if DS /= No_List then
5330 Compl := Compl + Integer (List_Length (DS));
5334 Comps : array (Natural range 0 .. Compl) of Entity_Id;
5335 -- Gather components (zero entry is for sort routine)
5337 Ncomps : Natural := 0;
5338 -- Number of entries stored in Comps (starting at Comps (1))
5341 -- One component item or discriminant specification
5344 -- Starting bit for next component
5352 function Lt (Op1, Op2 : Natural) return Boolean;
5353 -- Compare routine for Sort
5355 procedure Move (From : Natural; To : Natural);
5356 -- Move routine for Sort
5358 package Sorting is new GNAT.Heap_Sort_G (Move, Lt);
5364 function Lt (Op1, Op2 : Natural) return Boolean is
5366 return Component_Bit_Offset (Comps (Op1))
5368 Component_Bit_Offset (Comps (Op2));
5375 procedure Move (From : Natural; To : Natural) is
5377 Comps (To) := Comps (From);
5381 -- Gather discriminants into Comp
5383 if DS /= No_List then
5384 Citem := First (DS);
5385 while Present (Citem) loop
5386 if Nkind (Citem) = N_Discriminant_Specification then
5388 Ent : constant Entity_Id :=
5389 Defining_Identifier (Citem);
5391 if Ekind (Ent) = E_Discriminant then
5392 Ncomps := Ncomps + 1;
5393 Comps (Ncomps) := Ent;
5402 -- Gather component entities into Comp
5404 Citem := First (Component_Items (CL));
5405 while Present (Citem) loop
5406 if Nkind (Citem) = N_Component_Declaration then
5407 Ncomps := Ncomps + 1;
5408 Comps (Ncomps) := Defining_Identifier (Citem);
5414 -- Now sort the component entities based on the first bit.
5415 -- Note we already know there are no overlapping components.
5417 Sorting.Sort (Ncomps);
5419 -- Loop through entries checking for holes
5422 for J in 1 .. Ncomps loop
5424 Error_Msg_Uint_1 := Component_Bit_Offset (CEnt) - Nbit;
5426 if Error_Msg_Uint_1 > 0 then
5428 ("?^-bit gap before component&",
5429 Component_Name (Component_Clause (CEnt)), CEnt);
5432 Nbit := Component_Bit_Offset (CEnt) + Esize (CEnt);
5435 -- Process variant parts recursively if present
5437 if Present (Variant_Part (CL)) then
5438 Variant := First (Variants (Variant_Part (CL)));
5439 while Present (Variant) loop
5440 Check_Component_List
5441 (Component_List (Variant), Nbit, No_List);
5446 end Check_Component_List;
5448 -- Start of processing for Record_Hole_Check
5455 if Is_Tagged_Type (Rectype) then
5456 Sbit := UI_From_Int (System_Address_Size);
5461 if Nkind (Decl) = N_Full_Type_Declaration
5462 and then Nkind (Type_Definition (Decl)) = N_Record_Definition
5464 Check_Component_List
5465 (Component_List (Type_Definition (Decl)),
5467 Discriminant_Specifications (Decl));
5470 end Record_Hole_Check;
5473 -- For records that have component clauses for all components, and whose
5474 -- size is less than or equal to 32, we need to know the size in the
5475 -- front end to activate possible packed array processing where the
5476 -- component type is a record.
5478 -- At this stage Hbit + 1 represents the first unused bit from all the
5479 -- component clauses processed, so if the component clauses are
5480 -- complete, then this is the length of the record.
5482 -- For records longer than System.Storage_Unit, and for those where not
5483 -- all components have component clauses, the back end determines the
5484 -- length (it may for example be appropriate to round up the size
5485 -- to some convenient boundary, based on alignment considerations, etc).
5487 if Unknown_RM_Size (Rectype) and then Hbit + 1 <= 32 then
5489 -- Nothing to do if at least one component has no component clause
5491 Comp := First_Component_Or_Discriminant (Rectype);
5492 while Present (Comp) loop
5493 exit when No (Component_Clause (Comp));
5494 Next_Component_Or_Discriminant (Comp);
5497 -- If we fall out of loop, all components have component clauses
5498 -- and so we can set the size to the maximum value.
5501 Set_RM_Size (Rectype, Hbit + 1);
5504 end Check_Record_Representation_Clause;
5510 procedure Check_Size
5514 Biased : out Boolean)
5516 UT : constant Entity_Id := Underlying_Type (T);
5522 -- Dismiss cases for generic types or types with previous errors
5525 or else UT = Any_Type
5526 or else Is_Generic_Type (UT)
5527 or else Is_Generic_Type (Root_Type (UT))
5531 -- Check case of bit packed array
5533 elsif Is_Array_Type (UT)
5534 and then Known_Static_Component_Size (UT)
5535 and then Is_Bit_Packed_Array (UT)
5543 Asiz := Component_Size (UT);
5544 Indx := First_Index (UT);
5546 Ityp := Etype (Indx);
5548 -- If non-static bound, then we are not in the business of
5549 -- trying to check the length, and indeed an error will be
5550 -- issued elsewhere, since sizes of non-static array types
5551 -- cannot be set implicitly or explicitly.
5553 if not Is_Static_Subtype (Ityp) then
5557 -- Otherwise accumulate next dimension
5559 Asiz := Asiz * (Expr_Value (Type_High_Bound (Ityp)) -
5560 Expr_Value (Type_Low_Bound (Ityp)) +
5564 exit when No (Indx);
5570 Error_Msg_Uint_1 := Asiz;
5572 ("size for& too small, minimum allowed is ^", N, T);
5573 Set_Esize (T, Asiz);
5574 Set_RM_Size (T, Asiz);
5578 -- All other composite types are ignored
5580 elsif Is_Composite_Type (UT) then
5583 -- For fixed-point types, don't check minimum if type is not frozen,
5584 -- since we don't know all the characteristics of the type that can
5585 -- affect the size (e.g. a specified small) till freeze time.
5587 elsif Is_Fixed_Point_Type (UT)
5588 and then not Is_Frozen (UT)
5592 -- Cases for which a minimum check is required
5595 -- Ignore if specified size is correct for the type
5597 if Known_Esize (UT) and then Siz = Esize (UT) then
5601 -- Otherwise get minimum size
5603 M := UI_From_Int (Minimum_Size (UT));
5607 -- Size is less than minimum size, but one possibility remains
5608 -- that we can manage with the new size if we bias the type.
5610 M := UI_From_Int (Minimum_Size (UT, Biased => True));
5613 Error_Msg_Uint_1 := M;
5615 ("size for& too small, minimum allowed is ^", N, T);
5625 -------------------------
5626 -- Get_Alignment_Value --
5627 -------------------------
5629 function Get_Alignment_Value (Expr : Node_Id) return Uint is
5630 Align : constant Uint := Static_Integer (Expr);
5633 if Align = No_Uint then
5636 elsif Align <= 0 then
5637 Error_Msg_N ("alignment value must be positive", Expr);
5641 for J in Int range 0 .. 64 loop
5643 M : constant Uint := Uint_2 ** J;
5646 exit when M = Align;
5650 ("alignment value must be power of 2", Expr);
5658 end Get_Alignment_Value;
5664 procedure Initialize is
5666 Address_Clause_Checks.Init;
5667 Independence_Checks.Init;
5668 Unchecked_Conversions.Init;
5671 -------------------------
5672 -- Is_Operational_Item --
5673 -------------------------
5675 function Is_Operational_Item (N : Node_Id) return Boolean is
5677 if Nkind (N) /= N_Attribute_Definition_Clause then
5681 Id : constant Attribute_Id := Get_Attribute_Id (Chars (N));
5683 return Id = Attribute_Input
5684 or else Id = Attribute_Output
5685 or else Id = Attribute_Read
5686 or else Id = Attribute_Write
5687 or else Id = Attribute_External_Tag;
5690 end Is_Operational_Item;
5696 function Minimum_Size
5698 Biased : Boolean := False) return Nat
5700 Lo : Uint := No_Uint;
5701 Hi : Uint := No_Uint;
5702 LoR : Ureal := No_Ureal;
5703 HiR : Ureal := No_Ureal;
5704 LoSet : Boolean := False;
5705 HiSet : Boolean := False;
5709 R_Typ : constant Entity_Id := Root_Type (T);
5712 -- If bad type, return 0
5714 if T = Any_Type then
5717 -- For generic types, just return zero. There cannot be any legitimate
5718 -- need to know such a size, but this routine may be called with a
5719 -- generic type as part of normal processing.
5721 elsif Is_Generic_Type (R_Typ)
5722 or else R_Typ = Any_Type
5726 -- Access types. Normally an access type cannot have a size smaller
5727 -- than the size of System.Address. The exception is on VMS, where
5728 -- we have short and long addresses, and it is possible for an access
5729 -- type to have a short address size (and thus be less than the size
5730 -- of System.Address itself). We simply skip the check for VMS, and
5731 -- leave it to the back end to do the check.
5733 elsif Is_Access_Type (T) then
5734 if OpenVMS_On_Target then
5737 return System_Address_Size;
5740 -- Floating-point types
5742 elsif Is_Floating_Point_Type (T) then
5743 return UI_To_Int (Esize (R_Typ));
5747 elsif Is_Discrete_Type (T) then
5749 -- The following loop is looking for the nearest compile time known
5750 -- bounds following the ancestor subtype chain. The idea is to find
5751 -- the most restrictive known bounds information.
5755 if Ancest = Any_Type or else Etype (Ancest) = Any_Type then
5760 if Compile_Time_Known_Value (Type_Low_Bound (Ancest)) then
5761 Lo := Expr_Rep_Value (Type_Low_Bound (Ancest));
5768 if Compile_Time_Known_Value (Type_High_Bound (Ancest)) then
5769 Hi := Expr_Rep_Value (Type_High_Bound (Ancest));
5775 Ancest := Ancestor_Subtype (Ancest);
5778 Ancest := Base_Type (T);
5780 if Is_Generic_Type (Ancest) then
5786 -- Fixed-point types. We can't simply use Expr_Value to get the
5787 -- Corresponding_Integer_Value values of the bounds, since these do not
5788 -- get set till the type is frozen, and this routine can be called
5789 -- before the type is frozen. Similarly the test for bounds being static
5790 -- needs to include the case where we have unanalyzed real literals for
5793 elsif Is_Fixed_Point_Type (T) then
5795 -- The following loop is looking for the nearest compile time known
5796 -- bounds following the ancestor subtype chain. The idea is to find
5797 -- the most restrictive known bounds information.
5801 if Ancest = Any_Type or else Etype (Ancest) = Any_Type then
5805 -- Note: In the following two tests for LoSet and HiSet, it may
5806 -- seem redundant to test for N_Real_Literal here since normally
5807 -- one would assume that the test for the value being known at
5808 -- compile time includes this case. However, there is a glitch.
5809 -- If the real literal comes from folding a non-static expression,
5810 -- then we don't consider any non- static expression to be known
5811 -- at compile time if we are in configurable run time mode (needed
5812 -- in some cases to give a clearer definition of what is and what
5813 -- is not accepted). So the test is indeed needed. Without it, we
5814 -- would set neither Lo_Set nor Hi_Set and get an infinite loop.
5817 if Nkind (Type_Low_Bound (Ancest)) = N_Real_Literal
5818 or else Compile_Time_Known_Value (Type_Low_Bound (Ancest))
5820 LoR := Expr_Value_R (Type_Low_Bound (Ancest));
5827 if Nkind (Type_High_Bound (Ancest)) = N_Real_Literal
5828 or else Compile_Time_Known_Value (Type_High_Bound (Ancest))
5830 HiR := Expr_Value_R (Type_High_Bound (Ancest));
5836 Ancest := Ancestor_Subtype (Ancest);
5839 Ancest := Base_Type (T);
5841 if Is_Generic_Type (Ancest) then
5847 Lo := UR_To_Uint (LoR / Small_Value (T));
5848 Hi := UR_To_Uint (HiR / Small_Value (T));
5850 -- No other types allowed
5853 raise Program_Error;
5856 -- Fall through with Hi and Lo set. Deal with biased case
5859 and then not Is_Fixed_Point_Type (T)
5860 and then not (Is_Enumeration_Type (T)
5861 and then Has_Non_Standard_Rep (T)))
5862 or else Has_Biased_Representation (T)
5868 -- Signed case. Note that we consider types like range 1 .. -1 to be
5869 -- signed for the purpose of computing the size, since the bounds have
5870 -- to be accommodated in the base type.
5872 if Lo < 0 or else Hi < 0 then
5876 -- S = size, B = 2 ** (size - 1) (can accommodate -B .. +(B - 1))
5877 -- Note that we accommodate the case where the bounds cross. This
5878 -- can happen either because of the way the bounds are declared
5879 -- or because of the algorithm in Freeze_Fixed_Point_Type.
5893 -- If both bounds are positive, make sure that both are represen-
5894 -- table in the case where the bounds are crossed. This can happen
5895 -- either because of the way the bounds are declared, or because of
5896 -- the algorithm in Freeze_Fixed_Point_Type.
5902 -- S = size, (can accommodate 0 .. (2**size - 1))
5905 while Hi >= Uint_2 ** S loop
5913 ---------------------------
5914 -- New_Stream_Subprogram --
5915 ---------------------------
5917 procedure New_Stream_Subprogram
5921 Nam : TSS_Name_Type)
5923 Loc : constant Source_Ptr := Sloc (N);
5924 Sname : constant Name_Id := Make_TSS_Name (Base_Type (Ent), Nam);
5925 Subp_Id : Entity_Id;
5926 Subp_Decl : Node_Id;
5930 Defer_Declaration : constant Boolean :=
5931 Is_Tagged_Type (Ent) or else Is_Private_Type (Ent);
5932 -- For a tagged type, there is a declaration for each stream attribute
5933 -- at the freeze point, and we must generate only a completion of this
5934 -- declaration. We do the same for private types, because the full view
5935 -- might be tagged. Otherwise we generate a declaration at the point of
5936 -- the attribute definition clause.
5938 function Build_Spec return Node_Id;
5939 -- Used for declaration and renaming declaration, so that this is
5940 -- treated as a renaming_as_body.
5946 function Build_Spec return Node_Id is
5947 Out_P : constant Boolean := (Nam = TSS_Stream_Read);
5950 T_Ref : constant Node_Id := New_Reference_To (Etyp, Loc);
5953 Subp_Id := Make_Defining_Identifier (Loc, Sname);
5955 -- S : access Root_Stream_Type'Class
5957 Formals := New_List (
5958 Make_Parameter_Specification (Loc,
5959 Defining_Identifier =>
5960 Make_Defining_Identifier (Loc, Name_S),
5962 Make_Access_Definition (Loc,
5965 Designated_Type (Etype (F)), Loc))));
5967 if Nam = TSS_Stream_Input then
5968 Spec := Make_Function_Specification (Loc,
5969 Defining_Unit_Name => Subp_Id,
5970 Parameter_Specifications => Formals,
5971 Result_Definition => T_Ref);
5976 Make_Parameter_Specification (Loc,
5977 Defining_Identifier => Make_Defining_Identifier (Loc, Name_V),
5978 Out_Present => Out_P,
5979 Parameter_Type => T_Ref));
5982 Make_Procedure_Specification (Loc,
5983 Defining_Unit_Name => Subp_Id,
5984 Parameter_Specifications => Formals);
5990 -- Start of processing for New_Stream_Subprogram
5993 F := First_Formal (Subp);
5995 if Ekind (Subp) = E_Procedure then
5996 Etyp := Etype (Next_Formal (F));
5998 Etyp := Etype (Subp);
6001 -- Prepare subprogram declaration and insert it as an action on the
6002 -- clause node. The visibility for this entity is used to test for
6003 -- visibility of the attribute definition clause (in the sense of
6004 -- 8.3(23) as amended by AI-195).
6006 if not Defer_Declaration then
6008 Make_Subprogram_Declaration (Loc,
6009 Specification => Build_Spec);
6011 -- For a tagged type, there is always a visible declaration for each
6012 -- stream TSS (it is a predefined primitive operation), and the
6013 -- completion of this declaration occurs at the freeze point, which is
6014 -- not always visible at places where the attribute definition clause is
6015 -- visible. So, we create a dummy entity here for the purpose of
6016 -- tracking the visibility of the attribute definition clause itself.
6020 Make_Defining_Identifier (Loc,
6021 Chars => New_External_Name (Sname, 'V'));
6023 Make_Object_Declaration (Loc,
6024 Defining_Identifier => Subp_Id,
6025 Object_Definition => New_Occurrence_Of (Standard_Boolean, Loc));
6028 Insert_Action (N, Subp_Decl);
6029 Set_Entity (N, Subp_Id);
6032 Make_Subprogram_Renaming_Declaration (Loc,
6033 Specification => Build_Spec,
6034 Name => New_Reference_To (Subp, Loc));
6036 if Defer_Declaration then
6037 Set_TSS (Base_Type (Ent), Subp_Id);
6039 Insert_Action (N, Subp_Decl);
6040 Copy_TSS (Subp_Id, Base_Type (Ent));
6042 end New_Stream_Subprogram;
6044 ------------------------
6045 -- Rep_Item_Too_Early --
6046 ------------------------
6048 function Rep_Item_Too_Early (T : Entity_Id; N : Node_Id) return Boolean is
6050 -- Cannot apply non-operational rep items to generic types
6052 if Is_Operational_Item (N) then
6056 and then Is_Generic_Type (Root_Type (T))
6058 Error_Msg_N ("representation item not allowed for generic type", N);
6062 -- Otherwise check for incomplete type
6064 if Is_Incomplete_Or_Private_Type (T)
6065 and then No (Underlying_Type (T))
6068 ("representation item must be after full type declaration", N);
6071 -- If the type has incomplete components, a representation clause is
6072 -- illegal but stream attributes and Convention pragmas are correct.
6074 elsif Has_Private_Component (T) then
6075 if Nkind (N) = N_Pragma then
6079 ("representation item must appear after type is fully defined",
6086 end Rep_Item_Too_Early;
6088 -----------------------
6089 -- Rep_Item_Too_Late --
6090 -----------------------
6092 function Rep_Item_Too_Late
6095 FOnly : Boolean := False) return Boolean
6098 Parent_Type : Entity_Id;
6101 -- Output the too late message. Note that this is not considered a
6102 -- serious error, since the effect is simply that we ignore the
6103 -- representation clause in this case.
6109 procedure Too_Late is
6111 Error_Msg_N ("|representation item appears too late!", N);
6114 -- Start of processing for Rep_Item_Too_Late
6117 -- First make sure entity is not frozen (RM 13.1(9)). Exclude imported
6118 -- types, which may be frozen if they appear in a representation clause
6119 -- for a local type.
6122 and then not From_With_Type (T)
6125 S := First_Subtype (T);
6127 if Present (Freeze_Node (S)) then
6129 ("?no more representation items for }", Freeze_Node (S), S);
6134 -- Check for case of non-tagged derived type whose parent either has
6135 -- primitive operations, or is a by reference type (RM 13.1(10)).
6139 and then Is_Derived_Type (T)
6140 and then not Is_Tagged_Type (T)
6142 Parent_Type := Etype (Base_Type (T));
6144 if Has_Primitive_Operations (Parent_Type) then
6147 ("primitive operations already defined for&!", N, Parent_Type);
6150 elsif Is_By_Reference_Type (Parent_Type) then
6153 ("parent type & is a by reference type!", N, Parent_Type);
6158 -- No error, link item into head of chain of rep items for the entity,
6159 -- but avoid chaining if we have an overloadable entity, and the pragma
6160 -- is one that can apply to multiple overloaded entities.
6162 if Is_Overloadable (T)
6163 and then Nkind (N) = N_Pragma
6166 Pname : constant Name_Id := Pragma_Name (N);
6168 if Pname = Name_Convention or else
6169 Pname = Name_Import or else
6170 Pname = Name_Export or else
6171 Pname = Name_External or else
6172 Pname = Name_Interface
6179 Record_Rep_Item (T, N);
6181 end Rep_Item_Too_Late;
6183 -------------------------
6184 -- Same_Representation --
6185 -------------------------
6187 function Same_Representation (Typ1, Typ2 : Entity_Id) return Boolean is
6188 T1 : constant Entity_Id := Underlying_Type (Typ1);
6189 T2 : constant Entity_Id := Underlying_Type (Typ2);
6192 -- A quick check, if base types are the same, then we definitely have
6193 -- the same representation, because the subtype specific representation
6194 -- attributes (Size and Alignment) do not affect representation from
6195 -- the point of view of this test.
6197 if Base_Type (T1) = Base_Type (T2) then
6200 elsif Is_Private_Type (Base_Type (T2))
6201 and then Base_Type (T1) = Full_View (Base_Type (T2))
6206 -- Tagged types never have differing representations
6208 if Is_Tagged_Type (T1) then
6212 -- Representations are definitely different if conventions differ
6214 if Convention (T1) /= Convention (T2) then
6218 -- Representations are different if component alignments differ
6220 if (Is_Record_Type (T1) or else Is_Array_Type (T1))
6222 (Is_Record_Type (T2) or else Is_Array_Type (T2))
6223 and then Component_Alignment (T1) /= Component_Alignment (T2)
6228 -- For arrays, the only real issue is component size. If we know the
6229 -- component size for both arrays, and it is the same, then that's
6230 -- good enough to know we don't have a change of representation.
6232 if Is_Array_Type (T1) then
6233 if Known_Component_Size (T1)
6234 and then Known_Component_Size (T2)
6235 and then Component_Size (T1) = Component_Size (T2)
6241 -- Types definitely have same representation if neither has non-standard
6242 -- representation since default representations are always consistent.
6243 -- If only one has non-standard representation, and the other does not,
6244 -- then we consider that they do not have the same representation. They
6245 -- might, but there is no way of telling early enough.
6247 if Has_Non_Standard_Rep (T1) then
6248 if not Has_Non_Standard_Rep (T2) then
6252 return not Has_Non_Standard_Rep (T2);
6255 -- Here the two types both have non-standard representation, and we need
6256 -- to determine if they have the same non-standard representation.
6258 -- For arrays, we simply need to test if the component sizes are the
6259 -- same. Pragma Pack is reflected in modified component sizes, so this
6260 -- check also deals with pragma Pack.
6262 if Is_Array_Type (T1) then
6263 return Component_Size (T1) = Component_Size (T2);
6265 -- Tagged types always have the same representation, because it is not
6266 -- possible to specify different representations for common fields.
6268 elsif Is_Tagged_Type (T1) then
6271 -- Case of record types
6273 elsif Is_Record_Type (T1) then
6275 -- Packed status must conform
6277 if Is_Packed (T1) /= Is_Packed (T2) then
6280 -- Otherwise we must check components. Typ2 maybe a constrained
6281 -- subtype with fewer components, so we compare the components
6282 -- of the base types.
6285 Record_Case : declare
6286 CD1, CD2 : Entity_Id;
6288 function Same_Rep return Boolean;
6289 -- CD1 and CD2 are either components or discriminants. This
6290 -- function tests whether the two have the same representation
6296 function Same_Rep return Boolean is
6298 if No (Component_Clause (CD1)) then
6299 return No (Component_Clause (CD2));
6303 Present (Component_Clause (CD2))
6305 Component_Bit_Offset (CD1) = Component_Bit_Offset (CD2)
6307 Esize (CD1) = Esize (CD2);
6311 -- Start of processing for Record_Case
6314 if Has_Discriminants (T1) then
6315 CD1 := First_Discriminant (T1);
6316 CD2 := First_Discriminant (T2);
6318 -- The number of discriminants may be different if the
6319 -- derived type has fewer (constrained by values). The
6320 -- invisible discriminants retain the representation of
6321 -- the original, so the discrepancy does not per se
6322 -- indicate a different representation.
6325 and then Present (CD2)
6327 if not Same_Rep then
6330 Next_Discriminant (CD1);
6331 Next_Discriminant (CD2);
6336 CD1 := First_Component (Underlying_Type (Base_Type (T1)));
6337 CD2 := First_Component (Underlying_Type (Base_Type (T2)));
6339 while Present (CD1) loop
6340 if not Same_Rep then
6343 Next_Component (CD1);
6344 Next_Component (CD2);
6352 -- For enumeration types, we must check each literal to see if the
6353 -- representation is the same. Note that we do not permit enumeration
6354 -- representation clauses for Character and Wide_Character, so these
6355 -- cases were already dealt with.
6357 elsif Is_Enumeration_Type (T1) then
6358 Enumeration_Case : declare
6362 L1 := First_Literal (T1);
6363 L2 := First_Literal (T2);
6365 while Present (L1) loop
6366 if Enumeration_Rep (L1) /= Enumeration_Rep (L2) then
6376 end Enumeration_Case;
6378 -- Any other types have the same representation for these purposes
6383 end Same_Representation;
6389 procedure Set_Biased
6393 Biased : Boolean := True)
6397 Set_Has_Biased_Representation (E);
6399 if Warn_On_Biased_Representation then
6401 ("?" & Msg & " forces biased representation for&", N, E);
6406 --------------------
6407 -- Set_Enum_Esize --
6408 --------------------
6410 procedure Set_Enum_Esize (T : Entity_Id) is
6418 -- Find the minimum standard size (8,16,32,64) that fits
6420 Lo := Enumeration_Rep (Entity (Type_Low_Bound (T)));
6421 Hi := Enumeration_Rep (Entity (Type_High_Bound (T)));
6424 if Lo >= -Uint_2**07 and then Hi < Uint_2**07 then
6425 Sz := Standard_Character_Size; -- May be > 8 on some targets
6427 elsif Lo >= -Uint_2**15 and then Hi < Uint_2**15 then
6430 elsif Lo >= -Uint_2**31 and then Hi < Uint_2**31 then
6433 else pragma Assert (Lo >= -Uint_2**63 and then Hi < Uint_2**63);
6438 if Hi < Uint_2**08 then
6439 Sz := Standard_Character_Size; -- May be > 8 on some targets
6441 elsif Hi < Uint_2**16 then
6444 elsif Hi < Uint_2**32 then
6447 else pragma Assert (Hi < Uint_2**63);
6452 -- That minimum is the proper size unless we have a foreign convention
6453 -- and the size required is 32 or less, in which case we bump the size
6454 -- up to 32. This is required for C and C++ and seems reasonable for
6455 -- all other foreign conventions.
6457 if Has_Foreign_Convention (T)
6458 and then Esize (T) < Standard_Integer_Size
6460 Init_Esize (T, Standard_Integer_Size);
6466 ------------------------------
6467 -- Validate_Address_Clauses --
6468 ------------------------------
6470 procedure Validate_Address_Clauses is
6472 for J in Address_Clause_Checks.First .. Address_Clause_Checks.Last loop
6474 ACCR : Address_Clause_Check_Record
6475 renames Address_Clause_Checks.Table (J);
6486 -- Skip processing of this entry if warning already posted
6488 if not Address_Warning_Posted (ACCR.N) then
6490 Expr := Original_Node (Expression (ACCR.N));
6494 X_Alignment := Alignment (ACCR.X);
6495 Y_Alignment := Alignment (ACCR.Y);
6497 -- Similarly obtain sizes
6499 X_Size := Esize (ACCR.X);
6500 Y_Size := Esize (ACCR.Y);
6502 -- Check for large object overlaying smaller one
6505 and then X_Size > Uint_0
6506 and then X_Size > Y_Size
6509 ("?& overlays smaller object", ACCR.N, ACCR.X);
6511 ("\?program execution may be erroneous", ACCR.N);
6512 Error_Msg_Uint_1 := X_Size;
6514 ("\?size of & is ^", ACCR.N, ACCR.X);
6515 Error_Msg_Uint_1 := Y_Size;
6517 ("\?size of & is ^", ACCR.N, ACCR.Y);
6519 -- Check for inadequate alignment, both of the base object
6520 -- and of the offset, if any.
6522 -- Note: we do not check the alignment if we gave a size
6523 -- warning, since it would likely be redundant.
6525 elsif Y_Alignment /= Uint_0
6526 and then (Y_Alignment < X_Alignment
6529 Nkind (Expr) = N_Attribute_Reference
6531 Attribute_Name (Expr) = Name_Address
6533 Has_Compatible_Alignment
6534 (ACCR.X, Prefix (Expr))
6535 /= Known_Compatible))
6538 ("?specified address for& may be inconsistent "
6542 ("\?program execution may be erroneous (RM 13.3(27))",
6544 Error_Msg_Uint_1 := X_Alignment;
6546 ("\?alignment of & is ^",
6548 Error_Msg_Uint_1 := Y_Alignment;
6550 ("\?alignment of & is ^",
6552 if Y_Alignment >= X_Alignment then
6554 ("\?but offset is not multiple of alignment",
6561 end Validate_Address_Clauses;
6563 ---------------------------
6564 -- Validate_Independence --
6565 ---------------------------
6567 procedure Validate_Independence is
6568 SU : constant Uint := UI_From_Int (System_Storage_Unit);
6576 procedure Check_Array_Type (Atyp : Entity_Id);
6577 -- Checks if the array type Atyp has independent components, and
6578 -- if not, outputs an appropriate set of error messages.
6580 procedure No_Independence;
6581 -- Output message that independence cannot be guaranteed
6583 function OK_Component (C : Entity_Id) return Boolean;
6584 -- Checks one component to see if it is independently accessible, and
6585 -- if so yields True, otherwise yields False if independent access
6586 -- cannot be guaranteed. This is a conservative routine, it only
6587 -- returns True if it knows for sure, it returns False if it knows
6588 -- there is a problem, or it cannot be sure there is no problem.
6590 procedure Reason_Bad_Component (C : Entity_Id);
6591 -- Outputs continuation message if a reason can be determined for
6592 -- the component C being bad.
6594 ----------------------
6595 -- Check_Array_Type --
6596 ----------------------
6598 procedure Check_Array_Type (Atyp : Entity_Id) is
6599 Ctyp : constant Entity_Id := Component_Type (Atyp);
6602 -- OK if no alignment clause, no pack, and no component size
6604 if not Has_Component_Size_Clause (Atyp)
6605 and then not Has_Alignment_Clause (Atyp)
6606 and then not Is_Packed (Atyp)
6611 -- Check actual component size
6613 if not Known_Component_Size (Atyp)
6614 or else not (Addressable (Component_Size (Atyp))
6615 and then Component_Size (Atyp) < 64)
6616 or else Component_Size (Atyp) mod Esize (Ctyp) /= 0
6620 -- Bad component size, check reason
6622 if Has_Component_Size_Clause (Atyp) then
6624 Get_Attribute_Definition_Clause
6625 (Atyp, Attribute_Component_Size);
6628 Error_Msg_Sloc := Sloc (P);
6629 Error_Msg_N ("\because of Component_Size clause#", N);
6634 if Is_Packed (Atyp) then
6635 P := Get_Rep_Pragma (Atyp, Name_Pack);
6638 Error_Msg_Sloc := Sloc (P);
6639 Error_Msg_N ("\because of pragma Pack#", N);
6644 -- No reason found, just return
6649 -- Array type is OK independence-wise
6652 end Check_Array_Type;
6654 ---------------------
6655 -- No_Independence --
6656 ---------------------
6658 procedure No_Independence is
6660 if Pragma_Name (N) = Name_Independent then
6662 ("independence cannot be guaranteed for&", N, E);
6665 ("independent components cannot be guaranteed for&", N, E);
6667 end No_Independence;
6673 function OK_Component (C : Entity_Id) return Boolean is
6674 Rec : constant Entity_Id := Scope (C);
6675 Ctyp : constant Entity_Id := Etype (C);
6678 -- OK if no component clause, no Pack, and no alignment clause
6680 if No (Component_Clause (C))
6681 and then not Is_Packed (Rec)
6682 and then not Has_Alignment_Clause (Rec)
6687 -- Here we look at the actual component layout. A component is
6688 -- addressable if its size is a multiple of the Esize of the
6689 -- component type, and its starting position in the record has
6690 -- appropriate alignment, and the record itself has appropriate
6691 -- alignment to guarantee the component alignment.
6693 -- Make sure sizes are static, always assume the worst for any
6694 -- cases where we cannot check static values.
6696 if not (Known_Static_Esize (C)
6697 and then Known_Static_Esize (Ctyp))
6702 -- Size of component must be addressable or greater than 64 bits
6703 -- and a multiple of bytes.
6705 if not Addressable (Esize (C))
6706 and then Esize (C) < Uint_64
6711 -- Check size is proper multiple
6713 if Esize (C) mod Esize (Ctyp) /= 0 then
6717 -- Check alignment of component is OK
6719 if not Known_Component_Bit_Offset (C)
6720 or else Component_Bit_Offset (C) < Uint_0
6721 or else Component_Bit_Offset (C) mod Esize (Ctyp) /= 0
6726 -- Check alignment of record type is OK
6728 if not Known_Alignment (Rec)
6729 or else (Alignment (Rec) * SU) mod Esize (Ctyp) /= 0
6734 -- All tests passed, component is addressable
6739 --------------------------
6740 -- Reason_Bad_Component --
6741 --------------------------
6743 procedure Reason_Bad_Component (C : Entity_Id) is
6744 Rec : constant Entity_Id := Scope (C);
6745 Ctyp : constant Entity_Id := Etype (C);
6748 -- If component clause present assume that's the problem
6750 if Present (Component_Clause (C)) then
6751 Error_Msg_Sloc := Sloc (Component_Clause (C));
6752 Error_Msg_N ("\because of Component_Clause#", N);
6756 -- If pragma Pack clause present, assume that's the problem
6758 if Is_Packed (Rec) then
6759 P := Get_Rep_Pragma (Rec, Name_Pack);
6762 Error_Msg_Sloc := Sloc (P);
6763 Error_Msg_N ("\because of pragma Pack#", N);
6768 -- See if record has bad alignment clause
6770 if Has_Alignment_Clause (Rec)
6771 and then Known_Alignment (Rec)
6772 and then (Alignment (Rec) * SU) mod Esize (Ctyp) /= 0
6774 P := Get_Attribute_Definition_Clause (Rec, Attribute_Alignment);
6777 Error_Msg_Sloc := Sloc (P);
6778 Error_Msg_N ("\because of Alignment clause#", N);
6782 -- Couldn't find a reason, so return without a message
6785 end Reason_Bad_Component;
6787 -- Start of processing for Validate_Independence
6790 for J in Independence_Checks.First .. Independence_Checks.Last loop
6791 N := Independence_Checks.Table (J).N;
6792 E := Independence_Checks.Table (J).E;
6793 IC := Pragma_Name (N) = Name_Independent_Components;
6795 -- Deal with component case
6797 if Ekind (E) = E_Discriminant or else Ekind (E) = E_Component then
6798 if not OK_Component (E) then
6800 Reason_Bad_Component (E);
6805 -- Deal with record with Independent_Components
6807 if IC and then Is_Record_Type (E) then
6808 Comp := First_Component_Or_Discriminant (E);
6809 while Present (Comp) loop
6810 if not OK_Component (Comp) then
6812 Reason_Bad_Component (Comp);
6816 Next_Component_Or_Discriminant (Comp);
6820 -- Deal with address clause case
6822 if Is_Object (E) then
6823 Addr := Address_Clause (E);
6825 if Present (Addr) then
6827 Error_Msg_Sloc := Sloc (Addr);
6828 Error_Msg_N ("\because of Address clause#", N);
6833 -- Deal with independent components for array type
6835 if IC and then Is_Array_Type (E) then
6836 Check_Array_Type (E);
6839 -- Deal with independent components for array object
6841 if IC and then Is_Object (E) and then Is_Array_Type (Etype (E)) then
6842 Check_Array_Type (Etype (E));
6847 end Validate_Independence;
6849 -----------------------------------
6850 -- Validate_Unchecked_Conversion --
6851 -----------------------------------
6853 procedure Validate_Unchecked_Conversion
6855 Act_Unit : Entity_Id)
6862 -- Obtain source and target types. Note that we call Ancestor_Subtype
6863 -- here because the processing for generic instantiation always makes
6864 -- subtypes, and we want the original frozen actual types.
6866 -- If we are dealing with private types, then do the check on their
6867 -- fully declared counterparts if the full declarations have been
6868 -- encountered (they don't have to be visible, but they must exist!)
6870 Source := Ancestor_Subtype (Etype (First_Formal (Act_Unit)));
6872 if Is_Private_Type (Source)
6873 and then Present (Underlying_Type (Source))
6875 Source := Underlying_Type (Source);
6878 Target := Ancestor_Subtype (Etype (Act_Unit));
6880 -- If either type is generic, the instantiation happens within a generic
6881 -- unit, and there is nothing to check. The proper check
6882 -- will happen when the enclosing generic is instantiated.
6884 if Is_Generic_Type (Source) or else Is_Generic_Type (Target) then
6888 if Is_Private_Type (Target)
6889 and then Present (Underlying_Type (Target))
6891 Target := Underlying_Type (Target);
6894 -- Source may be unconstrained array, but not target
6896 if Is_Array_Type (Target)
6897 and then not Is_Constrained (Target)
6900 ("unchecked conversion to unconstrained array not allowed", N);
6904 -- Warn if conversion between two different convention pointers
6906 if Is_Access_Type (Target)
6907 and then Is_Access_Type (Source)
6908 and then Convention (Target) /= Convention (Source)
6909 and then Warn_On_Unchecked_Conversion
6911 -- Give warnings for subprogram pointers only on most targets. The
6912 -- exception is VMS, where data pointers can have different lengths
6913 -- depending on the pointer convention.
6915 if Is_Access_Subprogram_Type (Target)
6916 or else Is_Access_Subprogram_Type (Source)
6917 or else OpenVMS_On_Target
6920 ("?conversion between pointers with different conventions!", N);
6924 -- Warn if one of the operands is Ada.Calendar.Time. Do not emit a
6925 -- warning when compiling GNAT-related sources.
6927 if Warn_On_Unchecked_Conversion
6928 and then not In_Predefined_Unit (N)
6929 and then RTU_Loaded (Ada_Calendar)
6931 (Chars (Source) = Name_Time
6933 Chars (Target) = Name_Time)
6935 -- If Ada.Calendar is loaded and the name of one of the operands is
6936 -- Time, there is a good chance that this is Ada.Calendar.Time.
6939 Calendar_Time : constant Entity_Id :=
6940 Full_View (RTE (RO_CA_Time));
6942 pragma Assert (Present (Calendar_Time));
6944 if Source = Calendar_Time
6945 or else Target = Calendar_Time
6948 ("?representation of 'Time values may change between " &
6949 "'G'N'A'T versions", N);
6954 -- Make entry in unchecked conversion table for later processing by
6955 -- Validate_Unchecked_Conversions, which will check sizes and alignments
6956 -- (using values set by the back-end where possible). This is only done
6957 -- if the appropriate warning is active.
6959 if Warn_On_Unchecked_Conversion then
6960 Unchecked_Conversions.Append
6961 (New_Val => UC_Entry'
6966 -- If both sizes are known statically now, then back end annotation
6967 -- is not required to do a proper check but if either size is not
6968 -- known statically, then we need the annotation.
6970 if Known_Static_RM_Size (Source)
6971 and then Known_Static_RM_Size (Target)
6975 Back_Annotate_Rep_Info := True;
6979 -- If unchecked conversion to access type, and access type is declared
6980 -- in the same unit as the unchecked conversion, then set the
6981 -- No_Strict_Aliasing flag (no strict aliasing is implicit in this
6984 if Is_Access_Type (Target) and then
6985 In_Same_Source_Unit (Target, N)
6987 Set_No_Strict_Aliasing (Implementation_Base_Type (Target));
6990 -- Generate N_Validate_Unchecked_Conversion node for back end in
6991 -- case the back end needs to perform special validation checks.
6993 -- Shouldn't this be in Exp_Ch13, since the check only gets done
6994 -- if we have full expansion and the back end is called ???
6997 Make_Validate_Unchecked_Conversion (Sloc (N));
6998 Set_Source_Type (Vnode, Source);
6999 Set_Target_Type (Vnode, Target);
7001 -- If the unchecked conversion node is in a list, just insert before it.
7002 -- If not we have some strange case, not worth bothering about.
7004 if Is_List_Member (N) then
7005 Insert_After (N, Vnode);
7007 end Validate_Unchecked_Conversion;
7009 ------------------------------------
7010 -- Validate_Unchecked_Conversions --
7011 ------------------------------------
7013 procedure Validate_Unchecked_Conversions is
7015 for N in Unchecked_Conversions.First .. Unchecked_Conversions.Last loop
7017 T : UC_Entry renames Unchecked_Conversions.Table (N);
7019 Eloc : constant Source_Ptr := T.Eloc;
7020 Source : constant Entity_Id := T.Source;
7021 Target : constant Entity_Id := T.Target;
7027 -- This validation check, which warns if we have unequal sizes for
7028 -- unchecked conversion, and thus potentially implementation
7029 -- dependent semantics, is one of the few occasions on which we
7030 -- use the official RM size instead of Esize. See description in
7031 -- Einfo "Handling of Type'Size Values" for details.
7033 if Serious_Errors_Detected = 0
7034 and then Known_Static_RM_Size (Source)
7035 and then Known_Static_RM_Size (Target)
7037 -- Don't do the check if warnings off for either type, note the
7038 -- deliberate use of OR here instead of OR ELSE to get the flag
7039 -- Warnings_Off_Used set for both types if appropriate.
7041 and then not (Has_Warnings_Off (Source)
7043 Has_Warnings_Off (Target))
7045 Source_Siz := RM_Size (Source);
7046 Target_Siz := RM_Size (Target);
7048 if Source_Siz /= Target_Siz then
7050 ("?types for unchecked conversion have different sizes!",
7053 if All_Errors_Mode then
7054 Error_Msg_Name_1 := Chars (Source);
7055 Error_Msg_Uint_1 := Source_Siz;
7056 Error_Msg_Name_2 := Chars (Target);
7057 Error_Msg_Uint_2 := Target_Siz;
7058 Error_Msg ("\size of % is ^, size of % is ^?", Eloc);
7060 Error_Msg_Uint_1 := UI_Abs (Source_Siz - Target_Siz);
7062 if Is_Discrete_Type (Source)
7063 and then Is_Discrete_Type (Target)
7065 if Source_Siz > Target_Siz then
7067 ("\?^ high order bits of source will be ignored!",
7070 elsif Is_Unsigned_Type (Source) then
7072 ("\?source will be extended with ^ high order " &
7073 "zero bits?!", Eloc);
7077 ("\?source will be extended with ^ high order " &
7082 elsif Source_Siz < Target_Siz then
7083 if Is_Discrete_Type (Target) then
7084 if Bytes_Big_Endian then
7086 ("\?target value will include ^ undefined " &
7091 ("\?target value will include ^ undefined " &
7098 ("\?^ trailing bits of target value will be " &
7099 "undefined!", Eloc);
7102 else pragma Assert (Source_Siz > Target_Siz);
7104 ("\?^ trailing bits of source will be ignored!",
7111 -- If both types are access types, we need to check the alignment.
7112 -- If the alignment of both is specified, we can do it here.
7114 if Serious_Errors_Detected = 0
7115 and then Ekind (Source) in Access_Kind
7116 and then Ekind (Target) in Access_Kind
7117 and then Target_Strict_Alignment
7118 and then Present (Designated_Type (Source))
7119 and then Present (Designated_Type (Target))
7122 D_Source : constant Entity_Id := Designated_Type (Source);
7123 D_Target : constant Entity_Id := Designated_Type (Target);
7126 if Known_Alignment (D_Source)
7127 and then Known_Alignment (D_Target)
7130 Source_Align : constant Uint := Alignment (D_Source);
7131 Target_Align : constant Uint := Alignment (D_Target);
7134 if Source_Align < Target_Align
7135 and then not Is_Tagged_Type (D_Source)
7137 -- Suppress warning if warnings suppressed on either
7138 -- type or either designated type. Note the use of
7139 -- OR here instead of OR ELSE. That is intentional,
7140 -- we would like to set flag Warnings_Off_Used in
7141 -- all types for which warnings are suppressed.
7143 and then not (Has_Warnings_Off (D_Source)
7145 Has_Warnings_Off (D_Target)
7147 Has_Warnings_Off (Source)
7149 Has_Warnings_Off (Target))
7151 Error_Msg_Uint_1 := Target_Align;
7152 Error_Msg_Uint_2 := Source_Align;
7153 Error_Msg_Node_1 := D_Target;
7154 Error_Msg_Node_2 := D_Source;
7156 ("?alignment of & (^) is stricter than " &
7157 "alignment of & (^)!", Eloc);
7159 ("\?resulting access value may have invalid " &
7160 "alignment!", Eloc);
7168 end Validate_Unchecked_Conversions;