1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2020, 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 Debug; use Debug;
30 with Einfo; use Einfo;
31 with Elists; use Elists;
32 with Errout; use Errout;
33 with Exp_Disp; use Exp_Disp;
34 with Exp_Tss; use Exp_Tss;
35 with Exp_Util; use Exp_Util;
36 with Freeze; use Freeze;
37 with Ghost; use Ghost;
39 with Lib.Xref; use Lib.Xref;
40 with Namet; use Namet;
41 with Nlists; use Nlists;
42 with Nmake; use Nmake;
44 with Par_SCO; use Par_SCO;
45 with Restrict; use Restrict;
46 with Rident; use Rident;
47 with Rtsfind; use Rtsfind;
49 with Sem_Aux; use Sem_Aux;
50 with Sem_Case; use Sem_Case;
51 with Sem_Cat; use Sem_Cat;
52 with Sem_Ch3; use Sem_Ch3;
53 with Sem_Ch6; use Sem_Ch6;
54 with Sem_Ch7; use Sem_Ch7;
55 with Sem_Ch8; use Sem_Ch8;
56 with Sem_Dim; use Sem_Dim;
57 with Sem_Disp; use Sem_Disp;
58 with Sem_Eval; use Sem_Eval;
59 with Sem_Prag; use Sem_Prag;
60 with Sem_Res; use Sem_Res;
61 with Sem_Type; use Sem_Type;
62 with Sem_Util; use Sem_Util;
63 with Sem_Warn; use Sem_Warn;
64 with Sinfo; use Sinfo;
65 with Sinput; use Sinput;
66 with Snames; use Snames;
67 with Stand; use Stand;
68 with Targparm; use Targparm;
69 with Ttypes; use Ttypes;
70 with Tbuild; use Tbuild;
71 with Urealp; use Urealp;
72 with Warnsw; use Warnsw;
74 with GNAT.Heap_Sort_G;
76 package body Sem_Ch13 is
78 SSU : constant Pos := System_Storage_Unit;
79 -- Convenient short hand for commonly used constant
81 -----------------------
82 -- Local Subprograms --
83 -----------------------
85 procedure Adjust_Record_For_Reverse_Bit_Order_Ada_95 (R : Entity_Id);
86 -- Helper routine providing the original (pre-AI95-0133) behavior for
87 -- Adjust_Record_For_Reverse_Bit_Order.
89 procedure Alignment_Check_For_Size_Change (Typ : Entity_Id; Size : Uint);
90 -- This routine is called after setting one of the sizes of type entity
91 -- Typ to Size. The purpose is to deal with the situation of a derived
92 -- type whose inherited alignment is no longer appropriate for the new
93 -- size value. In this case, we reset the Alignment to unknown.
95 function All_Static_Choices (L : List_Id) return Boolean;
96 -- Returns true if all elements of the list are OK static choices
97 -- as defined below for Is_Static_Choice. Used for case expression
98 -- alternatives and for the right operand of a membership test. An
99 -- others_choice is static if the corresponding expression is static.
100 -- The staticness of the bounds is checked separately.
102 procedure Build_Discrete_Static_Predicate
106 -- Given a predicated type Typ, where Typ is a discrete static subtype,
107 -- whose predicate expression is Expr, tests if Expr is a static predicate,
108 -- and if so, builds the predicate range list. Nam is the name of the one
109 -- argument to the predicate function. Occurrences of the type name in the
110 -- predicate expression have been replaced by identifier references to this
111 -- name, which is unique, so any identifier with Chars matching Nam must be
112 -- a reference to the type. If the predicate is non-static, this procedure
113 -- returns doing nothing. If the predicate is static, then the predicate
114 -- list is stored in Static_Discrete_Predicate (Typ), and the Expr is
115 -- rewritten as a canonicalized membership operation.
117 function Build_Export_Import_Pragma
119 Id : Entity_Id) return Node_Id;
120 -- Create the corresponding pragma for aspect Export or Import denoted by
121 -- Asp. Id is the related entity subject to the aspect. Return Empty when
122 -- the expression of aspect Asp evaluates to False or is erroneous.
124 function Build_Predicate_Function_Declaration
125 (Typ : Entity_Id) return Node_Id;
126 -- Build the declaration for a predicate function. The declaration is built
127 -- at the end of the declarative part containing the type definition, which
128 -- may be before the freeze point of the type. The predicate expression is
129 -- preanalyzed at this point, to catch visibility errors.
131 procedure Build_Predicate_Functions (Typ : Entity_Id; N : Node_Id);
132 -- If Typ has predicates (indicated by Has_Predicates being set for Typ),
133 -- then either there are pragma Predicate entries on the rep chain for the
134 -- type (note that Predicate aspects are converted to pragma Predicate), or
135 -- there are inherited aspects from a parent type, or ancestor subtypes.
136 -- This procedure builds body for the Predicate function that tests these
137 -- predicates. N is the freeze node for the type. The spec of the function
138 -- is inserted before the freeze node, and the body of the function is
139 -- inserted after the freeze node. If the predicate expression has a least
140 -- one Raise_Expression, then this procedure also builds the M version of
141 -- the predicate function for use in membership tests.
143 procedure Check_Pool_Size_Clash (Ent : Entity_Id; SP, SS : Node_Id);
144 -- Called if both Storage_Pool and Storage_Size attribute definition
145 -- clauses (SP and SS) are present for entity Ent. Issue error message.
147 procedure Freeze_Entity_Checks (N : Node_Id);
148 -- Called from Analyze_Freeze_Entity and Analyze_Generic_Freeze Entity
149 -- to generate appropriate semantic checks that are delayed until this
150 -- point (they had to be delayed this long for cases of delayed aspects,
151 -- e.g. analysis of statically predicated subtypes in choices, for which
152 -- we have to be sure the subtypes in question are frozen before checking).
154 function Get_Alignment_Value (Expr : Node_Id) return Uint;
155 -- Given the expression for an alignment value, returns the corresponding
156 -- Uint value. If the value is inappropriate, then error messages are
157 -- posted as required, and a value of No_Uint is returned.
159 function Is_Operational_Item (N : Node_Id) return Boolean;
160 -- A specification for a stream attribute is allowed before the full type
161 -- is declared, as explained in AI-00137 and the corrigendum. Attributes
162 -- that do not specify a representation characteristic are operational
165 function Is_Static_Choice (N : Node_Id) return Boolean;
166 -- Returns True if N represents a static choice (static subtype, or
167 -- static subtype indication, or static expression, or static range).
169 -- Note that this is a bit more inclusive than we actually need
170 -- (in particular membership tests do not allow the use of subtype
171 -- indications). But that doesn't matter, we have already checked
172 -- that the construct is legal to get this far.
174 function Is_Type_Related_Rep_Item (N : Node_Id) return Boolean;
175 -- Returns True for a representation clause/pragma that specifies a
176 -- type-related representation (as opposed to operational) aspect.
178 function Is_Predicate_Static
180 Nam : Name_Id) return Boolean;
181 -- Given predicate expression Expr, tests if Expr is predicate-static in
182 -- the sense of the rules in (RM 3.2.4 (15-24)). Occurrences of the type
183 -- name in the predicate expression have been replaced by references to
184 -- an identifier whose Chars field is Nam. This name is unique, so any
185 -- identifier with Chars matching Nam must be a reference to the type.
186 -- Returns True if the expression is predicate-static and False otherwise,
187 -- but is not in the business of setting flags or issuing error messages.
189 -- Only scalar types can have static predicates, so False is always
190 -- returned for non-scalar types.
192 -- Note: the RM seems to suggest that string types can also have static
193 -- predicates. But that really makes lttle sense as very few useful
194 -- predicates can be constructed for strings. Remember that:
198 -- is not a static expression. So even though the clearly faulty RM wording
199 -- allows the following:
201 -- subtype S is String with Static_Predicate => S < "DEF"
203 -- We can't allow this, otherwise we have predicate-static applying to a
204 -- larger class than static expressions, which was never intended.
206 procedure New_Put_Image_Subprogram
210 -- Similar to New_Stream_Subprogram, but for the Put_Image attribute
212 procedure New_Stream_Subprogram
216 Nam : TSS_Name_Type);
217 -- Create a subprogram renaming of a given stream attribute to the
218 -- designated subprogram and then in the tagged case, provide this as a
219 -- primitive operation, or in the untagged case make an appropriate TSS
220 -- entry. This is more properly an expansion activity than just semantics,
221 -- but the presence of user-defined stream functions for limited types
222 -- is a legality check, which is why this takes place here rather than in
223 -- exp_ch13, where it was previously. Nam indicates the name of the TSS
224 -- function to be generated.
226 -- To avoid elaboration anomalies with freeze nodes, for untagged types
227 -- we generate both a subprogram declaration and a subprogram renaming
228 -- declaration, so that the attribute specification is handled as a
229 -- renaming_as_body. For tagged types, the specification is one of the
232 procedure No_Type_Rep_Item (N : Node_Id);
233 -- Output message indicating that no type-related aspects can be
234 -- specified due to some property of the parent type.
236 procedure Register_Address_Clause_Check
242 -- Register a check for the address clause N. The rest of the parameters
243 -- are in keeping with the components of Address_Clause_Check_Record below.
245 procedure Resolve_Iterable_Operation
250 -- If the name of a primitive operation for an Iterable aspect is
251 -- overloaded, resolve according to required signature.
257 Biased : Boolean := True);
258 -- If Biased is True, sets Has_Biased_Representation flag for E, and
259 -- outputs a warning message at node N if Warn_On_Biased_Representation is
260 -- is True. This warning inserts the string Msg to describe the construct
263 -----------------------------------------------------------
264 -- Visibility of Discriminants in Aspect Specifications --
265 -----------------------------------------------------------
267 -- The discriminants of a type are visible when analyzing the aspect
268 -- specifications of a type declaration or protected type declaration,
269 -- but not when analyzing those of a subtype declaration. The following
270 -- routines enforce this distinction.
272 procedure Push_Type (E : Entity_Id);
273 -- Push scope E and make visible the discriminants of type entity E if E
274 -- has discriminants and is not a subtype.
276 procedure Pop_Type (E : Entity_Id);
277 -- Remove visibility to the discriminants of type entity E and pop the
278 -- scope stack if E has discriminants and is not a subtype.
280 ----------------------------------------------
281 -- Table for Validate_Unchecked_Conversions --
282 ----------------------------------------------
284 -- The following table collects unchecked conversions for validation.
285 -- Entries are made by Validate_Unchecked_Conversion and then the call
286 -- to Validate_Unchecked_Conversions does the actual error checking and
287 -- posting of warnings. The reason for this delayed processing is to take
288 -- advantage of back-annotations of size and alignment values performed by
291 -- Note: the reason we store a Source_Ptr value instead of a Node_Id is
292 -- that by the time Validate_Unchecked_Conversions is called, Sprint will
293 -- already have modified all Sloc values if the -gnatD option is set.
295 type UC_Entry is record
296 Eloc : Source_Ptr; -- node used for posting warnings
297 Source : Entity_Id; -- source type for unchecked conversion
298 Target : Entity_Id; -- target type for unchecked conversion
299 Act_Unit : Entity_Id; -- actual function instantiated
302 package Unchecked_Conversions is new Table.Table (
303 Table_Component_Type => UC_Entry,
304 Table_Index_Type => Int,
305 Table_Low_Bound => 1,
307 Table_Increment => 200,
308 Table_Name => "Unchecked_Conversions");
310 ----------------------------------------
311 -- Table for Validate_Address_Clauses --
312 ----------------------------------------
314 -- If an address clause has the form
316 -- for X'Address use Expr
318 -- where Expr has a value known at compile time or is of the form Y'Address
319 -- or recursively is a reference to a constant initialized with either of
320 -- these forms, and the value of Expr is not a multiple of X's alignment,
321 -- or if Y has a smaller alignment than X, then that merits a warning about
322 -- possible bad alignment. The following table collects address clauses of
323 -- this kind. We put these in a table so that they can be checked after the
324 -- back end has completed annotation of the alignments of objects, since we
325 -- can catch more cases that way.
327 type Address_Clause_Check_Record is record
329 -- The address clause
332 -- The entity of the object subject to the address clause
335 -- The value of the address in the first case
338 -- The entity of the object being overlaid in the second case
341 -- Whether the address is offset within Y in the second case
343 Alignment_Checks_Suppressed : Boolean;
344 -- Whether alignment checks are suppressed by an active scope suppress
345 -- setting. We need to save the value in order to be able to reuse it
346 -- after the back end has been run.
349 package Address_Clause_Checks is new Table.Table (
350 Table_Component_Type => Address_Clause_Check_Record,
351 Table_Index_Type => Int,
352 Table_Low_Bound => 1,
354 Table_Increment => 200,
355 Table_Name => "Address_Clause_Checks");
357 function Alignment_Checks_Suppressed
358 (ACCR : Address_Clause_Check_Record) return Boolean;
359 -- Return whether the alignment check generated for the address clause
362 ---------------------------------
363 -- Alignment_Checks_Suppressed --
364 ---------------------------------
366 function Alignment_Checks_Suppressed
367 (ACCR : Address_Clause_Check_Record) return Boolean
370 if Checks_May_Be_Suppressed (ACCR.X) then
371 return Is_Check_Suppressed (ACCR.X, Alignment_Check);
373 return ACCR.Alignment_Checks_Suppressed;
375 end Alignment_Checks_Suppressed;
377 -----------------------------------------
378 -- Adjust_Record_For_Reverse_Bit_Order --
379 -----------------------------------------
381 procedure Adjust_Record_For_Reverse_Bit_Order (R : Entity_Id) is
382 Max_Machine_Scalar_Size : constant Uint :=
384 (Standard_Long_Long_Integer_Size);
385 -- We use this as the maximum machine scalar size
387 SSU : constant Uint := UI_From_Int (System_Storage_Unit);
394 -- The processing done here used to depend on the Ada version, but the
395 -- behavior has been changed by AI95-0133. However this AI is a Binding
396 -- Interpretation, so we now implement it even in Ada 95 mode. But the
397 -- original behavior from unamended Ada 95 is available for the sake of
398 -- compatibility under the debugging switch -gnatd.p in Ada 95 mode.
400 if Ada_Version < Ada_2005 and then Debug_Flag_Dot_P then
401 Adjust_Record_For_Reverse_Bit_Order_Ada_95 (R);
405 -- For Ada 2005, we do machine scalar processing, as fully described In
406 -- AI-133. This involves gathering all components which start at the
407 -- same byte offset and processing them together. Same approach is still
408 -- valid in later versions including Ada 2012.
410 -- Note that component clauses found on record types may be inherited,
411 -- in which case the layout of the component with such a clause still
412 -- has to be done at this point. Therefore, the processing done here
413 -- must exclusively rely on the Component_Clause of the component.
415 -- This first loop through components does two things. First it deals
416 -- with the case of components with component clauses whose length is
417 -- greater than the maximum machine scalar size (either accepting them
418 -- or rejecting as needed). Second, it counts the number of components
419 -- with component clauses whose length does not exceed this maximum for
423 Comp := First_Component_Or_Discriminant (R);
424 while Present (Comp) loop
425 CC := Component_Clause (Comp);
429 Fbit : constant Uint := Static_Integer (First_Bit (CC));
430 Lbit : constant Uint := Static_Integer (Last_Bit (CC));
433 -- Case of component with last bit >= max machine scalar
435 if Lbit >= Max_Machine_Scalar_Size then
437 -- This is allowed only if first bit is zero, and last bit
438 -- + 1 is a multiple of storage unit size.
440 if Fbit = 0 and then (Lbit + 1) mod SSU = 0 then
442 -- This is the case to give a warning if enabled
444 if Warn_On_Reverse_Bit_Order then
446 ("info: multi-byte field specified with "
447 & "non-standard Bit_Order?V?", CC);
449 if Bytes_Big_Endian then
451 ("\bytes are not reversed "
452 & "(component is big-endian)?V?", CC);
455 ("\bytes are not reversed "
456 & "(component is little-endian)?V?", CC);
460 -- Give error message for RM 13.5.1(10) violation
464 ("machine scalar rules not followed for&",
465 First_Bit (CC), Comp);
467 Error_Msg_Uint_1 := Lbit + 1;
468 Error_Msg_Uint_2 := Max_Machine_Scalar_Size;
470 ("\last bit + 1 (^) exceeds maximum machine scalar "
471 & "size (^)", First_Bit (CC));
473 if (Lbit + 1) mod SSU /= 0 then
474 Error_Msg_Uint_1 := SSU;
476 ("\and is not a multiple of Storage_Unit (^) "
477 & "(RM 13.5.1(10))", First_Bit (CC));
480 Error_Msg_Uint_1 := Fbit;
482 ("\and first bit (^) is non-zero "
483 & "(RM 13.4.1(10))", First_Bit (CC));
487 -- OK case of machine scalar related component clause. For now,
491 Num_CC := Num_CC + 1;
496 Next_Component_Or_Discriminant (Comp);
499 -- We need to sort the component clauses on the basis of the Position
500 -- values in the clause, so we can group clauses with the same Position
501 -- together to determine the relevant machine scalar size.
504 Comps : array (0 .. Num_CC) of Entity_Id;
505 -- Array to collect component and discriminant entities. The data
506 -- starts at index 1, the 0'th entry is for the sort routine.
508 function CP_Lt (Op1, Op2 : Natural) return Boolean;
509 -- Compare routine for Sort
511 procedure CP_Move (From : Natural; To : Natural);
512 -- Move routine for Sort
514 package Sorting is new GNAT.Heap_Sort_G (CP_Move, CP_Lt);
517 -- Maximum last bit value of any component in this set
520 -- Corresponding machine scalar size
524 -- Start and stop positions in the component list of the set of
525 -- components with the same starting position (that constitute
526 -- components in a single machine scalar).
532 function CP_Lt (Op1, Op2 : Natural) return Boolean is
535 Position (Component_Clause (Comps (Op1))) <
536 Position (Component_Clause (Comps (Op2)));
543 procedure CP_Move (From : Natural; To : Natural) is
545 Comps (To) := Comps (From);
548 -- Start of processing for Sort_CC
551 -- Collect the machine scalar relevant component clauses
554 Comp := First_Component_Or_Discriminant (R);
555 while Present (Comp) loop
557 CC : constant Node_Id := Component_Clause (Comp);
560 -- Collect only component clauses whose last bit is less than
561 -- machine scalar size. Any component clause whose last bit
562 -- exceeds this value does not take part in machine scalar
563 -- layout considerations. The test for Error_Posted makes sure
564 -- we exclude component clauses for which we already posted an
568 and then not Error_Posted (Last_Bit (CC))
569 and then Static_Integer (Last_Bit (CC)) <
570 Max_Machine_Scalar_Size
572 Num_CC := Num_CC + 1;
573 Comps (Num_CC) := Comp;
577 Next_Component_Or_Discriminant (Comp);
580 -- Sort by ascending position number
582 Sorting.Sort (Num_CC);
584 -- We now have all the components whose size does not exceed the max
585 -- machine scalar value, sorted by starting position. In this loop we
586 -- gather groups of clauses starting at the same position, to process
587 -- them in accordance with AI-133.
590 while Stop < Num_CC loop
595 (Last_Bit (Component_Clause (Comps (Start))));
596 while Stop < Num_CC loop
598 (Position (Component_Clause (Comps (Stop + 1)))) =
600 (Position (Component_Clause (Comps (Stop))))
608 (Component_Clause (Comps (Stop)))));
614 -- Now we have a group of component clauses from Start to Stop
615 -- whose positions are identical, and MaxL is the maximum last
616 -- bit value of any of these components.
618 -- We need to determine the corresponding machine scalar size.
619 -- This loop assumes that machine scalar sizes are even, and that
620 -- each possible machine scalar has twice as many bits as the next
623 MSS := Max_Machine_Scalar_Size;
625 and then (MSS / 2) >= SSU
626 and then (MSS / 2) > MaxL
631 -- Here is where we fix up the Component_Bit_Offset value to
632 -- account for the reverse bit order. Some examples of what needs
633 -- to be done for the case of a machine scalar size of 8 are:
635 -- First_Bit .. Last_Bit Component_Bit_Offset
647 -- The rule is that the first bit is obtained by subtracting the
648 -- old ending bit from machine scalar size - 1.
650 for C in Start .. Stop loop
652 Comp : constant Entity_Id := Comps (C);
653 CC : constant Node_Id := Component_Clause (Comp);
655 FB : constant Uint := Static_Integer (First_Bit (CC));
656 LB : constant Uint := Static_Integer (Last_Bit (CC));
657 NFB : constant Uint := MSS - 1 - LB;
658 NLB : constant Uint := NFB + LB - FB;
659 Pos : constant Uint := Static_Integer (Position (CC));
662 -- Do not warn for the artificial clause built for the tag
663 -- in Check_Record_Representation_Clause if it is inherited.
665 if Warn_On_Reverse_Bit_Order
666 and then Chars (Comp) /= Name_uTag
668 Error_Msg_Uint_1 := MSS;
670 ("info: reverse bit order in machine scalar of "
671 & "length^?V?", First_Bit (CC));
672 Error_Msg_Uint_1 := NFB;
673 Error_Msg_Uint_2 := NLB;
675 if Bytes_Big_Endian then
677 ("\big-endian range for component & is ^ .. ^?V?",
678 First_Bit (CC), Comp);
681 ("\little-endian range for component & is ^ .. ^?V?",
682 First_Bit (CC), Comp);
686 Set_Component_Bit_Offset (Comp, Pos * SSU + NFB);
687 Set_Esize (Comp, 1 + (NLB - NFB));
688 Set_Normalized_First_Bit (Comp, NFB mod SSU);
689 Set_Normalized_Position (Comp, Pos + NFB / SSU);
694 end Adjust_Record_For_Reverse_Bit_Order;
696 ------------------------------------------------
697 -- Adjust_Record_For_Reverse_Bit_Order_Ada_95 --
698 ------------------------------------------------
700 procedure Adjust_Record_For_Reverse_Bit_Order_Ada_95 (R : Entity_Id) is
705 -- For Ada 95, we just renumber bits within a storage unit. We do the
706 -- same for Ada 83 mode, since we recognize the Bit_Order attribute in
707 -- Ada 83, and are free to add this extension.
709 Comp := First_Component_Or_Discriminant (R);
710 while Present (Comp) loop
711 CC := Component_Clause (Comp);
713 -- If component clause is present, then deal with the non-default
714 -- bit order case for Ada 95 mode.
716 -- We only do this processing for the base type, and in fact that
717 -- is important, since otherwise if there are record subtypes, we
718 -- could reverse the bits once for each subtype, which is wrong.
720 if Present (CC) and then Ekind (R) = E_Record_Type then
722 CFB : constant Uint := Component_Bit_Offset (Comp);
723 CSZ : constant Uint := Esize (Comp);
724 CLC : constant Node_Id := Component_Clause (Comp);
725 Pos : constant Node_Id := Position (CLC);
726 FB : constant Node_Id := First_Bit (CLC);
728 Storage_Unit_Offset : constant Uint :=
729 CFB / System_Storage_Unit;
731 Start_Bit : constant Uint :=
732 CFB mod System_Storage_Unit;
735 -- Cases where field goes over storage unit boundary
737 if Start_Bit + CSZ > System_Storage_Unit then
739 -- Allow multi-byte field but generate warning
741 if Start_Bit mod System_Storage_Unit = 0
742 and then CSZ mod System_Storage_Unit = 0
745 ("info: multi-byte field specified with non-standard "
746 & "Bit_Order?V?", CLC);
748 if Bytes_Big_Endian then
750 ("\bytes are not reversed "
751 & "(component is big-endian)?V?", CLC);
754 ("\bytes are not reversed "
755 & "(component is little-endian)?V?", CLC);
758 -- Do not allow non-contiguous field
762 ("attempt to specify non-contiguous field not "
765 ("\caused by non-standard Bit_Order specified in "
766 & "legacy Ada 95 mode", CLC);
769 -- Case where field fits in one storage unit
772 -- Give warning if suspicious component clause
774 if Intval (FB) >= System_Storage_Unit
775 and then Warn_On_Reverse_Bit_Order
778 ("info: Bit_Order clause does not affect byte "
779 & "ordering?V?", Pos);
781 Intval (Pos) + Intval (FB) /
784 ("info: position normalized to ^ before bit order "
785 & "interpreted?V?", Pos);
788 -- Here is where we fix up the Component_Bit_Offset value
789 -- to account for the reverse bit order. Some examples of
790 -- what needs to be done are:
792 -- First_Bit .. Last_Bit Component_Bit_Offset
804 -- The rule is that the first bit is obtained by subtracting
805 -- the old ending bit from storage_unit - 1.
807 Set_Component_Bit_Offset (Comp,
808 (Storage_Unit_Offset * System_Storage_Unit) +
809 (System_Storage_Unit - 1) -
810 (Start_Bit + CSZ - 1));
812 Set_Normalized_Position (Comp,
813 Component_Bit_Offset (Comp) / System_Storage_Unit);
815 Set_Normalized_First_Bit (Comp,
816 Component_Bit_Offset (Comp) mod System_Storage_Unit);
821 Next_Component_Or_Discriminant (Comp);
823 end Adjust_Record_For_Reverse_Bit_Order_Ada_95;
825 -------------------------------------
826 -- Alignment_Check_For_Size_Change --
827 -------------------------------------
829 procedure Alignment_Check_For_Size_Change (Typ : Entity_Id; Size : Uint) is
831 -- If the alignment is known, and not set by a rep clause, and is
832 -- inconsistent with the size being set, then reset it to unknown,
833 -- we assume in this case that the size overrides the inherited
834 -- alignment, and that the alignment must be recomputed.
836 if Known_Alignment (Typ)
837 and then not Has_Alignment_Clause (Typ)
838 and then Size mod (Alignment (Typ) * SSU) /= 0
840 Init_Alignment (Typ);
842 end Alignment_Check_For_Size_Change;
844 -----------------------------------
845 -- All_Membership_Choices_Static --
846 -----------------------------------
848 function All_Membership_Choices_Static (Expr : Node_Id) return Boolean is
849 pragma Assert (Nkind (Expr) in N_Membership_Test);
852 (Present (Right_Opnd (Expr))
854 Present (Alternatives (Expr)));
856 if Present (Right_Opnd (Expr)) then
857 return Is_Static_Choice (Right_Opnd (Expr));
859 return All_Static_Choices (Alternatives (Expr));
861 end All_Membership_Choices_Static;
863 ------------------------
864 -- All_Static_Choices --
865 ------------------------
867 function All_Static_Choices (L : List_Id) return Boolean is
872 while Present (N) loop
873 if not Is_Static_Choice (N) then
881 end All_Static_Choices;
883 -------------------------------------
884 -- Analyze_Aspects_At_Freeze_Point --
885 -------------------------------------
887 procedure Analyze_Aspects_At_Freeze_Point (E : Entity_Id) is
888 procedure Analyze_Aspect_Default_Value (ASN : Node_Id);
889 -- This routine analyzes an Aspect_Default_[Component_]Value denoted by
890 -- the aspect specification node ASN.
892 procedure Check_Aspect_Too_Late (N : Node_Id);
893 -- This procedure is similar to Rep_Item_Too_Late for representation
894 -- aspects that apply to type and that do not have a corresponding
896 -- Used to check in particular that the expression associated with
897 -- aspect node N for the given type (entity) of the aspect does not
898 -- appear too late according to the rules in RM 13.1(9) and 13.1(10).
900 procedure Inherit_Delayed_Rep_Aspects (ASN : Node_Id);
901 -- As discussed in the spec of Aspects (see Aspect_Delay declaration),
902 -- a derived type can inherit aspects from its parent which have been
903 -- specified at the time of the derivation using an aspect, as in:
905 -- type A is range 1 .. 10
906 -- with Size => Not_Defined_Yet;
910 -- Not_Defined_Yet : constant := 64;
912 -- In this example, the Size of A is considered to be specified prior
913 -- to the derivation, and thus inherited, even though the value is not
914 -- known at the time of derivation. To deal with this, we use two entity
915 -- flags. The flag Has_Derived_Rep_Aspects is set in the parent type (A
916 -- here), and then the flag May_Inherit_Delayed_Rep_Aspects is set in
917 -- the derived type (B here). If this flag is set when the derived type
918 -- is frozen, then this procedure is called to ensure proper inheritance
919 -- of all delayed aspects from the parent type. The derived type is E,
920 -- the argument to Analyze_Aspects_At_Freeze_Point. ASN is the first
921 -- aspect specification node in the Rep_Item chain for the parent type.
923 procedure Make_Pragma_From_Boolean_Aspect (ASN : Node_Id);
924 -- Given an aspect specification node ASN whose expression is an
925 -- optional Boolean, this routines creates the corresponding pragma
926 -- at the freezing point.
928 ----------------------------------
929 -- Analyze_Aspect_Default_Value --
930 ----------------------------------
932 procedure Analyze_Aspect_Default_Value (ASN : Node_Id) is
933 Ent : constant Entity_Id := Entity (ASN);
934 Expr : constant Node_Id := Expression (ASN);
937 Set_Has_Default_Aspect (Base_Type (Ent));
939 if Is_Scalar_Type (Ent) then
940 Set_Default_Aspect_Value (Base_Type (Ent), Expr);
942 Set_Default_Aspect_Component_Value (Base_Type (Ent), Expr);
945 Check_Aspect_Too_Late (ASN);
946 end Analyze_Aspect_Default_Value;
948 ---------------------------
949 -- Check_Aspect_Too_Late --
950 ---------------------------
952 procedure Check_Aspect_Too_Late (N : Node_Id) is
953 Typ : constant Entity_Id := Entity (N);
954 Expr : constant Node_Id := Expression (N);
956 function Find_Type_Reference
957 (Typ : Entity_Id; Expr : Node_Id) return Boolean;
958 -- Return True if a reference to type Typ is found in the expression
961 -------------------------
962 -- Find_Type_Reference --
963 -------------------------
965 function Find_Type_Reference
966 (Typ : Entity_Id; Expr : Node_Id) return Boolean
968 function Find_Type (N : Node_Id) return Traverse_Result;
969 -- Set Found to True if N refers to Typ
975 function Find_Type (N : Node_Id) return Traverse_Result is
978 or else (Nkind_In (N, N_Identifier, N_Expanded_Name)
979 and then Present (Entity (N))
980 and then Entity (N) = Typ)
988 function Search_Type_Reference is new Traverse_Func (Find_Type);
991 return Search_Type_Reference (Expr) = Abandon;
992 end Find_Type_Reference;
994 Parent_Type : Entity_Id;
997 -- Ensure Expr is analyzed so that e.g. all types are properly
998 -- resolved for Find_Type_Reference.
1002 -- A self-referential aspect is illegal if it forces freezing the
1003 -- entity before the corresponding aspect has been analyzed.
1005 if Find_Type_Reference (Typ, Expr) then
1007 ("aspect specification causes premature freezing of&", N, Typ);
1010 -- For representation aspects, check for case of untagged derived
1011 -- type whose parent either has primitive operations (pre Ada 202x),
1012 -- or is a by-reference type (RM 13.1(10)).
1013 -- Strictly speaking the check also applies to Ada 2012 but it is
1014 -- really too constraining for existing code already, so relax it.
1015 -- ??? Confirming aspects should be allowed here.
1017 if Is_Representation_Aspect (Get_Aspect_Id (N))
1018 and then Is_Derived_Type (Typ)
1019 and then not Is_Tagged_Type (Typ)
1021 Parent_Type := Etype (Base_Type (Typ));
1023 if Ada_Version <= Ada_2012
1024 and then Has_Primitive_Operations (Parent_Type)
1027 ("|representation aspect not permitted before Ada 202x!", N);
1029 ("\parent type & has primitive operations!", N, Parent_Type);
1031 elsif Is_By_Reference_Type (Parent_Type) then
1032 No_Type_Rep_Item (N);
1034 ("\parent type & is a by-reference type!", N, Parent_Type);
1037 end Check_Aspect_Too_Late;
1039 ---------------------------------
1040 -- Inherit_Delayed_Rep_Aspects --
1041 ---------------------------------
1043 procedure Inherit_Delayed_Rep_Aspects (ASN : Node_Id) is
1044 A_Id : constant Aspect_Id := Get_Aspect_Id (ASN);
1045 P : constant Entity_Id := Entity (ASN);
1046 -- Entity for parent type
1049 -- Item from Rep_Item chain
1054 -- Loop through delayed aspects for the parent type
1057 while Present (N) loop
1058 if Nkind (N) = N_Aspect_Specification then
1059 exit when Entity (N) /= P;
1061 if Is_Delayed_Aspect (N) then
1062 A := Get_Aspect_Id (Chars (Identifier (N)));
1064 -- Process delayed rep aspect. For Boolean attributes it is
1065 -- not possible to cancel an attribute once set (the attempt
1066 -- to use an aspect with xxx => False is an error) for a
1067 -- derived type. So for those cases, we do not have to check
1068 -- if a clause has been given for the derived type, since it
1069 -- is harmless to set it again if it is already set.
1075 when Aspect_Alignment =>
1076 if not Has_Alignment_Clause (E) then
1077 Set_Alignment (E, Alignment (P));
1082 when Aspect_Atomic =>
1083 if Is_Atomic (P) then
1087 -- Atomic_Components
1089 when Aspect_Atomic_Components =>
1090 if Has_Atomic_Components (P) then
1091 Set_Has_Atomic_Components (Base_Type (E));
1096 when Aspect_Bit_Order =>
1097 if Is_Record_Type (E)
1098 and then No (Get_Attribute_Definition_Clause
1099 (E, Attribute_Bit_Order))
1100 and then Reverse_Bit_Order (P)
1102 Set_Reverse_Bit_Order (Base_Type (E));
1107 when Aspect_Component_Size =>
1108 if Is_Array_Type (E)
1109 and then not Has_Component_Size_Clause (E)
1112 (Base_Type (E), Component_Size (P));
1117 when Aspect_Machine_Radix =>
1118 if Is_Decimal_Fixed_Point_Type (E)
1119 and then not Has_Machine_Radix_Clause (E)
1121 Set_Machine_Radix_10 (E, Machine_Radix_10 (P));
1124 -- Object_Size (also Size which also sets Object_Size)
1126 when Aspect_Object_Size
1129 if not Has_Size_Clause (E)
1131 No (Get_Attribute_Definition_Clause
1132 (E, Attribute_Object_Size))
1134 Set_Esize (E, Esize (P));
1140 if not Is_Packed (E) then
1141 Set_Is_Packed (Base_Type (E));
1143 if Is_Bit_Packed_Array (P) then
1144 Set_Is_Bit_Packed_Array (Base_Type (E));
1145 Set_Packed_Array_Impl_Type
1146 (E, Packed_Array_Impl_Type (P));
1150 -- Scalar_Storage_Order
1152 when Aspect_Scalar_Storage_Order =>
1153 if (Is_Record_Type (E) or else Is_Array_Type (E))
1154 and then No (Get_Attribute_Definition_Clause
1155 (E, Attribute_Scalar_Storage_Order))
1156 and then Reverse_Storage_Order (P)
1158 Set_Reverse_Storage_Order (Base_Type (E));
1160 -- Clear default SSO indications, since the aspect
1161 -- overrides the default.
1163 Set_SSO_Set_Low_By_Default (Base_Type (E), False);
1164 Set_SSO_Set_High_By_Default (Base_Type (E), False);
1169 when Aspect_Small =>
1170 if Is_Fixed_Point_Type (E)
1171 and then not Has_Small_Clause (E)
1173 Set_Small_Value (E, Small_Value (P));
1178 when Aspect_Storage_Size =>
1179 if (Is_Access_Type (E) or else Is_Task_Type (E))
1180 and then not Has_Storage_Size_Clause (E)
1182 Set_Storage_Size_Variable
1183 (Base_Type (E), Storage_Size_Variable (P));
1188 when Aspect_Value_Size =>
1190 -- Value_Size is never inherited, it is either set by
1191 -- default, or it is explicitly set for the derived
1192 -- type. So nothing to do here.
1198 when Aspect_Volatile =>
1199 if Is_Volatile (P) then
1200 Set_Is_Volatile (E);
1203 -- Volatile_Full_Access
1205 when Aspect_Volatile_Full_Access =>
1206 if Is_Volatile_Full_Access (P) then
1207 Set_Is_Volatile_Full_Access (E);
1210 -- Volatile_Components
1212 when Aspect_Volatile_Components =>
1213 if Has_Volatile_Components (P) then
1214 Set_Has_Volatile_Components (Base_Type (E));
1217 -- That should be all the Rep Aspects
1220 pragma Assert (Aspect_Delay (A_Id) /= Rep_Aspect);
1228 end Inherit_Delayed_Rep_Aspects;
1230 -------------------------------------
1231 -- Make_Pragma_From_Boolean_Aspect --
1232 -------------------------------------
1234 procedure Make_Pragma_From_Boolean_Aspect (ASN : Node_Id) is
1235 Ident : constant Node_Id := Identifier (ASN);
1236 A_Name : constant Name_Id := Chars (Ident);
1237 A_Id : constant Aspect_Id := Get_Aspect_Id (A_Name);
1238 Ent : constant Entity_Id := Entity (ASN);
1239 Expr : constant Node_Id := Expression (ASN);
1240 Loc : constant Source_Ptr := Sloc (ASN);
1242 procedure Check_False_Aspect_For_Derived_Type;
1243 -- This procedure checks for the case of a false aspect for a derived
1244 -- type, which improperly tries to cancel an aspect inherited from
1247 -----------------------------------------
1248 -- Check_False_Aspect_For_Derived_Type --
1249 -----------------------------------------
1251 procedure Check_False_Aspect_For_Derived_Type is
1255 -- We are only checking derived types
1257 if not Is_Derived_Type (E) then
1261 Par := Nearest_Ancestor (E);
1267 if not Is_Atomic (Par) then
1271 when Aspect_Atomic_Components =>
1272 if not Has_Atomic_Components (Par) then
1276 when Aspect_Discard_Names =>
1277 if not Discard_Names (Par) then
1282 if not Is_Packed (Par) then
1286 when Aspect_Unchecked_Union =>
1287 if not Is_Unchecked_Union (Par) then
1291 when Aspect_Volatile =>
1292 if not Is_Volatile (Par) then
1296 when Aspect_Volatile_Components =>
1297 if not Has_Volatile_Components (Par) then
1301 when Aspect_Volatile_Full_Access =>
1302 if not Is_Volatile_Full_Access (Par) then
1310 -- Fall through means we are canceling an inherited aspect
1312 Error_Msg_Name_1 := A_Name;
1314 ("derived type& inherits aspect%, cannot cancel", Expr, E);
1315 end Check_False_Aspect_For_Derived_Type;
1321 -- Start of processing for Make_Pragma_From_Boolean_Aspect
1324 -- Note that we know Expr is present, because for a missing Expr
1325 -- argument, we knew it was True and did not need to delay the
1326 -- evaluation to the freeze point.
1328 if Is_False (Static_Boolean (Expr)) then
1329 Check_False_Aspect_For_Derived_Type;
1334 Pragma_Identifier =>
1335 Make_Identifier (Sloc (Ident), Chars (Ident)),
1336 Pragma_Argument_Associations => New_List (
1337 Make_Pragma_Argument_Association (Sloc (Ident),
1338 Expression => New_Occurrence_Of (Ent, Sloc (Ident)))));
1340 Set_From_Aspect_Specification (Prag, True);
1341 Set_Corresponding_Aspect (Prag, ASN);
1342 Set_Aspect_Rep_Item (ASN, Prag);
1343 Set_Is_Delayed_Aspect (Prag);
1344 Set_Parent (Prag, ASN);
1346 end Make_Pragma_From_Boolean_Aspect;
1354 -- Start of processing for Analyze_Aspects_At_Freeze_Point
1357 -- Must be visible in current scope, but if this is a type from a nested
1358 -- package it may be frozen from an object declaration in the enclosing
1359 -- scope, so install the package declarations to complete the analysis
1360 -- of the aspects, if any. If the package itself is frozen the type will
1361 -- have been frozen as well.
1363 if not Scope_Within_Or_Same (Current_Scope, Scope (E)) then
1364 if Is_Type (E) and then From_Nested_Package (E) then
1366 Pack : constant Entity_Id := Scope (E);
1370 Install_Visible_Declarations (Pack);
1371 Install_Private_Declarations (Pack);
1372 Analyze_Aspects_At_Freeze_Point (E);
1374 if Is_Private_Type (E)
1375 and then Present (Full_View (E))
1377 Analyze_Aspects_At_Freeze_Point (Full_View (E));
1380 End_Package_Scope (Pack);
1384 -- Aspects from other entities in different contexts are analyzed
1392 -- Look for aspect specification entries for this entity
1394 ASN := First_Rep_Item (E);
1395 while Present (ASN) loop
1396 if Nkind (ASN) = N_Aspect_Specification then
1397 exit when Entity (ASN) /= E;
1399 if Is_Delayed_Aspect (ASN) then
1400 A_Id := Get_Aspect_Id (ASN);
1404 -- For aspects whose expression is an optional Boolean, make
1405 -- the corresponding pragma at the freeze point.
1407 when Boolean_Aspects
1408 | Library_Unit_Aspects
1410 -- Aspects Export and Import require special handling.
1411 -- Both are by definition Boolean and may benefit from
1412 -- forward references, however their expressions are
1413 -- treated as static. In addition, the syntax of their
1414 -- corresponding pragmas requires extra "pieces" which
1415 -- may also contain forward references. To account for
1416 -- all of this, the corresponding pragma is created by
1417 -- Analyze_Aspect_Export_Import, but is not analyzed as
1418 -- the complete analysis must happen now.
1420 if A_Id = Aspect_Export or else A_Id = Aspect_Import then
1423 -- Otherwise create a corresponding pragma
1426 Make_Pragma_From_Boolean_Aspect (ASN);
1429 -- Special handling for aspects that don't correspond to
1430 -- pragmas/attributes.
1432 when Aspect_Default_Value
1433 | Aspect_Default_Component_Value
1435 -- Do not inherit aspect for anonymous base type of a
1436 -- scalar or array type, because they apply to the first
1437 -- subtype of the type, and will be processed when that
1438 -- first subtype is frozen.
1440 if Is_Derived_Type (E)
1441 and then not Comes_From_Source (E)
1442 and then E /= First_Subtype (E)
1446 Analyze_Aspect_Default_Value (ASN);
1449 -- Ditto for iterator aspects, because the corresponding
1450 -- attributes may not have been analyzed yet.
1452 when Aspect_Constant_Indexing
1453 | Aspect_Default_Iterator
1454 | Aspect_Iterator_Element
1455 | Aspect_Variable_Indexing
1457 Analyze (Expression (ASN));
1459 if Etype (Expression (ASN)) = Any_Type then
1461 ("\aspect must be fully defined before & is frozen",
1465 when Aspect_Integer_Literal
1466 | Aspect_Real_Literal
1467 | Aspect_String_Literal
1469 Validate_Literal_Aspect (E, ASN);
1471 when Aspect_Iterable =>
1472 Validate_Iterable_Aspect (E, ASN);
1478 Ritem := Aspect_Rep_Item (ASN);
1480 if Present (Ritem) then
1486 Next_Rep_Item (ASN);
1489 -- This is where we inherit delayed rep aspects from our parent. Note
1490 -- that if we fell out of the above loop with ASN non-empty, it means
1491 -- we hit an aspect for an entity other than E, and it must be the
1492 -- type from which we were derived.
1494 if May_Inherit_Delayed_Rep_Aspects (E) then
1495 Inherit_Delayed_Rep_Aspects (ASN);
1499 and then E /= Base_Type (E)
1500 and then Is_First_Subtype (E)
1502 Inherit_Rep_Item_Chain (Base_Type (E), E);
1504 end Analyze_Aspects_At_Freeze_Point;
1506 -----------------------------------
1507 -- Analyze_Aspect_Specifications --
1508 -----------------------------------
1510 procedure Analyze_Aspect_Specifications (N : Node_Id; E : Entity_Id) is
1511 pragma Assert (Present (E));
1513 procedure Decorate (Asp : Node_Id; Prag : Node_Id);
1514 -- Establish linkages between an aspect and its corresponding pragma
1516 procedure Insert_Pragma
1518 Is_Instance : Boolean := False);
1519 -- Subsidiary to the analysis of aspects
1526 -- Initial_Condition
1535 -- Insert pragma Prag such that it mimics the placement of a source
1536 -- pragma of the same kind. Flag Is_Generic should be set when the
1537 -- context denotes a generic instance.
1543 procedure Decorate (Asp : Node_Id; Prag : Node_Id) is
1545 Set_Aspect_Rep_Item (Asp, Prag);
1546 Set_Corresponding_Aspect (Prag, Asp);
1547 Set_From_Aspect_Specification (Prag);
1548 Set_Parent (Prag, Asp);
1555 procedure Insert_Pragma
1557 Is_Instance : Boolean := False)
1563 Inserted : Boolean := False;
1566 -- When the aspect appears on an entry, package, protected unit,
1567 -- subprogram, or task unit body, insert the generated pragma at the
1568 -- top of the body declarations to emulate the behavior of a source
1571 -- package body Pack with Aspect is
1573 -- package body Pack is
1576 if Nkind_In (N, N_Entry_Body,
1582 Decls := Declarations (N);
1586 Set_Declarations (N, Decls);
1589 Prepend_To (Decls, Prag);
1591 -- When the aspect is associated with a [generic] package declaration
1592 -- insert the generated pragma at the top of the visible declarations
1593 -- to emulate the behavior of a source pragma.
1595 -- package Pack with Aspect is
1600 elsif Nkind_In (N, N_Generic_Package_Declaration,
1601 N_Package_Declaration)
1603 Decls := Visible_Declarations (Specification (N));
1607 Set_Visible_Declarations (Specification (N), Decls);
1610 -- The visible declarations of a generic instance have the
1611 -- following structure:
1613 -- <renamings of generic formals>
1614 -- <renamings of internally-generated spec and body>
1615 -- <first source declaration>
1617 -- Insert the pragma before the first source declaration by
1618 -- skipping the instance "header" to ensure proper visibility of
1622 Decl := First (Decls);
1623 while Present (Decl) loop
1624 if Comes_From_Source (Decl) then
1625 Insert_Before (Decl, Prag);
1633 -- The pragma is placed after the instance "header"
1635 if not Inserted then
1636 Append_To (Decls, Prag);
1639 -- Otherwise this is not a generic instance
1642 Prepend_To (Decls, Prag);
1645 -- When the aspect is associated with a protected unit declaration,
1646 -- insert the generated pragma at the top of the visible declarations
1647 -- the emulate the behavior of a source pragma.
1649 -- protected [type] Prot with Aspect is
1651 -- protected [type] Prot is
1654 elsif Nkind (N) = N_Protected_Type_Declaration then
1655 Def := Protected_Definition (N);
1659 Make_Protected_Definition (Sloc (N),
1660 Visible_Declarations => New_List,
1661 End_Label => Empty);
1663 Set_Protected_Definition (N, Def);
1666 Decls := Visible_Declarations (Def);
1670 Set_Visible_Declarations (Def, Decls);
1673 Prepend_To (Decls, Prag);
1675 -- When the aspect is associated with a task unit declaration, insert
1676 -- insert the generated pragma at the top of the visible declarations
1677 -- the emulate the behavior of a source pragma.
1679 -- task [type] Prot with Aspect is
1681 -- task [type] Prot is
1684 elsif Nkind (N) = N_Task_Type_Declaration then
1685 Def := Task_Definition (N);
1689 Make_Task_Definition (Sloc (N),
1690 Visible_Declarations => New_List,
1691 End_Label => Empty);
1693 Set_Task_Definition (N, Def);
1696 Decls := Visible_Declarations (Def);
1700 Set_Visible_Declarations (Def, Decls);
1703 Prepend_To (Decls, Prag);
1705 -- When the context is a library unit, the pragma is added to the
1706 -- Pragmas_After list.
1708 elsif Nkind (Parent (N)) = N_Compilation_Unit then
1709 Aux := Aux_Decls_Node (Parent (N));
1711 if No (Pragmas_After (Aux)) then
1712 Set_Pragmas_After (Aux, New_List);
1715 Prepend (Prag, Pragmas_After (Aux));
1717 -- Default, the pragma is inserted after the context
1720 Insert_After (N, Prag);
1727 Aitem : Node_Id := Empty;
1730 L : constant List_Id := Aspect_Specifications (N);
1731 pragma Assert (Present (L));
1733 Ins_Node : Node_Id := N;
1734 -- Insert pragmas/attribute definition clause after this node when no
1735 -- delayed analysis is required.
1737 -- Start of processing for Analyze_Aspect_Specifications
1740 -- The general processing involves building an attribute definition
1741 -- clause or a pragma node that corresponds to the aspect. Then in order
1742 -- to delay the evaluation of this aspect to the freeze point, we attach
1743 -- the corresponding pragma/attribute definition clause to the aspect
1744 -- specification node, which is then placed in the Rep Item chain. In
1745 -- this case we mark the entity by setting the flag Has_Delayed_Aspects
1746 -- and we evaluate the rep item at the freeze point. When the aspect
1747 -- doesn't have a corresponding pragma/attribute definition clause, then
1748 -- its analysis is simply delayed at the freeze point.
1750 -- Some special cases don't require delay analysis, thus the aspect is
1751 -- analyzed right now.
1753 -- Note that there is a special handling for Pre, Post, Test_Case,
1754 -- Contract_Cases aspects. In these cases, we do not have to worry
1755 -- about delay issues, since the pragmas themselves deal with delay
1756 -- of visibility for the expression analysis. Thus, we just insert
1757 -- the pragma after the node N.
1759 -- Loop through aspects
1761 Aspect := First (L);
1762 Aspect_Loop : while Present (Aspect) loop
1763 Analyze_One_Aspect : declare
1764 Expr : constant Node_Id := Expression (Aspect);
1765 Id : constant Node_Id := Identifier (Aspect);
1766 Loc : constant Source_Ptr := Sloc (Aspect);
1767 Nam : constant Name_Id := Chars (Id);
1768 A_Id : constant Aspect_Id := Get_Aspect_Id (Nam);
1771 Delay_Required : Boolean;
1772 -- Set False if delay is not required
1774 Eloc : Source_Ptr := No_Location;
1775 -- Source location of expression, modified when we split PPC's. It
1776 -- is set below when Expr is present.
1778 procedure Analyze_Aspect_Convention;
1779 -- Perform analysis of aspect Convention
1781 procedure Analyze_Aspect_Disable_Controlled;
1782 -- Perform analysis of aspect Disable_Controlled
1784 procedure Analyze_Aspect_Export_Import;
1785 -- Perform analysis of aspects Export or Import
1787 procedure Analyze_Aspect_External_Link_Name;
1788 -- Perform analysis of aspects External_Name or Link_Name
1790 procedure Analyze_Aspect_Implicit_Dereference;
1791 -- Perform analysis of the Implicit_Dereference aspects
1793 procedure Analyze_Aspect_Relaxed_Initialization;
1794 -- Perform analysis of aspect Relaxed_Initialization
1796 procedure Analyze_Aspect_Yield;
1797 -- Perform analysis of aspect Yield
1799 procedure Analyze_Aspect_Static;
1800 -- Ada 202x (AI12-0075): Perform analysis of aspect Static
1802 procedure Make_Aitem_Pragma
1803 (Pragma_Argument_Associations : List_Id;
1804 Pragma_Name : Name_Id);
1805 -- This is a wrapper for Make_Pragma used for converting aspects
1806 -- to pragmas. It takes care of Sloc (set from Loc) and building
1807 -- the pragma identifier from the given name. In addition the
1808 -- flags Class_Present and Split_PPC are set from the aspect
1809 -- node, as well as Is_Ignored. This routine also sets the
1810 -- From_Aspect_Specification in the resulting pragma node to
1811 -- True, and sets Corresponding_Aspect to point to the aspect.
1812 -- The resulting pragma is assigned to Aitem.
1814 -------------------------------
1815 -- Analyze_Aspect_Convention --
1816 -------------------------------
1818 procedure Analyze_Aspect_Convention is
1827 -- Obtain all interfacing aspects that apply to the related
1830 Get_Interfacing_Aspects
1831 (Iface_Asp => Aspect,
1832 Conv_Asp => Dummy_1,
1839 -- The related entity is subject to aspect Export or Import.
1840 -- Do not process Convention now because it must be analysed
1841 -- as part of Export or Import.
1843 if Present (Expo) or else Present (Imp) then
1846 -- Otherwise Convention appears by itself
1849 -- The aspect specifies a particular convention
1851 if Present (Expr) then
1852 Conv := New_Copy_Tree (Expr);
1854 -- Otherwise assume convention Ada
1857 Conv := Make_Identifier (Loc, Name_Ada);
1861 -- pragma Convention (<Conv>, <E>);
1864 (Pragma_Name => Name_Convention,
1865 Pragma_Argument_Associations => New_List (
1866 Make_Pragma_Argument_Association (Loc,
1867 Expression => Conv),
1868 Make_Pragma_Argument_Association (Loc,
1869 Expression => New_Occurrence_Of (E, Loc))));
1871 Decorate (Aspect, Aitem);
1872 Insert_Pragma (Aitem);
1874 end Analyze_Aspect_Convention;
1876 ---------------------------------------
1877 -- Analyze_Aspect_Disable_Controlled --
1878 ---------------------------------------
1880 procedure Analyze_Aspect_Disable_Controlled is
1882 -- The aspect applies only to controlled records
1884 if not (Ekind (E) = E_Record_Type
1885 and then Is_Controlled_Active (E))
1888 ("aspect % requires controlled record type", Aspect);
1892 -- Preanalyze the expression (if any) when the aspect resides
1893 -- in a generic unit.
1895 if Inside_A_Generic then
1896 if Present (Expr) then
1897 Preanalyze_And_Resolve (Expr, Any_Boolean);
1900 -- Otherwise the aspect resides in a nongeneric context
1903 -- A controlled record type loses its controlled semantics
1904 -- when the expression statically evaluates to True.
1906 if Present (Expr) then
1907 Analyze_And_Resolve (Expr, Any_Boolean);
1909 if Is_OK_Static_Expression (Expr) then
1910 if Is_True (Static_Boolean (Expr)) then
1911 Set_Disable_Controlled (E);
1914 -- Otherwise the expression is not static
1918 ("expression of aspect % must be static", Aspect);
1921 -- Otherwise the aspect appears without an expression and
1922 -- defaults to True.
1925 Set_Disable_Controlled (E);
1928 end Analyze_Aspect_Disable_Controlled;
1930 ----------------------------------
1931 -- Analyze_Aspect_Export_Import --
1932 ----------------------------------
1934 procedure Analyze_Aspect_Export_Import is
1942 -- Obtain all interfacing aspects that apply to the related
1945 Get_Interfacing_Aspects
1946 (Iface_Asp => Aspect,
1947 Conv_Asp => Dummy_1,
1954 -- The related entity cannot be subject to both aspects Export
1957 if Present (Expo) and then Present (Imp) then
1959 ("incompatible interfacing aspects given for &", E);
1960 Error_Msg_Sloc := Sloc (Expo);
1961 Error_Msg_N ("\aspect `Export` #", E);
1962 Error_Msg_Sloc := Sloc (Imp);
1963 Error_Msg_N ("\aspect `Import` #", E);
1966 -- A variable is most likely modified from the outside. Take
1967 -- the optimistic approach to avoid spurious errors.
1969 if Ekind (E) = E_Variable then
1970 Set_Never_Set_In_Source (E, False);
1973 -- Resolve the expression of an Import or Export here, and
1974 -- require it to be of type Boolean and static. This is not
1975 -- quite right, because in general this should be delayed,
1976 -- but that seems tricky for these, because normally Boolean
1977 -- aspects are replaced with pragmas at the freeze point in
1978 -- Make_Pragma_From_Boolean_Aspect.
1980 if not Present (Expr)
1981 or else Is_True (Static_Boolean (Expr))
1983 if A_Id = Aspect_Import then
1984 Set_Has_Completion (E);
1985 Set_Is_Imported (E);
1987 -- An imported object cannot be explicitly initialized
1989 if Nkind (N) = N_Object_Declaration
1990 and then Present (Expression (N))
1993 ("imported entities cannot be initialized "
1994 & "(RM B.1(24))", Expression (N));
1998 pragma Assert (A_Id = Aspect_Export);
1999 Set_Is_Exported (E);
2002 -- Create the proper form of pragma Export or Import taking
2003 -- into account Conversion, External_Name, and Link_Name.
2005 Aitem := Build_Export_Import_Pragma (Aspect, E);
2007 -- Otherwise the expression is either False or erroneous. There
2008 -- is no corresponding pragma.
2013 end Analyze_Aspect_Export_Import;
2015 ---------------------------------------
2016 -- Analyze_Aspect_External_Link_Name --
2017 ---------------------------------------
2019 procedure Analyze_Aspect_External_Link_Name is
2027 -- Obtain all interfacing aspects that apply to the related
2030 Get_Interfacing_Aspects
2031 (Iface_Asp => Aspect,
2032 Conv_Asp => Dummy_1,
2039 -- Ensure that aspect External_Name applies to aspect Export or
2042 if A_Id = Aspect_External_Name then
2043 if No (Expo) and then No (Imp) then
2045 ("aspect `External_Name` requires aspect `Import` or "
2046 & "`Export`", Aspect);
2049 -- Otherwise ensure that aspect Link_Name applies to aspect
2050 -- Export or Import.
2053 pragma Assert (A_Id = Aspect_Link_Name);
2054 if No (Expo) and then No (Imp) then
2056 ("aspect `Link_Name` requires aspect `Import` or "
2057 & "`Export`", Aspect);
2060 end Analyze_Aspect_External_Link_Name;
2062 -----------------------------------------
2063 -- Analyze_Aspect_Implicit_Dereference --
2064 -----------------------------------------
2066 procedure Analyze_Aspect_Implicit_Dereference is
2068 if not Is_Type (E) or else not Has_Discriminants (E) then
2070 ("aspect must apply to a type with discriminants", Expr);
2072 elsif not Is_Entity_Name (Expr) then
2074 ("aspect must name a discriminant of current type", Expr);
2077 -- Discriminant type be an anonymous access type or an
2078 -- anonymous access to subprogram.
2080 -- Missing synchronized types???
2083 Disc : Entity_Id := First_Discriminant (E);
2085 while Present (Disc) loop
2086 if Chars (Expr) = Chars (Disc)
2089 E_Anonymous_Access_Subprogram_Type,
2090 E_Anonymous_Access_Type)
2092 Set_Has_Implicit_Dereference (E);
2093 Set_Has_Implicit_Dereference (Disc);
2097 Next_Discriminant (Disc);
2100 -- Error if no proper access discriminant
2102 if Present (Disc) then
2103 -- For a type extension, check whether parent has
2104 -- a reference discriminant, to verify that use is
2107 if Is_Derived_Type (E)
2108 and then Has_Discriminants (Etype (E))
2111 Parent_Disc : constant Entity_Id :=
2112 Get_Reference_Discriminant (Etype (E));
2114 if Present (Parent_Disc)
2115 and then Corresponding_Discriminant (Disc) /=
2119 ("reference discriminant does not match "
2120 & "discriminant of parent type", Expr);
2127 ("not an access discriminant of&", Expr, E);
2132 end Analyze_Aspect_Implicit_Dereference;
2134 -------------------------------------------
2135 -- Analyze_Aspect_Relaxed_Initialization --
2136 -------------------------------------------
2138 procedure Analyze_Aspect_Relaxed_Initialization is
2139 procedure Analyze_Relaxed_Parameter
2140 (Subp_Id : Entity_Id;
2142 Seen : in out Elist_Id);
2143 -- Analyze parameter that appears in the expression of the
2144 -- aspect Relaxed_Initialization.
2146 -------------------------------
2147 -- Analyze_Relaxed_Parameter --
2148 -------------------------------
2150 procedure Analyze_Relaxed_Parameter
2151 (Subp_Id : Entity_Id;
2153 Seen : in out Elist_Id)
2156 -- The relaxed parameter is a formal parameter
2158 if Nkind_In (Param, N_Identifier, N_Expanded_Name) then
2162 Item : constant Entity_Id := Entity (Param);
2164 -- It must be a formal of the analyzed subprogram
2166 if Scope (Item) = Subp_Id then
2168 pragma Assert (Is_Formal (Item));
2170 -- Detect duplicated items
2172 if Contains (Seen, Item) then
2173 Error_Msg_N ("duplicate aspect % item", Param);
2175 Append_New_Elmt (Item, Seen);
2178 Error_Msg_N ("illegal aspect % item", Param);
2182 -- The relaxed parameter is the function's Result attribute
2184 elsif Is_Attribute_Result (Param) then
2188 Pref : constant Node_Id := Prefix (Param);
2192 Nkind_In (Pref, N_Identifier, N_Expanded_Name)
2194 Entity (Pref) = Subp_Id
2196 -- Detect duplicated items
2198 if Contains (Seen, Subp_Id) then
2199 Error_Msg_N ("duplicate aspect % item", Param);
2201 Append_New_Elmt (Entity (Pref), Seen);
2205 Error_Msg_N ("illegal aspect % item", Param);
2209 Error_Msg_N ("illegal aspect % item", Param);
2211 end Analyze_Relaxed_Parameter;
2215 Seen : Elist_Id := No_Elist;
2216 -- Items that appear in the relaxed initialization aspect
2217 -- expression of a subprogram; for detecting duplicates.
2219 Restore_Scope : Boolean;
2220 -- Will be set to True if we need to restore the scope table
2221 -- after analyzing the aspect expression.
2223 Prev_Id : Entity_Id;
2225 -- Start of processing for Analyze_Aspect_Relaxed_Initialization
2228 -- Set name of the aspect for error messages
2229 Error_Msg_Name_1 := Nam;
2231 -- Annotation of a type; no aspect expression is allowed.
2232 -- For a private type, the aspect must be attached to the
2235 -- ??? Once the exact rule for this aspect is ready, we will
2236 -- likely reject concurrent types, etc., so let's keep the code
2237 -- for types and variable separate.
2239 if Is_First_Subtype (E) then
2240 Prev_Id := Incomplete_Or_Partial_View (E);
2241 if Present (Prev_Id) then
2243 -- Aspect may appear on the full view of an incomplete
2244 -- type because the incomplete declaration cannot have
2247 if Ekind (Prev_Id) = E_Incomplete_Type then
2250 Error_Msg_N ("aspect % must apply to partial view", N);
2253 elsif Present (Expr) then
2254 Error_Msg_N ("illegal aspect % expression", Expr);
2257 -- Annotation of a variable; no aspect expression is allowed
2259 elsif Ekind (E) = E_Variable then
2260 if Present (Expr) then
2261 Error_Msg_N ("illegal aspect % expression", Expr);
2264 -- Annotation of a constant; no aspect expression is allowed.
2265 -- For a deferred constant, the aspect must be attached to the
2268 elsif Ekind (E) = E_Constant then
2269 if Present (Incomplete_Or_Partial_View (E)) then
2271 ("aspect % must apply to deferred constant", N);
2273 elsif Present (Expr) then
2274 Error_Msg_N ("illegal aspect % expression", Expr);
2277 -- Annotation of a subprogram; aspect expression is required
2279 elsif Is_Subprogram_Or_Entry (E) then
2280 if Present (Expr) then
2282 -- If we analyze subprogram body that acts as its own
2283 -- spec, then the subprogram itself and its formals are
2284 -- already installed; otherwise, we need to install them,
2285 -- as they must be visible when analyzing the aspect
2288 if In_Open_Scopes (E) then
2289 Restore_Scope := False;
2291 Restore_Scope := True;
2294 if Is_Generic_Subprogram (E) then
2295 Install_Generic_Formals (E);
2297 Install_Formals (E);
2301 -- Aspect expression is either an aggregate with list of
2302 -- parameters (and possibly the Result attribute for a
2305 if Nkind (Expr) = N_Aggregate then
2307 -- Component associations in the aggregate must be a
2308 -- parameter name followed by a static boolean
2311 if Present (Component_Associations (Expr)) then
2314 First (Component_Associations (Expr));
2316 while Present (Assoc) loop
2317 if List_Length (Choices (Assoc)) = 1 then
2318 Analyze_Relaxed_Parameter
2319 (E, First (Choices (Assoc)), Seen);
2321 if Inside_A_Generic then
2322 Preanalyze_And_Resolve
2323 (Expression (Assoc), Any_Boolean);
2326 (Expression (Assoc), Any_Boolean);
2329 if not Is_OK_Static_Expression
2330 (Expression (Assoc))
2333 ("expression of aspect %" &
2334 "must be static", Aspect);
2339 ("illegal aspect % expression", Expr);
2346 -- Expressions of the aggregate are parameter names
2348 if Present (Expressions (Expr)) then
2350 Param : Node_Id := First (Expressions (Expr));
2353 while Present (Param) loop
2354 Analyze_Relaxed_Parameter (E, Param, Seen);
2360 -- Mark the aggregate expression itself as analyzed;
2361 -- its subexpressions were marked when they themselves
2364 Set_Analyzed (Expr);
2366 -- Otherwise, it is a single name of a subprogram
2367 -- parameter (or possibly the Result attribute for
2371 Analyze_Relaxed_Parameter (E, Expr, Seen);
2374 if Restore_Scope then
2378 Error_Msg_N ("missing expression for aspect %", N);
2382 Error_Msg_N ("inappropriate entity for aspect %", E);
2384 end Analyze_Aspect_Relaxed_Initialization;
2386 ---------------------------
2387 -- Analyze_Aspect_Static --
2388 ---------------------------
2390 procedure Analyze_Aspect_Static is
2392 if Ada_Version < Ada_2020 then
2394 ("aspect % is an Ada 202x feature", Aspect);
2395 Error_Msg_N ("\compile with -gnat2020", Aspect);
2399 -- The aspect applies only to expression functions that
2400 -- statisfy the requirements for a static expression function
2401 -- (such as having an expression that is predicate-static).
2403 elsif not Is_Expression_Function (E) then
2405 ("aspect % requires expression function", Aspect);
2409 -- Ada 202x (AI12-0075): Check that the function satisfies
2410 -- several requirements of static expression functions as
2411 -- specified in RM 6.8(5.1-5.8). Note that some of the
2412 -- requirements given there are checked elsewhere.
2415 -- The expression of the expression function must be a
2416 -- potentially static expression (RM 202x 6.8(3.2-3.4)).
2417 -- That's checked in Sem_Ch6.Analyze_Expression_Function.
2419 -- The function must not contain any calls to itself, which
2420 -- is checked in Sem_Res.Resolve_Call.
2422 -- Each formal must be of mode in and have a static subtype
2425 Formal : Entity_Id := First_Formal (E);
2427 while Present (Formal) loop
2428 if Ekind (Formal) /= E_In_Parameter then
2430 ("aspect % requires formals of mode IN",
2436 if not Is_Static_Subtype (Etype (Formal)) then
2438 ("aspect % requires formals with static subtypes",
2444 Next_Formal (Formal);
2448 -- The function's result subtype must be a static subtype
2450 if not Is_Static_Subtype (Etype (E)) then
2452 ("aspect % requires function with result of "
2453 & "a static subtype",
2459 -- Check that the function does not have any applicable
2460 -- precondition or postcondition expression.
2462 for Asp in Pre_Post_Aspects loop
2463 if Has_Aspect (E, Asp) then
2465 ("this aspect not allowed for static expression "
2466 & "functions", Find_Aspect (E, Asp));
2472 -- ??? TBD: Must check that "for result type R, if the
2473 -- function is a boundary entity for type R (see 7.3.2),
2474 -- no type invariant applies to type R; if R has a
2475 -- component type C, a similar rule applies to C."
2478 -- Preanalyze the expression (if any) when the aspect resides
2479 -- in a generic unit. (Is this generic-related code necessary
2480 -- for this aspect? It's modeled on what's done for aspect
2481 -- Disable_Controlled. ???)
2483 if Inside_A_Generic then
2484 if Present (Expr) then
2485 Preanalyze_And_Resolve (Expr, Any_Boolean);
2488 -- Otherwise the aspect resides in a nongeneric context
2491 -- When the expression statically evaluates to True, the
2492 -- expression function is treated as a static function.
2493 -- Otherwise the aspect appears without an expression and
2494 -- defaults to True.
2496 if Present (Expr) then
2497 Analyze_And_Resolve (Expr, Any_Boolean);
2499 -- Error if the boolean expression is not static
2501 if not Is_OK_Static_Expression (Expr) then
2503 ("expression of aspect % must be static", Aspect);
2507 end Analyze_Aspect_Static;
2509 --------------------------
2510 -- Analyze_Aspect_Yield --
2511 --------------------------
2513 procedure Analyze_Aspect_Yield is
2514 Expr_Value : Boolean := False;
2517 -- Check valid declarations for 'Yield
2519 if (Nkind_In (N, N_Abstract_Subprogram_Declaration,
2520 N_Entry_Declaration,
2521 N_Generic_Subprogram_Declaration,
2522 N_Subprogram_Declaration)
2523 or else Nkind (N) in N_Formal_Subprogram_Declaration)
2524 and then not Within_Protected_Type (E)
2528 elsif Within_Protected_Type (E) then
2530 ("aspect% not applicable to protected operations", Id);
2535 ("aspect% only applicable to subprogram and entry "
2536 & "declarations", Id);
2540 -- Evaluate its static expression (if available); otherwise it
2541 -- defaults to True.
2546 -- Otherwise it must have a static boolean expression
2549 if Inside_A_Generic then
2550 Preanalyze_And_Resolve (Expr, Any_Boolean);
2552 Analyze_And_Resolve (Expr, Any_Boolean);
2555 if Is_OK_Static_Expression (Expr) then
2556 if Is_True (Static_Boolean (Expr)) then
2561 ("expression of aspect % must be static", Aspect);
2567 -- Adding minimum decoration to generic subprograms to set
2568 -- the Yield attribute (since at this stage it may not be
2569 -- set; see Analyze_Generic_Subprogram_Declaration).
2571 if Nkind (N) in N_Generic_Subprogram_Declaration
2572 and then Ekind (E) = E_Void
2574 if Nkind (Specification (N)) = N_Function_Specification
2576 Set_Ekind (E, E_Function);
2578 Set_Ekind (E, E_Procedure);
2582 Set_Has_Yield_Aspect (E);
2585 -- If the Yield aspect is specified for a dispatching
2586 -- subprogram that inherits the aspect, the specified
2587 -- value shall be confirming.
2590 and then Is_Dispatching_Operation (E)
2591 and then Present (Overridden_Operation (E))
2592 and then Has_Yield_Aspect (Overridden_Operation (E))
2593 /= Is_True (Static_Boolean (Expr))
2595 Error_Msg_N ("specification of inherited aspect% can only " &
2596 "confirm parent value", Id);
2598 end Analyze_Aspect_Yield;
2600 -----------------------
2601 -- Make_Aitem_Pragma --
2602 -----------------------
2604 procedure Make_Aitem_Pragma
2605 (Pragma_Argument_Associations : List_Id;
2606 Pragma_Name : Name_Id)
2608 Args : List_Id := Pragma_Argument_Associations;
2611 -- We should never get here if aspect was disabled
2613 pragma Assert (not Is_Disabled (Aspect));
2615 -- Certain aspects allow for an optional name or expression. Do
2616 -- not generate a pragma with empty argument association list.
2618 if No (Args) or else No (Expression (First (Args))) then
2626 Pragma_Argument_Associations => Args,
2627 Pragma_Identifier =>
2628 Make_Identifier (Sloc (Id), Pragma_Name),
2629 Class_Present => Class_Present (Aspect),
2630 Split_PPC => Split_PPC (Aspect));
2632 -- Set additional semantic fields
2634 if Is_Ignored (Aspect) then
2635 Set_Is_Ignored (Aitem);
2636 elsif Is_Checked (Aspect) then
2637 Set_Is_Checked (Aitem);
2640 Set_Corresponding_Aspect (Aitem, Aspect);
2641 Set_From_Aspect_Specification (Aitem);
2642 end Make_Aitem_Pragma;
2644 -- Start of processing for Analyze_One_Aspect
2647 -- Skip aspect if already analyzed, to avoid looping in some cases
2649 if Analyzed (Aspect) then
2653 -- Skip looking at aspect if it is totally disabled. Just mark it
2654 -- as such for later reference in the tree. This also sets the
2655 -- Is_Ignored and Is_Checked flags appropriately.
2657 Check_Applicable_Policy (Aspect);
2659 if Is_Disabled (Aspect) then
2663 -- Set the source location of expression, used in the case of
2664 -- a failed precondition/postcondition or invariant. Note that
2665 -- the source location of the expression is not usually the best
2666 -- choice here. For example, it gets located on the last AND
2667 -- keyword in a chain of boolean expressiond AND'ed together.
2668 -- It is best to put the message on the first character of the
2669 -- assertion, which is the effect of the First_Node call here.
2671 if Present (Expr) then
2672 Eloc := Sloc (First_Node (Expr));
2675 -- Check restriction No_Implementation_Aspect_Specifications
2677 if Implementation_Defined_Aspect (A_Id) then
2679 (No_Implementation_Aspect_Specifications, Aspect);
2682 -- Check restriction No_Specification_Of_Aspect
2684 Check_Restriction_No_Specification_Of_Aspect (Aspect);
2686 -- Mark aspect analyzed (actual analysis is delayed till later)
2688 Set_Analyzed (Aspect);
2689 Set_Entity (Aspect, E);
2691 -- Build the reference to E that will be used in the built pragmas
2693 Ent := New_Occurrence_Of (E, Sloc (Id));
2695 if A_Id = Aspect_Attach_Handler
2696 or else A_Id = Aspect_Interrupt_Handler
2699 -- Treat the specification as a reference to the protected
2700 -- operation, which might otherwise appear unreferenced and
2701 -- generate spurious warnings.
2703 Generate_Reference (E, Id);
2706 -- Check for duplicate aspect. Note that the Comes_From_Source
2707 -- test allows duplicate Pre/Post's that we generate internally
2708 -- to escape being flagged here.
2710 if No_Duplicates_Allowed (A_Id) then
2712 while Anod /= Aspect loop
2713 if Comes_From_Source (Aspect)
2714 and then Same_Aspect (A_Id, Get_Aspect_Id (Anod))
2716 Error_Msg_Name_1 := Nam;
2717 Error_Msg_Sloc := Sloc (Anod);
2719 -- Case of same aspect specified twice
2721 if Class_Present (Anod) = Class_Present (Aspect) then
2722 if not Class_Present (Anod) then
2724 ("aspect% for & previously given#",
2728 ("aspect `%''Class` for & previously given#",
2738 -- Check some general restrictions on language defined aspects
2740 if not Implementation_Defined_Aspect (A_Id)
2741 or else A_Id = Aspect_Async_Readers
2742 or else A_Id = Aspect_Async_Writers
2743 or else A_Id = Aspect_Effective_Reads
2744 or else A_Id = Aspect_Effective_Reads
2746 Error_Msg_Name_1 := Nam;
2748 -- Not allowed for renaming declarations. Examine the original
2749 -- node because a subprogram renaming may have been rewritten
2752 if Nkind (Original_Node (N)) in N_Renaming_Declaration then
2754 ("aspect % not allowed for renaming declaration",
2758 -- Not allowed for formal type declarations in previous
2759 -- versions of the language. Allowed for them only for
2760 -- shared variable control aspects.
2762 if Nkind (N) = N_Formal_Type_Declaration then
2763 if Ada_Version < Ada_2020 then
2765 ("aspect % not allowed for formal type declaration",
2768 elsif A_Id /= Aspect_Atomic
2769 and then A_Id /= Aspect_Volatile
2770 and then A_Id /= Aspect_Independent
2771 and then A_Id /= Aspect_Atomic_Components
2772 and then A_Id /= Aspect_Independent_Components
2773 and then A_Id /= Aspect_Volatile_Components
2774 and then A_Id /= Aspect_Async_Readers
2775 and then A_Id /= Aspect_Async_Writers
2776 and then A_Id /= Aspect_Effective_Reads
2777 and then A_Id /= Aspect_Effective_Reads
2780 ("aspect % not allowed for formal type declaration",
2786 -- Copy expression for later processing by the procedures
2787 -- Check_Aspect_At_[Freeze_Point | End_Of_Declarations]
2789 Set_Entity (Id, New_Copy_Tree (Expr));
2791 -- Set Delay_Required as appropriate to aspect
2793 case Aspect_Delay (A_Id) is
2794 when Always_Delay =>
2795 Delay_Required := True;
2798 Delay_Required := False;
2802 -- If expression has the form of an integer literal, then
2803 -- do not delay, since we know the value cannot change.
2804 -- This optimization catches most rep clause cases.
2806 -- For Boolean aspects, don't delay if no expression
2808 if A_Id in Boolean_Aspects and then No (Expr) then
2809 Delay_Required := False;
2811 -- For non-Boolean aspects, don't delay if integer literal,
2812 -- unless the aspect is Alignment, which affects the
2813 -- freezing of an initialized object.
2815 elsif A_Id not in Boolean_Aspects
2816 and then A_Id /= Aspect_Alignment
2817 and then Present (Expr)
2818 and then Nkind (Expr) = N_Integer_Literal
2820 Delay_Required := False;
2822 -- All other cases are delayed
2825 Delay_Required := True;
2826 Set_Has_Delayed_Rep_Aspects (E);
2830 -- Check 13.1(9.2/5): A representation aspect of a subtype or type
2831 -- shall not be specified (whether by a representation item or an
2832 -- aspect_specification) before the type is completely defined
2835 if Is_Representation_Aspect (A_Id)
2836 and then Rep_Item_Too_Early (E, N)
2841 -- Processing based on specific aspect
2844 when Aspect_Unimplemented =>
2845 null; -- ??? temp for now
2847 -- No_Aspect should be impossible
2850 raise Program_Error;
2852 -- Case 1: Aspects corresponding to attribute definition
2858 | Aspect_Component_Size
2859 | Aspect_Constant_Indexing
2860 | Aspect_Default_Iterator
2861 | Aspect_Dispatching_Domain
2862 | Aspect_External_Tag
2865 | Aspect_Iterator_Element
2866 | Aspect_Machine_Radix
2867 | Aspect_Object_Size
2871 | Aspect_Scalar_Storage_Order
2872 | Aspect_Simple_Storage_Pool
2875 | Aspect_Storage_Pool
2876 | Aspect_Stream_Size
2878 | Aspect_Variable_Indexing
2881 -- Indexing aspects apply only to tagged type
2883 if (A_Id = Aspect_Constant_Indexing
2885 A_Id = Aspect_Variable_Indexing)
2886 and then not (Is_Type (E)
2887 and then Is_Tagged_Type (E))
2890 ("indexing aspect can only apply to a tagged type",
2895 -- For the case of aspect Address, we don't consider that we
2896 -- know the entity is never set in the source, since it is
2897 -- is likely aliasing is occurring.
2899 -- Note: one might think that the analysis of the resulting
2900 -- attribute definition clause would take care of that, but
2901 -- that's not the case since it won't be from source.
2903 if A_Id = Aspect_Address then
2904 Set_Never_Set_In_Source (E, False);
2907 -- Correctness of the profile of a stream operation is
2908 -- verified at the freeze point, but we must detect the
2909 -- illegal specification of this aspect for a subtype now,
2910 -- to prevent malformed rep_item chains.
2912 if A_Id = Aspect_Input or else
2913 A_Id = Aspect_Output or else
2914 A_Id = Aspect_Read or else
2917 if not Is_First_Subtype (E) then
2919 ("local name must be a first subtype", Aspect);
2922 -- If stream aspect applies to the class-wide type,
2923 -- the generated attribute definition applies to the
2924 -- class-wide type as well.
2926 elsif Class_Present (Aspect) then
2928 Make_Attribute_Reference (Loc,
2930 Attribute_Name => Name_Class);
2934 -- Construct the attribute_definition_clause. The expression
2935 -- in the aspect specification is simply shared with the
2936 -- constructed attribute, because it will be fully analyzed
2937 -- when the attribute is processed.
2940 Make_Attribute_Definition_Clause (Loc,
2942 Chars => Chars (Id),
2943 Expression => Relocate_Node (Expr));
2945 -- If the address is specified, then we treat the entity as
2946 -- referenced, to avoid spurious warnings. This is analogous
2947 -- to what is done with an attribute definition clause, but
2948 -- here we don't want to generate a reference because this
2949 -- is the point of definition of the entity.
2951 if A_Id = Aspect_Address then
2955 -- Case 2: Aspects corresponding to pragmas
2957 -- Case 2a: Aspects corresponding to pragmas with two
2958 -- arguments, where the first argument is a local name
2959 -- referring to the entity, and the second argument is the
2960 -- aspect definition expression.
2962 -- Linker_Section/Suppress/Unsuppress
2964 when Aspect_Linker_Section
2969 (Pragma_Argument_Associations => New_List (
2970 Make_Pragma_Argument_Association (Loc,
2971 Expression => New_Occurrence_Of (E, Loc)),
2972 Make_Pragma_Argument_Association (Sloc (Expr),
2973 Expression => Relocate_Node (Expr))),
2974 Pragma_Name => Chars (Id));
2976 -- Linker_Section does not need delaying, as its argument
2977 -- must be a static string. Furthermore, if applied to
2978 -- an object with an explicit initialization, the object
2979 -- must be frozen in order to elaborate the initialization
2980 -- code. (This is already done for types with implicit
2981 -- initialization, such as protected types.)
2983 if A_Id = Aspect_Linker_Section
2984 and then Nkind (N) = N_Object_Declaration
2985 and then Has_Init_Expression (N)
2987 Delay_Required := False;
2992 -- Corresponds to pragma Implemented, construct the pragma
2994 when Aspect_Synchronization =>
2996 (Pragma_Argument_Associations => New_List (
2997 Make_Pragma_Argument_Association (Loc,
2998 Expression => New_Occurrence_Of (E, Loc)),
2999 Make_Pragma_Argument_Association (Sloc (Expr),
3000 Expression => Relocate_Node (Expr))),
3001 Pragma_Name => Name_Implemented);
3005 when Aspect_Attach_Handler =>
3007 (Pragma_Argument_Associations => New_List (
3008 Make_Pragma_Argument_Association (Sloc (Ent),
3010 Make_Pragma_Argument_Association (Sloc (Expr),
3011 Expression => Relocate_Node (Expr))),
3012 Pragma_Name => Name_Attach_Handler);
3014 -- We need to insert this pragma into the tree to get proper
3015 -- processing and to look valid from a placement viewpoint.
3017 Insert_Pragma (Aitem);
3020 -- Dynamic_Predicate, Predicate, Static_Predicate
3022 when Aspect_Dynamic_Predicate
3024 | Aspect_Static_Predicate
3026 -- These aspects apply only to subtypes
3028 if not Is_Type (E) then
3030 ("predicate can only be specified for a subtype",
3034 elsif Is_Incomplete_Type (E) then
3036 ("predicate cannot apply to incomplete view", Aspect);
3038 elsif Is_Generic_Type (E) then
3040 ("predicate cannot apply to formal type", Aspect);
3044 -- Construct the pragma (always a pragma Predicate, with
3045 -- flags recording whether it is static/dynamic). We also
3046 -- set flags recording this in the type itself.
3049 (Pragma_Argument_Associations => New_List (
3050 Make_Pragma_Argument_Association (Sloc (Ent),
3052 Make_Pragma_Argument_Association (Sloc (Expr),
3053 Expression => Relocate_Node (Expr))),
3054 Pragma_Name => Name_Predicate);
3056 -- Mark type has predicates, and remember what kind of
3057 -- aspect lead to this predicate (we need this to access
3058 -- the right set of check policies later on).
3060 Set_Has_Predicates (E);
3062 if A_Id = Aspect_Dynamic_Predicate then
3063 Set_Has_Dynamic_Predicate_Aspect (E);
3065 -- If the entity has a dynamic predicate, any inherited
3066 -- static predicate becomes dynamic as well, and the
3067 -- predicate function includes the conjunction of both.
3069 Set_Has_Static_Predicate_Aspect (E, False);
3071 elsif A_Id = Aspect_Static_Predicate then
3072 Set_Has_Static_Predicate_Aspect (E);
3075 -- If the type is private, indicate that its completion
3076 -- has a freeze node, because that is the one that will
3077 -- be visible at freeze time.
3079 if Is_Private_Type (E) and then Present (Full_View (E)) then
3080 Set_Has_Predicates (Full_View (E));
3082 if A_Id = Aspect_Dynamic_Predicate then
3083 Set_Has_Dynamic_Predicate_Aspect (Full_View (E));
3084 elsif A_Id = Aspect_Static_Predicate then
3085 Set_Has_Static_Predicate_Aspect (Full_View (E));
3088 Set_Has_Delayed_Aspects (Full_View (E));
3089 Ensure_Freeze_Node (Full_View (E));
3091 -- If there is an Underlying_Full_View, also create a
3092 -- freeze node for that one.
3094 if Is_Private_Type (Full_View (E)) then
3096 U_Full : constant Entity_Id :=
3097 Underlying_Full_View (Full_View (E));
3099 if Present (U_Full) then
3100 Set_Has_Delayed_Aspects (U_Full);
3101 Ensure_Freeze_Node (U_Full);
3107 -- Predicate_Failure
3109 when Aspect_Predicate_Failure =>
3111 -- This aspect applies only to subtypes
3113 if not Is_Type (E) then
3115 ("predicate can only be specified for a subtype",
3119 elsif Is_Incomplete_Type (E) then
3121 ("predicate cannot apply to incomplete view", Aspect);
3124 elsif not Has_Predicates (E) then
3126 ("Predicate_Failure requires previous predicate" &
3127 " specification", Aspect);
3131 -- Construct the pragma
3134 (Pragma_Argument_Associations => New_List (
3135 Make_Pragma_Argument_Association (Sloc (Ent),
3137 Make_Pragma_Argument_Association (Sloc (Expr),
3138 Expression => Relocate_Node (Expr))),
3139 Pragma_Name => Name_Predicate_Failure);
3141 -- Case 2b: Aspects corresponding to pragmas with two
3142 -- arguments, where the second argument is a local name
3143 -- referring to the entity, and the first argument is the
3144 -- aspect definition expression.
3148 when Aspect_Convention =>
3149 Analyze_Aspect_Convention;
3152 -- External_Name, Link_Name
3154 when Aspect_External_Name
3157 Analyze_Aspect_External_Link_Name;
3160 -- CPU, Interrupt_Priority, Priority
3162 -- These three aspects can be specified for a subprogram spec
3163 -- or body, in which case we analyze the expression and export
3164 -- the value of the aspect.
3166 -- Previously, we generated an equivalent pragma for bodies
3167 -- (note that the specs cannot contain these pragmas). The
3168 -- pragma was inserted ahead of local declarations, rather than
3169 -- after the body. This leads to a certain duplication between
3170 -- the processing performed for the aspect and the pragma, but
3171 -- given the straightforward handling required it is simpler
3172 -- to duplicate than to translate the aspect in the spec into
3173 -- a pragma in the declarative part of the body.
3176 | Aspect_Interrupt_Priority
3179 if Nkind_In (N, N_Subprogram_Body,
3180 N_Subprogram_Declaration)
3182 -- Analyze the aspect expression
3184 Analyze_And_Resolve (Expr, Standard_Integer);
3186 -- Interrupt_Priority aspect not allowed for main
3187 -- subprograms. RM D.1 does not forbid this explicitly,
3188 -- but RM J.15.11(6/3) does not permit pragma
3189 -- Interrupt_Priority for subprograms.
3191 if A_Id = Aspect_Interrupt_Priority then
3193 ("Interrupt_Priority aspect cannot apply to "
3194 & "subprogram", Expr);
3196 -- The expression must be static
3198 elsif not Is_OK_Static_Expression (Expr) then
3199 Flag_Non_Static_Expr
3200 ("aspect requires static expression!", Expr);
3202 -- Check whether this is the main subprogram. Issue a
3203 -- warning only if it is obviously not a main program
3204 -- (when it has parameters or when the subprogram is
3205 -- within a package).
3207 elsif Present (Parameter_Specifications
3208 (Specification (N)))
3209 or else not Is_Compilation_Unit (Defining_Entity (N))
3211 -- See RM D.1(14/3) and D.16(12/3)
3214 ("aspect applied to subprogram other than the "
3215 & "main subprogram has no effect??", Expr);
3217 -- Otherwise check in range and export the value
3219 -- For the CPU aspect
3221 elsif A_Id = Aspect_CPU then
3222 if Is_In_Range (Expr, RTE (RE_CPU_Range)) then
3224 -- Value is correct so we export the value to make
3225 -- it available at execution time.
3228 (Main_Unit, UI_To_Int (Expr_Value (Expr)));
3232 ("main subprogram CPU is out of range", Expr);
3235 -- For the Priority aspect
3237 elsif A_Id = Aspect_Priority then
3238 if Is_In_Range (Expr, RTE (RE_Priority)) then
3240 -- Value is correct so we export the value to make
3241 -- it available at execution time.
3244 (Main_Unit, UI_To_Int (Expr_Value (Expr)));
3246 -- Ignore pragma if Relaxed_RM_Semantics to support
3247 -- other targets/non GNAT compilers.
3249 elsif not Relaxed_RM_Semantics then
3251 ("main subprogram priority is out of range",
3256 -- Load an arbitrary entity from System.Tasking.Stages
3257 -- or System.Tasking.Restricted.Stages (depending on
3258 -- the supported profile) to make sure that one of these
3259 -- packages is implicitly with'ed, since we need to have
3260 -- the tasking run time active for the pragma Priority to
3261 -- have any effect. Previously we with'ed the package
3262 -- System.Tasking, but this package does not trigger the
3263 -- required initialization of the run-time library.
3266 Discard : Entity_Id;
3268 if Restricted_Profile then
3269 Discard := RTE (RE_Activate_Restricted_Tasks);
3271 Discard := RTE (RE_Activate_Tasks);
3275 -- Handling for these aspects in subprograms is complete
3279 -- For task and protected types pass the aspect as an
3284 Make_Attribute_Definition_Clause (Loc,
3286 Chars => Chars (Id),
3287 Expression => Relocate_Node (Expr));
3292 when Aspect_Warnings =>
3294 (Pragma_Argument_Associations => New_List (
3295 Make_Pragma_Argument_Association (Sloc (Expr),
3296 Expression => Relocate_Node (Expr)),
3297 Make_Pragma_Argument_Association (Loc,
3298 Expression => New_Occurrence_Of (E, Loc))),
3299 Pragma_Name => Chars (Id));
3301 Decorate (Aspect, Aitem);
3302 Insert_Pragma (Aitem);
3305 -- Case 2c: Aspects corresponding to pragmas with three
3308 -- Invariant aspects have a first argument that references the
3309 -- entity, a second argument that is the expression and a third
3310 -- argument that is an appropriate message.
3312 -- Invariant, Type_Invariant
3314 when Aspect_Invariant
3315 | Aspect_Type_Invariant
3317 -- Analysis of the pragma will verify placement legality:
3318 -- an invariant must apply to a private type, or appear in
3319 -- the private part of a spec and apply to a completion.
3322 (Pragma_Argument_Associations => New_List (
3323 Make_Pragma_Argument_Association (Sloc (Ent),
3325 Make_Pragma_Argument_Association (Sloc (Expr),
3326 Expression => Relocate_Node (Expr))),
3327 Pragma_Name => Name_Invariant);
3329 -- Add message unless exception messages are suppressed
3331 if not Opt.Exception_Locations_Suppressed then
3332 Append_To (Pragma_Argument_Associations (Aitem),
3333 Make_Pragma_Argument_Association (Eloc,
3334 Chars => Name_Message,
3336 Make_String_Literal (Eloc,
3337 Strval => "failed invariant from "
3338 & Build_Location_String (Eloc))));
3341 -- For Invariant case, insert immediately after the entity
3342 -- declaration. We do not have to worry about delay issues
3343 -- since the pragma processing takes care of this.
3345 Delay_Required := False;
3347 -- Case 2d : Aspects that correspond to a pragma with one
3352 -- Aspect Abstract_State introduces implicit declarations for
3353 -- all state abstraction entities it defines. To emulate this
3354 -- behavior, insert the pragma at the beginning of the visible
3355 -- declarations of the related package so that it is analyzed
3358 when Aspect_Abstract_State => Abstract_State : declare
3359 Context : Node_Id := N;
3362 -- When aspect Abstract_State appears on a generic package,
3363 -- it is propageted to the package instance. The context in
3364 -- this case is the instance spec.
3366 if Nkind (Context) = N_Package_Instantiation then
3367 Context := Instance_Spec (Context);
3370 if Nkind_In (Context, N_Generic_Package_Declaration,
3371 N_Package_Declaration)
3374 (Pragma_Argument_Associations => New_List (
3375 Make_Pragma_Argument_Association (Loc,
3376 Expression => Relocate_Node (Expr))),
3377 Pragma_Name => Name_Abstract_State);
3379 Decorate (Aspect, Aitem);
3383 Is_Generic_Instance (Defining_Entity (Context)));
3387 ("aspect & must apply to a package declaration",
3394 -- Aspect Async_Readers is never delayed because it is
3395 -- equivalent to a source pragma which appears after the
3396 -- related object declaration.
3398 when Aspect_Async_Readers =>
3400 (Pragma_Argument_Associations => New_List (
3401 Make_Pragma_Argument_Association (Loc,
3402 Expression => Relocate_Node (Expr))),
3403 Pragma_Name => Name_Async_Readers);
3405 Decorate (Aspect, Aitem);
3406 Insert_Pragma (Aitem);
3409 -- Aspect Async_Writers is never delayed because it is
3410 -- equivalent to a source pragma which appears after the
3411 -- related object declaration.
3413 when Aspect_Async_Writers =>
3415 (Pragma_Argument_Associations => New_List (
3416 Make_Pragma_Argument_Association (Loc,
3417 Expression => Relocate_Node (Expr))),
3418 Pragma_Name => Name_Async_Writers);
3420 Decorate (Aspect, Aitem);
3421 Insert_Pragma (Aitem);
3424 -- Aspect Constant_After_Elaboration is never delayed because
3425 -- it is equivalent to a source pragma which appears after the
3426 -- related object declaration.
3428 when Aspect_Constant_After_Elaboration =>
3430 (Pragma_Argument_Associations => New_List (
3431 Make_Pragma_Argument_Association (Loc,
3432 Expression => Relocate_Node (Expr))),
3434 Name_Constant_After_Elaboration);
3436 Decorate (Aspect, Aitem);
3437 Insert_Pragma (Aitem);
3440 -- Aspect Default_Internal_Condition is never delayed because
3441 -- it is equivalent to a source pragma which appears after the
3442 -- related private type. To deal with forward references, the
3443 -- generated pragma is stored in the rep chain of the related
3444 -- private type as types do not carry contracts. The pragma is
3445 -- wrapped inside of a procedure at the freeze point of the
3446 -- private type's full view.
3448 when Aspect_Default_Initial_Condition =>
3450 (Pragma_Argument_Associations => New_List (
3451 Make_Pragma_Argument_Association (Loc,
3452 Expression => Relocate_Node (Expr))),
3454 Name_Default_Initial_Condition);
3456 Decorate (Aspect, Aitem);
3457 Insert_Pragma (Aitem);
3460 -- Default_Storage_Pool
3462 when Aspect_Default_Storage_Pool =>
3464 (Pragma_Argument_Associations => New_List (
3465 Make_Pragma_Argument_Association (Loc,
3466 Expression => Relocate_Node (Expr))),
3468 Name_Default_Storage_Pool);
3470 Decorate (Aspect, Aitem);
3471 Insert_Pragma (Aitem);
3476 -- Aspect Depends is never delayed because it is equivalent to
3477 -- a source pragma which appears after the related subprogram.
3478 -- To deal with forward references, the generated pragma is
3479 -- stored in the contract of the related subprogram and later
3480 -- analyzed at the end of the declarative region. See routine
3481 -- Analyze_Depends_In_Decl_Part for details.
3483 when Aspect_Depends =>
3485 (Pragma_Argument_Associations => New_List (
3486 Make_Pragma_Argument_Association (Loc,
3487 Expression => Relocate_Node (Expr))),
3488 Pragma_Name => Name_Depends);
3490 Decorate (Aspect, Aitem);
3491 Insert_Pragma (Aitem);
3494 -- Aspect Effective_Reads is never delayed because it is
3495 -- equivalent to a source pragma which appears after the
3496 -- related object declaration.
3498 when Aspect_Effective_Reads =>
3500 (Pragma_Argument_Associations => New_List (
3501 Make_Pragma_Argument_Association (Loc,
3502 Expression => Relocate_Node (Expr))),
3503 Pragma_Name => Name_Effective_Reads);
3505 Decorate (Aspect, Aitem);
3506 Insert_Pragma (Aitem);
3509 -- Aspect Effective_Writes is never delayed because it is
3510 -- equivalent to a source pragma which appears after the
3511 -- related object declaration.
3513 when Aspect_Effective_Writes =>
3515 (Pragma_Argument_Associations => New_List (
3516 Make_Pragma_Argument_Association (Loc,
3517 Expression => Relocate_Node (Expr))),
3518 Pragma_Name => Name_Effective_Writes);
3520 Decorate (Aspect, Aitem);
3521 Insert_Pragma (Aitem);
3524 -- Aspect Extensions_Visible is never delayed because it is
3525 -- equivalent to a source pragma which appears after the
3526 -- related subprogram.
3528 when Aspect_Extensions_Visible =>
3530 (Pragma_Argument_Associations => New_List (
3531 Make_Pragma_Argument_Association (Loc,
3532 Expression => Relocate_Node (Expr))),
3533 Pragma_Name => Name_Extensions_Visible);
3535 Decorate (Aspect, Aitem);
3536 Insert_Pragma (Aitem);
3539 -- Aspect Ghost is never delayed because it is equivalent to a
3540 -- source pragma which appears at the top of [generic] package
3541 -- declarations or after an object, a [generic] subprogram, or
3542 -- a type declaration.
3544 when Aspect_Ghost =>
3546 (Pragma_Argument_Associations => New_List (
3547 Make_Pragma_Argument_Association (Loc,
3548 Expression => Relocate_Node (Expr))),
3549 Pragma_Name => Name_Ghost);
3551 Decorate (Aspect, Aitem);
3552 Insert_Pragma (Aitem);
3557 -- Aspect Global is never delayed because it is equivalent to
3558 -- a source pragma which appears after the related subprogram.
3559 -- To deal with forward references, the generated pragma is
3560 -- stored in the contract of the related subprogram and later
3561 -- analyzed at the end of the declarative region. See routine
3562 -- Analyze_Global_In_Decl_Part for details.
3564 when Aspect_Global =>
3566 (Pragma_Argument_Associations => New_List (
3567 Make_Pragma_Argument_Association (Loc,
3568 Expression => Relocate_Node (Expr))),
3569 Pragma_Name => Name_Global);
3571 Decorate (Aspect, Aitem);
3572 Insert_Pragma (Aitem);
3575 -- Initial_Condition
3577 -- Aspect Initial_Condition is never delayed because it is
3578 -- equivalent to a source pragma which appears after the
3579 -- related package. To deal with forward references, the
3580 -- generated pragma is stored in the contract of the related
3581 -- package and later analyzed at the end of the declarative
3582 -- region. See routine Analyze_Initial_Condition_In_Decl_Part
3585 when Aspect_Initial_Condition => Initial_Condition : declare
3586 Context : Node_Id := N;
3589 -- When aspect Initial_Condition appears on a generic
3590 -- package, it is propageted to the package instance. The
3591 -- context in this case is the instance spec.
3593 if Nkind (Context) = N_Package_Instantiation then
3594 Context := Instance_Spec (Context);
3597 if Nkind_In (Context, N_Generic_Package_Declaration,
3598 N_Package_Declaration)
3601 (Pragma_Argument_Associations => New_List (
3602 Make_Pragma_Argument_Association (Loc,
3603 Expression => Relocate_Node (Expr))),
3605 Name_Initial_Condition);
3607 Decorate (Aspect, Aitem);
3611 Is_Generic_Instance (Defining_Entity (Context)));
3613 -- Otherwise the context is illegal
3617 ("aspect & must apply to a package declaration",
3622 end Initial_Condition;
3626 -- Aspect Initializes is never delayed because it is equivalent
3627 -- to a source pragma appearing after the related package. To
3628 -- deal with forward references, the generated pragma is stored
3629 -- in the contract of the related package and later analyzed at
3630 -- the end of the declarative region. For details, see routine
3631 -- Analyze_Initializes_In_Decl_Part.
3633 when Aspect_Initializes => Initializes : declare
3634 Context : Node_Id := N;
3637 -- When aspect Initializes appears on a generic package,
3638 -- it is propageted to the package instance. The context
3639 -- in this case is the instance spec.
3641 if Nkind (Context) = N_Package_Instantiation then
3642 Context := Instance_Spec (Context);
3645 if Nkind_In (Context, N_Generic_Package_Declaration,
3646 N_Package_Declaration)
3649 (Pragma_Argument_Associations => New_List (
3650 Make_Pragma_Argument_Association (Loc,
3651 Expression => Relocate_Node (Expr))),
3652 Pragma_Name => Name_Initializes);
3654 Decorate (Aspect, Aitem);
3658 Is_Generic_Instance (Defining_Entity (Context)));
3660 -- Otherwise the context is illegal
3664 ("aspect & must apply to a package declaration",
3671 -- Max_Entry_Queue_Depth
3673 when Aspect_Max_Entry_Queue_Depth =>
3675 (Pragma_Argument_Associations => New_List (
3676 Make_Pragma_Argument_Association (Loc,
3677 Expression => Relocate_Node (Expr))),
3678 Pragma_Name => Name_Max_Entry_Queue_Depth);
3680 Decorate (Aspect, Aitem);
3681 Insert_Pragma (Aitem);
3684 -- Max_Entry_Queue_Length
3686 when Aspect_Max_Entry_Queue_Length =>
3688 (Pragma_Argument_Associations => New_List (
3689 Make_Pragma_Argument_Association (Loc,
3690 Expression => Relocate_Node (Expr))),
3691 Pragma_Name => Name_Max_Entry_Queue_Length);
3693 Decorate (Aspect, Aitem);
3694 Insert_Pragma (Aitem);
3699 when Aspect_Max_Queue_Length =>
3701 (Pragma_Argument_Associations => New_List (
3702 Make_Pragma_Argument_Association (Loc,
3703 Expression => Relocate_Node (Expr))),
3704 Pragma_Name => Name_Max_Queue_Length);
3706 Decorate (Aspect, Aitem);
3707 Insert_Pragma (Aitem);
3710 -- Aspect No_Caching is never delayed because it is equivalent
3711 -- to a source pragma which appears after the related object
3714 when Aspect_No_Caching =>
3716 (Pragma_Argument_Associations => New_List (
3717 Make_Pragma_Argument_Association (Loc,
3718 Expression => Relocate_Node (Expr))),
3719 Pragma_Name => Name_No_Caching);
3721 Decorate (Aspect, Aitem);
3722 Insert_Pragma (Aitem);
3727 when Aspect_Obsolescent => declare
3735 Make_Pragma_Argument_Association (Sloc (Expr),
3736 Expression => Relocate_Node (Expr)));
3740 (Pragma_Argument_Associations => Args,
3741 Pragma_Name => Chars (Id));
3746 when Aspect_Part_Of =>
3747 if Nkind_In (N, N_Object_Declaration,
3748 N_Package_Instantiation)
3749 or else Is_Single_Concurrent_Type_Declaration (N)
3752 (Pragma_Argument_Associations => New_List (
3753 Make_Pragma_Argument_Association (Loc,
3754 Expression => Relocate_Node (Expr))),
3755 Pragma_Name => Name_Part_Of);
3757 Decorate (Aspect, Aitem);
3758 Insert_Pragma (Aitem);
3762 ("aspect & must apply to package instantiation, "
3763 & "object, single protected type or single task type",
3771 when Aspect_SPARK_Mode =>
3773 (Pragma_Argument_Associations => New_List (
3774 Make_Pragma_Argument_Association (Loc,
3775 Expression => Relocate_Node (Expr))),
3776 Pragma_Name => Name_SPARK_Mode);
3778 Decorate (Aspect, Aitem);
3779 Insert_Pragma (Aitem);
3784 -- Aspect Refined_Depends is never delayed because it is
3785 -- equivalent to a source pragma which appears in the
3786 -- declarations of the related subprogram body. To deal with
3787 -- forward references, the generated pragma is stored in the
3788 -- contract of the related subprogram body and later analyzed
3789 -- at the end of the declarative region. For details, see
3790 -- routine Analyze_Refined_Depends_In_Decl_Part.
3792 when Aspect_Refined_Depends =>
3794 (Pragma_Argument_Associations => New_List (
3795 Make_Pragma_Argument_Association (Loc,
3796 Expression => Relocate_Node (Expr))),
3797 Pragma_Name => Name_Refined_Depends);
3799 Decorate (Aspect, Aitem);
3800 Insert_Pragma (Aitem);
3805 -- Aspect Refined_Global is never delayed because it is
3806 -- equivalent to a source pragma which appears in the
3807 -- declarations of the related subprogram body. To deal with
3808 -- forward references, the generated pragma is stored in the
3809 -- contract of the related subprogram body and later analyzed
3810 -- at the end of the declarative region. For details, see
3811 -- routine Analyze_Refined_Global_In_Decl_Part.
3813 when Aspect_Refined_Global =>
3815 (Pragma_Argument_Associations => New_List (
3816 Make_Pragma_Argument_Association (Loc,
3817 Expression => Relocate_Node (Expr))),
3818 Pragma_Name => Name_Refined_Global);
3820 Decorate (Aspect, Aitem);
3821 Insert_Pragma (Aitem);
3826 when Aspect_Refined_Post =>
3828 (Pragma_Argument_Associations => New_List (
3829 Make_Pragma_Argument_Association (Loc,
3830 Expression => Relocate_Node (Expr))),
3831 Pragma_Name => Name_Refined_Post);
3833 Decorate (Aspect, Aitem);
3834 Insert_Pragma (Aitem);
3839 when Aspect_Refined_State =>
3841 -- The corresponding pragma for Refined_State is inserted in
3842 -- the declarations of the related package body. This action
3843 -- synchronizes both the source and from-aspect versions of
3846 if Nkind (N) = N_Package_Body then
3848 (Pragma_Argument_Associations => New_List (
3849 Make_Pragma_Argument_Association (Loc,
3850 Expression => Relocate_Node (Expr))),
3851 Pragma_Name => Name_Refined_State);
3853 Decorate (Aspect, Aitem);
3854 Insert_Pragma (Aitem);
3856 -- Otherwise the context is illegal
3860 ("aspect & must apply to a package body", Aspect, Id);
3865 -- Relative_Deadline
3867 when Aspect_Relative_Deadline =>
3869 (Pragma_Argument_Associations => New_List (
3870 Make_Pragma_Argument_Association (Loc,
3871 Expression => Relocate_Node (Expr))),
3872 Pragma_Name => Name_Relative_Deadline);
3874 -- If the aspect applies to a task, the corresponding pragma
3875 -- must appear within its declarations, not after.
3877 if Nkind (N) = N_Task_Type_Declaration then
3883 if No (Task_Definition (N)) then
3884 Set_Task_Definition (N,
3885 Make_Task_Definition (Loc,
3886 Visible_Declarations => New_List,
3887 End_Label => Empty));
3890 Def := Task_Definition (N);
3891 V := Visible_Declarations (Def);
3892 if not Is_Empty_List (V) then
3893 Insert_Before (First (V), Aitem);
3896 Set_Visible_Declarations (Def, New_List (Aitem));
3903 -- Relaxed_Initialization
3905 when Aspect_Relaxed_Initialization =>
3906 Analyze_Aspect_Relaxed_Initialization;
3909 -- Secondary_Stack_Size
3911 -- Aspect Secondary_Stack_Size needs to be converted into a
3912 -- pragma for two reasons: the attribute is not analyzed until
3913 -- after the expansion of the task type declaration and the
3914 -- attribute does not have visibility on the discriminant.
3916 when Aspect_Secondary_Stack_Size =>
3918 (Pragma_Argument_Associations => New_List (
3919 Make_Pragma_Argument_Association (Loc,
3920 Expression => Relocate_Node (Expr))),
3922 Name_Secondary_Stack_Size);
3924 Decorate (Aspect, Aitem);
3925 Insert_Pragma (Aitem);
3928 -- Volatile_Function
3930 -- Aspect Volatile_Function is never delayed because it is
3931 -- equivalent to a source pragma which appears after the
3932 -- related subprogram.
3934 when Aspect_Volatile_Function =>
3936 (Pragma_Argument_Associations => New_List (
3937 Make_Pragma_Argument_Association (Loc,
3938 Expression => Relocate_Node (Expr))),
3939 Pragma_Name => Name_Volatile_Function);
3941 Decorate (Aspect, Aitem);
3942 Insert_Pragma (Aitem);
3945 -- Case 2e: Annotate aspect
3947 when Aspect_Annotate =>
3954 -- The argument can be a single identifier
3956 if Nkind (Expr) = N_Identifier then
3958 -- One level of parens is allowed
3960 if Paren_Count (Expr) > 1 then
3961 Error_Msg_F ("extra parentheses ignored", Expr);
3964 Set_Paren_Count (Expr, 0);
3966 -- Add the single item to the list
3968 Args := New_List (Expr);
3970 -- Otherwise we must have an aggregate
3972 elsif Nkind (Expr) = N_Aggregate then
3974 -- Must be positional
3976 if Present (Component_Associations (Expr)) then
3978 ("purely positional aggregate required", Expr);
3982 -- Must not be parenthesized
3984 if Paren_Count (Expr) /= 0 then
3985 Error_Msg_F ("extra parentheses ignored", Expr);
3988 -- List of arguments is list of aggregate expressions
3990 Args := Expressions (Expr);
3992 -- Anything else is illegal
3995 Error_Msg_F ("wrong form for Annotate aspect", Expr);
3999 -- Prepare pragma arguments
4002 Arg := First (Args);
4003 while Present (Arg) loop
4005 Make_Pragma_Argument_Association (Sloc (Arg),
4006 Expression => Relocate_Node (Arg)));
4011 Make_Pragma_Argument_Association (Sloc (Ent),
4012 Chars => Name_Entity,
4013 Expression => Ent));
4016 (Pragma_Argument_Associations => Pargs,
4017 Pragma_Name => Name_Annotate);
4020 -- Case 3 : Aspects that don't correspond to pragma/attribute
4021 -- definition clause.
4023 -- Case 3a: The aspects listed below don't correspond to
4024 -- pragmas/attributes but do require delayed analysis.
4026 when Aspect_Default_Value | Aspect_Default_Component_Value =>
4027 Error_Msg_Name_1 := Chars (Id);
4029 if not Is_Type (E) then
4030 Error_Msg_N ("aspect% can only apply to a type", Id);
4033 elsif not Is_First_Subtype (E) then
4034 Error_Msg_N ("aspect% cannot apply to subtype", Id);
4037 elsif A_Id = Aspect_Default_Value
4038 and then not Is_Scalar_Type (E)
4040 Error_Msg_N ("aspect% can only be applied to scalar type",
4044 elsif A_Id = Aspect_Default_Component_Value then
4045 if not Is_Array_Type (E) then
4046 Error_Msg_N ("aspect% can only be applied to array " &
4050 elsif not Is_Scalar_Type (Component_Type (E)) then
4051 Error_Msg_N ("aspect% requires scalar components", Id);
4058 when Aspect_Integer_Literal
4059 | Aspect_Real_Literal
4060 | Aspect_String_Literal
4063 if not Is_First_Subtype (E) then
4065 ("may only be specified for a first subtype", Aspect);
4069 if Ada_Version < Ada_2020 then
4071 (No_Implementation_Aspect_Specifications, N);
4076 -- Case 3b: The aspects listed below don't correspond to
4077 -- pragmas/attributes and don't need delayed analysis.
4079 -- Implicit_Dereference
4081 -- For Implicit_Dereference, External_Name and Link_Name, only
4082 -- the legality checks are done during the analysis, thus no
4083 -- delay is required.
4085 when Aspect_Implicit_Dereference =>
4086 Analyze_Aspect_Implicit_Dereference;
4091 when Aspect_Dimension =>
4092 Analyze_Aspect_Dimension (N, Id, Expr);
4097 when Aspect_Dimension_System =>
4098 Analyze_Aspect_Dimension_System (N, Id, Expr);
4101 -- Case 4: Aspects requiring special handling
4103 -- Pre/Post/Test_Case/Contract_Cases whose corresponding
4104 -- pragmas take care of the delay.
4108 -- Aspects Pre/Post generate Precondition/Postcondition pragmas
4109 -- with a first argument that is the expression, and a second
4110 -- argument that is an informative message if the test fails.
4111 -- This is inserted right after the declaration, to get the
4112 -- required pragma placement. The processing for the pragmas
4113 -- takes care of the required delay.
4115 when Pre_Post_Aspects => Pre_Post : declare
4119 if A_Id = Aspect_Pre or else A_Id = Aspect_Precondition then
4120 Pname := Name_Precondition;
4122 Pname := Name_Postcondition;
4125 -- Check that the class-wide predicate cannot be applied to
4126 -- an operation of a synchronized type. AI12-0182 forbids
4127 -- these altogether, while earlier language semantics made
4128 -- them legal on tagged synchronized types.
4130 -- Other legality checks are performed when analyzing the
4131 -- contract of the operation.
4133 if Class_Present (Aspect)
4134 and then Is_Concurrent_Type (Current_Scope)
4135 and then Ekind_In (E, E_Entry, E_Function, E_Procedure)
4137 Error_Msg_Name_1 := Original_Aspect_Pragma_Name (Aspect);
4139 ("aspect % can only be specified for a primitive "
4140 & "operation of a tagged type", Aspect);
4145 -- If the expressions is of the form A and then B, then
4146 -- we generate separate Pre/Post aspects for the separate
4147 -- clauses. Since we allow multiple pragmas, there is no
4148 -- problem in allowing multiple Pre/Post aspects internally.
4149 -- These should be treated in reverse order (B first and
4150 -- A second) since they are later inserted just after N in
4151 -- the order they are treated. This way, the pragma for A
4152 -- ends up preceding the pragma for B, which may have an
4153 -- importance for the error raised (either constraint error
4154 -- or precondition error).
4156 -- We do not do this for Pre'Class, since we have to put
4157 -- these conditions together in a complex OR expression.
4159 -- We don't do this in GNATprove mode, because it brings no
4160 -- benefit for proof and causes annoyance for flow analysis,
4161 -- which prefers to be as close to the original source code
4162 -- as possible. Also we don't do this when analyzing generic
4163 -- units since it causes spurious visibility errors in the
4164 -- preanalysis of instantiations.
4166 if not GNATprove_Mode
4167 and then (Pname = Name_Postcondition
4168 or else not Class_Present (Aspect))
4169 and then not Inside_A_Generic
4171 while Nkind (Expr) = N_And_Then loop
4172 Insert_After (Aspect,
4173 Make_Aspect_Specification (Sloc (Left_Opnd (Expr)),
4174 Identifier => Identifier (Aspect),
4175 Expression => Relocate_Node (Left_Opnd (Expr)),
4176 Class_Present => Class_Present (Aspect),
4177 Split_PPC => True));
4178 Rewrite (Expr, Relocate_Node (Right_Opnd (Expr)));
4179 Eloc := Sloc (Expr);
4183 -- Build the precondition/postcondition pragma
4185 -- We use Relocate_Node here rather than New_Copy_Tree
4186 -- because subsequent visibility analysis of the aspect
4187 -- depends on this sharing. This should be cleaned up???
4189 -- If the context is generic, we want to preserve the
4190 -- original tree, and simply share it between aspect and
4191 -- generated attribute. This parallels what is done in
4192 -- sem_prag.adb (see Get_Argument).
4198 if Inside_A_Generic then
4201 New_Expr := Relocate_Node (Expr);
4205 (Pragma_Argument_Associations => New_List (
4206 Make_Pragma_Argument_Association (Eloc,
4207 Chars => Name_Check,
4208 Expression => New_Expr)),
4209 Pragma_Name => Pname);
4212 -- Add message unless exception messages are suppressed
4214 if not Opt.Exception_Locations_Suppressed then
4215 Append_To (Pragma_Argument_Associations (Aitem),
4216 Make_Pragma_Argument_Association (Eloc,
4217 Chars => Name_Message,
4219 Make_String_Literal (Eloc,
4221 & Get_Name_String (Pname)
4223 & Build_Location_String (Eloc))));
4226 Set_Is_Delayed_Aspect (Aspect);
4228 -- For Pre/Post cases, insert immediately after the entity
4229 -- declaration, since that is the required pragma placement.
4230 -- Note that for these aspects, we do not have to worry
4231 -- about delay issues, since the pragmas themselves deal
4232 -- with delay of visibility for the expression analysis.
4234 Insert_Pragma (Aitem);
4241 when Aspect_Test_Case => Test_Case : declare
4243 Comp_Expr : Node_Id;
4244 Comp_Assn : Node_Id;
4249 if Nkind (Parent (N)) = N_Compilation_Unit then
4250 Error_Msg_Name_1 := Nam;
4251 Error_Msg_N ("incorrect placement of aspect `%`", E);
4255 if Nkind (Expr) /= N_Aggregate then
4256 Error_Msg_Name_1 := Nam;
4258 ("wrong syntax for aspect `%` for &", Id, E);
4262 -- Create the list of arguments for building the Test_Case
4265 Comp_Expr := First (Expressions (Expr));
4266 while Present (Comp_Expr) loop
4268 Make_Pragma_Argument_Association (Sloc (Comp_Expr),
4269 Expression => Relocate_Node (Comp_Expr)));
4273 Comp_Assn := First (Component_Associations (Expr));
4274 while Present (Comp_Assn) loop
4275 if List_Length (Choices (Comp_Assn)) /= 1
4277 Nkind (First (Choices (Comp_Assn))) /= N_Identifier
4279 Error_Msg_Name_1 := Nam;
4281 ("wrong syntax for aspect `%` for &", Id, E);
4286 Make_Pragma_Argument_Association (Sloc (Comp_Assn),
4287 Chars => Chars (First (Choices (Comp_Assn))),
4289 Relocate_Node (Expression (Comp_Assn))));
4293 -- Build the test-case pragma
4296 (Pragma_Argument_Associations => Args,
4297 Pragma_Name => Nam);
4302 when Aspect_Contract_Cases =>
4304 (Pragma_Argument_Associations => New_List (
4305 Make_Pragma_Argument_Association (Loc,
4306 Expression => Relocate_Node (Expr))),
4307 Pragma_Name => Nam);
4309 Decorate (Aspect, Aitem);
4310 Insert_Pragma (Aitem);
4313 -- Case 5: Special handling for aspects with an optional
4314 -- boolean argument.
4316 -- In the delayed case, the corresponding pragma cannot be
4317 -- generated yet because the evaluation of the boolean needs
4318 -- to be delayed till the freeze point.
4320 when Boolean_Aspects
4321 | Library_Unit_Aspects
4323 Set_Is_Boolean_Aspect (Aspect);
4325 -- Lock_Free aspect only apply to protected objects
4327 if A_Id = Aspect_Lock_Free then
4328 if Ekind (E) /= E_Protected_Type then
4329 Error_Msg_Name_1 := Nam;
4331 ("aspect % only applies to a protected object",
4335 -- Set the Uses_Lock_Free flag to True if there is no
4336 -- expression or if the expression is True. The
4337 -- evaluation of this aspect should be delayed to the
4338 -- freeze point (why???)
4341 or else Is_True (Static_Boolean (Expr))
4343 Set_Uses_Lock_Free (E);
4346 Record_Rep_Item (E, Aspect);
4351 elsif A_Id = Aspect_Export or else A_Id = Aspect_Import then
4352 Analyze_Aspect_Export_Import;
4354 -- Disable_Controlled
4356 elsif A_Id = Aspect_Disable_Controlled then
4357 Analyze_Aspect_Disable_Controlled;
4360 -- Ada 202x (AI12-0075): static expression functions
4362 elsif A_Id = Aspect_Static then
4363 Analyze_Aspect_Static;
4366 -- Ada 2020 (AI12-0279)
4368 elsif A_Id = Aspect_Yield then
4369 Analyze_Aspect_Yield;
4373 -- Library unit aspects require special handling in the case
4374 -- of a package declaration, the pragma needs to be inserted
4375 -- in the list of declarations for the associated package.
4376 -- There is no issue of visibility delay for these aspects.
4378 if A_Id in Library_Unit_Aspects
4380 Nkind_In (N, N_Package_Declaration,
4381 N_Generic_Package_Declaration)
4382 and then Nkind (Parent (N)) /= N_Compilation_Unit
4384 -- Aspect is legal on a local instantiation of a library-
4385 -- level generic unit.
4387 and then not Is_Generic_Instance (Defining_Entity (N))
4390 ("incorrect context for library unit aspect&", Id);
4394 -- Cases where we do not delay, includes all cases where the
4395 -- expression is missing other than the above cases.
4397 if not Delay_Required or else No (Expr) then
4399 -- Exclude aspects Export and Import because their pragma
4400 -- syntax does not map directly to a Boolean aspect.
4402 if A_Id /= Aspect_Export
4403 and then A_Id /= Aspect_Import
4406 (Pragma_Argument_Associations => New_List (
4407 Make_Pragma_Argument_Association (Sloc (Ent),
4408 Expression => Ent)),
4409 Pragma_Name => Chars (Id));
4412 Delay_Required := False;
4414 -- In general cases, the corresponding pragma/attribute
4415 -- definition clause will be inserted later at the freezing
4416 -- point, and we do not need to build it now.
4424 -- This is special because for access types we need to generate
4425 -- an attribute definition clause. This also works for single
4426 -- task declarations, but it does not work for task type
4427 -- declarations, because we have the case where the expression
4428 -- references a discriminant of the task type. That can't use
4429 -- an attribute definition clause because we would not have
4430 -- visibility on the discriminant. For that case we must
4431 -- generate a pragma in the task definition.
4433 when Aspect_Storage_Size =>
4437 if Ekind (E) = E_Task_Type then
4439 Decl : constant Node_Id := Declaration_Node (E);
4442 pragma Assert (Nkind (Decl) = N_Task_Type_Declaration);
4444 -- If no task definition, create one
4446 if No (Task_Definition (Decl)) then
4447 Set_Task_Definition (Decl,
4448 Make_Task_Definition (Loc,
4449 Visible_Declarations => Empty_List,
4450 End_Label => Empty));
4453 -- Create a pragma and put it at the start of the task
4454 -- definition for the task type declaration.
4457 (Pragma_Argument_Associations => New_List (
4458 Make_Pragma_Argument_Association (Loc,
4459 Expression => Relocate_Node (Expr))),
4460 Pragma_Name => Name_Storage_Size);
4464 Visible_Declarations (Task_Definition (Decl)));
4468 -- All other cases, generate attribute definition
4472 Make_Attribute_Definition_Clause (Loc,
4474 Chars => Chars (Id),
4475 Expression => Relocate_Node (Expr));
4479 -- Attach the corresponding pragma/attribute definition clause to
4480 -- the aspect specification node.
4482 if Present (Aitem) then
4483 Set_From_Aspect_Specification (Aitem);
4486 -- For an aspect that applies to a type, indicate whether it
4487 -- appears on a partial view of the type.
4490 and then Is_Private_Type (E)
4492 Set_Aspect_On_Partial_View (Aspect);
4495 -- In the context of a compilation unit, we directly put the
4496 -- pragma in the Pragmas_After list of the N_Compilation_Unit_Aux
4497 -- node (no delay is required here) except for aspects on a
4498 -- subprogram body (see below) and a generic package, for which we
4499 -- need to introduce the pragma before building the generic copy
4500 -- (see sem_ch12), and for package instantiations, where the
4501 -- library unit pragmas are better handled early.
4503 if Nkind (Parent (N)) = N_Compilation_Unit
4504 and then (Present (Aitem) or else Is_Boolean_Aspect (Aspect))
4507 Aux : constant Node_Id := Aux_Decls_Node (Parent (N));
4510 pragma Assert (Nkind (Aux) = N_Compilation_Unit_Aux);
4512 -- For a Boolean aspect, create the corresponding pragma if
4513 -- no expression or if the value is True.
4515 if Is_Boolean_Aspect (Aspect) and then No (Aitem) then
4516 if Is_True (Static_Boolean (Expr)) then
4518 (Pragma_Argument_Associations => New_List (
4519 Make_Pragma_Argument_Association (Sloc (Ent),
4520 Expression => Ent)),
4521 Pragma_Name => Chars (Id));
4523 Set_From_Aspect_Specification (Aitem, True);
4524 Set_Corresponding_Aspect (Aitem, Aspect);
4531 -- If the aspect is on a subprogram body (relevant aspect
4532 -- is Inline), add the pragma in front of the declarations.
4534 if Nkind (N) = N_Subprogram_Body then
4535 if No (Declarations (N)) then
4536 Set_Declarations (N, New_List);
4539 Prepend (Aitem, Declarations (N));
4541 elsif Nkind (N) = N_Generic_Package_Declaration then
4542 if No (Visible_Declarations (Specification (N))) then
4543 Set_Visible_Declarations (Specification (N), New_List);
4547 Visible_Declarations (Specification (N)));
4549 elsif Nkind (N) = N_Package_Instantiation then
4551 Spec : constant Node_Id :=
4552 Specification (Instance_Spec (N));
4554 if No (Visible_Declarations (Spec)) then
4555 Set_Visible_Declarations (Spec, New_List);
4558 Prepend (Aitem, Visible_Declarations (Spec));
4562 if No (Pragmas_After (Aux)) then
4563 Set_Pragmas_After (Aux, New_List);
4566 Append (Aitem, Pragmas_After (Aux));
4573 -- The evaluation of the aspect is delayed to the freezing point.
4574 -- The pragma or attribute clause if there is one is then attached
4575 -- to the aspect specification which is put in the rep item list.
4577 if Delay_Required then
4578 if Present (Aitem) then
4579 Set_Is_Delayed_Aspect (Aitem);
4580 Set_Aspect_Rep_Item (Aspect, Aitem);
4581 Set_Parent (Aitem, Aspect);
4584 Set_Is_Delayed_Aspect (Aspect);
4586 -- In the case of Default_Value, link the aspect to base type
4587 -- as well, even though it appears on a first subtype. This is
4588 -- mandated by the semantics of the aspect. Do not establish
4589 -- the link when processing the base type itself as this leads
4590 -- to a rep item circularity.
4592 if A_Id = Aspect_Default_Value and then Base_Type (E) /= E then
4593 Set_Has_Delayed_Aspects (Base_Type (E));
4594 Record_Rep_Item (Base_Type (E), Aspect);
4597 Set_Has_Delayed_Aspects (E);
4598 Record_Rep_Item (E, Aspect);
4600 -- When delay is not required and the context is a package or a
4601 -- subprogram body, insert the pragma in the body declarations.
4603 elsif Nkind_In (N, N_Package_Body, N_Subprogram_Body) then
4604 if No (Declarations (N)) then
4605 Set_Declarations (N, New_List);
4608 -- The pragma is added before source declarations
4610 Prepend_To (Declarations (N), Aitem);
4612 -- When delay is not required and the context is not a compilation
4613 -- unit, we simply insert the pragma/attribute definition clause
4616 elsif Present (Aitem) then
4617 Insert_After (Ins_Node, Aitem);
4620 end Analyze_One_Aspect;
4624 end loop Aspect_Loop;
4626 if Has_Delayed_Aspects (E) then
4627 Ensure_Freeze_Node (E);
4629 end Analyze_Aspect_Specifications;
4631 ------------------------------------------------
4632 -- Analyze_Aspects_On_Subprogram_Body_Or_Stub --
4633 ------------------------------------------------
4635 procedure Analyze_Aspects_On_Subprogram_Body_Or_Stub (N : Node_Id) is
4636 Body_Id : constant Entity_Id := Defining_Entity (N);
4638 procedure Diagnose_Misplaced_Aspects (Spec_Id : Entity_Id);
4639 -- Body [stub] N has aspects, but they are not properly placed. Emit an
4640 -- error message depending on the aspects involved. Spec_Id denotes the
4641 -- entity of the corresponding spec.
4643 --------------------------------
4644 -- Diagnose_Misplaced_Aspects --
4645 --------------------------------
4647 procedure Diagnose_Misplaced_Aspects (Spec_Id : Entity_Id) is
4648 procedure Misplaced_Aspect_Error
4651 -- Emit an error message concerning misplaced aspect Asp. Ref_Nam is
4652 -- the name of the refined version of the aspect.
4654 ----------------------------
4655 -- Misplaced_Aspect_Error --
4656 ----------------------------
4658 procedure Misplaced_Aspect_Error
4662 Asp_Nam : constant Name_Id := Chars (Identifier (Asp));
4663 Asp_Id : constant Aspect_Id := Get_Aspect_Id (Asp_Nam);
4666 -- The corresponding spec already contains the aspect in question
4667 -- and the one appearing on the body must be the refined form:
4669 -- procedure P with Global ...;
4670 -- procedure P with Global ... is ... end P;
4674 if Has_Aspect (Spec_Id, Asp_Id) then
4675 Error_Msg_Name_1 := Asp_Nam;
4677 -- Subunits cannot carry aspects that apply to a subprogram
4680 if Nkind (Parent (N)) = N_Subunit then
4681 Error_Msg_N ("aspect % cannot apply to a subunit", Asp);
4683 -- Otherwise suggest the refined form
4686 Error_Msg_Name_2 := Ref_Nam;
4687 Error_Msg_N ("aspect % should be %", Asp);
4690 -- Otherwise the aspect must appear on the spec, not on the body
4693 -- procedure P with Global ... is ... end P;
4697 ("aspect specification must appear on initial declaration",
4700 end Misplaced_Aspect_Error;
4707 -- Start of processing for Diagnose_Misplaced_Aspects
4710 -- Iterate over the aspect specifications and emit specific errors
4711 -- where applicable.
4713 Asp := First (Aspect_Specifications (N));
4714 while Present (Asp) loop
4715 Asp_Nam := Chars (Identifier (Asp));
4717 -- Do not emit errors on aspects that can appear on a subprogram
4718 -- body. This scenario occurs when the aspect specification list
4719 -- contains both misplaced and properly placed aspects.
4721 if Aspect_On_Body_Or_Stub_OK (Get_Aspect_Id (Asp_Nam)) then
4724 -- Special diagnostics for SPARK aspects
4726 elsif Asp_Nam = Name_Depends then
4727 Misplaced_Aspect_Error (Asp, Name_Refined_Depends);
4729 elsif Asp_Nam = Name_Global then
4730 Misplaced_Aspect_Error (Asp, Name_Refined_Global);
4732 elsif Asp_Nam = Name_Post then
4733 Misplaced_Aspect_Error (Asp, Name_Refined_Post);
4735 -- Otherwise a language-defined aspect is misplaced
4739 ("aspect specification must appear on initial declaration",
4745 end Diagnose_Misplaced_Aspects;
4749 Spec_Id : constant Entity_Id := Unique_Defining_Entity (N);
4751 -- Start of processing for Analyze_Aspects_On_Subprogram_Body_Or_Stub
4754 -- Language-defined aspects cannot be associated with a subprogram body
4755 -- [stub] if the subprogram has a spec. Certain implementation defined
4756 -- aspects are allowed to break this rule (for all applicable cases, see
4757 -- table Aspects.Aspect_On_Body_Or_Stub_OK).
4759 if Spec_Id /= Body_Id and then not Aspects_On_Body_Or_Stub_OK (N) then
4760 Diagnose_Misplaced_Aspects (Spec_Id);
4762 Analyze_Aspect_Specifications (N, Body_Id);
4764 end Analyze_Aspects_On_Subprogram_Body_Or_Stub;
4766 -----------------------
4767 -- Analyze_At_Clause --
4768 -----------------------
4770 -- An at clause is replaced by the corresponding Address attribute
4771 -- definition clause that is the preferred approach in Ada 95.
4773 procedure Analyze_At_Clause (N : Node_Id) is
4774 CS : constant Boolean := Comes_From_Source (N);
4777 -- This is an obsolescent feature
4779 Check_Restriction (No_Obsolescent_Features, N);
4781 if Warn_On_Obsolescent_Feature then
4783 ("?j?at clause is an obsolescent feature (RM J.7(2))", N);
4785 ("\?j?use address attribute definition clause instead", N);
4788 -- Rewrite as address clause
4791 Make_Attribute_Definition_Clause (Sloc (N),
4792 Name => Identifier (N),
4793 Chars => Name_Address,
4794 Expression => Expression (N)));
4796 -- We preserve Comes_From_Source, since logically the clause still comes
4797 -- from the source program even though it is changed in form.
4799 Set_Comes_From_Source (N, CS);
4801 -- Analyze rewritten clause
4803 Analyze_Attribute_Definition_Clause (N);
4804 end Analyze_At_Clause;
4806 -----------------------------------------
4807 -- Analyze_Attribute_Definition_Clause --
4808 -----------------------------------------
4810 procedure Analyze_Attribute_Definition_Clause (N : Node_Id) is
4811 Loc : constant Source_Ptr := Sloc (N);
4812 Nam : constant Node_Id := Name (N);
4813 Attr : constant Name_Id := Chars (N);
4814 Expr : constant Node_Id := Expression (N);
4815 Id : constant Attribute_Id := Get_Attribute_Id (Attr);
4818 -- The entity of Nam after it is analyzed. In the case of an incomplete
4819 -- type, this is the underlying type.
4822 -- The underlying entity to which the attribute applies. Generally this
4823 -- is the Underlying_Type of Ent, except in the case where the clause
4824 -- applies to the full view of an incomplete or private type, in which
4825 -- case U_Ent is just a copy of Ent.
4827 FOnly : Boolean := False;
4828 -- Reset to True for subtype specific attribute (Alignment, Size)
4829 -- and for stream attributes, i.e. those cases where in the call to
4830 -- Rep_Item_Too_Late, FOnly is set True so that only the freezing rules
4831 -- are checked. Note that the case of stream attributes is not clear
4832 -- from the RM, but see AI95-00137. Also, the RM seems to disallow
4833 -- Storage_Size for derived task types, but that is also clearly
4836 procedure Analyze_Put_Image_TSS_Definition;
4838 procedure Analyze_Stream_TSS_Definition (TSS_Nam : TSS_Name_Type);
4839 -- Common processing for 'Read, 'Write, 'Input and 'Output attribute
4840 -- definition clauses.
4842 function Duplicate_Clause return Boolean;
4843 -- This routine checks if the aspect for U_Ent being given by attribute
4844 -- definition clause N is for an aspect that has already been specified,
4845 -- and if so gives an error message. If there is a duplicate, True is
4846 -- returned, otherwise if there is no error, False is returned.
4848 procedure Check_Indexing_Functions;
4849 -- Check that the function in Constant_Indexing or Variable_Indexing
4850 -- attribute has the proper type structure. If the name is overloaded,
4851 -- check that some interpretation is legal.
4853 procedure Check_Iterator_Functions;
4854 -- Check that there is a single function in Default_Iterator attribute
4855 -- that has the proper type structure.
4857 function Check_Primitive_Function (Subp : Entity_Id) return Boolean;
4858 -- Common legality check for the previous two
4860 -----------------------------------
4861 -- Analyze_Put_Image_TSS_Definition --
4862 -----------------------------------
4864 procedure Analyze_Put_Image_TSS_Definition is
4865 Subp : Entity_Id := Empty;
4870 function Has_Good_Profile
4872 Report : Boolean := False) return Boolean;
4873 -- Return true if the entity is a subprogram with an appropriate
4874 -- profile for the attribute being defined. If result is False and
4875 -- Report is True, function emits appropriate error.
4877 ----------------------
4878 -- Has_Good_Profile --
4879 ----------------------
4881 function Has_Good_Profile
4883 Report : Boolean := False) return Boolean
4889 if Ekind (Subp) /= E_Procedure then
4893 F := First_Formal (Subp);
4895 if No (F) or else Etype (F) /= Class_Wide_Type (RTE (RE_Sink)) then
4901 if Parameter_Mode (F) /= E_In_Parameter then
4907 -- Verify that the prefix of the attribute and the local name for
4908 -- the type of the formal match.
4914 if Present (Next_Formal (F)) then
4917 elsif not Is_Scalar_Type (Typ)
4918 and then not Is_First_Subtype (Typ)
4920 if Report and not Is_First_Subtype (Typ) then
4922 ("subtype of formal in Put_Image operation must be a "
4923 & "first subtype", Parameter_Type (Parent (F)));
4931 end Has_Good_Profile;
4933 -- Start of processing for Analyze_Put_Image_TSS_Definition
4936 if not Is_Type (U_Ent) then
4937 Error_Msg_N ("local name must be a subtype", Nam);
4940 elsif not Is_First_Subtype (U_Ent) then
4941 Error_Msg_N ("local name must be a first subtype", Nam);
4945 Pnam := TSS (Base_Type (U_Ent), TSS_Put_Image);
4947 -- If Pnam is present, it can be either inherited from an ancestor
4948 -- type (in which case it is legal to redefine it for this type), or
4949 -- be a previous definition of the attribute for the same type (in
4950 -- which case it is illegal).
4952 -- In the first case, it will have been analyzed already, and we can
4953 -- check that its profile does not match the expected profile for the
4954 -- Put_Image attribute of U_Ent. In the second case, either Pnam has
4955 -- been analyzed (and has the expected profile), or it has not been
4956 -- analyzed yet (case of a type that has not been frozen yet and for
4957 -- which Put_Image has been set using Set_TSS).
4960 and then (No (First_Entity (Pnam)) or else Has_Good_Profile (Pnam))
4962 Error_Msg_Sloc := Sloc (Pnam);
4963 Error_Msg_Name_1 := Attr;
4964 Error_Msg_N ("% attribute already defined #", Nam);
4970 if Is_Entity_Name (Expr) then
4971 if not Is_Overloaded (Expr) then
4972 if Has_Good_Profile (Entity (Expr), Report => True) then
4973 Subp := Entity (Expr);
4977 Get_First_Interp (Expr, I, It);
4978 while Present (It.Nam) loop
4979 if Has_Good_Profile (It.Nam) then
4984 Get_Next_Interp (I, It);
4989 if Present (Subp) then
4990 if Is_Abstract_Subprogram (Subp) then
4991 Error_Msg_N ("Put_Image subprogram must not be abstract", Expr);
4995 Set_Entity (Expr, Subp);
4996 Set_Etype (Expr, Etype (Subp));
4998 New_Put_Image_Subprogram (N, U_Ent, Subp);
5001 Error_Msg_Name_1 := Attr;
5002 Error_Msg_N ("incorrect expression for% attribute", Expr);
5004 end Analyze_Put_Image_TSS_Definition;
5006 -----------------------------------
5007 -- Analyze_Stream_TSS_Definition --
5008 -----------------------------------
5010 procedure Analyze_Stream_TSS_Definition (TSS_Nam : TSS_Name_Type) is
5011 Subp : Entity_Id := Empty;
5016 Is_Read : constant Boolean := (TSS_Nam = TSS_Stream_Read);
5017 -- True for Read attribute, False for other attributes
5019 function Has_Good_Profile
5021 Report : Boolean := False) return Boolean;
5022 -- Return true if the entity is a subprogram with an appropriate
5023 -- profile for the attribute being defined. If result is False and
5024 -- Report is True, function emits appropriate error.
5026 ----------------------
5027 -- Has_Good_Profile --
5028 ----------------------
5030 function Has_Good_Profile
5032 Report : Boolean := False) return Boolean
5034 Expected_Ekind : constant array (Boolean) of Entity_Kind :=
5035 (False => E_Procedure, True => E_Function);
5036 Is_Function : constant Boolean := (TSS_Nam = TSS_Stream_Input);
5041 if Ekind (Subp) /= Expected_Ekind (Is_Function) then
5045 F := First_Formal (Subp);
5048 or else Ekind (Etype (F)) /= E_Anonymous_Access_Type
5049 or else Designated_Type (Etype (F)) /=
5050 Class_Wide_Type (RTE (RE_Root_Stream_Type))
5055 if not Is_Function then
5059 Expected_Mode : constant array (Boolean) of Entity_Kind :=
5060 (False => E_In_Parameter,
5061 True => E_Out_Parameter);
5063 if Parameter_Mode (F) /= Expected_Mode (Is_Read) then
5071 Typ := Etype (Subp);
5074 -- Verify that the prefix of the attribute and the local name for
5075 -- the type of the formal match.
5077 if Base_Type (Typ) /= Base_Type (Ent) then
5081 if Present (Next_Formal (F)) then
5084 elsif not Is_Scalar_Type (Typ)
5085 and then not Is_First_Subtype (Typ)
5086 and then not Is_Class_Wide_Type (Typ)
5088 if Report and not Is_First_Subtype (Typ) then
5090 ("subtype of formal in stream operation must be a first "
5091 & "subtype", Parameter_Type (Parent (F)));
5099 end Has_Good_Profile;
5101 -- Start of processing for Analyze_Stream_TSS_Definition
5106 if not Is_Type (U_Ent) then
5107 Error_Msg_N ("local name must be a subtype", Nam);
5110 elsif not Is_First_Subtype (U_Ent) then
5111 Error_Msg_N ("local name must be a first subtype", Nam);
5115 Pnam := TSS (Base_Type (U_Ent), TSS_Nam);
5117 -- If Pnam is present, it can be either inherited from an ancestor
5118 -- type (in which case it is legal to redefine it for this type), or
5119 -- be a previous definition of the attribute for the same type (in
5120 -- which case it is illegal).
5122 -- In the first case, it will have been analyzed already, and we
5123 -- can check that its profile does not match the expected profile
5124 -- for a stream attribute of U_Ent. In the second case, either Pnam
5125 -- has been analyzed (and has the expected profile), or it has not
5126 -- been analyzed yet (case of a type that has not been frozen yet
5127 -- and for which the stream attribute has been set using Set_TSS).
5130 and then (No (First_Entity (Pnam)) or else Has_Good_Profile (Pnam))
5132 Error_Msg_Sloc := Sloc (Pnam);
5133 Error_Msg_Name_1 := Attr;
5134 Error_Msg_N ("% attribute already defined #", Nam);
5140 if Is_Entity_Name (Expr) then
5141 if not Is_Overloaded (Expr) then
5142 if Has_Good_Profile (Entity (Expr), Report => True) then
5143 Subp := Entity (Expr);
5147 Get_First_Interp (Expr, I, It);
5148 while Present (It.Nam) loop
5149 if Has_Good_Profile (It.Nam) then
5154 Get_Next_Interp (I, It);
5159 if Present (Subp) then
5160 if Is_Abstract_Subprogram (Subp) then
5161 Error_Msg_N ("stream subprogram must not be abstract", Expr);
5164 -- A stream subprogram for an interface type must be a null
5165 -- procedure (RM 13.13.2 (38/3)). Note that the class-wide type
5166 -- of an interface is not an interface type (3.9.4 (6.b/2)).
5168 elsif Is_Interface (U_Ent)
5169 and then not Is_Class_Wide_Type (U_Ent)
5170 and then not Inside_A_Generic
5172 (Ekind (Subp) = E_Function
5176 (Unit_Declaration_Node (Ultimate_Alias (Subp)))))
5179 ("stream subprogram for interface type must be null "
5180 & "procedure", Expr);
5183 Set_Entity (Expr, Subp);
5184 Set_Etype (Expr, Etype (Subp));
5186 New_Stream_Subprogram (N, U_Ent, Subp, TSS_Nam);
5189 Error_Msg_Name_1 := Attr;
5191 if Is_Class_Wide_Type (Base_Type (Ent)) then
5193 ("incorrect expression for class-wide% attribute", Expr);
5195 Error_Msg_N ("incorrect expression for% attribute", Expr);
5198 end Analyze_Stream_TSS_Definition;
5200 ------------------------------
5201 -- Check_Indexing_Functions --
5202 ------------------------------
5204 procedure Check_Indexing_Functions is
5205 Indexing_Found : Boolean := False;
5207 procedure Check_Inherited_Indexing;
5208 -- For a derived type, check that no indexing aspect is specified
5209 -- for the type if it is also inherited
5210 -- AI12-0160: verify that an indexing cannot be specified for
5211 -- a derived type unless it is specified for the parent.
5213 procedure Check_One_Function (Subp : Entity_Id);
5214 -- Check one possible interpretation. Sets Indexing_Found True if a
5215 -- legal indexing function is found.
5217 procedure Illegal_Indexing (Msg : String);
5218 -- Diagnose illegal indexing function if not overloaded. In the
5219 -- overloaded case indicate that no legal interpretation exists.
5221 ------------------------------
5222 -- Check_Inherited_Indexing --
5223 ------------------------------
5225 procedure Check_Inherited_Indexing is
5226 Inherited : Node_Id;
5227 Other_Indexing : Node_Id;
5230 if Attr = Name_Constant_Indexing then
5232 Find_Aspect (Etype (Ent), Aspect_Constant_Indexing);
5234 Find_Aspect (Etype (Ent), Aspect_Variable_Indexing);
5236 else pragma Assert (Attr = Name_Variable_Indexing);
5238 Find_Aspect (Etype (Ent), Aspect_Variable_Indexing);
5240 Find_Aspect (Etype (Ent), Aspect_Constant_Indexing);
5243 if Present (Inherited) then
5244 if Debug_Flag_Dot_XX then
5247 -- OK if current attribute_definition_clause is expansion of
5248 -- inherited aspect.
5250 elsif Aspect_Rep_Item (Inherited) = N then
5253 -- Check if this is a confirming specification. The name
5254 -- may be overloaded between the parent operation and the
5255 -- inherited one, so we check that the Chars fields match.
5257 elsif Is_Entity_Name (Expression (Inherited))
5258 and then Chars (Entity (Expression (Inherited))) =
5259 Chars (Entity (Expression (N)))
5261 Indexing_Found := True;
5263 -- Indicate the operation that must be overridden, rather than
5264 -- redefining the indexing aspect.
5268 ("indexing function already inherited from parent type");
5270 ("!override & instead",
5271 N, Entity (Expression (Inherited)));
5274 -- If not inherited and the parent has another indexing function
5275 -- this is illegal, because it leads to inconsistent results in
5276 -- class-wide calls.
5278 elsif Present (Other_Indexing) then
5280 ("cannot specify indexing operation on derived type"
5281 & " if not specified for parent", N);
5283 end Check_Inherited_Indexing;
5285 ------------------------
5286 -- Check_One_Function --
5287 ------------------------
5289 procedure Check_One_Function (Subp : Entity_Id) is
5290 Default_Element : Node_Id;
5291 Ret_Type : constant Entity_Id := Etype (Subp);
5294 if not Is_Overloadable (Subp) then
5295 Illegal_Indexing ("illegal indexing function for type&");
5298 elsif Scope (Subp) /= Scope (Ent) then
5299 if Nkind (Expr) = N_Expanded_Name then
5301 -- Indexing function can't be declared elsewhere
5304 ("indexing function must be declared"
5305 & " in scope of type&");
5308 if Is_Derived_Type (Ent) then
5309 Check_Inherited_Indexing;
5314 elsif No (First_Formal (Subp)) then
5316 ("Indexing requires a function that applies to type&");
5319 elsif No (Next_Formal (First_Formal (Subp))) then
5321 ("indexing function must have at least two parameters");
5324 elsif Is_Derived_Type (Ent) then
5325 Check_Inherited_Indexing;
5328 if not Check_Primitive_Function (Subp) then
5330 ("Indexing aspect requires a function that applies to type&");
5334 -- If partial declaration exists, verify that it is not tagged.
5336 if Ekind (Current_Scope) = E_Package
5337 and then Has_Private_Declaration (Ent)
5338 and then From_Aspect_Specification (N)
5340 List_Containing (Parent (Ent)) =
5341 Private_Declarations
5342 (Specification (Unit_Declaration_Node (Current_Scope)))
5343 and then Nkind (N) = N_Attribute_Definition_Clause
5350 First (Visible_Declarations
5352 (Unit_Declaration_Node (Current_Scope))));
5354 while Present (Decl) loop
5355 if Nkind (Decl) = N_Private_Type_Declaration
5356 and then Ent = Full_View (Defining_Identifier (Decl))
5357 and then Tagged_Present (Decl)
5358 and then No (Aspect_Specifications (Decl))
5361 ("Indexing aspect cannot be specified on full view "
5362 & "if partial view is tagged");
5371 -- An indexing function must return either the default element of
5372 -- the container, or a reference type. For variable indexing it
5373 -- must be the latter.
5376 Find_Value_Of_Aspect
5377 (Etype (First_Formal (Subp)), Aspect_Iterator_Element);
5379 if Present (Default_Element) then
5380 Analyze (Default_Element);
5383 -- For variable_indexing the return type must be a reference type
5385 if Attr = Name_Variable_Indexing then
5386 if not Has_Implicit_Dereference (Ret_Type) then
5388 ("variable indexing must return a reference type");
5391 elsif Is_Access_Constant
5392 (Etype (First_Discriminant (Ret_Type)))
5395 ("variable indexing must return an access to variable");
5400 if Has_Implicit_Dereference (Ret_Type)
5403 (Etype (Get_Reference_Discriminant (Ret_Type)))
5406 ("constant indexing must return an access to constant");
5409 elsif Is_Access_Type (Etype (First_Formal (Subp)))
5410 and then not Is_Access_Constant (Etype (First_Formal (Subp)))
5413 ("constant indexing must apply to an access to constant");
5418 -- All checks succeeded.
5420 Indexing_Found := True;
5421 end Check_One_Function;
5423 -----------------------
5424 -- Illegal_Indexing --
5425 -----------------------
5427 procedure Illegal_Indexing (Msg : String) is
5429 Error_Msg_NE (Msg, N, Ent);
5430 end Illegal_Indexing;
5432 -- Start of processing for Check_Indexing_Functions
5436 Check_Inherited_Indexing;
5441 if not Is_Overloaded (Expr) then
5442 Check_One_Function (Entity (Expr));
5450 Indexing_Found := False;
5451 Get_First_Interp (Expr, I, It);
5452 while Present (It.Nam) loop
5454 -- Note that analysis will have added the interpretation
5455 -- that corresponds to the dereference. We only check the
5456 -- subprogram itself. Ignore homonyms that may come from
5457 -- derived types in the context.
5459 if Is_Overloadable (It.Nam)
5460 and then Comes_From_Source (It.Nam)
5462 Check_One_Function (It.Nam);
5465 Get_Next_Interp (I, It);
5470 if not Indexing_Found and then not Error_Posted (N) then
5472 ("aspect Indexing requires a local function that applies to "
5473 & "type&", Expr, Ent);
5475 end Check_Indexing_Functions;
5477 ------------------------------
5478 -- Check_Iterator_Functions --
5479 ------------------------------
5481 procedure Check_Iterator_Functions is
5482 function Valid_Default_Iterator (Subp : Entity_Id) return Boolean;
5483 -- Check one possible interpretation for validity
5485 ----------------------------
5486 -- Valid_Default_Iterator --
5487 ----------------------------
5489 function Valid_Default_Iterator (Subp : Entity_Id) return Boolean is
5490 Root_T : constant Entity_Id := Root_Type (Etype (Etype (Subp)));
5494 if not Check_Primitive_Function (Subp) then
5497 -- The return type must be derived from a type in an instance
5498 -- of Iterator.Interfaces, and thus its root type must have a
5501 elsif Chars (Root_T) /= Name_Forward_Iterator
5502 and then Chars (Root_T) /= Name_Reversible_Iterator
5507 Formal := First_Formal (Subp);
5510 -- False if any subsequent formal has no default expression
5512 Next_Formal (Formal);
5513 while Present (Formal) loop
5514 if No (Expression (Parent (Formal))) then
5518 Next_Formal (Formal);
5521 -- True if all subsequent formals have default expressions
5524 end Valid_Default_Iterator;
5526 -- Start of processing for Check_Iterator_Functions
5531 if not Is_Entity_Name (Expr) then
5532 Error_Msg_N ("aspect Iterator must be a function name", Expr);
5535 if not Is_Overloaded (Expr) then
5536 if Entity (Expr) /= Any_Id
5537 and then not Check_Primitive_Function (Entity (Expr))
5540 ("aspect Indexing requires a function that applies to type&",
5541 Entity (Expr), Ent);
5544 -- Flag the default_iterator as well as the denoted function.
5546 if not Valid_Default_Iterator (Entity (Expr)) then
5547 Error_Msg_N ("improper function for default iterator!", Expr);
5552 Default : Entity_Id := Empty;
5557 Get_First_Interp (Expr, I, It);
5558 while Present (It.Nam) loop
5559 if not Check_Primitive_Function (It.Nam)
5560 or else not Valid_Default_Iterator (It.Nam)
5564 elsif Present (Default) then
5566 -- An explicit one should override an implicit one
5568 if Comes_From_Source (Default) =
5569 Comes_From_Source (It.Nam)
5571 Error_Msg_N ("default iterator must be unique", Expr);
5572 Error_Msg_Sloc := Sloc (Default);
5573 Error_Msg_N ("\\possible interpretation#", Expr);
5574 Error_Msg_Sloc := Sloc (It.Nam);
5575 Error_Msg_N ("\\possible interpretation#", Expr);
5577 elsif Comes_From_Source (It.Nam) then
5584 Get_Next_Interp (I, It);
5587 if Present (Default) then
5588 Set_Entity (Expr, Default);
5589 Set_Is_Overloaded (Expr, False);
5592 ("no interpretation is a valid default iterator!", Expr);
5596 end Check_Iterator_Functions;
5598 -------------------------------
5599 -- Check_Primitive_Function --
5600 -------------------------------
5602 function Check_Primitive_Function (Subp : Entity_Id) return Boolean is
5606 if Ekind (Subp) /= E_Function then
5610 if No (First_Formal (Subp)) then
5613 Ctrl := Etype (First_Formal (Subp));
5616 -- To be a primitive operation subprogram has to be in same scope.
5618 if Scope (Ctrl) /= Scope (Subp) then
5622 -- Type of formal may be the class-wide type, an access to such,
5623 -- or an incomplete view.
5626 or else Ctrl = Class_Wide_Type (Ent)
5628 (Ekind (Ctrl) = E_Anonymous_Access_Type
5629 and then (Designated_Type (Ctrl) = Ent
5631 Designated_Type (Ctrl) = Class_Wide_Type (Ent)))
5633 (Ekind (Ctrl) = E_Incomplete_Type
5634 and then Full_View (Ctrl) = Ent)
5642 end Check_Primitive_Function;
5644 ----------------------
5645 -- Duplicate_Clause --
5646 ----------------------
5648 function Duplicate_Clause return Boolean is
5652 -- Nothing to do if this attribute definition clause comes from
5653 -- an aspect specification, since we could not be duplicating an
5654 -- explicit clause, and we dealt with the case of duplicated aspects
5655 -- in Analyze_Aspect_Specifications.
5657 if From_Aspect_Specification (N) then
5661 -- Otherwise current clause may duplicate previous clause, or a
5662 -- previously given pragma or aspect specification for the same
5665 A := Get_Rep_Item (U_Ent, Chars (N), Check_Parents => False);
5668 Error_Msg_Name_1 := Chars (N);
5669 Error_Msg_Sloc := Sloc (A);
5671 Error_Msg_NE ("aspect% for & previously given#", N, U_Ent);
5676 end Duplicate_Clause;
5678 -- Start of processing for Analyze_Attribute_Definition_Clause
5681 -- The following code is a defense against recursion. Not clear that
5682 -- this can happen legitimately, but perhaps some error situations can
5683 -- cause it, and we did see this recursion during testing.
5685 if Analyzed (N) then
5688 Set_Analyzed (N, True);
5691 Check_Restriction_No_Use_Of_Attribute (N);
5693 if Get_Aspect_Id (Chars (N)) /= No_Aspect then
5694 -- 6.1/3 No_Specification_of_Aspect: Identifies an aspect for which
5695 -- no aspect_specification, attribute_definition_clause, or pragma
5697 Check_Restriction_No_Specification_Of_Aspect (N);
5700 -- Ignore some selected attributes in CodePeer mode since they are not
5701 -- relevant in this context.
5703 if CodePeer_Mode then
5706 -- Ignore Component_Size in CodePeer mode, to avoid changing the
5707 -- internal representation of types by implicitly packing them.
5709 when Attribute_Component_Size =>
5710 Rewrite (N, Make_Null_Statement (Sloc (N)));
5718 -- Process Ignore_Rep_Clauses option
5720 if Ignore_Rep_Clauses then
5723 -- The following should be ignored. They do not affect legality
5724 -- and may be target dependent. The basic idea of -gnatI is to
5725 -- ignore any rep clauses that may be target dependent but do not
5726 -- affect legality (except possibly to be rejected because they
5727 -- are incompatible with the compilation target).
5729 when Attribute_Alignment
5730 | Attribute_Bit_Order
5731 | Attribute_Component_Size
5732 | Attribute_Default_Scalar_Storage_Order
5733 | Attribute_Machine_Radix
5734 | Attribute_Object_Size
5735 | Attribute_Scalar_Storage_Order
5738 | Attribute_Stream_Size
5739 | Attribute_Value_Size
5741 Kill_Rep_Clause (N);
5744 -- The following should not be ignored, because in the first place
5745 -- they are reasonably portable, and should not cause problems
5746 -- in compiling code from another target, and also they do affect
5747 -- legality, e.g. failing to provide a stream attribute for a type
5748 -- may make a program illegal.
5750 when Attribute_External_Tag
5753 | Attribute_Put_Image
5755 | Attribute_Simple_Storage_Pool
5756 | Attribute_Storage_Pool
5757 | Attribute_Storage_Size
5762 -- We do not do anything here with address clauses, they will be
5763 -- removed by Freeze later on, but for now, it works better to
5764 -- keep them in the tree.
5766 when Attribute_Address =>
5769 -- Other cases are errors ("attribute& cannot be set with
5770 -- definition clause"), which will be caught below.
5778 Ent := Entity (Nam);
5780 if Rep_Item_Too_Early (Ent, N) then
5784 -- Rep clause applies to (underlying) full view of private or incomplete
5785 -- type if we have one (if not, this is a premature use of the type).
5786 -- However, some semantic checks need to be done on the specified entity
5787 -- i.e. the private view, so we save it in Ent.
5789 if Is_Private_Type (Ent)
5790 and then Is_Derived_Type (Ent)
5791 and then not Is_Tagged_Type (Ent)
5792 and then No (Full_View (Ent))
5793 and then No (Underlying_Full_View (Ent))
5797 elsif Ekind (Ent) = E_Incomplete_Type then
5799 -- The attribute applies to the full view, set the entity of the
5800 -- attribute definition accordingly.
5802 Ent := Underlying_Type (Ent);
5804 Set_Entity (Nam, Ent);
5807 U_Ent := Underlying_Type (Ent);
5810 -- Avoid cascaded error
5812 if Etype (Nam) = Any_Type then
5815 -- Must be declared in current scope or in case of an aspect
5816 -- specification, must be visible in current scope.
5818 elsif Scope (Ent) /= Current_Scope
5820 not (From_Aspect_Specification (N)
5821 and then Scope_Within_Or_Same (Current_Scope, Scope (Ent)))
5823 Error_Msg_N ("entity must be declared in this scope", Nam);
5826 -- Must not be a source renaming (we do have some cases where the
5827 -- expander generates a renaming, and those cases are OK, in such
5828 -- cases any attribute applies to the renamed object as well).
5830 elsif Is_Object (Ent)
5831 and then Present (Renamed_Object (Ent))
5833 -- In the case of a renamed object from source, this is an error
5834 -- unless the object is an aggregate and the renaming is created
5835 -- for an object declaration.
5837 if Comes_From_Source (Renamed_Object (Ent))
5838 and then Nkind (Renamed_Object (Ent)) /= N_Aggregate
5840 Get_Name_String (Chars (N));
5841 Error_Msg_Strlen := Name_Len;
5842 Error_Msg_String (1 .. Name_Len) := Name_Buffer (1 .. Name_Len);
5844 ("~ clause not allowed for a renaming declaration "
5845 & "(RM 13.1(6))", Nam);
5848 -- For the case of a compiler generated renaming, the attribute
5849 -- definition clause applies to the renamed object created by the
5850 -- expander. The easiest general way to handle this is to create a
5851 -- copy of the attribute definition clause for this object.
5853 elsif Is_Entity_Name (Renamed_Object (Ent)) then
5855 Make_Attribute_Definition_Clause (Loc,
5857 New_Occurrence_Of (Entity (Renamed_Object (Ent)), Loc),
5859 Expression => Duplicate_Subexpr (Expression (N))));
5861 -- If the renamed object is not an entity, it must be a dereference
5862 -- of an unconstrained function call, and we must introduce a new
5863 -- declaration to capture the expression. This is needed in the case
5864 -- of 'Alignment, where the original declaration must be rewritten.
5868 (Nkind (Renamed_Object (Ent)) = N_Explicit_Dereference);
5872 -- If no underlying entity, use entity itself, applies to some
5873 -- previously detected error cases ???
5875 elsif No (U_Ent) then
5878 -- Cannot specify for a subtype (exception Object/Value_Size)
5880 elsif Is_Type (U_Ent)
5881 and then not Is_First_Subtype (U_Ent)
5882 and then Id /= Attribute_Object_Size
5883 and then Id /= Attribute_Value_Size
5884 and then not From_At_Mod (N)
5886 Error_Msg_N ("cannot specify attribute for subtype", Nam);
5890 Set_Entity (N, U_Ent);
5892 -- Switch on particular attribute
5900 -- Address attribute definition clause
5902 when Attribute_Address => Address : begin
5904 -- A little error check, catch for X'Address use X'Address;
5906 if Nkind (Nam) = N_Identifier
5907 and then Nkind (Expr) = N_Attribute_Reference
5908 and then Attribute_Name (Expr) = Name_Address
5909 and then Nkind (Prefix (Expr)) = N_Identifier
5910 and then Chars (Nam) = Chars (Prefix (Expr))
5913 ("address for & is self-referencing", Prefix (Expr), Ent);
5917 -- Not that special case, carry on with analysis of expression
5919 Analyze_And_Resolve (Expr, RTE (RE_Address));
5921 -- Even when ignoring rep clauses we need to indicate that the
5922 -- entity has an address clause and thus it is legal to declare
5923 -- it imported. Freeze will get rid of the address clause later.
5924 -- Also call Set_Address_Taken to indicate that an address clause
5925 -- was present, even if we are about to remove it.
5927 if Ignore_Rep_Clauses then
5928 Set_Address_Taken (U_Ent);
5930 if Ekind_In (U_Ent, E_Variable, E_Constant) then
5931 Record_Rep_Item (U_Ent, N);
5937 if Duplicate_Clause then
5940 -- Case of address clause for subprogram
5942 elsif Is_Subprogram (U_Ent) then
5943 if Has_Homonym (U_Ent) then
5945 ("address clause cannot be given for overloaded "
5946 & "subprogram", Nam);
5950 -- For subprograms, all address clauses are permitted, and we
5951 -- mark the subprogram as having a deferred freeze so that Gigi
5952 -- will not elaborate it too soon.
5954 -- Above needs more comments, what is too soon about???
5956 Set_Has_Delayed_Freeze (U_Ent);
5958 -- Case of address clause for entry
5960 elsif Ekind (U_Ent) = E_Entry then
5961 if Nkind (Parent (N)) = N_Task_Body then
5963 ("entry address must be specified in task spec", Nam);
5967 -- For entries, we require a constant address
5969 Check_Constant_Address_Clause (Expr, U_Ent);
5971 -- Special checks for task types
5973 if Is_Task_Type (Scope (U_Ent))
5974 and then Comes_From_Source (Scope (U_Ent))
5977 ("??entry address declared for entry in task type", N);
5979 ("\??only one task can be declared of this type", N);
5982 -- Entry address clauses are obsolescent
5984 Check_Restriction (No_Obsolescent_Features, N);
5986 if Warn_On_Obsolescent_Feature then
5988 ("?j?attaching interrupt to task entry is an obsolescent "
5989 & "feature (RM J.7.1)", N);
5991 ("\?j?use interrupt procedure instead", N);
5994 -- Case of an address clause for a class-wide object, which is
5995 -- considered erroneous.
5997 elsif Is_Class_Wide_Type (Etype (U_Ent)) then
5999 ("??class-wide object & must not be overlaid", Nam, U_Ent);
6001 ("\??Program_Error will be raised at run time", Nam);
6002 Insert_Action (Declaration_Node (U_Ent),
6003 Make_Raise_Program_Error (Loc,
6004 Reason => PE_Overlaid_Controlled_Object));
6007 -- Case of address clause for an object
6009 elsif Ekind_In (U_Ent, E_Constant, E_Variable) then
6011 Expr : constant Node_Id := Expression (N);
6016 -- Exported variables cannot have an address clause, because
6017 -- this cancels the effect of the pragma Export.
6019 if Is_Exported (U_Ent) then
6021 ("cannot export object with address clause", Nam);
6025 Find_Overlaid_Entity (N, O_Ent, Off);
6027 if Present (O_Ent) then
6029 -- If the object overlays a constant object, mark it so
6031 if Is_Constant_Object (O_Ent) then
6032 Set_Overlays_Constant (U_Ent);
6035 -- If the address clause is of the form:
6037 -- for X'Address use Y'Address;
6041 -- C : constant Address := Y'Address;
6043 -- for X'Address use C;
6045 -- then we make an entry in the table to check the size
6046 -- and alignment of the overlaying variable. But we defer
6047 -- this check till after code generation to take full
6048 -- advantage of the annotation done by the back end.
6050 -- If the entity has a generic type, the check will be
6051 -- performed in the instance if the actual type justifies
6052 -- it, and we do not insert the clause in the table to
6053 -- prevent spurious warnings.
6055 -- Note: we used to test Comes_From_Source and only give
6056 -- this warning for source entities, but we have removed
6057 -- this test. It really seems bogus to generate overlays
6058 -- that would trigger this warning in generated code.
6059 -- Furthermore, by removing the test, we handle the
6060 -- aspect case properly.
6062 if Is_Object (O_Ent)
6063 and then not Is_Generic_Formal (O_Ent)
6064 and then not Is_Generic_Type (Etype (U_Ent))
6065 and then Address_Clause_Overlay_Warnings
6067 Register_Address_Clause_Check
6068 (N, U_Ent, No_Uint, O_Ent, Off);
6071 -- If the overlay changes the storage order, mark the
6072 -- entity as being volatile to block any optimization
6073 -- for it since the construct is not really supported
6076 if (Is_Record_Type (Etype (U_Ent))
6077 or else Is_Array_Type (Etype (U_Ent)))
6078 and then (Is_Record_Type (Etype (O_Ent))
6079 or else Is_Array_Type (Etype (O_Ent)))
6080 and then Reverse_Storage_Order (Etype (U_Ent)) /=
6081 Reverse_Storage_Order (Etype (O_Ent))
6083 Set_Treat_As_Volatile (U_Ent);
6087 -- If this is not an overlay, mark a variable as being
6088 -- volatile to prevent unwanted optimizations. It's a
6089 -- conservative interpretation of RM 13.3(19) for the
6090 -- cases where the compiler cannot detect potential
6091 -- aliasing issues easily and it also covers the case
6092 -- of an absolute address where the volatile aspect is
6093 -- kind of implicit.
6095 if Ekind (U_Ent) = E_Variable then
6096 Set_Treat_As_Volatile (U_Ent);
6099 -- Make an entry in the table for an absolute address as
6100 -- above to check that the value is compatible with the
6101 -- alignment of the object.
6104 Addr : constant Node_Id := Address_Value (Expr);
6106 if Compile_Time_Known_Value (Addr)
6107 and then Address_Clause_Overlay_Warnings
6109 Register_Address_Clause_Check
6110 (N, U_Ent, Expr_Value (Addr), Empty, False);
6115 -- Issue an unconditional warning for a constant overlaying
6116 -- a variable. For the reverse case, we will issue it only
6117 -- if the variable is modified.
6119 if Ekind (U_Ent) = E_Constant
6120 and then Present (O_Ent)
6121 and then not Overlays_Constant (U_Ent)
6122 and then Address_Clause_Overlay_Warnings
6124 Error_Msg_N ("??constant overlays a variable", Expr);
6126 -- Imported variables can have an address clause, but then
6127 -- the import is pretty meaningless except to suppress
6128 -- initializations, so we do not need such variables to
6129 -- be statically allocated (and in fact it causes trouble
6130 -- if the address clause is a local value).
6132 elsif Is_Imported (U_Ent) then
6133 Set_Is_Statically_Allocated (U_Ent, False);
6136 -- We mark a possible modification of a variable with an
6137 -- address clause, since it is likely aliasing is occurring.
6139 Note_Possible_Modification (Nam, Sure => False);
6141 -- Legality checks on the address clause for initialized
6142 -- objects is deferred until the freeze point, because
6143 -- a subsequent pragma might indicate that the object
6144 -- is imported and thus not initialized. Also, the address
6145 -- clause might involve entities that have yet to be
6148 Set_Has_Delayed_Freeze (U_Ent);
6150 -- If an initialization call has been generated for this
6151 -- object, it needs to be deferred to after the freeze node
6152 -- we have just now added, otherwise GIGI will see a
6153 -- reference to the variable (as actual to the IP call)
6154 -- before its definition.
6157 Init_Call : constant Node_Id :=
6158 Remove_Init_Call (U_Ent, N);
6161 if Present (Init_Call) then
6162 Append_Freeze_Action (U_Ent, Init_Call);
6164 -- Reset Initialization_Statements pointer so that
6165 -- if there is a pragma Import further down, it can
6166 -- clear any default initialization.
6168 Set_Initialization_Statements (U_Ent, Init_Call);
6172 -- Entity has delayed freeze, so we will generate an
6173 -- alignment check at the freeze point unless suppressed.
6175 if not Range_Checks_Suppressed (U_Ent)
6176 and then not Alignment_Checks_Suppressed (U_Ent)
6178 Set_Check_Address_Alignment (N);
6181 -- Kill the size check code, since we are not allocating
6182 -- the variable, it is somewhere else.
6184 Kill_Size_Check_Code (U_Ent);
6187 -- Not a valid entity for an address clause
6190 Error_Msg_N ("address cannot be given for &", Nam);
6198 -- Alignment attribute definition clause
6200 when Attribute_Alignment => Alignment : declare
6201 Align : constant Uint := Get_Alignment_Value (Expr);
6202 Max_Align : constant Uint := UI_From_Int (Maximum_Alignment);
6207 if not Is_Type (U_Ent)
6208 and then Ekind (U_Ent) /= E_Variable
6209 and then Ekind (U_Ent) /= E_Constant
6211 Error_Msg_N ("alignment cannot be given for &", Nam);
6213 elsif Duplicate_Clause then
6216 elsif Align /= No_Uint then
6217 Set_Has_Alignment_Clause (U_Ent);
6219 -- Tagged type case, check for attempt to set alignment to a
6220 -- value greater than Max_Align, and reset if so.
6222 if Is_Tagged_Type (U_Ent) and then Align > Max_Align then
6224 ("alignment for & set to Maximum_Aligment??", Nam);
6225 Set_Alignment (U_Ent, Max_Align);
6230 Set_Alignment (U_Ent, Align);
6233 -- For an array type, U_Ent is the first subtype. In that case,
6234 -- also set the alignment of the anonymous base type so that
6235 -- other subtypes (such as the itypes for aggregates of the
6236 -- type) also receive the expected alignment.
6238 if Is_Array_Type (U_Ent) then
6239 Set_Alignment (Base_Type (U_Ent), Align);
6248 -- Bit_Order attribute definition clause
6250 when Attribute_Bit_Order =>
6251 if not Is_Record_Type (U_Ent) then
6253 ("Bit_Order can only be defined for record type", Nam);
6255 elsif Is_Tagged_Type (U_Ent) and then Is_Derived_Type (U_Ent) then
6257 ("Bit_Order cannot be defined for record extensions", Nam);
6259 elsif Duplicate_Clause then
6263 Analyze_And_Resolve (Expr, RTE (RE_Bit_Order));
6265 if Etype (Expr) = Any_Type then
6268 elsif not Is_OK_Static_Expression (Expr) then
6269 Flag_Non_Static_Expr
6270 ("Bit_Order requires static expression!", Expr);
6272 elsif (Expr_Value (Expr) = 0) /= Bytes_Big_Endian then
6273 Set_Reverse_Bit_Order (Base_Type (U_Ent), True);
6277 --------------------
6278 -- Component_Size --
6279 --------------------
6281 -- Component_Size attribute definition clause
6283 when Attribute_Component_Size => Component_Size_Case : declare
6284 Csize : constant Uint := Static_Integer (Expr);
6288 New_Ctyp : Entity_Id;
6292 if not Is_Array_Type (U_Ent) then
6293 Error_Msg_N ("component size requires array type", Nam);
6297 Btype := Base_Type (U_Ent);
6298 Ctyp := Component_Type (Btype);
6300 if Duplicate_Clause then
6303 elsif Rep_Item_Too_Early (Btype, N) then
6306 elsif Csize /= No_Uint then
6307 Check_Size (Expr, Ctyp, Csize, Biased);
6309 -- For the biased case, build a declaration for a subtype that
6310 -- will be used to represent the biased subtype that reflects
6311 -- the biased representation of components. We need the subtype
6312 -- to get proper conversions on referencing elements of the
6317 Make_Defining_Identifier (Loc,
6319 New_External_Name (Chars (U_Ent), 'C', 0, 'T'));
6322 Make_Subtype_Declaration (Loc,
6323 Defining_Identifier => New_Ctyp,
6324 Subtype_Indication =>
6325 New_Occurrence_Of (Component_Type (Btype), Loc));
6327 Set_Parent (Decl, N);
6328 Analyze (Decl, Suppress => All_Checks);
6330 Set_Has_Delayed_Freeze (New_Ctyp, False);
6331 Init_Esize (New_Ctyp);
6332 Set_RM_Size (New_Ctyp, Csize);
6333 Init_Alignment (New_Ctyp);
6334 Set_Is_Itype (New_Ctyp, True);
6335 Set_Associated_Node_For_Itype (New_Ctyp, U_Ent);
6337 Set_Component_Type (Btype, New_Ctyp);
6338 Set_Biased (New_Ctyp, N, "component size clause");
6341 Set_Component_Size (Btype, Csize);
6343 -- Deal with warning on overridden size
6345 if Warn_On_Overridden_Size
6346 and then Has_Size_Clause (Ctyp)
6347 and then RM_Size (Ctyp) /= Csize
6350 ("component size overrides size clause for&?S?", N, Ctyp);
6353 Set_Has_Component_Size_Clause (Btype, True);
6354 Set_Has_Non_Standard_Rep (Btype, True);
6356 end Component_Size_Case;
6358 -----------------------
6359 -- Constant_Indexing --
6360 -----------------------
6362 when Attribute_Constant_Indexing =>
6363 Check_Indexing_Functions;
6369 when Attribute_CPU =>
6371 -- CPU attribute definition clause not allowed except from aspect
6374 if From_Aspect_Specification (N) then
6375 if not Is_Task_Type (U_Ent) then
6376 Error_Msg_N ("CPU can only be defined for task", Nam);
6378 elsif Duplicate_Clause then
6382 -- The expression must be analyzed in the special manner
6383 -- described in "Handling of Default and Per-Object
6384 -- Expressions" in sem.ads.
6386 -- The visibility to the components must be established
6387 -- and restored before and after analysis.
6390 Preanalyze_Spec_Expression (Expr, RTE (RE_CPU_Range));
6393 if not Is_OK_Static_Expression (Expr) then
6394 Check_Restriction (Static_Priorities, Expr);
6400 ("attribute& cannot be set with definition clause", N);
6403 ----------------------
6404 -- Default_Iterator --
6405 ----------------------
6407 when Attribute_Default_Iterator => Default_Iterator : declare
6412 -- If target type is untagged, further checks are irrelevant
6414 if not Is_Tagged_Type (U_Ent) then
6416 ("aspect Default_Iterator applies to tagged type", Nam);
6420 Check_Iterator_Functions;
6424 if not Is_Entity_Name (Expr)
6425 or else Ekind (Entity (Expr)) /= E_Function
6427 Error_Msg_N ("aspect Iterator must be a function", Expr);
6430 Func := Entity (Expr);
6433 -- The type of the first parameter must be T, T'class, or a
6434 -- corresponding access type (5.5.1 (8/3). If function is
6435 -- parameterless label type accordingly.
6437 if No (First_Formal (Func)) then
6440 Typ := Etype (First_Formal (Func));
6444 or else Typ = Class_Wide_Type (U_Ent)
6445 or else (Is_Access_Type (Typ)
6446 and then Designated_Type (Typ) = U_Ent)
6447 or else (Is_Access_Type (Typ)
6448 and then Designated_Type (Typ) =
6449 Class_Wide_Type (U_Ent))
6455 ("Default Iterator must be a primitive of&", Func, U_Ent);
6457 end Default_Iterator;
6459 ------------------------
6460 -- Dispatching_Domain --
6461 ------------------------
6463 when Attribute_Dispatching_Domain =>
6465 -- Dispatching_Domain attribute definition clause not allowed
6466 -- except from aspect specification.
6468 if From_Aspect_Specification (N) then
6469 if not Is_Task_Type (U_Ent) then
6471 ("Dispatching_Domain can only be defined for task", Nam);
6473 elsif Duplicate_Clause then
6477 -- The expression must be analyzed in the special manner
6478 -- described in "Handling of Default and Per-Object
6479 -- Expressions" in sem.ads.
6481 -- The visibility to the components must be restored
6485 Preanalyze_Spec_Expression
6486 (Expr, RTE (RE_Dispatching_Domain));
6493 ("attribute& cannot be set with definition clause", N);
6500 when Attribute_External_Tag =>
6501 if not Is_Tagged_Type (U_Ent) then
6502 Error_Msg_N ("should be a tagged type", Nam);
6505 if Duplicate_Clause then
6509 Analyze_And_Resolve (Expr, Standard_String);
6511 if not Is_OK_Static_Expression (Expr) then
6512 Flag_Non_Static_Expr
6513 ("static string required for tag name!", Nam);
6516 if not Is_Library_Level_Entity (U_Ent) then
6518 ("??non-unique external tag supplied for &", N, U_Ent);
6520 ("\??same external tag applies to all subprogram calls",
6523 ("\??corresponding internal tag cannot be obtained", N);
6527 --------------------------
6528 -- Implicit_Dereference --
6529 --------------------------
6531 when Attribute_Implicit_Dereference =>
6533 -- Legality checks already performed at the point of the type
6534 -- declaration, aspect is not delayed.
6542 when Attribute_Input =>
6543 Analyze_Stream_TSS_Definition (TSS_Stream_Input);
6544 Set_Has_Specified_Stream_Input (Ent);
6546 ------------------------
6547 -- Interrupt_Priority --
6548 ------------------------
6550 when Attribute_Interrupt_Priority =>
6552 -- Interrupt_Priority attribute definition clause not allowed
6553 -- except from aspect specification.
6555 if From_Aspect_Specification (N) then
6556 if not Is_Concurrent_Type (U_Ent) then
6558 ("Interrupt_Priority can only be defined for task and "
6559 & "protected object", Nam);
6561 elsif Duplicate_Clause then
6565 -- The expression must be analyzed in the special manner
6566 -- described in "Handling of Default and Per-Object
6567 -- Expressions" in sem.ads.
6569 -- The visibility to the components must be restored
6573 Preanalyze_Spec_Expression
6574 (Expr, RTE (RE_Interrupt_Priority));
6578 -- Check the No_Task_At_Interrupt_Priority restriction
6580 if Is_Task_Type (U_Ent) then
6581 Check_Restriction (No_Task_At_Interrupt_Priority, N);
6587 ("attribute& cannot be set with definition clause", N);
6594 when Attribute_Iterable =>
6597 if Nkind (Expr) /= N_Aggregate then
6598 Error_Msg_N ("aspect Iterable must be an aggregate", Expr);
6605 Assoc := First (Component_Associations (Expr));
6606 while Present (Assoc) loop
6607 Analyze (Expression (Assoc));
6609 if not Is_Entity_Name (Expression (Assoc)) then
6610 Error_Msg_N ("value must be a function", Assoc);
6617 ----------------------
6618 -- Iterator_Element --
6619 ----------------------
6621 when Attribute_Iterator_Element =>
6624 if not Is_Entity_Name (Expr)
6625 or else not Is_Type (Entity (Expr))
6627 Error_Msg_N ("aspect Iterator_Element must be a type", Expr);
6634 -- Machine radix attribute definition clause
6636 when Attribute_Machine_Radix => Machine_Radix : declare
6637 Radix : constant Uint := Static_Integer (Expr);
6640 if not Is_Decimal_Fixed_Point_Type (U_Ent) then
6641 Error_Msg_N ("decimal fixed-point type expected for &", Nam);
6643 elsif Duplicate_Clause then
6646 elsif Radix /= No_Uint then
6647 Set_Has_Machine_Radix_Clause (U_Ent);
6648 Set_Has_Non_Standard_Rep (Base_Type (U_Ent));
6653 elsif Radix = 10 then
6654 Set_Machine_Radix_10 (U_Ent);
6657 Error_Msg_N ("machine radix value must be 2 or 10", Expr);
6666 -- Object_Size attribute definition clause
6668 when Attribute_Object_Size => Object_Size : declare
6669 Size : constant Uint := Static_Integer (Expr);
6672 pragma Warnings (Off, Biased);
6675 if not Is_Type (U_Ent) then
6676 Error_Msg_N ("Object_Size cannot be given for &", Nam);
6678 elsif Duplicate_Clause then
6682 Check_Size (Expr, U_Ent, Size, Biased);
6685 Error_Msg_N ("Object_Size must be positive", Expr);
6687 elsif Is_Scalar_Type (U_Ent) then
6688 if Size /= 8 and then Size /= 16 and then Size /= 32
6689 and then UI_Mod (Size, 64) /= 0
6692 ("Object_Size must be 8, 16, 32, or multiple of 64",
6696 elsif Size mod 8 /= 0 then
6697 Error_Msg_N ("Object_Size must be a multiple of 8", Expr);
6700 Set_Esize (U_Ent, Size);
6701 Set_Has_Object_Size_Clause (U_Ent);
6702 Alignment_Check_For_Size_Change (U_Ent, Size);
6710 when Attribute_Output =>
6711 Analyze_Stream_TSS_Definition (TSS_Stream_Output);
6712 Set_Has_Specified_Stream_Output (Ent);
6718 when Attribute_Priority =>
6720 -- Priority attribute definition clause not allowed except from
6721 -- aspect specification.
6723 if From_Aspect_Specification (N) then
6724 if not (Is_Concurrent_Type (U_Ent)
6725 or else Ekind (U_Ent) = E_Procedure)
6728 ("Priority can only be defined for task and protected "
6731 elsif Duplicate_Clause then
6735 -- The expression must be analyzed in the special manner
6736 -- described in "Handling of Default and Per-Object
6737 -- Expressions" in sem.ads.
6739 -- The visibility to the components must be restored
6742 Preanalyze_Spec_Expression (Expr, Standard_Integer);
6745 if not Is_OK_Static_Expression (Expr) then
6746 Check_Restriction (Static_Priorities, Expr);
6752 ("attribute& cannot be set with definition clause", N);
6759 when Attribute_Put_Image =>
6760 Analyze_Put_Image_TSS_Definition;
6766 when Attribute_Read =>
6767 Analyze_Stream_TSS_Definition (TSS_Stream_Read);
6768 Set_Has_Specified_Stream_Read (Ent);
6770 --------------------------
6771 -- Scalar_Storage_Order --
6772 --------------------------
6774 -- Scalar_Storage_Order attribute definition clause
6776 when Attribute_Scalar_Storage_Order =>
6777 if not (Is_Record_Type (U_Ent) or else Is_Array_Type (U_Ent)) then
6779 ("Scalar_Storage_Order can only be defined for record or "
6780 & "array type", Nam);
6782 elsif Duplicate_Clause then
6786 Analyze_And_Resolve (Expr, RTE (RE_Bit_Order));
6788 if Etype (Expr) = Any_Type then
6791 elsif not Is_OK_Static_Expression (Expr) then
6792 Flag_Non_Static_Expr
6793 ("Scalar_Storage_Order requires static expression!", Expr);
6795 elsif (Expr_Value (Expr) = 0) /= Bytes_Big_Endian then
6797 -- Here for the case of a non-default (i.e. non-confirming)
6798 -- Scalar_Storage_Order attribute definition.
6800 if Support_Nondefault_SSO_On_Target then
6801 Set_Reverse_Storage_Order (Base_Type (U_Ent), True);
6804 ("non-default Scalar_Storage_Order not supported on "
6809 -- Clear SSO default indications since explicit setting of the
6810 -- order overrides the defaults.
6812 Set_SSO_Set_Low_By_Default (Base_Type (U_Ent), False);
6813 Set_SSO_Set_High_By_Default (Base_Type (U_Ent), False);
6820 -- Size attribute definition clause
6822 when Attribute_Size => Size : declare
6823 Size : constant Uint := Static_Integer (Expr);
6830 if Duplicate_Clause then
6833 elsif not Is_Type (U_Ent)
6834 and then Ekind (U_Ent) /= E_Variable
6835 and then Ekind (U_Ent) /= E_Constant
6837 Error_Msg_N ("size cannot be given for &", Nam);
6839 elsif Is_Array_Type (U_Ent)
6840 and then not Is_Constrained (U_Ent)
6843 ("size cannot be given for unconstrained array", Nam);
6845 elsif Size /= No_Uint then
6846 if Is_Type (U_Ent) then
6849 Etyp := Etype (U_Ent);
6852 -- Check size, note that Gigi is in charge of checking that the
6853 -- size of an array or record type is OK. Also we do not check
6854 -- the size in the ordinary fixed-point case, since it is too
6855 -- early to do so (there may be subsequent small clause that
6856 -- affects the size). We can check the size if a small clause
6857 -- has already been given.
6859 if not Is_Ordinary_Fixed_Point_Type (U_Ent)
6860 or else Has_Small_Clause (U_Ent)
6862 Check_Size (Expr, Etyp, Size, Biased);
6863 Set_Biased (U_Ent, N, "size clause", Biased);
6866 -- For types set RM_Size and Esize if possible
6868 if Is_Type (U_Ent) then
6869 Set_RM_Size (U_Ent, Size);
6871 -- For elementary types, increase Object_Size to power of 2,
6872 -- but not less than a storage unit in any case (normally
6873 -- this means it will be byte addressable).
6875 -- For all other types, nothing else to do, we leave Esize
6876 -- (object size) unset, the back end will set it from the
6877 -- size and alignment in an appropriate manner.
6879 -- In both cases, we check whether the alignment must be
6880 -- reset in the wake of the size change.
6882 if Is_Elementary_Type (U_Ent) then
6883 if Size <= System_Storage_Unit then
6884 Init_Esize (U_Ent, System_Storage_Unit);
6885 elsif Size <= 16 then
6886 Init_Esize (U_Ent, 16);
6887 elsif Size <= 32 then
6888 Init_Esize (U_Ent, 32);
6890 Set_Esize (U_Ent, (Size + 63) / 64 * 64);
6893 Alignment_Check_For_Size_Change (U_Ent, Esize (U_Ent));
6895 Alignment_Check_For_Size_Change (U_Ent, Size);
6898 -- For objects, set Esize only
6901 if Is_Elementary_Type (Etyp)
6902 and then Size /= System_Storage_Unit
6903 and then Size /= System_Storage_Unit * 2
6904 and then Size /= System_Storage_Unit * 4
6905 and then Size /= System_Storage_Unit * 8
6907 Error_Msg_Uint_1 := UI_From_Int (System_Storage_Unit);
6908 Error_Msg_Uint_2 := Error_Msg_Uint_1 * 8;
6910 ("size for primitive object must be a power of 2 in "
6911 & "the range ^-^", N);
6914 Set_Esize (U_Ent, Size);
6917 Set_Has_Size_Clause (U_Ent);
6925 -- Small attribute definition clause
6927 when Attribute_Small => Small : declare
6928 Implicit_Base : constant Entity_Id := Base_Type (U_Ent);
6932 Analyze_And_Resolve (Expr, Any_Real);
6934 if Etype (Expr) = Any_Type then
6937 elsif not Is_OK_Static_Expression (Expr) then
6938 Flag_Non_Static_Expr
6939 ("small requires static expression!", Expr);
6943 Small := Expr_Value_R (Expr);
6945 if Small <= Ureal_0 then
6946 Error_Msg_N ("small value must be greater than zero", Expr);
6952 if not Is_Ordinary_Fixed_Point_Type (U_Ent) then
6954 ("small requires an ordinary fixed point type", Nam);
6956 elsif Has_Small_Clause (U_Ent) then
6957 Error_Msg_N ("small already given for &", Nam);
6959 elsif Small > Delta_Value (U_Ent) then
6961 ("small value must not be greater than delta value", Nam);
6964 Set_Small_Value (U_Ent, Small);
6965 Set_Small_Value (Implicit_Base, Small);
6966 Set_Has_Small_Clause (U_Ent);
6967 Set_Has_Small_Clause (Implicit_Base);
6968 Set_Has_Non_Standard_Rep (Implicit_Base);
6976 -- Storage_Pool attribute definition clause
6978 when Attribute_Simple_Storage_Pool
6979 | Attribute_Storage_Pool
6981 Storage_Pool : declare
6986 if Ekind (U_Ent) = E_Access_Subprogram_Type then
6988 ("storage pool cannot be given for access-to-subprogram type",
6992 elsif not Ekind_In (U_Ent, E_Access_Type, E_General_Access_Type)
6995 ("storage pool can only be given for access types", Nam);
6998 elsif Is_Derived_Type (U_Ent) then
7000 ("storage pool cannot be given for a derived access type",
7003 elsif Duplicate_Clause then
7006 elsif Present (Associated_Storage_Pool (U_Ent)) then
7007 Error_Msg_N ("storage pool already given for &", Nam);
7011 -- Check for Storage_Size previously given
7014 SS : constant Node_Id :=
7015 Get_Attribute_Definition_Clause
7016 (U_Ent, Attribute_Storage_Size);
7018 if Present (SS) then
7019 Check_Pool_Size_Clash (U_Ent, N, SS);
7023 -- Storage_Pool case
7025 if Id = Attribute_Storage_Pool then
7027 (Expr, Class_Wide_Type (RTE (RE_Root_Storage_Pool)));
7029 -- In the Simple_Storage_Pool case, we allow a variable of any
7030 -- simple storage pool type, so we Resolve without imposing an
7034 Analyze_And_Resolve (Expr);
7036 if not Present (Get_Rep_Pragma
7037 (Etype (Expr), Name_Simple_Storage_Pool_Type))
7040 ("expression must be of a simple storage pool type", Expr);
7044 if not Denotes_Variable (Expr) then
7045 Error_Msg_N ("storage pool must be a variable", Expr);
7049 if Nkind (Expr) = N_Type_Conversion then
7050 T := Etype (Expression (Expr));
7055 -- The Stack_Bounded_Pool is used internally for implementing
7056 -- access types with a Storage_Size. Since it only work properly
7057 -- when used on one specific type, we need to check that it is not
7058 -- hijacked improperly:
7060 -- type T is access Integer;
7061 -- for T'Storage_Size use n;
7062 -- type Q is access Float;
7063 -- for Q'Storage_Size use T'Storage_Size; -- incorrect
7065 if RTE_Available (RE_Stack_Bounded_Pool)
7066 and then Base_Type (T) = RTE (RE_Stack_Bounded_Pool)
7068 Error_Msg_N ("non-shareable internal Pool", Expr);
7072 -- Validate_Remote_Access_To_Class_Wide_Type for attribute
7073 -- Storage_Pool since this attribute cannot be defined for such
7074 -- types (RM E.2.2(17)).
7076 Validate_Remote_Access_To_Class_Wide_Type (N);
7078 -- If the argument is a name that is not an entity name, then
7079 -- we construct a renaming operation to define an entity of
7080 -- type storage pool.
7082 if not Is_Entity_Name (Expr)
7083 and then Is_Object_Reference (Expr)
7085 Pool := Make_Temporary (Loc, 'P', Expr);
7088 Rnode : constant Node_Id :=
7089 Make_Object_Renaming_Declaration (Loc,
7090 Defining_Identifier => Pool,
7092 New_Occurrence_Of (Etype (Expr), Loc),
7096 -- If the attribute definition clause comes from an aspect
7097 -- clause, then insert the renaming before the associated
7098 -- entity's declaration, since the attribute clause has
7099 -- not yet been appended to the declaration list.
7101 if From_Aspect_Specification (N) then
7102 Insert_Before (Parent (Entity (N)), Rnode);
7104 Insert_Before (N, Rnode);
7108 Set_Associated_Storage_Pool (U_Ent, Pool);
7111 elsif Is_Entity_Name (Expr) then
7112 Pool := Entity (Expr);
7114 -- If pool is a renamed object, get original one. This can
7115 -- happen with an explicit renaming, and within instances.
7117 while Present (Renamed_Object (Pool))
7118 and then Is_Entity_Name (Renamed_Object (Pool))
7120 Pool := Entity (Renamed_Object (Pool));
7123 if Present (Renamed_Object (Pool))
7124 and then Nkind (Renamed_Object (Pool)) = N_Type_Conversion
7125 and then Is_Entity_Name (Expression (Renamed_Object (Pool)))
7127 Pool := Entity (Expression (Renamed_Object (Pool)));
7130 Set_Associated_Storage_Pool (U_Ent, Pool);
7132 elsif Nkind (Expr) = N_Type_Conversion
7133 and then Is_Entity_Name (Expression (Expr))
7134 and then Nkind (Original_Node (Expr)) = N_Attribute_Reference
7136 Pool := Entity (Expression (Expr));
7137 Set_Associated_Storage_Pool (U_Ent, Pool);
7140 Error_Msg_N ("incorrect reference to a Storage Pool", Expr);
7149 -- Storage_Size attribute definition clause
7151 when Attribute_Storage_Size => Storage_Size : declare
7152 Btype : constant Entity_Id := Base_Type (U_Ent);
7155 if Is_Task_Type (U_Ent) then
7157 -- Check obsolescent (but never obsolescent if from aspect)
7159 if not From_Aspect_Specification (N) then
7160 Check_Restriction (No_Obsolescent_Features, N);
7162 if Warn_On_Obsolescent_Feature then
7164 ("?j?storage size clause for task is an obsolescent "
7165 & "feature (RM J.9)", N);
7166 Error_Msg_N ("\?j?use Storage_Size pragma instead", N);
7173 if not Is_Access_Type (U_Ent)
7174 and then Ekind (U_Ent) /= E_Task_Type
7176 Error_Msg_N ("storage size cannot be given for &", Nam);
7178 elsif Is_Access_Type (U_Ent) and Is_Derived_Type (U_Ent) then
7180 ("storage size cannot be given for a derived access type",
7183 elsif Duplicate_Clause then
7187 -- Validate_Remote_Access_To_Class_Wide_Type for attribute
7188 -- Storage_Size since this attribute cannot be defined for such
7189 -- types (RM E.2.2(17)).
7191 Validate_Remote_Access_To_Class_Wide_Type (N);
7193 Analyze_And_Resolve (Expr, Any_Integer);
7195 if Is_Access_Type (U_Ent) then
7197 -- Check for Storage_Pool previously given
7200 SP : constant Node_Id :=
7201 Get_Attribute_Definition_Clause
7202 (U_Ent, Attribute_Storage_Pool);
7205 if Present (SP) then
7206 Check_Pool_Size_Clash (U_Ent, SP, N);
7210 -- Special case of for x'Storage_Size use 0
7212 if Is_OK_Static_Expression (Expr)
7213 and then Expr_Value (Expr) = 0
7215 Set_No_Pool_Assigned (Btype);
7219 Set_Has_Storage_Size_Clause (Btype);
7227 when Attribute_Stream_Size => Stream_Size : declare
7228 Size : constant Uint := Static_Integer (Expr);
7231 if Ada_Version <= Ada_95 then
7232 Check_Restriction (No_Implementation_Attributes, N);
7235 if Duplicate_Clause then
7238 elsif Is_Elementary_Type (U_Ent) then
7239 if Size /= System_Storage_Unit
7240 and then Size /= System_Storage_Unit * 2
7241 and then Size /= System_Storage_Unit * 3
7242 and then Size /= System_Storage_Unit * 4
7243 and then Size /= System_Storage_Unit * 8
7246 ("stream size for elementary type must be 8, 16, 24, " &
7249 elsif RM_Size (U_Ent) > Size then
7250 Error_Msg_Uint_1 := RM_Size (U_Ent);
7252 ("stream size for elementary type must be 8, 16, 24, " &
7253 "32 or 64 and at least ^", N);
7256 Set_Has_Stream_Size_Clause (U_Ent);
7259 Error_Msg_N ("Stream_Size cannot be given for &", Nam);
7267 -- Value_Size attribute definition clause
7269 when Attribute_Value_Size => Value_Size : declare
7270 Size : constant Uint := Static_Integer (Expr);
7274 if not Is_Type (U_Ent) then
7275 Error_Msg_N ("Value_Size cannot be given for &", Nam);
7277 elsif Duplicate_Clause then
7280 elsif Is_Array_Type (U_Ent)
7281 and then not Is_Constrained (U_Ent)
7284 ("Value_Size cannot be given for unconstrained array", Nam);
7287 if Is_Elementary_Type (U_Ent) then
7288 Check_Size (Expr, U_Ent, Size, Biased);
7289 Set_Biased (U_Ent, N, "value size clause", Biased);
7292 Set_RM_Size (U_Ent, Size);
7296 -----------------------
7297 -- Variable_Indexing --
7298 -----------------------
7300 when Attribute_Variable_Indexing =>
7301 Check_Indexing_Functions;
7307 when Attribute_Write =>
7308 Analyze_Stream_TSS_Definition (TSS_Stream_Write);
7309 Set_Has_Specified_Stream_Write (Ent);
7311 -- All other attributes cannot be set
7315 ("attribute& cannot be set with definition clause", N);
7318 -- The test for the type being frozen must be performed after any
7319 -- expression the clause has been analyzed since the expression itself
7320 -- might cause freezing that makes the clause illegal.
7322 if Rep_Item_Too_Late (U_Ent, N, FOnly) then
7325 end Analyze_Attribute_Definition_Clause;
7327 ----------------------------
7328 -- Analyze_Code_Statement --
7329 ----------------------------
7331 procedure Analyze_Code_Statement (N : Node_Id) is
7332 HSS : constant Node_Id := Parent (N);
7333 SBody : constant Node_Id := Parent (HSS);
7334 Subp : constant Entity_Id := Current_Scope;
7341 -- Accept foreign code statements for CodePeer. The analysis is skipped
7342 -- to avoid rejecting unrecognized constructs.
7344 if CodePeer_Mode then
7349 -- Analyze and check we get right type, note that this implements the
7350 -- requirement (RM 13.8(1)) that Machine_Code be with'ed, since that is
7351 -- the only way that Asm_Insn could possibly be visible.
7353 Analyze_And_Resolve (Expression (N));
7355 if Etype (Expression (N)) = Any_Type then
7357 elsif Etype (Expression (N)) /= RTE (RE_Asm_Insn) then
7358 Error_Msg_N ("incorrect type for code statement", N);
7362 Check_Code_Statement (N);
7364 -- Make sure we appear in the handled statement sequence of a subprogram
7367 if Nkind (HSS) /= N_Handled_Sequence_Of_Statements
7368 or else Nkind (SBody) /= N_Subprogram_Body
7371 ("code statement can only appear in body of subprogram", N);
7375 -- Do remaining checks (RM 13.8(3)) if not already done
7377 if not Is_Machine_Code_Subprogram (Subp) then
7378 Set_Is_Machine_Code_Subprogram (Subp);
7380 -- No exception handlers allowed
7382 if Present (Exception_Handlers (HSS)) then
7384 ("exception handlers not permitted in machine code subprogram",
7385 First (Exception_Handlers (HSS)));
7388 -- No declarations other than use clauses and pragmas (we allow
7389 -- certain internally generated declarations as well).
7391 Decl := First (Declarations (SBody));
7392 while Present (Decl) loop
7393 DeclO := Original_Node (Decl);
7394 if Comes_From_Source (DeclO)
7395 and not Nkind_In (DeclO, N_Pragma,
7396 N_Use_Package_Clause,
7398 N_Implicit_Label_Declaration)
7401 ("this declaration not allowed in machine code subprogram",
7408 -- No statements other than code statements, pragmas, and labels.
7409 -- Again we allow certain internally generated statements.
7411 -- In Ada 2012, qualified expressions are names, and the code
7412 -- statement is initially parsed as a procedure call.
7414 Stmt := First (Statements (HSS));
7415 while Present (Stmt) loop
7416 StmtO := Original_Node (Stmt);
7418 -- A procedure call transformed into a code statement is OK
7420 if Ada_Version >= Ada_2012
7421 and then Nkind (StmtO) = N_Procedure_Call_Statement
7422 and then Nkind (Name (StmtO)) = N_Qualified_Expression
7426 elsif Comes_From_Source (StmtO)
7427 and then not Nkind_In (StmtO, N_Pragma,
7432 ("this statement is not allowed in machine code subprogram",
7439 end Analyze_Code_Statement;
7441 -----------------------------------------------
7442 -- Analyze_Enumeration_Representation_Clause --
7443 -----------------------------------------------
7445 procedure Analyze_Enumeration_Representation_Clause (N : Node_Id) is
7446 Ident : constant Node_Id := Identifier (N);
7447 Aggr : constant Node_Id := Array_Aggregate (N);
7448 Enumtype : Entity_Id;
7455 Err : Boolean := False;
7456 -- Set True to avoid cascade errors and crashes on incorrect source code
7458 Lo : constant Uint := Expr_Value (Type_Low_Bound (Universal_Integer));
7459 Hi : constant Uint := Expr_Value (Type_High_Bound (Universal_Integer));
7460 -- Allowed range of universal integer (= allowed range of enum lit vals)
7464 -- Minimum and maximum values of entries
7466 Max_Node : Node_Id := Empty; -- init to avoid warning
7467 -- Pointer to node for literal providing max value
7470 if Ignore_Rep_Clauses then
7471 Kill_Rep_Clause (N);
7475 -- Ignore enumeration rep clauses by default in CodePeer mode,
7476 -- unless -gnatd.I is specified, as a work around for potential false
7477 -- positive messages.
7479 if CodePeer_Mode and not Debug_Flag_Dot_II then
7483 -- First some basic error checks
7486 Enumtype := Entity (Ident);
7488 if Enumtype = Any_Type
7489 or else Rep_Item_Too_Early (Enumtype, N)
7493 Enumtype := Underlying_Type (Enumtype);
7496 if not Is_Enumeration_Type (Enumtype) then
7498 ("enumeration type required, found}",
7499 Ident, First_Subtype (Enumtype));
7503 -- Ignore rep clause on generic actual type. This will already have
7504 -- been flagged on the template as an error, and this is the safest
7505 -- way to ensure we don't get a junk cascaded message in the instance.
7507 if Is_Generic_Actual_Type (Enumtype) then
7510 -- Type must be in current scope
7512 elsif Scope (Enumtype) /= Current_Scope then
7513 Error_Msg_N ("type must be declared in this scope", Ident);
7516 -- Type must be a first subtype
7518 elsif not Is_First_Subtype (Enumtype) then
7519 Error_Msg_N ("cannot give enumeration rep clause for subtype", N);
7522 -- Ignore duplicate rep clause
7524 elsif Has_Enumeration_Rep_Clause (Enumtype) then
7525 Error_Msg_N ("duplicate enumeration rep clause ignored", N);
7528 -- Don't allow rep clause for standard [wide_[wide_]]character
7530 elsif Is_Standard_Character_Type (Enumtype) then
7531 Error_Msg_N ("enumeration rep clause not allowed for this type", N);
7534 -- Check that the expression is a proper aggregate (no parentheses)
7536 elsif Paren_Count (Aggr) /= 0 then
7538 ("extra parentheses surrounding aggregate not allowed",
7542 -- All tests passed, so set rep clause in place
7545 Set_Has_Enumeration_Rep_Clause (Enumtype);
7546 Set_Has_Enumeration_Rep_Clause (Base_Type (Enumtype));
7549 -- Now we process the aggregate. Note that we don't use the normal
7550 -- aggregate code for this purpose, because we don't want any of the
7551 -- normal expansion activities, and a number of special semantic
7552 -- rules apply (including the component type being any integer type)
7554 Elit := First_Literal (Enumtype);
7556 -- First the positional entries if any
7558 if Present (Expressions (Aggr)) then
7559 Expr := First (Expressions (Aggr));
7560 while Present (Expr) loop
7562 Error_Msg_N ("too many entries in aggregate", Expr);
7566 Val := Static_Integer (Expr);
7568 -- Err signals that we found some incorrect entries processing
7569 -- the list. The final checks for completeness and ordering are
7570 -- skipped in this case.
7572 if Val = No_Uint then
7575 elsif Val < Lo or else Hi < Val then
7576 Error_Msg_N ("value outside permitted range", Expr);
7580 Set_Enumeration_Rep (Elit, Val);
7581 Set_Enumeration_Rep_Expr (Elit, Expr);
7587 -- Now process the named entries if present
7589 if Present (Component_Associations (Aggr)) then
7590 Assoc := First (Component_Associations (Aggr));
7591 while Present (Assoc) loop
7592 Choice := First (Choices (Assoc));
7594 if Present (Next (Choice)) then
7596 ("multiple choice not allowed here", Next (Choice));
7600 if Nkind (Choice) = N_Others_Choice then
7601 Error_Msg_N ("others choice not allowed here", Choice);
7604 elsif Nkind (Choice) = N_Range then
7606 -- ??? should allow zero/one element range here
7608 Error_Msg_N ("range not allowed here", Choice);
7612 Analyze_And_Resolve (Choice, Enumtype);
7614 if Error_Posted (Choice) then
7619 if Is_Entity_Name (Choice)
7620 and then Is_Type (Entity (Choice))
7622 Error_Msg_N ("subtype name not allowed here", Choice);
7625 -- ??? should allow static subtype with zero/one entry
7627 elsif Etype (Choice) = Base_Type (Enumtype) then
7628 if not Is_OK_Static_Expression (Choice) then
7629 Flag_Non_Static_Expr
7630 ("non-static expression used for choice!", Choice);
7634 Elit := Expr_Value_E (Choice);
7636 if Present (Enumeration_Rep_Expr (Elit)) then
7638 Sloc (Enumeration_Rep_Expr (Elit));
7640 ("representation for& previously given#",
7645 Set_Enumeration_Rep_Expr (Elit, Expression (Assoc));
7647 Expr := Expression (Assoc);
7648 Val := Static_Integer (Expr);
7650 if Val = No_Uint then
7653 elsif Val < Lo or else Hi < Val then
7654 Error_Msg_N ("value outside permitted range", Expr);
7658 Set_Enumeration_Rep (Elit, Val);
7668 -- Aggregate is fully processed. Now we check that a full set of
7669 -- representations was given, and that they are in range and in order.
7670 -- These checks are only done if no other errors occurred.
7676 Elit := First_Literal (Enumtype);
7677 while Present (Elit) loop
7678 if No (Enumeration_Rep_Expr (Elit)) then
7679 Error_Msg_NE ("missing representation for&!", N, Elit);
7682 Val := Enumeration_Rep (Elit);
7684 if Min = No_Uint then
7688 if Val /= No_Uint then
7689 if Max /= No_Uint and then Val <= Max then
7691 ("enumeration value for& not ordered!",
7692 Enumeration_Rep_Expr (Elit), Elit);
7695 Max_Node := Enumeration_Rep_Expr (Elit);
7699 -- If there is at least one literal whose representation is not
7700 -- equal to the Pos value, then note that this enumeration type
7701 -- has a non-standard representation.
7703 if Val /= Enumeration_Pos (Elit) then
7704 Set_Has_Non_Standard_Rep (Base_Type (Enumtype));
7711 -- Now set proper size information
7714 Minsize : Uint := UI_From_Int (Minimum_Size (Enumtype));
7717 if Has_Size_Clause (Enumtype) then
7719 -- All OK, if size is OK now
7721 if RM_Size (Enumtype) >= Minsize then
7725 -- Try if we can get by with biasing
7728 UI_From_Int (Minimum_Size (Enumtype, Biased => True));
7730 -- Error message if even biasing does not work
7732 if RM_Size (Enumtype) < Minsize then
7733 Error_Msg_Uint_1 := RM_Size (Enumtype);
7734 Error_Msg_Uint_2 := Max;
7736 ("previously given size (^) is too small "
7737 & "for this value (^)", Max_Node);
7739 -- If biasing worked, indicate that we now have biased rep
7743 (Enumtype, Size_Clause (Enumtype), "size clause");
7748 Set_RM_Size (Enumtype, Minsize);
7749 Set_Enum_Esize (Enumtype);
7752 Set_RM_Size (Base_Type (Enumtype), RM_Size (Enumtype));
7753 Set_Esize (Base_Type (Enumtype), Esize (Enumtype));
7754 Set_Alignment (Base_Type (Enumtype), Alignment (Enumtype));
7758 -- We repeat the too late test in case it froze itself
7760 if Rep_Item_Too_Late (Enumtype, N) then
7763 end Analyze_Enumeration_Representation_Clause;
7765 ----------------------------
7766 -- Analyze_Free_Statement --
7767 ----------------------------
7769 procedure Analyze_Free_Statement (N : Node_Id) is
7771 Analyze (Expression (N));
7772 end Analyze_Free_Statement;
7774 ---------------------------
7775 -- Analyze_Freeze_Entity --
7776 ---------------------------
7778 procedure Analyze_Freeze_Entity (N : Node_Id) is
7780 Freeze_Entity_Checks (N);
7781 end Analyze_Freeze_Entity;
7783 -----------------------------------
7784 -- Analyze_Freeze_Generic_Entity --
7785 -----------------------------------
7787 procedure Analyze_Freeze_Generic_Entity (N : Node_Id) is
7788 E : constant Entity_Id := Entity (N);
7791 if not Is_Frozen (E) and then Has_Delayed_Aspects (E) then
7792 Analyze_Aspects_At_Freeze_Point (E);
7795 Freeze_Entity_Checks (N);
7796 end Analyze_Freeze_Generic_Entity;
7798 ------------------------------------------
7799 -- Analyze_Record_Representation_Clause --
7800 ------------------------------------------
7802 -- Note: we check as much as we can here, but we can't do any checks
7803 -- based on the position values (e.g. overlap checks) until freeze time
7804 -- because especially in Ada 2005 (machine scalar mode), the processing
7805 -- for non-standard bit order can substantially change the positions.
7806 -- See procedure Check_Record_Representation_Clause (called from Freeze)
7807 -- for the remainder of this processing.
7809 procedure Analyze_Record_Representation_Clause (N : Node_Id) is
7810 Ident : constant Node_Id := Identifier (N);
7818 Rectype : Entity_Id;
7821 function Is_Inherited (Comp : Entity_Id) return Boolean;
7822 -- True if Comp is an inherited component in a record extension
7828 function Is_Inherited (Comp : Entity_Id) return Boolean is
7829 Comp_Base : Entity_Id;
7832 if Ekind (Rectype) = E_Record_Subtype then
7833 Comp_Base := Original_Record_Component (Comp);
7838 return Comp_Base /= Original_Record_Component (Comp_Base);
7843 Is_Record_Extension : Boolean;
7844 -- True if Rectype is a record extension
7846 CR_Pragma : Node_Id := Empty;
7847 -- Points to N_Pragma node if Complete_Representation pragma present
7849 -- Start of processing for Analyze_Record_Representation_Clause
7852 if Ignore_Rep_Clauses then
7853 Kill_Rep_Clause (N);
7858 Rectype := Entity (Ident);
7860 if Rectype = Any_Type or else Rep_Item_Too_Early (Rectype, N) then
7863 Rectype := Underlying_Type (Rectype);
7866 -- First some basic error checks
7868 if not Is_Record_Type (Rectype) then
7870 ("record type required, found}", Ident, First_Subtype (Rectype));
7873 elsif Scope (Rectype) /= Current_Scope then
7874 Error_Msg_N ("type must be declared in this scope", N);
7877 elsif not Is_First_Subtype (Rectype) then
7878 Error_Msg_N ("cannot give record rep clause for subtype", N);
7881 elsif Has_Record_Rep_Clause (Rectype) then
7882 Error_Msg_N ("duplicate record rep clause ignored", N);
7885 elsif Rep_Item_Too_Late (Rectype, N) then
7889 -- We know we have a first subtype, now possibly go to the anonymous
7890 -- base type to determine whether Rectype is a record extension.
7892 Recdef := Type_Definition (Declaration_Node (Base_Type (Rectype)));
7893 Is_Record_Extension :=
7894 Nkind (Recdef) = N_Derived_Type_Definition
7895 and then Present (Record_Extension_Part (Recdef));
7897 if Present (Mod_Clause (N)) then
7899 M : constant Node_Id := Mod_Clause (N);
7900 P : constant List_Id := Pragmas_Before (M);
7904 Check_Restriction (No_Obsolescent_Features, Mod_Clause (N));
7906 if Warn_On_Obsolescent_Feature then
7908 ("?j?mod clause is an obsolescent feature (RM J.8)", N);
7910 ("\?j?use alignment attribute definition clause instead", N);
7917 -- Get the alignment value to perform error checking
7919 Ignore := Get_Alignment_Value (Expression (M));
7923 -- For untagged types, clear any existing component clauses for the
7924 -- type. If the type is derived, this is what allows us to override
7925 -- a rep clause for the parent. For type extensions, the representation
7926 -- of the inherited components is inherited, so we want to keep previous
7927 -- component clauses for completeness.
7929 if not Is_Tagged_Type (Rectype) then
7930 Comp := First_Component_Or_Discriminant (Rectype);
7931 while Present (Comp) loop
7932 Set_Component_Clause (Comp, Empty);
7933 Next_Component_Or_Discriminant (Comp);
7937 -- All done if no component clauses
7939 CC := First (Component_Clauses (N));
7945 -- A representation like this applies to the base type
7947 Set_Has_Record_Rep_Clause (Base_Type (Rectype));
7948 Set_Has_Non_Standard_Rep (Base_Type (Rectype));
7949 Set_Has_Specified_Layout (Base_Type (Rectype));
7951 -- Process the component clauses
7953 while Present (CC) loop
7957 if Nkind (CC) = N_Pragma then
7960 -- The only pragma of interest is Complete_Representation
7962 if Pragma_Name (CC) = Name_Complete_Representation then
7966 -- Processing for real component clause
7969 Posit := Static_Integer (Position (CC));
7970 Fbit := Static_Integer (First_Bit (CC));
7971 Lbit := Static_Integer (Last_Bit (CC));
7974 and then Fbit /= No_Uint
7975 and then Lbit /= No_Uint
7978 Error_Msg_N ("position cannot be negative", Position (CC));
7981 Error_Msg_N ("first bit cannot be negative", First_Bit (CC));
7983 -- The Last_Bit specified in a component clause must not be
7984 -- less than the First_Bit minus one (RM-13.5.1(10)).
7986 elsif Lbit < Fbit - 1 then
7988 ("last bit cannot be less than first bit minus one",
7991 -- Values look OK, so find the corresponding record component
7992 -- Even though the syntax allows an attribute reference for
7993 -- implementation-defined components, GNAT does not allow the
7994 -- tag to get an explicit position.
7996 elsif Nkind (Component_Name (CC)) = N_Attribute_Reference then
7997 if Attribute_Name (Component_Name (CC)) = Name_Tag then
7998 Error_Msg_N ("position of tag cannot be specified", CC);
8000 Error_Msg_N ("illegal component name", CC);
8004 Comp := First_Entity (Rectype);
8005 while Present (Comp) loop
8006 exit when Chars (Comp) = Chars (Component_Name (CC));
8012 -- Maybe component of base type that is absent from
8013 -- statically constrained first subtype.
8015 Comp := First_Entity (Base_Type (Rectype));
8016 while Present (Comp) loop
8017 exit when Chars (Comp) = Chars (Component_Name (CC));
8024 ("component clause is for non-existent field", CC);
8026 -- Ada 2012 (AI05-0026): Any name that denotes a
8027 -- discriminant of an object of an unchecked union type
8028 -- shall not occur within a record_representation_clause.
8030 -- The general restriction of using record rep clauses on
8031 -- Unchecked_Union types has now been lifted. Since it is
8032 -- possible to introduce a record rep clause which mentions
8033 -- the discriminant of an Unchecked_Union in non-Ada 2012
8034 -- code, this check is applied to all versions of the
8037 elsif Ekind (Comp) = E_Discriminant
8038 and then Is_Unchecked_Union (Rectype)
8041 ("cannot reference discriminant of unchecked union",
8042 Component_Name (CC));
8044 elsif Is_Record_Extension and then Is_Inherited (Comp) then
8046 ("component clause not allowed for inherited "
8047 & "component&", CC, Comp);
8049 elsif Present (Component_Clause (Comp)) then
8051 -- Diagnose duplicate rep clause, or check consistency
8052 -- if this is an inherited component. In a double fault,
8053 -- there may be a duplicate inconsistent clause for an
8054 -- inherited component.
8056 if Scope (Original_Record_Component (Comp)) = Rectype
8057 or else Parent (Component_Clause (Comp)) = N
8059 Error_Msg_Sloc := Sloc (Component_Clause (Comp));
8060 Error_Msg_N ("component clause previously given#", CC);
8064 Rep1 : constant Node_Id := Component_Clause (Comp);
8066 if Intval (Position (Rep1)) /=
8067 Intval (Position (CC))
8068 or else Intval (First_Bit (Rep1)) /=
8069 Intval (First_Bit (CC))
8070 or else Intval (Last_Bit (Rep1)) /=
8071 Intval (Last_Bit (CC))
8074 ("component clause inconsistent with "
8075 & "representation of ancestor", CC);
8077 elsif Warn_On_Redundant_Constructs then
8079 ("?r?redundant confirming component clause "
8080 & "for component!", CC);
8085 -- Normal case where this is the first component clause we
8086 -- have seen for this entity, so set it up properly.
8089 -- Make reference for field in record rep clause and set
8090 -- appropriate entity field in the field identifier.
8093 (Comp, Component_Name (CC), Set_Ref => False);
8094 Set_Entity (Component_Name (CC), Comp);
8096 -- Update Fbit and Lbit to the actual bit number
8098 Fbit := Fbit + UI_From_Int (SSU) * Posit;
8099 Lbit := Lbit + UI_From_Int (SSU) * Posit;
8101 if Has_Size_Clause (Rectype)
8102 and then RM_Size (Rectype) <= Lbit
8104 Error_Msg_Uint_1 := RM_Size (Rectype);
8105 Error_Msg_Uint_2 := Lbit + 1;
8106 Error_Msg_N ("bit number out of range of specified "
8107 & "size (expected ^, got ^)",
8110 Set_Component_Clause (Comp, CC);
8111 Set_Component_Bit_Offset (Comp, Fbit);
8112 Set_Esize (Comp, 1 + (Lbit - Fbit));
8113 Set_Normalized_First_Bit (Comp, Fbit mod SSU);
8114 Set_Normalized_Position (Comp, Fbit / SSU);
8116 Set_Normalized_Position_Max
8117 (Comp, Normalized_Position (Comp));
8119 if Warn_On_Overridden_Size
8120 and then Has_Size_Clause (Etype (Comp))
8121 and then RM_Size (Etype (Comp)) /= Esize (Comp)
8124 ("?S?component size overrides size clause for&",
8125 Component_Name (CC), Etype (Comp));
8129 (Component_Name (CC),
8135 (Comp, First_Node (CC), "component clause", Biased);
8137 -- This information is also set in the corresponding
8138 -- component of the base type, found by accessing the
8139 -- Original_Record_Component link if it is present.
8141 Ocomp := Original_Record_Component (Comp);
8143 if Present (Ocomp) and then Ocomp /= Comp then
8144 Set_Component_Clause (Ocomp, CC);
8145 Set_Component_Bit_Offset (Ocomp, Fbit);
8146 Set_Esize (Ocomp, 1 + (Lbit - Fbit));
8147 Set_Normalized_First_Bit (Ocomp, Fbit mod SSU);
8148 Set_Normalized_Position (Ocomp, Fbit / SSU);
8150 Set_Normalized_Position_Max
8151 (Ocomp, Normalized_Position (Ocomp));
8153 -- Note: we don't use Set_Biased here, because we
8154 -- already gave a warning above if needed, and we
8155 -- would get a duplicate for the same name here.
8157 Set_Has_Biased_Representation
8158 (Ocomp, Has_Biased_Representation (Comp));
8161 if Esize (Comp) < 0 then
8162 Error_Msg_N ("component size is negative", CC);
8173 -- Check missing components if Complete_Representation pragma appeared
8175 if Present (CR_Pragma) then
8176 Comp := First_Component_Or_Discriminant (Rectype);
8177 while Present (Comp) loop
8178 if No (Component_Clause (Comp)) then
8180 ("missing component clause for &", CR_Pragma, Comp);
8183 Next_Component_Or_Discriminant (Comp);
8186 -- Give missing components warning if required
8188 elsif Warn_On_Unrepped_Components then
8190 Num_Repped_Components : Nat := 0;
8191 Num_Unrepped_Components : Nat := 0;
8194 -- First count number of repped and unrepped components
8196 Comp := First_Component_Or_Discriminant (Rectype);
8197 while Present (Comp) loop
8198 if Present (Component_Clause (Comp)) then
8199 Num_Repped_Components := Num_Repped_Components + 1;
8201 Num_Unrepped_Components := Num_Unrepped_Components + 1;
8204 Next_Component_Or_Discriminant (Comp);
8207 -- We are only interested in the case where there is at least one
8208 -- unrepped component, and at least half the components have rep
8209 -- clauses. We figure that if less than half have them, then the
8210 -- partial rep clause is really intentional. If the component
8211 -- type has no underlying type set at this point (as for a generic
8212 -- formal type), we don't know enough to give a warning on the
8215 if Num_Unrepped_Components > 0
8216 and then Num_Unrepped_Components < Num_Repped_Components
8218 Comp := First_Component_Or_Discriminant (Rectype);
8219 while Present (Comp) loop
8220 if No (Component_Clause (Comp))
8221 and then Comes_From_Source (Comp)
8222 and then Present (Underlying_Type (Etype (Comp)))
8223 and then (Is_Scalar_Type (Underlying_Type (Etype (Comp)))
8224 or else Size_Known_At_Compile_Time
8225 (Underlying_Type (Etype (Comp))))
8226 and then not Has_Warnings_Off (Rectype)
8228 -- Ignore discriminant in unchecked union, since it is
8229 -- not there, and cannot have a component clause.
8231 and then (not Is_Unchecked_Union (Rectype)
8232 or else Ekind (Comp) /= E_Discriminant)
8234 Error_Msg_Sloc := Sloc (Comp);
8236 ("?C?no component clause given for & declared #",
8240 Next_Component_Or_Discriminant (Comp);
8245 end Analyze_Record_Representation_Clause;
8247 -------------------------------------
8248 -- Build_Discrete_Static_Predicate --
8249 -------------------------------------
8251 procedure Build_Discrete_Static_Predicate
8256 Loc : constant Source_Ptr := Sloc (Expr);
8258 Non_Static : exception;
8259 -- Raised if something non-static is found
8261 Btyp : constant Entity_Id := Base_Type (Typ);
8263 BLo : constant Uint := Expr_Value (Type_Low_Bound (Btyp));
8264 BHi : constant Uint := Expr_Value (Type_High_Bound (Btyp));
8265 -- Low bound and high bound value of base type of Typ
8269 -- Bounds for constructing the static predicate. We use the bound of the
8270 -- subtype if it is static, otherwise the corresponding base type bound.
8271 -- Note: a non-static subtype can have a static predicate.
8276 -- One entry in a Rlist value, a single REnt (range entry) value denotes
8277 -- one range from Lo to Hi. To represent a single value range Lo = Hi =
8280 type RList is array (Nat range <>) of REnt;
8281 -- A list of ranges. The ranges are sorted in increasing order, and are
8282 -- disjoint (there is a gap of at least one value between each range in
8283 -- the table). A value is in the set of ranges in Rlist if it lies
8284 -- within one of these ranges.
8286 False_Range : constant RList :=
8287 RList'(1 .. 0 => REnt'(No_Uint, No_Uint));
8288 -- An empty set of ranges represents a range list that can never be
8289 -- satisfied, since there are no ranges in which the value could lie,
8290 -- so it does not lie in any of them. False_Range is a canonical value
8291 -- for this empty set, but general processing should test for an Rlist
8292 -- with length zero (see Is_False predicate), since other null ranges
8293 -- may appear which must be treated as False.
8295 True_Range : constant RList := RList'(1 => REnt'(BLo, BHi));
8296 -- Range representing True, value must be in the base range
8298 function "and" (Left : RList; Right : RList) return RList;
8299 -- And's together two range lists, returning a range list. This is a set
8300 -- intersection operation.
8302 function "or" (Left : RList; Right : RList) return RList;
8303 -- Or's together two range lists, returning a range list. This is a set
8306 function "not" (Right : RList) return RList;
8307 -- Returns complement of a given range list, i.e. a range list
8308 -- representing all the values in TLo .. THi that are not in the input
8311 function Build_Val (V : Uint) return Node_Id;
8312 -- Return an analyzed N_Identifier node referencing this value, suitable
8313 -- for use as an entry in the Static_Discrte_Predicate list. This node
8314 -- is typed with the base type.
8316 function Build_Range (Lo : Uint; Hi : Uint) return Node_Id;
8317 -- Return an analyzed N_Range node referencing this range, suitable for
8318 -- use as an entry in the Static_Discrete_Predicate list. This node is
8319 -- typed with the base type.
8321 function Get_RList (Exp : Node_Id) return RList;
8322 -- This is a recursive routine that converts the given expression into a
8323 -- list of ranges, suitable for use in building the static predicate.
8325 function Is_False (R : RList) return Boolean;
8326 pragma Inline (Is_False);
8327 -- Returns True if the given range list is empty, and thus represents a
8328 -- False list of ranges that can never be satisfied.
8330 function Is_True (R : RList) return Boolean;
8331 -- Returns True if R trivially represents the True predicate by having a
8332 -- single range from BLo to BHi.
8334 function Is_Type_Ref (N : Node_Id) return Boolean;
8335 pragma Inline (Is_Type_Ref);
8336 -- Returns if True if N is a reference to the type for the predicate in
8337 -- the expression (i.e. if it is an identifier whose Chars field matches
8338 -- the Nam given in the call). N must not be parenthesized, if the type
8339 -- name appears in parens, this routine will return False.
8341 function Lo_Val (N : Node_Id) return Uint;
8342 -- Given an entry from a Static_Discrete_Predicate list that is either
8343 -- a static expression or static range, gets either the expression value
8344 -- or the low bound of the range.
8346 function Hi_Val (N : Node_Id) return Uint;
8347 -- Given an entry from a Static_Discrete_Predicate list that is either
8348 -- a static expression or static range, gets either the expression value
8349 -- or the high bound of the range.
8351 function Membership_Entry (N : Node_Id) return RList;
8352 -- Given a single membership entry (range, value, or subtype), returns
8353 -- the corresponding range list. Raises Static_Error if not static.
8355 function Membership_Entries (N : Node_Id) return RList;
8356 -- Given an element on an alternatives list of a membership operation,
8357 -- returns the range list corresponding to this entry and all following
8358 -- entries (i.e. returns the "or" of this list of values).
8360 function Stat_Pred (Typ : Entity_Id) return RList;
8361 -- Given a type, if it has a static predicate, then return the predicate
8362 -- as a range list, otherwise raise Non_Static.
8368 function "and" (Left : RList; Right : RList) return RList is
8370 -- First range of result
8372 SLeft : Nat := Left'First;
8373 -- Start of rest of left entries
8375 SRight : Nat := Right'First;
8376 -- Start of rest of right entries
8379 -- If either range is True, return the other
8381 if Is_True (Left) then
8383 elsif Is_True (Right) then
8387 -- If either range is False, return False
8389 if Is_False (Left) or else Is_False (Right) then
8393 -- Loop to remove entries at start that are disjoint, and thus just
8394 -- get discarded from the result entirely.
8397 -- If no operands left in either operand, result is false
8399 if SLeft > Left'Last or else SRight > Right'Last then
8402 -- Discard first left operand entry if disjoint with right
8404 elsif Left (SLeft).Hi < Right (SRight).Lo then
8407 -- Discard first right operand entry if disjoint with left
8409 elsif Right (SRight).Hi < Left (SLeft).Lo then
8410 SRight := SRight + 1;
8412 -- Otherwise we have an overlapping entry
8419 -- Now we have two non-null operands, and first entries overlap. The
8420 -- first entry in the result will be the overlapping part of these
8423 FEnt := REnt'(Lo => UI_Max (Left (SLeft).Lo, Right (SRight).Lo),
8424 Hi => UI_Min (Left (SLeft).Hi, Right (SRight).Hi));
8426 -- Now we can remove the entry that ended at a lower value, since its
8427 -- contribution is entirely contained in Fent.
8429 if Left (SLeft).Hi <= Right (SRight).Hi then
8432 SRight := SRight + 1;
8435 -- Compute result by concatenating this first entry with the "and" of
8436 -- the remaining parts of the left and right operands. Note that if
8437 -- either of these is empty, "and" will yield empty, so that we will
8438 -- end up with just Fent, which is what we want in that case.
8441 FEnt & (Left (SLeft .. Left'Last) and Right (SRight .. Right'Last));
8448 function "not" (Right : RList) return RList is
8450 -- Return True if False range
8452 if Is_False (Right) then
8456 -- Return False if True range
8458 if Is_True (Right) then
8462 -- Here if not trivial case
8465 Result : RList (1 .. Right'Length + 1);
8466 -- May need one more entry for gap at beginning and end
8469 -- Number of entries stored in Result
8474 if Right (Right'First).Lo > TLo then
8476 Result (Count) := REnt'(TLo, Right (Right'First).Lo - 1);
8479 -- Gaps between ranges
8481 for J in Right'First .. Right'Last - 1 loop
8483 Result (Count) := REnt'(Right (J).Hi + 1, Right (J + 1).Lo - 1);
8488 if Right (Right'Last).Hi < THi then
8490 Result (Count) := REnt'(Right (Right'Last).Hi + 1, THi);
8493 return Result (1 .. Count);
8501 function "or" (Left : RList; Right : RList) return RList is
8503 -- First range of result
8505 SLeft : Nat := Left'First;
8506 -- Start of rest of left entries
8508 SRight : Nat := Right'First;
8509 -- Start of rest of right entries
8512 -- If either range is True, return True
8514 if Is_True (Left) or else Is_True (Right) then
8518 -- If either range is False (empty), return the other
8520 if Is_False (Left) then
8522 elsif Is_False (Right) then
8526 -- Initialize result first entry from left or right operand depending
8527 -- on which starts with the lower range.
8529 if Left (SLeft).Lo < Right (SRight).Lo then
8530 FEnt := Left (SLeft);
8533 FEnt := Right (SRight);
8534 SRight := SRight + 1;
8537 -- This loop eats ranges from left and right operands that are
8538 -- contiguous with the first range we are gathering.
8541 -- Eat first entry in left operand if contiguous or overlapped by
8542 -- gathered first operand of result.
8544 if SLeft <= Left'Last
8545 and then Left (SLeft).Lo <= FEnt.Hi + 1
8547 FEnt.Hi := UI_Max (FEnt.Hi, Left (SLeft).Hi);
8550 -- Eat first entry in right operand if contiguous or overlapped by
8551 -- gathered right operand of result.
8553 elsif SRight <= Right'Last
8554 and then Right (SRight).Lo <= FEnt.Hi + 1
8556 FEnt.Hi := UI_Max (FEnt.Hi, Right (SRight).Hi);
8557 SRight := SRight + 1;
8559 -- All done if no more entries to eat
8566 -- Obtain result as the first entry we just computed, concatenated
8567 -- to the "or" of the remaining results (if one operand is empty,
8568 -- this will just concatenate with the other
8571 FEnt & (Left (SLeft .. Left'Last) or Right (SRight .. Right'Last));
8578 function Build_Range (Lo : Uint; Hi : Uint) return Node_Id is
8583 Low_Bound => Build_Val (Lo),
8584 High_Bound => Build_Val (Hi));
8585 Set_Etype (Result, Btyp);
8586 Set_Analyzed (Result);
8594 function Build_Val (V : Uint) return Node_Id is
8598 if Is_Enumeration_Type (Typ) then
8599 Result := Get_Enum_Lit_From_Pos (Typ, V, Loc);
8601 Result := Make_Integer_Literal (Loc, V);
8604 Set_Etype (Result, Btyp);
8605 Set_Is_Static_Expression (Result);
8606 Set_Analyzed (Result);
8614 function Get_RList (Exp : Node_Id) return RList is
8619 -- Static expression can only be true or false
8621 if Is_OK_Static_Expression (Exp) then
8622 if Expr_Value (Exp) = 0 then
8629 -- Otherwise test node type
8640 return Get_RList (Left_Opnd (Exp))
8642 Get_RList (Right_Opnd (Exp));
8649 return Get_RList (Left_Opnd (Exp))
8651 Get_RList (Right_Opnd (Exp));
8656 return not Get_RList (Right_Opnd (Exp));
8658 -- Comparisons of type with static value
8660 when N_Op_Compare =>
8662 -- Type is left operand
8664 if Is_Type_Ref (Left_Opnd (Exp))
8665 and then Is_OK_Static_Expression (Right_Opnd (Exp))
8667 Val := Expr_Value (Right_Opnd (Exp));
8669 -- Typ is right operand
8671 elsif Is_Type_Ref (Right_Opnd (Exp))
8672 and then Is_OK_Static_Expression (Left_Opnd (Exp))
8674 Val := Expr_Value (Left_Opnd (Exp));
8676 -- Invert sense of comparison
8679 when N_Op_Gt => Op := N_Op_Lt;
8680 when N_Op_Lt => Op := N_Op_Gt;
8681 when N_Op_Ge => Op := N_Op_Le;
8682 when N_Op_Le => Op := N_Op_Ge;
8683 when others => null;
8686 -- Other cases are non-static
8692 -- Construct range according to comparison operation
8696 return RList'(1 => REnt'(Val, Val));
8699 return RList'(1 => REnt'(Val, BHi));
8702 return RList'(1 => REnt'(Val + 1, BHi));
8705 return RList'(1 => REnt'(BLo, Val));
8708 return RList'(1 => REnt'(BLo, Val - 1));
8711 return RList'(REnt'(BLo, Val - 1), REnt'(Val + 1, BHi));
8714 raise Program_Error;
8720 if not Is_Type_Ref (Left_Opnd (Exp)) then
8724 if Present (Right_Opnd (Exp)) then
8725 return Membership_Entry (Right_Opnd (Exp));
8727 return Membership_Entries (First (Alternatives (Exp)));
8730 -- Negative membership (NOT IN)
8733 if not Is_Type_Ref (Left_Opnd (Exp)) then
8737 if Present (Right_Opnd (Exp)) then
8738 return not Membership_Entry (Right_Opnd (Exp));
8740 return not Membership_Entries (First (Alternatives (Exp)));
8743 -- Function call, may be call to static predicate
8745 when N_Function_Call =>
8746 if Is_Entity_Name (Name (Exp)) then
8748 Ent : constant Entity_Id := Entity (Name (Exp));
8750 if Is_Predicate_Function (Ent)
8752 Is_Predicate_Function_M (Ent)
8754 return Stat_Pred (Etype (First_Formal (Ent)));
8759 -- Other function call cases are non-static
8763 -- Qualified expression, dig out the expression
8765 when N_Qualified_Expression =>
8766 return Get_RList (Expression (Exp));
8768 when N_Case_Expression =>
8775 if not Is_Entity_Name (Expression (Expr))
8776 or else Etype (Expression (Expr)) /= Typ
8779 ("expression must denaote subtype", Expression (Expr));
8783 -- Collect discrete choices in all True alternatives
8785 Choices := New_List;
8786 Alt := First (Alternatives (Exp));
8787 while Present (Alt) loop
8788 Dep := Expression (Alt);
8790 if not Is_OK_Static_Expression (Dep) then
8793 elsif Is_True (Expr_Value (Dep)) then
8794 Append_List_To (Choices,
8795 New_Copy_List (Discrete_Choices (Alt)));
8801 return Membership_Entries (First (Choices));
8804 -- Expression with actions: if no actions, dig out expression
8806 when N_Expression_With_Actions =>
8807 if Is_Empty_List (Actions (Exp)) then
8808 return Get_RList (Expression (Exp));
8816 return (Get_RList (Left_Opnd (Exp))
8817 and not Get_RList (Right_Opnd (Exp)))
8818 or (Get_RList (Right_Opnd (Exp))
8819 and not Get_RList (Left_Opnd (Exp)));
8821 -- Any other node type is non-static
8832 function Hi_Val (N : Node_Id) return Uint is
8834 if Is_OK_Static_Expression (N) then
8835 return Expr_Value (N);
8837 pragma Assert (Nkind (N) = N_Range);
8838 return Expr_Value (High_Bound (N));
8846 function Is_False (R : RList) return Boolean is
8848 return R'Length = 0;
8855 function Is_True (R : RList) return Boolean is
8858 and then R (R'First).Lo = BLo
8859 and then R (R'First).Hi = BHi;
8866 function Is_Type_Ref (N : Node_Id) return Boolean is
8868 return Nkind (N) = N_Identifier
8869 and then Chars (N) = Nam
8870 and then Paren_Count (N) = 0;
8877 function Lo_Val (N : Node_Id) return Uint is
8879 if Is_OK_Static_Expression (N) then
8880 return Expr_Value (N);
8882 pragma Assert (Nkind (N) = N_Range);
8883 return Expr_Value (Low_Bound (N));
8887 ------------------------
8888 -- Membership_Entries --
8889 ------------------------
8891 function Membership_Entries (N : Node_Id) return RList is
8893 if No (Next (N)) then
8894 return Membership_Entry (N);
8896 return Membership_Entry (N) or Membership_Entries (Next (N));
8898 end Membership_Entries;
8900 ----------------------
8901 -- Membership_Entry --
8902 ----------------------
8904 function Membership_Entry (N : Node_Id) return RList is
8912 if Nkind (N) = N_Range then
8913 if not Is_OK_Static_Expression (Low_Bound (N))
8915 not Is_OK_Static_Expression (High_Bound (N))
8919 SLo := Expr_Value (Low_Bound (N));
8920 SHi := Expr_Value (High_Bound (N));
8921 return RList'(1 => REnt'(SLo, SHi));
8924 -- Static expression case
8926 elsif Is_OK_Static_Expression (N) then
8927 Val := Expr_Value (N);
8928 return RList'(1 => REnt'(Val, Val));
8930 -- Identifier (other than static expression) case
8932 else pragma Assert (Nkind_In (N, N_Expanded_Name, N_Identifier));
8936 if Is_Type (Entity (N)) then
8938 -- If type has predicates, process them
8940 if Has_Predicates (Entity (N)) then
8941 return Stat_Pred (Entity (N));
8943 -- For static subtype without predicates, get range
8945 elsif Is_OK_Static_Subtype (Entity (N)) then
8946 SLo := Expr_Value (Type_Low_Bound (Entity (N)));
8947 SHi := Expr_Value (Type_High_Bound (Entity (N)));
8948 return RList'(1 => REnt'(SLo, SHi));
8950 -- Any other type makes us non-static
8956 -- Any other kind of identifier in predicate (e.g. a non-static
8957 -- expression value) means this is not a static predicate.
8963 end Membership_Entry;
8969 function Stat_Pred (Typ : Entity_Id) return RList is
8971 -- Not static if type does not have static predicates
8973 if not Has_Static_Predicate (Typ) then
8977 -- Otherwise we convert the predicate list to a range list
8980 Spred : constant List_Id := Static_Discrete_Predicate (Typ);
8981 Result : RList (1 .. List_Length (Spred));
8985 P := First (Static_Discrete_Predicate (Typ));
8986 for J in Result'Range loop
8987 Result (J) := REnt'(Lo_Val (P), Hi_Val (P));
8995 -- Start of processing for Build_Discrete_Static_Predicate
8998 -- Establish bounds for the predicate
9000 if Compile_Time_Known_Value (Type_Low_Bound (Typ)) then
9001 TLo := Expr_Value (Type_Low_Bound (Typ));
9006 if Compile_Time_Known_Value (Type_High_Bound (Typ)) then
9007 THi := Expr_Value (Type_High_Bound (Typ));
9012 -- Analyze the expression to see if it is a static predicate
9015 Ranges : constant RList := Get_RList (Expr);
9016 -- Range list from expression if it is static
9021 -- Convert range list into a form for the static predicate. In the
9022 -- Ranges array, we just have raw ranges, these must be converted
9023 -- to properly typed and analyzed static expressions or range nodes.
9025 -- Note: here we limit ranges to the ranges of the subtype, so that
9026 -- a predicate is always false for values outside the subtype. That
9027 -- seems fine, such values are invalid anyway, and considering them
9028 -- to fail the predicate seems allowed and friendly, and furthermore
9029 -- simplifies processing for case statements and loops.
9033 for J in Ranges'Range loop
9035 Lo : Uint := Ranges (J).Lo;
9036 Hi : Uint := Ranges (J).Hi;
9039 -- Ignore completely out of range entry
9041 if Hi < TLo or else Lo > THi then
9044 -- Otherwise process entry
9047 -- Adjust out of range value to subtype range
9057 -- Convert range into required form
9059 Append_To (Plist, Build_Range (Lo, Hi));
9064 -- Processing was successful and all entries were static, so now we
9065 -- can store the result as the predicate list.
9067 Set_Static_Discrete_Predicate (Typ, Plist);
9069 -- Within a generic the predicate functions themselves need not
9072 if Inside_A_Generic then
9076 -- The processing for static predicates put the expression into
9077 -- canonical form as a series of ranges. It also eliminated
9078 -- duplicates and collapsed and combined ranges. We might as well
9079 -- replace the alternatives list of the right operand of the
9080 -- membership test with the static predicate list, which will
9081 -- usually be more efficient.
9084 New_Alts : constant List_Id := New_List;
9089 Old_Node := First (Plist);
9090 while Present (Old_Node) loop
9091 New_Node := New_Copy (Old_Node);
9093 if Nkind (New_Node) = N_Range then
9094 Set_Low_Bound (New_Node, New_Copy (Low_Bound (Old_Node)));
9095 Set_High_Bound (New_Node, New_Copy (High_Bound (Old_Node)));
9098 Append_To (New_Alts, New_Node);
9102 -- If empty list, replace by False
9104 if Is_Empty_List (New_Alts) then
9105 Rewrite (Expr, New_Occurrence_Of (Standard_False, Loc));
9107 -- Else replace by set membership test
9112 Left_Opnd => Make_Identifier (Loc, Nam),
9113 Right_Opnd => Empty,
9114 Alternatives => New_Alts));
9116 -- Resolve new expression in function context
9118 Install_Formals (Predicate_Function (Typ));
9119 Push_Scope (Predicate_Function (Typ));
9120 Analyze_And_Resolve (Expr, Standard_Boolean);
9126 -- If non-static, return doing nothing
9131 end Build_Discrete_Static_Predicate;
9133 --------------------------------
9134 -- Build_Export_Import_Pragma --
9135 --------------------------------
9137 function Build_Export_Import_Pragma
9139 Id : Entity_Id) return Node_Id
9141 Asp_Id : constant Aspect_Id := Get_Aspect_Id (Asp);
9142 Expr : constant Node_Id := Expression (Asp);
9143 Loc : constant Source_Ptr := Sloc (Asp);
9154 Create_Pragma : Boolean := False;
9155 -- This flag is set when the aspect form is such that it warrants the
9156 -- creation of a corresponding pragma.
9159 if Present (Expr) then
9160 if Error_Posted (Expr) then
9163 elsif Is_True (Expr_Value (Expr)) then
9164 Create_Pragma := True;
9167 -- Otherwise the aspect defaults to True
9170 Create_Pragma := True;
9173 -- Nothing to do when the expression is False or is erroneous
9175 if not Create_Pragma then
9179 -- Obtain all interfacing aspects that apply to the related entity
9181 Get_Interfacing_Aspects
9185 Expo_Asp => Dummy_1,
9191 -- Handle the convention argument
9193 if Present (Conv) then
9194 Conv_Arg := New_Copy_Tree (Expression (Conv));
9196 -- Assume convention "Ada' when aspect Convention is missing
9199 Conv_Arg := Make_Identifier (Loc, Name_Ada);
9203 Make_Pragma_Argument_Association (Loc,
9204 Chars => Name_Convention,
9205 Expression => Conv_Arg));
9207 -- Handle the entity argument
9210 Make_Pragma_Argument_Association (Loc,
9211 Chars => Name_Entity,
9212 Expression => New_Occurrence_Of (Id, Loc)));
9214 -- Handle the External_Name argument
9216 if Present (EN) then
9218 Make_Pragma_Argument_Association (Loc,
9219 Chars => Name_External_Name,
9220 Expression => New_Copy_Tree (Expression (EN))));
9223 -- Handle the Link_Name argument
9225 if Present (LN) then
9227 Make_Pragma_Argument_Association (Loc,
9228 Chars => Name_Link_Name,
9229 Expression => New_Copy_Tree (Expression (LN))));
9233 -- pragma Export/Import
9234 -- (Convention => <Conv>/Ada,
9236 -- [External_Name => <EN>,]
9237 -- [Link_Name => <LN>]);
9241 Pragma_Identifier =>
9242 Make_Identifier (Loc, Chars (Identifier (Asp))),
9243 Pragma_Argument_Associations => Args);
9245 -- Decorate the relevant aspect and the pragma
9247 Set_Aspect_Rep_Item (Asp, Prag);
9249 Set_Corresponding_Aspect (Prag, Asp);
9250 Set_From_Aspect_Specification (Prag);
9251 Set_Parent (Prag, Asp);
9253 if Asp_Id = Aspect_Import and then Is_Subprogram (Id) then
9254 Set_Import_Pragma (Id, Prag);
9258 end Build_Export_Import_Pragma;
9260 -------------------------------
9261 -- Build_Predicate_Functions --
9262 -------------------------------
9264 -- The functions that are constructed here have the form:
9266 -- function typPredicate (Ixxx : typ) return Boolean is
9269 -- typ1Predicate (typ1 (Ixxx))
9270 -- and then typ2Predicate (typ2 (Ixxx))
9272 -- and then exp1 and then exp2 and then ...;
9273 -- end typPredicate;
9275 -- Here exp1, and exp2 are expressions from Predicate pragmas. Note that
9276 -- this is the point at which these expressions get analyzed, providing the
9277 -- required delay, and typ1, typ2, are entities from which predicates are
9278 -- inherited. Note that we do NOT generate Check pragmas, that's because we
9279 -- use this function even if checks are off, e.g. for membership tests.
9281 -- Note that the inherited predicates are evaluated first, as required by
9284 -- Note that Sem_Eval.Real_Or_String_Static_Predicate_Matches depends on
9285 -- the form of this return expression.
9287 -- If the expression has at least one Raise_Expression, then we also build
9288 -- the typPredicateM version of the function, in which any occurrence of a
9289 -- Raise_Expression is converted to "return False".
9291 -- WARNING: This routine manages Ghost regions. Return statements must be
9292 -- replaced by gotos which jump to the end of the routine and restore the
9295 procedure Build_Predicate_Functions (Typ : Entity_Id; N : Node_Id) is
9296 Loc : constant Source_Ptr := Sloc (Typ);
9299 -- This is the expression for the result of the function. It is
9300 -- is build by connecting the component predicates with AND THEN.
9302 Expr_M : Node_Id := Empty; -- init to avoid warning
9303 -- This is the corresponding return expression for the Predicate_M
9304 -- function. It differs in that raise expressions are marked for
9305 -- special expansion (see Process_REs).
9307 Object_Name : Name_Id;
9308 -- Name for argument of Predicate procedure. Note that we use the same
9309 -- name for both predicate functions. That way the reference within the
9310 -- predicate expression is the same in both functions.
9312 Object_Entity : Entity_Id;
9313 -- Entity for argument of Predicate procedure
9315 Object_Entity_M : Entity_Id;
9316 -- Entity for argument of separate Predicate procedure when exceptions
9317 -- are present in expression.
9320 -- The function declaration
9325 Raise_Expression_Present : Boolean := False;
9326 -- Set True if Expr has at least one Raise_Expression
9328 procedure Add_Condition (Cond : Node_Id);
9329 -- Append Cond to Expr using "and then" (or just copy Cond to Expr if
9332 procedure Add_Predicates;
9333 -- Appends expressions for any Predicate pragmas in the rep item chain
9334 -- Typ to Expr. Note that we look only at items for this exact entity.
9335 -- Inheritance of predicates for the parent type is done by calling the
9336 -- Predicate_Function of the parent type, using Add_Call above.
9338 procedure Add_Call (T : Entity_Id);
9339 -- Includes a call to the predicate function for type T in Expr if T
9340 -- has predicates and Predicate_Function (T) is non-empty.
9342 function Process_RE (N : Node_Id) return Traverse_Result;
9343 -- Used in Process REs, tests if node N is a raise expression, and if
9344 -- so, marks it to be converted to return False.
9346 procedure Process_REs is new Traverse_Proc (Process_RE);
9347 -- Marks any raise expressions in Expr_M to return False
9349 function Test_RE (N : Node_Id) return Traverse_Result;
9350 -- Used in Test_REs, tests one node for being a raise expression, and if
9351 -- so sets Raise_Expression_Present True.
9353 procedure Test_REs is new Traverse_Proc (Test_RE);
9354 -- Tests to see if Expr contains any raise expressions
9360 procedure Add_Call (T : Entity_Id) is
9364 if Present (T) and then Present (Predicate_Function (T)) then
9365 Set_Has_Predicates (Typ);
9367 -- Build the call to the predicate function of T. The type may be
9368 -- derived, so use an unchecked conversion for the actual.
9374 Unchecked_Convert_To (T,
9375 Make_Identifier (Loc, Object_Name)));
9377 -- "and"-in the call to evolving expression
9379 Add_Condition (Exp);
9381 -- Output info message on inheritance if required. Note we do not
9382 -- give this information for generic actual types, since it is
9383 -- unwelcome noise in that case in instantiations. We also
9384 -- generally suppress the message in instantiations, and also
9385 -- if it involves internal names.
9387 if Opt.List_Inherited_Aspects
9388 and then not Is_Generic_Actual_Type (Typ)
9389 and then Instantiation_Depth (Sloc (Typ)) = 0
9390 and then not Is_Internal_Name (Chars (T))
9391 and then not Is_Internal_Name (Chars (Typ))
9393 Error_Msg_Sloc := Sloc (Predicate_Function (T));
9394 Error_Msg_Node_2 := T;
9395 Error_Msg_N ("info: & inherits predicate from & #?L?", Typ);
9404 procedure Add_Condition (Cond : Node_Id) is
9406 -- This is the first predicate expression
9411 -- Otherwise concatenate to the existing predicate expressions by
9412 -- using "and then".
9417 Left_Opnd => Relocate_Node (Expr),
9418 Right_Opnd => Cond);
9422 --------------------
9423 -- Add_Predicates --
9424 --------------------
9426 procedure Add_Predicates is
9427 procedure Add_Predicate (Prag : Node_Id);
9428 -- Concatenate the expression of predicate pragma Prag to Expr by
9429 -- using a short circuit "and then" operator.
9435 procedure Add_Predicate (Prag : Node_Id) is
9436 procedure Replace_Type_Reference (N : Node_Id);
9437 -- Replace a single occurrence N of the subtype name with a
9438 -- reference to the formal of the predicate function. N can be an
9439 -- identifier referencing the subtype, or a selected component,
9440 -- representing an appropriately qualified occurrence of the
9443 procedure Replace_Type_References is
9444 new Replace_Type_References_Generic (Replace_Type_Reference);
9445 -- Traverse an expression changing every occurrence of an
9446 -- identifier whose name matches the name of the subtype with a
9447 -- reference to the formal parameter of the predicate function.
9449 ----------------------------
9450 -- Replace_Type_Reference --
9451 ----------------------------
9453 procedure Replace_Type_Reference (N : Node_Id) is
9455 Rewrite (N, Make_Identifier (Sloc (N), Object_Name));
9456 -- Use the Sloc of the usage name, not the defining name
9459 Set_Entity (N, Object_Entity);
9460 end Replace_Type_Reference;
9464 Asp : constant Node_Id := Corresponding_Aspect (Prag);
9468 -- Start of processing for Add_Predicate
9471 -- Mark corresponding SCO as enabled
9473 Set_SCO_Pragma_Enabled (Sloc (Prag));
9475 -- Extract the arguments of the pragma. The expression itself
9476 -- is copied for use in the predicate function, to preserve the
9477 -- original version for ASIS use.
9478 -- Is this still needed???
9480 Arg1 := First (Pragma_Argument_Associations (Prag));
9481 Arg2 := Next (Arg1);
9483 Arg1 := Get_Pragma_Arg (Arg1);
9484 Arg2 := New_Copy_Tree (Get_Pragma_Arg (Arg2));
9486 -- When the predicate pragma applies to the current type or its
9487 -- full view, replace all occurrences of the subtype name with
9488 -- references to the formal parameter of the predicate function.
9490 if Entity (Arg1) = Typ
9491 or else Full_View (Entity (Arg1)) = Typ
9493 Replace_Type_References (Arg2, Typ);
9495 -- If the predicate pragma comes from an aspect, replace the
9496 -- saved expression because we need the subtype references
9497 -- replaced for the calls to Preanalyze_Spec_Expression in
9498 -- Check_Aspect_At_xxx routines.
9500 if Present (Asp) then
9501 Set_Entity (Identifier (Asp), New_Copy_Tree (Arg2));
9504 -- "and"-in the Arg2 condition to evolving expression
9506 Add_Condition (Relocate_Node (Arg2));
9514 -- Start of processing for Add_Predicates
9517 Ritem := First_Rep_Item (Typ);
9519 -- If the type is private, check whether full view has inherited
9522 if Is_Private_Type (Typ) and then No (Ritem) then
9523 Ritem := First_Rep_Item (Full_View (Typ));
9526 while Present (Ritem) loop
9527 if Nkind (Ritem) = N_Pragma
9528 and then Pragma_Name (Ritem) = Name_Predicate
9530 Add_Predicate (Ritem);
9532 -- If the type is declared in an inner package it may be frozen
9533 -- outside of the package, and the generated pragma has not been
9534 -- analyzed yet, so capture the expression for the predicate
9535 -- function at this point.
9537 elsif Nkind (Ritem) = N_Aspect_Specification
9538 and then Present (Aspect_Rep_Item (Ritem))
9539 and then Scope (Typ) /= Current_Scope
9542 Prag : constant Node_Id := Aspect_Rep_Item (Ritem);
9545 if Nkind (Prag) = N_Pragma
9546 and then Pragma_Name (Prag) = Name_Predicate
9548 Add_Predicate (Prag);
9553 Next_Rep_Item (Ritem);
9561 function Process_RE (N : Node_Id) return Traverse_Result is
9563 if Nkind (N) = N_Raise_Expression then
9564 Set_Convert_To_Return_False (N);
9575 function Test_RE (N : Node_Id) return Traverse_Result is
9577 if Nkind (N) = N_Raise_Expression then
9578 Raise_Expression_Present := True;
9587 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
9588 Saved_IGR : constant Node_Id := Ignored_Ghost_Region;
9589 -- Save the Ghost-related attributes to restore on exit
9591 -- Start of processing for Build_Predicate_Functions
9594 -- Return if already built or if type does not have predicates
9596 SId := Predicate_Function (Typ);
9597 if not Has_Predicates (Typ)
9598 or else (Present (SId) and then Has_Completion (SId))
9602 -- Do not generate predicate bodies within a generic unit. The
9603 -- expressions have been analyzed already, and the bodies play
9604 -- no role if not within an executable unit. However, if a statc
9605 -- predicate is present it must be processed for legality checks
9606 -- such as case coverage in an expression.
9608 elsif Inside_A_Generic
9609 and then not Has_Static_Predicate_Aspect (Typ)
9614 -- The related type may be subject to pragma Ghost. Set the mode now to
9615 -- ensure that the predicate functions are properly marked as Ghost.
9617 Set_Ghost_Mode (Typ);
9619 -- Prepare to construct predicate expression
9623 if Present (SId) then
9624 FDecl := Unit_Declaration_Node (SId);
9627 FDecl := Build_Predicate_Function_Declaration (Typ);
9628 SId := Defining_Entity (FDecl);
9631 -- Recover name of formal parameter of function that replaces references
9632 -- to the type in predicate expressions.
9636 (First (Parameter_Specifications (Specification (FDecl))));
9638 Object_Name := Chars (Object_Entity);
9639 Object_Entity_M := Make_Defining_Identifier (Loc, Chars => Object_Name);
9641 -- Add predicates for ancestor if present. These must come before the
9642 -- ones for the current type, as required by AI12-0071-1.
9644 -- Looks like predicates aren't added for case of inheriting from
9645 -- multiple progenitors???
9650 Atyp := Nearest_Ancestor (Typ);
9652 -- The type may be private but the full view may inherit predicates
9654 if No (Atyp) and then Is_Private_Type (Typ) then
9655 Atyp := Nearest_Ancestor (Full_View (Typ));
9658 if Present (Atyp) then
9663 -- Add Predicates for the current type
9667 -- Case where predicates are present
9669 if Present (Expr) then
9671 -- Test for raise expression present
9675 -- If raise expression is present, capture a copy of Expr for use
9676 -- in building the predicateM function version later on. For this
9677 -- copy we replace references to Object_Entity by Object_Entity_M.
9679 if Raise_Expression_Present then
9681 function Reset_Loop_Variable
9682 (N : Node_Id) return Traverse_Result;
9684 procedure Reset_Loop_Variables is
9685 new Traverse_Proc (Reset_Loop_Variable);
9687 ------------------------
9688 -- Reset_Loop_Variable --
9689 ------------------------
9691 function Reset_Loop_Variable
9692 (N : Node_Id) return Traverse_Result
9695 if Nkind (N) = N_Iterator_Specification then
9696 Set_Defining_Identifier (N,
9697 Make_Defining_Identifier
9698 (Sloc (N), Chars (Defining_Identifier (N))));
9702 end Reset_Loop_Variable;
9706 Map : constant Elist_Id := New_Elmt_List;
9709 Append_Elmt (Object_Entity, Map);
9710 Append_Elmt (Object_Entity_M, Map);
9711 Expr_M := New_Copy_Tree (Expr, Map => Map);
9713 -- The unanalyzed expression will be copied and appear in
9714 -- both functions. Normally expressions do not declare new
9715 -- entities, but quantified expressions do, so we need to
9716 -- create new entities for their bound variables, to prevent
9717 -- multiple definitions in gigi.
9719 Reset_Loop_Variables (Expr_M);
9723 -- Build the main predicate function
9726 SIdB : constant Entity_Id :=
9727 Make_Defining_Identifier (Loc,
9728 Chars => New_External_Name (Chars (Typ), "Predicate"));
9729 -- The entity for the function body
9735 Set_Ekind (SIdB, E_Function);
9736 Set_Is_Predicate_Function (SIdB);
9738 -- Build function body
9741 Make_Function_Specification (Loc,
9742 Defining_Unit_Name => SIdB,
9743 Parameter_Specifications => New_List (
9744 Make_Parameter_Specification (Loc,
9745 Defining_Identifier =>
9746 Make_Defining_Identifier (Loc, Object_Name),
9748 New_Occurrence_Of (Typ, Loc))),
9749 Result_Definition =>
9750 New_Occurrence_Of (Standard_Boolean, Loc));
9753 Make_Subprogram_Body (Loc,
9754 Specification => Spec,
9755 Declarations => Empty_List,
9756 Handled_Statement_Sequence =>
9757 Make_Handled_Sequence_Of_Statements (Loc,
9758 Statements => New_List (
9759 Make_Simple_Return_Statement (Loc,
9760 Expression => Expr))));
9762 -- The declaration has been analyzed when created, and placed
9763 -- after type declaration. Insert body itself after freeze node,
9764 -- unless subprogram declaration is already there, in which case
9765 -- body better be placed afterwards.
9767 if FDecl = Next (N) then
9768 Insert_After_And_Analyze (FDecl, FBody);
9770 Insert_After_And_Analyze (N, FBody);
9773 -- The defining identifier of a quantified expression carries the
9774 -- scope in which the type appears, but when unnesting we need
9775 -- to indicate that its proper scope is the constructed predicate
9776 -- function. The quantified expressions have been converted into
9777 -- loops during analysis and expansion.
9780 function Reset_Quantified_Variable_Scope
9781 (N : Node_Id) return Traverse_Result;
9783 procedure Reset_Quantified_Variables_Scope is
9784 new Traverse_Proc (Reset_Quantified_Variable_Scope);
9786 -------------------------------------
9787 -- Reset_Quantified_Variable_Scope --
9788 -------------------------------------
9790 function Reset_Quantified_Variable_Scope
9791 (N : Node_Id) return Traverse_Result
9794 if Nkind_In (N, N_Iterator_Specification,
9795 N_Loop_Parameter_Specification)
9797 Set_Scope (Defining_Identifier (N),
9798 Predicate_Function (Typ));
9802 end Reset_Quantified_Variable_Scope;
9805 if Unnest_Subprogram_Mode then
9806 Reset_Quantified_Variables_Scope (Expr);
9810 -- within a generic unit, prevent a double analysis of the body
9811 -- which will not be marked analyzed yet. This will happen when
9812 -- the freeze node is created during the preanalysis of an
9813 -- expression function.
9815 if Inside_A_Generic then
9816 Set_Analyzed (FBody);
9819 -- Static predicate functions are always side-effect free, and
9820 -- in most cases dynamic predicate functions are as well. Mark
9821 -- them as such whenever possible, so redundant predicate checks
9822 -- can be optimized. If there is a variable reference within the
9823 -- expression, the function is not pure.
9825 if Expander_Active then
9827 Side_Effect_Free (Expr, Variable_Ref => True));
9828 Set_Is_Inlined (SId);
9832 -- Test for raise expressions present and if so build M version
9834 if Raise_Expression_Present then
9836 SId : constant Entity_Id :=
9837 Make_Defining_Identifier (Loc,
9838 Chars => New_External_Name (Chars (Typ), "PredicateM"));
9839 -- The entity for the function spec
9841 SIdB : constant Entity_Id :=
9842 Make_Defining_Identifier (Loc,
9843 Chars => New_External_Name (Chars (Typ), "PredicateM"));
9844 -- The entity for the function body
9851 CRec_Typ : Entity_Id;
9852 -- The corresponding record type of Full_Typ
9854 Full_Typ : Entity_Id;
9855 -- The full view of Typ
9857 Priv_Typ : Entity_Id;
9858 -- The partial view of Typ
9860 UFull_Typ : Entity_Id;
9861 -- The underlying full view of Full_Typ
9864 -- Mark any raise expressions for special expansion
9866 Process_REs (Expr_M);
9868 -- Build function declaration
9870 Set_Ekind (SId, E_Function);
9871 Set_Is_Predicate_Function_M (SId);
9872 Set_Predicate_Function_M (Typ, SId);
9874 -- Obtain all views of the input type
9876 Get_Views (Typ, Priv_Typ, Full_Typ, UFull_Typ, CRec_Typ);
9878 -- Associate the predicate function with all views
9880 Propagate_Predicate_Attributes (Priv_Typ, From_Typ => Typ);
9881 Propagate_Predicate_Attributes (Full_Typ, From_Typ => Typ);
9882 Propagate_Predicate_Attributes (UFull_Typ, From_Typ => Typ);
9883 Propagate_Predicate_Attributes (CRec_Typ, From_Typ => Typ);
9886 Make_Function_Specification (Loc,
9887 Defining_Unit_Name => SId,
9888 Parameter_Specifications => New_List (
9889 Make_Parameter_Specification (Loc,
9890 Defining_Identifier => Object_Entity_M,
9891 Parameter_Type => New_Occurrence_Of (Typ, Loc))),
9892 Result_Definition =>
9893 New_Occurrence_Of (Standard_Boolean, Loc));
9896 Make_Subprogram_Declaration (Loc,
9897 Specification => Spec);
9899 -- Build function body
9902 Make_Function_Specification (Loc,
9903 Defining_Unit_Name => SIdB,
9904 Parameter_Specifications => New_List (
9905 Make_Parameter_Specification (Loc,
9906 Defining_Identifier =>
9907 Make_Defining_Identifier (Loc, Object_Name),
9909 New_Occurrence_Of (Typ, Loc))),
9910 Result_Definition =>
9911 New_Occurrence_Of (Standard_Boolean, Loc));
9913 -- Build the body, we declare the boolean expression before
9914 -- doing the return, because we are not really confident of
9915 -- what happens if a return appears within a return.
9918 Make_Defining_Identifier (Loc,
9919 Chars => New_Internal_Name ('B'));
9922 Make_Subprogram_Body (Loc,
9923 Specification => Spec,
9925 Declarations => New_List (
9926 Make_Object_Declaration (Loc,
9927 Defining_Identifier => BTemp,
9928 Constant_Present => True,
9929 Object_Definition =>
9930 New_Occurrence_Of (Standard_Boolean, Loc),
9931 Expression => Expr_M)),
9933 Handled_Statement_Sequence =>
9934 Make_Handled_Sequence_Of_Statements (Loc,
9935 Statements => New_List (
9936 Make_Simple_Return_Statement (Loc,
9937 Expression => New_Occurrence_Of (BTemp, Loc)))));
9939 -- Insert declaration before freeze node and body after
9941 Insert_Before_And_Analyze (N, FDecl);
9942 Insert_After_And_Analyze (N, FBody);
9944 -- Should quantified expressions be handled here as well ???
9948 -- See if we have a static predicate. Note that the answer may be
9949 -- yes even if we have an explicit Dynamic_Predicate present.
9956 if not Is_Scalar_Type (Typ) and then not Is_String_Type (Typ) then
9959 PS := Is_Predicate_Static (Expr, Object_Name);
9962 -- Case where we have a predicate-static aspect
9966 -- We don't set Has_Static_Predicate_Aspect, since we can have
9967 -- any of the three cases (Predicate, Dynamic_Predicate, or
9968 -- Static_Predicate) generating a predicate with an expression
9969 -- that is predicate-static. We just indicate that we have a
9970 -- predicate that can be treated as static.
9972 Set_Has_Static_Predicate (Typ);
9974 -- For discrete subtype, build the static predicate list
9976 if Is_Discrete_Type (Typ) then
9977 Build_Discrete_Static_Predicate (Typ, Expr, Object_Name);
9979 -- If we don't get a static predicate list, it means that we
9980 -- have a case where this is not possible, most typically in
9981 -- the case where we inherit a dynamic predicate. We do not
9982 -- consider this an error, we just leave the predicate as
9983 -- dynamic. But if we do succeed in building the list, then
9984 -- we mark the predicate as static.
9986 if No (Static_Discrete_Predicate (Typ)) then
9987 Set_Has_Static_Predicate (Typ, False);
9990 -- For real or string subtype, save predicate expression
9992 elsif Is_Real_Type (Typ) or else Is_String_Type (Typ) then
9993 Set_Static_Real_Or_String_Predicate (Typ, Expr);
9996 -- Case of dynamic predicate (expression is not predicate-static)
9999 -- Again, we don't set Has_Dynamic_Predicate_Aspect, since that
10000 -- is only set if we have an explicit Dynamic_Predicate aspect
10001 -- given. Here we may simply have a Predicate aspect where the
10002 -- expression happens not to be predicate-static.
10004 -- Emit an error when the predicate is categorized as static
10005 -- but its expression is not predicate-static.
10007 -- First a little fiddling to get a nice location for the
10008 -- message. If the expression is of the form (A and then B),
10009 -- where A is an inherited predicate, then use the right
10010 -- operand for the Sloc. This avoids getting confused by a call
10011 -- to an inherited predicate with a less convenient source
10015 while Nkind (EN) = N_And_Then
10016 and then Nkind (Left_Opnd (EN)) = N_Function_Call
10017 and then Is_Predicate_Function
10018 (Entity (Name (Left_Opnd (EN))))
10020 EN := Right_Opnd (EN);
10023 -- Now post appropriate message
10025 if Has_Static_Predicate_Aspect (Typ) then
10026 if Is_Scalar_Type (Typ) or else Is_String_Type (Typ) then
10028 ("expression is not predicate-static (RM 3.2.4(16-22))",
10032 ("static predicate requires scalar or string type", EN);
10039 Restore_Ghost_Region (Saved_GM, Saved_IGR);
10040 end Build_Predicate_Functions;
10042 ------------------------------------------
10043 -- Build_Predicate_Function_Declaration --
10044 ------------------------------------------
10046 -- WARNING: This routine manages Ghost regions. Return statements must be
10047 -- replaced by gotos which jump to the end of the routine and restore the
10050 function Build_Predicate_Function_Declaration
10051 (Typ : Entity_Id) return Node_Id
10053 Loc : constant Source_Ptr := Sloc (Typ);
10055 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
10056 Saved_IGR : constant Node_Id := Ignored_Ghost_Region;
10057 -- Save the Ghost-related attributes to restore on exit
10059 Func_Decl : Node_Id;
10060 Func_Id : Entity_Id;
10063 CRec_Typ : Entity_Id;
10064 -- The corresponding record type of Full_Typ
10066 Full_Typ : Entity_Id;
10067 -- The full view of Typ
10069 Priv_Typ : Entity_Id;
10070 -- The partial view of Typ
10072 UFull_Typ : Entity_Id;
10073 -- The underlying full view of Full_Typ
10076 -- The related type may be subject to pragma Ghost. Set the mode now to
10077 -- ensure that the predicate functions are properly marked as Ghost.
10079 Set_Ghost_Mode (Typ);
10082 Make_Defining_Identifier (Loc,
10083 Chars => New_External_Name (Chars (Typ), "Predicate"));
10085 Set_Ekind (Func_Id, E_Function);
10086 Set_Etype (Func_Id, Standard_Boolean);
10087 Set_Is_Internal (Func_Id);
10088 Set_Is_Predicate_Function (Func_Id);
10089 Set_Predicate_Function (Typ, Func_Id);
10091 -- The predicate function requires debug info when the predicates are
10092 -- subject to Source Coverage Obligations.
10094 if Opt.Generate_SCO then
10095 Set_Debug_Info_Needed (Func_Id);
10098 -- Obtain all views of the input type
10100 Get_Views (Typ, Priv_Typ, Full_Typ, UFull_Typ, CRec_Typ);
10102 -- Associate the predicate function and various flags with all views
10104 Propagate_Predicate_Attributes (Priv_Typ, From_Typ => Typ);
10105 Propagate_Predicate_Attributes (Full_Typ, From_Typ => Typ);
10106 Propagate_Predicate_Attributes (UFull_Typ, From_Typ => Typ);
10107 Propagate_Predicate_Attributes (CRec_Typ, From_Typ => Typ);
10110 Make_Function_Specification (Loc,
10111 Defining_Unit_Name => Func_Id,
10112 Parameter_Specifications => New_List (
10113 Make_Parameter_Specification (Loc,
10114 Defining_Identifier => Make_Temporary (Loc, 'I'),
10115 Parameter_Type => New_Occurrence_Of (Typ, Loc))),
10116 Result_Definition =>
10117 New_Occurrence_Of (Standard_Boolean, Loc));
10119 Func_Decl := Make_Subprogram_Declaration (Loc, Specification => Spec);
10121 Insert_After (Parent (Typ), Func_Decl);
10122 Analyze (Func_Decl);
10124 Restore_Ghost_Region (Saved_GM, Saved_IGR);
10127 end Build_Predicate_Function_Declaration;
10129 -----------------------------------------
10130 -- Check_Aspect_At_End_Of_Declarations --
10131 -----------------------------------------
10133 procedure Check_Aspect_At_End_Of_Declarations (ASN : Node_Id) is
10134 Ent : constant Entity_Id := Entity (ASN);
10135 Ident : constant Node_Id := Identifier (ASN);
10136 A_Id : constant Aspect_Id := Get_Aspect_Id (Chars (Ident));
10138 End_Decl_Expr : constant Node_Id := Entity (Ident);
10139 -- Expression to be analyzed at end of declarations
10141 Freeze_Expr : constant Node_Id := Expression (ASN);
10142 -- Expression from call to Check_Aspect_At_Freeze_Point.
10144 T : constant Entity_Id := Etype (Original_Node (Freeze_Expr));
10145 -- Type required for preanalyze call. We use the original expression to
10146 -- get the proper type, to prevent cascaded errors when the expression
10147 -- is constant-folded.
10150 -- Set False if error
10152 -- On entry to this procedure, Entity (Ident) contains a copy of the
10153 -- original expression from the aspect, saved for this purpose, and
10154 -- but Expression (Ident) is a preanalyzed copy of the expression,
10155 -- preanalyzed just after the freeze point.
10157 procedure Check_Overloaded_Name;
10158 -- For aspects whose expression is simply a name, this routine checks if
10159 -- the name is overloaded or not. If so, it verifies there is an
10160 -- interpretation that matches the entity obtained at the freeze point,
10161 -- otherwise the compiler complains.
10163 ---------------------------
10164 -- Check_Overloaded_Name --
10165 ---------------------------
10167 procedure Check_Overloaded_Name is
10169 if not Is_Overloaded (End_Decl_Expr) then
10170 Err := not Is_Entity_Name (End_Decl_Expr)
10171 or else Entity (End_Decl_Expr) /= Entity (Freeze_Expr);
10177 Index : Interp_Index;
10181 Get_First_Interp (End_Decl_Expr, Index, It);
10182 while Present (It.Typ) loop
10183 if It.Nam = Entity (Freeze_Expr) then
10188 Get_Next_Interp (Index, It);
10192 end Check_Overloaded_Name;
10194 -- Start of processing for Check_Aspect_At_End_Of_Declarations
10197 -- In an instance we do not perform the consistency check between freeze
10198 -- point and end of declarations, because it was done already in the
10199 -- analysis of the generic. Furthermore, the delayed analysis of an
10200 -- aspect of the instance may produce spurious errors when the generic
10201 -- is a child unit that references entities in the parent (which might
10202 -- not be in scope at the freeze point of the instance).
10204 if In_Instance then
10207 -- The enclosing scope may have been rewritten during expansion (.e.g. a
10208 -- task body is rewritten as a procedure) after this conformance check
10209 -- has been performed, so do not perform it again (it may not easily be
10210 -- done if full visibility of local entities is not available).
10212 elsif not Comes_From_Source (Current_Scope) then
10215 -- Case of aspects Dimension, Dimension_System and Synchronization
10217 elsif A_Id = Aspect_Synchronization then
10220 -- Case of stream attributes and Put_Image, just have to compare
10221 -- entities. However, the expression is just a possibly-overloaded
10222 -- name, so we need to verify that one of these interpretations is
10223 -- the one available at at the freeze point.
10225 elsif A_Id = Aspect_Input or else
10226 A_Id = Aspect_Output or else
10227 A_Id = Aspect_Read or else
10228 A_Id = Aspect_Write or else
10229 A_Id = Aspect_Put_Image
10231 Analyze (End_Decl_Expr);
10232 Check_Overloaded_Name;
10234 elsif A_Id = Aspect_Variable_Indexing or else
10235 A_Id = Aspect_Constant_Indexing or else
10236 A_Id = Aspect_Default_Iterator or else
10237 A_Id = Aspect_Iterator_Element or else
10238 A_Id = Aspect_Integer_Literal or else
10239 A_Id = Aspect_Real_Literal or else
10240 A_Id = Aspect_String_Literal
10242 -- Make type unfrozen before analysis, to prevent spurious errors
10243 -- about late attributes.
10245 Set_Is_Frozen (Ent, False);
10246 Analyze (End_Decl_Expr);
10247 Set_Is_Frozen (Ent, True);
10249 -- If the end of declarations comes before any other freeze point,
10250 -- the Freeze_Expr is not analyzed: no check needed.
10252 if Analyzed (Freeze_Expr) and then not In_Instance then
10253 Check_Overloaded_Name;
10261 -- In a generic context freeze nodes are not always generated, so
10262 -- analyze the expression now. If the aspect is for a type, we must
10263 -- also make its potential components accessible.
10265 if not Analyzed (Freeze_Expr) and then Inside_A_Generic then
10266 if A_Id = Aspect_Dynamic_Predicate
10267 or else A_Id = Aspect_Predicate
10270 Preanalyze_Spec_Expression (Freeze_Expr, Standard_Boolean);
10273 elsif A_Id = Aspect_Priority then
10275 Preanalyze_Spec_Expression (Freeze_Expr, Any_Integer);
10279 Preanalyze (Freeze_Expr);
10283 -- Indicate that the expression comes from an aspect specification,
10284 -- which is used in subsequent analysis even if expansion is off.
10286 Set_Parent (End_Decl_Expr, ASN);
10288 -- In a generic context the original aspect expressions have not
10289 -- been preanalyzed, so do it now. There are no conformance checks
10290 -- to perform in this case. As before, we have to make components
10291 -- visible for aspects that may reference them.
10294 if A_Id = Aspect_Dynamic_Predicate
10295 or else A_Id = Aspect_Predicate
10296 or else A_Id = Aspect_Priority
10299 Check_Aspect_At_Freeze_Point (ASN);
10303 Check_Aspect_At_Freeze_Point (ASN);
10307 -- The default values attributes may be defined in the private part,
10308 -- and the analysis of the expression may take place when only the
10309 -- partial view is visible. The expression must be scalar, so use
10310 -- the full view to resolve.
10312 elsif (A_Id = Aspect_Default_Value
10314 A_Id = Aspect_Default_Component_Value)
10315 and then Is_Private_Type (T)
10317 Preanalyze_Spec_Expression (End_Decl_Expr, Full_View (T));
10319 -- The following aspect expressions may contain references to
10320 -- components and discriminants of the type.
10322 elsif A_Id = Aspect_Dynamic_Predicate
10323 or else A_Id = Aspect_Predicate
10324 or else A_Id = Aspect_Priority
10325 or else A_Id = Aspect_CPU
10328 Preanalyze_Spec_Expression (End_Decl_Expr, T);
10332 Preanalyze_Spec_Expression (End_Decl_Expr, T);
10336 not Fully_Conformant_Expressions
10337 (End_Decl_Expr, Freeze_Expr, Report => True);
10340 -- Output error message if error. Force error on aspect specification
10341 -- even if there is an error on the expression itself.
10345 ("!visibility of aspect for& changes after freeze point",
10348 ("info: & is frozen here, (RM 13.1.1 (13/3))??",
10349 Freeze_Node (Ent), Ent);
10351 end Check_Aspect_At_End_Of_Declarations;
10353 ----------------------------------
10354 -- Check_Aspect_At_Freeze_Point --
10355 ----------------------------------
10357 procedure Check_Aspect_At_Freeze_Point (ASN : Node_Id) is
10358 Ident : constant Node_Id := Identifier (ASN);
10359 -- Identifier (use Entity field to save expression)
10361 Expr : constant Node_Id := Expression (ASN);
10362 -- For cases where using Entity (Identifier) doesn't work
10364 A_Id : constant Aspect_Id := Get_Aspect_Id (Chars (Ident));
10366 T : Entity_Id := Empty;
10367 -- Type required for preanalyze call
10370 -- On entry to this procedure, Entity (Ident) contains a copy of the
10371 -- original expression from the aspect, saved for this purpose.
10373 -- On exit from this procedure Entity (Ident) is unchanged, still
10374 -- containing that copy, but Expression (Ident) is a preanalyzed copy
10375 -- of the expression, preanalyzed just after the freeze point.
10377 -- Make a copy of the expression to be preanalyzed
10379 Set_Expression (ASN, New_Copy_Tree (Entity (Ident)));
10381 -- Find type for preanalyze call
10385 -- No_Aspect should be impossible
10388 raise Program_Error;
10390 -- Aspects taking an optional boolean argument
10392 when Boolean_Aspects
10393 | Library_Unit_Aspects
10395 T := Standard_Boolean;
10397 -- Aspects corresponding to attribute definition clauses
10399 when Aspect_Address =>
10400 T := RTE (RE_Address);
10402 when Aspect_Attach_Handler =>
10403 T := RTE (RE_Interrupt_ID);
10405 when Aspect_Bit_Order
10406 | Aspect_Scalar_Storage_Order
10408 T := RTE (RE_Bit_Order);
10410 when Aspect_Convention =>
10414 T := RTE (RE_CPU_Range);
10416 -- Default_Component_Value is resolved with the component type
10418 when Aspect_Default_Component_Value =>
10419 T := Component_Type (Entity (ASN));
10421 when Aspect_Default_Storage_Pool =>
10422 T := Class_Wide_Type (RTE (RE_Root_Storage_Pool));
10424 -- Default_Value is resolved with the type entity in question
10426 when Aspect_Default_Value =>
10429 when Aspect_Dispatching_Domain =>
10430 T := RTE (RE_Dispatching_Domain);
10432 when Aspect_External_Tag =>
10433 T := Standard_String;
10435 when Aspect_External_Name =>
10436 T := Standard_String;
10438 when Aspect_Link_Name =>
10439 T := Standard_String;
10441 when Aspect_Interrupt_Priority
10444 T := Standard_Integer;
10446 when Aspect_Relative_Deadline =>
10447 T := RTE (RE_Time_Span);
10449 when Aspect_Secondary_Stack_Size =>
10450 T := Standard_Integer;
10452 when Aspect_Small =>
10454 -- Note that the expression can be of any real type (not just a
10455 -- real universal literal) as long as it is a static constant.
10459 -- For a simple storage pool, we have to retrieve the type of the
10460 -- pool object associated with the aspect's corresponding attribute
10461 -- definition clause.
10463 when Aspect_Simple_Storage_Pool =>
10464 T := Etype (Expression (Aspect_Rep_Item (ASN)));
10466 when Aspect_Storage_Pool =>
10467 T := Class_Wide_Type (RTE (RE_Root_Storage_Pool));
10469 when Aspect_Alignment
10470 | Aspect_Component_Size
10471 | Aspect_Machine_Radix
10472 | Aspect_Object_Size
10474 | Aspect_Storage_Size
10475 | Aspect_Stream_Size
10476 | Aspect_Value_Size
10480 when Aspect_Linker_Section =>
10481 T := Standard_String;
10483 when Aspect_Synchronization =>
10486 -- Special case, the expression of these aspects is just an entity
10487 -- that does not need any resolution, so just analyze.
10494 | Aspect_Unsuppress
10498 Analyze (Expression (ASN));
10501 -- Same for Iterator aspects, where the expression is a function
10502 -- name. Legality rules are checked separately.
10504 when Aspect_Constant_Indexing
10505 | Aspect_Default_Iterator
10506 | Aspect_Iterator_Element
10507 | Aspect_Variable_Indexing
10509 Analyze (Expression (ASN));
10512 -- Same for Literal aspects, where the expression is a function
10513 -- name. Legality rules are checked separately. Use Expr to avoid
10514 -- losing track of the previous resolution of Expression.
10516 when Aspect_Integer_Literal
10517 | Aspect_Real_Literal
10518 | Aspect_String_Literal
10520 Set_Entity (Expression (ASN), Entity (Expr));
10521 Set_Etype (Expression (ASN), Etype (Expr));
10522 Set_Is_Overloaded (Expression (ASN), False);
10523 Analyze (Expression (ASN));
10526 -- Ditto for Iterable, legality checks in Validate_Iterable_Aspect.
10528 when Aspect_Iterable =>
10532 Cursor : constant Entity_Id := Get_Cursor_Type (ASN, T);
10537 if Cursor = Any_Type then
10541 Assoc := First (Component_Associations (Expression (ASN)));
10542 while Present (Assoc) loop
10543 Expr := Expression (Assoc);
10546 if not Error_Posted (Expr) then
10547 Resolve_Iterable_Operation
10548 (Expr, Cursor, T, Chars (First (Choices (Assoc))));
10557 -- Invariant/Predicate take boolean expressions
10559 when Aspect_Dynamic_Predicate
10562 | Aspect_Static_Predicate
10563 | Aspect_Type_Invariant
10565 T := Standard_Boolean;
10567 when Aspect_Predicate_Failure =>
10568 T := Standard_String;
10570 -- Here is the list of aspects that don't require delay analysis
10572 when Aspect_Abstract_State
10574 | Aspect_Async_Readers
10575 | Aspect_Async_Writers
10576 | Aspect_Constant_After_Elaboration
10577 | Aspect_Contract_Cases
10578 | Aspect_Default_Initial_Condition
10581 | Aspect_Dimension_System
10582 | Aspect_Effective_Reads
10583 | Aspect_Effective_Writes
10584 | Aspect_Extensions_Visible
10587 | Aspect_Implicit_Dereference
10588 | Aspect_Initial_Condition
10589 | Aspect_Initializes
10590 | Aspect_Max_Entry_Queue_Depth
10591 | Aspect_Max_Entry_Queue_Length
10592 | Aspect_Max_Queue_Length
10593 | Aspect_No_Caching
10594 | Aspect_Obsolescent
10597 | Aspect_Postcondition
10599 | Aspect_Precondition
10600 | Aspect_Refined_Depends
10601 | Aspect_Refined_Global
10602 | Aspect_Refined_Post
10603 | Aspect_Refined_State
10604 | Aspect_Relaxed_Initialization
10605 | Aspect_SPARK_Mode
10607 | Aspect_Unimplemented
10608 | Aspect_Volatile_Function
10610 raise Program_Error;
10614 -- Do the preanalyze call
10616 Preanalyze_Spec_Expression (Expression (ASN), T);
10617 end Check_Aspect_At_Freeze_Point;
10619 -----------------------------------
10620 -- Check_Constant_Address_Clause --
10621 -----------------------------------
10623 procedure Check_Constant_Address_Clause
10627 procedure Check_At_Constant_Address (Nod : Node_Id);
10628 -- Checks that the given node N represents a name whose 'Address is
10629 -- constant (in the same sense as OK_Constant_Address_Clause, i.e. the
10630 -- address value is the same at the point of declaration of U_Ent and at
10631 -- the time of elaboration of the address clause.
10633 procedure Check_Expr_Constants (Nod : Node_Id);
10634 -- Checks that Nod meets the requirements for a constant address clause
10635 -- in the sense of the enclosing procedure.
10637 procedure Check_List_Constants (Lst : List_Id);
10638 -- Check that all elements of list Lst meet the requirements for a
10639 -- constant address clause in the sense of the enclosing procedure.
10641 -------------------------------
10642 -- Check_At_Constant_Address --
10643 -------------------------------
10645 procedure Check_At_Constant_Address (Nod : Node_Id) is
10647 if Is_Entity_Name (Nod) then
10648 if Present (Address_Clause (Entity ((Nod)))) then
10650 ("invalid address clause for initialized object &!",
10653 ("address for& cannot depend on another address clause! "
10654 & "(RM 13.1(22))!", Nod, U_Ent);
10656 elsif In_Same_Source_Unit (Entity (Nod), U_Ent)
10657 and then Sloc (U_Ent) < Sloc (Entity (Nod))
10660 ("invalid address clause for initialized object &!",
10662 Error_Msg_Node_2 := U_Ent;
10664 ("\& must be defined before & (RM 13.1(22))!",
10665 Nod, Entity (Nod));
10668 elsif Nkind (Nod) = N_Selected_Component then
10670 T : constant Entity_Id := Etype (Prefix (Nod));
10673 if (Is_Record_Type (T)
10674 and then Has_Discriminants (T))
10676 (Is_Access_Type (T)
10677 and then Is_Record_Type (Designated_Type (T))
10678 and then Has_Discriminants (Designated_Type (T)))
10681 ("invalid address clause for initialized object &!",
10684 ("\address cannot depend on component of discriminated "
10685 & "record (RM 13.1(22))!", Nod);
10687 Check_At_Constant_Address (Prefix (Nod));
10691 elsif Nkind (Nod) = N_Indexed_Component then
10692 Check_At_Constant_Address (Prefix (Nod));
10693 Check_List_Constants (Expressions (Nod));
10696 Check_Expr_Constants (Nod);
10698 end Check_At_Constant_Address;
10700 --------------------------
10701 -- Check_Expr_Constants --
10702 --------------------------
10704 procedure Check_Expr_Constants (Nod : Node_Id) is
10705 Loc_U_Ent : constant Source_Ptr := Sloc (U_Ent);
10706 Ent : Entity_Id := Empty;
10709 if Nkind (Nod) in N_Has_Etype
10710 and then Etype (Nod) = Any_Type
10715 case Nkind (Nod) is
10721 when N_Expanded_Name
10724 Ent := Entity (Nod);
10726 -- We need to look at the original node if it is different
10727 -- from the node, since we may have rewritten things and
10728 -- substituted an identifier representing the rewrite.
10730 if Is_Rewrite_Substitution (Nod) then
10731 Check_Expr_Constants (Original_Node (Nod));
10733 -- If the node is an object declaration without initial
10734 -- value, some code has been expanded, and the expression
10735 -- is not constant, even if the constituents might be
10736 -- acceptable, as in A'Address + offset.
10738 if Ekind (Ent) = E_Variable
10740 Nkind (Declaration_Node (Ent)) = N_Object_Declaration
10742 No (Expression (Declaration_Node (Ent)))
10745 ("invalid address clause for initialized object &!",
10748 -- If entity is constant, it may be the result of expanding
10749 -- a check. We must verify that its declaration appears
10750 -- before the object in question, else we also reject the
10753 elsif Ekind (Ent) = E_Constant
10754 and then In_Same_Source_Unit (Ent, U_Ent)
10755 and then Sloc (Ent) > Loc_U_Ent
10758 ("invalid address clause for initialized object &!",
10765 -- Otherwise look at the identifier and see if it is OK
10767 if Ekind_In (Ent, E_Named_Integer, E_Named_Real)
10768 or else Is_Type (Ent)
10772 elsif Ekind_In (Ent, E_Constant, E_In_Parameter) then
10774 -- This is the case where we must have Ent defined before
10775 -- U_Ent. Clearly if they are in different units this
10776 -- requirement is met since the unit containing Ent is
10777 -- already processed.
10779 if not In_Same_Source_Unit (Ent, U_Ent) then
10782 -- Otherwise location of Ent must be before the location
10783 -- of U_Ent, that's what prior defined means.
10785 elsif Sloc (Ent) < Loc_U_Ent then
10790 ("invalid address clause for initialized object &!",
10792 Error_Msg_Node_2 := U_Ent;
10794 ("\& must be defined before & (RM 13.1(22))!",
10798 elsif Nkind (Original_Node (Nod)) = N_Function_Call then
10799 Check_Expr_Constants (Original_Node (Nod));
10803 ("invalid address clause for initialized object &!",
10806 if Comes_From_Source (Ent) then
10808 ("\reference to variable& not allowed"
10809 & " (RM 13.1(22))!", Nod, Ent);
10812 ("non-static expression not allowed"
10813 & " (RM 13.1(22))!", Nod);
10817 when N_Integer_Literal =>
10819 -- If this is a rewritten unchecked conversion, in a system
10820 -- where Address is an integer type, always use the base type
10821 -- for a literal value. This is user-friendly and prevents
10822 -- order-of-elaboration issues with instances of unchecked
10825 if Nkind (Original_Node (Nod)) = N_Function_Call then
10826 Set_Etype (Nod, Base_Type (Etype (Nod)));
10829 when N_Character_Literal
10836 Check_Expr_Constants (Low_Bound (Nod));
10837 Check_Expr_Constants (High_Bound (Nod));
10839 when N_Explicit_Dereference =>
10840 Check_Expr_Constants (Prefix (Nod));
10842 when N_Indexed_Component =>
10843 Check_Expr_Constants (Prefix (Nod));
10844 Check_List_Constants (Expressions (Nod));
10847 Check_Expr_Constants (Prefix (Nod));
10848 Check_Expr_Constants (Discrete_Range (Nod));
10850 when N_Selected_Component =>
10851 Check_Expr_Constants (Prefix (Nod));
10853 when N_Attribute_Reference =>
10854 if Nam_In (Attribute_Name (Nod), Name_Address,
10856 Name_Unchecked_Access,
10857 Name_Unrestricted_Access)
10859 Check_At_Constant_Address (Prefix (Nod));
10861 -- Normally, System'To_Address will have been transformed into
10862 -- an Unchecked_Conversion, but in -gnatc mode, it will not,
10863 -- and we don't want to give an error, because the whole point
10864 -- of 'To_Address is that it is static.
10866 elsif Attribute_Name (Nod) = Name_To_Address then
10867 pragma Assert (Operating_Mode = Check_Semantics);
10871 Check_Expr_Constants (Prefix (Nod));
10872 Check_List_Constants (Expressions (Nod));
10875 when N_Aggregate =>
10876 Check_List_Constants (Component_Associations (Nod));
10877 Check_List_Constants (Expressions (Nod));
10879 when N_Component_Association =>
10880 Check_Expr_Constants (Expression (Nod));
10882 when N_Extension_Aggregate =>
10883 Check_Expr_Constants (Ancestor_Part (Nod));
10884 Check_List_Constants (Component_Associations (Nod));
10885 Check_List_Constants (Expressions (Nod));
10891 | N_Membership_Test
10894 Check_Expr_Constants (Left_Opnd (Nod));
10895 Check_Expr_Constants (Right_Opnd (Nod));
10898 Check_Expr_Constants (Right_Opnd (Nod));
10901 | N_Qualified_Expression
10902 | N_Type_Conversion
10903 | N_Unchecked_Type_Conversion
10905 Check_Expr_Constants (Expression (Nod));
10907 when N_Function_Call =>
10908 if not Is_Pure (Entity (Name (Nod))) then
10910 ("invalid address clause for initialized object &!",
10914 ("\function & is not pure (RM 13.1(22))!",
10915 Nod, Entity (Name (Nod)));
10918 Check_List_Constants (Parameter_Associations (Nod));
10921 when N_Parameter_Association =>
10922 Check_Expr_Constants (Explicit_Actual_Parameter (Nod));
10926 ("invalid address clause for initialized object &!",
10929 ("\must be constant defined before& (RM 13.1(22))!",
10932 end Check_Expr_Constants;
10934 --------------------------
10935 -- Check_List_Constants --
10936 --------------------------
10938 procedure Check_List_Constants (Lst : List_Id) is
10942 if Present (Lst) then
10943 Nod1 := First (Lst);
10944 while Present (Nod1) loop
10945 Check_Expr_Constants (Nod1);
10949 end Check_List_Constants;
10951 -- Start of processing for Check_Constant_Address_Clause
10954 -- If rep_clauses are to be ignored, no need for legality checks. In
10955 -- particular, no need to pester user about rep clauses that violate the
10956 -- rule on constant addresses, given that these clauses will be removed
10957 -- by Freeze before they reach the back end. Similarly in CodePeer mode,
10958 -- we want to relax these checks.
10960 if not Ignore_Rep_Clauses and not CodePeer_Mode then
10961 Check_Expr_Constants (Expr);
10963 end Check_Constant_Address_Clause;
10965 ---------------------------
10966 -- Check_Pool_Size_Clash --
10967 ---------------------------
10969 procedure Check_Pool_Size_Clash (Ent : Entity_Id; SP, SS : Node_Id) is
10973 -- We need to find out which one came first. Note that in the case of
10974 -- aspects mixed with pragmas there are cases where the processing order
10975 -- is reversed, which is why we do the check here.
10977 if Sloc (SP) < Sloc (SS) then
10978 Error_Msg_Sloc := Sloc (SP);
10980 Error_Msg_NE ("Storage_Pool previously given for&#", Post, Ent);
10983 Error_Msg_Sloc := Sloc (SS);
10985 Error_Msg_NE ("Storage_Size previously given for&#", Post, Ent);
10989 ("\cannot have Storage_Size and Storage_Pool (RM 13.11(3))", Post);
10990 end Check_Pool_Size_Clash;
10992 ----------------------------------------
10993 -- Check_Record_Representation_Clause --
10994 ----------------------------------------
10996 procedure Check_Record_Representation_Clause (N : Node_Id) is
10997 Loc : constant Source_Ptr := Sloc (N);
10998 Ident : constant Node_Id := Identifier (N);
10999 Rectype : Entity_Id;
11002 Fbit : Uint := No_Uint;
11003 Lbit : Uint := No_Uint;
11004 Hbit : Uint := Uint_0;
11008 Max_Bit_So_Far : Uint;
11009 -- Records the maximum bit position so far. If all field positions
11010 -- are monotonically increasing, then we can skip the circuit for
11011 -- checking for overlap, since no overlap is possible.
11013 Tagged_Parent : Entity_Id := Empty;
11014 -- This is set in the case of an extension for which we have either a
11015 -- size clause or Is_Fully_Repped_Tagged_Type True (indicating that all
11016 -- components are positioned by record representation clauses) on the
11017 -- parent type. In this case we check for overlap between components of
11018 -- this tagged type and the parent component. Tagged_Parent will point
11019 -- to this parent type. For all other cases, Tagged_Parent is Empty.
11021 Parent_Last_Bit : Uint := No_Uint; -- init to avoid warning
11022 -- Relevant only if Tagged_Parent is set, Parent_Last_Bit indicates the
11023 -- last bit position for any field in the parent type. We only need to
11024 -- check overlap for fields starting below this point.
11026 Overlap_Check_Required : Boolean;
11027 -- Used to keep track of whether or not an overlap check is required
11029 Overlap_Detected : Boolean := False;
11030 -- Set True if an overlap is detected
11032 Ccount : Natural := 0;
11033 -- Number of component clauses in record rep clause
11035 procedure Check_Component_Overlap (C1_Ent, C2_Ent : Entity_Id);
11036 -- Given two entities for record components or discriminants, checks
11037 -- if they have overlapping component clauses and issues errors if so.
11039 procedure Find_Component;
11040 -- Finds component entity corresponding to current component clause (in
11041 -- CC), and sets Comp to the entity, and Fbit/Lbit to the zero origin
11042 -- start/stop bits for the field. If there is no matching component or
11043 -- if the matching component does not have a component clause, then
11044 -- that's an error and Comp is set to Empty, but no error message is
11045 -- issued, since the message was already given. Comp is also set to
11046 -- Empty if the current "component clause" is in fact a pragma.
11048 procedure Record_Hole_Check
11049 (Rectype : Entity_Id; After_Last : out Uint; Warn : Boolean);
11050 -- Checks for gaps in the given Rectype. Compute After_Last, the bit
11051 -- number after the last component. Warn is True on the initial call,
11052 -- and warnings are given for gaps. For a type extension, this is called
11053 -- recursively to compute After_Last for the parent type; in this case
11054 -- Warn is False and the warnings are suppressed.
11056 procedure Component_Order_Check (Rectype : Entity_Id);
11057 -- Check that the order of component clauses agrees with the order of
11058 -- component declarations, and that the component clauses are given in
11059 -- increasing order of bit offset.
11061 -----------------------------
11062 -- Check_Component_Overlap --
11063 -----------------------------
11065 procedure Check_Component_Overlap (C1_Ent, C2_Ent : Entity_Id) is
11066 CC1 : constant Node_Id := Component_Clause (C1_Ent);
11067 CC2 : constant Node_Id := Component_Clause (C2_Ent);
11070 if Present (CC1) and then Present (CC2) then
11072 -- Exclude odd case where we have two tag components in the same
11073 -- record, both at location zero. This seems a bit strange, but
11074 -- it seems to happen in some circumstances, perhaps on an error.
11076 if Nam_In (Chars (C1_Ent), Name_uTag, Name_uTag) then
11080 -- Here we check if the two fields overlap
11083 S1 : constant Uint := Component_Bit_Offset (C1_Ent);
11084 S2 : constant Uint := Component_Bit_Offset (C2_Ent);
11085 E1 : constant Uint := S1 + Esize (C1_Ent);
11086 E2 : constant Uint := S2 + Esize (C2_Ent);
11089 if E2 <= S1 or else E1 <= S2 then
11092 Error_Msg_Node_2 := Component_Name (CC2);
11093 Error_Msg_Sloc := Sloc (Error_Msg_Node_2);
11094 Error_Msg_Node_1 := Component_Name (CC1);
11096 ("component& overlaps & #", Component_Name (CC1));
11097 Overlap_Detected := True;
11101 end Check_Component_Overlap;
11103 ---------------------------
11104 -- Component_Order_Check --
11105 ---------------------------
11107 procedure Component_Order_Check (Rectype : Entity_Id) is
11108 Comp : Entity_Id := First_Component (Rectype);
11109 Clause : Node_Id := First (Component_Clauses (N));
11110 Prev_Bit_Offset : Uint := Uint_0;
11111 OOO : constant String :=
11112 "?component clause out of order with respect to declaration";
11115 -- Step Comp through components and Clause through component clauses,
11116 -- skipping pragmas. We ignore discriminants and variant parts,
11117 -- because we get most of the benefit from the plain vanilla
11118 -- component cases, without the extra complexity. If we find a Comp
11119 -- and Clause that don't match, give a warning on both and quit. If
11120 -- we find two subsequent clauses out of order by bit layout, give
11121 -- warning and quit. On each iteration, Prev_Bit_Offset is the one
11122 -- from the previous iteration (or 0 to start).
11124 while Present (Comp) and then Present (Clause) loop
11125 if Nkind (Clause) = N_Component_Clause
11126 and then Ekind (Entity (Component_Name (Clause))) = E_Component
11128 if Entity (Component_Name (Clause)) /= Comp then
11129 Error_Msg_N (OOO, Comp);
11130 Error_Msg_N (OOO, Clause);
11134 if not Reverse_Bit_Order (Rectype)
11135 and then not Reverse_Storage_Order (Rectype)
11136 and then Component_Bit_Offset (Comp) < Prev_Bit_Offset
11138 Error_Msg_N ("?memory layout out of order", Clause);
11142 Prev_Bit_Offset := Component_Bit_Offset (Comp);
11143 Next_Component (Comp);
11148 end Component_Order_Check;
11150 --------------------
11151 -- Find_Component --
11152 --------------------
11154 procedure Find_Component is
11156 procedure Search_Component (R : Entity_Id);
11157 -- Search components of R for a match. If found, Comp is set
11159 ----------------------
11160 -- Search_Component --
11161 ----------------------
11163 procedure Search_Component (R : Entity_Id) is
11165 Comp := First_Component_Or_Discriminant (R);
11166 while Present (Comp) loop
11168 -- Ignore error of attribute name for component name (we
11169 -- already gave an error message for this, so no need to
11172 if Nkind (Component_Name (CC)) = N_Attribute_Reference then
11175 exit when Chars (Comp) = Chars (Component_Name (CC));
11178 Next_Component_Or_Discriminant (Comp);
11180 end Search_Component;
11182 -- Start of processing for Find_Component
11185 -- Return with Comp set to Empty if we have a pragma
11187 if Nkind (CC) = N_Pragma then
11192 -- Search current record for matching component
11194 Search_Component (Rectype);
11196 -- If not found, maybe component of base type discriminant that is
11197 -- absent from statically constrained first subtype.
11200 Search_Component (Base_Type (Rectype));
11203 -- If no component, or the component does not reference the component
11204 -- clause in question, then there was some previous error for which
11205 -- we already gave a message, so just return with Comp Empty.
11207 if No (Comp) or else Component_Clause (Comp) /= CC then
11208 Check_Error_Detected;
11211 -- Normal case where we have a component clause
11214 Fbit := Component_Bit_Offset (Comp);
11215 Lbit := Fbit + Esize (Comp) - 1;
11217 end Find_Component;
11219 -----------------------
11220 -- Record_Hole_Check --
11221 -----------------------
11223 procedure Record_Hole_Check
11224 (Rectype : Entity_Id; After_Last : out Uint; Warn : Boolean)
11226 Decl : constant Node_Id := Declaration_Node (Base_Type (Rectype));
11227 -- Full declaration of record type
11229 procedure Check_Component_List
11234 -- Check component list CL for holes. DS is a list of discriminant
11235 -- specifications to be included in the consideration of components.
11236 -- Sbit is the starting bit, which is zero if there are no preceding
11237 -- components (before a variant part, or a parent type, or a tag
11238 -- field). If there are preceding components, Sbit is the bit just
11239 -- after the last such component. Abit is set to the bit just after
11240 -- the last component of DS and CL.
11242 --------------------------
11243 -- Check_Component_List --
11244 --------------------------
11246 procedure Check_Component_List
11255 Compl := Integer (List_Length (Component_Items (CL)));
11257 if DS /= No_List then
11258 Compl := Compl + Integer (List_Length (DS));
11262 Comps : array (Natural range 0 .. Compl) of Entity_Id;
11263 -- Gather components (zero entry is for sort routine)
11265 Ncomps : Natural := 0;
11266 -- Number of entries stored in Comps (starting at Comps (1))
11269 -- One component item or discriminant specification
11272 -- Starting bit for next component
11275 -- Component entity
11280 function Lt (Op1, Op2 : Natural) return Boolean;
11281 -- Compare routine for Sort
11283 procedure Move (From : Natural; To : Natural);
11284 -- Move routine for Sort
11286 package Sorting is new GNAT.Heap_Sort_G (Move, Lt);
11292 function Lt (Op1, Op2 : Natural) return Boolean is
11294 return Component_Bit_Offset (Comps (Op1))
11295 < Component_Bit_Offset (Comps (Op2));
11302 procedure Move (From : Natural; To : Natural) is
11304 Comps (To) := Comps (From);
11308 -- Gather discriminants into Comp
11310 if DS /= No_List then
11311 Citem := First (DS);
11312 while Present (Citem) loop
11313 if Nkind (Citem) = N_Discriminant_Specification then
11315 Ent : constant Entity_Id :=
11316 Defining_Identifier (Citem);
11318 if Ekind (Ent) = E_Discriminant then
11319 Ncomps := Ncomps + 1;
11320 Comps (Ncomps) := Ent;
11329 -- Gather component entities into Comp
11331 Citem := First (Component_Items (CL));
11332 while Present (Citem) loop
11333 if Nkind (Citem) = N_Component_Declaration then
11334 Ncomps := Ncomps + 1;
11335 Comps (Ncomps) := Defining_Identifier (Citem);
11341 -- Now sort the component entities based on the first bit.
11342 -- Note we already know there are no overlapping components.
11344 Sorting.Sort (Ncomps);
11346 -- Loop through entries checking for holes
11349 for J in 1 .. Ncomps loop
11351 pragma Annotate (CodePeer, Modified, CEnt);
11354 CBO : constant Uint := Component_Bit_Offset (CEnt);
11357 -- Skip components with unknown offsets
11359 if CBO /= No_Uint and then CBO >= 0 then
11360 Error_Msg_Uint_1 := CBO - Nbit;
11362 if Warn and then Error_Msg_Uint_1 > 0 then
11364 ("?H?^-bit gap before component&",
11365 Component_Name (Component_Clause (CEnt)),
11369 Nbit := CBO + Esize (CEnt);
11374 -- Set Abit to just after the last nonvariant component
11378 -- Process variant parts recursively if present. Set Abit to
11379 -- the maximum for all variant parts.
11381 if Present (Variant_Part (CL)) then
11383 Var_Start : constant Uint := Nbit;
11385 Variant := First (Variants (Variant_Part (CL)));
11386 while Present (Variant) loop
11387 Check_Component_List
11388 (No_List, Component_List (Variant), Var_Start, Nbit);
11390 if Nbit > Abit then
11397 end Check_Component_List;
11400 -- Starting bit for call to Check_Component_List. Zero for an
11401 -- untagged type. The size of the Tag for a nonderived tagged
11402 -- type. Parent size for a type extension.
11404 Record_Definition : Node_Id;
11405 -- Record_Definition containing Component_List to pass to
11406 -- Check_Component_List.
11408 -- Start of processing for Record_Hole_Check
11411 if Is_Tagged_Type (Rectype) then
11412 Sbit := UI_From_Int (System_Address_Size);
11417 After_Last := Uint_0;
11419 if Nkind (Decl) = N_Full_Type_Declaration then
11420 Record_Definition := Type_Definition (Decl);
11422 -- If we have a record extension, set Sbit to point after the last
11423 -- component of the parent type, by calling Record_Hole_Check
11426 if Nkind (Record_Definition) = N_Derived_Type_Definition then
11427 Record_Definition := Record_Extension_Part (Record_Definition);
11428 Record_Hole_Check (Underlying_Type (Parent_Subtype (Rectype)),
11429 After_Last => Sbit, Warn => False);
11432 if Nkind (Record_Definition) = N_Record_Definition then
11433 Check_Component_List
11434 (Discriminant_Specifications (Decl),
11435 Component_List (Record_Definition),
11439 end Record_Hole_Check;
11441 -- Start of processing for Check_Record_Representation_Clause
11445 Rectype := Entity (Ident);
11447 if Rectype = Any_Type then
11451 Rectype := Underlying_Type (Rectype);
11453 -- See if we have a fully repped derived tagged type
11456 PS : constant Entity_Id := Parent_Subtype (Rectype);
11459 if Present (PS) and then Known_Static_RM_Size (PS) then
11460 Tagged_Parent := PS;
11461 Parent_Last_Bit := RM_Size (PS) - 1;
11463 elsif Present (PS) and then Is_Fully_Repped_Tagged_Type (PS) then
11464 Tagged_Parent := PS;
11466 -- Find maximum bit of any component of the parent type
11468 Parent_Last_Bit := UI_From_Int (System_Address_Size - 1);
11469 Pcomp := First_Entity (Tagged_Parent);
11470 while Present (Pcomp) loop
11471 if Ekind_In (Pcomp, E_Discriminant, E_Component) then
11472 if Component_Bit_Offset (Pcomp) /= No_Uint
11473 and then Known_Static_Esize (Pcomp)
11478 Component_Bit_Offset (Pcomp) + Esize (Pcomp) - 1);
11482 -- Skip anonymous types generated for constrained array
11483 -- or record components.
11488 Next_Entity (Pcomp);
11493 -- All done if no component clauses
11495 CC := First (Component_Clauses (N));
11501 -- If a tag is present, then create a component clause that places it
11502 -- at the start of the record (otherwise gigi may place it after other
11503 -- fields that have rep clauses).
11505 Fent := First_Entity (Rectype);
11507 if Nkind (Fent) = N_Defining_Identifier
11508 and then Chars (Fent) = Name_uTag
11510 Set_Component_Bit_Offset (Fent, Uint_0);
11511 Set_Normalized_Position (Fent, Uint_0);
11512 Set_Normalized_First_Bit (Fent, Uint_0);
11513 Set_Normalized_Position_Max (Fent, Uint_0);
11514 Init_Esize (Fent, System_Address_Size);
11516 Set_Component_Clause (Fent,
11517 Make_Component_Clause (Loc,
11518 Component_Name => Make_Identifier (Loc, Name_uTag),
11520 Position => Make_Integer_Literal (Loc, Uint_0),
11521 First_Bit => Make_Integer_Literal (Loc, Uint_0),
11523 Make_Integer_Literal (Loc,
11524 UI_From_Int (System_Address_Size - 1))));
11526 Ccount := Ccount + 1;
11529 Max_Bit_So_Far := Uint_Minus_1;
11530 Overlap_Check_Required := False;
11532 -- Process the component clauses
11534 while Present (CC) loop
11537 if Present (Comp) then
11538 Ccount := Ccount + 1;
11540 -- We need a full overlap check if record positions non-monotonic
11542 if Fbit <= Max_Bit_So_Far then
11543 Overlap_Check_Required := True;
11546 Max_Bit_So_Far := Lbit;
11548 -- Check bit position out of range of specified size
11550 if Has_Size_Clause (Rectype)
11551 and then RM_Size (Rectype) <= Lbit
11553 Error_Msg_Uint_1 := RM_Size (Rectype);
11554 Error_Msg_Uint_2 := Lbit + 1;
11555 Error_Msg_N ("bit number out of range of specified "
11556 & "size (expected ^, got ^)",
11559 -- Check for overlap with tag or parent component
11562 if Is_Tagged_Type (Rectype)
11563 and then Fbit < System_Address_Size
11566 ("component overlaps tag field of&",
11567 Component_Name (CC), Rectype);
11568 Overlap_Detected := True;
11570 elsif Present (Tagged_Parent)
11571 and then Fbit <= Parent_Last_Bit
11574 ("component overlaps parent field of&",
11575 Component_Name (CC), Rectype);
11576 Overlap_Detected := True;
11579 if Hbit < Lbit then
11588 -- Now that we have processed all the component clauses, check for
11589 -- overlap. We have to leave this till last, since the components can
11590 -- appear in any arbitrary order in the representation clause.
11592 -- We do not need this check if all specified ranges were monotonic,
11593 -- as recorded by Overlap_Check_Required being False at this stage.
11595 -- This first section checks if there are any overlapping entries at
11596 -- all. It does this by sorting all entries and then seeing if there are
11597 -- any overlaps. If there are none, then that is decisive, but if there
11598 -- are overlaps, they may still be OK (they may result from fields in
11599 -- different variants).
11601 if Overlap_Check_Required then
11602 Overlap_Check1 : declare
11604 OC_Fbit : array (0 .. Ccount) of Uint;
11605 -- First-bit values for component clauses, the value is the offset
11606 -- of the first bit of the field from start of record. The zero
11607 -- entry is for use in sorting.
11609 OC_Lbit : array (0 .. Ccount) of Uint;
11610 -- Last-bit values for component clauses, the value is the offset
11611 -- of the last bit of the field from start of record. The zero
11612 -- entry is for use in sorting.
11614 OC_Count : Natural := 0;
11615 -- Count of entries in OC_Fbit and OC_Lbit
11617 function OC_Lt (Op1, Op2 : Natural) return Boolean;
11618 -- Compare routine for Sort
11620 procedure OC_Move (From : Natural; To : Natural);
11621 -- Move routine for Sort
11623 package Sorting is new GNAT.Heap_Sort_G (OC_Move, OC_Lt);
11629 function OC_Lt (Op1, Op2 : Natural) return Boolean is
11631 return OC_Fbit (Op1) < OC_Fbit (Op2);
11638 procedure OC_Move (From : Natural; To : Natural) is
11640 OC_Fbit (To) := OC_Fbit (From);
11641 OC_Lbit (To) := OC_Lbit (From);
11644 -- Start of processing for Overlap_Check
11647 CC := First (Component_Clauses (N));
11648 while Present (CC) loop
11650 -- Exclude component clause already marked in error
11652 if not Error_Posted (CC) then
11655 if Present (Comp) then
11656 OC_Count := OC_Count + 1;
11657 OC_Fbit (OC_Count) := Fbit;
11658 OC_Lbit (OC_Count) := Lbit;
11665 Sorting.Sort (OC_Count);
11667 Overlap_Check_Required := False;
11668 for J in 1 .. OC_Count - 1 loop
11669 if OC_Lbit (J) >= OC_Fbit (J + 1) then
11670 Overlap_Check_Required := True;
11674 end Overlap_Check1;
11677 -- If Overlap_Check_Required is still True, then we have to do the full
11678 -- scale overlap check, since we have at least two fields that do
11679 -- overlap, and we need to know if that is OK since they are in
11680 -- different variant, or whether we have a definite problem.
11682 if Overlap_Check_Required then
11683 Overlap_Check2 : declare
11684 C1_Ent, C2_Ent : Entity_Id;
11685 -- Entities of components being checked for overlap
11688 -- Component_List node whose Component_Items are being checked
11691 -- Component declaration for component being checked
11694 C1_Ent := First_Entity (Base_Type (Rectype));
11696 -- Loop through all components in record. For each component check
11697 -- for overlap with any of the preceding elements on the component
11698 -- list containing the component and also, if the component is in
11699 -- a variant, check against components outside the case structure.
11700 -- This latter test is repeated recursively up the variant tree.
11702 Main_Component_Loop : while Present (C1_Ent) loop
11703 if not Ekind_In (C1_Ent, E_Component, E_Discriminant) then
11704 goto Continue_Main_Component_Loop;
11707 -- Skip overlap check if entity has no declaration node. This
11708 -- happens with discriminants in constrained derived types.
11709 -- Possibly we are missing some checks as a result, but that
11710 -- does not seem terribly serious.
11712 if No (Declaration_Node (C1_Ent)) then
11713 goto Continue_Main_Component_Loop;
11716 Clist := Parent (List_Containing (Declaration_Node (C1_Ent)));
11718 -- Loop through component lists that need checking. Check the
11719 -- current component list and all lists in variants above us.
11721 Component_List_Loop : loop
11723 -- If derived type definition, go to full declaration
11724 -- If at outer level, check discriminants if there are any.
11726 if Nkind (Clist) = N_Derived_Type_Definition then
11727 Clist := Parent (Clist);
11730 -- Outer level of record definition, check discriminants
11731 -- but be careful not to flag a non-girder discriminant
11732 -- and the girder discriminant it renames as overlapping.
11734 if Nkind_In (Clist, N_Full_Type_Declaration,
11735 N_Private_Type_Declaration)
11737 if Has_Discriminants (Defining_Identifier (Clist)) then
11739 First_Discriminant (Defining_Identifier (Clist));
11740 while Present (C2_Ent) loop
11742 Original_Record_Component (C1_Ent) =
11743 Original_Record_Component (C2_Ent);
11744 Check_Component_Overlap (C1_Ent, C2_Ent);
11745 Next_Discriminant (C2_Ent);
11749 -- Record extension case
11751 elsif Nkind (Clist) = N_Derived_Type_Definition then
11754 -- Otherwise check one component list
11757 Citem := First (Component_Items (Clist));
11758 while Present (Citem) loop
11759 if Nkind (Citem) = N_Component_Declaration then
11760 C2_Ent := Defining_Identifier (Citem);
11761 exit when C1_Ent = C2_Ent;
11762 Check_Component_Overlap (C1_Ent, C2_Ent);
11769 -- Check for variants above us (the parent of the Clist can
11770 -- be a variant, in which case its parent is a variant part,
11771 -- and the parent of the variant part is a component list
11772 -- whose components must all be checked against the current
11773 -- component for overlap).
11775 if Nkind (Parent (Clist)) = N_Variant then
11776 Clist := Parent (Parent (Parent (Clist)));
11778 -- Check for possible discriminant part in record, this
11779 -- is treated essentially as another level in the
11780 -- recursion. For this case the parent of the component
11781 -- list is the record definition, and its parent is the
11782 -- full type declaration containing the discriminant
11785 elsif Nkind (Parent (Clist)) = N_Record_Definition then
11786 Clist := Parent (Parent ((Clist)));
11788 -- If neither of these two cases, we are at the top of
11792 exit Component_List_Loop;
11794 end loop Component_List_Loop;
11796 <<Continue_Main_Component_Loop>>
11797 Next_Entity (C1_Ent);
11799 end loop Main_Component_Loop;
11800 end Overlap_Check2;
11803 -- Skip the following warnings if overlap was detected; programmer
11804 -- should fix the errors first.
11806 if not Overlap_Detected then
11807 -- Check for record holes (gaps)
11809 if Warn_On_Record_Holes then
11813 Record_Hole_Check (Rectype, After_Last => Ignore, Warn => True);
11817 -- Check for out-of-order component clauses
11819 if Warn_On_Component_Order then
11820 Component_Order_Check (Rectype);
11824 -- For records that have component clauses for all components, and whose
11825 -- size is less than or equal to 32, and which can be fully packed, we
11826 -- need to know the size in the front end to activate possible packed
11827 -- array processing where the component type is a record.
11829 -- At this stage Hbit + 1 represents the first unused bit from all the
11830 -- component clauses processed, so if the component clauses are
11831 -- complete, then this is the length of the record.
11833 -- For records longer than System.Storage_Unit, and for those where not
11834 -- all components have component clauses, the back end determines the
11835 -- length (it may for example be appropriate to round up the size
11836 -- to some convenient boundary, based on alignment considerations, etc).
11838 if Unknown_RM_Size (Rectype)
11839 and then Hbit + 1 <= 32
11840 and then not Strict_Alignment (Rectype)
11843 -- Nothing to do if at least one component has no component clause
11845 Comp := First_Component_Or_Discriminant (Rectype);
11846 while Present (Comp) loop
11847 exit when No (Component_Clause (Comp));
11848 Next_Component_Or_Discriminant (Comp);
11851 -- If we fall out of loop, all components have component clauses
11852 -- and so we can set the size to the maximum value.
11855 Set_RM_Size (Rectype, Hbit + 1);
11858 end Check_Record_Representation_Clause;
11864 procedure Check_Size
11868 Biased : out Boolean)
11870 procedure Size_Too_Small_Error (Min_Siz : Uint);
11871 -- Emit an error concerning illegal size Siz. Min_Siz denotes the
11874 --------------------------
11875 -- Size_Too_Small_Error --
11876 --------------------------
11878 procedure Size_Too_Small_Error (Min_Siz : Uint) is
11880 Error_Msg_Uint_1 := Min_Siz;
11881 Error_Msg_NE (Size_Too_Small_Message, N, T);
11882 end Size_Too_Small_Error;
11886 UT : constant Entity_Id := Underlying_Type (T);
11889 -- Start of processing for Check_Size
11894 -- Reject patently improper size values
11896 if Is_Elementary_Type (T)
11897 and then Siz > UI_From_Int (Int'Last)
11899 Error_Msg_N ("Size value too large for elementary type", N);
11901 if Nkind (Original_Node (N)) = N_Op_Expon then
11903 ("\maybe '* was meant, rather than '*'*", Original_Node (N));
11907 -- Dismiss generic types
11909 if Is_Generic_Type (T)
11911 Is_Generic_Type (UT)
11913 Is_Generic_Type (Root_Type (UT))
11917 -- Guard against previous errors
11919 elsif No (UT) or else UT = Any_Type then
11920 Check_Error_Detected;
11923 -- Check case of bit packed array
11925 elsif Is_Array_Type (UT)
11926 and then Known_Static_Component_Size (UT)
11927 and then Is_Bit_Packed_Array (UT)
11935 Asiz := Component_Size (UT);
11936 Indx := First_Index (UT);
11938 Ityp := Etype (Indx);
11940 -- If non-static bound, then we are not in the business of
11941 -- trying to check the length, and indeed an error will be
11942 -- issued elsewhere, since sizes of non-static array types
11943 -- cannot be set implicitly or explicitly.
11945 if not Is_OK_Static_Subtype (Ityp) then
11949 -- Otherwise accumulate next dimension
11951 Asiz := Asiz * (Expr_Value (Type_High_Bound (Ityp)) -
11952 Expr_Value (Type_Low_Bound (Ityp)) +
11956 exit when No (Indx);
11959 if Asiz <= Siz then
11963 Size_Too_Small_Error (Asiz);
11964 Set_Esize (T, Asiz);
11965 Set_RM_Size (T, Asiz);
11969 -- All other composite types are ignored
11971 elsif Is_Composite_Type (UT) then
11974 -- For fixed-point types, don't check minimum if type is not frozen,
11975 -- since we don't know all the characteristics of the type that can
11976 -- affect the size (e.g. a specified small) till freeze time.
11978 elsif Is_Fixed_Point_Type (UT) and then not Is_Frozen (UT) then
11981 -- Cases for which a minimum check is required
11984 -- Ignore if specified size is correct for the type
11986 if Known_Esize (UT) and then Siz = Esize (UT) then
11990 -- Otherwise get minimum size
11992 M := UI_From_Int (Minimum_Size (UT));
11996 -- Size is less than minimum size, but one possibility remains
11997 -- that we can manage with the new size if we bias the type.
11999 M := UI_From_Int (Minimum_Size (UT, Biased => True));
12002 Size_Too_Small_Error (M);
12004 Set_RM_Size (T, M);
12012 --------------------------
12013 -- Freeze_Entity_Checks --
12014 --------------------------
12016 procedure Freeze_Entity_Checks (N : Node_Id) is
12017 procedure Hide_Non_Overridden_Subprograms (Typ : Entity_Id);
12018 -- Inspect the primitive operations of type Typ and hide all pairs of
12019 -- implicitly declared non-overridden non-fully conformant homographs
12020 -- (Ada RM 8.3 12.3/2).
12022 -------------------------------------
12023 -- Hide_Non_Overridden_Subprograms --
12024 -------------------------------------
12026 procedure Hide_Non_Overridden_Subprograms (Typ : Entity_Id) is
12027 procedure Hide_Matching_Homographs
12028 (Subp_Id : Entity_Id;
12029 Start_Elmt : Elmt_Id);
12030 -- Inspect a list of primitive operations starting with Start_Elmt
12031 -- and find matching implicitly declared non-overridden non-fully
12032 -- conformant homographs of Subp_Id. If found, all matches along
12033 -- with Subp_Id are hidden from all visibility.
12035 function Is_Non_Overridden_Or_Null_Procedure
12036 (Subp_Id : Entity_Id) return Boolean;
12037 -- Determine whether subprogram Subp_Id is implicitly declared non-
12038 -- overridden subprogram or an implicitly declared null procedure.
12040 ------------------------------
12041 -- Hide_Matching_Homographs --
12042 ------------------------------
12044 procedure Hide_Matching_Homographs
12045 (Subp_Id : Entity_Id;
12046 Start_Elmt : Elmt_Id)
12049 Prim_Elmt : Elmt_Id;
12052 Prim_Elmt := Start_Elmt;
12053 while Present (Prim_Elmt) loop
12054 Prim := Node (Prim_Elmt);
12056 -- The current primitive is implicitly declared non-overridden
12057 -- non-fully conformant homograph of Subp_Id. Both subprograms
12058 -- must be hidden from visibility.
12060 if Chars (Prim) = Chars (Subp_Id)
12061 and then Is_Non_Overridden_Or_Null_Procedure (Prim)
12062 and then not Fully_Conformant (Prim, Subp_Id)
12064 Set_Is_Hidden_Non_Overridden_Subpgm (Prim);
12065 Set_Is_Immediately_Visible (Prim, False);
12066 Set_Is_Potentially_Use_Visible (Prim, False);
12068 Set_Is_Hidden_Non_Overridden_Subpgm (Subp_Id);
12069 Set_Is_Immediately_Visible (Subp_Id, False);
12070 Set_Is_Potentially_Use_Visible (Subp_Id, False);
12073 Next_Elmt (Prim_Elmt);
12075 end Hide_Matching_Homographs;
12077 -----------------------------------------
12078 -- Is_Non_Overridden_Or_Null_Procedure --
12079 -----------------------------------------
12081 function Is_Non_Overridden_Or_Null_Procedure
12082 (Subp_Id : Entity_Id) return Boolean
12084 Alias_Id : Entity_Id;
12087 -- The subprogram is inherited (implicitly declared), it does not
12088 -- override and does not cover a primitive of an interface.
12090 if Ekind_In (Subp_Id, E_Function, E_Procedure)
12091 and then Present (Alias (Subp_Id))
12092 and then No (Interface_Alias (Subp_Id))
12093 and then No (Overridden_Operation (Subp_Id))
12095 Alias_Id := Alias (Subp_Id);
12097 if Requires_Overriding (Alias_Id) then
12100 elsif Nkind (Parent (Alias_Id)) = N_Procedure_Specification
12101 and then Null_Present (Parent (Alias_Id))
12108 end Is_Non_Overridden_Or_Null_Procedure;
12112 Prim_Ops : constant Elist_Id := Direct_Primitive_Operations (Typ);
12114 Prim_Elmt : Elmt_Id;
12116 -- Start of processing for Hide_Non_Overridden_Subprograms
12119 -- Inspect the list of primitives looking for non-overridden
12122 if Present (Prim_Ops) then
12123 Prim_Elmt := First_Elmt (Prim_Ops);
12124 while Present (Prim_Elmt) loop
12125 Prim := Node (Prim_Elmt);
12126 Next_Elmt (Prim_Elmt);
12128 if Is_Non_Overridden_Or_Null_Procedure (Prim) then
12129 Hide_Matching_Homographs
12131 Start_Elmt => Prim_Elmt);
12135 end Hide_Non_Overridden_Subprograms;
12139 E : constant Entity_Id := Entity (N);
12141 Nongeneric_Case : constant Boolean := Nkind (N) = N_Freeze_Entity;
12142 -- True in nongeneric case. Some of the processing here is skipped
12143 -- for the generic case since it is not needed. Basically in the
12144 -- generic case, we only need to do stuff that might generate error
12145 -- messages or warnings.
12147 -- Start of processing for Freeze_Entity_Checks
12150 -- Remember that we are processing a freezing entity. Required to
12151 -- ensure correct decoration of internal entities associated with
12152 -- interfaces (see New_Overloaded_Entity).
12154 Inside_Freezing_Actions := Inside_Freezing_Actions + 1;
12156 -- For tagged types covering interfaces add internal entities that link
12157 -- the primitives of the interfaces with the primitives that cover them.
12158 -- Note: These entities were originally generated only when generating
12159 -- code because their main purpose was to provide support to initialize
12160 -- the secondary dispatch tables. They are also used to locate
12161 -- primitives covering interfaces when processing generics (see
12162 -- Derive_Subprograms).
12164 -- This is not needed in the generic case
12166 if Ada_Version >= Ada_2005
12167 and then Nongeneric_Case
12168 and then Ekind (E) = E_Record_Type
12169 and then Is_Tagged_Type (E)
12170 and then not Is_Interface (E)
12171 and then Has_Interfaces (E)
12173 -- This would be a good common place to call the routine that checks
12174 -- overriding of interface primitives (and thus factorize calls to
12175 -- Check_Abstract_Overriding located at different contexts in the
12176 -- compiler). However, this is not possible because it causes
12177 -- spurious errors in case of late overriding.
12179 Add_Internal_Interface_Entities (E);
12182 -- After all forms of overriding have been resolved, a tagged type may
12183 -- be left with a set of implicitly declared and possibly erroneous
12184 -- abstract subprograms, null procedures and subprograms that require
12185 -- overriding. If this set contains fully conformant homographs, then
12186 -- one is chosen arbitrarily (already done during resolution), otherwise
12187 -- all remaining non-fully conformant homographs are hidden from
12188 -- visibility (Ada RM 8.3 12.3/2).
12190 if Is_Tagged_Type (E) then
12191 Hide_Non_Overridden_Subprograms (E);
12196 if Ekind (E) = E_Record_Type
12197 and then Is_CPP_Class (E)
12198 and then Is_Tagged_Type (E)
12199 and then Tagged_Type_Expansion
12201 if CPP_Num_Prims (E) = 0 then
12203 -- If the CPP type has user defined components then it must import
12204 -- primitives from C++. This is required because if the C++ class
12205 -- has no primitives then the C++ compiler does not added the _tag
12206 -- component to the type.
12208 if First_Entity (E) /= Last_Entity (E) then
12210 ("'C'P'P type must import at least one primitive from C++??",
12215 -- Check that all its primitives are abstract or imported from C++.
12216 -- Check also availability of the C++ constructor.
12219 Has_Constructors : constant Boolean := Has_CPP_Constructors (E);
12221 Error_Reported : Boolean := False;
12225 Elmt := First_Elmt (Primitive_Operations (E));
12226 while Present (Elmt) loop
12227 Prim := Node (Elmt);
12229 if Comes_From_Source (Prim) then
12230 if Is_Abstract_Subprogram (Prim) then
12233 elsif not Is_Imported (Prim)
12234 or else Convention (Prim) /= Convention_CPP
12237 ("primitives of 'C'P'P types must be imported from C++ "
12238 & "or abstract??", Prim);
12240 elsif not Has_Constructors
12241 and then not Error_Reported
12243 Error_Msg_Name_1 := Chars (E);
12245 ("??'C'P'P constructor required for type %", Prim);
12246 Error_Reported := True;
12255 -- Check Ada derivation of CPP type
12257 if Expander_Active -- why? losing errors in -gnatc mode???
12258 and then Present (Etype (E)) -- defend against errors
12259 and then Tagged_Type_Expansion
12260 and then Ekind (E) = E_Record_Type
12261 and then Etype (E) /= E
12262 and then Is_CPP_Class (Etype (E))
12263 and then CPP_Num_Prims (Etype (E)) > 0
12264 and then not Is_CPP_Class (E)
12265 and then not Has_CPP_Constructors (Etype (E))
12267 -- If the parent has C++ primitives but it has no constructor then
12268 -- check that all the primitives are overridden in this derivation;
12269 -- otherwise the constructor of the parent is needed to build the
12277 Elmt := First_Elmt (Primitive_Operations (E));
12278 while Present (Elmt) loop
12279 Prim := Node (Elmt);
12281 if not Is_Abstract_Subprogram (Prim)
12282 and then No (Interface_Alias (Prim))
12283 and then Find_Dispatching_Type (Ultimate_Alias (Prim)) /= E
12285 Error_Msg_Name_1 := Chars (Etype (E));
12287 ("'C'P'P constructor required for parent type %", E);
12296 Inside_Freezing_Actions := Inside_Freezing_Actions - 1;
12298 -- If we have a type with predicates, build predicate function. This is
12299 -- not needed in the generic case, nor within TSS subprograms and other
12300 -- predefined primitives. For a derived type, ensure that the parent
12301 -- type is already frozen so that its predicate function has been
12302 -- constructed already. This is necessary if the parent is declared
12303 -- in a nested package and its own freeze point has not been reached.
12306 and then Nongeneric_Case
12307 and then not Within_Internal_Subprogram
12308 and then Has_Predicates (E)
12311 Atyp : constant Entity_Id := Nearest_Ancestor (E);
12314 and then Has_Predicates (Atyp)
12315 and then not Is_Frozen (Atyp)
12317 Freeze_Before (N, Atyp);
12321 Build_Predicate_Functions (E, N);
12324 -- If type has delayed aspects, this is where we do the preanalysis at
12325 -- the freeze point, as part of the consistent visibility check. Note
12326 -- that this must be done after calling Build_Predicate_Functions or
12327 -- Build_Invariant_Procedure since these subprograms fix occurrences of
12328 -- the subtype name in the saved expression so that they will not cause
12329 -- trouble in the preanalysis.
12331 -- This is also not needed in the generic case
12334 and then Has_Delayed_Aspects (E)
12335 and then Scope (E) = Current_Scope
12342 -- Look for aspect specification entries for this entity
12344 Ritem := First_Rep_Item (E);
12345 while Present (Ritem) loop
12346 if Nkind (Ritem) = N_Aspect_Specification
12347 and then Entity (Ritem) = E
12348 and then Is_Delayed_Aspect (Ritem)
12350 A_Id := Get_Aspect_Id (Ritem);
12352 if A_Id = Aspect_Dynamic_Predicate
12353 or else A_Id = Aspect_Predicate
12354 or else A_Id = Aspect_Priority
12355 or else A_Id = Aspect_CPU
12357 -- Retrieve the visibility to components and discriminants
12358 -- in order to properly analyze the aspects.
12361 Check_Aspect_At_Freeze_Point (Ritem);
12365 Check_Aspect_At_Freeze_Point (Ritem);
12369 Next_Rep_Item (Ritem);
12375 -- For a record type, deal with variant parts. This has to be delayed to
12376 -- this point, because of the issue of statically predicated subtypes,
12377 -- which we have to ensure are frozen before checking choices, since we
12378 -- need to have the static choice list set.
12380 if Is_Record_Type (E) then
12381 Check_Variant_Part : declare
12382 D : constant Node_Id := Declaration_Node (E);
12387 Others_Present : Boolean;
12388 pragma Warnings (Off, Others_Present);
12389 -- Indicates others present, not used in this case
12391 procedure Non_Static_Choice_Error (Choice : Node_Id);
12392 -- Error routine invoked by the generic instantiation below when
12393 -- the variant part has a non static choice.
12395 procedure Process_Declarations (Variant : Node_Id);
12396 -- Processes declarations associated with a variant. We analyzed
12397 -- the declarations earlier (in Sem_Ch3.Analyze_Variant_Part),
12398 -- but we still need the recursive call to Check_Choices for any
12399 -- nested variant to get its choices properly processed. This is
12400 -- also where we expand out the choices if expansion is active.
12402 package Variant_Choices_Processing is new
12403 Generic_Check_Choices
12404 (Process_Empty_Choice => No_OP,
12405 Process_Non_Static_Choice => Non_Static_Choice_Error,
12406 Process_Associated_Node => Process_Declarations);
12407 use Variant_Choices_Processing;
12409 -----------------------------
12410 -- Non_Static_Choice_Error --
12411 -----------------------------
12413 procedure Non_Static_Choice_Error (Choice : Node_Id) is
12415 Flag_Non_Static_Expr
12416 ("choice given in variant part is not static!", Choice);
12417 end Non_Static_Choice_Error;
12419 --------------------------
12420 -- Process_Declarations --
12421 --------------------------
12423 procedure Process_Declarations (Variant : Node_Id) is
12424 CL : constant Node_Id := Component_List (Variant);
12428 -- Check for static predicate present in this variant
12430 if Has_SP_Choice (Variant) then
12432 -- Here we expand. You might expect to find this call in
12433 -- Expand_N_Variant_Part, but that is called when we first
12434 -- see the variant part, and we cannot do this expansion
12435 -- earlier than the freeze point, since for statically
12436 -- predicated subtypes, the predicate is not known till
12437 -- the freeze point.
12439 -- Furthermore, we do this expansion even if the expander
12440 -- is not active, because other semantic processing, e.g.
12441 -- for aggregates, requires the expanded list of choices.
12443 -- If the expander is not active, then we can't just clobber
12444 -- the list since it would invalidate the tree.
12445 -- So we have to rewrite the variant part with a Rewrite
12446 -- call that replaces it with a copy and clobber the copy.
12448 if not Expander_Active then
12450 NewV : constant Node_Id := New_Copy (Variant);
12452 Set_Discrete_Choices
12453 (NewV, New_Copy_List (Discrete_Choices (Variant)));
12454 Rewrite (Variant, NewV);
12458 Expand_Static_Predicates_In_Choices (Variant);
12461 -- We don't need to worry about the declarations in the variant
12462 -- (since they were analyzed by Analyze_Choices when we first
12463 -- encountered the variant), but we do need to take care of
12464 -- expansion of any nested variants.
12466 if not Null_Present (CL) then
12467 VP := Variant_Part (CL);
12469 if Present (VP) then
12471 (VP, Variants (VP), Etype (Name (VP)), Others_Present);
12474 end Process_Declarations;
12476 -- Start of processing for Check_Variant_Part
12479 -- Find component list
12483 if Nkind (D) = N_Full_Type_Declaration then
12484 T := Type_Definition (D);
12486 if Nkind (T) = N_Record_Definition then
12487 C := Component_List (T);
12489 elsif Nkind (T) = N_Derived_Type_Definition
12490 and then Present (Record_Extension_Part (T))
12492 C := Component_List (Record_Extension_Part (T));
12496 -- Case of variant part present
12498 if Present (C) and then Present (Variant_Part (C)) then
12499 VP := Variant_Part (C);
12504 (VP, Variants (VP), Etype (Name (VP)), Others_Present);
12506 -- If the last variant does not contain the Others choice,
12507 -- replace it with an N_Others_Choice node since Gigi always
12508 -- wants an Others. Note that we do not bother to call Analyze
12509 -- on the modified variant part, since its only effect would be
12510 -- to compute the Others_Discrete_Choices node laboriously, and
12511 -- of course we already know the list of choices corresponding
12512 -- to the others choice (it's the list we're replacing).
12514 -- We only want to do this if the expander is active, since
12515 -- we do not want to clobber the tree.
12517 if Expander_Active then
12519 Last_Var : constant Node_Id :=
12520 Last_Non_Pragma (Variants (VP));
12522 Others_Node : Node_Id;
12525 if Nkind (First (Discrete_Choices (Last_Var))) /=
12528 Others_Node := Make_Others_Choice (Sloc (Last_Var));
12529 Set_Others_Discrete_Choices
12530 (Others_Node, Discrete_Choices (Last_Var));
12531 Set_Discrete_Choices
12532 (Last_Var, New_List (Others_Node));
12537 end Check_Variant_Part;
12539 end Freeze_Entity_Checks;
12541 -------------------------
12542 -- Get_Alignment_Value --
12543 -------------------------
12545 function Get_Alignment_Value (Expr : Node_Id) return Uint is
12546 Align : constant Uint := Static_Integer (Expr);
12549 if Align = No_Uint then
12552 elsif Align < 0 then
12553 Error_Msg_N ("alignment value must be positive", Expr);
12556 -- If Alignment is specified to be 0, we treat it the same as 1
12558 elsif Align = 0 then
12562 for J in Int range 0 .. 64 loop
12564 M : constant Uint := Uint_2 ** J;
12567 exit when M = Align;
12570 Error_Msg_N ("alignment value must be power of 2", Expr);
12578 end Get_Alignment_Value;
12580 -------------------------------------
12581 -- Inherit_Aspects_At_Freeze_Point --
12582 -------------------------------------
12584 procedure Inherit_Aspects_At_Freeze_Point (Typ : Entity_Id) is
12585 function Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
12586 (Rep_Item : Node_Id) return Boolean;
12587 -- This routine checks if Rep_Item is either a pragma or an aspect
12588 -- specification node whose correponding pragma (if any) is present in
12589 -- the Rep Item chain of the entity it has been specified to.
12591 function Rep_Item_Entity (Rep_Item : Node_Id) return Entity_Id;
12592 -- Return the entity for which Rep_Item is specified
12594 --------------------------------------------------
12595 -- Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item --
12596 --------------------------------------------------
12598 function Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
12599 (Rep_Item : Node_Id) return Boolean
12603 Nkind (Rep_Item) = N_Pragma
12604 or else Present_In_Rep_Item
12605 (Entity (Rep_Item), Aspect_Rep_Item (Rep_Item));
12606 end Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item;
12608 ---------------------
12609 -- Rep_Item_Entity --
12610 ---------------------
12612 function Rep_Item_Entity (Rep_Item : Node_Id) return Entity_Id is
12614 if Nkind (Rep_Item) = N_Aspect_Specification then
12615 return Entity (Rep_Item);
12618 pragma Assert (Nkind_In (Rep_Item,
12619 N_Attribute_Definition_Clause,
12621 return Entity (Name (Rep_Item));
12623 end Rep_Item_Entity;
12625 -- Start of processing for Inherit_Aspects_At_Freeze_Point
12628 -- A representation item is either subtype-specific (Size and Alignment
12629 -- clauses) or type-related (all others). Subtype-specific aspects may
12630 -- differ for different subtypes of the same type (RM 13.1.8).
12632 -- A derived type inherits each type-related representation aspect of
12633 -- its parent type that was directly specified before the declaration of
12634 -- the derived type (RM 13.1.15).
12636 -- A derived subtype inherits each subtype-specific representation
12637 -- aspect of its parent subtype that was directly specified before the
12638 -- declaration of the derived type (RM 13.1.15).
12640 -- The general processing involves inheriting a representation aspect
12641 -- from a parent type whenever the first rep item (aspect specification,
12642 -- attribute definition clause, pragma) corresponding to the given
12643 -- representation aspect in the rep item chain of Typ, if any, isn't
12644 -- directly specified to Typ but to one of its parents.
12646 -- ??? Note that, for now, just a limited number of representation
12647 -- aspects have been inherited here so far. Many of them are
12648 -- still inherited in Sem_Ch3. This will be fixed soon. Here is
12649 -- a non- exhaustive list of aspects that likely also need to
12650 -- be moved to this routine: Alignment, Component_Alignment,
12651 -- Component_Size, Machine_Radix, Object_Size, Pack, Predicates,
12652 -- Preelaborable_Initialization, RM_Size and Small.
12654 -- In addition, Convention must be propagated from base type to subtype,
12655 -- because the subtype may have been declared on an incomplete view.
12657 if Nkind (Parent (Typ)) = N_Private_Extension_Declaration then
12663 if not Has_Rep_Item (Typ, Name_Ada_05, Name_Ada_2005, False)
12664 and then Has_Rep_Item (Typ, Name_Ada_05, Name_Ada_2005)
12665 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
12666 (Get_Rep_Item (Typ, Name_Ada_05, Name_Ada_2005))
12668 Set_Is_Ada_2005_Only (Typ);
12673 if not Has_Rep_Item (Typ, Name_Ada_12, Name_Ada_2012, False)
12674 and then Has_Rep_Item (Typ, Name_Ada_12, Name_Ada_2012)
12675 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
12676 (Get_Rep_Item (Typ, Name_Ada_12, Name_Ada_2012))
12678 Set_Is_Ada_2012_Only (Typ);
12683 if not Has_Rep_Item (Typ, Name_Atomic, Name_Shared, False)
12684 and then Has_Rep_Pragma (Typ, Name_Atomic, Name_Shared)
12685 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
12686 (Get_Rep_Item (Typ, Name_Atomic, Name_Shared))
12688 Set_Is_Atomic (Typ);
12689 Set_Is_Volatile (Typ);
12690 Set_Treat_As_Volatile (Typ);
12695 if Is_Record_Type (Typ)
12696 and then Typ /= Base_Type (Typ) and then Is_Frozen (Base_Type (Typ))
12698 Set_Convention (Typ, Convention (Base_Type (Typ)));
12701 -- Default_Component_Value
12703 -- Verify that there is no rep_item declared for the type, and there
12704 -- is one coming from an ancestor.
12706 if Is_Array_Type (Typ)
12707 and then Is_Base_Type (Typ)
12708 and then not Has_Rep_Item (Typ, Name_Default_Component_Value, False)
12709 and then Has_Rep_Item (Typ, Name_Default_Component_Value)
12711 Set_Default_Aspect_Component_Value (Typ,
12712 Default_Aspect_Component_Value
12713 (Entity (Get_Rep_Item (Typ, Name_Default_Component_Value))));
12718 if Is_Scalar_Type (Typ)
12719 and then Is_Base_Type (Typ)
12720 and then not Has_Rep_Item (Typ, Name_Default_Value, False)
12721 and then Has_Rep_Item (Typ, Name_Default_Value)
12723 Set_Has_Default_Aspect (Typ);
12724 Set_Default_Aspect_Value (Typ,
12725 Default_Aspect_Value
12726 (Entity (Get_Rep_Item (Typ, Name_Default_Value))));
12731 if not Has_Rep_Item (Typ, Name_Discard_Names, False)
12732 and then Has_Rep_Item (Typ, Name_Discard_Names)
12733 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
12734 (Get_Rep_Item (Typ, Name_Discard_Names))
12736 Set_Discard_Names (Typ);
12741 if not Has_Rep_Item (Typ, Name_Volatile, False)
12742 and then Has_Rep_Item (Typ, Name_Volatile)
12743 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
12744 (Get_Rep_Item (Typ, Name_Volatile))
12746 Set_Is_Volatile (Typ);
12747 Set_Treat_As_Volatile (Typ);
12750 -- Volatile_Full_Access
12752 if not Has_Rep_Item (Typ, Name_Volatile_Full_Access, False)
12753 and then Has_Rep_Pragma (Typ, Name_Volatile_Full_Access)
12754 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
12755 (Get_Rep_Item (Typ, Name_Volatile_Full_Access))
12757 Set_Is_Volatile_Full_Access (Typ);
12758 Set_Is_Volatile (Typ);
12759 Set_Treat_As_Volatile (Typ);
12762 -- Inheritance for derived types only
12764 if Is_Derived_Type (Typ) then
12766 Bas_Typ : constant Entity_Id := Base_Type (Typ);
12767 Imp_Bas_Typ : constant Entity_Id := Implementation_Base_Type (Typ);
12770 -- Atomic_Components
12772 if not Has_Rep_Item (Typ, Name_Atomic_Components, False)
12773 and then Has_Rep_Item (Typ, Name_Atomic_Components)
12774 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
12775 (Get_Rep_Item (Typ, Name_Atomic_Components))
12777 Set_Has_Atomic_Components (Imp_Bas_Typ);
12780 -- Volatile_Components
12782 if not Has_Rep_Item (Typ, Name_Volatile_Components, False)
12783 and then Has_Rep_Item (Typ, Name_Volatile_Components)
12784 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
12785 (Get_Rep_Item (Typ, Name_Volatile_Components))
12787 Set_Has_Volatile_Components (Imp_Bas_Typ);
12790 -- Finalize_Storage_Only
12792 if not Has_Rep_Pragma (Typ, Name_Finalize_Storage_Only, False)
12793 and then Has_Rep_Pragma (Typ, Name_Finalize_Storage_Only)
12795 Set_Finalize_Storage_Only (Bas_Typ);
12798 -- Universal_Aliasing
12800 if not Has_Rep_Item (Typ, Name_Universal_Aliasing, False)
12801 and then Has_Rep_Item (Typ, Name_Universal_Aliasing)
12802 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
12803 (Get_Rep_Item (Typ, Name_Universal_Aliasing))
12805 Set_Universal_Aliasing (Imp_Bas_Typ);
12810 if Is_Record_Type (Typ) then
12811 if not Has_Rep_Item (Typ, Name_Bit_Order, False)
12812 and then Has_Rep_Item (Typ, Name_Bit_Order)
12814 Set_Reverse_Bit_Order (Bas_Typ,
12815 Reverse_Bit_Order (Rep_Item_Entity
12816 (Get_Rep_Item (Typ, Name_Bit_Order))));
12820 -- Scalar_Storage_Order
12822 -- Note: the aspect is specified on a first subtype, but recorded
12823 -- in a flag of the base type!
12825 if (Is_Record_Type (Typ) or else Is_Array_Type (Typ))
12826 and then Typ = Bas_Typ
12828 -- For a type extension, always inherit from parent; otherwise
12829 -- inherit if no default applies. Note: we do not check for
12830 -- an explicit rep item on the parent type when inheriting,
12831 -- because the parent SSO may itself have been set by default.
12833 if not Has_Rep_Item (First_Subtype (Typ),
12834 Name_Scalar_Storage_Order, False)
12835 and then (Is_Tagged_Type (Bas_Typ)
12836 or else not (SSO_Set_Low_By_Default (Bas_Typ)
12838 SSO_Set_High_By_Default (Bas_Typ)))
12840 Set_Reverse_Storage_Order (Bas_Typ,
12841 Reverse_Storage_Order
12842 (Implementation_Base_Type (Etype (Bas_Typ))));
12844 -- Clear default SSO indications, since the inherited aspect
12845 -- which was set explicitly overrides the default.
12847 Set_SSO_Set_Low_By_Default (Bas_Typ, False);
12848 Set_SSO_Set_High_By_Default (Bas_Typ, False);
12853 end Inherit_Aspects_At_Freeze_Point;
12859 procedure Initialize is
12861 Address_Clause_Checks.Init;
12862 Unchecked_Conversions.Init;
12864 -- ??? Might be needed in the future for some non GCC back-ends
12865 -- if AAMP_On_Target then
12866 -- Independence_Checks.Init;
12870 ---------------------------
12871 -- Install_Discriminants --
12872 ---------------------------
12874 procedure Install_Discriminants (E : Entity_Id) is
12878 Disc := First_Discriminant (E);
12879 while Present (Disc) loop
12880 Prev := Current_Entity (Disc);
12881 Set_Current_Entity (Disc);
12882 Set_Is_Immediately_Visible (Disc);
12883 Set_Homonym (Disc, Prev);
12884 Next_Discriminant (Disc);
12886 end Install_Discriminants;
12888 -------------------------
12889 -- Is_Operational_Item --
12890 -------------------------
12892 function Is_Operational_Item (N : Node_Id) return Boolean is
12894 if Nkind (N) /= N_Attribute_Definition_Clause then
12899 Id : constant Attribute_Id := Get_Attribute_Id (Chars (N));
12902 -- List of operational items is given in AARM 13.1(8.mm/1).
12903 -- It is clearly incomplete, as it does not include iterator
12904 -- aspects, among others.
12906 return Id = Attribute_Constant_Indexing
12907 or else Id = Attribute_Default_Iterator
12908 or else Id = Attribute_Implicit_Dereference
12909 or else Id = Attribute_Input
12910 or else Id = Attribute_Iterator_Element
12911 or else Id = Attribute_Iterable
12912 or else Id = Attribute_Output
12913 or else Id = Attribute_Read
12914 or else Id = Attribute_Variable_Indexing
12915 or else Id = Attribute_Write
12916 or else Id = Attribute_External_Tag;
12919 end Is_Operational_Item;
12921 -------------------------
12922 -- Is_Predicate_Static --
12923 -------------------------
12925 -- Note: the basic legality of the expression has already been checked, so
12926 -- we don't need to worry about cases or ranges on strings for example.
12928 function Is_Predicate_Static
12930 Nam : Name_Id) return Boolean
12932 function All_Static_Case_Alternatives (L : List_Id) return Boolean;
12933 -- Given a list of case expression alternatives, returns True if all
12934 -- the alternatives are static (have all static choices, and a static
12937 function Is_Type_Ref (N : Node_Id) return Boolean;
12938 pragma Inline (Is_Type_Ref);
12939 -- Returns True if N is a reference to the type for the predicate in the
12940 -- expression (i.e. if it is an identifier whose Chars field matches the
12941 -- Nam given in the call). N must not be parenthesized, if the type name
12942 -- appears in parens, this routine will return False.
12944 -- The routine also returns True for function calls generated during the
12945 -- expansion of comparison operators on strings, which are intended to
12946 -- be legal in static predicates, and are converted into calls to array
12947 -- comparison routines in the body of the corresponding predicate
12950 ----------------------------------
12951 -- All_Static_Case_Alternatives --
12952 ----------------------------------
12954 function All_Static_Case_Alternatives (L : List_Id) return Boolean is
12959 while Present (N) loop
12960 if not (All_Static_Choices (Discrete_Choices (N))
12961 and then Is_OK_Static_Expression (Expression (N)))
12970 end All_Static_Case_Alternatives;
12976 function Is_Type_Ref (N : Node_Id) return Boolean is
12978 return (Nkind (N) = N_Identifier
12979 and then Chars (N) = Nam
12980 and then Paren_Count (N) = 0)
12981 or else Nkind (N) = N_Function_Call;
12984 -- Start of processing for Is_Predicate_Static
12987 -- Predicate_Static means one of the following holds. Numbers are the
12988 -- corresponding paragraph numbers in (RM 3.2.4(16-22)).
12990 -- 16: A static expression
12992 if Is_OK_Static_Expression (Expr) then
12995 -- 17: A membership test whose simple_expression is the current
12996 -- instance, and whose membership_choice_list meets the requirements
12997 -- for a static membership test.
12999 elsif Nkind (Expr) in N_Membership_Test
13000 and then All_Membership_Choices_Static (Expr)
13004 -- 18. A case_expression whose selecting_expression is the current
13005 -- instance, and whose dependent expressions are static expressions.
13007 elsif Nkind (Expr) = N_Case_Expression
13008 and then Is_Type_Ref (Expression (Expr))
13009 and then All_Static_Case_Alternatives (Alternatives (Expr))
13013 -- 19. A call to a predefined equality or ordering operator, where one
13014 -- operand is the current instance, and the other is a static
13017 -- Note: the RM is clearly wrong here in not excluding string types.
13018 -- Without this exclusion, we would allow expressions like X > "ABC"
13019 -- to be considered as predicate-static, which is clearly not intended,
13020 -- since the idea is for predicate-static to be a subset of normal
13021 -- static expressions (and "DEF" > "ABC" is not a static expression).
13023 -- However, we do allow internally generated (not from source) equality
13024 -- and inequality operations to be valid on strings (this helps deal
13025 -- with cases where we transform A in "ABC" to A = "ABC).
13027 -- In fact, it appears that the intent of the ARG is to extend static
13028 -- predicates to strings, and that the extension should probably apply
13029 -- to static expressions themselves. The code below accepts comparison
13030 -- operators that apply to static strings.
13032 elsif Nkind (Expr) in N_Op_Compare
13033 and then ((Is_Type_Ref (Left_Opnd (Expr))
13034 and then Is_OK_Static_Expression (Right_Opnd (Expr)))
13036 (Is_Type_Ref (Right_Opnd (Expr))
13037 and then Is_OK_Static_Expression (Left_Opnd (Expr))))
13041 -- 20. A call to a predefined boolean logical operator, where each
13042 -- operand is predicate-static.
13044 elsif (Nkind_In (Expr, N_Op_And, N_Op_Or, N_Op_Xor)
13045 and then Is_Predicate_Static (Left_Opnd (Expr), Nam)
13046 and then Is_Predicate_Static (Right_Opnd (Expr), Nam))
13048 (Nkind (Expr) = N_Op_Not
13049 and then Is_Predicate_Static (Right_Opnd (Expr), Nam))
13053 -- 21. A short-circuit control form where both operands are
13054 -- predicate-static.
13056 elsif Nkind (Expr) in N_Short_Circuit
13057 and then Is_Predicate_Static (Left_Opnd (Expr), Nam)
13058 and then Is_Predicate_Static (Right_Opnd (Expr), Nam)
13062 -- 22. A parenthesized predicate-static expression. This does not
13063 -- require any special test, since we just ignore paren levels in
13064 -- all the cases above.
13066 -- One more test that is an implementation artifact caused by the fact
13067 -- that we are analyzing not the original expression, but the generated
13068 -- expression in the body of the predicate function. This can include
13069 -- references to inherited predicates, so that the expression we are
13070 -- processing looks like:
13072 -- xxPredicate (typ (Inns)) and then expression
13074 -- Where the call is to a Predicate function for an inherited predicate.
13075 -- We simply ignore such a call, which could be to either a dynamic or
13076 -- a static predicate. Note that if the parent predicate is dynamic then
13077 -- eventually this type will be marked as dynamic, but you are allowed
13078 -- to specify a static predicate for a subtype which is inheriting a
13079 -- dynamic predicate, so the static predicate validation here ignores
13080 -- the inherited predicate even if it is dynamic.
13081 -- In all cases, a static predicate can only apply to a scalar type.
13083 elsif Nkind (Expr) = N_Function_Call
13084 and then Is_Predicate_Function (Entity (Name (Expr)))
13085 and then Is_Scalar_Type (Etype (First_Entity (Entity (Name (Expr)))))
13089 elsif Is_Entity_Name (Expr)
13090 and then Entity (Expr) = Standard_True
13092 Error_Msg_N ("predicate is redundant (always True)?", Expr);
13095 -- That's an exhaustive list of tests, all other cases are not
13096 -- predicate-static, so we return False.
13101 end Is_Predicate_Static;
13103 ----------------------
13104 -- Is_Static_Choice --
13105 ----------------------
13107 function Is_Static_Choice (N : Node_Id) return Boolean is
13109 return Nkind (N) = N_Others_Choice
13110 or else Is_OK_Static_Expression (N)
13111 or else (Is_Entity_Name (N) and then Is_Type (Entity (N))
13112 and then Is_OK_Static_Subtype (Entity (N)))
13113 or else (Nkind (N) = N_Subtype_Indication
13114 and then Is_OK_Static_Subtype (Entity (N)))
13115 or else (Nkind (N) = N_Range and then Is_OK_Static_Range (N));
13116 end Is_Static_Choice;
13118 ------------------------------
13119 -- Is_Type_Related_Rep_Item --
13120 ------------------------------
13122 function Is_Type_Related_Rep_Item (N : Node_Id) return Boolean is
13125 when N_Attribute_Definition_Clause =>
13127 Id : constant Attribute_Id := Get_Attribute_Id (Chars (N));
13128 -- See AARM 13.1(8.f-8.x) list items that end in "clause"
13129 -- ???: include any GNAT-defined attributes here?
13131 return Id = Attribute_Component_Size
13132 or else Id = Attribute_Bit_Order
13133 or else Id = Attribute_Storage_Pool
13134 or else Id = Attribute_Stream_Size
13135 or else Id = Attribute_Machine_Radix;
13139 case Get_Pragma_Id (N) is
13140 -- See AARM 13.1(8.f-8.x) list items that start with "pragma"
13141 -- ???: include any GNAT-defined pragmas here?
13145 | Pragma_Convention
13147 | Pragma_Independent
13149 | Pragma_Atomic_Components
13150 | Pragma_Independent_Components
13151 | Pragma_Volatile_Components
13152 | Pragma_Discard_Names
13159 when N_Enumeration_Representation_Clause
13160 | N_Record_Representation_Clause
13169 end Is_Type_Related_Rep_Item;
13171 ---------------------
13172 -- Kill_Rep_Clause --
13173 ---------------------
13175 procedure Kill_Rep_Clause (N : Node_Id) is
13177 pragma Assert (Ignore_Rep_Clauses);
13179 -- Note: we use Replace rather than Rewrite, because we don't want
13180 -- tools to be able to use Original_Node to dig out the (undecorated)
13181 -- rep clause that is being replaced.
13183 Replace (N, Make_Null_Statement (Sloc (N)));
13185 -- The null statement must be marked as not coming from source. This is
13186 -- so that tools ignore it, and also the back end does not expect bogus
13187 -- "from source" null statements in weird places (e.g. in declarative
13188 -- regions where such null statements are not allowed).
13190 Set_Comes_From_Source (N, False);
13191 end Kill_Rep_Clause;
13197 function Minimum_Size
13199 Biased : Boolean := False) return Nat
13201 Lo : Uint := No_Uint;
13202 Hi : Uint := No_Uint;
13203 LoR : Ureal := No_Ureal;
13204 HiR : Ureal := No_Ureal;
13205 LoSet : Boolean := False;
13206 HiSet : Boolean := False;
13209 Ancest : Entity_Id;
13210 R_Typ : constant Entity_Id := Root_Type (T);
13213 -- If bad type, return 0
13215 if T = Any_Type then
13218 -- For generic types, just return zero. There cannot be any legitimate
13219 -- need to know such a size, but this routine may be called with a
13220 -- generic type as part of normal processing.
13222 elsif Is_Generic_Type (R_Typ) or else R_Typ = Any_Type then
13225 -- Access types (cannot have size smaller than System.Address)
13227 elsif Is_Access_Type (T) then
13228 return System_Address_Size;
13230 -- Floating-point types
13232 elsif Is_Floating_Point_Type (T) then
13233 return UI_To_Int (Esize (R_Typ));
13237 elsif Is_Discrete_Type (T) then
13239 -- The following loop is looking for the nearest compile time known
13240 -- bounds following the ancestor subtype chain. The idea is to find
13241 -- the most restrictive known bounds information.
13245 if Ancest = Any_Type or else Etype (Ancest) = Any_Type then
13250 if Compile_Time_Known_Value (Type_Low_Bound (Ancest)) then
13251 Lo := Expr_Rep_Value (Type_Low_Bound (Ancest));
13258 if Compile_Time_Known_Value (Type_High_Bound (Ancest)) then
13259 Hi := Expr_Rep_Value (Type_High_Bound (Ancest));
13265 Ancest := Ancestor_Subtype (Ancest);
13267 if No (Ancest) then
13268 Ancest := Base_Type (T);
13270 if Is_Generic_Type (Ancest) then
13276 -- Fixed-point types. We can't simply use Expr_Value to get the
13277 -- Corresponding_Integer_Value values of the bounds, since these do not
13278 -- get set till the type is frozen, and this routine can be called
13279 -- before the type is frozen. Similarly the test for bounds being static
13280 -- needs to include the case where we have unanalyzed real literals for
13281 -- the same reason.
13283 elsif Is_Fixed_Point_Type (T) then
13285 -- The following loop is looking for the nearest compile time known
13286 -- bounds following the ancestor subtype chain. The idea is to find
13287 -- the most restrictive known bounds information.
13291 if Ancest = Any_Type or else Etype (Ancest) = Any_Type then
13295 -- Note: In the following two tests for LoSet and HiSet, it may
13296 -- seem redundant to test for N_Real_Literal here since normally
13297 -- one would assume that the test for the value being known at
13298 -- compile time includes this case. However, there is a glitch.
13299 -- If the real literal comes from folding a non-static expression,
13300 -- then we don't consider any non- static expression to be known
13301 -- at compile time if we are in configurable run time mode (needed
13302 -- in some cases to give a clearer definition of what is and what
13303 -- is not accepted). So the test is indeed needed. Without it, we
13304 -- would set neither Lo_Set nor Hi_Set and get an infinite loop.
13307 if Nkind (Type_Low_Bound (Ancest)) = N_Real_Literal
13308 or else Compile_Time_Known_Value (Type_Low_Bound (Ancest))
13310 LoR := Expr_Value_R (Type_Low_Bound (Ancest));
13317 if Nkind (Type_High_Bound (Ancest)) = N_Real_Literal
13318 or else Compile_Time_Known_Value (Type_High_Bound (Ancest))
13320 HiR := Expr_Value_R (Type_High_Bound (Ancest));
13326 Ancest := Ancestor_Subtype (Ancest);
13328 if No (Ancest) then
13329 Ancest := Base_Type (T);
13331 if Is_Generic_Type (Ancest) then
13337 Lo := UR_To_Uint (LoR / Small_Value (T));
13338 Hi := UR_To_Uint (HiR / Small_Value (T));
13340 -- No other types allowed
13343 raise Program_Error;
13346 -- Fall through with Hi and Lo set. Deal with biased case
13349 and then not Is_Fixed_Point_Type (T)
13350 and then not (Is_Enumeration_Type (T)
13351 and then Has_Non_Standard_Rep (T)))
13352 or else Has_Biased_Representation (T)
13358 -- Null range case, size is always zero. We only do this in the discrete
13359 -- type case, since that's the odd case that came up. Probably we should
13360 -- also do this in the fixed-point case, but doing so causes peculiar
13361 -- gigi failures, and it is not worth worrying about this incredibly
13362 -- marginal case (explicit null-range fixed-point type declarations)???
13364 if Lo > Hi and then Is_Discrete_Type (T) then
13367 -- Signed case. Note that we consider types like range 1 .. -1 to be
13368 -- signed for the purpose of computing the size, since the bounds have
13369 -- to be accommodated in the base type.
13371 elsif Lo < 0 or else Hi < 0 then
13375 -- S = size, B = 2 ** (size - 1) (can accommodate -B .. +(B - 1))
13376 -- Note that we accommodate the case where the bounds cross. This
13377 -- can happen either because of the way the bounds are declared
13378 -- or because of the algorithm in Freeze_Fixed_Point_Type.
13392 -- If both bounds are positive, make sure that both are represen-
13393 -- table in the case where the bounds are crossed. This can happen
13394 -- either because of the way the bounds are declared, or because of
13395 -- the algorithm in Freeze_Fixed_Point_Type.
13401 -- S = size, (can accommodate 0 .. (2**size - 1))
13404 while Hi >= Uint_2 ** S loop
13412 ------------------------------
13413 -- New_Put_Image_Subprogram --
13414 ------------------------------
13416 procedure New_Put_Image_Subprogram
13421 Loc : constant Source_Ptr := Sloc (N);
13422 Sname : constant Name_Id :=
13423 Make_TSS_Name (Base_Type (Ent), TSS_Put_Image);
13424 Subp_Id : Entity_Id;
13425 Subp_Decl : Node_Id;
13429 Defer_Declaration : constant Boolean :=
13430 Is_Tagged_Type (Ent) or else Is_Private_Type (Ent);
13431 -- For a tagged type, there is a declaration at the freeze point, and
13432 -- we must generate only a completion of this declaration. We do the
13433 -- same for private types, because the full view might be tagged.
13434 -- Otherwise we generate a declaration at the point of the attribute
13435 -- definition clause. If the attribute definition comes from an aspect
13436 -- specification the declaration is part of the freeze actions of the
13439 function Build_Spec return Node_Id;
13440 -- Used for declaration and renaming declaration, so that this is
13441 -- treated as a renaming_as_body.
13447 function Build_Spec return Node_Id is
13450 T_Ref : constant Node_Id := New_Occurrence_Of (Etyp, Loc);
13453 Subp_Id := Make_Defining_Identifier (Loc, Sname);
13457 Formals := New_List (
13458 Make_Parameter_Specification (Loc,
13459 Defining_Identifier =>
13460 Make_Defining_Identifier (Loc, Name_S),
13461 In_Present => True,
13462 Out_Present => True,
13464 New_Occurrence_Of (Etype (F), Loc)));
13468 Append_To (Formals,
13469 Make_Parameter_Specification (Loc,
13470 Defining_Identifier => Make_Defining_Identifier (Loc, Name_V),
13471 Parameter_Type => T_Ref));
13474 Make_Procedure_Specification (Loc,
13475 Defining_Unit_Name => Subp_Id,
13476 Parameter_Specifications => Formals);
13481 -- Start of processing for New_Put_Image_Subprogram
13484 F := First_Formal (Subp);
13486 Etyp := Etype (Next_Formal (F));
13488 -- Prepare subprogram declaration and insert it as an action on the
13489 -- clause node. The visibility for this entity is used to test for
13490 -- visibility of the attribute definition clause (in the sense of
13491 -- 8.3(23) as amended by AI-195).
13493 if not Defer_Declaration then
13495 Make_Subprogram_Declaration (Loc,
13496 Specification => Build_Spec);
13498 -- For a tagged type, there is always a visible declaration for the
13499 -- Put_Image TSS (it is a predefined primitive operation), and the
13500 -- completion of this declaration occurs at the freeze point, which is
13501 -- not always visible at places where the attribute definition clause is
13502 -- visible. So, we create a dummy entity here for the purpose of
13503 -- tracking the visibility of the attribute definition clause itself.
13507 Make_Defining_Identifier (Loc, New_External_Name (Sname, 'V'));
13509 Make_Object_Declaration (Loc,
13510 Defining_Identifier => Subp_Id,
13511 Object_Definition => New_Occurrence_Of (Standard_Boolean, Loc));
13514 if not Defer_Declaration
13515 and then From_Aspect_Specification (N)
13516 and then Has_Delayed_Freeze (Ent)
13518 Append_Freeze_Action (Ent, Subp_Decl);
13521 Insert_Action (N, Subp_Decl);
13522 Set_Entity (N, Subp_Id);
13526 Make_Subprogram_Renaming_Declaration (Loc,
13527 Specification => Build_Spec,
13528 Name => New_Occurrence_Of (Subp, Loc));
13530 if Defer_Declaration then
13531 Set_TSS (Base_Type (Ent), Subp_Id);
13534 if From_Aspect_Specification (N) then
13535 Append_Freeze_Action (Ent, Subp_Decl);
13537 Insert_Action (N, Subp_Decl);
13540 Copy_TSS (Subp_Id, Base_Type (Ent));
13542 end New_Put_Image_Subprogram;
13544 ---------------------------
13545 -- New_Stream_Subprogram --
13546 ---------------------------
13548 procedure New_Stream_Subprogram
13552 Nam : TSS_Name_Type)
13554 Loc : constant Source_Ptr := Sloc (N);
13555 Sname : constant Name_Id := Make_TSS_Name (Base_Type (Ent), Nam);
13556 Subp_Id : Entity_Id;
13557 Subp_Decl : Node_Id;
13561 Defer_Declaration : constant Boolean :=
13562 Is_Tagged_Type (Ent) or else Is_Private_Type (Ent);
13563 -- For a tagged type, there is a declaration for each stream attribute
13564 -- at the freeze point, and we must generate only a completion of this
13565 -- declaration. We do the same for private types, because the full view
13566 -- might be tagged. Otherwise we generate a declaration at the point of
13567 -- the attribute definition clause. If the attribute definition comes
13568 -- from an aspect specification the declaration is part of the freeze
13569 -- actions of the type.
13571 function Build_Spec return Node_Id;
13572 -- Used for declaration and renaming declaration, so that this is
13573 -- treated as a renaming_as_body.
13579 function Build_Spec return Node_Id is
13580 Out_P : constant Boolean := (Nam = TSS_Stream_Read);
13583 T_Ref : constant Node_Id := New_Occurrence_Of (Etyp, Loc);
13586 Subp_Id := Make_Defining_Identifier (Loc, Sname);
13588 -- S : access Root_Stream_Type'Class
13590 Formals := New_List (
13591 Make_Parameter_Specification (Loc,
13592 Defining_Identifier =>
13593 Make_Defining_Identifier (Loc, Name_S),
13595 Make_Access_Definition (Loc,
13597 New_Occurrence_Of (
13598 Designated_Type (Etype (F)), Loc))));
13600 if Nam = TSS_Stream_Input then
13602 Make_Function_Specification (Loc,
13603 Defining_Unit_Name => Subp_Id,
13604 Parameter_Specifications => Formals,
13605 Result_Definition => T_Ref);
13609 Append_To (Formals,
13610 Make_Parameter_Specification (Loc,
13611 Defining_Identifier => Make_Defining_Identifier (Loc, Name_V),
13612 Out_Present => Out_P,
13613 Parameter_Type => T_Ref));
13616 Make_Procedure_Specification (Loc,
13617 Defining_Unit_Name => Subp_Id,
13618 Parameter_Specifications => Formals);
13624 -- Start of processing for New_Stream_Subprogram
13627 F := First_Formal (Subp);
13629 if Ekind (Subp) = E_Procedure then
13630 Etyp := Etype (Next_Formal (F));
13632 Etyp := Etype (Subp);
13635 -- Prepare subprogram declaration and insert it as an action on the
13636 -- clause node. The visibility for this entity is used to test for
13637 -- visibility of the attribute definition clause (in the sense of
13638 -- 8.3(23) as amended by AI-195).
13640 if not Defer_Declaration then
13642 Make_Subprogram_Declaration (Loc,
13643 Specification => Build_Spec);
13645 -- For a tagged type, there is always a visible declaration for each
13646 -- stream TSS (it is a predefined primitive operation), and the
13647 -- completion of this declaration occurs at the freeze point, which is
13648 -- not always visible at places where the attribute definition clause is
13649 -- visible. So, we create a dummy entity here for the purpose of
13650 -- tracking the visibility of the attribute definition clause itself.
13654 Make_Defining_Identifier (Loc, New_External_Name (Sname, 'V'));
13656 Make_Object_Declaration (Loc,
13657 Defining_Identifier => Subp_Id,
13658 Object_Definition => New_Occurrence_Of (Standard_Boolean, Loc));
13661 if not Defer_Declaration
13662 and then From_Aspect_Specification (N)
13663 and then Has_Delayed_Freeze (Ent)
13665 Append_Freeze_Action (Ent, Subp_Decl);
13668 Insert_Action (N, Subp_Decl);
13669 Set_Entity (N, Subp_Id);
13673 Make_Subprogram_Renaming_Declaration (Loc,
13674 Specification => Build_Spec,
13675 Name => New_Occurrence_Of (Subp, Loc));
13677 if Defer_Declaration then
13678 Set_TSS (Base_Type (Ent), Subp_Id);
13681 if From_Aspect_Specification (N) then
13682 Append_Freeze_Action (Ent, Subp_Decl);
13684 Insert_Action (N, Subp_Decl);
13687 Copy_TSS (Subp_Id, Base_Type (Ent));
13689 end New_Stream_Subprogram;
13691 ----------------------
13692 -- No_Type_Rep_Item --
13693 ----------------------
13695 procedure No_Type_Rep_Item (N : Node_Id) is
13697 Error_Msg_N ("|type-related representation item not permitted!", N);
13698 end No_Type_Rep_Item;
13704 procedure Pop_Type (E : Entity_Id) is
13706 if Ekind (E) = E_Record_Type and then E = Current_Scope then
13710 and then Has_Discriminants (E)
13711 and then Nkind (Parent (E)) /= N_Subtype_Declaration
13713 Uninstall_Discriminants (E);
13722 procedure Push_Type (E : Entity_Id) is
13726 if Ekind (E) = E_Record_Type then
13729 Comp := First_Component (E);
13730 while Present (Comp) loop
13731 Install_Entity (Comp);
13732 Next_Component (Comp);
13735 if Has_Discriminants (E) then
13736 Install_Discriminants (E);
13740 and then Has_Discriminants (E)
13741 and then Nkind (Parent (E)) /= N_Subtype_Declaration
13744 Install_Discriminants (E);
13748 -----------------------------------
13749 -- Register_Address_Clause_Check --
13750 -----------------------------------
13752 procedure Register_Address_Clause_Check
13759 ACS : constant Boolean := Scope_Suppress.Suppress (Alignment_Check);
13761 Address_Clause_Checks.Append ((N, X, A, Y, Off, ACS));
13762 end Register_Address_Clause_Check;
13764 ------------------------
13765 -- Rep_Item_Too_Early --
13766 ------------------------
13768 function Rep_Item_Too_Early (T : Entity_Id; N : Node_Id) return Boolean is
13769 function Has_Generic_Parent (E : Entity_Id) return Boolean;
13770 -- Return True if R or any ancestor is a generic type
13772 ------------------------
13773 -- Has_Generic_Parent --
13774 ------------------------
13776 function Has_Generic_Parent (E : Entity_Id) return Boolean is
13777 Ancestor_Type : Entity_Id := Etype (E);
13780 if Is_Generic_Type (E) then
13784 while Present (Ancestor_Type)
13785 and then not Is_Generic_Type (Ancestor_Type)
13786 and then Etype (Ancestor_Type) /= Ancestor_Type
13788 Ancestor_Type := Etype (Ancestor_Type);
13792 Present (Ancestor_Type) and then Is_Generic_Type (Ancestor_Type);
13793 end Has_Generic_Parent;
13795 -- Start of processing for Rep_Item_Too_Early
13798 -- Cannot apply non-operational rep items to generic types
13800 if Is_Operational_Item (N) then
13804 and then Has_Generic_Parent (T)
13805 and then (Nkind (N) /= N_Pragma
13806 or else Get_Pragma_Id (N) /= Pragma_Convention)
13808 if Ada_Version < Ada_2020 then
13810 ("representation item not allowed for generic type", N);
13817 -- Otherwise check for incomplete type
13819 if Is_Incomplete_Or_Private_Type (T)
13820 and then No (Underlying_Type (T))
13822 (Nkind (N) /= N_Pragma
13823 or else Get_Pragma_Id (N) /= Pragma_Import)
13826 ("representation item must be after full type declaration", N);
13829 -- If the type has incomplete components, a representation clause is
13830 -- illegal but stream attributes and Convention pragmas are correct.
13832 elsif Has_Private_Component (T) then
13833 if Nkind (N) = N_Pragma then
13838 ("representation item must appear after type is fully defined",
13845 end Rep_Item_Too_Early;
13847 -----------------------
13848 -- Rep_Item_Too_Late --
13849 -----------------------
13851 function Rep_Item_Too_Late
13854 FOnly : Boolean := False) return Boolean
13856 procedure Too_Late;
13857 -- Output message for an aspect being specified too late
13859 -- Note that neither of the above errors is considered a serious one,
13860 -- since the effect is simply that we ignore the representation clause
13862 -- Is this really true? In any case if we make this change we must
13863 -- document the requirement in the spec of Rep_Item_Too_Late that
13864 -- if True is returned, then the rep item must be completely ignored???
13870 procedure Too_Late is
13872 -- Other compilers seem more relaxed about rep items appearing too
13873 -- late. Since analysis tools typically don't care about rep items
13874 -- anyway, no reason to be too strict about this.
13876 if not Relaxed_RM_Semantics then
13877 Error_Msg_N ("|representation item appears too late!", N);
13883 Parent_Type : Entity_Id;
13886 -- Start of processing for Rep_Item_Too_Late
13889 -- First make sure entity is not frozen (RM 13.1(9))
13893 -- Exclude imported types, which may be frozen if they appear in a
13894 -- representation clause for a local type.
13896 and then not From_Limited_With (T)
13898 -- Exclude generated entities (not coming from source). The common
13899 -- case is when we generate a renaming which prematurely freezes the
13900 -- renamed internal entity, but we still want to be able to set copies
13901 -- of attribute values such as Size/Alignment.
13903 and then Comes_From_Source (T)
13905 -- A self-referential aspect is illegal if it forces freezing the
13906 -- entity before the corresponding pragma has been analyzed.
13908 if Nkind_In (N, N_Attribute_Definition_Clause, N_Pragma)
13909 and then From_Aspect_Specification (N)
13912 ("aspect specification causes premature freezing of&", N, T);
13913 Set_Has_Delayed_Freeze (T, False);
13918 S := First_Subtype (T);
13920 if Present (Freeze_Node (S)) then
13921 if not Relaxed_RM_Semantics then
13923 ("??no more representation items for }", Freeze_Node (S), S);
13929 -- Check for case of untagged derived type whose parent either has
13930 -- primitive operations (pre Ada 202x), or is a by-reference type (RM
13931 -- 13.1(10)). In this case we do not output a Too_Late message, since
13932 -- there is no earlier point where the rep item could be placed to make
13934 -- ??? Confirming representation clauses should be allowed here.
13938 and then Is_Derived_Type (T)
13939 and then not Is_Tagged_Type (T)
13941 Parent_Type := Etype (Base_Type (T));
13943 if Relaxed_RM_Semantics then
13946 elsif Ada_Version <= Ada_2012
13947 and then Has_Primitive_Operations (Parent_Type)
13950 ("|representation item not permitted before Ada 202x!", N);
13952 ("\parent type & has primitive operations!", N, Parent_Type);
13955 elsif Is_By_Reference_Type (Parent_Type) then
13956 No_Type_Rep_Item (N);
13958 ("\parent type & is a by-reference type!", N, Parent_Type);
13963 -- No error, but one more warning to consider. The RM (surprisingly)
13964 -- allows this pattern in some cases:
13967 -- primitive operations for S
13968 -- type R is new S;
13969 -- rep clause for S
13971 -- Meaning that calls on the primitive operations of S for values of
13972 -- type R may require possibly expensive implicit conversion operations.
13973 -- So even when this is not an error, it is still worth a warning.
13975 if not Relaxed_RM_Semantics and then Is_Type (T) then
13977 DTL : constant Entity_Id := Derived_Type_Link (Base_Type (T));
13982 -- For now, do not generate this warning for the case of
13983 -- aspect specification using Ada 2012 syntax, since we get
13984 -- wrong messages we do not understand. The whole business
13985 -- of derived types and rep items seems a bit confused when
13986 -- aspects are used, since the aspects are not evaluated
13987 -- till freeze time. However, AI12-0109 confirms (in an AARM
13988 -- ramification) that inheritance in this case is required
13991 and then not From_Aspect_Specification (N)
13993 if Is_By_Reference_Type (T)
13994 and then not Is_Tagged_Type (T)
13995 and then Is_Type_Related_Rep_Item (N)
13996 and then (Ada_Version >= Ada_2012
13997 or else Has_Primitive_Operations (Base_Type (T)))
13999 -- Treat as hard error (AI12-0109, binding interpretation).
14000 -- Implementing a change of representation is not really
14001 -- an option in the case of a by-reference type, so we
14002 -- take this path for all Ada dialects if primitive
14003 -- operations are present.
14004 Error_Msg_Sloc := Sloc (DTL);
14006 ("representation item for& appears after derived type "
14007 & "declaration#", N);
14009 elsif Has_Primitive_Operations (Base_Type (T)) then
14010 Error_Msg_Sloc := Sloc (DTL);
14013 ("representation item for& appears after derived type "
14014 & "declaration#??", N);
14016 ("\may result in implicit conversions for primitive "
14017 & "operations of&??", N, T);
14019 ("\to change representations when called with arguments "
14020 & "of type&??", N, DTL);
14026 -- No error, link item into head of chain of rep items for the entity,
14027 -- but avoid chaining if we have an overloadable entity, and the pragma
14028 -- is one that can apply to multiple overloaded entities.
14030 if Is_Overloadable (T) and then Nkind (N) = N_Pragma then
14032 Pname : constant Name_Id := Pragma_Name (N);
14034 if Nam_In (Pname, Name_Convention, Name_Import, Name_Export,
14035 Name_External, Name_Interface)
14042 Record_Rep_Item (T, N);
14044 end Rep_Item_Too_Late;
14046 -------------------------------------
14047 -- Replace_Type_References_Generic --
14048 -------------------------------------
14050 procedure Replace_Type_References_Generic (N : Node_Id; T : Entity_Id) is
14051 TName : constant Name_Id := Chars (T);
14053 function Replace_Type_Ref (N : Node_Id) return Traverse_Result;
14054 -- Processes a single node in the traversal procedure below, checking
14055 -- if node N should be replaced, and if so, doing the replacement.
14057 function Visible_Component (Comp : Name_Id) return Entity_Id;
14058 -- Given an identifier in the expression, check whether there is a
14059 -- discriminant or component of the type that is directy visible, and
14060 -- rewrite it as the corresponding selected component of the formal of
14061 -- the subprogram. The entity is located by a sequential search, which
14062 -- seems acceptable given the typical size of component lists and check
14063 -- expressions. Possible optimization ???
14065 ----------------------
14066 -- Replace_Type_Ref --
14067 ----------------------
14069 function Replace_Type_Ref (N : Node_Id) return Traverse_Result is
14070 Loc : constant Source_Ptr := Sloc (N);
14072 procedure Add_Prefix (Ref : Node_Id; Comp : Entity_Id);
14073 -- Add the proper prefix to a reference to a component of the type
14074 -- when it is not already a selected component.
14080 procedure Add_Prefix (Ref : Node_Id; Comp : Entity_Id) is
14083 Make_Selected_Component (Loc,
14084 Prefix => New_Occurrence_Of (T, Loc),
14085 Selector_Name => New_Occurrence_Of (Comp, Loc)));
14086 Replace_Type_Reference (Prefix (Ref));
14095 -- Start of processing for Replace_Type_Ref
14098 if Nkind (N) = N_Identifier then
14100 -- If not the type name, check whether it is a reference to some
14101 -- other type, which must be frozen before the predicate function
14102 -- is analyzed, i.e. before the freeze node of the type to which
14103 -- the predicate applies.
14105 if Chars (N) /= TName then
14106 if Present (Current_Entity (N))
14107 and then Is_Type (Current_Entity (N))
14109 Freeze_Before (Freeze_Node (T), Current_Entity (N));
14112 -- The components of the type are directly visible and can
14113 -- be referenced without a prefix.
14115 if Nkind (Parent (N)) = N_Selected_Component then
14118 -- In expression C (I), C may be a directly visible function
14119 -- or a visible component that has an array type. Disambiguate
14120 -- by examining the component type.
14122 elsif Nkind (Parent (N)) = N_Indexed_Component
14123 and then N = Prefix (Parent (N))
14125 Comp := Visible_Component (Chars (N));
14127 if Present (Comp) and then Is_Array_Type (Etype (Comp)) then
14128 Add_Prefix (N, Comp);
14132 Comp := Visible_Component (Chars (N));
14134 if Present (Comp) then
14135 Add_Prefix (N, Comp);
14141 -- Otherwise do the replacement if this is not a qualified
14142 -- reference to a homograph of the type itself. Note that the
14143 -- current instance could not appear in such a context, e.g.
14144 -- the prefix of a type conversion.
14147 if Nkind (Parent (N)) /= N_Selected_Component
14148 or else N /= Selector_Name (Parent (N))
14150 Replace_Type_Reference (N);
14156 -- Case of selected component, which may be a subcomponent of the
14157 -- current instance, or an expanded name which is still unanalyzed.
14159 elsif Nkind (N) = N_Selected_Component then
14161 -- If selector name is not our type, keep going (we might still
14162 -- have an occurrence of the type in the prefix). If it is a
14163 -- subcomponent of the current entity, add prefix.
14165 if Nkind (Selector_Name (N)) /= N_Identifier
14166 or else Chars (Selector_Name (N)) /= TName
14168 if Nkind (Prefix (N)) = N_Identifier then
14169 Comp := Visible_Component (Chars (Prefix (N)));
14171 if Present (Comp) then
14172 Add_Prefix (Prefix (N), Comp);
14178 -- Selector name is our type, check qualification
14181 -- Loop through scopes and prefixes, doing comparison
14183 Scop := Current_Scope;
14184 Pref := Prefix (N);
14186 -- Continue if no more scopes or scope with no name
14188 if No (Scop) or else Nkind (Scop) not in N_Has_Chars then
14192 -- Do replace if prefix is an identifier matching the scope
14193 -- that we are currently looking at.
14195 if Nkind (Pref) = N_Identifier
14196 and then Chars (Pref) = Chars (Scop)
14198 Replace_Type_Reference (N);
14202 -- Go check scope above us if prefix is itself of the form
14203 -- of a selected component, whose selector matches the scope
14204 -- we are currently looking at.
14206 if Nkind (Pref) = N_Selected_Component
14207 and then Nkind (Selector_Name (Pref)) = N_Identifier
14208 and then Chars (Selector_Name (Pref)) = Chars (Scop)
14210 Scop := Scope (Scop);
14211 Pref := Prefix (Pref);
14213 -- For anything else, we don't have a match, so keep on
14214 -- going, there are still some weird cases where we may
14215 -- still have a replacement within the prefix.
14223 -- Continue for any other node kind
14228 end Replace_Type_Ref;
14230 procedure Replace_Type_Refs is new Traverse_Proc (Replace_Type_Ref);
14232 -----------------------
14233 -- Visible_Component --
14234 -----------------------
14236 function Visible_Component (Comp : Name_Id) return Entity_Id is
14240 -- Types with nameable components are records and discriminated
14243 if Ekind (T) = E_Record_Type
14244 or else (Is_Private_Type (T) and then Has_Discriminants (T))
14246 E := First_Entity (T);
14247 while Present (E) loop
14248 if Comes_From_Source (E) and then Chars (E) = Comp then
14256 -- Nothing by that name, or the type has no components
14259 end Visible_Component;
14261 -- Start of processing for Replace_Type_References_Generic
14264 Replace_Type_Refs (N);
14265 end Replace_Type_References_Generic;
14267 --------------------------------
14268 -- Resolve_Aspect_Expressions --
14269 --------------------------------
14271 procedure Resolve_Aspect_Expressions (E : Entity_Id) is
14272 function Resolve_Name (N : Node_Id) return Traverse_Result;
14273 -- Verify that all identifiers in the expression, with the exception
14274 -- of references to the current entity, denote visible entities. This
14275 -- is done only to detect visibility errors, as the expression will be
14276 -- properly analyzed/expanded during analysis of the predicate function
14277 -- body. We omit quantified expressions from this test, given that they
14278 -- introduce a local identifier that would require proper expansion to
14279 -- handle properly.
14285 function Resolve_Name (N : Node_Id) return Traverse_Result is
14286 Dummy : Traverse_Result;
14289 if Nkind (N) = N_Selected_Component then
14290 if Nkind (Prefix (N)) = N_Identifier
14291 and then Chars (Prefix (N)) /= Chars (E)
14293 Find_Selected_Component (N);
14298 -- Resolve identifiers that are not selectors in parameter
14299 -- associations (these are never resolved by visibility).
14301 elsif Nkind (N) = N_Identifier
14302 and then Chars (N) /= Chars (E)
14303 and then (Nkind (Parent (N)) /= N_Parameter_Association
14304 or else N /= Selector_Name (Parent (N)))
14306 Find_Direct_Name (N);
14307 Set_Entity (N, Empty);
14309 -- The name is component association needs no resolution.
14311 elsif Nkind (N) = N_Component_Association then
14312 Dummy := Resolve_Name (Expression (N));
14315 elsif Nkind (N) = N_Quantified_Expression then
14322 procedure Resolve_Aspect_Expression is new Traverse_Proc (Resolve_Name);
14326 ASN : Node_Id := First_Rep_Item (E);
14328 -- Start of processing for Resolve_Aspect_Expressions
14335 while Present (ASN) loop
14336 if Nkind (ASN) = N_Aspect_Specification and then Entity (ASN) = E then
14338 A_Id : constant Aspect_Id := Get_Aspect_Id (ASN);
14339 Expr : constant Node_Id := Expression (ASN);
14344 -- For now we only deal with aspects that do not generate
14345 -- subprograms, or that may mention current instances of
14346 -- types. These will require special handling (???TBD).
14348 when Aspect_Invariant
14350 | Aspect_Predicate_Failure
14354 when Aspect_Dynamic_Predicate
14355 | Aspect_Static_Predicate
14357 -- Build predicate function specification and preanalyze
14358 -- expression after type replacement. The function
14359 -- declaration must be analyzed in the scope of the type,
14360 -- but the expression can reference components and
14361 -- discriminants of the type.
14363 if No (Predicate_Function (E)) then
14365 FDecl : constant Node_Id :=
14366 Build_Predicate_Function_Declaration (E);
14367 pragma Unreferenced (FDecl);
14371 Resolve_Aspect_Expression (Expr);
14376 when Pre_Post_Aspects =>
14379 when Aspect_Iterable =>
14380 if Nkind (Expr) = N_Aggregate then
14385 Assoc := First (Component_Associations (Expr));
14386 while Present (Assoc) loop
14387 Find_Direct_Name (Expression (Assoc));
14393 -- The expression for Default_Value is a static expression
14394 -- of the type, but this expression does not freeze the
14395 -- type, so it can still appear in a representation clause
14396 -- before the actual freeze point.
14398 when Aspect_Default_Value =>
14399 Set_Must_Not_Freeze (Expr);
14400 Preanalyze_Spec_Expression (Expr, E);
14402 when Aspect_Priority =>
14404 Preanalyze_Spec_Expression (Expr, Any_Integer);
14407 -- Ditto for Storage_Size. Any other aspects that carry
14408 -- expressions that should not freeze ??? This is only
14409 -- relevant to the misuse of deferred constants.
14411 when Aspect_Storage_Size =>
14412 Set_Must_Not_Freeze (Expr);
14413 Preanalyze_Spec_Expression (Expr, Any_Integer);
14416 if Present (Expr) then
14417 case Aspect_Argument (A_Id) is
14419 | Optional_Expression
14421 Analyze_And_Resolve (Expr);
14426 if Nkind (Expr) = N_Identifier then
14427 Find_Direct_Name (Expr);
14429 elsif Nkind (Expr) = N_Selected_Component then
14430 Find_Selected_Component (Expr);
14438 Next_Rep_Item (ASN);
14440 end Resolve_Aspect_Expressions;
14442 -------------------------
14443 -- Same_Representation --
14444 -------------------------
14446 function Same_Representation (Typ1, Typ2 : Entity_Id) return Boolean is
14447 T1 : constant Entity_Id := Underlying_Type (Typ1);
14448 T2 : constant Entity_Id := Underlying_Type (Typ2);
14451 -- A quick check, if base types are the same, then we definitely have
14452 -- the same representation, because the subtype specific representation
14453 -- attributes (Size and Alignment) do not affect representation from
14454 -- the point of view of this test.
14456 if Base_Type (T1) = Base_Type (T2) then
14459 elsif Is_Private_Type (Base_Type (T2))
14460 and then Base_Type (T1) = Full_View (Base_Type (T2))
14464 -- If T2 is a generic actual it is declared as a subtype, so
14465 -- check against its base type.
14467 elsif Is_Generic_Actual_Type (T1)
14468 and then Same_Representation (Base_Type (T1), T2)
14473 -- Tagged types always have the same representation, because it is not
14474 -- possible to specify different representations for common fields.
14476 if Is_Tagged_Type (T1) then
14480 -- Representations are definitely different if conventions differ
14482 if Convention (T1) /= Convention (T2) then
14486 -- Representations are different if component alignments or scalar
14487 -- storage orders differ.
14489 if (Is_Record_Type (T1) or else Is_Array_Type (T1))
14491 (Is_Record_Type (T2) or else Is_Array_Type (T2))
14493 (Component_Alignment (T1) /= Component_Alignment (T2)
14494 or else Reverse_Storage_Order (T1) /= Reverse_Storage_Order (T2))
14499 -- For arrays, the only real issue is component size. If we know the
14500 -- component size for both arrays, and it is the same, then that's
14501 -- good enough to know we don't have a change of representation.
14503 if Is_Array_Type (T1) then
14504 if Known_Component_Size (T1)
14505 and then Known_Component_Size (T2)
14506 and then Component_Size (T1) = Component_Size (T2)
14512 -- For records, representations are different if reorderings differ
14514 if Is_Record_Type (T1)
14515 and then Is_Record_Type (T2)
14516 and then No_Reordering (T1) /= No_Reordering (T2)
14521 -- Types definitely have same representation if neither has non-standard
14522 -- representation since default representations are always consistent.
14523 -- If only one has non-standard representation, and the other does not,
14524 -- then we consider that they do not have the same representation. They
14525 -- might, but there is no way of telling early enough.
14527 if Has_Non_Standard_Rep (T1) then
14528 if not Has_Non_Standard_Rep (T2) then
14532 return not Has_Non_Standard_Rep (T2);
14535 -- Here the two types both have non-standard representation, and we need
14536 -- to determine if they have the same non-standard representation.
14538 -- For arrays, we simply need to test if the component sizes are the
14539 -- same. Pragma Pack is reflected in modified component sizes, so this
14540 -- check also deals with pragma Pack.
14542 if Is_Array_Type (T1) then
14543 return Component_Size (T1) = Component_Size (T2);
14545 -- Case of record types
14547 elsif Is_Record_Type (T1) then
14549 -- Packed status must conform
14551 if Is_Packed (T1) /= Is_Packed (T2) then
14554 -- Otherwise we must check components. Typ2 maybe a constrained
14555 -- subtype with fewer components, so we compare the components
14556 -- of the base types.
14559 Record_Case : declare
14560 CD1, CD2 : Entity_Id;
14562 function Same_Rep return Boolean;
14563 -- CD1 and CD2 are either components or discriminants. This
14564 -- function tests whether they have the same representation.
14570 function Same_Rep return Boolean is
14572 if No (Component_Clause (CD1)) then
14573 return No (Component_Clause (CD2));
14575 -- Note: at this point, component clauses have been
14576 -- normalized to the default bit order, so that the
14577 -- comparison of Component_Bit_Offsets is meaningful.
14580 Present (Component_Clause (CD2))
14582 Component_Bit_Offset (CD1) = Component_Bit_Offset (CD2)
14584 Esize (CD1) = Esize (CD2);
14588 -- Start of processing for Record_Case
14591 if Has_Discriminants (T1) then
14593 -- The number of discriminants may be different if the
14594 -- derived type has fewer (constrained by values). The
14595 -- invisible discriminants retain the representation of
14596 -- the original, so the discrepancy does not per se
14597 -- indicate a different representation.
14599 CD1 := First_Discriminant (T1);
14600 CD2 := First_Discriminant (T2);
14601 while Present (CD1) and then Present (CD2) loop
14602 if not Same_Rep then
14605 Next_Discriminant (CD1);
14606 Next_Discriminant (CD2);
14611 CD1 := First_Component (Underlying_Type (Base_Type (T1)));
14612 CD2 := First_Component (Underlying_Type (Base_Type (T2)));
14613 while Present (CD1) loop
14614 if not Same_Rep then
14617 Next_Component (CD1);
14618 Next_Component (CD2);
14626 -- For enumeration types, we must check each literal to see if the
14627 -- representation is the same. Note that we do not permit enumeration
14628 -- representation clauses for Character and Wide_Character, so these
14629 -- cases were already dealt with.
14631 elsif Is_Enumeration_Type (T1) then
14632 Enumeration_Case : declare
14633 L1, L2 : Entity_Id;
14636 L1 := First_Literal (T1);
14637 L2 := First_Literal (T2);
14638 while Present (L1) loop
14639 if Enumeration_Rep (L1) /= Enumeration_Rep (L2) then
14648 end Enumeration_Case;
14650 -- Any other types have the same representation for these purposes
14655 end Same_Representation;
14657 --------------------------------
14658 -- Resolve_Iterable_Operation --
14659 --------------------------------
14661 procedure Resolve_Iterable_Operation
14663 Cursor : Entity_Id;
14672 if not Is_Overloaded (N) then
14673 if not Is_Entity_Name (N)
14674 or else Ekind (Entity (N)) /= E_Function
14675 or else Scope (Entity (N)) /= Scope (Typ)
14676 or else No (First_Formal (Entity (N)))
14677 or else Etype (First_Formal (Entity (N))) /= Typ
14680 ("iterable primitive must be local function name whose first "
14681 & "formal is an iterable type", N);
14686 F1 := First_Formal (Ent);
14688 if Nam = Name_First or else Nam = Name_Last then
14690 -- First or Last (Container) => Cursor
14692 if Etype (Ent) /= Cursor then
14693 Error_Msg_N ("primitive for First must yield a curosr", N);
14696 elsif Nam = Name_Next then
14698 -- Next (Container, Cursor) => Cursor
14700 F2 := Next_Formal (F1);
14702 if Etype (F2) /= Cursor
14703 or else Etype (Ent) /= Cursor
14704 or else Present (Next_Formal (F2))
14706 Error_Msg_N ("no match for Next iterable primitive", N);
14709 elsif Nam = Name_Previous then
14711 -- Previous (Container, Cursor) => Cursor
14713 F2 := Next_Formal (F1);
14715 if Etype (F2) /= Cursor
14716 or else Etype (Ent) /= Cursor
14717 or else Present (Next_Formal (F2))
14719 Error_Msg_N ("no match for Previous iterable primitive", N);
14722 elsif Nam = Name_Has_Element then
14724 -- Has_Element (Container, Cursor) => Boolean
14726 F2 := Next_Formal (F1);
14728 if Etype (F2) /= Cursor
14729 or else Etype (Ent) /= Standard_Boolean
14730 or else Present (Next_Formal (F2))
14732 Error_Msg_N ("no match for Has_Element iterable primitive", N);
14735 elsif Nam = Name_Element then
14736 F2 := Next_Formal (F1);
14739 or else Etype (F2) /= Cursor
14740 or else Present (Next_Formal (F2))
14742 Error_Msg_N ("no match for Element iterable primitive", N);
14746 raise Program_Error;
14750 -- Overloaded case: find subprogram with proper signature. Caller
14751 -- will report error if no match is found.
14758 Get_First_Interp (N, I, It);
14759 while Present (It.Typ) loop
14760 if Ekind (It.Nam) = E_Function
14761 and then Scope (It.Nam) = Scope (Typ)
14762 and then Etype (First_Formal (It.Nam)) = Typ
14764 F1 := First_Formal (It.Nam);
14766 if Nam = Name_First then
14767 if Etype (It.Nam) = Cursor
14768 and then No (Next_Formal (F1))
14770 Set_Entity (N, It.Nam);
14774 elsif Nam = Name_Next then
14775 F2 := Next_Formal (F1);
14778 and then No (Next_Formal (F2))
14779 and then Etype (F2) = Cursor
14780 and then Etype (It.Nam) = Cursor
14782 Set_Entity (N, It.Nam);
14786 elsif Nam = Name_Has_Element then
14787 F2 := Next_Formal (F1);
14790 and then No (Next_Formal (F2))
14791 and then Etype (F2) = Cursor
14792 and then Etype (It.Nam) = Standard_Boolean
14794 Set_Entity (N, It.Nam);
14795 F2 := Next_Formal (F1);
14799 elsif Nam = Name_Element then
14800 F2 := Next_Formal (F1);
14803 and then No (Next_Formal (F2))
14804 and then Etype (F2) = Cursor
14806 Set_Entity (N, It.Nam);
14812 Get_Next_Interp (I, It);
14816 end Resolve_Iterable_Operation;
14822 procedure Set_Biased
14826 Biased : Boolean := True)
14830 Set_Has_Biased_Representation (E);
14832 if Warn_On_Biased_Representation then
14834 ("?B?" & Msg & " forces biased representation for&", N, E);
14839 --------------------
14840 -- Set_Enum_Esize --
14841 --------------------
14843 procedure Set_Enum_Esize (T : Entity_Id) is
14849 Init_Alignment (T);
14851 -- Find the minimum standard size (8,16,32,64) that fits
14853 Lo := Enumeration_Rep (Entity (Type_Low_Bound (T)));
14854 Hi := Enumeration_Rep (Entity (Type_High_Bound (T)));
14857 if Lo >= -Uint_2**07 and then Hi < Uint_2**07 then
14858 Sz := Standard_Character_Size; -- May be > 8 on some targets
14860 elsif Lo >= -Uint_2**15 and then Hi < Uint_2**15 then
14863 elsif Lo >= -Uint_2**31 and then Hi < Uint_2**31 then
14866 else pragma Assert (Lo >= -Uint_2**63 and then Hi < Uint_2**63);
14871 if Hi < Uint_2**08 then
14872 Sz := Standard_Character_Size; -- May be > 8 on some targets
14874 elsif Hi < Uint_2**16 then
14877 elsif Hi < Uint_2**32 then
14880 else pragma Assert (Hi < Uint_2**63);
14885 -- That minimum is the proper size unless we have a foreign convention
14886 -- and the size required is 32 or less, in which case we bump the size
14887 -- up to 32. This is required for C and C++ and seems reasonable for
14888 -- all other foreign conventions.
14890 if Has_Foreign_Convention (T)
14891 and then Esize (T) < Standard_Integer_Size
14893 -- Don't do this if Short_Enums on target
14895 and then not Target_Short_Enums
14897 Init_Esize (T, Standard_Integer_Size);
14899 Init_Esize (T, Sz);
14901 end Set_Enum_Esize;
14903 -----------------------------
14904 -- Uninstall_Discriminants --
14905 -----------------------------
14907 procedure Uninstall_Discriminants (E : Entity_Id) is
14913 -- Discriminants have been made visible for type declarations and
14914 -- protected type declarations, not for subtype declarations.
14916 if Nkind (Parent (E)) /= N_Subtype_Declaration then
14917 Disc := First_Discriminant (E);
14918 while Present (Disc) loop
14919 if Disc /= Current_Entity (Disc) then
14920 Prev := Current_Entity (Disc);
14921 while Present (Prev)
14922 and then Present (Homonym (Prev))
14923 and then Homonym (Prev) /= Disc
14925 Prev := Homonym (Prev);
14931 Set_Is_Immediately_Visible (Disc, False);
14933 Outer := Homonym (Disc);
14934 while Present (Outer) and then Scope (Outer) = E loop
14935 Outer := Homonym (Outer);
14938 -- Reset homonym link of other entities, but do not modify link
14939 -- between entities in current scope, so that the back end can
14940 -- have a proper count of local overloadings.
14943 Set_Name_Entity_Id (Chars (Disc), Outer);
14945 elsif Scope (Prev) /= Scope (Disc) then
14946 Set_Homonym (Prev, Outer);
14949 Next_Discriminant (Disc);
14952 end Uninstall_Discriminants;
14954 ------------------------------
14955 -- Validate_Address_Clauses --
14956 ------------------------------
14958 procedure Validate_Address_Clauses is
14959 function Offset_Value (Expr : Node_Id) return Uint;
14960 -- Given an Address attribute reference, return the value in bits of its
14961 -- offset from the first bit of the underlying entity, or 0 if it is not
14962 -- known at compile time.
14968 function Offset_Value (Expr : Node_Id) return Uint is
14969 N : Node_Id := Prefix (Expr);
14971 Val : Uint := Uint_0;
14974 -- Climb the prefix chain and compute the cumulative offset
14977 if Is_Entity_Name (N) then
14980 elsif Nkind (N) = N_Selected_Component then
14981 Off := Component_Bit_Offset (Entity (Selector_Name (N)));
14982 if Off /= No_Uint and then Off >= Uint_0 then
14989 elsif Nkind (N) = N_Indexed_Component then
14990 Off := Indexed_Component_Bit_Offset (N);
14991 if Off /= No_Uint then
15004 -- Start of processing for Validate_Address_Clauses
15007 for J in Address_Clause_Checks.First .. Address_Clause_Checks.Last loop
15009 ACCR : Address_Clause_Check_Record
15010 renames Address_Clause_Checks.Table (J);
15014 X_Alignment : Uint;
15015 Y_Alignment : Uint := Uint_0;
15018 Y_Size : Uint := Uint_0;
15023 -- Skip processing of this entry if warning already posted
15025 if not Address_Warning_Posted (ACCR.N) then
15026 Expr := Original_Node (Expression (ACCR.N));
15028 -- Get alignments, sizes and offset, if any
15030 X_Alignment := Alignment (ACCR.X);
15031 X_Size := Esize (ACCR.X);
15033 if Present (ACCR.Y) then
15034 Y_Alignment := Alignment (ACCR.Y);
15035 Y_Size := Esize (ACCR.Y);
15039 and then Nkind (Expr) = N_Attribute_Reference
15040 and then Attribute_Name (Expr) = Name_Address
15042 X_Offs := Offset_Value (Expr);
15047 -- Check for known value not multiple of alignment
15049 if No (ACCR.Y) then
15050 if not Alignment_Checks_Suppressed (ACCR)
15051 and then X_Alignment /= 0
15052 and then ACCR.A mod X_Alignment /= 0
15055 ("??specified address for& is inconsistent with "
15056 & "alignment", ACCR.N, ACCR.X);
15058 ("\??program execution may be erroneous (RM 13.3(27))",
15061 Error_Msg_Uint_1 := X_Alignment;
15062 Error_Msg_NE ("\??alignment of & is ^", ACCR.N, ACCR.X);
15065 -- Check for large object overlaying smaller one
15067 elsif Y_Size > Uint_0
15068 and then X_Size > Uint_0
15069 and then X_Offs + X_Size > Y_Size
15071 Error_Msg_NE ("??& overlays smaller object", ACCR.N, ACCR.X);
15073 ("\??program execution may be erroneous", ACCR.N);
15075 Error_Msg_Uint_1 := X_Size;
15076 Error_Msg_NE ("\??size of & is ^", ACCR.N, ACCR.X);
15078 Error_Msg_Uint_1 := Y_Size;
15079 Error_Msg_NE ("\??size of & is ^", ACCR.N, ACCR.Y);
15081 if Y_Size >= X_Size then
15082 Error_Msg_Uint_1 := X_Offs;
15083 Error_Msg_NE ("\??but offset of & is ^", ACCR.N, ACCR.X);
15086 -- Check for inadequate alignment, both of the base object
15087 -- and of the offset, if any. We only do this check if the
15088 -- run-time Alignment_Check is active. No point in warning
15089 -- if this check has been suppressed (or is suppressed by
15090 -- default in the non-strict alignment machine case).
15092 -- Note: we do not check the alignment if we gave a size
15093 -- warning, since it would likely be redundant.
15095 elsif not Alignment_Checks_Suppressed (ACCR)
15096 and then Y_Alignment /= Uint_0
15098 (Y_Alignment < X_Alignment
15101 and then Nkind (Expr) = N_Attribute_Reference
15102 and then Attribute_Name (Expr) = Name_Address
15103 and then Has_Compatible_Alignment
15104 (ACCR.X, Prefix (Expr), True) /=
15108 ("??specified address for& may be inconsistent with "
15109 & "alignment", ACCR.N, ACCR.X);
15111 ("\??program execution may be erroneous (RM 13.3(27))",
15114 Error_Msg_Uint_1 := X_Alignment;
15115 Error_Msg_NE ("\??alignment of & is ^", ACCR.N, ACCR.X);
15117 Error_Msg_Uint_1 := Y_Alignment;
15118 Error_Msg_NE ("\??alignment of & is ^", ACCR.N, ACCR.Y);
15120 if Y_Alignment >= X_Alignment then
15122 ("\??but offset is not multiple of alignment", ACCR.N);
15128 end Validate_Address_Clauses;
15130 ---------------------------
15131 -- Validate_Independence --
15132 ---------------------------
15134 procedure Validate_Independence is
15135 SU : constant Uint := UI_From_Int (System_Storage_Unit);
15143 procedure Check_Array_Type (Atyp : Entity_Id);
15144 -- Checks if the array type Atyp has independent components, and
15145 -- if not, outputs an appropriate set of error messages.
15147 procedure No_Independence;
15148 -- Output message that independence cannot be guaranteed
15150 function OK_Component (C : Entity_Id) return Boolean;
15151 -- Checks one component to see if it is independently accessible, and
15152 -- if so yields True, otherwise yields False if independent access
15153 -- cannot be guaranteed. This is a conservative routine, it only
15154 -- returns True if it knows for sure, it returns False if it knows
15155 -- there is a problem, or it cannot be sure there is no problem.
15157 procedure Reason_Bad_Component (C : Entity_Id);
15158 -- Outputs continuation message if a reason can be determined for
15159 -- the component C being bad.
15161 ----------------------
15162 -- Check_Array_Type --
15163 ----------------------
15165 procedure Check_Array_Type (Atyp : Entity_Id) is
15166 Ctyp : constant Entity_Id := Component_Type (Atyp);
15169 -- OK if no alignment clause, no pack, and no component size
15171 if not Has_Component_Size_Clause (Atyp)
15172 and then not Has_Alignment_Clause (Atyp)
15173 and then not Is_Packed (Atyp)
15178 -- Case of component size is greater than or equal to 64 and the
15179 -- alignment of the array is at least as large as the alignment
15180 -- of the component. We are definitely OK in this situation.
15182 if Known_Component_Size (Atyp)
15183 and then Component_Size (Atyp) >= 64
15184 and then Known_Alignment (Atyp)
15185 and then Known_Alignment (Ctyp)
15186 and then Alignment (Atyp) >= Alignment (Ctyp)
15191 -- Check actual component size
15193 if not Known_Component_Size (Atyp)
15194 or else not (Addressable (Component_Size (Atyp))
15195 and then Component_Size (Atyp) < 64)
15196 or else Component_Size (Atyp) mod Esize (Ctyp) /= 0
15200 -- Bad component size, check reason
15202 if Has_Component_Size_Clause (Atyp) then
15203 P := Get_Attribute_Definition_Clause
15204 (Atyp, Attribute_Component_Size);
15206 if Present (P) then
15207 Error_Msg_Sloc := Sloc (P);
15208 Error_Msg_N ("\because of Component_Size clause#", N);
15213 if Is_Packed (Atyp) then
15214 P := Get_Rep_Pragma (Atyp, Name_Pack);
15216 if Present (P) then
15217 Error_Msg_Sloc := Sloc (P);
15218 Error_Msg_N ("\because of pragma Pack#", N);
15223 -- No reason found, just return
15228 -- Array type is OK independence-wise
15231 end Check_Array_Type;
15233 ---------------------
15234 -- No_Independence --
15235 ---------------------
15237 procedure No_Independence is
15239 if Pragma_Name (N) = Name_Independent then
15240 Error_Msg_NE ("independence cannot be guaranteed for&", N, E);
15243 ("independent components cannot be guaranteed for&", N, E);
15245 end No_Independence;
15251 function OK_Component (C : Entity_Id) return Boolean is
15252 Rec : constant Entity_Id := Scope (C);
15253 Ctyp : constant Entity_Id := Etype (C);
15256 -- OK if no component clause, no Pack, and no alignment clause
15258 if No (Component_Clause (C))
15259 and then not Is_Packed (Rec)
15260 and then not Has_Alignment_Clause (Rec)
15265 -- Here we look at the actual component layout. A component is
15266 -- addressable if its size is a multiple of the Esize of the
15267 -- component type, and its starting position in the record has
15268 -- appropriate alignment, and the record itself has appropriate
15269 -- alignment to guarantee the component alignment.
15271 -- Make sure sizes are static, always assume the worst for any
15272 -- cases where we cannot check static values.
15274 if not (Known_Static_Esize (C)
15276 Known_Static_Esize (Ctyp))
15281 -- Size of component must be addressable or greater than 64 bits
15282 -- and a multiple of bytes.
15284 if not Addressable (Esize (C)) and then Esize (C) < Uint_64 then
15288 -- Check size is proper multiple
15290 if Esize (C) mod Esize (Ctyp) /= 0 then
15294 -- Check alignment of component is OK
15296 if not Known_Component_Bit_Offset (C)
15297 or else Component_Bit_Offset (C) < Uint_0
15298 or else Component_Bit_Offset (C) mod Esize (Ctyp) /= 0
15303 -- Check alignment of record type is OK
15305 if not Known_Alignment (Rec)
15306 or else (Alignment (Rec) * SU) mod Esize (Ctyp) /= 0
15311 -- All tests passed, component is addressable
15316 --------------------------
15317 -- Reason_Bad_Component --
15318 --------------------------
15320 procedure Reason_Bad_Component (C : Entity_Id) is
15321 Rec : constant Entity_Id := Scope (C);
15322 Ctyp : constant Entity_Id := Etype (C);
15325 -- If component clause present assume that's the problem
15327 if Present (Component_Clause (C)) then
15328 Error_Msg_Sloc := Sloc (Component_Clause (C));
15329 Error_Msg_N ("\because of Component_Clause#", N);
15333 -- If pragma Pack clause present, assume that's the problem
15335 if Is_Packed (Rec) then
15336 P := Get_Rep_Pragma (Rec, Name_Pack);
15338 if Present (P) then
15339 Error_Msg_Sloc := Sloc (P);
15340 Error_Msg_N ("\because of pragma Pack#", N);
15345 -- See if record has bad alignment clause
15347 if Has_Alignment_Clause (Rec)
15348 and then Known_Alignment (Rec)
15349 and then (Alignment (Rec) * SU) mod Esize (Ctyp) /= 0
15351 P := Get_Attribute_Definition_Clause (Rec, Attribute_Alignment);
15353 if Present (P) then
15354 Error_Msg_Sloc := Sloc (P);
15355 Error_Msg_N ("\because of Alignment clause#", N);
15359 -- Couldn't find a reason, so return without a message
15362 end Reason_Bad_Component;
15364 -- Start of processing for Validate_Independence
15367 for J in Independence_Checks.First .. Independence_Checks.Last loop
15368 N := Independence_Checks.Table (J).N;
15369 E := Independence_Checks.Table (J).E;
15370 IC := Pragma_Name (N) = Name_Independent_Components;
15372 -- Deal with component case
15374 if Ekind (E) = E_Discriminant or else Ekind (E) = E_Component then
15375 if not OK_Component (E) then
15377 Reason_Bad_Component (E);
15382 -- Deal with record with Independent_Components
15384 if IC and then Is_Record_Type (E) then
15385 Comp := First_Component_Or_Discriminant (E);
15386 while Present (Comp) loop
15387 if not OK_Component (Comp) then
15389 Reason_Bad_Component (Comp);
15393 Next_Component_Or_Discriminant (Comp);
15397 -- Deal with address clause case
15399 if Is_Object (E) then
15400 Addr := Address_Clause (E);
15402 if Present (Addr) then
15404 Error_Msg_Sloc := Sloc (Addr);
15405 Error_Msg_N ("\because of Address clause#", N);
15410 -- Deal with independent components for array type
15412 if IC and then Is_Array_Type (E) then
15413 Check_Array_Type (E);
15416 -- Deal with independent components for array object
15418 if IC and then Is_Object (E) and then Is_Array_Type (Etype (E)) then
15419 Check_Array_Type (Etype (E));
15424 end Validate_Independence;
15426 ------------------------------
15427 -- Validate_Iterable_Aspect --
15428 ------------------------------
15430 procedure Validate_Iterable_Aspect (Typ : Entity_Id; ASN : Node_Id) is
15435 Cursor : constant Entity_Id := Get_Cursor_Type (ASN, Typ);
15437 First_Id : Entity_Id;
15438 Last_Id : Entity_Id;
15439 Next_Id : Entity_Id;
15440 Has_Element_Id : Entity_Id;
15441 Element_Id : Entity_Id;
15444 -- If previous error aspect is unusable
15446 if Cursor = Any_Type then
15453 Has_Element_Id := Empty;
15454 Element_Id := Empty;
15456 -- Each expression must resolve to a function with the proper signature
15458 Assoc := First (Component_Associations (Expression (ASN)));
15459 while Present (Assoc) loop
15460 Expr := Expression (Assoc);
15463 Prim := First (Choices (Assoc));
15465 if Nkind (Prim) /= N_Identifier or else Present (Next (Prim)) then
15466 Error_Msg_N ("illegal name in association", Prim);
15468 elsif Chars (Prim) = Name_First then
15469 Resolve_Iterable_Operation (Expr, Cursor, Typ, Name_First);
15470 First_Id := Entity (Expr);
15472 elsif Chars (Prim) = Name_Last then
15473 Resolve_Iterable_Operation (Expr, Cursor, Typ, Name_Last);
15474 Last_Id := Entity (Expr);
15476 elsif Chars (Prim) = Name_Previous then
15477 Resolve_Iterable_Operation (Expr, Cursor, Typ, Name_Previous);
15478 Last_Id := Entity (Expr);
15480 elsif Chars (Prim) = Name_Next then
15481 Resolve_Iterable_Operation (Expr, Cursor, Typ, Name_Next);
15482 Next_Id := Entity (Expr);
15484 elsif Chars (Prim) = Name_Has_Element then
15485 Resolve_Iterable_Operation (Expr, Cursor, Typ, Name_Has_Element);
15486 Has_Element_Id := Entity (Expr);
15488 elsif Chars (Prim) = Name_Element then
15489 Resolve_Iterable_Operation (Expr, Cursor, Typ, Name_Element);
15490 Element_Id := Entity (Expr);
15493 Error_Msg_N ("invalid name for iterable function", Prim);
15499 if No (First_Id) then
15500 Error_Msg_N ("match for First primitive not found", ASN);
15502 elsif No (Next_Id) then
15503 Error_Msg_N ("match for Next primitive not found", ASN);
15505 elsif No (Has_Element_Id) then
15506 Error_Msg_N ("match for Has_Element primitive not found", ASN);
15508 elsif No (Element_Id) or else No (Last_Id) then
15511 end Validate_Iterable_Aspect;
15513 ------------------------------
15514 -- Validate_Literal_Aspect --
15515 ------------------------------
15517 procedure Validate_Literal_Aspect (Typ : Entity_Id; ASN : Node_Id) is
15518 A_Id : constant Aspect_Id := Get_Aspect_Id (ASN);
15519 pragma Assert ((A_Id = Aspect_Integer_Literal) or
15520 (A_Id = Aspect_Real_Literal) or
15521 (A_Id = Aspect_String_Literal));
15522 Func_Name : constant Node_Id := Expression (ASN);
15523 Overloaded : Boolean := Is_Overloaded (Func_Name);
15527 Param_Type : Entity_Id;
15528 Match_Found : Boolean := False;
15529 Is_Match : Boolean;
15532 if not Is_Type (Typ) then
15533 Error_Msg_N ("aspect can only be specified for a type", ASN);
15535 elsif not Is_First_Subtype (Typ) then
15536 Error_Msg_N ("aspect cannot be specified for a subtype", ASN);
15540 if A_Id = Aspect_String_Literal then
15541 if Is_String_Type (Typ) then
15542 Error_Msg_N ("aspect cannot be specified for a string type", ASN);
15545 Param_Type := Standard_Wide_Wide_String;
15547 if Is_Numeric_Type (Typ) then
15548 Error_Msg_N ("aspect cannot be specified for a numeric type", ASN);
15551 Param_Type := Standard_String;
15554 if not Overloaded and then not Present (Entity (Func_Name)) then
15555 Analyze (Func_Name);
15556 Overloaded := Is_Overloaded (Func_Name);
15560 Get_First_Interp (Func_Name, I => I, It => It);
15562 -- only one possible interpretation
15563 It.Nam := Entity (Func_Name);
15564 pragma Assert (Present (It.Nam));
15567 while It.Nam /= Empty loop
15570 if Ekind (It.Nam) = E_Function
15571 and then Base_Type (Etype (It.Nam)) = Typ
15574 Params : constant List_Id :=
15575 Parameter_Specifications (Parent (It.Nam));
15576 Param_Spec : Node_Id;
15577 Param_Id : Entity_Id;
15579 if List_Length (Params) = 1 then
15580 Param_Spec := First (Params);
15581 if not More_Ids (Param_Spec) then
15582 Param_Id := Defining_Identifier (Param_Spec);
15583 if Base_Type (Etype (Param_Id)) = Param_Type
15584 and then Ekind (Param_Id) = E_In_Parameter
15594 if Match_Found then
15595 Error_Msg_N ("aspect specification is ambiguous", ASN);
15598 Match_Found := True;
15602 exit when not Overloaded;
15604 if not Is_Match then
15605 Remove_Interp (I => I);
15608 Get_Next_Interp (I => I, It => It);
15611 if not Match_Found then
15613 ("function name in aspect specification cannot be resolved", ASN);
15617 Set_Entity (Func_Name, Match.Nam);
15618 Set_Etype (Func_Name, Etype (Match.Nam));
15619 Set_Is_Overloaded (Func_Name, False);
15620 end Validate_Literal_Aspect;
15622 -----------------------------------
15623 -- Validate_Unchecked_Conversion --
15624 -----------------------------------
15626 procedure Validate_Unchecked_Conversion
15628 Act_Unit : Entity_Id)
15630 Source : Entity_Id;
15631 Target : Entity_Id;
15635 -- Obtain source and target types. Note that we call Ancestor_Subtype
15636 -- here because the processing for generic instantiation always makes
15637 -- subtypes, and we want the original frozen actual types.
15639 -- If we are dealing with private types, then do the check on their
15640 -- fully declared counterparts if the full declarations have been
15641 -- encountered (they don't have to be visible, but they must exist).
15643 Source := Ancestor_Subtype (Etype (First_Formal (Act_Unit)));
15645 if Is_Private_Type (Source)
15646 and then Present (Underlying_Type (Source))
15648 Source := Underlying_Type (Source);
15651 Target := Ancestor_Subtype (Etype (Act_Unit));
15653 -- If either type is generic, the instantiation happens within a generic
15654 -- unit, and there is nothing to check. The proper check will happen
15655 -- when the enclosing generic is instantiated.
15657 if Is_Generic_Type (Source) or else Is_Generic_Type (Target) then
15661 if Is_Private_Type (Target)
15662 and then Present (Underlying_Type (Target))
15664 Target := Underlying_Type (Target);
15667 -- Source may be unconstrained array, but not target, except in relaxed
15670 if Is_Array_Type (Target)
15671 and then not Is_Constrained (Target)
15672 and then not Relaxed_RM_Semantics
15675 ("unchecked conversion to unconstrained array not allowed", N);
15679 -- Warn if conversion between two different convention pointers
15681 if Is_Access_Type (Target)
15682 and then Is_Access_Type (Source)
15683 and then Convention (Target) /= Convention (Source)
15684 and then Warn_On_Unchecked_Conversion
15686 -- Give warnings for subprogram pointers only on most targets
15688 if Is_Access_Subprogram_Type (Target)
15689 or else Is_Access_Subprogram_Type (Source)
15692 ("?z?conversion between pointers with different conventions!",
15697 -- Warn if one of the operands is Ada.Calendar.Time. Do not emit a
15698 -- warning when compiling GNAT-related sources.
15700 if Warn_On_Unchecked_Conversion
15701 and then not In_Predefined_Unit (N)
15702 and then RTU_Loaded (Ada_Calendar)
15703 and then (Chars (Source) = Name_Time
15705 Chars (Target) = Name_Time)
15707 -- If Ada.Calendar is loaded and the name of one of the operands is
15708 -- Time, there is a good chance that this is Ada.Calendar.Time.
15711 Calendar_Time : constant Entity_Id := Full_View (RTE (RO_CA_Time));
15713 pragma Assert (Present (Calendar_Time));
15715 if Source = Calendar_Time or else Target = Calendar_Time then
15717 ("?z?representation of 'Time values may change between "
15718 & "'G'N'A'T versions", N);
15723 -- Make entry in unchecked conversion table for later processing by
15724 -- Validate_Unchecked_Conversions, which will check sizes and alignments
15725 -- (using values set by the back end where possible). This is only done
15726 -- if the appropriate warning is active.
15728 if Warn_On_Unchecked_Conversion then
15729 Unchecked_Conversions.Append
15730 (New_Val => UC_Entry'(Eloc => Sloc (N),
15733 Act_Unit => Act_Unit));
15735 -- If both sizes are known statically now, then back-end annotation
15736 -- is not required to do a proper check but if either size is not
15737 -- known statically, then we need the annotation.
15739 if Known_Static_RM_Size (Source)
15741 Known_Static_RM_Size (Target)
15745 Back_Annotate_Rep_Info := True;
15749 -- If unchecked conversion to access type, and access type is declared
15750 -- in the same unit as the unchecked conversion, then set the flag
15751 -- No_Strict_Aliasing (no strict aliasing is implicit here)
15753 if Is_Access_Type (Target) and then
15754 In_Same_Source_Unit (Target, N)
15756 Set_No_Strict_Aliasing (Implementation_Base_Type (Target));
15759 -- Generate N_Validate_Unchecked_Conversion node for back end in case
15760 -- the back end needs to perform special validation checks.
15762 -- Shouldn't this be in Exp_Ch13, since the check only gets done if we
15763 -- have full expansion and the back end is called ???
15766 Make_Validate_Unchecked_Conversion (Sloc (N));
15767 Set_Source_Type (Vnode, Source);
15768 Set_Target_Type (Vnode, Target);
15770 -- If the unchecked conversion node is in a list, just insert before it.
15771 -- If not we have some strange case, not worth bothering about.
15773 if Is_List_Member (N) then
15774 Insert_After (N, Vnode);
15776 end Validate_Unchecked_Conversion;
15778 ------------------------------------
15779 -- Validate_Unchecked_Conversions --
15780 ------------------------------------
15782 procedure Validate_Unchecked_Conversions is
15783 function Is_Null_Array (T : Entity_Id) return Boolean;
15784 -- We want to warn in the case of converting to a wrong-sized array of
15785 -- bytes, including the zero-size case. This returns True in that case,
15786 -- which is necessary because a size of 0 is used to indicate both an
15787 -- unknown size and a size of 0. It's OK for this to return True in
15788 -- other zero-size cases, but we don't go out of our way; for example,
15789 -- we don't bother with multidimensional arrays.
15791 function Is_Null_Array (T : Entity_Id) return Boolean is
15793 if Is_Array_Type (T) and then Is_Constrained (T) then
15795 Index : constant Node_Id := First_Index (T);
15796 R : Node_Id; -- N_Range
15798 case Nkind (Index) is
15801 when N_Subtype_Indication =>
15802 R := Range_Expression (Constraint (Index));
15803 when N_Identifier | N_Expanded_Name =>
15804 R := Scalar_Range (Entity (Index));
15806 raise Program_Error;
15809 return Is_Null_Range (Low_Bound (R), High_Bound (R));
15817 for N in Unchecked_Conversions.First .. Unchecked_Conversions.Last loop
15819 T : UC_Entry renames Unchecked_Conversions.Table (N);
15821 Act_Unit : constant Entity_Id := T.Act_Unit;
15822 Eloc : constant Source_Ptr := T.Eloc;
15823 Source : constant Entity_Id := T.Source;
15824 Target : constant Entity_Id := T.Target;
15830 -- Skip if function marked as warnings off
15832 if Warnings_Off (Act_Unit) or else Serious_Errors_Detected > 0 then
15836 -- Don't do the check if warnings off for either type, note the
15837 -- deliberate use of OR here instead of OR ELSE to get the flag
15838 -- Warnings_Off_Used set for both types if appropriate.
15840 if Has_Warnings_Off (Source) or Has_Warnings_Off (Target) then
15844 if (Known_Static_RM_Size (Source)
15845 and then Known_Static_RM_Size (Target))
15846 or else Is_Null_Array (Target)
15848 -- This validation check, which warns if we have unequal sizes
15849 -- for unchecked conversion, and thus implementation dependent
15850 -- semantics, is one of the few occasions on which we use the
15851 -- official RM size instead of Esize. See description in Einfo
15852 -- "Handling of Type'Size Values" for details.
15854 Source_Siz := RM_Size (Source);
15855 Target_Siz := RM_Size (Target);
15857 if Source_Siz /= Target_Siz then
15859 ("?z?types for unchecked conversion have different sizes!",
15862 if All_Errors_Mode then
15863 Error_Msg_Name_1 := Chars (Source);
15864 Error_Msg_Uint_1 := Source_Siz;
15865 Error_Msg_Name_2 := Chars (Target);
15866 Error_Msg_Uint_2 := Target_Siz;
15867 Error_Msg ("\size of % is ^, size of % is ^?z?", Eloc);
15869 Error_Msg_Uint_1 := UI_Abs (Source_Siz - Target_Siz);
15871 if Is_Discrete_Type (Source)
15873 Is_Discrete_Type (Target)
15875 if Source_Siz > Target_Siz then
15877 ("\?z?^ high order bits of source will "
15878 & "be ignored!", Eloc);
15880 elsif Is_Unsigned_Type (Source) then
15882 ("\?z?source will be extended with ^ high order "
15883 & "zero bits!", Eloc);
15887 ("\?z?source will be extended with ^ high order "
15888 & "sign bits!", Eloc);
15891 elsif Source_Siz < Target_Siz then
15892 if Is_Discrete_Type (Target) then
15893 if Bytes_Big_Endian then
15895 ("\?z?target value will include ^ undefined "
15896 & "low order bits!", Eloc, Act_Unit);
15899 ("\?z?target value will include ^ undefined "
15900 & "high order bits!", Eloc, Act_Unit);
15905 ("\?z?^ trailing bits of target value will be "
15906 & "undefined!", Eloc, Act_Unit);
15909 else pragma Assert (Source_Siz > Target_Siz);
15910 if Is_Discrete_Type (Source) then
15911 if Bytes_Big_Endian then
15913 ("\?z?^ low order bits of source will be "
15914 & "ignored!", Eloc, Act_Unit);
15917 ("\?z?^ high order bits of source will be "
15918 & "ignored!", Eloc, Act_Unit);
15923 ("\?z?^ trailing bits of source will be "
15924 & "ignored!", Eloc, Act_Unit);
15931 -- If both types are access types, we need to check the alignment.
15932 -- If the alignment of both is specified, we can do it here.
15934 if Serious_Errors_Detected = 0
15935 and then Is_Access_Type (Source)
15936 and then Is_Access_Type (Target)
15937 and then Target_Strict_Alignment
15938 and then Present (Designated_Type (Source))
15939 and then Present (Designated_Type (Target))
15942 D_Source : constant Entity_Id := Designated_Type (Source);
15943 D_Target : constant Entity_Id := Designated_Type (Target);
15946 if Known_Alignment (D_Source)
15948 Known_Alignment (D_Target)
15951 Source_Align : constant Uint := Alignment (D_Source);
15952 Target_Align : constant Uint := Alignment (D_Target);
15955 if Source_Align < Target_Align
15956 and then not Is_Tagged_Type (D_Source)
15958 -- Suppress warning if warnings suppressed on either
15959 -- type or either designated type. Note the use of
15960 -- OR here instead of OR ELSE. That is intentional,
15961 -- we would like to set flag Warnings_Off_Used in
15962 -- all types for which warnings are suppressed.
15964 and then not (Has_Warnings_Off (D_Source)
15966 Has_Warnings_Off (D_Target)
15968 Has_Warnings_Off (Source)
15970 Has_Warnings_Off (Target))
15972 Error_Msg_Uint_1 := Target_Align;
15973 Error_Msg_Uint_2 := Source_Align;
15974 Error_Msg_Node_1 := D_Target;
15975 Error_Msg_Node_2 := D_Source;
15977 ("?z?alignment of & (^) is stricter than "
15978 & "alignment of & (^)!", Eloc, Act_Unit);
15980 ("\?z?resulting access value may have invalid "
15981 & "alignment!", Eloc, Act_Unit);
15992 end Validate_Unchecked_Conversions;