1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2022, 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 Contracts; use Contracts;
30 with Debug; use Debug;
31 with Einfo; use Einfo;
32 with Einfo.Entities; use Einfo.Entities;
33 with Einfo.Utils; use Einfo.Utils;
34 with Elists; use Elists;
35 with Errout; use Errout;
36 with Exp_Ch3; use Exp_Ch3;
37 with Exp_Disp; use Exp_Disp;
38 with Exp_Tss; use Exp_Tss;
39 with Exp_Util; use Exp_Util;
40 with Freeze; use Freeze;
41 with Ghost; use Ghost;
43 with Lib.Xref; use Lib.Xref;
44 with Namet; use Namet;
45 with Nlists; use Nlists;
46 with Nmake; use Nmake;
48 with Par_SCO; use Par_SCO;
49 with Restrict; use Restrict;
50 with Rident; use Rident;
51 with Rtsfind; use Rtsfind;
53 with Sem_Aux; use Sem_Aux;
54 with Sem_Case; use Sem_Case;
55 with Sem_Cat; use Sem_Cat;
56 with Sem_Ch3; use Sem_Ch3;
57 with Sem_Ch6; use Sem_Ch6;
58 with Sem_Ch7; use Sem_Ch7;
59 with Sem_Ch8; use Sem_Ch8;
60 with Sem_Dim; use Sem_Dim;
61 with Sem_Eval; use Sem_Eval;
62 with Sem_Prag; use Sem_Prag;
63 with Sem_Res; use Sem_Res;
64 with Sem_Type; use Sem_Type;
65 with Sem_Util; use Sem_Util;
66 with Sem_Warn; use Sem_Warn;
67 with Sinfo; use Sinfo;
68 with Sinfo.Nodes; use Sinfo.Nodes;
69 with Sinfo.Utils; use Sinfo.Utils;
70 with Sinput; use Sinput;
71 with Snames; use Snames;
72 with Stand; use Stand;
73 with System.Case_Util; use System.Case_Util;
75 with Targparm; use Targparm;
76 with Ttypes; use Ttypes;
77 with Tbuild; use Tbuild;
78 with Urealp; use Urealp;
79 with Warnsw; use Warnsw;
81 with GNAT.Heap_Sort_G;
83 package body Sem_Ch13 is
85 SSU : constant Pos := System_Storage_Unit;
86 -- Convenient short hand for commonly used constant
88 -----------------------
89 -- Local Subprograms --
90 -----------------------
92 procedure Adjust_Record_For_Reverse_Bit_Order_Ada_95 (R : Entity_Id);
93 -- Helper routine providing the original (pre-AI95-0133) behavior for
94 -- Adjust_Record_For_Reverse_Bit_Order.
96 procedure Alignment_Check_For_Size_Change (Typ : Entity_Id; Size : Uint);
97 -- This routine is called after setting one of the sizes of type entity
98 -- Typ to Size. The purpose is to deal with the situation of a derived
99 -- type whose inherited alignment is no longer appropriate for the new
100 -- size value. In this case, we reset the Alignment to unknown.
102 function All_Static_Choices (L : List_Id) return Boolean;
103 -- Returns true if all elements of the list are OK static choices
104 -- as defined below for Is_Static_Choice. Used for case expression
105 -- alternatives and for the right operand of a membership test. An
106 -- others_choice is static if the corresponding expression is static.
107 -- The staticness of the bounds is checked separately.
109 procedure Build_Discrete_Static_Predicate
113 -- Given a predicated type Typ, where Typ is a discrete static subtype,
114 -- whose predicate expression is Expr, tests if Expr is a static predicate,
115 -- and if so, builds the predicate range list. Nam is the name of the one
116 -- argument to the predicate function. Occurrences of the type name in the
117 -- predicate expression have been replaced by identifier references to this
118 -- name, which is unique, so any identifier with Chars matching Nam must be
119 -- a reference to the type. If the predicate is non-static, this procedure
120 -- returns doing nothing. If the predicate is static, then the predicate
121 -- list is stored in Static_Discrete_Predicate (Typ), and the Expr is
122 -- rewritten as a canonicalized membership operation.
124 function Build_Export_Import_Pragma
126 Id : Entity_Id) return Node_Id;
127 -- Create the corresponding pragma for aspect Export or Import denoted by
128 -- Asp. Id is the related entity subject to the aspect. Return Empty when
129 -- the expression of aspect Asp evaluates to False or is erroneous.
131 function Build_Predicate_Function_Declaration
132 (Typ : Entity_Id) return Node_Id;
133 -- Build the declaration for a predicate function. The declaration is built
134 -- at the end of the declarative part containing the type definition, which
135 -- may be before the freeze point of the type. The predicate expression is
136 -- preanalyzed at this point, to catch visibility errors.
138 procedure Build_Predicate_Function (Typ : Entity_Id; N : Node_Id);
139 -- If Typ has predicates (indicated by Has_Predicates being set for Typ),
140 -- then either there are pragma Predicate entries on the rep chain for the
141 -- type (note that Predicate aspects are converted to pragma Predicate), or
142 -- there are inherited aspects from a parent type, or ancestor subtypes.
143 -- This procedure builds body for the Predicate function that tests these
144 -- predicates. N is the freeze node for the type. The spec of the function
145 -- is inserted before the freeze node, and the body of the function is
146 -- inserted after the freeze node.
148 procedure Check_Pool_Size_Clash (Ent : Entity_Id; SP, SS : Node_Id);
149 -- Called if both Storage_Pool and Storage_Size attribute definition
150 -- clauses (SP and SS) are present for entity Ent. Issue error message.
152 procedure Freeze_Entity_Checks (N : Node_Id);
153 -- Called from Analyze_Freeze_Entity and Analyze_Generic_Freeze Entity
154 -- to generate appropriate semantic checks that are delayed until this
155 -- point (they had to be delayed this long for cases of delayed aspects,
156 -- e.g. analysis of statically predicated subtypes in choices, for which
157 -- we have to be sure the subtypes in question are frozen before checking).
159 function Get_Alignment_Value (Expr : Node_Id) return Uint;
160 -- Given the expression for an alignment value, returns the corresponding
161 -- Uint value. If the value is inappropriate, then error messages are
162 -- posted as required, and a value of No_Uint is returned.
164 function Is_Operational_Item (N : Node_Id) return Boolean;
165 -- A specification for a stream attribute is allowed before the full type
166 -- is declared, as explained in AI-00137 and the corrigendum. Attributes
167 -- that do not specify a representation characteristic are operational
170 function Is_Static_Choice (N : Node_Id) return Boolean;
171 -- Returns True if N represents a static choice (static subtype, or
172 -- static subtype indication, or static expression, or static range).
174 -- Note that this is a bit more inclusive than we actually need
175 -- (in particular membership tests do not allow the use of subtype
176 -- indications). But that doesn't matter, we have already checked
177 -- that the construct is legal to get this far.
179 function Is_Type_Related_Rep_Item (N : Node_Id) return Boolean;
180 -- Returns True for a representation clause/pragma that specifies a
181 -- type-related representation (as opposed to operational) aspect.
183 function Is_Predicate_Static
186 Warn : Boolean := True) return Boolean;
187 -- Given predicate expression Expr, tests if Expr is predicate-static in
188 -- the sense of the rules in (RM 3.2.4 (15-24)). Occurrences of the type
189 -- name in the predicate expression have been replaced by references to
190 -- an identifier whose Chars field is Nam. This name is unique, so any
191 -- identifier with Chars matching Nam must be a reference to the type.
192 -- Returns True if the expression is predicate-static and False otherwise,
193 -- but is not in the business of setting flags or issuing error messages.
195 -- Only scalar types can have static predicates, so False is always
196 -- returned for non-scalar types.
198 -- Note: the RM seems to suggest that string types can also have static
199 -- predicates. But that really makes little sense as very few useful
200 -- predicates can be constructed for strings. Remember that:
204 -- is not a static expression. So even though the clearly faulty RM wording
205 -- allows the following:
207 -- subtype S is String with Static_Predicate => S < "DEF"
209 -- We can't allow this, otherwise we have predicate-static applying to a
210 -- larger class than static expressions, which was never intended.
212 -- The Warn parameter is True iff this is not a recursive call. This
213 -- parameter is used to avoid generating warnings for subexpressions and
214 -- for cases where the predicate expression (as originally written by
215 -- the user, before any transformations) is a Boolean literal.
217 procedure New_Put_Image_Subprogram
221 -- Similar to New_Stream_Subprogram, but for the Put_Image attribute
223 procedure New_Stream_Subprogram
227 Nam : TSS_Name_Type);
228 -- Create a subprogram renaming of a given stream attribute to the
229 -- designated subprogram and then in the tagged case, provide this as a
230 -- primitive operation, or in the untagged case make an appropriate TSS
231 -- entry. This is more properly an expansion activity than just semantics,
232 -- but the presence of user-defined stream functions for limited types
233 -- is a legality check, which is why this takes place here rather than in
234 -- exp_ch13, where it was previously. Nam indicates the name of the TSS
235 -- function to be generated.
237 -- To avoid elaboration anomalies with freeze nodes, for untagged types
238 -- we generate both a subprogram declaration and a subprogram renaming
239 -- declaration, so that the attribute specification is handled as a
240 -- renaming_as_body. For tagged types, the specification is one of the
243 procedure No_Type_Rep_Item (N : Node_Id);
244 -- Output message indicating that no type-related aspects can be
245 -- specified due to some property of the parent type.
247 procedure Register_Address_Clause_Check
253 -- Register a check for the address clause N. The rest of the parameters
254 -- are in keeping with the components of Address_Clause_Check_Record below.
256 procedure Validate_Aspect_Aggregate (N : Node_Id);
257 -- Check legality of operations given in the Ada 2022 Aggregate aspect for
260 procedure Resolve_Aspect_Aggregate
263 -- Resolve each one of the operations specified in the specification of
266 procedure Validate_Aspect_Stable_Properties
267 (E : Entity_Id; N : Node_Id; Class_Present : Boolean);
268 -- Check legality of functions given in the Ada 2022 Stable_Properties
269 -- (or Stable_Properties'Class) aspect.
271 procedure Validate_Storage_Model_Type_Aspect
272 (Typ : Entity_Id; ASN : Node_Id);
273 -- Check legality and completeness of the aggregate associations given in
274 -- the Storage_Model_Type aspect associated with Typ.
276 procedure Resolve_Storage_Model_Type_Argument
279 Addr_Type : in out Entity_Id;
281 -- Resolve argument N to be of the proper kind (when a type or constant)
282 -- or to have the proper profile (when a subprogram).
284 procedure Resolve_Aspect_Stable_Properties
285 (Typ_Or_Subp : Entity_Id;
287 Class_Present : Boolean);
288 -- Resolve each one of the functions specified in the specification of
289 -- aspect Stable_Properties (or Stable_Properties'Class).
291 procedure Resolve_Iterable_Operation
296 -- If the name of a primitive operation for an Iterable aspect is
297 -- overloaded, resolve according to required signature.
303 Biased : Boolean := True);
304 -- If Biased is True, sets Has_Biased_Representation flag for E, and
305 -- outputs a warning message at node N if Warn_On_Biased_Representation is
306 -- is True. This warning inserts the string Msg to describe the construct
309 -----------------------------------------------------------
310 -- Visibility of Discriminants in Aspect Specifications --
311 -----------------------------------------------------------
313 -- The discriminants of a type are visible when analyzing the aspect
314 -- specifications of a type declaration or protected type declaration,
315 -- but not when analyzing those of a subtype declaration. The following
316 -- routines enforce this distinction.
318 procedure Push_Type (E : Entity_Id);
319 -- Push scope E and make visible the discriminants of type entity E if E
320 -- has discriminants and is not a subtype.
322 procedure Pop_Type (E : Entity_Id);
323 -- Remove visibility to the discriminants of type entity E and pop the
324 -- scope stack if E has discriminants and is not a subtype.
326 ----------------------------------------------
327 -- Table for Validate_Unchecked_Conversions --
328 ----------------------------------------------
330 -- The following table collects unchecked conversions for validation.
331 -- Entries are made by Validate_Unchecked_Conversion and then the call
332 -- to Validate_Unchecked_Conversions does the actual error checking and
333 -- posting of warnings. The reason for this delayed processing is to take
334 -- advantage of back-annotations of size and alignment values performed by
337 -- Note: the reason we store a Source_Ptr value instead of a Node_Id is
338 -- that by the time Validate_Unchecked_Conversions is called, Sprint will
339 -- already have modified all Sloc values if the -gnatD option is set.
341 type UC_Entry is record
342 Eloc : Source_Ptr; -- node used for posting warnings
343 Source : Entity_Id; -- source type for unchecked conversion
344 Target : Entity_Id; -- target type for unchecked conversion
345 Act_Unit : Entity_Id; -- actual function instantiated
348 package Unchecked_Conversions is new Table.Table (
349 Table_Component_Type => UC_Entry,
350 Table_Index_Type => Int,
351 Table_Low_Bound => 1,
353 Table_Increment => 200,
354 Table_Name => "Unchecked_Conversions");
356 ----------------------------------------
357 -- Table for Validate_Address_Clauses --
358 ----------------------------------------
360 -- If an address clause has the form
362 -- for X'Address use Expr
364 -- where Expr has a value known at compile time or is of the form Y'Address
365 -- or recursively is a reference to a constant initialized with either of
366 -- these forms, and the value of Expr is not a multiple of X's alignment,
367 -- or if Y has a smaller alignment than X, then that merits a warning about
368 -- possible bad alignment. The following table collects address clauses of
369 -- this kind. We put these in a table so that they can be checked after the
370 -- back end has completed annotation of the alignments of objects, since we
371 -- can catch more cases that way.
373 type Address_Clause_Check_Record is record
375 -- The address clause
378 -- The entity of the object subject to the address clause
381 -- The value of the address in the first case
384 -- The entity of the object being overlaid in the second case
387 -- Whether the address is offset within Y in the second case
389 Alignment_Checks_Suppressed : Boolean;
390 -- Whether alignment checks are suppressed by an active scope suppress
391 -- setting. We need to save the value in order to be able to reuse it
392 -- after the back end has been run.
395 package Address_Clause_Checks is new Table.Table (
396 Table_Component_Type => Address_Clause_Check_Record,
397 Table_Index_Type => Int,
398 Table_Low_Bound => 1,
400 Table_Increment => 200,
401 Table_Name => "Address_Clause_Checks");
403 function Alignment_Checks_Suppressed
404 (ACCR : Address_Clause_Check_Record) return Boolean;
405 -- Return whether the alignment check generated for the address clause
408 ---------------------------------
409 -- Alignment_Checks_Suppressed --
410 ---------------------------------
412 function Alignment_Checks_Suppressed
413 (ACCR : Address_Clause_Check_Record) return Boolean
416 if Checks_May_Be_Suppressed (ACCR.X) then
417 return Is_Check_Suppressed (ACCR.X, Alignment_Check);
419 return ACCR.Alignment_Checks_Suppressed;
421 end Alignment_Checks_Suppressed;
423 -----------------------------------------
424 -- Adjust_Record_For_Reverse_Bit_Order --
425 -----------------------------------------
427 procedure Adjust_Record_For_Reverse_Bit_Order (R : Entity_Id) is
428 Max_Machine_Scalar_Size : constant Uint :=
429 UI_From_Int (System_Max_Integer_Size);
430 -- We use this as the maximum machine scalar size
432 SSU : constant Uint := UI_From_Int (System_Storage_Unit);
439 -- The processing done here used to depend on the Ada version, but the
440 -- behavior has been changed by AI95-0133. However this AI is a Binding
441 -- Interpretation, so we now implement it even in Ada 95 mode. But the
442 -- original behavior from unamended Ada 95 is available for the sake of
443 -- compatibility under the debugging switch -gnatd.p in Ada 95 mode.
445 if Ada_Version < Ada_2005 and then Debug_Flag_Dot_P then
446 Adjust_Record_For_Reverse_Bit_Order_Ada_95 (R);
450 -- For Ada 2005, we do machine scalar processing, as fully described In
451 -- AI-133. This involves gathering all components which start at the
452 -- same byte offset and processing them together. Same approach is still
453 -- valid in later versions including Ada 2012.
455 -- Note that component clauses found on record types may be inherited,
456 -- in which case the layout of the component with such a clause still
457 -- has to be done at this point. Therefore, the processing done here
458 -- must exclusively rely on the Component_Clause of the component.
460 -- This first loop through components does two things. First it deals
461 -- with the case of components with component clauses whose length is
462 -- greater than the maximum machine scalar size (either accepting them
463 -- or rejecting as needed). Second, it counts the number of components
464 -- with component clauses whose length does not exceed this maximum for
468 Comp := First_Component_Or_Discriminant (R);
469 while Present (Comp) loop
470 CC := Component_Clause (Comp);
474 Fbit : constant Uint := Static_Integer (First_Bit (CC));
475 Lbit : constant Uint := Static_Integer (Last_Bit (CC));
478 -- Case of component with last bit >= max machine scalar
480 if Lbit >= Max_Machine_Scalar_Size then
482 -- This is allowed only if first bit is zero, and last bit
483 -- + 1 is a multiple of storage unit size.
485 if Fbit = 0 and then (Lbit + 1) mod SSU = 0 then
487 -- This is the case to give a warning if enabled
489 if Warn_On_Reverse_Bit_Order then
491 ("info: multi-byte field specified with "
492 & "non-standard Bit_Order?.v?", CC);
494 if Bytes_Big_Endian then
496 ("\bytes are not reversed "
497 & "(component is big-endian)?.v?", CC);
500 ("\bytes are not reversed "
501 & "(component is little-endian)?.v?", CC);
505 -- Give error message for RM 13.5.1(10) violation
509 ("machine scalar rules not followed for&",
510 First_Bit (CC), Comp);
512 Error_Msg_Uint_1 := Lbit + 1;
513 Error_Msg_Uint_2 := Max_Machine_Scalar_Size;
515 ("\last bit + 1 (^) exceeds maximum machine scalar "
516 & "size (^)", First_Bit (CC));
518 if (Lbit + 1) mod SSU /= 0 then
519 Error_Msg_Uint_1 := SSU;
521 ("\and is not a multiple of Storage_Unit (^) "
522 & "(RM 13.5.1(10))", First_Bit (CC));
525 Error_Msg_Uint_1 := Fbit;
527 ("\and first bit (^) is non-zero "
528 & "(RM 13.4.1(10))", First_Bit (CC));
532 -- OK case of machine scalar related component clause. For now,
536 Num_CC := Num_CC + 1;
541 Next_Component_Or_Discriminant (Comp);
544 -- We need to sort the component clauses on the basis of the Position
545 -- values in the clause, so we can group clauses with the same Position
546 -- together to determine the relevant machine scalar size.
549 Comps : array (0 .. Num_CC) of Entity_Id;
550 -- Array to collect component and discriminant entities. The data
551 -- starts at index 1, the 0'th entry is for the sort routine.
553 function CP_Lt (Op1, Op2 : Natural) return Boolean;
554 -- Compare routine for Sort
556 procedure CP_Move (From : Natural; To : Natural);
557 -- Move routine for Sort
559 package Sorting is new GNAT.Heap_Sort_G (CP_Move, CP_Lt);
562 -- Maximum last bit value of any component in this set
565 -- Corresponding machine scalar size
569 -- Start and stop positions in the component list of the set of
570 -- components with the same starting position (that constitute
571 -- components in a single machine scalar).
577 function CP_Lt (Op1, Op2 : Natural) return Boolean is
580 Position (Component_Clause (Comps (Op1))) <
581 Position (Component_Clause (Comps (Op2)));
588 procedure CP_Move (From : Natural; To : Natural) is
590 Comps (To) := Comps (From);
593 -- Start of processing for Sort_CC
596 -- Collect the machine scalar relevant component clauses
599 Comp := First_Component_Or_Discriminant (R);
600 while Present (Comp) loop
602 CC : constant Node_Id := Component_Clause (Comp);
605 -- Collect only component clauses whose last bit is less than
606 -- machine scalar size. Any component clause whose last bit
607 -- exceeds this value does not take part in machine scalar
608 -- layout considerations. The test for Error_Posted makes sure
609 -- we exclude component clauses for which we already posted an
613 and then not Error_Posted (Last_Bit (CC))
614 and then Static_Integer (Last_Bit (CC)) <
615 Max_Machine_Scalar_Size
617 Num_CC := Num_CC + 1;
618 Comps (Num_CC) := Comp;
622 Next_Component_Or_Discriminant (Comp);
625 -- Sort by ascending position number
627 Sorting.Sort (Num_CC);
629 -- We now have all the components whose size does not exceed the max
630 -- machine scalar value, sorted by starting position. In this loop we
631 -- gather groups of clauses starting at the same position, to process
632 -- them in accordance with AI-133.
635 while Stop < Num_CC loop
640 (Last_Bit (Component_Clause (Comps (Start))));
641 while Stop < Num_CC loop
643 (Position (Component_Clause (Comps (Stop + 1)))) =
645 (Position (Component_Clause (Comps (Stop))))
653 (Component_Clause (Comps (Stop)))));
659 -- Now we have a group of component clauses from Start to Stop
660 -- whose positions are identical, and MaxL is the maximum last
661 -- bit value of any of these components.
663 -- We need to determine the corresponding machine scalar size.
664 -- This loop assumes that machine scalar sizes are even, and that
665 -- each possible machine scalar has twice as many bits as the next
668 MSS := Max_Machine_Scalar_Size;
670 and then (MSS / 2) >= SSU
671 and then (MSS / 2) > MaxL
676 -- Here is where we fix up the Component_Bit_Offset value to
677 -- account for the reverse bit order. Some examples of what needs
678 -- to be done for the case of a machine scalar size of 8 are:
680 -- First_Bit .. Last_Bit Component_Bit_Offset
692 -- The rule is that the first bit is obtained by subtracting the
693 -- old ending bit from machine scalar size - 1.
695 for C in Start .. Stop loop
697 Comp : constant Entity_Id := Comps (C);
698 CC : constant Node_Id := Component_Clause (Comp);
700 FB : constant Uint := Static_Integer (First_Bit (CC));
701 LB : constant Uint := Static_Integer (Last_Bit (CC));
702 NFB : constant Uint := MSS - 1 - LB;
703 NLB : constant Uint := NFB + LB - FB;
704 Pos : constant Uint := Static_Integer (Position (CC));
707 -- Do not warn for the artificial clause built for the tag
708 -- in Check_Record_Representation_Clause if it is inherited.
710 if Warn_On_Reverse_Bit_Order
711 and then Chars (Comp) /= Name_uTag
713 Error_Msg_Uint_1 := MSS;
715 ("info: reverse bit order in machine scalar of "
716 & "length^?.v?", First_Bit (CC));
717 Error_Msg_Uint_1 := NFB;
718 Error_Msg_Uint_2 := NLB;
720 if Bytes_Big_Endian then
722 ("\big-endian range for component & is ^ .. ^?.v?",
723 First_Bit (CC), Comp);
726 ("\little-endian range for component " &
728 First_Bit (CC), Comp);
732 Set_Component_Bit_Offset (Comp, Pos * SSU + NFB);
733 Set_Esize (Comp, 1 + (NLB - NFB));
734 Set_Normalized_First_Bit (Comp, NFB mod SSU);
735 Set_Normalized_Position (Comp, Pos + NFB / SSU);
740 end Adjust_Record_For_Reverse_Bit_Order;
742 ------------------------------------------------
743 -- Adjust_Record_For_Reverse_Bit_Order_Ada_95 --
744 ------------------------------------------------
746 procedure Adjust_Record_For_Reverse_Bit_Order_Ada_95 (R : Entity_Id) is
751 -- For Ada 95, we just renumber bits within a storage unit. We do the
752 -- same for Ada 83 mode, since we recognize the Bit_Order attribute in
753 -- Ada 83, and are free to add this extension.
755 Comp := First_Component_Or_Discriminant (R);
756 while Present (Comp) loop
757 CC := Component_Clause (Comp);
759 -- If component clause is present, then deal with the non-default
760 -- bit order case for Ada 95 mode.
762 -- We only do this processing for the base type, and in fact that
763 -- is important, since otherwise if there are record subtypes, we
764 -- could reverse the bits once for each subtype, which is wrong.
766 if Present (CC) and then Ekind (R) = E_Record_Type then
768 CFB : constant Uint := Component_Bit_Offset (Comp);
769 CSZ : constant Uint := Esize (Comp);
770 CLC : constant Node_Id := Component_Clause (Comp);
771 Pos : constant Node_Id := Position (CLC);
772 FB : constant Node_Id := First_Bit (CLC);
774 Storage_Unit_Offset : constant Uint :=
775 CFB / System_Storage_Unit;
777 Start_Bit : constant Uint :=
778 CFB mod System_Storage_Unit;
781 -- Cases where field goes over storage unit boundary
783 if Start_Bit + CSZ > System_Storage_Unit then
785 -- Allow multi-byte field but generate warning
787 if Start_Bit mod System_Storage_Unit = 0
788 and then CSZ mod System_Storage_Unit = 0
791 ("info: multi-byte field specified with non-standard "
792 & "Bit_Order?.v?", CLC);
794 if Bytes_Big_Endian then
796 ("\bytes are not reversed "
797 & "(component is big-endian)?.v?", CLC);
800 ("\bytes are not reversed "
801 & "(component is little-endian)?.v?", CLC);
804 -- Do not allow non-contiguous field
808 ("attempt to specify non-contiguous field not "
811 ("\caused by non-standard Bit_Order specified in "
812 & "legacy Ada 95 mode", CLC);
815 -- Case where field fits in one storage unit
818 -- Give warning if suspicious component clause
820 if Intval (FB) >= System_Storage_Unit
821 and then Warn_On_Reverse_Bit_Order
824 ("info: Bit_Order clause does not affect byte "
825 & "ordering?.v?", Pos);
827 Intval (Pos) + Intval (FB) /
830 ("info: position normalized to ^ before bit order "
831 & "interpreted?.v?", Pos);
834 -- Here is where we fix up the Component_Bit_Offset value
835 -- to account for the reverse bit order. Some examples of
836 -- what needs to be done are:
838 -- First_Bit .. Last_Bit Component_Bit_Offset
850 -- The rule is that the first bit is obtained by subtracting
851 -- the old ending bit from storage_unit - 1.
853 Set_Component_Bit_Offset (Comp,
854 (Storage_Unit_Offset * System_Storage_Unit) +
855 (System_Storage_Unit - 1) -
856 (Start_Bit + CSZ - 1));
858 Set_Normalized_Position (Comp,
859 Component_Bit_Offset (Comp) / System_Storage_Unit);
861 Set_Normalized_First_Bit (Comp,
862 Component_Bit_Offset (Comp) mod System_Storage_Unit);
867 Next_Component_Or_Discriminant (Comp);
869 end Adjust_Record_For_Reverse_Bit_Order_Ada_95;
871 -------------------------------------
872 -- Alignment_Check_For_Size_Change --
873 -------------------------------------
875 procedure Alignment_Check_For_Size_Change (Typ : Entity_Id; Size : Uint) is
877 -- If the alignment is known, and not set by a rep clause, and is
878 -- inconsistent with the size being set, then reset it to unknown,
879 -- we assume in this case that the size overrides the inherited
880 -- alignment, and that the alignment must be recomputed.
882 if Known_Alignment (Typ)
883 and then not Has_Alignment_Clause (Typ)
884 and then Present (Size)
885 and then Size mod (Alignment (Typ) * SSU) /= 0
887 Reinit_Alignment (Typ);
889 end Alignment_Check_For_Size_Change;
891 -----------------------------------
892 -- All_Membership_Choices_Static --
893 -----------------------------------
895 function All_Membership_Choices_Static (Expr : Node_Id) return Boolean is
896 pragma Assert (Nkind (Expr) in N_Membership_Test);
899 (Present (Right_Opnd (Expr))
901 Present (Alternatives (Expr)));
903 if Present (Right_Opnd (Expr)) then
904 return Is_Static_Choice (Right_Opnd (Expr));
906 return All_Static_Choices (Alternatives (Expr));
908 end All_Membership_Choices_Static;
910 ------------------------
911 -- All_Static_Choices --
912 ------------------------
914 function All_Static_Choices (L : List_Id) return Boolean is
919 while Present (N) loop
920 if not Is_Static_Choice (N) then
928 end All_Static_Choices;
930 -------------------------------------
931 -- Analyze_Aspects_At_Freeze_Point --
932 -------------------------------------
934 procedure Analyze_Aspects_At_Freeze_Point (E : Entity_Id) is
935 procedure Analyze_Aspect_Default_Value (ASN : Node_Id);
936 -- This routine analyzes an Aspect_Default_[Component_]Value denoted by
937 -- the aspect specification node ASN.
939 procedure Check_Aspect_Too_Late (N : Node_Id);
940 -- This procedure is similar to Rep_Item_Too_Late for representation
941 -- aspects that apply to type and that do not have a corresponding
943 -- Used to check in particular that the expression associated with
944 -- aspect node N for the given type (entity) of the aspect does not
945 -- appear too late according to the rules in RM 13.1(9) and 13.1(10).
947 procedure Make_Pragma_From_Boolean_Aspect (ASN : Node_Id);
948 -- Given an aspect specification node ASN whose expression is an
949 -- optional Boolean, this routines creates the corresponding pragma
950 -- at the freezing point.
952 ----------------------------------
953 -- Analyze_Aspect_Default_Value --
954 ----------------------------------
956 procedure Analyze_Aspect_Default_Value (ASN : Node_Id) is
957 Ent : constant Entity_Id := Entity (ASN);
958 Expr : constant Node_Id := Expression (ASN);
961 Set_Has_Default_Aspect (Base_Type (Ent));
963 if Is_Scalar_Type (Ent) then
964 Set_Default_Aspect_Value (Base_Type (Ent), Expr);
966 Set_Default_Aspect_Component_Value (Base_Type (Ent), Expr);
969 Check_Aspect_Too_Late (ASN);
970 end Analyze_Aspect_Default_Value;
972 ---------------------------
973 -- Check_Aspect_Too_Late --
974 ---------------------------
976 procedure Check_Aspect_Too_Late (N : Node_Id) is
977 Typ : constant Entity_Id := Entity (N);
978 Expr : constant Node_Id := Expression (N);
980 function Find_Type_Reference
981 (Typ : Entity_Id; Expr : Node_Id) return Boolean;
982 -- Return True if a reference to type Typ is found in the expression
985 -------------------------
986 -- Find_Type_Reference --
987 -------------------------
989 function Find_Type_Reference
990 (Typ : Entity_Id; Expr : Node_Id) return Boolean
992 function Find_Type (N : Node_Id) return Traverse_Result;
993 -- Set Found to True if N refers to Typ
999 function Find_Type (N : Node_Id) return Traverse_Result is
1002 or else (Nkind (N) in N_Identifier | N_Expanded_Name
1003 and then Present (Entity (N))
1004 and then Entity (N) = Typ)
1012 function Search_Type_Reference is new Traverse_Func (Find_Type);
1015 return Search_Type_Reference (Expr) = Abandon;
1016 end Find_Type_Reference;
1018 Parent_Type : Entity_Id;
1021 -- Ensure Expr is analyzed so that e.g. all types are properly
1022 -- resolved for Find_Type_Reference.
1026 -- A self-referential aspect is illegal if it forces freezing the
1027 -- entity before the corresponding aspect has been analyzed.
1029 if Find_Type_Reference (Typ, Expr) then
1031 ("aspect specification causes premature freezing of&", N, Typ);
1034 -- For representation aspects, check for case of untagged derived
1035 -- type whose parent either has primitive operations (pre Ada 2022),
1036 -- or is a by-reference type (RM 13.1(10)).
1037 -- Strictly speaking the check also applies to Ada 2012 but it is
1038 -- really too constraining for existing code already, so relax it.
1039 -- ??? Confirming aspects should be allowed here.
1041 if Is_Representation_Aspect (Get_Aspect_Id (N))
1042 and then Is_Derived_Type (Typ)
1043 and then not Is_Tagged_Type (Typ)
1045 Parent_Type := Etype (Base_Type (Typ));
1047 if Ada_Version <= Ada_2012
1048 and then Has_Primitive_Operations (Parent_Type)
1051 ("|representation aspect not permitted before Ada 2022: " &
1052 "use -gnat2022!", N);
1054 ("\parent type & has primitive operations!", N, Parent_Type);
1056 elsif Is_By_Reference_Type (Parent_Type) then
1057 No_Type_Rep_Item (N);
1059 ("\parent type & is a by-reference type!", N, Parent_Type);
1062 end Check_Aspect_Too_Late;
1064 -------------------------------------
1065 -- Make_Pragma_From_Boolean_Aspect --
1066 -------------------------------------
1068 procedure Make_Pragma_From_Boolean_Aspect (ASN : Node_Id) is
1069 Ident : constant Node_Id := Identifier (ASN);
1070 A_Name : constant Name_Id := Chars (Ident);
1071 A_Id : constant Aspect_Id := Get_Aspect_Id (A_Name);
1072 Ent : constant Entity_Id := Entity (ASN);
1073 Expr : constant Node_Id := Expression (ASN);
1074 Loc : constant Source_Ptr := Sloc (ASN);
1076 procedure Check_False_Aspect_For_Derived_Type;
1077 -- This procedure checks for the case of a false aspect for a derived
1078 -- type, which improperly tries to cancel an aspect inherited from
1081 -----------------------------------------
1082 -- Check_False_Aspect_For_Derived_Type --
1083 -----------------------------------------
1085 procedure Check_False_Aspect_For_Derived_Type is
1089 -- We are only checking derived types
1091 if not Is_Derived_Type (E) then
1095 Par := Nearest_Ancestor (E);
1101 if not Is_Atomic (Par) then
1105 when Aspect_Atomic_Components =>
1106 if not Has_Atomic_Components (Par) then
1110 when Aspect_Discard_Names =>
1111 if not Discard_Names (Par) then
1116 if not Is_Packed (Par) then
1120 when Aspect_Unchecked_Union =>
1121 if not Is_Unchecked_Union (Par) then
1125 when Aspect_Volatile =>
1126 if not Is_Volatile (Par) then
1130 when Aspect_Volatile_Components =>
1131 if not Has_Volatile_Components (Par) then
1135 when Aspect_Volatile_Full_Access
1136 | Aspect_Full_Access_Only
1138 if not Is_Volatile_Full_Access (Par) then
1146 -- Fall through means we are canceling an inherited aspect
1148 Error_Msg_Name_1 := A_Name;
1150 ("derived type& inherits aspect%, cannot cancel", Expr, E);
1151 end Check_False_Aspect_For_Derived_Type;
1158 -- Start of processing for Make_Pragma_From_Boolean_Aspect
1161 if Present (Expr) and then Is_False (Static_Boolean (Expr)) then
1162 Check_False_Aspect_For_Derived_Type;
1165 -- There is no Full_Access_Only pragma so use VFA instead
1167 if A_Name = Name_Full_Access_Only then
1168 P_Name := Name_Volatile_Full_Access;
1175 Pragma_Identifier =>
1176 Make_Identifier (Sloc (Ident), P_Name),
1177 Pragma_Argument_Associations => New_List (
1178 Make_Pragma_Argument_Association (Sloc (Ident),
1179 Expression => New_Occurrence_Of (Ent, Sloc (Ident)))));
1181 Set_From_Aspect_Specification (Prag, True);
1182 Set_Corresponding_Aspect (Prag, ASN);
1183 Set_Aspect_Rep_Item (ASN, Prag);
1184 Set_Is_Delayed_Aspect (Prag);
1185 Set_Parent (Prag, ASN);
1187 end Make_Pragma_From_Boolean_Aspect;
1195 -- Start of processing for Analyze_Aspects_At_Freeze_Point
1198 -- Must be visible in current scope, but if this is a type from a nested
1199 -- package it may be frozen from an object declaration in the enclosing
1200 -- scope, so install the package declarations to complete the analysis
1201 -- of the aspects, if any. If the package itself is frozen the type will
1202 -- have been frozen as well.
1204 if not Scope_Within_Or_Same (Current_Scope, Scope (E)) then
1205 if Is_Type (E) and then From_Nested_Package (E) then
1207 Pack : constant Entity_Id := Scope (E);
1211 Install_Visible_Declarations (Pack);
1212 Install_Private_Declarations (Pack);
1213 Analyze_Aspects_At_Freeze_Point (E);
1215 if Is_Private_Type (E)
1216 and then Present (Full_View (E))
1218 Analyze_Aspects_At_Freeze_Point (Full_View (E));
1221 End_Package_Scope (Pack);
1225 -- Aspects from other entities in different contexts are analyzed
1233 -- Look for aspect specification entries for this entity
1235 ASN := First_Rep_Item (E);
1236 while Present (ASN) loop
1237 if Nkind (ASN) = N_Aspect_Specification then
1238 exit when Entity (ASN) /= E;
1240 if Is_Delayed_Aspect (ASN) then
1241 A_Id := Get_Aspect_Id (ASN);
1245 -- For aspects whose expression is an optional Boolean, make
1246 -- the corresponding pragma at the freeze point.
1248 when Boolean_Aspects
1249 | Library_Unit_Aspects
1251 -- Aspects Export and Import require special handling.
1252 -- Both are by definition Boolean and may benefit from
1253 -- forward references, however their expressions are
1254 -- treated as static. In addition, the syntax of their
1255 -- corresponding pragmas requires extra "pieces" which
1256 -- may also contain forward references. To account for
1257 -- all of this, the corresponding pragma is created by
1258 -- Analyze_Aspect_Export_Import, but is not analyzed as
1259 -- the complete analysis must happen now.
1261 -- Aspect Full_Access_Only must be analyzed last so that
1262 -- aspects Volatile and Atomic, if any, are analyzed.
1264 -- Skip creation of pragma Preelaborable_Initialization
1265 -- in the case where the aspect has an expression,
1266 -- because the pragma is only needed for setting flag
1267 -- Known_To_Have_Preelab_Init, which is set by other
1268 -- means following resolution of the aspect expression.
1270 if A_Id not in Aspect_Export
1271 | Aspect_Full_Access_Only
1273 and then (A_Id /= Aspect_Preelaborable_Initialization
1274 or else No (Expression (ASN)))
1276 Make_Pragma_From_Boolean_Aspect (ASN);
1279 -- Special handling for aspects that don't correspond to
1280 -- pragmas/attributes.
1282 when Aspect_Default_Value
1283 | Aspect_Default_Component_Value
1285 -- Do not inherit aspect for anonymous base type of a
1286 -- scalar or array type, because they apply to the first
1287 -- subtype of the type, and will be processed when that
1288 -- first subtype is frozen.
1290 if Is_Derived_Type (E)
1291 and then not Comes_From_Source (E)
1292 and then E /= First_Subtype (E)
1296 Analyze_Aspect_Default_Value (ASN);
1299 -- Ditto for iterator aspects, because the corresponding
1300 -- attributes may not have been analyzed yet.
1302 when Aspect_Constant_Indexing
1303 | Aspect_Default_Iterator
1304 | Aspect_Iterator_Element
1305 | Aspect_Variable_Indexing
1307 Analyze (Expression (ASN));
1309 if Etype (Expression (ASN)) = Any_Type then
1311 ("\aspect must be fully defined before & is frozen",
1315 when Aspect_Integer_Literal
1316 | Aspect_Real_Literal
1317 | Aspect_String_Literal
1319 Validate_Literal_Aspect (E, ASN);
1321 when Aspect_Iterable =>
1322 Validate_Iterable_Aspect (E, ASN);
1324 when Aspect_Designated_Storage_Model =>
1325 Analyze_And_Resolve (Expression (ASN));
1327 if not Is_Entity_Name (Expression (ASN))
1328 or else not Is_Object (Entity (Expression (ASN)))
1330 No (Find_Aspect (Etype (Expression (ASN)),
1331 Aspect_Storage_Model_Type))
1334 ("must specify name of stand-alone object of type "
1335 & "with aspect Storage_Model_Type",
1338 -- Set access type's Associated_Storage_Pool to denote
1339 -- the Storage_Model_Type object given for the aspect
1340 -- (even though that isn't actually an Ada storage pool).
1343 Set_Associated_Storage_Pool
1344 (E, Entity (Expression (ASN)));
1347 when Aspect_Storage_Model_Type =>
1348 Validate_Storage_Model_Type_Aspect (E, ASN);
1350 when Aspect_Aggregate =>
1357 Ritem := Aspect_Rep_Item (ASN);
1359 if Present (Ritem) then
1365 Next_Rep_Item (ASN);
1368 -- Make a second pass for a Full_Access_Only entry
1370 ASN := First_Rep_Item (E);
1371 while Present (ASN) loop
1372 if Nkind (ASN) = N_Aspect_Specification then
1373 exit when Entity (ASN) /= E;
1375 if Get_Aspect_Id (ASN) = Aspect_Full_Access_Only then
1376 Make_Pragma_From_Boolean_Aspect (ASN);
1377 Ritem := Aspect_Rep_Item (ASN);
1378 if Present (Ritem) then
1384 Next_Rep_Item (ASN);
1388 and then E /= Base_Type (E)
1389 and then Is_First_Subtype (E)
1391 Inherit_Rep_Item_Chain (Base_Type (E), E);
1393 end Analyze_Aspects_At_Freeze_Point;
1395 -----------------------------------
1396 -- Analyze_Aspect_Specifications --
1397 -----------------------------------
1399 procedure Analyze_Aspect_Specifications (N : Node_Id; E : Entity_Id) is
1400 pragma Assert (Present (E));
1402 procedure Decorate (Asp : Node_Id; Prag : Node_Id);
1403 -- Establish linkages between an aspect and its corresponding pragma
1405 procedure Insert_Pragma
1407 Is_Instance : Boolean := False);
1408 -- Subsidiary to the analysis of aspects
1415 -- Initial_Condition
1423 -- Subprogram_Variant
1425 -- Insert pragma Prag such that it mimics the placement of a source
1426 -- pragma of the same kind. Flag Is_Generic should be set when the
1427 -- context denotes a generic instance.
1429 function Relocate_Expression (Source : Node_Id) return Node_Id;
1430 -- Outside of a generic this function is equivalent to Relocate_Node.
1431 -- Inside a generic it is an identity function, because Relocate_Node
1432 -- would create a new node that is not associated with the generic
1433 -- template. This association is needed to save references to entities
1434 -- that are global to the generic (and might be not visible from where
1435 -- the generic is instantiated).
1437 -- Inside a generic the original tree is shared between aspect and
1438 -- a corresponding pragma (or an attribute definition clause). This
1439 -- parallels what is done in sem_prag.adb (see Get_Argument).
1445 procedure Decorate (Asp : Node_Id; Prag : Node_Id) is
1447 Set_Aspect_Rep_Item (Asp, Prag);
1448 Set_Corresponding_Aspect (Prag, Asp);
1449 Set_From_Aspect_Specification (Prag);
1450 Set_Parent (Prag, Asp);
1457 procedure Insert_Pragma
1459 Is_Instance : Boolean := False)
1465 Inserted : Boolean := False;
1468 -- When the aspect appears on an entry, package, protected unit,
1469 -- subprogram, or task unit body, insert the generated pragma at the
1470 -- top of the body declarations to emulate the behavior of a source
1473 -- package body Pack with Aspect is
1475 -- package body Pack is
1478 if Nkind (N) in N_Entry_Body
1484 Decls := Declarations (N);
1488 Set_Declarations (N, Decls);
1491 Prepend_To (Decls, Prag);
1493 -- When the aspect is associated with a [generic] package declaration
1494 -- insert the generated pragma at the top of the visible declarations
1495 -- to emulate the behavior of a source pragma.
1497 -- package Pack with Aspect is
1502 elsif Nkind (N) in N_Generic_Package_Declaration
1503 | N_Package_Declaration
1505 Decls := Visible_Declarations (Specification (N));
1509 Set_Visible_Declarations (Specification (N), Decls);
1512 -- The visible declarations of a generic instance have the
1513 -- following structure:
1515 -- <renamings of generic formals>
1516 -- <renamings of internally-generated spec and body>
1517 -- <first source declaration>
1519 -- Insert the pragma before the first source declaration by
1520 -- skipping the instance "header" to ensure proper visibility of
1524 Decl := First (Decls);
1525 while Present (Decl) loop
1526 if Comes_From_Source (Decl) then
1527 Insert_Before (Decl, Prag);
1535 -- The pragma is placed after the instance "header"
1537 if not Inserted then
1538 Append_To (Decls, Prag);
1541 -- Otherwise this is not a generic instance
1544 Prepend_To (Decls, Prag);
1547 -- When the aspect is associated with a protected unit declaration,
1548 -- insert the generated pragma at the top of the visible declarations
1549 -- the emulate the behavior of a source pragma.
1551 -- protected [type] Prot with Aspect is
1553 -- protected [type] Prot is
1556 elsif Nkind (N) = N_Protected_Type_Declaration then
1557 Def := Protected_Definition (N);
1561 Make_Protected_Definition (Sloc (N),
1562 Visible_Declarations => New_List,
1563 End_Label => Empty);
1565 Set_Protected_Definition (N, Def);
1568 Decls := Visible_Declarations (Def);
1572 Set_Visible_Declarations (Def, Decls);
1575 Prepend_To (Decls, Prag);
1577 -- When the aspect is associated with a task unit declaration, insert
1578 -- insert the generated pragma at the top of the visible declarations
1579 -- the emulate the behavior of a source pragma.
1581 -- task [type] Prot with Aspect is
1583 -- task [type] Prot is
1586 elsif Nkind (N) = N_Task_Type_Declaration then
1587 Def := Task_Definition (N);
1591 Make_Task_Definition (Sloc (N),
1592 Visible_Declarations => New_List,
1593 End_Label => Empty);
1595 Set_Task_Definition (N, Def);
1598 Decls := Visible_Declarations (Def);
1602 Set_Visible_Declarations (Def, Decls);
1605 Prepend_To (Decls, Prag);
1607 -- When the context is a library unit, the pragma is added to the
1608 -- Pragmas_After list.
1610 elsif Nkind (Parent (N)) = N_Compilation_Unit then
1611 Aux := Aux_Decls_Node (Parent (N));
1613 if No (Pragmas_After (Aux)) then
1614 Set_Pragmas_After (Aux, New_List);
1617 Prepend (Prag, Pragmas_After (Aux));
1619 -- Default, the pragma is inserted after the context
1622 Insert_After (N, Prag);
1626 -------------------------
1627 -- Relocate_Expression --
1628 -------------------------
1630 function Relocate_Expression (Source : Node_Id) return Node_Id is
1632 if Inside_A_Generic then
1635 return Atree.Relocate_Node (Source);
1637 end Relocate_Expression;
1642 Aitem : Node_Id := Empty;
1645 L : constant List_Id := Aspect_Specifications (N);
1646 pragma Assert (Present (L));
1648 Ins_Node : Node_Id := N;
1649 -- Insert pragmas/attribute definition clause after this node when no
1650 -- delayed analysis is required.
1652 -- Start of processing for Analyze_Aspect_Specifications
1655 -- The general processing involves building an attribute definition
1656 -- clause or a pragma node that corresponds to the aspect. Then in order
1657 -- to delay the evaluation of this aspect to the freeze point, we attach
1658 -- the corresponding pragma/attribute definition clause to the aspect
1659 -- specification node, which is then placed in the Rep Item chain. In
1660 -- this case we mark the entity by setting the flag Has_Delayed_Aspects
1661 -- and we evaluate the rep item at the freeze point. When the aspect
1662 -- doesn't have a corresponding pragma/attribute definition clause, then
1663 -- its analysis is simply delayed at the freeze point.
1665 -- Some special cases don't require delay analysis, thus the aspect is
1666 -- analyzed right now.
1668 -- Note that there is a special handling for Pre, Post, Test_Case,
1669 -- Contract_Cases and Subprogram_Variant aspects. In these cases, we do
1670 -- not have to worry about delay issues, since the pragmas themselves
1671 -- deal with delay of visibility for the expression analysis. Thus, we
1672 -- just insert the pragma after the node N.
1674 -- Loop through aspects
1676 Aspect := First (L);
1677 Aspect_Loop : while Present (Aspect) loop
1678 Analyze_One_Aspect : declare
1680 Aspect_Exit : exception;
1681 -- This exception is used to exit aspect processing completely. It
1682 -- is used when an error is detected, and no further processing is
1683 -- required. It is also used if an earlier error has left the tree
1684 -- in a state where the aspect should not be processed.
1686 Expr : constant Node_Id := Expression (Aspect);
1687 Id : constant Node_Id := Identifier (Aspect);
1688 Loc : constant Source_Ptr := Sloc (Aspect);
1689 Nam : constant Name_Id := Chars (Id);
1690 A_Id : constant Aspect_Id := Get_Aspect_Id (Nam);
1693 Delay_Required : Boolean;
1694 -- Set False if delay is not required
1696 Eloc : Source_Ptr := No_Location;
1697 -- Source location of expression, modified when we split PPC's. It
1698 -- is set below when Expr is present.
1700 procedure Analyze_Aspect_Convention;
1701 -- Perform analysis of aspect Convention
1703 procedure Analyze_Aspect_Disable_Controlled;
1704 -- Perform analysis of aspect Disable_Controlled
1706 procedure Analyze_Aspect_Export_Import;
1707 -- Perform analysis of aspects Export or Import
1709 procedure Analyze_Aspect_External_Link_Name;
1710 -- Perform analysis of aspects External_Name or Link_Name
1712 procedure Analyze_Aspect_Implicit_Dereference;
1713 -- Perform analysis of the Implicit_Dereference aspects
1715 procedure Analyze_Aspect_Relaxed_Initialization;
1716 -- Perform analysis of aspect Relaxed_Initialization
1718 procedure Analyze_Aspect_Yield;
1719 -- Perform analysis of aspect Yield
1721 procedure Analyze_Aspect_Static;
1722 -- Ada 2022 (AI12-0075): Perform analysis of aspect Static
1724 procedure Check_Expr_Is_OK_Static_Expression
1726 Typ : Entity_Id := Empty);
1727 -- Check the specified expression Expr to make sure that it is a
1728 -- static expression of the given type (i.e. it will be analyzed
1729 -- and resolved using this type, which can be any valid argument
1730 -- to Resolve, e.g. Any_Integer is OK). If not, give an error
1731 -- and raise Aspect_Exit. If Typ is left Empty, then any static
1732 -- expression is allowed. Includes checking that the expression
1733 -- does not raise Constraint_Error.
1735 function Directly_Specified
1736 (Id : Entity_Id; A : Aspect_Id) return Boolean;
1737 -- Returns True if the given aspect is directly (as opposed to
1738 -- via any form of inheritance) specified for the given entity.
1740 function Make_Aitem_Pragma
1741 (Pragma_Argument_Associations : List_Id;
1742 Pragma_Name : Name_Id) return Node_Id;
1743 -- This is a wrapper for Make_Pragma used for converting aspects
1744 -- to pragmas. It takes care of Sloc (set from Loc) and building
1745 -- the pragma identifier from the given name. In addition the
1746 -- flags Class_Present and Split_PPC are set from the aspect
1747 -- node, as well as Is_Ignored. This routine also sets the
1748 -- From_Aspect_Specification in the resulting pragma node to
1749 -- True, and sets Corresponding_Aspect to point to the aspect.
1750 -- The resulting pragma is assigned to Aitem.
1752 -------------------------------
1753 -- Analyze_Aspect_Convention --
1754 -------------------------------
1756 procedure Analyze_Aspect_Convention is
1765 -- Obtain all interfacing aspects that apply to the related
1768 Get_Interfacing_Aspects
1769 (Iface_Asp => Aspect,
1770 Conv_Asp => Dummy_1,
1777 -- The related entity is subject to aspect Export or Import.
1778 -- Do not process Convention now because it must be analysed
1779 -- as part of Export or Import.
1781 if Present (Expo) or else Present (Imp) then
1784 -- Otherwise Convention appears by itself
1787 -- The aspect specifies a particular convention
1789 if Present (Expr) then
1790 Conv := New_Copy_Tree (Expr);
1792 -- Otherwise assume convention Ada
1795 Conv := Make_Identifier (Loc, Name_Ada);
1799 -- pragma Convention (<Conv>, <E>);
1801 Aitem := Make_Aitem_Pragma
1802 (Pragma_Name => Name_Convention,
1803 Pragma_Argument_Associations => New_List (
1804 Make_Pragma_Argument_Association (Loc,
1805 Expression => Conv),
1806 Make_Pragma_Argument_Association (Loc,
1807 Expression => New_Occurrence_Of (E, Loc))));
1809 Decorate (Aspect, Aitem);
1810 Insert_Pragma (Aitem);
1812 end Analyze_Aspect_Convention;
1814 ---------------------------------------
1815 -- Analyze_Aspect_Disable_Controlled --
1816 ---------------------------------------
1818 procedure Analyze_Aspect_Disable_Controlled is
1820 -- The aspect applies only to controlled records
1822 if not (Ekind (E) = E_Record_Type
1823 and then Is_Controlled_Active (E))
1826 ("aspect % requires controlled record type", Aspect);
1830 -- Preanalyze the expression (if any) when the aspect resides
1831 -- in a generic unit.
1833 if Inside_A_Generic then
1834 if Present (Expr) then
1835 Preanalyze_And_Resolve (Expr, Any_Boolean);
1838 -- Otherwise the aspect resides in a nongeneric context
1841 -- A controlled record type loses its controlled semantics
1842 -- when the expression statically evaluates to True.
1844 if Present (Expr) then
1845 Analyze_And_Resolve (Expr, Any_Boolean);
1847 if Is_OK_Static_Expression (Expr) then
1848 if Is_True (Static_Boolean (Expr)) then
1849 Set_Disable_Controlled (E);
1852 -- Otherwise the expression is not static
1856 ("expression of aspect % must be static", Aspect);
1859 -- Otherwise the aspect appears without an expression and
1860 -- defaults to True.
1863 Set_Disable_Controlled (E);
1866 end Analyze_Aspect_Disable_Controlled;
1868 ----------------------------------
1869 -- Analyze_Aspect_Export_Import --
1870 ----------------------------------
1872 procedure Analyze_Aspect_Export_Import is
1880 -- Obtain all interfacing aspects that apply to the related
1883 Get_Interfacing_Aspects
1884 (Iface_Asp => Aspect,
1885 Conv_Asp => Dummy_1,
1892 -- The related entity cannot be subject to both aspects Export
1895 if Present (Expo) and then Present (Imp) then
1897 ("incompatible interfacing aspects given for &", E);
1898 Error_Msg_Sloc := Sloc (Expo);
1899 Error_Msg_N ("\aspect Export #", E);
1900 Error_Msg_Sloc := Sloc (Imp);
1901 Error_Msg_N ("\aspect Import #", E);
1904 -- A variable is most likely modified from the outside. Take
1905 -- the optimistic approach to avoid spurious errors.
1907 if Ekind (E) = E_Variable then
1908 Set_Never_Set_In_Source (E, False);
1911 -- Resolve the expression of an Import or Export here, and
1912 -- require it to be of type Boolean and static. This is not
1913 -- quite right, because in general this should be delayed,
1914 -- but that seems tricky for these, because normally Boolean
1915 -- aspects are replaced with pragmas at the freeze point in
1916 -- Make_Pragma_From_Boolean_Aspect.
1919 or else Is_True (Static_Boolean (Expr))
1921 if A_Id = Aspect_Import then
1922 Set_Has_Completion (E);
1923 Set_Is_Imported (E);
1925 -- An imported object cannot be explicitly initialized
1927 if Nkind (N) = N_Object_Declaration
1928 and then Present (Expression (N))
1931 ("imported entities cannot be initialized "
1932 & "(RM B.1(24))", Expression (N));
1936 pragma Assert (A_Id = Aspect_Export);
1937 Set_Is_Exported (E);
1940 -- Create the proper form of pragma Export or Import taking
1941 -- into account Conversion, External_Name, and Link_Name.
1943 Aitem := Build_Export_Import_Pragma (Aspect, E);
1945 -- Otherwise the expression is either False or erroneous. There
1946 -- is no corresponding pragma.
1951 end Analyze_Aspect_Export_Import;
1953 ---------------------------------------
1954 -- Analyze_Aspect_External_Link_Name --
1955 ---------------------------------------
1957 procedure Analyze_Aspect_External_Link_Name is
1965 -- Obtain all interfacing aspects that apply to the related
1968 Get_Interfacing_Aspects
1969 (Iface_Asp => Aspect,
1970 Conv_Asp => Dummy_1,
1977 -- Ensure that aspect External_Name applies to aspect Export or
1980 if A_Id = Aspect_External_Name then
1981 if No (Expo) and then No (Imp) then
1983 ("aspect External_Name requires aspect Import or "
1984 & "Export", Aspect);
1987 -- Otherwise ensure that aspect Link_Name applies to aspect
1988 -- Export or Import.
1991 pragma Assert (A_Id = Aspect_Link_Name);
1992 if No (Expo) and then No (Imp) then
1994 ("aspect Link_Name requires aspect Import or Export",
1998 end Analyze_Aspect_External_Link_Name;
2000 -----------------------------------------
2001 -- Analyze_Aspect_Implicit_Dereference --
2002 -----------------------------------------
2004 procedure Analyze_Aspect_Implicit_Dereference is
2006 if not Is_Type (E) or else not Has_Discriminants (E) then
2008 ("aspect must apply to a type with discriminants", Expr);
2010 elsif not Is_Entity_Name (Expr) then
2012 ("aspect must name a discriminant of current type", Expr);
2015 -- Discriminant type be an anonymous access type or an
2016 -- anonymous access to subprogram.
2018 -- Missing synchronized types???
2021 Disc : Entity_Id := First_Discriminant (E);
2023 while Present (Disc) loop
2024 if Chars (Expr) = Chars (Disc)
2025 and then Ekind (Etype (Disc)) in
2026 E_Anonymous_Access_Subprogram_Type |
2027 E_Anonymous_Access_Type
2029 Set_Has_Implicit_Dereference (E);
2030 Set_Has_Implicit_Dereference (Disc);
2034 Next_Discriminant (Disc);
2037 -- Error if no proper access discriminant
2039 if Present (Disc) then
2040 -- For a type extension, check whether parent has
2041 -- a reference discriminant, to verify that use is
2044 if Is_Derived_Type (E)
2045 and then Has_Discriminants (Etype (E))
2048 Parent_Disc : constant Entity_Id :=
2049 Get_Reference_Discriminant (Etype (E));
2051 if Present (Parent_Disc)
2052 and then Corresponding_Discriminant (Disc) /=
2056 ("reference discriminant does not match "
2057 & "discriminant of parent type", Expr);
2064 ("not an access discriminant of&", Expr, E);
2069 end Analyze_Aspect_Implicit_Dereference;
2071 -------------------------------------------
2072 -- Analyze_Aspect_Relaxed_Initialization --
2073 -------------------------------------------
2075 procedure Analyze_Aspect_Relaxed_Initialization is
2076 procedure Analyze_Relaxed_Parameter
2077 (Subp_Id : Entity_Id;
2079 Seen : in out Elist_Id);
2080 -- Analyze parameter that appears in the expression of the
2081 -- aspect Relaxed_Initialization.
2083 -------------------------------
2084 -- Analyze_Relaxed_Parameter --
2085 -------------------------------
2087 procedure Analyze_Relaxed_Parameter
2088 (Subp_Id : Entity_Id;
2090 Seen : in out Elist_Id)
2093 -- Set name of the aspect for error messages
2094 Error_Msg_Name_1 := Nam;
2096 -- The relaxed parameter is a formal parameter
2098 if Nkind (Param) in N_Identifier | N_Expanded_Name then
2102 Item : constant Entity_Id := Entity (Param);
2104 -- It must be a formal of the analyzed subprogram
2106 if Scope (Item) = Subp_Id then
2108 pragma Assert (Is_Formal (Item));
2110 -- It must not have scalar or access type
2112 if Is_Elementary_Type (Etype (Item)) then
2113 Error_Msg_N ("illegal aspect % item", Param);
2115 ("\item must not have elementary type", Param);
2118 -- Detect duplicated items
2120 if Contains (Seen, Item) then
2121 Error_Msg_N ("duplicate aspect % item", Param);
2123 Append_New_Elmt (Item, Seen);
2126 Error_Msg_N ("illegal aspect % item", Param);
2130 -- The relaxed parameter is the function's Result attribute
2132 elsif Is_Attribute_Result (Param) then
2136 Pref : constant Node_Id := Prefix (Param);
2140 Nkind (Pref) in N_Identifier | N_Expanded_Name
2142 Entity (Pref) = Subp_Id
2144 -- Function result must not have scalar or access
2147 if Is_Elementary_Type (Etype (Pref)) then
2148 Error_Msg_N ("illegal aspect % item", Param);
2150 ("\function result must not have elementary"
2154 -- Detect duplicated items
2156 if Contains (Seen, Subp_Id) then
2157 Error_Msg_N ("duplicate aspect % item", Param);
2159 Append_New_Elmt (Entity (Pref), Seen);
2163 Error_Msg_N ("illegal aspect % item", Param);
2167 Error_Msg_N ("illegal aspect % item", Param);
2169 end Analyze_Relaxed_Parameter;
2173 Seen : Elist_Id := No_Elist;
2174 -- Items that appear in the relaxed initialization aspect
2175 -- expression of a subprogram; for detecting duplicates.
2177 Restore_Scope : Boolean;
2178 -- Will be set to True if we need to restore the scope table
2179 -- after analyzing the aspect expression.
2181 Prev_Id : Entity_Id;
2183 -- Start of processing for Analyze_Aspect_Relaxed_Initialization
2186 -- Set name of the aspect for error messages
2187 Error_Msg_Name_1 := Nam;
2189 -- Annotation of a type; no aspect expression is allowed.
2190 -- For a private type, the aspect must be attached to the
2193 -- ??? Once the exact rule for this aspect is ready, we will
2194 -- likely reject concurrent types, etc., so let's keep the code
2195 -- for types and variable separate.
2197 if Is_First_Subtype (E) then
2198 Prev_Id := Incomplete_Or_Partial_View (E);
2199 if Present (Prev_Id) then
2201 -- Aspect may appear on the full view of an incomplete
2202 -- type because the incomplete declaration cannot have
2205 if Ekind (Prev_Id) = E_Incomplete_Type then
2208 Error_Msg_N ("aspect % must apply to partial view", N);
2211 elsif Present (Expr) then
2212 Error_Msg_N ("illegal aspect % expression", Expr);
2215 -- Annotation of a variable; no aspect expression is allowed
2217 elsif Ekind (E) = E_Variable then
2218 if Present (Expr) then
2219 Error_Msg_N ("illegal aspect % expression", Expr);
2222 -- Annotation of a constant; no aspect expression is allowed.
2223 -- For a deferred constant, the aspect must be attached to the
2226 elsif Ekind (E) = E_Constant then
2227 if Present (Incomplete_Or_Partial_View (E)) then
2229 ("aspect % must apply to deferred constant", N);
2231 elsif Present (Expr) then
2232 Error_Msg_N ("illegal aspect % expression", Expr);
2235 -- Annotation of a subprogram; aspect expression is required
2237 elsif Is_Subprogram_Or_Entry (E)
2238 or else Is_Generic_Subprogram (E)
2240 if Present (Expr) then
2242 -- If we analyze subprogram body that acts as its own
2243 -- spec, then the subprogram itself and its formals are
2244 -- already installed; otherwise, we need to install them,
2245 -- as they must be visible when analyzing the aspect
2248 if In_Open_Scopes (E) then
2249 Restore_Scope := False;
2251 Restore_Scope := True;
2254 -- Only formals of the subprogram itself can appear
2255 -- in Relaxed_Initialization aspect expression, not
2256 -- formals of the enclosing generic unit. (This is
2257 -- different than in Precondition or Depends aspects,
2258 -- where both kinds of formals are allowed.)
2260 Install_Formals (E);
2263 -- Aspect expression is either an aggregate with list of
2264 -- parameters (and possibly the Result attribute for a
2267 if Nkind (Expr) = N_Aggregate then
2269 -- Component associations in the aggregate must be a
2270 -- parameter name followed by a static boolean
2273 if Present (Component_Associations (Expr)) then
2276 First (Component_Associations (Expr));
2278 while Present (Assoc) loop
2279 if List_Length (Choices (Assoc)) = 1 then
2280 Analyze_Relaxed_Parameter
2281 (E, First (Choices (Assoc)), Seen);
2283 if Inside_A_Generic then
2284 Preanalyze_And_Resolve
2285 (Expression (Assoc), Any_Boolean);
2288 (Expression (Assoc), Any_Boolean);
2291 if not Is_OK_Static_Expression
2292 (Expression (Assoc))
2294 Error_Msg_Name_1 := Nam;
2296 ("expression of aspect % " &
2297 "must be static", Aspect);
2301 Error_Msg_Name_1 := Nam;
2303 ("illegal aspect % expression", Expr);
2310 -- Expressions of the aggregate are parameter names
2312 if Present (Expressions (Expr)) then
2314 Param : Node_Id := First (Expressions (Expr));
2317 while Present (Param) loop
2318 Analyze_Relaxed_Parameter (E, Param, Seen);
2324 -- Mark the aggregate expression itself as analyzed;
2325 -- its subexpressions were marked when they themselves
2328 Set_Analyzed (Expr);
2330 -- Otherwise, it is a single name of a subprogram
2331 -- parameter (or possibly the Result attribute for
2335 Analyze_Relaxed_Parameter (E, Expr, Seen);
2338 if Restore_Scope then
2342 Error_Msg_N ("missing expression for aspect %", N);
2346 Error_Msg_N ("inappropriate entity for aspect %", E);
2348 end Analyze_Aspect_Relaxed_Initialization;
2350 ---------------------------
2351 -- Analyze_Aspect_Static --
2352 ---------------------------
2354 procedure Analyze_Aspect_Static is
2355 function Has_Convention_Intrinsic (L : List_Id) return Boolean;
2356 -- Return True if L contains a pragma argument association
2357 -- node representing a convention Intrinsic.
2359 ------------------------------
2360 -- Has_Convention_Intrinsic --
2361 ------------------------------
2363 function Has_Convention_Intrinsic
2364 (L : List_Id) return Boolean
2366 Arg : Node_Id := First (L);
2368 while Present (Arg) loop
2369 if Nkind (Arg) = N_Pragma_Argument_Association
2370 and then Chars (Arg) = Name_Convention
2371 and then Chars (Expression (Arg)) = Name_Intrinsic
2380 end Has_Convention_Intrinsic;
2382 Is_Imported_Intrinsic : Boolean;
2385 if Ada_Version < Ada_2022 then
2386 Error_Msg_Ada_2022_Feature ("aspect %", Sloc (Aspect));
2390 Is_Imported_Intrinsic := Is_Imported (E)
2392 Has_Convention_Intrinsic
2393 (Pragma_Argument_Associations (Import_Pragma (E)));
2395 -- The aspect applies only to expression functions that
2396 -- statisfy the requirements for a static expression function
2397 -- (such as having an expression that is predicate-static) as
2398 -- well as Intrinsic imported functions as a -gnatX extension.
2400 if not Is_Expression_Function (E)
2402 not (All_Extensions_Allowed and then Is_Imported_Intrinsic)
2404 if All_Extensions_Allowed then
2406 ("aspect % requires intrinsic or expression function",
2409 elsif Is_Imported_Intrinsic then
2410 Error_Msg_GNAT_Extension
2411 ("aspect % on intrinsic function", Sloc (Aspect),
2412 Is_Core_Extension => True);
2416 ("aspect % requires expression function", Aspect);
2421 -- Ada 2022 (AI12-0075): Check that the function satisfies
2422 -- several requirements of static functions as specified in
2423 -- RM 6.8(5.1-5.8). Note that some of the requirements given
2424 -- there are checked elsewhere.
2427 -- The expression of the expression function must be a
2428 -- potentially static expression (RM 2022 6.8(3.2-3.4)).
2429 -- That's checked in Sem_Ch6.Analyze_Expression_Function.
2431 -- The function must not contain any calls to itself, which
2432 -- is checked in Sem_Res.Resolve_Call.
2434 -- Each formal must be of mode in and have a static subtype
2437 Formal : Entity_Id := First_Formal (E);
2439 while Present (Formal) loop
2440 if Ekind (Formal) /= E_In_Parameter then
2442 ("aspect % requires formals of mode IN",
2448 if not Is_Static_Subtype (Etype (Formal)) then
2450 ("aspect % requires formals with static subtypes",
2456 Next_Formal (Formal);
2460 -- The function's result subtype must be a static subtype
2462 if not Is_Static_Subtype (Etype (E)) then
2464 ("aspect % requires function with result of "
2465 & "a static subtype",
2471 -- Check that the function does not have any applicable
2472 -- precondition or postcondition expression.
2474 for Asp in Pre_Post_Aspects loop
2475 if Has_Aspect (E, Asp) then
2476 Error_Msg_Name_1 := Aspect_Names (Asp);
2478 ("aspect % is not allowed for a static "
2479 & "expression function",
2480 Find_Aspect (E, Asp));
2486 -- ??? Must check that "for result type R, if the
2487 -- function is a boundary entity for type R (see 7.3.2),
2488 -- no type invariant applies to type R; if R has a
2489 -- component type C, a similar rule applies to C."
2492 -- When the expression is present, it must be static. If it
2493 -- evaluates to True, the expression function is treated as
2494 -- a static function. Otherwise the aspect appears without
2495 -- an expression and defaults to True.
2497 if Present (Expr) then
2498 -- Preanalyze the expression when the aspect resides in a
2499 -- generic unit. (Is this generic-related code necessary
2500 -- for this aspect? It's modeled on what's done for aspect
2501 -- Disable_Controlled. ???)
2503 if Inside_A_Generic then
2504 Preanalyze_And_Resolve (Expr, Any_Boolean);
2506 -- Otherwise the aspect resides in a nongeneric context
2509 Analyze_And_Resolve (Expr, Any_Boolean);
2511 -- Error if the boolean expression is not static
2513 if not Is_OK_Static_Expression (Expr) then
2515 ("expression of aspect % must be static", Aspect);
2519 end Analyze_Aspect_Static;
2521 --------------------------
2522 -- Analyze_Aspect_Yield --
2523 --------------------------
2525 procedure Analyze_Aspect_Yield is
2526 Expr_Value : Boolean := False;
2529 -- Check valid entity for 'Yield
2531 if (Is_Subprogram (E)
2532 or else Is_Generic_Subprogram (E)
2533 or else Is_Entry (E))
2534 and then not Within_Protected_Type (E)
2538 elsif Within_Protected_Type (E) then
2540 ("aspect% not applicable to protected operation", Id);
2545 ("aspect% only applicable to subprogram and entry "
2546 & "declarations", Id);
2550 -- Evaluate its static expression (if available); otherwise it
2551 -- defaults to True.
2556 -- Otherwise it must have a static boolean expression
2559 if Inside_A_Generic then
2560 Preanalyze_And_Resolve (Expr, Any_Boolean);
2562 Analyze_And_Resolve (Expr, Any_Boolean);
2565 if Is_OK_Static_Expression (Expr) then
2566 if Is_True (Static_Boolean (Expr)) then
2571 ("expression of aspect % must be static", Aspect);
2576 Set_Has_Yield_Aspect (E);
2579 -- If the Yield aspect is specified for a dispatching
2580 -- subprogram that inherits the aspect, the specified
2581 -- value shall be confirming.
2584 and then Is_Dispatching_Operation (E)
2585 and then Present (Overridden_Operation (E))
2586 and then Has_Yield_Aspect (Overridden_Operation (E))
2587 /= Is_True (Static_Boolean (Expr))
2589 Error_Msg_N ("specification of inherited aspect% can only " &
2590 "confirm parent value", Id);
2592 end Analyze_Aspect_Yield;
2594 ----------------------------------------
2595 -- Check_Expr_Is_OK_Static_Expression --
2596 ----------------------------------------
2598 procedure Check_Expr_Is_OK_Static_Expression
2600 Typ : Entity_Id := Empty)
2603 if Present (Typ) then
2604 Analyze_And_Resolve (Expr, Typ);
2606 Analyze_And_Resolve (Expr);
2609 -- An expression cannot be considered static if its resolution
2610 -- failed or if it's erroneous. Stop the analysis of the
2613 if Etype (Expr) = Any_Type or else Error_Posted (Expr) then
2616 elsif Is_OK_Static_Expression (Expr) then
2619 -- Finally, we have a real error
2622 Error_Msg_Name_1 := Nam;
2623 Flag_Non_Static_Expr
2624 ("entity for aspect% must be a static expression",
2628 end Check_Expr_Is_OK_Static_Expression;
2630 ------------------------
2631 -- Directly_Specified --
2632 ------------------------
2634 function Directly_Specified
2635 (Id : Entity_Id; A : Aspect_Id) return Boolean
2637 Aspect_Spec : constant Node_Id := Find_Aspect (Id, A);
2639 return Present (Aspect_Spec) and then Entity (Aspect_Spec) = Id;
2640 end Directly_Specified;
2642 -----------------------
2643 -- Make_Aitem_Pragma --
2644 -----------------------
2646 function Make_Aitem_Pragma
2647 (Pragma_Argument_Associations : List_Id;
2648 Pragma_Name : Name_Id) return Node_Id
2650 Args : List_Id := Pragma_Argument_Associations;
2654 -- We should never get here if aspect was disabled
2656 pragma Assert (not Is_Disabled (Aspect));
2658 -- Certain aspects allow for an optional name or expression. Do
2659 -- not generate a pragma with empty argument association list.
2661 if No (Args) or else No (Expression (First (Args))) then
2669 Pragma_Argument_Associations => Args,
2670 Pragma_Identifier =>
2671 Make_Identifier (Sloc (Id), Pragma_Name),
2672 Class_Present => Class_Present (Aspect),
2673 Split_PPC => Split_PPC (Aspect));
2675 -- Set additional semantic fields
2677 if Is_Ignored (Aspect) then
2678 Set_Is_Ignored (Aitem);
2679 elsif Is_Checked (Aspect) then
2680 Set_Is_Checked (Aitem);
2683 Set_Corresponding_Aspect (Aitem, Aspect);
2684 Set_From_Aspect_Specification (Aitem);
2687 end Make_Aitem_Pragma;
2689 -- Start of processing for Analyze_One_Aspect
2692 -- Skip aspect if already analyzed, to avoid looping in some cases
2694 if Analyzed (Aspect) then
2698 -- Skip looking at aspect if it is totally disabled. Just mark it
2699 -- as such for later reference in the tree. This also sets the
2700 -- Is_Ignored and Is_Checked flags appropriately.
2702 Check_Applicable_Policy (Aspect);
2704 if Is_Disabled (Aspect) then
2708 -- Set the source location of expression, used in the case of
2709 -- a failed precondition/postcondition or invariant. Note that
2710 -- the source location of the expression is not usually the best
2711 -- choice here. For example, it gets located on the last AND
2712 -- keyword in a chain of boolean expressiond AND'ed together.
2713 -- It is best to put the message on the first character of the
2714 -- assertion, which is the effect of the First_Node call here.
2716 if Present (Expr) then
2717 Eloc := Sloc (First_Node (Expr));
2720 -- Check restriction No_Implementation_Aspect_Specifications
2722 if Implementation_Defined_Aspect (A_Id) then
2724 (No_Implementation_Aspect_Specifications, Aspect);
2727 -- Check restriction No_Specification_Of_Aspect
2729 Check_Restriction_No_Specification_Of_Aspect (Aspect);
2731 -- Mark aspect analyzed (actual analysis is delayed till later)
2733 Set_Analyzed (Aspect);
2734 Set_Entity (Aspect, E);
2736 -- Build the reference to E that will be used in the built pragmas
2738 Ent := New_Occurrence_Of (E, Sloc (Id));
2740 if A_Id in Aspect_Attach_Handler | Aspect_Interrupt_Handler then
2742 -- Treat the specification as a reference to the protected
2743 -- operation, which might otherwise appear unreferenced and
2744 -- generate spurious warnings.
2746 Generate_Reference (E, Id);
2749 -- Check for duplicate aspect. Note that the Comes_From_Source
2750 -- test allows duplicate Pre/Post's that we generate internally
2751 -- to escape being flagged here.
2753 if No_Duplicates_Allowed (A_Id) then
2755 while Anod /= Aspect loop
2756 if Comes_From_Source (Aspect)
2757 and then Same_Aspect (A_Id, Get_Aspect_Id (Anod))
2759 Error_Msg_Name_1 := Nam;
2760 Error_Msg_Sloc := Sloc (Anod);
2762 -- Case of same aspect specified twice
2764 if Class_Present (Anod) = Class_Present (Aspect) then
2765 if not Class_Present (Anod) then
2767 ("aspect% for & previously given#",
2771 ("aspect `%''Class` for & previously given#",
2781 -- Check some general restrictions on language defined aspects
2783 if not Implementation_Defined_Aspect (A_Id)
2784 or else A_Id in Aspect_Async_Readers
2785 | Aspect_Async_Writers
2786 | Aspect_Effective_Reads
2787 | Aspect_Effective_Writes
2788 | Aspect_Preelaborable_Initialization
2790 Error_Msg_Name_1 := Nam;
2792 -- Not allowed for renaming declarations. Examine the original
2793 -- node because a subprogram renaming may have been rewritten
2796 if Nkind (Original_Node (N)) in N_Renaming_Declaration then
2798 ("aspect % not allowed for renaming declaration",
2802 -- Not allowed for formal type declarations in previous
2803 -- versions of the language. Allowed for them only for
2804 -- shared variable control aspects.
2806 -- Original node is used in case expansion rewrote the node -
2807 -- as is the case with generic derived types.
2809 if Nkind (Original_Node (N)) = N_Formal_Type_Declaration then
2810 if Ada_Version < Ada_2022 then
2812 ("aspect % not allowed for formal type declaration",
2815 elsif A_Id not in Aspect_Atomic
2817 | Aspect_Independent
2818 | Aspect_Atomic_Components
2819 | Aspect_Independent_Components
2820 | Aspect_Volatile_Components
2821 | Aspect_Async_Readers
2822 | Aspect_Async_Writers
2823 | Aspect_Effective_Reads
2824 | Aspect_Effective_Writes
2825 | Aspect_Preelaborable_Initialization
2828 ("aspect % not allowed for formal type declaration",
2834 -- Copy expression for later processing by the procedures
2835 -- Check_Aspect_At_[Freeze_Point | End_Of_Declarations]
2837 -- The expression may be a subprogram name, and can
2838 -- be an operator name that appears as a string, but
2839 -- requires its own analysis procedure (see sem_ch6).
2841 if Nkind (Expr) = N_Operator_Symbol then
2842 Set_Entity (Id, Expr);
2844 Set_Entity (Id, New_Copy_Tree (Expr));
2847 -- Set Delay_Required as appropriate to aspect
2849 case Aspect_Delay (A_Id) is
2850 when Always_Delay =>
2851 -- For Boolean aspects, do not delay if no expression
2853 if A_Id in Boolean_Aspects | Library_Unit_Aspects then
2854 Delay_Required := Present (Expr);
2856 Delay_Required := True;
2860 Delay_Required := False;
2864 -- For Boolean aspects, do not delay if no expression except
2865 -- for Full_Access_Only because we need to process it after
2866 -- Volatile and Atomic, which can be independently delayed.
2868 if A_Id in Boolean_Aspects
2869 and then A_Id /= Aspect_Full_Access_Only
2872 Delay_Required := False;
2874 -- For non-Boolean aspects, if the expression has the form
2875 -- of an integer literal, then do not delay, since we know
2876 -- the value cannot change. This optimization catches most
2877 -- rep clause cases.
2879 elsif A_Id not in Boolean_Aspects
2880 and then Present (Expr)
2881 and then Nkind (Expr) = N_Integer_Literal
2883 Delay_Required := False;
2885 -- For Alignment and various Size aspects, do not delay for
2886 -- an attribute reference whose prefix is Standard, for
2887 -- example Standard'Maximum_Alignment or Standard'Word_Size.
2889 elsif A_Id in Aspect_Alignment
2890 | Aspect_Component_Size
2891 | Aspect_Object_Size
2894 and then Present (Expr)
2895 and then Nkind (Expr) = N_Attribute_Reference
2896 and then Nkind (Prefix (Expr)) = N_Identifier
2897 and then Chars (Prefix (Expr)) = Name_Standard
2899 Delay_Required := False;
2901 -- All other cases are delayed
2904 Delay_Required := True;
2905 Set_Has_Delayed_Rep_Aspects (E);
2911 and then (A_Id = Aspect_Stable_Properties
2912 or else A_Id = Aspect_Designated_Storage_Model
2913 or else A_Id = Aspect_Storage_Model_Type)
2914 -- ??? It seems like we should do this for all aspects, not
2915 -- just these, but that causes as-yet-undiagnosed regressions.
2918 Set_Has_Delayed_Aspects (E);
2919 Set_Is_Delayed_Aspect (Aspect);
2922 -- Check 13.1(9.2/5): A representation aspect of a subtype or type
2923 -- shall not be specified (whether by a representation item or an
2924 -- aspect_specification) before the type is completely defined
2927 if Is_Representation_Aspect (A_Id)
2928 and then Rep_Item_Too_Early (E, N)
2933 -- Processing based on specific aspect
2936 when Aspect_Unimplemented =>
2937 null; -- ??? temp for now
2939 -- No_Aspect should be impossible
2942 raise Program_Error;
2944 -- Case 1: Aspects corresponding to attribute definition
2950 | Aspect_Component_Size
2951 | Aspect_Constant_Indexing
2952 | Aspect_Default_Iterator
2953 | Aspect_Dispatching_Domain
2954 | Aspect_External_Tag
2957 | Aspect_Iterator_Element
2958 | Aspect_Machine_Radix
2959 | Aspect_Object_Size
2963 | Aspect_Scalar_Storage_Order
2964 | Aspect_Simple_Storage_Pool
2967 | Aspect_Storage_Pool
2968 | Aspect_Stream_Size
2970 | Aspect_Variable_Indexing
2973 -- Indexing aspects apply only to tagged type
2975 if A_Id in Aspect_Constant_Indexing
2976 | Aspect_Variable_Indexing
2977 and then not (Is_Type (E)
2978 and then Is_Tagged_Type (E))
2981 ("indexing aspect can only apply to a tagged type",
2986 -- For the case of aspect Address, we don't consider that we
2987 -- know the entity is never set in the source, since it is
2988 -- is likely aliasing is occurring.
2990 -- Note: one might think that the analysis of the resulting
2991 -- attribute definition clause would take care of that, but
2992 -- that's not the case since it won't be from source.
2994 if A_Id = Aspect_Address then
2995 Set_Never_Set_In_Source (E, False);
2998 -- Correctness of the profile of a stream operation is
2999 -- verified at the freeze point, but we must detect the
3000 -- illegal specification of this aspect for a subtype now,
3001 -- to prevent malformed rep_item chains.
3003 if A_Id in Aspect_Input
3008 if not Is_First_Subtype (E) then
3010 ("local name must be a first subtype", Aspect);
3013 -- If stream aspect applies to the class-wide type,
3014 -- the generated attribute definition applies to the
3015 -- class-wide type as well.
3017 elsif Class_Present (Aspect) then
3019 Make_Attribute_Reference (Loc,
3021 Attribute_Name => Name_Class);
3025 -- Construct the attribute_definition_clause. The expression
3026 -- in the aspect specification is simply shared with the
3027 -- constructed attribute, because it will be fully analyzed
3028 -- when the attribute is processed.
3031 Make_Attribute_Definition_Clause (Loc,
3034 Expression => Relocate_Expression (Expr));
3036 -- If the address is specified, then we treat the entity as
3037 -- referenced, to avoid spurious warnings. This is analogous
3038 -- to what is done with an attribute definition clause, but
3039 -- here we don't want to generate a reference because this
3040 -- is the point of definition of the entity.
3042 if A_Id = Aspect_Address then
3046 -- Case 2: Aspects corresponding to pragmas
3048 -- Case 2a: Aspects corresponding to pragmas with two
3049 -- arguments, where the first argument is a local name
3050 -- referring to the entity, and the second argument is the
3051 -- aspect definition expression.
3055 when Aspect_Linker_Section =>
3056 Aitem := Make_Aitem_Pragma
3057 (Pragma_Argument_Associations => New_List (
3058 Make_Pragma_Argument_Association (Loc,
3059 Expression => New_Occurrence_Of (E, Loc)),
3060 Make_Pragma_Argument_Association (Sloc (Expr),
3061 Expression => Relocate_Node (Expr))),
3062 Pragma_Name => Name_Linker_Section);
3064 -- Linker_Section does not need delaying, as its argument
3065 -- must be a static string. Furthermore, if applied to
3066 -- an object with an explicit initialization, the object
3067 -- must be frozen in order to elaborate the initialization
3068 -- code. (This is already done for types with implicit
3069 -- initialization, such as protected types.)
3071 if Nkind (N) = N_Object_Declaration
3072 and then Has_Init_Expression (N)
3074 Delay_Required := False;
3079 -- Corresponds to pragma Implemented, construct the pragma
3081 when Aspect_Synchronization =>
3082 Aitem := Make_Aitem_Pragma
3083 (Pragma_Argument_Associations => New_List (
3084 Make_Pragma_Argument_Association (Loc,
3085 Expression => New_Occurrence_Of (E, Loc)),
3086 Make_Pragma_Argument_Association (Sloc (Expr),
3087 Expression => Relocate_Node (Expr))),
3088 Pragma_Name => Name_Implemented);
3092 when Aspect_Attach_Handler =>
3093 Aitem := Make_Aitem_Pragma
3094 (Pragma_Argument_Associations => New_List (
3095 Make_Pragma_Argument_Association (Sloc (Ent),
3097 Make_Pragma_Argument_Association (Sloc (Expr),
3098 Expression => Relocate_Expression (Expr))),
3099 Pragma_Name => Name_Attach_Handler);
3101 -- We need to insert this pragma into the tree to get proper
3102 -- processing and to look valid from a placement viewpoint.
3104 Insert_Pragma (Aitem);
3107 -- Dynamic_Predicate, Predicate, Static_Predicate
3109 when Aspect_Dynamic_Predicate
3111 | Aspect_Static_Predicate
3113 -- These aspects apply only to subtypes
3115 if not Is_Type (E) then
3117 ("predicate can only be specified for a subtype",
3121 elsif Is_Incomplete_Type (E) then
3123 ("predicate cannot apply to incomplete view", Aspect);
3125 elsif Is_Generic_Type (E) then
3127 ("predicate cannot apply to formal type", Aspect);
3131 -- Construct the pragma (always a pragma Predicate, with
3132 -- flags recording whether it is static/dynamic). We also
3133 -- set flags recording this in the type itself.
3135 Aitem := Make_Aitem_Pragma
3136 (Pragma_Argument_Associations => New_List (
3137 Make_Pragma_Argument_Association (Sloc (Ent),
3139 Make_Pragma_Argument_Association (Sloc (Expr),
3140 Expression => Relocate_Expression (Expr))),
3141 Pragma_Name => Name_Predicate);
3143 -- Mark type has predicates, and remember what kind of
3144 -- aspect lead to this predicate (we need this to access
3145 -- the right set of check policies later on).
3147 Set_Has_Predicates (E);
3149 if A_Id = Aspect_Dynamic_Predicate then
3150 Set_Has_Dynamic_Predicate_Aspect (E);
3152 -- If the entity has a dynamic predicate, any inherited
3153 -- static predicate becomes dynamic as well, and the
3154 -- predicate function includes the conjunction of both.
3156 Set_Has_Static_Predicate_Aspect (E, False);
3158 elsif A_Id = Aspect_Static_Predicate then
3159 Set_Has_Static_Predicate_Aspect (E);
3162 -- If the type is private, indicate that its completion
3163 -- has a freeze node, because that is the one that will
3164 -- be visible at freeze time.
3166 if Is_Private_Type (E) and then Present (Full_View (E)) then
3167 Set_Has_Predicates (Full_View (E));
3169 if A_Id = Aspect_Dynamic_Predicate then
3170 Set_Has_Dynamic_Predicate_Aspect (Full_View (E));
3171 elsif A_Id = Aspect_Static_Predicate then
3172 Set_Has_Static_Predicate_Aspect (Full_View (E));
3175 Set_Has_Delayed_Aspects (Full_View (E));
3176 Ensure_Freeze_Node (Full_View (E));
3178 -- If there is an Underlying_Full_View, also create a
3179 -- freeze node for that one.
3181 if Is_Private_Type (Full_View (E)) then
3183 U_Full : constant Entity_Id :=
3184 Underlying_Full_View (Full_View (E));
3186 if Present (U_Full) then
3187 Set_Has_Delayed_Aspects (U_Full);
3188 Ensure_Freeze_Node (U_Full);
3194 -- Predicate_Failure
3196 when Aspect_Predicate_Failure =>
3198 -- This aspect applies only to subtypes
3200 if not Is_Type (E) then
3202 ("predicate can only be specified for a subtype",
3206 elsif Is_Incomplete_Type (E) then
3208 ("predicate cannot apply to incomplete view", Aspect);
3211 elsif not Has_Predicates (E) then
3213 ("Predicate_Failure requires previous predicate" &
3214 " specification", Aspect);
3217 elsif not (Directly_Specified (E, Aspect_Dynamic_Predicate)
3218 or else Directly_Specified (E, Aspect_Static_Predicate)
3219 or else Directly_Specified (E, Aspect_Predicate))
3222 ("Predicate_Failure requires accompanying" &
3223 " noninherited predicate specification", Aspect);
3227 -- Construct the pragma
3229 Aitem := Make_Aitem_Pragma
3230 (Pragma_Argument_Associations => New_List (
3231 Make_Pragma_Argument_Association (Sloc (Ent),
3233 Make_Pragma_Argument_Association (Sloc (Expr),
3234 Expression => Relocate_Node (Expr))),
3235 Pragma_Name => Name_Predicate_Failure);
3237 -- Case 2b: Aspects corresponding to pragmas with two
3238 -- arguments, where the second argument is a local name
3239 -- referring to the entity, and the first argument is the
3240 -- aspect definition expression.
3244 when Aspect_Convention =>
3245 Analyze_Aspect_Convention;
3248 -- External_Name, Link_Name
3250 when Aspect_External_Name
3253 Analyze_Aspect_External_Link_Name;
3256 -- CPU, Interrupt_Priority, Priority
3258 -- These three aspects can be specified for a subprogram spec
3259 -- or body, in which case we analyze the expression and export
3260 -- the value of the aspect.
3262 -- Previously, we generated an equivalent pragma for bodies
3263 -- (note that the specs cannot contain these pragmas). The
3264 -- pragma was inserted ahead of local declarations, rather than
3265 -- after the body. This leads to a certain duplication between
3266 -- the processing performed for the aspect and the pragma, but
3267 -- given the straightforward handling required it is simpler
3268 -- to duplicate than to translate the aspect in the spec into
3269 -- a pragma in the declarative part of the body.
3272 | Aspect_Interrupt_Priority
3275 -- Verify the expression is static when Static_Priorities is
3278 if not Is_OK_Static_Expression (Expr) then
3279 Check_Restriction (Static_Priorities, Expr);
3282 if Nkind (N) in N_Subprogram_Body | N_Subprogram_Declaration
3284 -- Analyze the aspect expression
3286 Analyze_And_Resolve (Expr, Standard_Integer);
3288 -- Interrupt_Priority aspect not allowed for main
3289 -- subprograms. RM D.1 does not forbid this explicitly,
3290 -- but RM J.15.11(6/3) does not permit pragma
3291 -- Interrupt_Priority for subprograms.
3293 if A_Id = Aspect_Interrupt_Priority then
3295 ("Interrupt_Priority aspect cannot apply to "
3296 & "subprogram", Expr);
3298 -- The expression must be static
3300 elsif not Is_OK_Static_Expression (Expr) then
3301 Flag_Non_Static_Expr
3302 ("aspect requires static expression!", Expr);
3304 -- Check whether this is the main subprogram. Issue a
3305 -- warning only if it is obviously not a main program
3306 -- (when it has parameters or when the subprogram is
3307 -- within a package).
3309 elsif Present (Parameter_Specifications
3310 (Specification (N)))
3311 or else not Is_Compilation_Unit (Defining_Entity (N))
3313 -- See RM D.1(14/3) and D.16(12/3)
3316 ("aspect applied to subprogram other than the "
3317 & "main subprogram has no effect??", Expr);
3319 -- Otherwise check in range and export the value
3321 -- For the CPU aspect
3323 elsif A_Id = Aspect_CPU then
3324 if Is_In_Range (Expr, RTE (RE_CPU_Range)) then
3326 -- Value is correct so we export the value to make
3327 -- it available at execution time.
3330 (Main_Unit, UI_To_Int (Expr_Value (Expr)));
3334 ("main subprogram 'C'P'U is out of range", Expr);
3337 -- For the Priority aspect
3339 elsif A_Id = Aspect_Priority then
3340 if Is_In_Range (Expr, RTE (RE_Priority)) then
3342 -- Value is correct so we export the value to make
3343 -- it available at execution time.
3346 (Main_Unit, UI_To_Int (Expr_Value (Expr)));
3348 -- Ignore pragma if Relaxed_RM_Semantics to support
3349 -- other targets/non GNAT compilers.
3351 elsif not Relaxed_RM_Semantics then
3353 ("main subprogram priority is out of range",
3358 -- Load an arbitrary entity from System.Tasking.Stages
3359 -- or System.Tasking.Restricted.Stages (depending on
3360 -- the supported profile) to make sure that one of these
3361 -- packages is implicitly with'ed, since we need to have
3362 -- the tasking run time active for the pragma Priority to
3363 -- have any effect. Previously we with'ed the package
3364 -- System.Tasking, but this package does not trigger the
3365 -- required initialization of the run-time library.
3367 if Restricted_Profile then
3368 Discard_Node (RTE (RE_Activate_Restricted_Tasks));
3370 Discard_Node (RTE (RE_Activate_Tasks));
3373 -- Handling for these aspects in subprograms is complete
3377 -- For task and protected types pass the aspect as an
3382 Make_Attribute_Definition_Clause (Loc,
3385 Expression => Relocate_Expression (Expr));
3388 -- Suppress/Unsuppress
3390 when Aspect_Suppress
3393 Aitem := Make_Aitem_Pragma
3394 (Pragma_Argument_Associations => New_List (
3395 Make_Pragma_Argument_Association (Loc,
3396 Expression => Relocate_Node (Expr)),
3397 Make_Pragma_Argument_Association (Sloc (Expr),
3398 Expression => New_Occurrence_Of (E, Loc))),
3399 Pragma_Name => Nam);
3401 Delay_Required := False;
3405 when Aspect_Warnings =>
3406 Aitem := Make_Aitem_Pragma
3407 (Pragma_Argument_Associations => New_List (
3408 Make_Pragma_Argument_Association (Sloc (Expr),
3409 Expression => Relocate_Node (Expr)),
3410 Make_Pragma_Argument_Association (Loc,
3411 Expression => New_Occurrence_Of (E, Loc))),
3412 Pragma_Name => Name_Warnings);
3414 Decorate (Aspect, Aitem);
3415 Insert_Pragma (Aitem);
3418 -- Case 2c: Aspects corresponding to pragmas with three
3421 -- Invariant aspects have a first argument that references the
3422 -- entity, a second argument that is the expression and a third
3423 -- argument that is an appropriate message.
3425 -- Invariant, Type_Invariant
3427 when Aspect_Invariant
3428 | Aspect_Type_Invariant
3430 -- Analysis of the pragma will verify placement legality:
3431 -- an invariant must apply to a private type, or appear in
3432 -- the private part of a spec and apply to a completion.
3434 Aitem := Make_Aitem_Pragma
3435 (Pragma_Argument_Associations => New_List (
3436 Make_Pragma_Argument_Association (Sloc (Ent),
3438 Make_Pragma_Argument_Association (Sloc (Expr),
3439 Expression => Relocate_Node (Expr))),
3440 Pragma_Name => Name_Invariant);
3442 -- Add message unless exception messages are suppressed
3444 if not Opt.Exception_Locations_Suppressed then
3445 Append_To (Pragma_Argument_Associations (Aitem),
3446 Make_Pragma_Argument_Association (Eloc,
3447 Chars => Name_Message,
3449 Make_String_Literal (Eloc,
3450 Strval => "failed invariant from "
3451 & Build_Location_String (Eloc))));
3454 -- For Invariant case, insert immediately after the entity
3455 -- declaration. We do not have to worry about delay issues
3456 -- since the pragma processing takes care of this.
3458 Delay_Required := False;
3460 -- Case 2d : Aspects that correspond to a pragma with one
3465 -- Aspect Abstract_State introduces implicit declarations for
3466 -- all state abstraction entities it defines. To emulate this
3467 -- behavior, insert the pragma at the beginning of the visible
3468 -- declarations of the related package so that it is analyzed
3471 when Aspect_Abstract_State => Abstract_State : declare
3472 Context : Node_Id := N;
3475 -- When aspect Abstract_State appears on a generic package,
3476 -- it is propagated to the package instance. The context in
3477 -- this case is the instance spec.
3479 if Nkind (Context) = N_Package_Instantiation then
3480 Context := Instance_Spec (Context);
3483 if Nkind (Context) in N_Generic_Package_Declaration
3484 | N_Package_Declaration
3486 Aitem := Make_Aitem_Pragma
3487 (Pragma_Argument_Associations => New_List (
3488 Make_Pragma_Argument_Association (Loc,
3489 Expression => Relocate_Node (Expr))),
3490 Pragma_Name => Name_Abstract_State);
3492 Decorate (Aspect, Aitem);
3496 Is_Generic_Instance (Defining_Entity (Context)));
3500 ("aspect & must apply to a package declaration",
3507 -- Aspect Async_Readers is never delayed because it is
3508 -- equivalent to a source pragma which appears after the
3509 -- related object declaration.
3511 when Aspect_Async_Readers =>
3512 Aitem := Make_Aitem_Pragma
3513 (Pragma_Argument_Associations => New_List (
3514 Make_Pragma_Argument_Association (Loc,
3515 Expression => Relocate_Node (Expr))),
3516 Pragma_Name => Name_Async_Readers);
3518 Decorate (Aspect, Aitem);
3519 Insert_Pragma (Aitem);
3522 -- Aspect Async_Writers is never delayed because it is
3523 -- equivalent to a source pragma which appears after the
3524 -- related object declaration.
3526 when Aspect_Async_Writers =>
3527 Aitem := Make_Aitem_Pragma
3528 (Pragma_Argument_Associations => New_List (
3529 Make_Pragma_Argument_Association (Loc,
3530 Expression => Relocate_Node (Expr))),
3531 Pragma_Name => Name_Async_Writers);
3533 Decorate (Aspect, Aitem);
3534 Insert_Pragma (Aitem);
3537 -- Aspect Constant_After_Elaboration is never delayed because
3538 -- it is equivalent to a source pragma which appears after the
3539 -- related object declaration.
3541 when Aspect_Constant_After_Elaboration =>
3542 Aitem := Make_Aitem_Pragma
3543 (Pragma_Argument_Associations => New_List (
3544 Make_Pragma_Argument_Association (Loc,
3545 Expression => Relocate_Node (Expr))),
3547 Name_Constant_After_Elaboration);
3549 Decorate (Aspect, Aitem);
3550 Insert_Pragma (Aitem);
3553 -- Aspect Default_Internal_Condition is never delayed because
3554 -- it is equivalent to a source pragma which appears after the
3555 -- related private type. To deal with forward references, the
3556 -- generated pragma is stored in the rep chain of the related
3557 -- private type as types do not carry contracts. The pragma is
3558 -- wrapped inside of a procedure at the freeze point of the
3559 -- private type's full view.
3561 -- A type entity argument is appended to facilitate inheriting
3562 -- the aspect from parent types (see Build_DIC_Procedure_Body),
3563 -- though that extra argument isn't documented for the pragma.
3565 when Aspect_Default_Initial_Condition =>
3566 Aitem := Make_Aitem_Pragma
3567 (Pragma_Argument_Associations => New_List (
3568 Make_Pragma_Argument_Association (Loc,
3569 Expression => Relocate_Node (Expr)),
3570 Make_Pragma_Argument_Association (Sloc (Ent),
3571 Expression => Ent)),
3573 Name_Default_Initial_Condition);
3575 Decorate (Aspect, Aitem);
3576 Insert_Pragma (Aitem);
3579 -- Default_Storage_Pool
3581 when Aspect_Default_Storage_Pool =>
3582 Aitem := Make_Aitem_Pragma
3583 (Pragma_Argument_Associations => New_List (
3584 Make_Pragma_Argument_Association (Loc,
3585 Expression => Relocate_Node (Expr))),
3587 Name_Default_Storage_Pool);
3589 Decorate (Aspect, Aitem);
3590 Insert_Pragma (Aitem);
3595 -- Aspect Depends is never delayed because it is equivalent to
3596 -- a source pragma which appears after the related subprogram.
3597 -- To deal with forward references, the generated pragma is
3598 -- stored in the contract of the related subprogram and later
3599 -- analyzed at the end of the declarative region. See routine
3600 -- Analyze_Depends_In_Decl_Part for details.
3602 when Aspect_Depends =>
3603 Aitem := Make_Aitem_Pragma
3604 (Pragma_Argument_Associations => New_List (
3605 Make_Pragma_Argument_Association (Loc,
3606 Expression => Relocate_Node (Expr))),
3607 Pragma_Name => Name_Depends);
3609 Decorate (Aspect, Aitem);
3610 Insert_Pragma (Aitem);
3613 -- Aspect Effective_Reads is never delayed because it is
3614 -- equivalent to a source pragma which appears after the
3615 -- related object declaration.
3617 when Aspect_Effective_Reads =>
3618 Aitem := Make_Aitem_Pragma
3619 (Pragma_Argument_Associations => New_List (
3620 Make_Pragma_Argument_Association (Loc,
3621 Expression => Relocate_Node (Expr))),
3622 Pragma_Name => Name_Effective_Reads);
3624 Decorate (Aspect, Aitem);
3625 Insert_Pragma (Aitem);
3628 -- Aspect Effective_Writes is never delayed because it is
3629 -- equivalent to a source pragma which appears after the
3630 -- related object declaration.
3632 when Aspect_Effective_Writes =>
3633 Aitem := Make_Aitem_Pragma
3634 (Pragma_Argument_Associations => New_List (
3635 Make_Pragma_Argument_Association (Loc,
3636 Expression => Relocate_Node (Expr))),
3637 Pragma_Name => Name_Effective_Writes);
3639 Decorate (Aspect, Aitem);
3640 Insert_Pragma (Aitem);
3643 -- Aspect Extensions_Visible is never delayed because it is
3644 -- equivalent to a source pragma which appears after the
3645 -- related subprogram.
3647 when Aspect_Extensions_Visible =>
3648 Aitem := Make_Aitem_Pragma
3649 (Pragma_Argument_Associations => New_List (
3650 Make_Pragma_Argument_Association (Loc,
3651 Expression => Relocate_Node (Expr))),
3652 Pragma_Name => Name_Extensions_Visible);
3654 Decorate (Aspect, Aitem);
3655 Insert_Pragma (Aitem);
3658 -- Aspect Ghost is never delayed because it is equivalent to a
3659 -- source pragma which appears at the top of [generic] package
3660 -- declarations or after an object, a [generic] subprogram, or
3661 -- a type declaration.
3663 when Aspect_Ghost =>
3664 Aitem := Make_Aitem_Pragma
3665 (Pragma_Argument_Associations => New_List (
3666 Make_Pragma_Argument_Association (Loc,
3667 Expression => Relocate_Node (Expr))),
3668 Pragma_Name => Name_Ghost);
3670 Decorate (Aspect, Aitem);
3671 Insert_Pragma (Aitem);
3676 -- Aspect Global is never delayed because it is equivalent to
3677 -- a source pragma which appears after the related subprogram.
3678 -- To deal with forward references, the generated pragma is
3679 -- stored in the contract of the related subprogram and later
3680 -- analyzed at the end of the declarative region. See routine
3681 -- Analyze_Global_In_Decl_Part for details.
3683 when Aspect_Global =>
3684 Aitem := Make_Aitem_Pragma
3685 (Pragma_Argument_Associations => New_List (
3686 Make_Pragma_Argument_Association (Loc,
3687 Expression => Relocate_Node (Expr))),
3688 Pragma_Name => Name_Global);
3690 Decorate (Aspect, Aitem);
3691 Insert_Pragma (Aitem);
3694 -- Initial_Condition
3696 -- Aspect Initial_Condition is never delayed because it is
3697 -- equivalent to a source pragma which appears after the
3698 -- related package. To deal with forward references, the
3699 -- generated pragma is stored in the contract of the related
3700 -- package and later analyzed at the end of the declarative
3701 -- region. See routine Analyze_Initial_Condition_In_Decl_Part
3704 when Aspect_Initial_Condition => Initial_Condition : declare
3705 Context : Node_Id := N;
3708 -- When aspect Initial_Condition appears on a generic
3709 -- package, it is propagated to the package instance. The
3710 -- context in this case is the instance spec.
3712 if Nkind (Context) = N_Package_Instantiation then
3713 Context := Instance_Spec (Context);
3716 if Nkind (Context) in N_Generic_Package_Declaration
3717 | N_Package_Declaration
3719 Aitem := Make_Aitem_Pragma
3720 (Pragma_Argument_Associations => New_List (
3721 Make_Pragma_Argument_Association (Loc,
3722 Expression => Relocate_Node (Expr))),
3724 Name_Initial_Condition);
3726 Decorate (Aspect, Aitem);
3730 Is_Generic_Instance (Defining_Entity (Context)));
3732 -- Otherwise the context is illegal
3736 ("aspect & must apply to a package declaration",
3741 end Initial_Condition;
3745 -- Aspect Initializes is never delayed because it is equivalent
3746 -- to a source pragma appearing after the related package. To
3747 -- deal with forward references, the generated pragma is stored
3748 -- in the contract of the related package and later analyzed at
3749 -- the end of the declarative region. For details, see routine
3750 -- Analyze_Initializes_In_Decl_Part.
3752 when Aspect_Initializes => Initializes : declare
3753 Context : Node_Id := N;
3756 -- When aspect Initializes appears on a generic package,
3757 -- it is propagated to the package instance. The context
3758 -- in this case is the instance spec.
3760 if Nkind (Context) = N_Package_Instantiation then
3761 Context := Instance_Spec (Context);
3764 if Nkind (Context) in N_Generic_Package_Declaration
3765 | N_Package_Declaration
3767 Aitem := Make_Aitem_Pragma
3768 (Pragma_Argument_Associations => New_List (
3769 Make_Pragma_Argument_Association (Loc,
3770 Expression => Relocate_Node (Expr))),
3771 Pragma_Name => Name_Initializes);
3773 Decorate (Aspect, Aitem);
3777 Is_Generic_Instance (Defining_Entity (Context)));
3779 -- Otherwise the context is illegal
3783 ("aspect & must apply to a package declaration",
3790 -- Max_Entry_Queue_Depth
3792 when Aspect_Max_Entry_Queue_Depth =>
3793 Aitem := Make_Aitem_Pragma
3794 (Pragma_Argument_Associations => New_List (
3795 Make_Pragma_Argument_Association (Loc,
3796 Expression => Relocate_Node (Expr))),
3797 Pragma_Name => Name_Max_Entry_Queue_Depth);
3799 Decorate (Aspect, Aitem);
3800 Insert_Pragma (Aitem);
3803 -- Max_Entry_Queue_Length
3805 when Aspect_Max_Entry_Queue_Length =>
3806 Aitem := Make_Aitem_Pragma
3807 (Pragma_Argument_Associations => New_List (
3808 Make_Pragma_Argument_Association (Loc,
3809 Expression => Relocate_Node (Expr))),
3810 Pragma_Name => Name_Max_Entry_Queue_Length);
3812 Decorate (Aspect, Aitem);
3813 Insert_Pragma (Aitem);
3818 when Aspect_Max_Queue_Length =>
3819 Aitem := Make_Aitem_Pragma
3820 (Pragma_Argument_Associations => New_List (
3821 Make_Pragma_Argument_Association (Loc,
3822 Expression => Relocate_Node (Expr))),
3823 Pragma_Name => Name_Max_Queue_Length);
3825 Decorate (Aspect, Aitem);
3826 Insert_Pragma (Aitem);
3829 -- Aspect No_Caching is never delayed because it is equivalent
3830 -- to a source pragma which appears after the related object
3833 when Aspect_No_Caching =>
3834 Aitem := Make_Aitem_Pragma
3835 (Pragma_Argument_Associations => New_List (
3836 Make_Pragma_Argument_Association (Loc,
3837 Expression => Relocate_Node (Expr))),
3838 Pragma_Name => Name_No_Caching);
3840 Decorate (Aspect, Aitem);
3841 Insert_Pragma (Aitem);
3844 -- No_Controlled_Parts, No_Task_Parts
3846 when Aspect_No_Controlled_Parts | Aspect_No_Task_Parts =>
3848 -- Check appropriate type argument
3850 if not Is_Type (E) then
3852 ("aspect % can only be applied to types", E);
3855 -- Disallow subtypes
3857 if Nkind (Declaration_Node (E)) = N_Subtype_Declaration then
3859 ("aspect % cannot be applied to subtypes", E);
3862 -- Resolve the expression to a boolean
3864 if Present (Expr) then
3865 Check_Expr_Is_OK_Static_Expression (Expr, Any_Boolean);
3872 when Aspect_Obsolescent => declare
3880 Make_Pragma_Argument_Association (Sloc (Expr),
3881 Expression => Relocate_Node (Expr)));
3884 Aitem := Make_Aitem_Pragma
3885 (Pragma_Argument_Associations => Args,
3886 Pragma_Name => Name_Obsolescent);
3891 when Aspect_Part_Of =>
3892 if Nkind (N) in N_Object_Declaration
3893 | N_Package_Instantiation
3894 or else Is_Single_Concurrent_Type_Declaration (N)
3896 Aitem := Make_Aitem_Pragma
3897 (Pragma_Argument_Associations => New_List (
3898 Make_Pragma_Argument_Association (Loc,
3899 Expression => Relocate_Node (Expr))),
3900 Pragma_Name => Name_Part_Of);
3902 Decorate (Aspect, Aitem);
3903 Insert_Pragma (Aitem);
3907 ("aspect & must apply to package instantiation, "
3908 & "object, single protected type or single task type",
3916 when Aspect_SPARK_Mode =>
3917 Aitem := Make_Aitem_Pragma
3918 (Pragma_Argument_Associations => New_List (
3919 Make_Pragma_Argument_Association (Loc,
3920 Expression => Relocate_Node (Expr))),
3921 Pragma_Name => Name_SPARK_Mode);
3923 Decorate (Aspect, Aitem);
3924 Insert_Pragma (Aitem);
3929 -- Aspect Refined_Depends is never delayed because it is
3930 -- equivalent to a source pragma which appears in the
3931 -- declarations of the related subprogram body. To deal with
3932 -- forward references, the generated pragma is stored in the
3933 -- contract of the related subprogram body and later analyzed
3934 -- at the end of the declarative region. For details, see
3935 -- routine Analyze_Refined_Depends_In_Decl_Part.
3937 when Aspect_Refined_Depends =>
3938 Aitem := Make_Aitem_Pragma
3939 (Pragma_Argument_Associations => New_List (
3940 Make_Pragma_Argument_Association (Loc,
3941 Expression => Relocate_Node (Expr))),
3942 Pragma_Name => Name_Refined_Depends);
3944 Decorate (Aspect, Aitem);
3945 Insert_Pragma (Aitem);
3950 -- Aspect Refined_Global is never delayed because it is
3951 -- equivalent to a source pragma which appears in the
3952 -- declarations of the related subprogram body. To deal with
3953 -- forward references, the generated pragma is stored in the
3954 -- contract of the related subprogram body and later analyzed
3955 -- at the end of the declarative region. For details, see
3956 -- routine Analyze_Refined_Global_In_Decl_Part.
3958 when Aspect_Refined_Global =>
3959 Aitem := Make_Aitem_Pragma
3960 (Pragma_Argument_Associations => New_List (
3961 Make_Pragma_Argument_Association (Loc,
3962 Expression => Relocate_Node (Expr))),
3963 Pragma_Name => Name_Refined_Global);
3965 Decorate (Aspect, Aitem);
3966 Insert_Pragma (Aitem);
3971 when Aspect_Refined_Post =>
3972 Aitem := Make_Aitem_Pragma
3973 (Pragma_Argument_Associations => New_List (
3974 Make_Pragma_Argument_Association (Loc,
3975 Expression => Relocate_Node (Expr))),
3976 Pragma_Name => Name_Refined_Post);
3978 Decorate (Aspect, Aitem);
3979 Insert_Pragma (Aitem);
3984 when Aspect_Refined_State =>
3986 -- The corresponding pragma for Refined_State is inserted in
3987 -- the declarations of the related package body. This action
3988 -- synchronizes both the source and from-aspect versions of
3991 if Nkind (N) = N_Package_Body then
3992 Aitem := Make_Aitem_Pragma
3993 (Pragma_Argument_Associations => New_List (
3994 Make_Pragma_Argument_Association (Loc,
3995 Expression => Relocate_Node (Expr))),
3996 Pragma_Name => Name_Refined_State);
3998 Decorate (Aspect, Aitem);
3999 Insert_Pragma (Aitem);
4001 -- Otherwise the context is illegal
4005 ("aspect & must apply to a package body", Aspect, Id);
4010 -- Relative_Deadline
4012 when Aspect_Relative_Deadline =>
4013 Aitem := Make_Aitem_Pragma
4014 (Pragma_Argument_Associations => New_List (
4015 Make_Pragma_Argument_Association (Loc,
4016 Expression => Relocate_Node (Expr))),
4017 Pragma_Name => Name_Relative_Deadline);
4019 -- If the aspect applies to a task, the corresponding pragma
4020 -- must appear within its declarations, not after.
4022 if Nkind (N) = N_Task_Type_Declaration then
4028 if No (Task_Definition (N)) then
4029 Set_Task_Definition (N,
4030 Make_Task_Definition (Loc,
4031 Visible_Declarations => New_List,
4032 End_Label => Empty));
4035 Def := Task_Definition (N);
4036 V := Visible_Declarations (Def);
4037 if not Is_Empty_List (V) then
4038 Insert_Before (First (V), Aitem);
4041 Set_Visible_Declarations (Def, New_List (Aitem));
4048 -- Relaxed_Initialization
4050 when Aspect_Relaxed_Initialization =>
4051 Analyze_Aspect_Relaxed_Initialization;
4054 -- Secondary_Stack_Size
4056 -- Aspect Secondary_Stack_Size needs to be converted into a
4057 -- pragma for two reasons: the attribute is not analyzed until
4058 -- after the expansion of the task type declaration and the
4059 -- attribute does not have visibility on the discriminant.
4061 when Aspect_Secondary_Stack_Size =>
4062 Aitem := Make_Aitem_Pragma
4063 (Pragma_Argument_Associations => New_List (
4064 Make_Pragma_Argument_Association (Loc,
4065 Expression => Relocate_Node (Expr))),
4067 Name_Secondary_Stack_Size);
4069 Decorate (Aspect, Aitem);
4070 Insert_Pragma (Aitem);
4073 -- Volatile_Function
4075 -- Aspect Volatile_Function is never delayed because it is
4076 -- equivalent to a source pragma which appears after the
4077 -- related subprogram.
4079 when Aspect_Volatile_Function =>
4080 Aitem := Make_Aitem_Pragma
4081 (Pragma_Argument_Associations => New_List (
4082 Make_Pragma_Argument_Association (Loc,
4083 Expression => Relocate_Node (Expr))),
4084 Pragma_Name => Name_Volatile_Function);
4086 Decorate (Aspect, Aitem);
4087 Insert_Pragma (Aitem);
4090 -- Case 2e: Annotate aspect
4092 when Aspect_Annotate | Aspect_GNAT_Annotate =>
4099 -- The argument can be a single identifier
4101 if Nkind (Expr) = N_Identifier then
4103 -- One level of parens is allowed
4105 if Paren_Count (Expr) > 1 then
4106 Error_Msg_F ("extra parentheses ignored", Expr);
4109 Set_Paren_Count (Expr, 0);
4111 -- Add the single item to the list
4113 Args := New_List (Expr);
4115 -- Otherwise we must have an aggregate
4117 elsif Nkind (Expr) = N_Aggregate then
4119 -- Must be positional
4121 if Present (Component_Associations (Expr)) then
4123 ("purely positional aggregate required", Expr);
4127 -- Must not be parenthesized
4129 if Paren_Count (Expr) /= 0 then
4130 Error_Msg_F -- CODEFIX
4131 ("redundant parentheses", Expr);
4134 -- List of arguments is list of aggregate expressions
4136 Args := Expressions (Expr);
4138 -- Anything else is illegal
4141 Error_Msg_F ("wrong form for Annotate aspect", Expr);
4145 -- Prepare pragma arguments
4148 Arg := First (Args);
4149 while Present (Arg) loop
4151 Make_Pragma_Argument_Association (Sloc (Arg),
4152 Expression => Relocate_Node (Arg)));
4157 Make_Pragma_Argument_Association (Sloc (Ent),
4158 Chars => Name_Entity,
4159 Expression => Ent));
4161 Aitem := Make_Aitem_Pragma
4162 (Pragma_Argument_Associations => Pargs,
4163 Pragma_Name => Name_Annotate);
4166 -- Case 3 : Aspects that don't correspond to pragma/attribute
4167 -- definition clause.
4169 -- Case 3a: The aspects listed below don't correspond to
4170 -- pragmas/attributes but do require delayed analysis.
4172 when Aspect_Default_Value | Aspect_Default_Component_Value =>
4173 Error_Msg_Name_1 := Nam;
4175 if not Is_Type (E) then
4176 Error_Msg_N ("aspect% can only apply to a type", Id);
4179 elsif not Is_First_Subtype (E) then
4180 Error_Msg_N ("aspect% cannot apply to subtype", Id);
4183 elsif A_Id = Aspect_Default_Value
4184 and then not Is_Scalar_Type (E)
4187 ("aspect% can only be applied to scalar type", Id);
4190 elsif A_Id = Aspect_Default_Component_Value then
4191 if not Is_Array_Type (E) then
4193 ("aspect% can only be applied to array type", Id);
4196 elsif not Is_Scalar_Type (Component_Type (E)) then
4197 Error_Msg_N ("aspect% requires scalar components", Id);
4204 when Aspect_Aggregate =>
4205 Validate_Aspect_Aggregate (Expr);
4206 Record_Rep_Item (E, Aspect);
4209 when Aspect_Stable_Properties =>
4210 Validate_Aspect_Stable_Properties
4211 (E, Expr, Class_Present => Class_Present (Aspect));
4212 Record_Rep_Item (E, Aspect);
4215 when Aspect_Designated_Storage_Model =>
4216 if not All_Extensions_Allowed then
4217 Error_Msg_GNAT_Extension ("aspect %", Sloc (Aspect));
4219 elsif not Is_Type (E)
4220 or else Ekind (E) /= E_Access_Type
4223 ("can only be specified for pool-specific access type",
4227 Record_Rep_Item (E, Aspect);
4230 when Aspect_Storage_Model_Type =>
4231 if not All_Extensions_Allowed then
4232 Error_Msg_GNAT_Extension ("aspect %", Sloc (Aspect));
4234 elsif not Is_Type (E)
4235 or else not Is_Immutably_Limited_Type (E)
4238 ("can only be specified for immutably limited type",
4242 Record_Rep_Item (E, Aspect);
4245 when Aspect_Integer_Literal
4246 | Aspect_Real_Literal
4247 | Aspect_String_Literal
4250 if not Is_First_Subtype (E) then
4252 ("may only be specified for a first subtype", Aspect);
4256 if Ada_Version < Ada_2022 then
4258 (No_Implementation_Aspect_Specifications, N);
4263 -- Case 3b: The aspects listed below don't correspond to
4264 -- pragmas/attributes and don't need delayed analysis.
4266 -- Implicit_Dereference
4268 -- For Implicit_Dereference, External_Name and Link_Name, only
4269 -- the legality checks are done during the analysis, thus no
4270 -- delay is required.
4272 when Aspect_Implicit_Dereference =>
4273 Analyze_Aspect_Implicit_Dereference;
4278 when Aspect_Dimension =>
4279 Analyze_Aspect_Dimension (N, Id, Expr);
4284 when Aspect_Dimension_System =>
4285 Analyze_Aspect_Dimension_System (N, Id, Expr);
4288 -- Case 4: Aspects requiring special handling
4290 -- Pre/Post/Test_Case/Contract_Cases/Subprogram_Variant whose
4291 -- corresponding pragmas take care of the delay.
4295 -- Aspects Pre/Post generate Precondition/Postcondition pragmas
4296 -- with a first argument that is the expression, and a second
4297 -- argument that is an informative message if the test fails.
4298 -- This is inserted right after the declaration, to get the
4299 -- required pragma placement. The processing for the pragmas
4300 -- takes care of the required delay.
4302 when Pre_Post_Aspects => Pre_Post : declare
4306 if A_Id in Aspect_Pre | Aspect_Precondition then
4307 Pname := Name_Precondition;
4309 Pname := Name_Postcondition;
4312 -- Check that the class-wide predicate cannot be applied to
4313 -- an operation of a synchronized type. AI12-0182 forbids
4314 -- these altogether, while earlier language semantics made
4315 -- them legal on tagged synchronized types.
4317 -- Other legality checks are performed when analyzing the
4318 -- contract of the operation.
4320 if Class_Present (Aspect)
4321 and then Is_Concurrent_Type (Current_Scope)
4322 and then Ekind (E) in E_Entry | E_Function | E_Procedure
4324 Error_Msg_Name_1 := Original_Aspect_Pragma_Name (Aspect);
4326 ("aspect % can only be specified for a primitive "
4327 & "operation of a tagged type", Aspect);
4332 -- Remember class-wide conditions; they will be merged
4333 -- with inherited conditions.
4335 if Class_Present (Aspect)
4336 and then A_Id in Aspect_Pre | Aspect_Post
4337 and then Is_Subprogram (E)
4338 and then not Is_Ignored_Ghost_Entity (E)
4340 if A_Id = Aspect_Pre then
4341 if Is_Ignored (Aspect) then
4342 Set_Ignored_Class_Preconditions (E,
4343 New_Copy_Tree (Expr));
4345 Set_Class_Preconditions (E, New_Copy_Tree (Expr));
4348 -- Postconditions may split into separate aspects, and we
4349 -- remember the expression before such split (i.e. when
4350 -- the first postcondition is processed).
4352 elsif No (Class_Postconditions (E))
4353 and then No (Ignored_Class_Postconditions (E))
4355 if Is_Ignored (Aspect) then
4356 Set_Ignored_Class_Postconditions (E,
4357 New_Copy_Tree (Expr));
4359 Set_Class_Postconditions (E, New_Copy_Tree (Expr));
4364 -- If the expressions is of the form A and then B, then
4365 -- we generate separate Pre/Post aspects for the separate
4366 -- clauses. Since we allow multiple pragmas, there is no
4367 -- problem in allowing multiple Pre/Post aspects internally.
4368 -- These should be treated in reverse order (B first and
4369 -- A second) since they are later inserted just after N in
4370 -- the order they are treated. This way, the pragma for A
4371 -- ends up preceding the pragma for B, which may have an
4372 -- importance for the error raised (either constraint error
4373 -- or precondition error).
4375 -- We do not do this for Pre'Class, since we have to put
4376 -- these conditions together in a complex OR expression.
4378 -- We don't do this in GNATprove mode, because it brings no
4379 -- benefit for proof and causes annoyance for flow analysis,
4380 -- which prefers to be as close to the original source code
4381 -- as possible. Also we don't do this when analyzing generic
4382 -- units since it causes spurious visibility errors in the
4383 -- preanalysis of instantiations.
4385 if not GNATprove_Mode
4386 and then (Pname = Name_Postcondition
4387 or else not Class_Present (Aspect))
4388 and then not Inside_A_Generic
4390 while Nkind (Expr) = N_And_Then loop
4391 Insert_After (Aspect,
4392 Make_Aspect_Specification (Sloc (Left_Opnd (Expr)),
4393 Identifier => Identifier (Aspect),
4394 Expression => Relocate_Node (Left_Opnd (Expr)),
4395 Class_Present => Class_Present (Aspect),
4396 Split_PPC => True));
4397 Rewrite (Expr, Relocate_Node (Right_Opnd (Expr)));
4398 Eloc := Sloc (Expr);
4402 -- Build the precondition/postcondition pragma
4404 Aitem := Make_Aitem_Pragma
4405 (Pragma_Argument_Associations => New_List (
4406 Make_Pragma_Argument_Association (Eloc,
4407 Chars => Name_Check,
4408 Expression => Relocate_Expression (Expr))),
4409 Pragma_Name => Pname);
4411 -- Add message unless exception messages are suppressed
4413 if not Opt.Exception_Locations_Suppressed then
4414 Append_To (Pragma_Argument_Associations (Aitem),
4415 Make_Pragma_Argument_Association (Eloc,
4416 Chars => Name_Message,
4418 Make_String_Literal (Eloc,
4420 & Get_Name_String (Pname)
4422 & Build_Location_String (Eloc))));
4425 Set_Is_Delayed_Aspect (Aspect);
4427 -- For Pre/Post cases, insert immediately after the entity
4428 -- declaration, since that is the required pragma placement.
4429 -- Note that for these aspects, we do not have to worry
4430 -- about delay issues, since the pragmas themselves deal
4431 -- with delay of visibility for the expression analysis.
4433 Insert_Pragma (Aitem);
4440 when Aspect_Test_Case => Test_Case : declare
4442 Comp_Expr : Node_Id;
4443 Comp_Assn : Node_Id;
4448 if Nkind (Parent (N)) = N_Compilation_Unit then
4449 Error_Msg_Name_1 := Nam;
4450 Error_Msg_N ("incorrect placement of aspect %", E);
4454 if Nkind (Expr) /= N_Aggregate
4455 or else Null_Record_Present (Expr)
4457 Error_Msg_Name_1 := Nam;
4459 ("wrong syntax for aspect % for &", Id, E);
4463 -- Check that the expression is a proper aggregate (no
4466 if Paren_Count (Expr) /= 0 then
4467 Error_Msg_F -- CODEFIX
4468 ("redundant parentheses", Expr);
4472 -- Create the list of arguments for building the Test_Case
4475 Comp_Expr := First (Expressions (Expr));
4476 while Present (Comp_Expr) loop
4478 Make_Pragma_Argument_Association (Sloc (Comp_Expr),
4479 Expression => Relocate_Node (Comp_Expr)));
4483 Comp_Assn := First (Component_Associations (Expr));
4484 while Present (Comp_Assn) loop
4485 if List_Length (Choices (Comp_Assn)) /= 1
4487 Nkind (First (Choices (Comp_Assn))) /= N_Identifier
4489 Error_Msg_Name_1 := Nam;
4491 ("wrong syntax for aspect % for &", Id, E);
4496 Make_Pragma_Argument_Association (Sloc (Comp_Assn),
4497 Chars => Chars (First (Choices (Comp_Assn))),
4499 Relocate_Node (Expression (Comp_Assn))));
4503 -- Build the test-case pragma
4505 Aitem := Make_Aitem_Pragma
4506 (Pragma_Argument_Associations => Args,
4507 Pragma_Name => Name_Test_Case);
4512 when Aspect_Contract_Cases =>
4513 Aitem := Make_Aitem_Pragma
4514 (Pragma_Argument_Associations => New_List (
4515 Make_Pragma_Argument_Association (Loc,
4516 Expression => Relocate_Node (Expr))),
4517 Pragma_Name => Name_Contract_Cases);
4519 Decorate (Aspect, Aitem);
4520 Insert_Pragma (Aitem);
4523 -- Subprogram_Variant
4525 when Aspect_Subprogram_Variant =>
4526 Aitem := Make_Aitem_Pragma
4527 (Pragma_Argument_Associations => New_List (
4528 Make_Pragma_Argument_Association (Loc,
4529 Expression => Relocate_Node (Expr))),
4530 Pragma_Name => Name_Subprogram_Variant);
4532 Decorate (Aspect, Aitem);
4533 Insert_Pragma (Aitem);
4536 -- Case 5: Special handling for aspects with an optional
4537 -- boolean argument.
4539 -- In the delayed case, the corresponding pragma cannot be
4540 -- generated yet because the evaluation of the boolean needs
4541 -- to be delayed till the freeze point.
4543 when Boolean_Aspects
4544 | Library_Unit_Aspects
4546 Set_Is_Boolean_Aspect (Aspect);
4548 -- Lock_Free aspect only apply to protected objects
4550 if A_Id = Aspect_Lock_Free then
4551 if Ekind (E) /= E_Protected_Type then
4552 Error_Msg_Name_1 := Nam;
4554 ("aspect % only applies to a protected type " &
4559 -- Set the Uses_Lock_Free flag to True if there is no
4560 -- expression or if the expression is True. The
4561 -- evaluation of this aspect should be delayed to the
4562 -- freeze point if we wanted to handle the corner case
4563 -- of "true" or "false" being redefined.
4566 or else Is_True (Static_Boolean (Expr))
4568 Set_Uses_Lock_Free (E);
4571 Record_Rep_Item (E, Aspect);
4576 elsif A_Id in Aspect_Export | Aspect_Import then
4577 Analyze_Aspect_Export_Import;
4579 -- Disable_Controlled
4581 elsif A_Id = Aspect_Disable_Controlled then
4582 Analyze_Aspect_Disable_Controlled;
4585 -- Ada 2022 (AI12-0129): Exclusive_Functions
4587 elsif A_Id = Aspect_Exclusive_Functions then
4588 if Ekind (E) /= E_Protected_Type then
4589 Error_Msg_Name_1 := Nam;
4591 ("aspect % only applies to a protected type " &
4598 -- Ada 2022 (AI12-0363): Full_Access_Only
4600 elsif A_Id = Aspect_Full_Access_Only then
4601 Error_Msg_Ada_2022_Feature ("aspect %", Sloc (Aspect));
4603 -- Ada 2022 (AI12-0075): static expression functions
4605 elsif A_Id = Aspect_Static then
4606 Analyze_Aspect_Static;
4609 -- Ada 2022 (AI12-0279)
4611 elsif A_Id = Aspect_Yield then
4612 Analyze_Aspect_Yield;
4616 -- Library unit aspects require special handling in the case
4617 -- of a package declaration, the pragma needs to be inserted
4618 -- in the list of declarations for the associated package.
4619 -- There is no issue of visibility delay for these aspects.
4621 if A_Id in Library_Unit_Aspects
4623 Nkind (N) in N_Package_Declaration
4624 | N_Generic_Package_Declaration
4625 and then Nkind (Parent (N)) /= N_Compilation_Unit
4627 -- Aspect is legal on a local instantiation of a library-
4628 -- level generic unit.
4630 and then not Is_Generic_Instance (Defining_Entity (N))
4633 ("incorrect context for library unit aspect&", Id);
4637 -- Cases where we do not delay
4639 if not Delay_Required then
4641 -- Exclude aspects Export and Import because their pragma
4642 -- syntax does not map directly to a Boolean aspect.
4644 if A_Id not in Aspect_Export | Aspect_Import then
4645 Aitem := Make_Aitem_Pragma
4646 (Pragma_Argument_Associations => New_List (
4647 Make_Pragma_Argument_Association (Sloc (Ent),
4648 Expression => Ent)),
4649 Pragma_Name => Nam);
4652 -- In general cases, the corresponding pragma/attribute
4653 -- definition clause will be inserted later at the freezing
4654 -- point, and we do not need to build it now.
4662 -- This is special because for access types we need to generate
4663 -- an attribute definition clause. This also works for single
4664 -- task declarations, but it does not work for task type
4665 -- declarations, because we have the case where the expression
4666 -- references a discriminant of the task type. That can't use
4667 -- an attribute definition clause because we would not have
4668 -- visibility on the discriminant. For that case we must
4669 -- generate a pragma in the task definition.
4671 when Aspect_Storage_Size =>
4675 if Ekind (E) = E_Task_Type then
4677 Decl : constant Node_Id := Declaration_Node (E);
4680 pragma Assert (Nkind (Decl) = N_Task_Type_Declaration);
4682 -- If no task definition, create one
4684 if No (Task_Definition (Decl)) then
4685 Set_Task_Definition (Decl,
4686 Make_Task_Definition (Loc,
4687 Visible_Declarations => Empty_List,
4688 End_Label => Empty));
4691 -- Create a pragma and put it at the start of the task
4692 -- definition for the task type declaration.
4694 Aitem := Make_Aitem_Pragma
4695 (Pragma_Argument_Associations => New_List (
4696 Make_Pragma_Argument_Association (Loc,
4697 Expression => Relocate_Node (Expr))),
4698 Pragma_Name => Name_Storage_Size);
4702 Visible_Declarations (Task_Definition (Decl)));
4706 -- All other cases, generate attribute definition
4710 Make_Attribute_Definition_Clause (Loc,
4712 Chars => Name_Storage_Size,
4713 Expression => Relocate_Node (Expr));
4717 -- Attach the corresponding pragma/attribute definition clause to
4718 -- the aspect specification node.
4720 if Present (Aitem) then
4721 Set_From_Aspect_Specification (Aitem);
4724 -- For an aspect that applies to a type, indicate whether it
4725 -- appears on a partial view of the type.
4728 and then Is_Private_Type (E)
4730 Set_Aspect_On_Partial_View (Aspect);
4733 -- In the context of a compilation unit, we directly put the
4734 -- pragma in the Pragmas_After list of the N_Compilation_Unit_Aux
4735 -- node (no delay is required here) except for aspects on a
4736 -- subprogram body (see below) and a generic package, for which we
4737 -- need to introduce the pragma before building the generic copy
4738 -- (see sem_ch12), and for package instantiations, where the
4739 -- library unit pragmas are better handled early.
4741 if Nkind (Parent (N)) = N_Compilation_Unit
4742 and then (Present (Aitem) or else Is_Boolean_Aspect (Aspect))
4745 Aux : constant Node_Id := Aux_Decls_Node (Parent (N));
4748 pragma Assert (Nkind (Aux) = N_Compilation_Unit_Aux);
4750 -- For a Boolean aspect, create the corresponding pragma if
4751 -- no expression or if the value is True.
4753 if Is_Boolean_Aspect (Aspect) and then No (Aitem) then
4754 if Is_True (Static_Boolean (Expr)) then
4755 Aitem := Make_Aitem_Pragma
4756 (Pragma_Argument_Associations => New_List (
4757 Make_Pragma_Argument_Association (Sloc (Ent),
4758 Expression => Ent)),
4759 Pragma_Name => Nam);
4761 Set_From_Aspect_Specification (Aitem, True);
4762 Set_Corresponding_Aspect (Aitem, Aspect);
4769 -- If the aspect is on a subprogram body (relevant aspect
4770 -- is Inline), add the pragma in front of the declarations.
4772 if Nkind (N) = N_Subprogram_Body then
4773 if No (Declarations (N)) then
4774 Set_Declarations (N, New_List);
4777 Prepend (Aitem, Declarations (N));
4779 elsif Nkind (N) = N_Generic_Package_Declaration then
4780 if No (Visible_Declarations (Specification (N))) then
4781 Set_Visible_Declarations (Specification (N), New_List);
4785 Visible_Declarations (Specification (N)));
4787 elsif Nkind (N) = N_Package_Instantiation then
4789 Spec : constant Node_Id :=
4790 Specification (Instance_Spec (N));
4792 if No (Visible_Declarations (Spec)) then
4793 Set_Visible_Declarations (Spec, New_List);
4796 Prepend (Aitem, Visible_Declarations (Spec));
4800 if No (Pragmas_After (Aux)) then
4801 Set_Pragmas_After (Aux, New_List);
4804 Append (Aitem, Pragmas_After (Aux));
4811 -- The evaluation of the aspect is delayed to the freezing point.
4812 -- The pragma or attribute clause if there is one is then attached
4813 -- to the aspect specification which is put in the rep item list.
4815 if Delay_Required then
4816 if Present (Aitem) then
4817 Set_Is_Delayed_Aspect (Aitem);
4818 Set_Aspect_Rep_Item (Aspect, Aitem);
4819 Set_Parent (Aitem, Aspect);
4822 Set_Is_Delayed_Aspect (Aspect);
4824 -- In the case of Default_Value, link the aspect to base type
4825 -- as well, even though it appears on a first subtype. This is
4826 -- mandated by the semantics of the aspect. Do not establish
4827 -- the link when processing the base type itself as this leads
4828 -- to a rep item circularity.
4830 if A_Id = Aspect_Default_Value and then Base_Type (E) /= E then
4831 Set_Has_Delayed_Aspects (Base_Type (E));
4832 Record_Rep_Item (Base_Type (E), Aspect);
4835 Set_Has_Delayed_Aspects (E);
4836 Record_Rep_Item (E, Aspect);
4838 -- When delay is not required and the context is a package or a
4839 -- subprogram body, insert the pragma in the body declarations.
4841 elsif Nkind (N) in N_Package_Body | N_Subprogram_Body then
4842 if No (Declarations (N)) then
4843 Set_Declarations (N, New_List);
4846 -- The pragma is added before source declarations
4848 Prepend_To (Declarations (N), Aitem);
4850 -- When delay is not required and the context is not a compilation
4851 -- unit, we simply insert the pragma/attribute definition clause
4854 elsif Present (Aitem) then
4855 Insert_After (Ins_Node, Aitem);
4861 -- If a nonoverridable aspect is explicitly specified for a
4862 -- derived type, then check consistency with the parent type.
4864 if A_Id in Nonoverridable_Aspect_Id
4865 and then Nkind (N) = N_Full_Type_Declaration
4866 and then Nkind (Type_Definition (N)) = N_Derived_Type_Definition
4867 and then not In_Instance_Body
4870 Parent_Type : constant Entity_Id := Etype (E);
4871 Inherited_Aspect : constant Node_Id :=
4872 Find_Aspect (Parent_Type, A_Id);
4874 if Present (Inherited_Aspect)
4875 and then not Is_Confirming
4876 (A_Id, Inherited_Aspect, Aspect)
4878 Error_Msg_Name_1 := Aspect_Names (A_Id);
4879 Error_Msg_Sloc := Sloc (Inherited_Aspect);
4882 ("overriding aspect specification for "
4883 & "nonoverridable aspect % does not confirm "
4884 & "aspect specification inherited from #",
4890 when Aspect_Exit => null;
4891 end Analyze_One_Aspect;
4894 end loop Aspect_Loop;
4896 if Has_Delayed_Aspects (E) then
4897 Ensure_Freeze_Node (E);
4899 end Analyze_Aspect_Specifications;
4901 ------------------------------------------------
4902 -- Analyze_Aspects_On_Subprogram_Body_Or_Stub --
4903 ------------------------------------------------
4905 procedure Analyze_Aspects_On_Subprogram_Body_Or_Stub (N : Node_Id) is
4906 Body_Id : constant Entity_Id := Defining_Entity (N);
4908 procedure Diagnose_Misplaced_Aspects (Spec_Id : Entity_Id);
4909 -- Body [stub] N has aspects, but they are not properly placed. Emit an
4910 -- error message depending on the aspects involved. Spec_Id denotes the
4911 -- entity of the corresponding spec.
4913 --------------------------------
4914 -- Diagnose_Misplaced_Aspects --
4915 --------------------------------
4917 procedure Diagnose_Misplaced_Aspects (Spec_Id : Entity_Id) is
4918 procedure Misplaced_Aspect_Error
4921 -- Emit an error message concerning misplaced aspect Asp. Ref_Nam is
4922 -- the name of the refined version of the aspect.
4924 ----------------------------
4925 -- Misplaced_Aspect_Error --
4926 ----------------------------
4928 procedure Misplaced_Aspect_Error
4932 Asp_Nam : constant Name_Id := Chars (Identifier (Asp));
4933 Asp_Id : constant Aspect_Id := Get_Aspect_Id (Asp_Nam);
4936 -- The corresponding spec already contains the aspect in question
4937 -- and the one appearing on the body must be the refined form:
4939 -- procedure P with Global ...;
4940 -- procedure P with Global ... is ... end P;
4944 if Has_Aspect (Spec_Id, Asp_Id) then
4945 Error_Msg_Name_1 := Asp_Nam;
4947 -- Subunits cannot carry aspects that apply to a subprogram
4950 if Nkind (Parent (N)) = N_Subunit then
4951 Error_Msg_N ("aspect % cannot apply to a subunit", Asp);
4953 -- Otherwise suggest the refined form
4956 Error_Msg_Name_2 := Ref_Nam;
4957 Error_Msg_N ("aspect % should be %", Asp);
4960 -- Otherwise the aspect must appear on the spec, not on the body
4963 -- procedure P with Global ... is ... end P;
4967 ("aspect specification must appear on initial declaration",
4970 end Misplaced_Aspect_Error;
4977 -- Start of processing for Diagnose_Misplaced_Aspects
4980 -- Iterate over the aspect specifications and emit specific errors
4981 -- where applicable.
4983 Asp := First (Aspect_Specifications (N));
4984 while Present (Asp) loop
4985 Asp_Nam := Chars (Identifier (Asp));
4987 -- Do not emit errors on aspects that can appear on a subprogram
4988 -- body. This scenario occurs when the aspect specification list
4989 -- contains both misplaced and properly placed aspects.
4991 if Aspect_On_Body_Or_Stub_OK (Get_Aspect_Id (Asp_Nam)) then
4994 -- Special diagnostics for SPARK aspects
4996 elsif Asp_Nam = Name_Depends then
4997 Misplaced_Aspect_Error (Asp, Name_Refined_Depends);
4999 elsif Asp_Nam = Name_Global then
5000 Misplaced_Aspect_Error (Asp, Name_Refined_Global);
5002 elsif Asp_Nam = Name_Post then
5003 Misplaced_Aspect_Error (Asp, Name_Refined_Post);
5005 -- Otherwise a language-defined aspect is misplaced
5009 ("aspect specification must appear on initial declaration",
5015 end Diagnose_Misplaced_Aspects;
5019 Spec_Id : constant Entity_Id := Unique_Defining_Entity (N);
5021 -- Start of processing for Analyze_Aspects_On_Subprogram_Body_Or_Stub
5024 -- Language-defined aspects cannot be associated with a subprogram body
5025 -- [stub] if the subprogram has a spec. Certain implementation defined
5026 -- aspects are allowed to break this rule (for all applicable cases, see
5027 -- table Aspects.Aspect_On_Body_Or_Stub_OK).
5029 if Spec_Id /= Body_Id and then not Aspects_On_Body_Or_Stub_OK (N) then
5030 Diagnose_Misplaced_Aspects (Spec_Id);
5032 Analyze_Aspect_Specifications (N, Body_Id);
5034 end Analyze_Aspects_On_Subprogram_Body_Or_Stub;
5036 -----------------------
5037 -- Analyze_At_Clause --
5038 -----------------------
5040 -- An at clause is replaced by the corresponding Address attribute
5041 -- definition clause that is the preferred approach in Ada 95.
5043 procedure Analyze_At_Clause (N : Node_Id) is
5044 CS : constant Boolean := Comes_From_Source (N);
5047 -- This is an obsolescent feature
5049 Check_Restriction (No_Obsolescent_Features, N);
5051 if Warn_On_Obsolescent_Feature then
5053 ("?j?at clause is an obsolescent feature (RM J.7(2))", N);
5055 ("\?j?use address attribute definition clause instead", N);
5058 -- Rewrite as address clause
5061 Make_Attribute_Definition_Clause (Sloc (N),
5062 Name => Identifier (N),
5063 Chars => Name_Address,
5064 Expression => Expression (N)));
5066 -- We preserve Comes_From_Source, since logically the clause still comes
5067 -- from the source program even though it is changed in form.
5069 Set_Comes_From_Source (N, CS);
5071 -- Analyze rewritten clause
5073 Analyze_Attribute_Definition_Clause (N);
5074 end Analyze_At_Clause;
5076 -----------------------------------------
5077 -- Analyze_Attribute_Definition_Clause --
5078 -----------------------------------------
5080 procedure Analyze_Attribute_Definition_Clause (N : Node_Id) is
5081 Loc : constant Source_Ptr := Sloc (N);
5082 Nam : constant Node_Id := Name (N);
5083 Attr : constant Name_Id := Chars (N);
5084 Expr : constant Node_Id := Expression (N);
5085 Id : constant Attribute_Id := Get_Attribute_Id (Attr);
5088 -- The entity of Nam after it is analyzed. In the case of an incomplete
5089 -- type, this is the underlying type.
5092 -- The underlying entity to which the attribute applies. Generally this
5093 -- is the Underlying_Type of Ent, except in the case where the clause
5094 -- applies to the full view of an incomplete or private type, in which
5095 -- case U_Ent is just a copy of Ent.
5097 FOnly : Boolean := False;
5098 -- Reset to True for subtype specific attribute (Alignment, Size)
5099 -- and for stream attributes, i.e. those cases where in the call to
5100 -- Rep_Item_Too_Late, FOnly is set True so that only the freezing rules
5101 -- are checked. Note that the case of stream attributes is not clear
5102 -- from the RM, but see AI95-00137. Also, the RM seems to disallow
5103 -- Storage_Size for derived task types, but that is also clearly
5106 procedure Analyze_Put_Image_TSS_Definition;
5108 procedure Analyze_Stream_TSS_Definition (TSS_Nam : TSS_Name_Type);
5109 -- Common processing for 'Read, 'Write, 'Input and 'Output attribute
5110 -- definition clauses.
5112 function Duplicate_Clause return Boolean;
5113 -- This routine checks if the aspect for U_Ent being given by attribute
5114 -- definition clause N is for an aspect that has already been specified,
5115 -- and if so gives an error message. If there is a duplicate, True is
5116 -- returned, otherwise there is no error, and False is returned. Size
5117 -- and Value_Size are considered to conflict, but for compatibility,
5118 -- this is merely a warning.
5120 procedure Check_Indexing_Functions;
5121 -- Check that the function in Constant_Indexing or Variable_Indexing
5122 -- attribute has the proper type structure. If the name is overloaded,
5123 -- check that some interpretation is legal.
5125 procedure Check_Iterator_Functions;
5126 -- Check that there is a single function in Default_Iterator attribute
5127 -- that has the proper type structure.
5129 function Check_Primitive_Function (Subp : Entity_Id) return Boolean;
5130 -- Common legality check for the previous two
5132 -----------------------------------
5133 -- Analyze_Put_Image_TSS_Definition --
5134 -----------------------------------
5136 procedure Analyze_Put_Image_TSS_Definition is
5137 Subp : Entity_Id := Empty;
5142 function Has_Good_Profile
5144 Report : Boolean := False) return Boolean;
5145 -- Return true if the entity is a subprogram with an appropriate
5146 -- profile for the attribute being defined. If result is False and
5147 -- Report is True, function emits appropriate error.
5149 ----------------------
5150 -- Has_Good_Profile --
5151 ----------------------
5153 function Has_Good_Profile
5155 Report : Boolean := False) return Boolean
5161 if Ekind (Subp) /= E_Procedure then
5165 F := First_Formal (Subp);
5171 if Base_Type (Etype (F))
5172 /= Class_Wide_Type (RTE (RE_Root_Buffer_Type))
5176 ("wrong type for Put_Image procedure''s first parameter",
5177 Parameter_Type (Parent (F)));
5183 if Parameter_Mode (F) /= E_In_Out_Parameter then
5186 ("wrong mode for Put_Image procedure''s first parameter",
5197 -- Verify that the prefix of the attribute and the local name for
5198 -- the type of the formal match.
5200 if Base_Type (Typ) /= Base_Type (Ent) then
5203 ("wrong type for Put_Image procedure''s second parameter",
5204 Parameter_Type (Parent (F)));
5210 if Parameter_Mode (F) /= E_In_Parameter then
5213 ("wrong mode for Put_Image procedure''s second parameter",
5220 if Present (Next_Formal (F)) then
5225 end Has_Good_Profile;
5227 -- Start of processing for Analyze_Put_Image_TSS_Definition
5230 if not Is_Type (U_Ent) then
5231 Error_Msg_N ("local name must be a subtype", Nam);
5234 elsif not Is_First_Subtype (U_Ent) then
5235 Error_Msg_N ("local name must be a first subtype", Nam);
5239 Pnam := TSS (Base_Type (U_Ent), TSS_Put_Image);
5241 -- If Pnam is present, it can be either inherited from an ancestor
5242 -- type (in which case it is legal to redefine it for this type), or
5243 -- be a previous definition of the attribute for the same type (in
5244 -- which case it is illegal).
5246 -- In the first case, it will have been analyzed already, and we can
5247 -- check that its profile does not match the expected profile for the
5248 -- Put_Image attribute of U_Ent. In the second case, either Pnam has
5249 -- been analyzed (and has the expected profile), or it has not been
5250 -- analyzed yet (case of a type that has not been frozen yet and for
5251 -- which Put_Image has been set using Set_TSS).
5254 and then (No (First_Entity (Pnam)) or else Has_Good_Profile (Pnam))
5256 Error_Msg_Sloc := Sloc (Pnam);
5257 Error_Msg_Name_1 := Attr;
5258 Error_Msg_N ("% attribute already defined #", Nam);
5264 if Is_Entity_Name (Expr) then
5265 if not Is_Overloaded (Expr) then
5266 if Has_Good_Profile (Entity (Expr), Report => True) then
5267 Subp := Entity (Expr);
5271 Get_First_Interp (Expr, I, It);
5272 while Present (It.Nam) loop
5273 if Has_Good_Profile (It.Nam) then
5278 Get_Next_Interp (I, It);
5283 if Present (Subp) then
5284 if Is_Abstract_Subprogram (Subp) then
5285 Error_Msg_N ("Put_Image subprogram must not be abstract", Expr);
5289 Set_Entity (Expr, Subp);
5290 Set_Etype (Expr, Etype (Subp));
5292 New_Put_Image_Subprogram (N, U_Ent, Subp);
5295 Error_Msg_Name_1 := Attr;
5296 Error_Msg_N ("incorrect expression for% attribute", Expr);
5298 end Analyze_Put_Image_TSS_Definition;
5300 -----------------------------------
5301 -- Analyze_Stream_TSS_Definition --
5302 -----------------------------------
5304 procedure Analyze_Stream_TSS_Definition (TSS_Nam : TSS_Name_Type) is
5305 Subp : Entity_Id := Empty;
5310 Is_Read : constant Boolean := (TSS_Nam = TSS_Stream_Read);
5311 -- True for Read attribute, False for other attributes
5313 function Has_Good_Profile
5315 Report : Boolean := False) return Boolean;
5316 -- Return true if the entity is a subprogram with an appropriate
5317 -- profile for the attribute being defined. If result is False and
5318 -- Report is True, function emits appropriate error.
5320 ----------------------
5321 -- Has_Good_Profile --
5322 ----------------------
5324 function Has_Good_Profile
5326 Report : Boolean := False) return Boolean
5328 Expected_Ekind : constant array (Boolean) of Entity_Kind :=
5329 (False => E_Procedure, True => E_Function);
5330 Is_Function : constant Boolean := (TSS_Nam = TSS_Stream_Input);
5335 if Ekind (Subp) /= Expected_Ekind (Is_Function) then
5339 F := First_Formal (Subp);
5342 or else Ekind (Etype (F)) /= E_Anonymous_Access_Type
5343 or else Base_Type (Designated_Type (Etype (F))) /=
5344 Class_Wide_Type (RTE (RE_Root_Stream_Type))
5349 if not Is_Function then
5353 Expected_Mode : constant array (Boolean) of Entity_Kind :=
5354 (False => E_In_Parameter,
5355 True => E_Out_Parameter);
5357 if Parameter_Mode (F) /= Expected_Mode (Is_Read) then
5365 Typ := Etype (Subp);
5368 -- Verify that the prefix of the attribute and the local name for
5369 -- the type of the formal match.
5371 if Base_Type (Typ) /= Base_Type (Ent) then
5375 if Present (Next_Formal (F)) then
5378 elsif not Is_Scalar_Type (Typ)
5379 and then not Is_First_Subtype (Typ)
5380 and then not Is_Class_Wide_Type (Typ)
5382 if Report and not Is_First_Subtype (Typ) then
5384 ("subtype of formal in stream operation must be a first "
5385 & "subtype", Parameter_Type (Parent (F)));
5393 end Has_Good_Profile;
5395 -- Start of processing for Analyze_Stream_TSS_Definition
5400 if not Is_Type (U_Ent) then
5401 Error_Msg_N ("local name must be a subtype", Nam);
5404 elsif not Is_First_Subtype (U_Ent) then
5405 Error_Msg_N ("local name must be a first subtype", Nam);
5409 Pnam := TSS (Base_Type (U_Ent), TSS_Nam);
5411 -- If Pnam is present, it can be either inherited from an ancestor
5412 -- type (in which case it is legal to redefine it for this type), or
5413 -- be a previous definition of the attribute for the same type (in
5414 -- which case it is illegal).
5416 -- In the first case, it will have been analyzed already, and we
5417 -- can check that its profile does not match the expected profile
5418 -- for a stream attribute of U_Ent. In the second case, either Pnam
5419 -- has been analyzed (and has the expected profile), or it has not
5420 -- been analyzed yet (case of a type that has not been frozen yet
5421 -- and for which the stream attribute has been set using Set_TSS).
5424 and then (No (First_Entity (Pnam)) or else Has_Good_Profile (Pnam))
5426 Error_Msg_Sloc := Sloc (Pnam);
5427 Error_Msg_Name_1 := Attr;
5428 Error_Msg_N ("% attribute already defined #", Nam);
5434 if Is_Entity_Name (Expr) then
5435 if not Is_Overloaded (Expr) then
5436 if Has_Good_Profile (Entity (Expr), Report => True) then
5437 Subp := Entity (Expr);
5441 Get_First_Interp (Expr, I, It);
5442 while Present (It.Nam) loop
5443 if Has_Good_Profile (It.Nam) then
5448 Get_Next_Interp (I, It);
5453 if Present (Subp) then
5454 if Is_Abstract_Subprogram (Subp) then
5455 Error_Msg_N ("stream subprogram must not be abstract", Expr);
5458 -- A stream subprogram for an interface type must be a null
5459 -- procedure (RM 13.13.2 (38/3)). Note that the class-wide type
5460 -- of an interface is not an interface type (3.9.4 (6.b/2)).
5462 elsif Is_Interface (U_Ent)
5463 and then not Is_Class_Wide_Type (U_Ent)
5464 and then not Inside_A_Generic
5466 (Ekind (Subp) = E_Function
5470 (Unit_Declaration_Node (Ultimate_Alias (Subp)))))
5473 ("stream subprogram for interface type must be null "
5474 & "procedure", Expr);
5477 Set_Entity (Expr, Subp);
5478 Set_Etype (Expr, Etype (Subp));
5480 New_Stream_Subprogram (N, U_Ent, Subp, TSS_Nam);
5483 Error_Msg_Name_1 := Attr;
5485 if Is_Class_Wide_Type (Base_Type (Ent)) then
5487 ("incorrect expression for class-wide% attribute", Expr);
5489 Error_Msg_N ("incorrect expression for% attribute", Expr);
5492 end Analyze_Stream_TSS_Definition;
5494 ------------------------------
5495 -- Check_Indexing_Functions --
5496 ------------------------------
5498 procedure Check_Indexing_Functions is
5499 Indexing_Found : Boolean := False;
5501 procedure Check_Inherited_Indexing;
5502 -- For a derived type, check that for a derived type, a specification
5503 -- of an indexing aspect can only be confirming, i.e. uses the same
5504 -- name as in the parent type.
5505 -- AI12-0160: Verify that an indexing cannot be specified for
5506 -- a derived type unless it is specified for the parent.
5508 procedure Check_One_Function (Subp : Entity_Id);
5509 -- Check one possible interpretation. Sets Indexing_Found True if a
5510 -- legal indexing function is found.
5512 procedure Illegal_Indexing (Msg : String);
5513 -- Diagnose illegal indexing function if not overloaded. In the
5514 -- overloaded case indicate that no legal interpretation exists.
5516 ------------------------------
5517 -- Check_Inherited_Indexing --
5518 ------------------------------
5520 procedure Check_Inherited_Indexing is
5521 Inherited : Node_Id;
5522 Other_Indexing : Node_Id;
5525 if Attr = Name_Constant_Indexing then
5527 Find_Aspect (Etype (Ent), Aspect_Constant_Indexing);
5529 Find_Aspect (Etype (Ent), Aspect_Variable_Indexing);
5531 else pragma Assert (Attr = Name_Variable_Indexing);
5533 Find_Aspect (Etype (Ent), Aspect_Variable_Indexing);
5535 Find_Aspect (Etype (Ent), Aspect_Constant_Indexing);
5538 if Present (Inherited) then
5539 if Debug_Flag_Dot_XX then
5542 -- OK if current attribute_definition_clause is expansion of
5543 -- inherited aspect.
5545 elsif Aspect_Rep_Item (Inherited) = N then
5548 -- Check if this is a confirming specification. The name
5549 -- may be overloaded between the parent operation and the
5550 -- inherited one, so we check that the Chars fields match.
5552 elsif Is_Entity_Name (Expression (Inherited))
5553 and then Chars (Entity (Expression (Inherited))) =
5554 Chars (Entity (Expression (N)))
5556 Indexing_Found := True;
5558 -- Indicate the operation that must be overridden, rather than
5559 -- redefining the indexing aspect.
5563 ("indexing function already inherited from parent type");
5565 ("!override & instead",
5566 N, Entity (Expression (Inherited)));
5569 -- If not inherited and the parent has another indexing function
5570 -- this is illegal, because it leads to inconsistent results in
5571 -- class-wide calls.
5573 elsif Present (Other_Indexing) then
5575 ("cannot specify indexing operation on derived type"
5576 & " if not specified for parent", N);
5578 end Check_Inherited_Indexing;
5580 ------------------------
5581 -- Check_One_Function --
5582 ------------------------
5584 procedure Check_One_Function (Subp : Entity_Id) is
5585 Default_Element : Node_Id;
5586 Ret_Type : constant Entity_Id := Etype (Subp);
5589 if not Is_Overloadable (Subp) then
5590 Illegal_Indexing ("illegal indexing function for type&");
5593 elsif Scope (Subp) /= Scope (Ent) then
5594 if Nkind (Expr) = N_Expanded_Name then
5596 -- Indexing function can't be declared elsewhere
5599 ("indexing function must be declared"
5600 & " in scope of type&");
5603 if Is_Derived_Type (Ent) then
5604 Check_Inherited_Indexing;
5609 elsif No (First_Formal (Subp)) then
5611 ("Indexing requires a function that applies to type&");
5614 elsif No (Next_Formal (First_Formal (Subp))) then
5616 ("indexing function must have at least two parameters");
5619 elsif Is_Derived_Type (Ent) then
5620 Check_Inherited_Indexing;
5623 if not Check_Primitive_Function (Subp) then
5625 ("Indexing aspect requires a function that applies to type&");
5629 -- If partial declaration exists, verify that it is not tagged.
5631 if Ekind (Current_Scope) = E_Package
5632 and then Has_Private_Declaration (Ent)
5633 and then From_Aspect_Specification (N)
5635 List_Containing (Parent (Ent)) =
5636 Private_Declarations
5637 (Specification (Unit_Declaration_Node (Current_Scope)))
5638 and then Nkind (N) = N_Attribute_Definition_Clause
5645 First (Visible_Declarations
5647 (Unit_Declaration_Node (Current_Scope))));
5649 while Present (Decl) loop
5650 if Nkind (Decl) = N_Private_Type_Declaration
5651 and then Ent = Full_View (Defining_Identifier (Decl))
5652 and then Tagged_Present (Decl)
5653 and then No (Aspect_Specifications (Decl))
5656 ("Indexing aspect cannot be specified on full view "
5657 & "if partial view is tagged");
5666 -- An indexing function must return either the default element of
5667 -- the container, or a reference type. For variable indexing it
5668 -- must be the latter.
5671 Find_Value_Of_Aspect
5672 (Etype (First_Formal (Subp)), Aspect_Iterator_Element);
5674 if Present (Default_Element) then
5675 Analyze (Default_Element);
5678 -- For variable_indexing the return type must be a reference type
5680 if Attr = Name_Variable_Indexing then
5681 if not Has_Implicit_Dereference (Ret_Type) then
5683 ("variable indexing must return a reference type");
5686 elsif Is_Access_Constant
5687 (Etype (First_Discriminant (Ret_Type)))
5690 ("variable indexing must return an access to variable");
5695 if Has_Implicit_Dereference (Ret_Type)
5698 (Etype (Get_Reference_Discriminant (Ret_Type)))
5701 ("constant indexing must return an access to constant");
5704 elsif Is_Access_Type (Etype (First_Formal (Subp)))
5705 and then not Is_Access_Constant (Etype (First_Formal (Subp)))
5708 ("constant indexing must apply to an access to constant");
5713 -- All checks succeeded
5715 Indexing_Found := True;
5716 end Check_One_Function;
5718 -----------------------
5719 -- Illegal_Indexing --
5720 -----------------------
5722 procedure Illegal_Indexing (Msg : String) is
5724 Error_Msg_NE (Msg, N, Ent);
5725 end Illegal_Indexing;
5727 -- Start of processing for Check_Indexing_Functions
5731 Check_Inherited_Indexing;
5736 if not Is_Overloaded (Expr) then
5737 Check_One_Function (Entity (Expr));
5745 Indexing_Found := False;
5746 Get_First_Interp (Expr, I, It);
5747 while Present (It.Nam) loop
5749 -- Note that analysis will have added the interpretation
5750 -- that corresponds to the dereference. We only check the
5751 -- subprogram itself. Ignore homonyms that may come from
5752 -- derived types in the context.
5754 if Is_Overloadable (It.Nam)
5755 and then Comes_From_Source (It.Nam)
5757 Check_One_Function (It.Nam);
5760 Get_Next_Interp (I, It);
5765 if not Indexing_Found and then not Error_Posted (N) then
5767 ("aspect Indexing requires a local function that applies to "
5768 & "type&", Expr, Ent);
5770 end Check_Indexing_Functions;
5772 ------------------------------
5773 -- Check_Iterator_Functions --
5774 ------------------------------
5776 procedure Check_Iterator_Functions is
5777 function Valid_Default_Iterator (Subp : Entity_Id) return Boolean;
5778 -- Check one possible interpretation for validity
5780 ----------------------------
5781 -- Valid_Default_Iterator --
5782 ----------------------------
5784 function Valid_Default_Iterator (Subp : Entity_Id) return Boolean is
5785 Root_T : constant Entity_Id := Root_Type (Etype (Etype (Subp)));
5789 if not Check_Primitive_Function (Subp) then
5792 -- The return type must be derived from a type in an instance
5793 -- of Iterator.Interfaces, and thus its root type must have a
5796 elsif Chars (Root_T) /= Name_Forward_Iterator
5797 and then Chars (Root_T) /= Name_Reversible_Iterator
5802 Formal := First_Formal (Subp);
5805 -- False if any subsequent formal has no default expression
5807 Next_Formal (Formal);
5808 while Present (Formal) loop
5809 if No (Expression (Parent (Formal))) then
5813 Next_Formal (Formal);
5816 -- True if all subsequent formals have default expressions
5819 end Valid_Default_Iterator;
5821 -- Start of processing for Check_Iterator_Functions
5826 if not Is_Entity_Name (Expr) then
5827 Error_Msg_N ("aspect Iterator must be a function name", Expr);
5830 if not Is_Overloaded (Expr) then
5831 if Entity (Expr) /= Any_Id
5832 and then not Check_Primitive_Function (Entity (Expr))
5835 ("aspect Indexing requires a function that applies to type&",
5836 Entity (Expr), Ent);
5839 -- Flag the default_iterator as well as the denoted function.
5841 if not Valid_Default_Iterator (Entity (Expr)) then
5842 Error_Msg_N ("improper function for default iterator!", Expr);
5847 Default : Entity_Id := Empty;
5852 Get_First_Interp (Expr, I, It);
5853 while Present (It.Nam) loop
5854 if not Check_Primitive_Function (It.Nam)
5855 or else not Valid_Default_Iterator (It.Nam)
5859 elsif Present (Default) then
5861 -- An explicit one should override an implicit one
5863 if Comes_From_Source (Default) =
5864 Comes_From_Source (It.Nam)
5866 Error_Msg_N ("default iterator must be unique", Expr);
5867 Error_Msg_Sloc := Sloc (Default);
5868 Error_Msg_N ("\\possible interpretation#", Expr);
5869 Error_Msg_Sloc := Sloc (It.Nam);
5870 Error_Msg_N ("\\possible interpretation#", Expr);
5872 elsif Comes_From_Source (It.Nam) then
5879 Get_Next_Interp (I, It);
5882 if Present (Default) then
5883 Set_Entity (Expr, Default);
5884 Set_Is_Overloaded (Expr, False);
5887 ("no interpretation is a valid default iterator!", Expr);
5891 end Check_Iterator_Functions;
5893 -------------------------------
5894 -- Check_Primitive_Function --
5895 -------------------------------
5897 function Check_Primitive_Function (Subp : Entity_Id) return Boolean is
5901 if Ekind (Subp) /= E_Function then
5905 if No (First_Formal (Subp)) then
5908 Ctrl := Etype (First_Formal (Subp));
5911 -- To be a primitive operation subprogram has to be in same scope.
5913 if Scope (Ctrl) /= Scope (Subp) then
5917 -- Type of formal may be the class-wide type, an access to such,
5918 -- or an incomplete view.
5921 or else Ctrl = Class_Wide_Type (Ent)
5923 (Ekind (Ctrl) = E_Anonymous_Access_Type
5924 and then (Designated_Type (Ctrl) = Ent
5926 Designated_Type (Ctrl) = Class_Wide_Type (Ent)))
5928 (Ekind (Ctrl) = E_Incomplete_Type
5929 and then Full_View (Ctrl) = Ent)
5937 end Check_Primitive_Function;
5939 ----------------------
5940 -- Duplicate_Clause --
5941 ----------------------
5943 function Duplicate_Clause return Boolean is
5945 function Check_One_Attr (Attr_1, Attr_2 : Name_Id) return Boolean;
5946 -- Check for one attribute; Attr_1 is the attribute_designator we are
5947 -- looking for. Attr_2 is the attribute_designator of the current
5948 -- node. Normally, this is called just once by Duplicate_Clause, with
5949 -- Attr_1 = Attr_2. However, it needs to be called twice for Size and
5950 -- Value_Size, because these mean the same thing. For compatibility,
5951 -- we allow specifying both Size and Value_Size, but only if the two
5954 --------------------
5955 -- Check_One_Attr --
5956 --------------------
5958 function Check_One_Attr (Attr_1, Attr_2 : Name_Id) return Boolean is
5959 A : constant Node_Id :=
5960 Get_Rep_Item (U_Ent, Attr_1, Check_Parents => False);
5963 if Attr_1 = Attr_2 then
5964 Error_Msg_Name_1 := Attr_1;
5965 Error_Msg_Sloc := Sloc (A);
5966 Error_Msg_NE ("aspect% for & previously given#", N, U_Ent);
5969 pragma Assert (Attr_1 in Name_Size | Name_Value_Size);
5970 pragma Assert (Attr_2 in Name_Size | Name_Value_Size);
5972 Error_Msg_Name_1 := Attr_2;
5973 Error_Msg_Name_2 := Attr_1;
5974 Error_Msg_Sloc := Sloc (A);
5975 Error_Msg_NE ("?% for & conflicts with % #", N, U_Ent);
5984 -- Start of processing for Duplicate_Clause
5987 -- Nothing to do if this attribute definition clause comes from
5988 -- an aspect specification, since we could not be duplicating an
5989 -- explicit clause, and we dealt with the case of duplicated aspects
5990 -- in Analyze_Aspect_Specifications.
5992 if From_Aspect_Specification (N) then
5996 -- Special cases for Size and Value_Size
5998 if (Chars (N) = Name_Size
5999 and then Check_One_Attr (Name_Value_Size, Name_Size))
6001 (Chars (N) = Name_Value_Size
6002 and then Check_One_Attr (Name_Size, Name_Value_Size))
6007 -- Normal case (including Size and Value_Size)
6009 return Check_One_Attr (Chars (N), Chars (N));
6010 end Duplicate_Clause;
6012 -- Start of processing for Analyze_Attribute_Definition_Clause
6015 -- The following code is a defense against recursion. Not clear that
6016 -- this can happen legitimately, but perhaps some error situations can
6017 -- cause it, and we did see this recursion during testing.
6019 if Analyzed (N) then
6022 Set_Analyzed (N, True);
6025 Check_Restriction_No_Use_Of_Attribute (N);
6027 if Is_Aspect_Id (Chars (N)) then
6028 -- 6.1/3 No_Specification_of_Aspect: Identifies an aspect for which
6029 -- no aspect_specification, attribute_definition_clause, or pragma
6031 Check_Restriction_No_Specification_Of_Aspect (N);
6034 -- Ignore some selected attributes in CodePeer mode since they are not
6035 -- relevant in this context.
6037 if CodePeer_Mode then
6040 -- Ignore Component_Size in CodePeer mode, to avoid changing the
6041 -- internal representation of types by implicitly packing them.
6043 when Attribute_Component_Size =>
6044 Rewrite (N, Make_Null_Statement (Sloc (N)));
6052 -- Process Ignore_Rep_Clauses option
6054 if Ignore_Rep_Clauses then
6057 -- The following should be ignored. They do not affect legality
6058 -- and may be target dependent. The basic idea of -gnatI is to
6059 -- ignore any rep clauses that may be target dependent but do not
6060 -- affect legality (except possibly to be rejected because they
6061 -- are incompatible with the compilation target).
6063 when Attribute_Alignment
6064 | Attribute_Bit_Order
6065 | Attribute_Component_Size
6066 | Attribute_Default_Scalar_Storage_Order
6067 | Attribute_Machine_Radix
6068 | Attribute_Object_Size
6069 | Attribute_Scalar_Storage_Order
6072 | Attribute_Stream_Size
6073 | Attribute_Value_Size
6075 Kill_Rep_Clause (N);
6078 -- The following should not be ignored, because in the first place
6079 -- they are reasonably portable, and should not cause problems
6080 -- in compiling code from another target, and also they do affect
6081 -- legality, e.g. failing to provide a stream attribute for a type
6082 -- may make a program illegal.
6084 when Attribute_External_Tag
6087 | Attribute_Put_Image
6089 | Attribute_Simple_Storage_Pool
6090 | Attribute_Storage_Pool
6091 | Attribute_Storage_Size
6096 -- We do not do anything here with address clauses, they will be
6097 -- removed by Freeze later on, but for now, it works better to
6098 -- keep them in the tree.
6100 when Attribute_Address =>
6103 -- Other cases are errors ("attribute& cannot be set with
6104 -- definition clause"), which will be caught below.
6112 Ent := Entity (Nam);
6114 if Rep_Item_Too_Early (Ent, N) then
6118 -- Rep clause applies to (underlying) full view of private or incomplete
6119 -- type if we have one (if not, this is a premature use of the type).
6120 -- However, some semantic checks need to be done on the specified entity
6121 -- i.e. the private view, so we save it in Ent.
6123 if Is_Private_Type (Ent)
6124 and then Is_Derived_Type (Ent)
6125 and then not Is_Tagged_Type (Ent)
6126 and then No (Full_View (Ent))
6127 and then No (Underlying_Full_View (Ent))
6131 elsif Ekind (Ent) = E_Incomplete_Type then
6133 -- The attribute applies to the full view, set the entity of the
6134 -- attribute definition accordingly.
6136 Ent := Underlying_Type (Ent);
6138 Set_Entity (Nam, Ent);
6141 U_Ent := Underlying_Type (Ent);
6144 -- Avoid cascaded error
6146 if Etype (Nam) = Any_Type then
6149 -- Must be declared in current scope or in case of an aspect
6150 -- specification, must be visible in current scope.
6152 elsif Scope (Ent) /= Current_Scope
6154 not (From_Aspect_Specification (N)
6155 and then Scope_Within_Or_Same (Current_Scope, Scope (Ent)))
6157 Error_Msg_N ("entity must be declared in this scope", Nam);
6160 -- Must not be a source renaming (we do have some cases where the
6161 -- expander generates a renaming, and those cases are OK, in such
6162 -- cases any attribute applies to the renamed object as well).
6164 elsif Is_Object (Ent)
6165 and then Present (Renamed_Object (Ent))
6167 -- In the case of a renamed object from source, this is an error
6168 -- unless the object is an aggregate and the renaming is created
6169 -- for an object declaration.
6171 if Comes_From_Source (Renamed_Object (Ent))
6172 and then Nkind (Renamed_Object (Ent)) /= N_Aggregate
6174 Get_Name_String (Chars (N));
6175 Error_Msg_Strlen := Name_Len;
6176 Error_Msg_String (1 .. Name_Len) := Name_Buffer (1 .. Name_Len);
6178 ("~ clause not allowed for a renaming declaration "
6179 & "(RM 13.1(6))", Nam);
6182 -- For the case of a compiler generated renaming, the attribute
6183 -- definition clause applies to the renamed object created by the
6184 -- expander. The easiest general way to handle this is to create a
6185 -- copy of the attribute definition clause for this object.
6187 elsif Is_Entity_Name (Renamed_Object (Ent)) then
6189 Make_Attribute_Definition_Clause (Loc,
6191 New_Occurrence_Of (Entity (Renamed_Object (Ent)), Loc),
6193 Expression => Duplicate_Subexpr (Expression (N))));
6195 -- If the renamed object is not an entity, it must be a dereference
6196 -- of an unconstrained function call, and we must introduce a new
6197 -- declaration to capture the expression. This is needed in the case
6198 -- of 'Alignment, where the original declaration must be rewritten.
6202 (Nkind (Renamed_Object (Ent)) = N_Explicit_Dereference);
6206 -- If no underlying entity, use entity itself, applies to some
6207 -- previously detected error cases ???
6209 elsif No (U_Ent) then
6212 -- Cannot specify for a subtype (exception Object/Value_Size)
6214 elsif Is_Type (U_Ent)
6215 and then not Is_First_Subtype (U_Ent)
6216 and then Id /= Attribute_Object_Size
6217 and then Id /= Attribute_Value_Size
6218 and then not From_At_Mod (N)
6220 Error_Msg_N ("cannot specify attribute for subtype", Nam);
6224 Set_Entity (N, U_Ent);
6226 -- Switch on particular attribute
6234 -- Address attribute definition clause
6236 when Attribute_Address => Address : begin
6238 -- A little error check, catch for X'Address use X'Address;
6240 if Nkind (Nam) = N_Identifier
6241 and then Nkind (Expr) = N_Attribute_Reference
6242 and then Attribute_Name (Expr) = Name_Address
6243 and then Nkind (Prefix (Expr)) = N_Identifier
6244 and then Chars (Nam) = Chars (Prefix (Expr))
6247 ("address for & is self-referencing", Prefix (Expr), Ent);
6251 -- Not that special case, carry on with analysis of expression
6253 Analyze_And_Resolve (Expr, RTE (RE_Address));
6255 -- Even when ignoring rep clauses we need to indicate that the
6256 -- entity has an address clause and thus it is legal to declare
6257 -- it imported. Freeze will get rid of the address clause later.
6258 -- Also call Set_Address_Taken to indicate that an address clause
6259 -- was present, even if we are about to remove it.
6261 if Ignore_Rep_Clauses then
6262 Set_Address_Taken (U_Ent);
6264 if Ekind (U_Ent) in E_Variable | E_Constant then
6265 Record_Rep_Item (U_Ent, N);
6271 if Duplicate_Clause then
6274 -- Case of address clause for subprogram
6276 elsif Is_Subprogram (U_Ent) then
6277 if Has_Homonym (U_Ent) then
6279 ("address clause cannot be given for overloaded "
6280 & "subprogram", Nam);
6284 -- For subprograms, all address clauses are permitted, and we
6285 -- mark the subprogram as having a deferred freeze so that Gigi
6286 -- will not elaborate it too soon.
6288 -- Above needs more comments, what is too soon about???
6290 Set_Has_Delayed_Freeze (U_Ent);
6292 -- Case of address clause for entry
6294 elsif Ekind (U_Ent) = E_Entry then
6295 if Nkind (Parent (N)) = N_Task_Body then
6297 ("entry address must be specified in task spec", Nam);
6301 -- For entries, we require a constant address
6303 Check_Constant_Address_Clause (Expr, U_Ent);
6305 -- Special checks for task types
6307 if Is_Task_Type (Scope (U_Ent))
6308 and then Comes_From_Source (Scope (U_Ent))
6311 ("??entry address declared for entry in task type", N);
6313 ("\??only one task can be declared of this type", N);
6316 -- Entry address clauses are obsolescent
6318 Check_Restriction (No_Obsolescent_Features, N);
6320 if Warn_On_Obsolescent_Feature then
6322 ("?j?attaching interrupt to task entry is an obsolescent "
6323 & "feature (RM J.7.1)", N);
6325 ("\?j?use interrupt procedure instead", N);
6328 -- Case of address clause for an object
6330 elsif Ekind (U_Ent) in E_Constant | E_Variable then
6332 -- Disallow case of an address clause for an object of an
6333 -- indefinite subtype which takes its bounds/discriminant/tag
6334 -- from its initial value. Without this, we get a Gigi
6335 -- assertion failure for things like
6336 -- X : String := Some_Function (...) with Address => ...;
6337 -- where the result subtype of the function is unconstrained.
6339 -- We want to reject two cases: the class-wide case, and the
6340 -- case where the FE conjures up a renaming declaration and
6341 -- would then otherwise generate an address specification for
6342 -- that renaming (which is a malformed tree, which is why Gigi
6345 if Is_Class_Wide_Type (Etype (U_Ent)) then
6347 ("address specification not supported for class-wide " &
6348 "object declaration", Nam);
6350 elsif Is_Constr_Subt_For_U_Nominal (Etype (U_Ent))
6352 Nkind (Parent (U_Ent)) = N_Object_Renaming_Declaration
6354 -- Confirm accuracy of " and dynamic size" message text
6355 -- before including it. We want to include that text when
6356 -- it is correct because it may be useful to the reader.
6357 -- The case where we omit that part of the message text
6358 -- might be dead code, but let's not rely on that.
6361 ("address specification not supported for object " &
6362 "declaration with indefinite nominal subtype" &
6363 (if Size_Known_At_Compile_Time (Etype (U_Ent))
6365 else " and dynamic size"), Nam);
6370 Expr : constant Node_Id := Expression (N);
6375 -- Exported variables cannot have an address clause, because
6376 -- this cancels the effect of the pragma Export.
6378 if Is_Exported (U_Ent) then
6380 ("cannot export object with address clause", Nam);
6384 Find_Overlaid_Entity (N, O_Ent, Off);
6386 if Present (O_Ent) then
6388 -- If the object overlays a constant object, mark it so
6390 if Is_Constant_Object (O_Ent) then
6391 Set_Overlays_Constant (U_Ent);
6394 -- If the address clause is of the form:
6396 -- for X'Address use Y'Address;
6400 -- C : constant Address := Y'Address;
6402 -- for X'Address use C;
6404 -- then we make an entry in the table to check the size
6405 -- and alignment of the overlaying variable. But we defer
6406 -- this check till after code generation to take full
6407 -- advantage of the annotation done by the back end.
6409 -- If the entity has a generic type, the check will be
6410 -- performed in the instance if the actual type justifies
6411 -- it, and we do not insert the clause in the table to
6412 -- prevent spurious warnings.
6414 -- Note: we used to test Comes_From_Source and only give
6415 -- this warning for source entities, but we have removed
6416 -- this test. It really seems bogus to generate overlays
6417 -- that would trigger this warning in generated code.
6418 -- Furthermore, by removing the test, we handle the
6419 -- aspect case properly.
6421 if Is_Object (O_Ent)
6422 and then not Is_Generic_Formal (O_Ent)
6423 and then not Is_Generic_Type (Etype (U_Ent))
6424 and then Address_Clause_Overlay_Warnings
6426 Register_Address_Clause_Check
6427 (N, U_Ent, No_Uint, O_Ent, Off);
6430 -- If the overlay changes the storage order, warn since
6431 -- the construct is not really supported by the back end.
6432 -- Also mark the entity as being volatile to block the
6433 -- optimizer, even if there is no warranty on the result.
6435 if (Is_Record_Type (Etype (U_Ent))
6436 or else Is_Array_Type (Etype (U_Ent)))
6437 and then (Is_Record_Type (Etype (O_Ent))
6438 or else Is_Array_Type (Etype (O_Ent)))
6439 and then Reverse_Storage_Order (Etype (U_Ent)) /=
6440 Reverse_Storage_Order (Etype (O_Ent))
6443 ("??overlay changes scalar storage order", Expr);
6444 Set_Treat_As_Volatile (U_Ent);
6448 -- If this is not an overlay, mark a variable as being
6449 -- volatile to prevent unwanted optimizations. It's a
6450 -- conservative interpretation of RM 13.3(19) for the
6451 -- cases where the compiler cannot detect potential
6452 -- aliasing issues easily and it also covers the case
6453 -- of an absolute address where the volatile aspect is
6454 -- kind of implicit.
6456 if Ekind (U_Ent) = E_Variable then
6457 Set_Treat_As_Volatile (U_Ent);
6460 -- Make an entry in the table for an absolute address as
6461 -- above to check that the value is compatible with the
6462 -- alignment of the object.
6465 Addr : constant Node_Id := Address_Value (Expr);
6467 if Compile_Time_Known_Value (Addr)
6468 and then Address_Clause_Overlay_Warnings
6470 Register_Address_Clause_Check
6471 (N, U_Ent, Expr_Value (Addr), Empty, False);
6476 -- Issue an unconditional warning for a constant overlaying
6477 -- a variable. For the reverse case, we will issue it only
6478 -- if the variable is modified.
6479 -- Within a generic unit an In_Parameter is a constant.
6480 -- It can be instantiated with a variable, in which case
6481 -- there will be a warning on the instance.
6483 if Ekind (U_Ent) = E_Constant
6484 and then Present (O_Ent)
6485 and then Ekind (O_Ent) /= E_Generic_In_Parameter
6486 and then not Overlays_Constant (U_Ent)
6487 and then Address_Clause_Overlay_Warnings
6489 Error_Msg_N ("?o?constant overlays a variable", Expr);
6491 -- Imported variables can have an address clause, but then
6492 -- the import is pretty meaningless except to suppress
6493 -- initializations, so we do not need such variables to
6494 -- be statically allocated (and in fact it causes trouble
6495 -- if the address clause is a local value).
6497 elsif Is_Imported (U_Ent) then
6498 Set_Is_Statically_Allocated (U_Ent, False);
6501 -- We mark a possible modification of a variable with an
6502 -- address clause, since it is likely aliasing is occurring.
6504 Note_Possible_Modification (Nam, Sure => False);
6506 -- Legality checks on the address clause for initialized
6507 -- objects is deferred until the freeze point, because
6508 -- a subsequent pragma might indicate that the object
6509 -- is imported and thus not initialized. Also, the address
6510 -- clause might involve entities that have yet to be
6513 Set_Has_Delayed_Freeze (U_Ent);
6515 -- If an initialization call has been generated for this
6516 -- object, it needs to be deferred to after the freeze node
6517 -- we have just now added, otherwise GIGI will see a
6518 -- reference to the variable (as actual to the IP call)
6519 -- before its definition.
6522 Init_Call : constant Node_Id :=
6523 Remove_Init_Call (U_Ent, N);
6526 if Present (Init_Call) then
6527 Append_Freeze_Action (U_Ent, Init_Call);
6529 -- Reset Initialization_Statements pointer so that
6530 -- if there is a pragma Import further down, it can
6531 -- clear any default initialization.
6533 Set_Initialization_Statements (U_Ent, Init_Call);
6537 -- Entity has delayed freeze, so we will generate an
6538 -- alignment check at the freeze point unless suppressed.
6540 if not Range_Checks_Suppressed (U_Ent)
6541 and then not Alignment_Checks_Suppressed (U_Ent)
6543 Set_Check_Address_Alignment (N);
6546 -- Kill the size check code, since we are not allocating
6547 -- the variable, it is somewhere else.
6549 Kill_Size_Check_Code (U_Ent);
6552 -- Not a valid entity for an address clause
6555 Error_Msg_N ("address cannot be given for &", Nam);
6563 -- Alignment attribute definition clause
6565 when Attribute_Alignment => Alignment : declare
6566 Align : constant Uint := Get_Alignment_Value (Expr);
6567 Max_Align : constant Uint := UI_From_Int (Maximum_Alignment);
6572 if not Is_Type (U_Ent)
6573 and then Ekind (U_Ent) /= E_Variable
6574 and then Ekind (U_Ent) /= E_Constant
6576 Error_Msg_N ("alignment cannot be given for &", Nam);
6578 elsif Duplicate_Clause then
6581 elsif Present (Align) then
6582 Set_Has_Alignment_Clause (U_Ent);
6584 -- Tagged type case, check for attempt to set alignment to a
6585 -- value greater than Max_Align, and reset if so.
6587 if Is_Tagged_Type (U_Ent) and then Align > Max_Align then
6589 ("alignment for & set to Maximum_Aligment??", Nam);
6590 Set_Alignment (U_Ent, Max_Align);
6595 Set_Alignment (U_Ent, Align);
6598 -- For an array type, U_Ent is the first subtype. In that case,
6599 -- also set the alignment of the anonymous base type so that
6600 -- other subtypes (such as the itypes for aggregates of the
6601 -- type) also receive the expected alignment.
6603 if Is_Array_Type (U_Ent) then
6604 Set_Alignment (Base_Type (U_Ent), Align);
6613 -- Bit_Order attribute definition clause
6615 when Attribute_Bit_Order =>
6616 if not Is_Record_Type (U_Ent) then
6618 ("Bit_Order can only be defined for record type", Nam);
6620 elsif Is_Tagged_Type (U_Ent) and then Is_Derived_Type (U_Ent) then
6622 ("Bit_Order cannot be defined for record extensions", Nam);
6624 elsif Duplicate_Clause then
6628 Analyze_And_Resolve (Expr, RTE (RE_Bit_Order));
6630 if Etype (Expr) = Any_Type then
6633 elsif not Is_OK_Static_Expression (Expr) then
6634 Flag_Non_Static_Expr
6635 ("Bit_Order requires static expression!", Expr);
6637 elsif (Expr_Value (Expr) = 0) /= Bytes_Big_Endian then
6638 Set_Reverse_Bit_Order (Base_Type (U_Ent), True);
6642 --------------------
6643 -- Component_Size --
6644 --------------------
6646 -- Component_Size attribute definition clause
6648 when Attribute_Component_Size => Component_Size_Case : declare
6649 Csize : constant Uint := Static_Integer (Expr);
6653 New_Ctyp : Entity_Id;
6657 if not Is_Array_Type (U_Ent) then
6658 Error_Msg_N ("component size requires array type", Nam);
6662 Btype := Base_Type (U_Ent);
6663 Ctyp := Component_Type (Btype);
6665 if Duplicate_Clause then
6668 elsif Rep_Item_Too_Early (Btype, N) then
6671 elsif Present (Csize) then
6672 Check_Size (Expr, Ctyp, Csize, Biased);
6674 -- For the biased case, build a declaration for a subtype that
6675 -- will be used to represent the biased subtype that reflects
6676 -- the biased representation of components. We need the subtype
6677 -- to get proper conversions on referencing elements of the
6682 Make_Defining_Identifier (Loc,
6684 New_External_Name (Chars (U_Ent), 'C', 0, 'T'));
6687 Make_Subtype_Declaration (Loc,
6688 Defining_Identifier => New_Ctyp,
6689 Subtype_Indication =>
6690 New_Occurrence_Of (Component_Type (Btype), Loc));
6692 Set_Parent (Decl, N);
6693 Analyze (Decl, Suppress => All_Checks);
6695 Set_Has_Delayed_Freeze (New_Ctyp, False);
6696 Reinit_Esize (New_Ctyp);
6697 Set_RM_Size (New_Ctyp, Csize);
6698 Reinit_Alignment (New_Ctyp);
6699 Set_Is_Itype (New_Ctyp, True);
6700 Set_Associated_Node_For_Itype (New_Ctyp, U_Ent);
6702 Set_Component_Type (Btype, New_Ctyp);
6703 Set_Biased (New_Ctyp, N, "component size clause");
6706 Set_Component_Size (Btype, Csize);
6708 -- Deal with warning on overridden size
6710 if Warn_On_Overridden_Size
6711 and then Has_Size_Clause (Ctyp)
6712 and then RM_Size (Ctyp) /= Csize
6715 ("component size overrides size clause for&?.s?", N, Ctyp);
6718 Set_Has_Component_Size_Clause (Btype, True);
6719 Set_Has_Non_Standard_Rep (Btype, True);
6721 end Component_Size_Case;
6723 -----------------------
6724 -- Constant_Indexing --
6725 -----------------------
6727 when Attribute_Constant_Indexing =>
6728 Check_Indexing_Functions;
6734 when Attribute_CPU =>
6735 pragma Assert (From_Aspect_Specification (N));
6736 -- The parser forbids this clause in source code, so it must have
6737 -- come from an aspect specification.
6739 if not Is_Task_Type (U_Ent) then
6740 Error_Msg_N ("'C'P'U can only be defined for task", Nam);
6742 elsif Duplicate_Clause then
6746 -- The expression must be analyzed in the special manner
6747 -- described in "Handling of Default and Per-Object
6748 -- Expressions" in sem.ads.
6750 -- The visibility to the components must be established
6751 -- and restored before and after analysis.
6754 Preanalyze_Spec_Expression (Expr, RTE (RE_CPU_Range));
6757 -- AI12-0117-1, "Restriction No_Tasks_Unassigned_To_CPU":
6758 -- If the expression is static, and its value is
6759 -- System.Multiprocessors.Not_A_Specific_CPU (i.e. zero) then
6760 -- that's a violation of No_Tasks_Unassigned_To_CPU. It might
6761 -- seem better to refer to Not_A_Specific_CPU here, but that
6762 -- involves a lot of horsing around with Rtsfind, and this
6763 -- value is not going to change, so it's better to hardwire
6766 -- AI12-0055-1, "All properties of a usage profile are defined
6767 -- by pragmas": If the expression is nonstatic, that's a
6768 -- violation of No_Dynamic_CPU_Assignment.
6770 if Is_OK_Static_Expression (Expr) then
6771 if Expr_Value (Expr) = Uint_0 then
6772 Check_Restriction (No_Tasks_Unassigned_To_CPU, Expr);
6775 Check_Restriction (No_Dynamic_CPU_Assignment, Expr);
6779 ----------------------
6780 -- Default_Iterator --
6781 ----------------------
6783 when Attribute_Default_Iterator => Default_Iterator : declare
6788 -- If target type is untagged, further checks are irrelevant
6790 if not Is_Tagged_Type (U_Ent) then
6792 ("aspect Default_Iterator applies to tagged type", Nam);
6796 Check_Iterator_Functions;
6800 if not Is_Entity_Name (Expr)
6801 or else Ekind (Entity (Expr)) /= E_Function
6803 Error_Msg_N ("aspect Iterator must be a function", Expr);
6806 Func := Entity (Expr);
6809 -- The type of the first parameter must be T, T'class, or a
6810 -- corresponding access type (5.5.1 (8/3). If function is
6811 -- parameterless label type accordingly.
6813 if No (First_Formal (Func)) then
6816 Typ := Etype (First_Formal (Func));
6820 or else Typ = Class_Wide_Type (U_Ent)
6821 or else (Is_Access_Type (Typ)
6822 and then Designated_Type (Typ) = U_Ent)
6823 or else (Is_Access_Type (Typ)
6824 and then Designated_Type (Typ) =
6825 Class_Wide_Type (U_Ent))
6831 ("Default_Iterator must be a primitive of&", Func, U_Ent);
6833 end Default_Iterator;
6835 ------------------------
6836 -- Dispatching_Domain --
6837 ------------------------
6839 when Attribute_Dispatching_Domain =>
6840 pragma Assert (From_Aspect_Specification (N));
6841 -- The parser forbids this clause in source code, so it must have
6842 -- come from an aspect specification.
6844 if not Is_Task_Type (U_Ent) then
6846 ("Dispatching_Domain can only be defined for task", Nam);
6848 elsif Duplicate_Clause then
6852 -- The expression must be analyzed in the special manner
6853 -- described in "Handling of Default and Per-Object
6854 -- Expressions" in sem.ads.
6856 -- The visibility to the components must be restored
6860 Preanalyze_Spec_Expression
6861 (Expr, RTE (RE_Dispatching_Domain));
6870 when Attribute_External_Tag =>
6871 if not Is_Tagged_Type (U_Ent) then
6872 Error_Msg_N ("should be a tagged type", Nam);
6875 if Duplicate_Clause then
6879 Analyze_And_Resolve (Expr, Standard_String);
6881 if not Is_OK_Static_Expression (Expr) then
6882 Flag_Non_Static_Expr
6883 ("static string required for tag name!", Nam);
6886 if not Is_Library_Level_Entity (U_Ent) then
6888 ("??non-unique external tag supplied for &", N, U_Ent);
6890 ("\??same external tag applies to all subprogram calls",
6893 ("\??corresponding internal tag cannot be obtained", N);
6897 --------------------------
6898 -- Implicit_Dereference --
6899 --------------------------
6901 when Attribute_Implicit_Dereference =>
6903 -- Legality checks already performed at the point of the type
6904 -- declaration, aspect is not delayed.
6912 when Attribute_Input =>
6913 Analyze_Stream_TSS_Definition (TSS_Stream_Input);
6914 Set_Has_Specified_Stream_Input (Ent);
6916 ------------------------
6917 -- Interrupt_Priority --
6918 ------------------------
6920 when Attribute_Interrupt_Priority =>
6921 pragma Assert (From_Aspect_Specification (N));
6922 -- The parser forbids this clause in source code, so it must have
6923 -- come from an aspect specification.
6925 if not Is_Concurrent_Type (U_Ent) then
6927 ("Interrupt_Priority can only be defined for task and "
6928 & "protected object", Nam);
6930 elsif Duplicate_Clause then
6934 -- The expression must be analyzed in the special manner
6935 -- described in "Handling of Default and Per-Object
6936 -- Expressions" in sem.ads.
6938 -- The visibility to the components must be restored
6942 Preanalyze_Spec_Expression
6943 (Expr, RTE (RE_Interrupt_Priority));
6947 -- Check the No_Task_At_Interrupt_Priority restriction
6949 if Is_Task_Type (U_Ent) then
6950 Check_Restriction (No_Task_At_Interrupt_Priority, N);
6958 when Attribute_Iterable =>
6961 if Nkind (Expr) /= N_Aggregate then
6962 Error_Msg_N ("aspect Iterable must be an aggregate", Expr);
6970 Assoc := First (Component_Associations (Expr));
6971 while Present (Assoc) loop
6972 Analyze (Expression (Assoc));
6974 if not Is_Entity_Name (Expression (Assoc))
6975 or else Ekind (Entity (Expression (Assoc))) /= E_Function
6977 Error_Msg_N ("value must be a function", Assoc);
6984 ----------------------
6985 -- Iterator_Element --
6986 ----------------------
6988 when Attribute_Iterator_Element =>
6991 if not Is_Entity_Name (Expr)
6992 or else not Is_Type (Entity (Expr))
6994 Error_Msg_N ("aspect Iterator_Element must be a type", Expr);
7002 -- Machine radix attribute definition clause
7004 when Attribute_Machine_Radix => Machine_Radix : declare
7005 Radix : constant Uint := Static_Integer (Expr);
7008 if not Is_Decimal_Fixed_Point_Type (U_Ent) then
7009 Error_Msg_N ("decimal fixed-point type expected for &", Nam);
7011 elsif Duplicate_Clause then
7014 elsif Present (Radix) then
7015 Set_Has_Machine_Radix_Clause (U_Ent);
7016 Set_Has_Non_Standard_Rep (Base_Type (U_Ent));
7021 elsif Radix = 10 then
7022 Set_Machine_Radix_10 (U_Ent);
7025 Error_Msg_N ("machine radix value must be 2 or 10", Expr);
7034 -- Object_Size attribute definition clause
7036 when Attribute_Object_Size => Object_Size : declare
7037 Size : constant Uint := Static_Integer (Expr);
7040 pragma Warnings (Off, Biased);
7043 if not Is_Type (U_Ent) then
7044 Error_Msg_N ("Object_Size cannot be given for &", Nam);
7046 elsif Duplicate_Clause then
7050 Check_Size (Expr, U_Ent, Size, Biased);
7052 if No (Size) or else Size <= 0 then
7053 Error_Msg_N ("Object_Size must be positive", Expr);
7055 elsif Is_Scalar_Type (U_Ent) then
7056 if Size /= 8 and then Size /= 16 and then Size /= 32
7057 and then UI_Mod (Size, 64) /= 0
7060 ("Object_Size must be 8, 16, 32, or multiple of 64",
7064 elsif Size mod 8 /= 0 then
7065 Error_Msg_N ("Object_Size must be a multiple of 8", Expr);
7068 Set_Esize (U_Ent, Size);
7069 Set_Has_Object_Size_Clause (U_Ent);
7070 Alignment_Check_For_Size_Change (U_Ent, Size);
7078 when Attribute_Output =>
7079 Analyze_Stream_TSS_Definition (TSS_Stream_Output);
7080 Set_Has_Specified_Stream_Output (Ent);
7086 when Attribute_Priority =>
7088 -- Priority attribute definition clause not allowed except from
7089 -- aspect specification.
7091 if From_Aspect_Specification (N) then
7092 if not (Is_Concurrent_Type (U_Ent)
7093 or else Ekind (U_Ent) = E_Procedure)
7096 ("Priority can only be defined for task and protected "
7099 elsif Duplicate_Clause then
7103 -- The expression must be analyzed in the special manner
7104 -- described in "Handling of Default and Per-Object
7105 -- Expressions" in sem.ads.
7107 -- The visibility to the components must be restored
7110 Preanalyze_Spec_Expression (Expr, Standard_Integer);
7113 if not Is_OK_Static_Expression (Expr) then
7114 Check_Restriction (Static_Priorities, Expr);
7120 ("attribute& cannot be set with definition clause", N);
7127 when Attribute_Put_Image =>
7128 Analyze_Put_Image_TSS_Definition;
7134 when Attribute_Read =>
7135 Analyze_Stream_TSS_Definition (TSS_Stream_Read);
7136 Set_Has_Specified_Stream_Read (Ent);
7138 --------------------------
7139 -- Scalar_Storage_Order --
7140 --------------------------
7142 -- Scalar_Storage_Order attribute definition clause
7144 when Attribute_Scalar_Storage_Order =>
7145 if not (Is_Record_Type (U_Ent) or else Is_Array_Type (U_Ent)) then
7147 ("Scalar_Storage_Order can only be defined for record or "
7148 & "array type", Nam);
7150 elsif Duplicate_Clause then
7154 Analyze_And_Resolve (Expr, RTE (RE_Bit_Order));
7156 if Etype (Expr) = Any_Type then
7159 elsif not Is_OK_Static_Expression (Expr) then
7160 Flag_Non_Static_Expr
7161 ("Scalar_Storage_Order requires static expression!", Expr);
7163 elsif (Expr_Value (Expr) = 0) /= Bytes_Big_Endian then
7165 -- Here for the case of a non-default (i.e. non-confirming)
7166 -- Scalar_Storage_Order attribute definition.
7168 if Support_Nondefault_SSO_On_Target then
7169 Set_Reverse_Storage_Order (Base_Type (U_Ent), True);
7172 ("non-default Scalar_Storage_Order not supported on "
7177 -- Clear SSO default indications since explicit setting of the
7178 -- order overrides the defaults.
7180 Set_SSO_Set_Low_By_Default (Base_Type (U_Ent), False);
7181 Set_SSO_Set_High_By_Default (Base_Type (U_Ent), False);
7184 ------------------------
7185 -- Size or Value_Size --
7186 ------------------------
7188 -- Size or Value_Size attribute definition clause. These are treated
7189 -- the same, except that Size is allowed on objects, and Value_Size
7190 -- is allowed on nonfirst subtypes. First subtypes allow both Size
7191 -- and Value_Size; the treatment is the same for both.
7193 when Attribute_Size | Attribute_Value_Size => Size : declare
7194 Size : constant Uint := Static_Integer (Expr);
7196 Attr_Name : constant String :=
7197 (if Id = Attribute_Size then "size"
7198 elsif Id = Attribute_Value_Size then "value size"
7199 else ""); -- can't happen
7200 -- Name of the attribute for printing in messages
7202 OK_Prefix : constant Boolean :=
7203 (if Id = Attribute_Size then
7204 Ekind (U_Ent) in Type_Kind | Constant_Or_Variable_Kind
7205 elsif Id = Attribute_Value_Size then
7206 Ekind (U_Ent) in Type_Kind
7207 else False); -- can't happen
7208 -- For X'Size, X can be a type or object; for X'Value_Size,
7209 -- X can be a type. Note that we already checked that 'Size
7210 -- can be specified only for a first subtype.
7215 if not OK_Prefix then
7216 Error_Msg_N (Attr_Name & " cannot be given for &", Nam);
7218 elsif Duplicate_Clause then
7221 elsif Is_Array_Type (U_Ent)
7222 and then not Is_Constrained (U_Ent)
7225 (Attr_Name & " cannot be given for unconstrained array", Nam);
7227 elsif Present (Size) then
7229 Etyp : constant Entity_Id :=
7230 (if Is_Type (U_Ent) then U_Ent else Etype (U_Ent));
7233 -- Check size, note that Gigi is in charge of checking that
7234 -- the size of an array or record type is OK. Also we do not
7235 -- check the size in the ordinary fixed-point case, since
7236 -- it is too early to do so (there may be subsequent small
7237 -- clause that affects the size). We can check the size if
7238 -- a small clause has already been given.
7240 if not Is_Ordinary_Fixed_Point_Type (U_Ent)
7241 or else Has_Small_Clause (U_Ent)
7246 Check_Size (Expr, Etyp, Size, Biased);
7247 Set_Biased (U_Ent, N, Attr_Name & " clause", Biased);
7251 -- For types, set RM_Size and Esize if appropriate
7253 if Is_Type (U_Ent) then
7254 Set_RM_Size (U_Ent, Size);
7256 -- If we are specifying the Size or Value_Size of a
7257 -- first subtype, then for elementary types, increase
7258 -- Object_Size to power of 2, but not less than a storage
7259 -- unit in any case (normally this means it will be byte
7262 -- For all other types, nothing else to do, we leave
7263 -- Esize (object size) unset; the back end will set it
7264 -- from the size and alignment in an appropriate manner.
7266 -- In both cases, we check whether the alignment must be
7267 -- reset in the wake of the size change.
7269 -- For nonfirst subtypes ('Value_Size only), we do
7272 if Is_First_Subtype (U_Ent) then
7273 if Is_Elementary_Type (U_Ent) then
7274 if Size <= System_Storage_Unit then
7276 (U_Ent, UI_From_Int (System_Storage_Unit));
7277 elsif Size <= 16 then
7278 Set_Esize (U_Ent, Uint_16);
7279 elsif Size <= 32 then
7280 Set_Esize (U_Ent, Uint_32);
7282 Set_Esize (U_Ent, (Size + 63) / 64 * 64);
7285 Alignment_Check_For_Size_Change
7286 (U_Ent, Esize (U_Ent));
7288 Alignment_Check_For_Size_Change (U_Ent, Size);
7292 -- For Object'Size, set Esize only
7295 if Is_Elementary_Type (Etyp)
7296 and then Size /= System_Storage_Unit
7300 and then Size /= System_Max_Integer_Size
7302 Error_Msg_Uint_1 := UI_From_Int (System_Storage_Unit);
7304 UI_From_Int (System_Max_Integer_Size);
7306 ("size for primitive object must be a power of 2 in "
7307 & "the range ^-^", N);
7310 Set_Esize (U_Ent, Size);
7313 -- As of RM 13.1, only confirming size
7314 -- (i.e. (Size = Esize (Etyp))) for aliased object of
7315 -- elementary type must be supported.
7316 -- GNAT rejects nonconfirming size for such object.
7318 if Is_Aliased (U_Ent)
7319 and then Is_Elementary_Type (Etyp)
7320 and then Known_Esize (U_Ent)
7321 and then Size /= Esize (Etyp)
7324 ("nonconfirming Size for aliased object is not "
7328 Set_Has_Size_Clause (U_Ent);
7337 -- Small attribute definition clause
7339 when Attribute_Small => Small : declare
7340 Implicit_Base : constant Entity_Id := Base_Type (U_Ent);
7344 Analyze_And_Resolve (Expr, Any_Real);
7346 if Etype (Expr) = Any_Type then
7349 elsif not Is_OK_Static_Expression (Expr) then
7350 Flag_Non_Static_Expr
7351 ("small requires static expression!", Expr);
7355 Small := Expr_Value_R (Expr);
7357 if Small <= Ureal_0 then
7358 Error_Msg_N ("small value must be greater than zero", Expr);
7364 if not Is_Ordinary_Fixed_Point_Type (U_Ent) then
7366 ("small requires an ordinary fixed point type", Nam);
7368 elsif Has_Small_Clause (U_Ent) then
7369 Error_Msg_N ("small already given for &", Nam);
7371 elsif Small > Delta_Value (U_Ent) then
7373 ("small value must not be greater than delta value", Nam);
7376 Set_Small_Value (U_Ent, Small);
7377 Set_Small_Value (Implicit_Base, Small);
7378 Set_Has_Small_Clause (U_Ent);
7379 Set_Has_Small_Clause (Implicit_Base);
7380 Set_Has_Non_Standard_Rep (Implicit_Base);
7388 -- Storage_Pool attribute definition clause
7390 when Attribute_Simple_Storage_Pool
7391 | Attribute_Storage_Pool
7393 Storage_Pool : declare
7397 procedure Associate_Storage_Pool
7398 (Ent : Entity_Id; Pool : Entity_Id);
7399 -- Associate Pool to Ent and perform legality checks on subpools
7401 ----------------------------
7402 -- Associate_Storage_Pool --
7403 ----------------------------
7405 procedure Associate_Storage_Pool
7406 (Ent : Entity_Id; Pool : Entity_Id)
7408 function Object_From (Pool : Entity_Id) return Entity_Id;
7409 -- Return the entity of which Pool is a part of
7415 function Object_From
7416 (Pool : Entity_Id) return Entity_Id
7418 N : Node_Id := Pool;
7420 if Present (Renamed_Object (Pool)) then
7421 N := Renamed_Object (Pool);
7424 while Present (N) loop
7426 when N_Defining_Identifier =>
7429 when N_Identifier | N_Expanded_Name =>
7432 when N_Indexed_Component | N_Selected_Component |
7433 N_Explicit_Dereference
7437 when N_Type_Conversion =>
7438 N := Expression (N);
7441 -- ??? we probably should handle more cases but
7442 -- this is good enough in practice for this check
7443 -- on a corner case.
7455 Set_Associated_Storage_Pool (Ent, Pool);
7457 -- Check RM 13.11.4(22-23/3): a specification of a storage pool
7458 -- is illegal if the storage pool supports subpools and:
7459 -- (A) The access type is a general access type.
7460 -- (B) The access type is statically deeper than the storage
7462 -- (C) The storage pool object is a part of a formal parameter;
7463 -- (D) The storage pool object is a part of the dereference of
7464 -- a non-library level general access type;
7466 if Ada_Version >= Ada_2012
7467 and then RTU_Loaded (System_Storage_Pools_Subpools)
7469 Is_Ancestor (RTE (RE_Root_Storage_Pool_With_Subpools),
7474 if Ekind (Etype (Ent)) = E_General_Access_Type then
7476 ("subpool cannot be used on general access type", Ent);
7481 if Type_Access_Level (Ent)
7482 > Static_Accessibility_Level
7483 (Pool, Object_Decl_Level)
7486 ("subpool access type has deeper accessibility "
7487 & "level than pool", Ent);
7491 Obj := Object_From (Pool);
7495 if Present (Obj) and then Is_Formal (Obj) then
7497 ("subpool cannot be part of a parameter", Ent);
7504 and then Ekind (Etype (Obj)) = E_General_Access_Type
7505 and then not Is_Library_Level_Entity (Etype (Obj))
7508 ("subpool cannot be part of the dereference of a " &
7509 "nested general access type", Ent);
7513 end Associate_Storage_Pool;
7516 if Ekind (U_Ent) = E_Access_Subprogram_Type then
7518 ("storage pool cannot be given for access-to-subprogram type",
7522 elsif Ekind (U_Ent) not in E_Access_Type | E_General_Access_Type
7525 ("storage pool can only be given for access types", Nam);
7528 elsif Is_Derived_Type (U_Ent) then
7530 ("storage pool cannot be given for a derived access type",
7533 elsif Duplicate_Clause then
7536 elsif Present (Associated_Storage_Pool (U_Ent)) then
7537 Error_Msg_N ("storage pool already given for &", Nam);
7541 -- Check for Storage_Size previously given
7544 SS : constant Node_Id :=
7545 Get_Attribute_Definition_Clause
7546 (U_Ent, Attribute_Storage_Size);
7548 if Present (SS) then
7549 Check_Pool_Size_Clash (U_Ent, N, SS);
7553 -- Storage_Pool case
7555 if Id = Attribute_Storage_Pool then
7557 (Expr, Class_Wide_Type (RTE (RE_Root_Storage_Pool)));
7559 -- In the Simple_Storage_Pool case, we allow a variable of any
7560 -- simple storage pool type, so we Resolve without imposing an
7564 Analyze_And_Resolve (Expr);
7566 if No (Get_Rep_Pragma
7567 (Etype (Expr), Name_Simple_Storage_Pool_Type))
7570 ("expression must be of a simple storage pool type", Expr);
7574 if not Denotes_Variable (Expr) then
7575 Error_Msg_N ("storage pool must be a variable", Expr);
7579 if Nkind (Expr) = N_Type_Conversion then
7580 T := Etype (Expression (Expr));
7585 -- The Stack_Bounded_Pool is used internally for implementing
7586 -- access types with a Storage_Size. Since it only work properly
7587 -- when used on one specific type, we need to check that it is not
7588 -- hijacked improperly:
7590 -- type T is access Integer;
7591 -- for T'Storage_Size use n;
7592 -- type Q is access Float;
7593 -- for Q'Storage_Size use T'Storage_Size; -- incorrect
7595 if Is_RTE (Base_Type (T), RE_Stack_Bounded_Pool) then
7596 Error_Msg_N ("non-shareable internal Pool", Expr);
7600 -- Validate_Remote_Access_To_Class_Wide_Type for attribute
7601 -- Storage_Pool since this attribute cannot be defined for such
7602 -- types (RM E.2.2(17)).
7604 Validate_Remote_Access_To_Class_Wide_Type (N);
7606 -- If the argument is a name that is not an entity name, then
7607 -- we construct a renaming operation to define an entity of
7608 -- type storage pool.
7610 if not Is_Entity_Name (Expr)
7611 and then Is_Object_Reference (Expr)
7613 Pool := Make_Temporary (Loc, 'P', Expr);
7616 Rnode : constant Node_Id :=
7617 Make_Object_Renaming_Declaration (Loc,
7618 Defining_Identifier => Pool,
7620 New_Occurrence_Of (Etype (Expr), Loc),
7624 -- If the attribute definition clause comes from an aspect
7625 -- clause, then insert the renaming before the associated
7626 -- entity's declaration, since the attribute clause has
7627 -- not yet been appended to the declaration list.
7629 if From_Aspect_Specification (N) then
7630 Insert_Before (Parent (Entity (N)), Rnode);
7632 Insert_Before (N, Rnode);
7636 Associate_Storage_Pool (U_Ent, Pool);
7639 elsif Is_Entity_Name (Expr) then
7640 Pool := Entity (Expr);
7642 -- If pool is a renamed object, get original one. This can
7643 -- happen with an explicit renaming, and within instances.
7645 while Present (Renamed_Object (Pool))
7646 and then Is_Entity_Name (Renamed_Object (Pool))
7648 Pool := Entity (Renamed_Object (Pool));
7651 if Present (Renamed_Object (Pool))
7652 and then Nkind (Renamed_Object (Pool)) = N_Type_Conversion
7653 and then Is_Entity_Name (Expression (Renamed_Object (Pool)))
7655 Pool := Entity (Expression (Renamed_Object (Pool)));
7658 Associate_Storage_Pool (U_Ent, Pool);
7660 elsif Nkind (Expr) = N_Type_Conversion
7661 and then Is_Entity_Name (Expression (Expr))
7662 and then Nkind (Original_Node (Expr)) = N_Attribute_Reference
7664 Pool := Entity (Expression (Expr));
7665 Associate_Storage_Pool (U_Ent, Pool);
7668 Error_Msg_N ("incorrect reference to a Storage Pool", Expr);
7677 -- Storage_Size attribute definition clause
7679 when Attribute_Storage_Size => Storage_Size : declare
7680 Btype : constant Entity_Id := Base_Type (U_Ent);
7683 if Is_Task_Type (U_Ent) then
7685 -- Check obsolescent (but never obsolescent if from aspect)
7687 if not From_Aspect_Specification (N) then
7688 Check_Restriction (No_Obsolescent_Features, N);
7690 if Warn_On_Obsolescent_Feature then
7692 ("?j?storage size clause for task is an obsolescent "
7693 & "feature (RM J.9)", N);
7694 Error_Msg_N ("\?j?use Storage_Size pragma instead", N);
7701 if not Is_Access_Type (U_Ent)
7702 and then Ekind (U_Ent) /= E_Task_Type
7704 Error_Msg_N ("storage size cannot be given for &", Nam);
7706 elsif Is_Access_Type (U_Ent) and Is_Derived_Type (U_Ent) then
7708 ("storage size cannot be given for a derived access type",
7711 elsif Duplicate_Clause then
7715 -- Validate_Remote_Access_To_Class_Wide_Type for attribute
7716 -- Storage_Size since this attribute cannot be defined for such
7717 -- types (RM E.2.2(17)).
7719 Validate_Remote_Access_To_Class_Wide_Type (N);
7721 Analyze_And_Resolve (Expr, Any_Integer);
7723 if Is_Access_Type (U_Ent) then
7725 -- Check for Storage_Pool previously given
7728 SP : constant Node_Id :=
7729 Get_Attribute_Definition_Clause
7730 (U_Ent, Attribute_Storage_Pool);
7733 if Present (SP) then
7734 Check_Pool_Size_Clash (U_Ent, SP, N);
7738 -- Special case of for x'Storage_Size use 0
7740 if Is_OK_Static_Expression (Expr)
7741 and then Expr_Value (Expr) = 0
7743 Set_No_Pool_Assigned (Btype);
7747 Set_Has_Storage_Size_Clause (Btype);
7755 when Attribute_Stream_Size => Stream_Size : declare
7756 Size : constant Uint := Static_Integer (Expr);
7759 if Ada_Version <= Ada_95 then
7760 Check_Restriction (No_Implementation_Attributes, N);
7763 if Duplicate_Clause then
7766 elsif Is_Elementary_Type (U_Ent) then
7767 -- Size will be empty if we already detected an error
7768 -- (e.g. Expr is of the wrong type); we might as well
7769 -- give the useful hint below even in that case.
7771 if No (Size) or else
7772 (Size /= System_Storage_Unit
7773 and then Size /= System_Storage_Unit * 2
7774 and then Size /= System_Storage_Unit * 3
7775 and then Size /= System_Storage_Unit * 4
7776 and then Size /= System_Storage_Unit * 8)
7779 ("stream size for elementary type must be 8, 16, 24, " &
7782 elsif Known_RM_Size (U_Ent) and then RM_Size (U_Ent) > Size then
7783 Error_Msg_Uint_1 := RM_Size (U_Ent);
7785 ("stream size for elementary type must be 8, 16, 24, " &
7786 "32 or 64 and at least ^", N);
7789 Set_Has_Stream_Size_Clause (U_Ent);
7792 Error_Msg_N ("Stream_Size cannot be given for &", Nam);
7796 -----------------------
7797 -- Variable_Indexing --
7798 -----------------------
7800 when Attribute_Variable_Indexing =>
7801 Check_Indexing_Functions;
7807 when Attribute_Write =>
7808 Analyze_Stream_TSS_Definition (TSS_Stream_Write);
7809 Set_Has_Specified_Stream_Write (Ent);
7811 -- All other attributes cannot be set
7815 ("attribute& cannot be set with definition clause", N);
7818 -- The test for the type being frozen must be performed after any
7819 -- expression the clause has been analyzed since the expression itself
7820 -- might cause freezing that makes the clause illegal.
7822 if Rep_Item_Too_Late (U_Ent, N, FOnly) then
7825 end Analyze_Attribute_Definition_Clause;
7827 ----------------------------
7828 -- Analyze_Code_Statement --
7829 ----------------------------
7831 procedure Analyze_Code_Statement (N : Node_Id) is
7832 HSS : constant Node_Id := Parent (N);
7833 SBody : constant Node_Id := Parent (HSS);
7834 Subp : constant Entity_Id := Current_Scope;
7841 -- Accept foreign code statements for CodePeer. The analysis is skipped
7842 -- to avoid rejecting unrecognized constructs.
7844 if CodePeer_Mode then
7849 -- Analyze and check we get right type, note that this implements the
7850 -- requirement (RM 13.8(1)) that Machine_Code be with'ed, since that is
7851 -- the only way that Asm_Insn could possibly be visible.
7853 Analyze_And_Resolve (Expression (N));
7855 if Etype (Expression (N)) = Any_Type then
7857 elsif not Is_RTE (Etype (Expression (N)), RE_Asm_Insn) then
7858 Error_Msg_N ("incorrect type for code statement", N);
7862 Check_Code_Statement (N);
7864 -- Make sure we appear in the handled statement sequence of a subprogram
7867 if Nkind (HSS) /= N_Handled_Sequence_Of_Statements
7868 or else Nkind (SBody) /= N_Subprogram_Body
7871 ("code statement can only appear in body of subprogram", N);
7875 -- Do remaining checks (RM 13.8(3)) if not already done
7877 if not Is_Machine_Code_Subprogram (Subp) then
7878 Set_Is_Machine_Code_Subprogram (Subp);
7880 -- No exception handlers allowed
7882 if Present (Exception_Handlers (HSS)) then
7884 ("exception handlers not permitted in machine code subprogram",
7885 First (Exception_Handlers (HSS)));
7888 -- No declarations other than use clauses and pragmas (we allow
7889 -- certain internally generated declarations as well).
7891 Decl := First (Declarations (SBody));
7892 while Present (Decl) loop
7893 DeclO := Original_Node (Decl);
7894 if Comes_From_Source (DeclO)
7895 and Nkind (DeclO) not in N_Pragma
7896 | N_Use_Package_Clause
7898 | N_Implicit_Label_Declaration
7901 ("this declaration is not allowed in machine code subprogram",
7908 -- No statements other than code statements, pragmas, and labels.
7909 -- Again we allow certain internally generated statements.
7911 -- In Ada 2012, qualified expressions are names, and the code
7912 -- statement is initially parsed as a procedure call.
7914 Stmt := First (Statements (HSS));
7915 while Present (Stmt) loop
7916 StmtO := Original_Node (Stmt);
7918 -- A procedure call transformed into a code statement is OK
7920 if Ada_Version >= Ada_2012
7921 and then Nkind (StmtO) = N_Procedure_Call_Statement
7922 and then Nkind (Name (StmtO)) = N_Qualified_Expression
7926 elsif Comes_From_Source (StmtO)
7927 and then Nkind (StmtO) not in
7928 N_Pragma | N_Label | N_Code_Statement
7931 ("this statement is not allowed in machine code subprogram",
7938 end Analyze_Code_Statement;
7940 -----------------------------------------------
7941 -- Analyze_Enumeration_Representation_Clause --
7942 -----------------------------------------------
7944 procedure Analyze_Enumeration_Representation_Clause (N : Node_Id) is
7945 Ident : constant Node_Id := Identifier (N);
7946 Aggr : constant Node_Id := Array_Aggregate (N);
7947 Enumtype : Entity_Id;
7954 Err : Boolean := False;
7955 -- Set True to avoid cascade errors and crashes on incorrect source code
7957 Lo : constant Uint := Expr_Value (Type_Low_Bound (Universal_Integer));
7958 Hi : constant Uint := Expr_Value (Type_High_Bound (Universal_Integer));
7959 -- Allowed range of universal integer (= allowed range of enum lit vals)
7963 -- Minimum and maximum values of entries
7965 Max_Node : Node_Id := Empty; -- init to avoid warning
7966 -- Pointer to node for literal providing max value
7969 if Ignore_Rep_Clauses then
7970 Kill_Rep_Clause (N);
7974 -- Ignore enumeration rep clauses by default in CodePeer mode,
7975 -- unless -gnatd.I is specified, as a work around for potential false
7976 -- positive messages.
7978 if CodePeer_Mode and not Debug_Flag_Dot_II then
7982 -- First some basic error checks
7985 Enumtype := Entity (Ident);
7987 if Enumtype = Any_Type
7988 or else Rep_Item_Too_Early (Enumtype, N)
7992 Enumtype := Underlying_Type (Enumtype);
7995 if not Is_Enumeration_Type (Enumtype) then
7997 ("enumeration type required, found}",
7998 Ident, First_Subtype (Enumtype));
8002 -- Ignore rep clause on generic actual type. This will already have
8003 -- been flagged on the template as an error, and this is the safest
8004 -- way to ensure we don't get a junk cascaded message in the instance.
8006 if Is_Generic_Actual_Type (Enumtype) then
8009 -- Type must be in current scope
8011 elsif Scope (Enumtype) /= Current_Scope then
8012 Error_Msg_N ("type must be declared in this scope", Ident);
8015 -- Type must be a first subtype
8017 elsif not Is_First_Subtype (Enumtype) then
8018 Error_Msg_N ("cannot give enumeration rep clause for subtype", N);
8021 -- Ignore duplicate rep clause
8023 elsif Has_Enumeration_Rep_Clause (Enumtype) then
8024 Error_Msg_N ("duplicate enumeration rep clause ignored", N);
8027 -- Don't allow rep clause for standard [wide_[wide_]]character
8029 elsif Is_Standard_Character_Type (Enumtype) then
8030 Error_Msg_N ("enumeration rep clause not allowed for this type", N);
8033 -- Check that the expression is a proper aggregate (no parentheses)
8035 elsif Paren_Count (Aggr) /= 0 then
8037 ("extra parentheses surrounding aggregate not allowed", Aggr);
8040 -- Reject the mixing of named and positional entries in the aggregate
8042 elsif Present (Expressions (Aggr))
8043 and then Present (Component_Associations (Aggr))
8045 Error_Msg_N ("cannot mix positional and named entries in "
8046 & "enumeration rep clause", N);
8049 -- All tests passed, so set rep clause in place
8052 Set_Has_Enumeration_Rep_Clause (Enumtype);
8053 Set_Has_Enumeration_Rep_Clause (Base_Type (Enumtype));
8056 -- Now we process the aggregate. Note that we don't use the normal
8057 -- aggregate code for this purpose, because we don't want any of the
8058 -- normal expansion activities, and a number of special semantic
8059 -- rules apply (including the component type being any integer type)
8061 Elit := First_Literal (Enumtype);
8063 -- Process positional entries
8065 if Present (Expressions (Aggr)) then
8066 Expr := First (Expressions (Aggr));
8067 while Present (Expr) loop
8069 Error_Msg_N ("too many entries in aggregate", Expr);
8073 Val := Static_Integer (Expr);
8075 -- Err signals that we found some incorrect entries processing
8076 -- the list. The final checks for completeness and ordering are
8077 -- skipped in this case.
8082 elsif Val < Lo or else Hi < Val then
8083 Error_Msg_N ("value outside permitted range", Expr);
8087 Set_Enumeration_Rep (Elit, Val);
8088 Set_Enumeration_Rep_Expr (Elit, Expr);
8095 -- Process named entries
8097 elsif Present (Component_Associations (Aggr)) then
8098 Assoc := First (Component_Associations (Aggr));
8099 while Present (Assoc) loop
8100 Choice := First (Choices (Assoc));
8102 if Present (Next (Choice)) then
8104 ("multiple choice not allowed here", Next (Choice));
8108 if Nkind (Choice) = N_Others_Choice then
8109 Error_Msg_N ("OTHERS choice not allowed here", Choice);
8112 elsif Nkind (Choice) = N_Range then
8114 -- ??? should allow zero/one element range here
8116 Error_Msg_N ("range not allowed here", Choice);
8120 Analyze_And_Resolve (Choice, Enumtype);
8122 if Error_Posted (Choice) then
8127 if Is_Entity_Name (Choice)
8128 and then Is_Type (Entity (Choice))
8130 Error_Msg_N ("subtype name not allowed here", Choice);
8133 -- ??? should allow static subtype with zero/one entry
8135 elsif Etype (Choice) = Base_Type (Enumtype) then
8136 if not Is_OK_Static_Expression (Choice) then
8137 Flag_Non_Static_Expr
8138 ("non-static expression used for choice!", Choice);
8142 Elit := Expr_Value_E (Choice);
8144 if Present (Enumeration_Rep_Expr (Elit)) then
8146 Sloc (Enumeration_Rep_Expr (Elit));
8148 ("representation for& previously given#",
8153 Set_Enumeration_Rep_Expr (Elit, Expression (Assoc));
8155 Expr := Expression (Assoc);
8156 Val := Static_Integer (Expr);
8161 elsif Val < Lo or else Hi < Val then
8162 Error_Msg_N ("value outside permitted range", Expr);
8166 Set_Enumeration_Rep (Elit, Val);
8177 -- Aggregate is fully processed. Now we check that a full set of
8178 -- representations was given, and that they are in range and in order.
8179 -- These checks are only done if no other errors occurred.
8185 Elit := First_Literal (Enumtype);
8186 while Present (Elit) loop
8187 if No (Enumeration_Rep_Expr (Elit)) then
8188 Error_Msg_NE ("missing representation for&!", N, Elit);
8191 Val := Enumeration_Rep (Elit);
8197 if Present (Val) then
8198 if Present (Max) and then Val <= Max then
8200 ("enumeration value for& not ordered!",
8201 Enumeration_Rep_Expr (Elit), Elit);
8204 Max_Node := Enumeration_Rep_Expr (Elit);
8208 -- If there is at least one literal whose representation is not
8209 -- equal to the Pos value, then note that this enumeration type
8210 -- has a non-standard representation.
8212 if Val /= Enumeration_Pos (Elit) then
8213 Set_Has_Non_Standard_Rep (Base_Type (Enumtype));
8220 -- Now set proper size information
8223 Minsize : Uint := UI_From_Int (Minimum_Size (Enumtype));
8226 if Has_Size_Clause (Enumtype) then
8228 -- All OK, if size is OK now
8230 if RM_Size (Enumtype) >= Minsize then
8234 -- Try if we can get by with biasing
8237 UI_From_Int (Minimum_Size (Enumtype, Biased => True));
8239 -- Error message if even biasing does not work
8241 if RM_Size (Enumtype) < Minsize then
8242 Error_Msg_Uint_1 := RM_Size (Enumtype);
8243 Error_Msg_Uint_2 := Max;
8245 ("previously given size (^) is too small "
8246 & "for this value (^)", Max_Node);
8248 -- If biasing worked, indicate that we now have biased rep
8252 (Enumtype, Size_Clause (Enumtype), "size clause");
8257 Set_RM_Size (Enumtype, Minsize);
8258 Set_Enum_Esize (Enumtype);
8261 Set_RM_Size (Base_Type (Enumtype), RM_Size (Enumtype));
8262 Set_Esize (Base_Type (Enumtype), Esize (Enumtype));
8264 Copy_Alignment (To => Base_Type (Enumtype), From => Enumtype);
8268 -- We repeat the too late test in case it froze itself
8270 if Rep_Item_Too_Late (Enumtype, N) then
8273 end Analyze_Enumeration_Representation_Clause;
8275 ----------------------------
8276 -- Analyze_Free_Statement --
8277 ----------------------------
8279 procedure Analyze_Free_Statement (N : Node_Id) is
8281 Analyze (Expression (N));
8282 end Analyze_Free_Statement;
8284 ---------------------------
8285 -- Analyze_Freeze_Entity --
8286 ---------------------------
8288 procedure Analyze_Freeze_Entity (N : Node_Id) is
8290 Freeze_Entity_Checks (N);
8291 end Analyze_Freeze_Entity;
8293 -----------------------------------
8294 -- Analyze_Freeze_Generic_Entity --
8295 -----------------------------------
8297 procedure Analyze_Freeze_Generic_Entity (N : Node_Id) is
8298 E : constant Entity_Id := Entity (N);
8301 if not Is_Frozen (E) and then Has_Delayed_Aspects (E) then
8302 Analyze_Aspects_At_Freeze_Point (E);
8305 Freeze_Entity_Checks (N);
8306 end Analyze_Freeze_Generic_Entity;
8308 ------------------------------------------
8309 -- Analyze_Record_Representation_Clause --
8310 ------------------------------------------
8312 -- Note: we check as much as we can here, but we can't do any checks
8313 -- based on the position values (e.g. overlap checks) until freeze time
8314 -- because especially in Ada 2005 (machine scalar mode), the processing
8315 -- for non-standard bit order can substantially change the positions.
8316 -- See procedure Check_Record_Representation_Clause (called from Freeze)
8317 -- for the remainder of this processing.
8319 procedure Analyze_Record_Representation_Clause (N : Node_Id) is
8320 Ident : constant Node_Id := Identifier (N);
8328 Rectype : Entity_Id;
8331 function Is_Inherited (Comp : Entity_Id) return Boolean;
8332 -- True if Comp is an inherited component in a record extension
8338 function Is_Inherited (Comp : Entity_Id) return Boolean is
8339 Comp_Base : Entity_Id;
8342 if Ekind (Rectype) = E_Record_Subtype then
8343 Comp_Base := Original_Record_Component (Comp);
8348 return Comp_Base /= Original_Record_Component (Comp_Base);
8353 Is_Record_Extension : Boolean;
8354 -- True if Rectype is a record extension
8356 CR_Pragma : Node_Id := Empty;
8357 -- Points to N_Pragma node if Complete_Representation pragma present
8359 -- Start of processing for Analyze_Record_Representation_Clause
8362 if Ignore_Rep_Clauses then
8363 Kill_Rep_Clause (N);
8368 Rectype := Entity (Ident);
8370 if Rectype = Any_Type or else Rep_Item_Too_Early (Rectype, N) then
8373 Rectype := Underlying_Type (Rectype);
8376 -- First some basic error checks
8378 if not Is_Record_Type (Rectype) then
8380 ("record type required, found}", Ident, First_Subtype (Rectype));
8383 elsif Scope (Rectype) /= Current_Scope then
8384 Error_Msg_N ("type must be declared in this scope", N);
8387 elsif not Is_First_Subtype (Rectype) then
8388 Error_Msg_N ("cannot give record rep clause for subtype", N);
8391 elsif Has_Record_Rep_Clause (Rectype) then
8392 Error_Msg_N ("duplicate record rep clause ignored", N);
8395 elsif Rep_Item_Too_Late (Rectype, N) then
8399 -- We know we have a first subtype, now possibly go to the anonymous
8400 -- base type to determine whether Rectype is a record extension.
8402 Recdef := Type_Definition (Declaration_Node (Base_Type (Rectype)));
8403 Is_Record_Extension :=
8404 Nkind (Recdef) = N_Derived_Type_Definition
8405 and then Present (Record_Extension_Part (Recdef));
8407 if Present (Mod_Clause (N)) then
8409 M : constant Node_Id := Mod_Clause (N);
8410 P : constant List_Id := Pragmas_Before (M);
8414 Check_Restriction (No_Obsolescent_Features, Mod_Clause (N));
8416 if Warn_On_Obsolescent_Feature then
8418 ("?j?mod clause is an obsolescent feature (RM J.8)", N);
8420 ("\?j?use alignment attribute definition clause instead", N);
8427 -- Get the alignment value to perform error checking
8429 Ignore := Get_Alignment_Value (Expression (M));
8433 -- For untagged types, clear any existing component clauses for the
8434 -- type. If the type is derived, this is what allows us to override
8435 -- a rep clause for the parent. For type extensions, the representation
8436 -- of the inherited components is inherited, so we want to keep previous
8437 -- component clauses for completeness.
8439 if not Is_Tagged_Type (Rectype) then
8440 Comp := First_Component_Or_Discriminant (Rectype);
8441 while Present (Comp) loop
8442 Set_Component_Clause (Comp, Empty);
8443 Next_Component_Or_Discriminant (Comp);
8447 -- All done if no component clauses
8449 CC := First (Component_Clauses (N));
8455 -- A representation like this applies to the base type
8457 Set_Has_Record_Rep_Clause (Base_Type (Rectype));
8458 Set_Has_Non_Standard_Rep (Base_Type (Rectype));
8459 Set_Has_Specified_Layout (Base_Type (Rectype));
8461 -- Process the component clauses
8463 while Present (CC) loop
8467 if Nkind (CC) = N_Pragma then
8470 -- The only pragma of interest is Complete_Representation
8472 if Pragma_Name (CC) = Name_Complete_Representation then
8476 -- Processing for real component clause
8479 Posit := Static_Integer (Position (CC));
8480 Fbit := Static_Integer (First_Bit (CC));
8481 Lbit := Static_Integer (Last_Bit (CC));
8484 and then Present (Fbit)
8485 and then Present (Lbit)
8488 Error_Msg_N ("position cannot be negative", Position (CC));
8491 Error_Msg_N ("first bit cannot be negative", First_Bit (CC));
8493 -- The Last_Bit specified in a component clause must not be
8494 -- less than the First_Bit minus one (RM-13.5.1(10)).
8496 elsif Lbit < Fbit - 1 then
8498 ("last bit cannot be less than first bit minus one",
8501 -- Values look OK, so find the corresponding record component
8502 -- Even though the syntax allows an attribute reference for
8503 -- implementation-defined components, GNAT does not allow the
8504 -- tag to get an explicit position.
8506 elsif Nkind (Component_Name (CC)) = N_Attribute_Reference then
8507 if Attribute_Name (Component_Name (CC)) = Name_Tag then
8508 Error_Msg_N ("position of tag cannot be specified", CC);
8510 Error_Msg_N ("illegal component name", CC);
8514 Comp := First_Entity (Rectype);
8515 while Present (Comp) loop
8516 exit when Chars (Comp) = Chars (Component_Name (CC));
8522 -- Maybe component of base type that is absent from
8523 -- statically constrained first subtype.
8525 Comp := First_Entity (Base_Type (Rectype));
8526 while Present (Comp) loop
8527 exit when Chars (Comp) = Chars (Component_Name (CC));
8534 ("component clause is for non-existent field", CC);
8536 -- Ada 2012 (AI05-0026): Any name that denotes a
8537 -- discriminant of an object of an unchecked union type
8538 -- shall not occur within a record_representation_clause.
8540 -- The general restriction of using record rep clauses on
8541 -- Unchecked_Union types has now been lifted. Since it is
8542 -- possible to introduce a record rep clause which mentions
8543 -- the discriminant of an Unchecked_Union in non-Ada 2012
8544 -- code, this check is applied to all versions of the
8547 elsif Ekind (Comp) = E_Discriminant
8548 and then Is_Unchecked_Union (Rectype)
8551 ("cannot reference discriminant of unchecked union",
8552 Component_Name (CC));
8554 elsif Is_Record_Extension and then Is_Inherited (Comp) then
8556 ("component clause not allowed for inherited "
8557 & "component&", CC, Comp);
8559 elsif Present (Component_Clause (Comp)) then
8561 -- Diagnose duplicate rep clause, or check consistency
8562 -- if this is an inherited component. In a double fault,
8563 -- there may be a duplicate inconsistent clause for an
8564 -- inherited component.
8566 if Scope (Original_Record_Component (Comp)) = Rectype
8567 or else Parent (Component_Clause (Comp)) = N
8569 Error_Msg_Sloc := Sloc (Component_Clause (Comp));
8570 Error_Msg_N ("component clause previously given#", CC);
8574 Rep1 : constant Node_Id := Component_Clause (Comp);
8576 if Intval (Position (Rep1)) /=
8577 Intval (Position (CC))
8578 or else Intval (First_Bit (Rep1)) /=
8579 Intval (First_Bit (CC))
8580 or else Intval (Last_Bit (Rep1)) /=
8581 Intval (Last_Bit (CC))
8584 ("component clause inconsistent with "
8585 & "representation of ancestor", CC);
8587 elsif Warn_On_Redundant_Constructs then
8589 ("?r?redundant confirming component clause "
8590 & "for component!", CC);
8595 -- Normal case where this is the first component clause we
8596 -- have seen for this entity, so set it up properly.
8599 -- Make reference for field in record rep clause and set
8600 -- appropriate entity field in the field identifier.
8603 (Comp, Component_Name (CC), Set_Ref => False);
8604 Set_Entity_With_Checks (Component_Name (CC), Comp);
8606 -- Update Fbit and Lbit to the actual bit number
8608 Fbit := Fbit + UI_From_Int (SSU) * Posit;
8609 Lbit := Lbit + UI_From_Int (SSU) * Posit;
8611 if Has_Size_Clause (Rectype)
8612 and then RM_Size (Rectype) <= Lbit
8614 Error_Msg_Uint_1 := RM_Size (Rectype);
8615 Error_Msg_Uint_2 := Lbit + 1;
8616 Error_Msg_N ("bit number out of range of specified "
8617 & "size (expected ^, got ^)",
8620 Set_Component_Clause (Comp, CC);
8621 Set_Component_Bit_Offset (Comp, Fbit);
8622 Set_Esize (Comp, 1 + (Lbit - Fbit));
8623 Set_Normalized_First_Bit (Comp, Fbit mod SSU);
8624 Set_Normalized_Position (Comp, Fbit / SSU);
8626 if Warn_On_Overridden_Size
8627 and then Has_Size_Clause (Etype (Comp))
8628 and then RM_Size (Etype (Comp)) /= Esize (Comp)
8631 ("?.s?component size overrides size clause for&",
8632 Component_Name (CC), Etype (Comp));
8636 (Component_Name (CC),
8642 (Comp, First_Node (CC), "component clause", Biased);
8644 -- This information is also set in the corresponding
8645 -- component of the base type, found by accessing the
8646 -- Original_Record_Component link if it is present.
8648 Ocomp := Original_Record_Component (Comp);
8650 if Present (Ocomp) and then Ocomp /= Comp then
8651 Set_Component_Clause (Ocomp, CC);
8652 Set_Component_Bit_Offset (Ocomp, Fbit);
8653 Set_Esize (Ocomp, 1 + (Lbit - Fbit));
8654 Set_Normalized_First_Bit (Ocomp, Fbit mod SSU);
8655 Set_Normalized_Position (Ocomp, Fbit / SSU);
8657 -- Note: we don't use Set_Biased here, because we
8658 -- already gave a warning above if needed, and we
8659 -- would get a duplicate for the same name here.
8661 Set_Has_Biased_Representation
8662 (Ocomp, Has_Biased_Representation (Comp));
8665 if Esize (Comp) < 0 then
8666 Error_Msg_N ("component size is negative", CC);
8677 -- Check missing components if Complete_Representation pragma appeared
8679 if Present (CR_Pragma) then
8680 Comp := First_Component_Or_Discriminant (Rectype);
8681 while Present (Comp) loop
8682 if No (Component_Clause (Comp)) then
8684 ("missing component clause for &", CR_Pragma, Comp);
8687 Next_Component_Or_Discriminant (Comp);
8690 -- Give missing components warning if required
8692 elsif Warn_On_Unrepped_Components then
8694 Num_Repped_Components : Nat := 0;
8695 Num_Unrepped_Components : Nat := 0;
8698 -- First count number of repped and unrepped components
8700 Comp := First_Component_Or_Discriminant (Rectype);
8701 while Present (Comp) loop
8702 if Present (Component_Clause (Comp)) then
8703 Num_Repped_Components := Num_Repped_Components + 1;
8705 Num_Unrepped_Components := Num_Unrepped_Components + 1;
8708 Next_Component_Or_Discriminant (Comp);
8711 -- We are only interested in the case where there is at least one
8712 -- unrepped component, and at least half the components have rep
8713 -- clauses. We figure that if less than half have them, then the
8714 -- partial rep clause is really intentional. If the component
8715 -- type has no underlying type set at this point (as for a generic
8716 -- formal type), we don't know enough to give a warning on the
8719 if Num_Unrepped_Components > 0
8720 and then Num_Unrepped_Components < Num_Repped_Components
8722 Comp := First_Component_Or_Discriminant (Rectype);
8723 while Present (Comp) loop
8724 if No (Component_Clause (Comp))
8725 and then Comes_From_Source (Comp)
8726 and then Present (Underlying_Type (Etype (Comp)))
8727 and then (Is_Scalar_Type (Underlying_Type (Etype (Comp)))
8728 or else Size_Known_At_Compile_Time
8729 (Underlying_Type (Etype (Comp))))
8730 and then not Has_Warnings_Off (Rectype)
8732 -- Ignore discriminant in unchecked union, since it is
8733 -- not there, and cannot have a component clause.
8735 and then (not Is_Unchecked_Union (Rectype)
8736 or else Ekind (Comp) /= E_Discriminant)
8738 Error_Msg_Sloc := Sloc (Comp);
8740 ("?.c?no component clause given for & declared #",
8744 Next_Component_Or_Discriminant (Comp);
8749 end Analyze_Record_Representation_Clause;
8751 -------------------------------------
8752 -- Build_Discrete_Static_Predicate --
8753 -------------------------------------
8755 procedure Build_Discrete_Static_Predicate
8760 Loc : constant Source_Ptr := Sloc (Expr);
8762 Btyp : constant Entity_Id := Base_Type (Typ);
8764 BLo : constant Uint := Expr_Value (Type_Low_Bound (Btyp));
8765 BHi : constant Uint := Expr_Value (Type_High_Bound (Btyp));
8766 -- Low bound and high bound value of base type of Typ
8770 -- Bounds for constructing the static predicate. We use the bound of the
8771 -- subtype if it is static, otherwise the corresponding base type bound.
8772 -- Note: a non-static subtype can have a static predicate.
8777 -- One entry in a Rlist value, a single REnt (range entry) value denotes
8778 -- one range from Lo to Hi. To represent a single value range Lo = Hi =
8781 type RList is array (Nat range <>) of REnt;
8782 -- A list of ranges. The ranges are sorted in increasing order, and are
8783 -- disjoint (there is a gap of at least one value between each range in
8784 -- the table). A value is in the set of ranges in Rlist if it lies
8785 -- within one of these ranges.
8787 False_Range : constant RList :=
8788 RList'(1 .. 0 => REnt'(No_Uint, No_Uint));
8789 -- An empty set of ranges represents a range list that can never be
8790 -- satisfied, since there are no ranges in which the value could lie,
8791 -- so it does not lie in any of them. False_Range is a canonical value
8792 -- for this empty set, but general processing should test for an Rlist
8793 -- with length zero (see Is_False predicate), since other null ranges
8794 -- may appear which must be treated as False.
8796 True_Range : constant RList := RList'(1 => REnt'(BLo, BHi));
8797 -- Range representing True, value must be in the base range
8799 function "and" (Left : RList; Right : RList) return RList;
8800 -- And's together two range lists, returning a range list. This is a set
8801 -- intersection operation.
8803 function "or" (Left : RList; Right : RList) return RList;
8804 -- Or's together two range lists, returning a range list. This is a set
8807 function "not" (Right : RList) return RList;
8808 -- Returns complement of a given range list, i.e. a range list
8809 -- representing all the values in TLo .. THi that are not in the input
8812 function Build_Val (V : Uint) return Node_Id;
8813 -- Return an analyzed N_Identifier node referencing this value, suitable
8814 -- for use as an entry in the Static_Discrete_Predicate list. This node
8815 -- is typed with the base type.
8817 function Build_Range (Lo : Uint; Hi : Uint) return Node_Id;
8818 -- Return an analyzed N_Range node referencing this range, suitable for
8819 -- use as an entry in the Static_Discrete_Predicate list. This node is
8820 -- typed with the base type.
8824 Static : access Boolean) return RList;
8825 -- This is a recursive routine that converts the given expression into a
8826 -- list of ranges, suitable for use in building the static predicate.
8827 -- Static.all will be set to False if the expression is found to be non
8828 -- static. Note that Static.all should be set to True by the caller.
8830 function Is_False (R : RList) return Boolean;
8831 pragma Inline (Is_False);
8832 -- Returns True if the given range list is empty, and thus represents a
8833 -- False list of ranges that can never be satisfied.
8835 function Is_True (R : RList) return Boolean;
8836 -- Returns True if R trivially represents the True predicate by having a
8837 -- single range from BLo to BHi.
8839 function Is_Type_Ref (N : Node_Id) return Boolean;
8840 pragma Inline (Is_Type_Ref);
8841 -- Returns if True if N is a reference to the type for the predicate in
8842 -- the expression (i.e. if it is an identifier whose Chars field matches
8843 -- the Nam given in the call). N must not be parenthesized, if the type
8844 -- name appears in parens, this routine will return False.
8846 function Lo_Val (N : Node_Id) return Uint;
8847 -- Given an entry from a Static_Discrete_Predicate list that is either
8848 -- a static expression or static range, gets either the expression value
8849 -- or the low bound of the range.
8851 function Hi_Val (N : Node_Id) return Uint;
8852 -- Given an entry from a Static_Discrete_Predicate list that is either
8853 -- a static expression or static range, gets either the expression value
8854 -- or the high bound of the range.
8856 function Membership_Entry
8857 (N : Node_Id; Static : access Boolean) return RList;
8858 -- Given a single membership entry (range, value, or subtype), returns
8859 -- the corresponding range list. Set Static.all to False if not static.
8861 function Membership_Entries
8862 (N : Node_Id; Static : access Boolean) return RList;
8863 -- Given an element on an alternatives list of a membership operation,
8864 -- returns the range list corresponding to this entry and all following
8865 -- entries (i.e. returns the "or" of this list of values).
8866 -- Set Static.all to False if not static.
8870 Static : access Boolean) return RList;
8871 -- Given a type, if it has a static predicate, then set Result to the
8872 -- predicate as a range list, otherwise set Static.all to False.
8878 function "and" (Left : RList; Right : RList) return RList is
8880 -- First range of result
8882 SLeft : Nat := Left'First;
8883 -- Start of rest of left entries
8885 SRight : Nat := Right'First;
8886 -- Start of rest of right entries
8889 -- If either range is True, return the other
8891 if Is_True (Left) then
8893 elsif Is_True (Right) then
8897 -- If either range is False, return False
8899 if Is_False (Left) or else Is_False (Right) then
8903 -- Loop to remove entries at start that are disjoint, and thus just
8904 -- get discarded from the result entirely.
8907 -- If no operands left in either operand, result is false
8909 if SLeft > Left'Last or else SRight > Right'Last then
8912 -- Discard first left operand entry if disjoint with right
8914 elsif Left (SLeft).Hi < Right (SRight).Lo then
8917 -- Discard first right operand entry if disjoint with left
8919 elsif Right (SRight).Hi < Left (SLeft).Lo then
8920 SRight := SRight + 1;
8922 -- Otherwise we have an overlapping entry
8929 -- Now we have two non-null operands, and first entries overlap. The
8930 -- first entry in the result will be the overlapping part of these
8933 FEnt := REnt'(Lo => UI_Max (Left (SLeft).Lo, Right (SRight).Lo),
8934 Hi => UI_Min (Left (SLeft).Hi, Right (SRight).Hi));
8936 -- Now we can remove the entry that ended at a lower value, since its
8937 -- contribution is entirely contained in Fent.
8939 if Left (SLeft).Hi <= Right (SRight).Hi then
8942 SRight := SRight + 1;
8945 -- Compute result by concatenating this first entry with the "and" of
8946 -- the remaining parts of the left and right operands. Note that if
8947 -- either of these is empty, "and" will yield empty, so that we will
8948 -- end up with just Fent, which is what we want in that case.
8951 FEnt & (Left (SLeft .. Left'Last) and Right (SRight .. Right'Last));
8958 function "not" (Right : RList) return RList is
8960 -- Return True if False range
8962 if Is_False (Right) then
8966 -- Return False if True range
8968 if Is_True (Right) then
8972 -- Here if not trivial case
8975 Result : RList (1 .. Right'Length + 1);
8976 -- May need one more entry for gap at beginning and end
8979 -- Number of entries stored in Result
8984 if Right (Right'First).Lo > TLo then
8986 Result (Count) := REnt'(TLo, Right (Right'First).Lo - 1);
8989 -- Gaps between ranges
8991 for J in Right'First .. Right'Last - 1 loop
8993 Result (Count) := REnt'(Right (J).Hi + 1, Right (J + 1).Lo - 1);
8998 if Right (Right'Last).Hi < THi then
9000 Result (Count) := REnt'(Right (Right'Last).Hi + 1, THi);
9003 return Result (1 .. Count);
9011 function "or" (Left : RList; Right : RList) return RList is
9013 -- First range of result
9015 SLeft : Nat := Left'First;
9016 -- Start of rest of left entries
9018 SRight : Nat := Right'First;
9019 -- Start of rest of right entries
9022 -- If either range is True, return True
9024 if Is_True (Left) or else Is_True (Right) then
9028 -- If either range is False (empty), return the other
9030 if Is_False (Left) then
9032 elsif Is_False (Right) then
9036 -- Initialize result first entry from left or right operand depending
9037 -- on which starts with the lower range.
9039 if Left (SLeft).Lo < Right (SRight).Lo then
9040 FEnt := Left (SLeft);
9043 FEnt := Right (SRight);
9044 SRight := SRight + 1;
9047 -- This loop eats ranges from left and right operands that are
9048 -- contiguous with the first range we are gathering.
9051 -- Eat first entry in left operand if contiguous or overlapped by
9052 -- gathered first operand of result.
9054 if SLeft <= Left'Last
9055 and then Left (SLeft).Lo <= FEnt.Hi + 1
9057 FEnt.Hi := UI_Max (FEnt.Hi, Left (SLeft).Hi);
9060 -- Eat first entry in right operand if contiguous or overlapped by
9061 -- gathered right operand of result.
9063 elsif SRight <= Right'Last
9064 and then Right (SRight).Lo <= FEnt.Hi + 1
9066 FEnt.Hi := UI_Max (FEnt.Hi, Right (SRight).Hi);
9067 SRight := SRight + 1;
9069 -- All done if no more entries to eat
9076 -- Obtain result as the first entry we just computed, concatenated
9077 -- to the "or" of the remaining results (if one operand is empty,
9078 -- this will just concatenate with the other
9081 FEnt & (Left (SLeft .. Left'Last) or Right (SRight .. Right'Last));
9088 function Build_Range (Lo : Uint; Hi : Uint) return Node_Id is
9093 Low_Bound => Build_Val (Lo),
9094 High_Bound => Build_Val (Hi));
9095 Set_Etype (Result, Btyp);
9096 Set_Analyzed (Result);
9104 function Build_Val (V : Uint) return Node_Id is
9108 if Is_Enumeration_Type (Typ) then
9109 Result := Get_Enum_Lit_From_Pos (Typ, V, Loc);
9111 Result := Make_Integer_Literal (Loc, V);
9114 Set_Etype (Result, Btyp);
9115 Set_Is_Static_Expression (Result);
9116 Set_Analyzed (Result);
9126 Static : access Boolean) return RList
9132 -- Static expression can only be true or false
9134 if Is_OK_Static_Expression (Exp) then
9135 if Expr_Value (Exp) = 0 then
9142 -- Otherwise test node type
9153 return Get_RList (Left_Opnd (Exp), Static)
9155 Get_RList (Right_Opnd (Exp), Static);
9162 return Get_RList (Left_Opnd (Exp), Static)
9164 Get_RList (Right_Opnd (Exp), Static);
9169 return not Get_RList (Right_Opnd (Exp), Static);
9171 -- Comparisons of type with static value
9173 when N_Op_Compare =>
9175 -- Type is left operand
9177 if Is_Type_Ref (Left_Opnd (Exp))
9178 and then Is_OK_Static_Expression (Right_Opnd (Exp))
9180 Val := Expr_Value (Right_Opnd (Exp));
9182 -- Typ is right operand
9184 elsif Is_Type_Ref (Right_Opnd (Exp))
9185 and then Is_OK_Static_Expression (Left_Opnd (Exp))
9187 Val := Expr_Value (Left_Opnd (Exp));
9189 -- Invert sense of comparison
9192 when N_Op_Gt => Op := N_Op_Lt;
9193 when N_Op_Lt => Op := N_Op_Gt;
9194 when N_Op_Ge => Op := N_Op_Le;
9195 when N_Op_Le => Op := N_Op_Ge;
9196 when others => null;
9199 -- Other cases are non-static
9202 Static.all := False;
9206 -- Construct range according to comparison operation
9210 return RList'(1 => REnt'(Val, Val));
9213 return RList'(1 => REnt'(Val, BHi));
9216 return RList'(1 => REnt'(Val + 1, BHi));
9219 return RList'(1 => REnt'(BLo, Val));
9222 return RList'(1 => REnt'(BLo, Val - 1));
9225 return RList'(REnt'(BLo, Val - 1), REnt'(Val + 1, BHi));
9228 raise Program_Error;
9234 if not Is_Type_Ref (Left_Opnd (Exp)) then
9235 Static.all := False;
9239 if Present (Right_Opnd (Exp)) then
9240 return Membership_Entry (Right_Opnd (Exp), Static);
9242 return Membership_Entries
9243 (First (Alternatives (Exp)), Static);
9246 -- Negative membership (NOT IN)
9249 if not Is_Type_Ref (Left_Opnd (Exp)) then
9250 Static.all := False;
9254 if Present (Right_Opnd (Exp)) then
9255 return not Membership_Entry (Right_Opnd (Exp), Static);
9257 return not Membership_Entries
9258 (First (Alternatives (Exp)), Static);
9261 -- Function call, may be call to static predicate
9263 when N_Function_Call =>
9264 if Is_Entity_Name (Name (Exp)) then
9266 Ent : constant Entity_Id := Entity (Name (Exp));
9268 if Is_Predicate_Function (Ent) then
9269 return Stat_Pred (Etype (First_Formal (Ent)), Static);
9274 -- Other function call cases are non-static
9276 Static.all := False;
9279 -- Qualified expression, dig out the expression
9281 when N_Qualified_Expression =>
9282 return Get_RList (Expression (Exp), Static);
9284 when N_Case_Expression =>
9291 if not Is_Entity_Name (Expression (Expr))
9292 or else Etype (Expression (Expr)) /= Typ
9295 ("expression must denote subtype", Expression (Expr));
9299 -- Collect discrete choices in all True alternatives
9301 Choices := New_List;
9302 Alt := First (Alternatives (Exp));
9303 while Present (Alt) loop
9304 Dep := Expression (Alt);
9306 if not Is_OK_Static_Expression (Dep) then
9307 Static.all := False;
9310 elsif Is_True (Expr_Value (Dep)) then
9311 Append_List_To (Choices,
9312 New_Copy_List (Discrete_Choices (Alt)));
9318 return Membership_Entries (First (Choices), Static);
9321 -- Expression with actions: if no actions, dig out expression
9323 when N_Expression_With_Actions =>
9324 if Is_Empty_List (Actions (Exp)) then
9325 return Get_RList (Expression (Exp), Static);
9327 Static.all := False;
9334 return (Get_RList (Left_Opnd (Exp), Static)
9335 and not Get_RList (Right_Opnd (Exp), Static))
9336 or (Get_RList (Right_Opnd (Exp), Static)
9337 and not Get_RList (Left_Opnd (Exp), Static));
9339 -- Any other node type is non-static
9342 Static.all := False;
9351 function Hi_Val (N : Node_Id) return Uint is
9353 if Is_OK_Static_Expression (N) then
9354 return Expr_Value (N);
9356 pragma Assert (Nkind (N) = N_Range);
9357 return Expr_Value (High_Bound (N));
9365 function Is_False (R : RList) return Boolean is
9367 return R'Length = 0;
9374 function Is_True (R : RList) return Boolean is
9377 and then R (R'First).Lo = BLo
9378 and then R (R'First).Hi = BHi;
9385 function Is_Type_Ref (N : Node_Id) return Boolean is
9387 return Nkind (N) = N_Identifier
9388 and then Chars (N) = Nam
9389 and then Paren_Count (N) = 0;
9396 function Lo_Val (N : Node_Id) return Uint is
9398 if Is_OK_Static_Expression (N) then
9399 return Expr_Value (N);
9401 pragma Assert (Nkind (N) = N_Range);
9402 return Expr_Value (Low_Bound (N));
9406 ------------------------
9407 -- Membership_Entries --
9408 ------------------------
9410 function Membership_Entries
9411 (N : Node_Id; Static : access Boolean) return RList is
9413 if No (Next (N)) then
9414 return Membership_Entry (N, Static);
9416 return Membership_Entry (N, Static)
9417 or Membership_Entries (Next (N), Static);
9419 end Membership_Entries;
9421 ----------------------
9422 -- Membership_Entry --
9423 ----------------------
9425 function Membership_Entry
9426 (N : Node_Id; Static : access Boolean) return RList
9435 if Nkind (N) = N_Range then
9436 if not Is_OK_Static_Expression (Low_Bound (N))
9438 not Is_OK_Static_Expression (High_Bound (N))
9440 Static.all := False;
9443 SLo := Expr_Value (Low_Bound (N));
9444 SHi := Expr_Value (High_Bound (N));
9445 return RList'(1 => REnt'(SLo, SHi));
9450 elsif Nkind (N) = N_Others_Choice then
9452 Choices : constant List_Id := Others_Discrete_Choices (N);
9454 Range_List : RList (1 .. List_Length (Choices));
9457 Choice := First (Choices);
9459 for J in Range_List'Range loop
9460 Range_List (J) := REnt'(Lo_Val (Choice), Hi_Val (Choice));
9467 -- Static expression case
9469 elsif Is_OK_Static_Expression (N) then
9470 Val := Expr_Value (N);
9471 return RList'(1 => REnt'(Val, Val));
9473 -- Identifier (other than static expression) case
9475 else pragma Assert (Nkind (N) in N_Expanded_Name | N_Identifier);
9479 if Is_Type (Entity (N)) then
9481 -- If type has predicates, process them
9483 if Has_Predicates (Entity (N)) then
9484 return Stat_Pred (Entity (N), Static);
9486 -- For static subtype without predicates, get range
9488 elsif Is_OK_Static_Subtype (Entity (N)) then
9489 SLo := Expr_Value (Type_Low_Bound (Entity (N)));
9490 SHi := Expr_Value (Type_High_Bound (Entity (N)));
9491 return RList'(1 => REnt'(SLo, SHi));
9493 -- Any other type makes us non-static
9496 Static.all := False;
9500 -- Any other kind of identifier in predicate (e.g. a non-static
9501 -- expression value) means this is not a static predicate.
9504 Static.all := False;
9508 end Membership_Entry;
9516 Static : access Boolean) return RList is
9518 -- Not static if type does not have static predicates
9520 if not Has_Static_Predicate (Typ) then
9521 Static.all := False;
9525 -- Otherwise we convert the predicate list to a range list
9528 Spred : constant List_Id := Static_Discrete_Predicate (Typ);
9529 Result : RList (1 .. List_Length (Spred));
9533 P := First (Static_Discrete_Predicate (Typ));
9534 for J in Result'Range loop
9535 Result (J) := REnt'(Lo_Val (P), Hi_Val (P));
9543 -- Start of processing for Build_Discrete_Static_Predicate
9546 -- Establish bounds for the predicate
9548 if Compile_Time_Known_Value (Type_Low_Bound (Typ)) then
9549 TLo := Expr_Value (Type_Low_Bound (Typ));
9554 if Compile_Time_Known_Value (Type_High_Bound (Typ)) then
9555 THi := Expr_Value (Type_High_Bound (Typ));
9560 -- Analyze the expression to see if it is a static predicate
9563 Static : aliased Boolean := True;
9564 Ranges : constant RList := Get_RList (Expr, Static'Access);
9565 -- Range list from expression if it is static
9570 -- If non-static, return doing nothing
9576 -- Convert range list into a form for the static predicate. In the
9577 -- Ranges array, we just have raw ranges, these must be converted
9578 -- to properly typed and analyzed static expressions or range nodes.
9580 -- Note: here we limit ranges to the ranges of the subtype, so that
9581 -- a predicate is always false for values outside the subtype. That
9582 -- seems fine, such values are invalid anyway, and considering them
9583 -- to fail the predicate seems allowed and friendly, and furthermore
9584 -- simplifies processing for case statements and loops.
9588 for J in Ranges'Range loop
9590 Lo : Uint := Ranges (J).Lo;
9591 Hi : Uint := Ranges (J).Hi;
9594 -- Ignore completely out of range entry
9596 if Hi < TLo or else Lo > THi then
9599 -- Otherwise process entry
9602 -- Adjust out of range value to subtype range
9612 -- Convert range into required form
9614 Append_To (Plist, Build_Range (Lo, Hi));
9619 -- Processing was successful and all entries were static, so now we
9620 -- can store the result as the predicate list.
9622 Set_Static_Discrete_Predicate (Typ, Plist);
9624 -- Within a generic the predicate functions themselves need not
9627 if Inside_A_Generic then
9631 -- The processing for static predicates put the expression into
9632 -- canonical form as a series of ranges. It also eliminated
9633 -- duplicates and collapsed and combined ranges. We might as well
9634 -- replace the alternatives list of the right operand of the
9635 -- membership test with the static predicate list, which will
9636 -- usually be more efficient.
9639 New_Alts : constant List_Id := New_List;
9644 Old_Node := First (Plist);
9645 while Present (Old_Node) loop
9646 New_Node := New_Copy (Old_Node);
9648 if Nkind (New_Node) = N_Range then
9649 Set_Low_Bound (New_Node, New_Copy (Low_Bound (Old_Node)));
9650 Set_High_Bound (New_Node, New_Copy (High_Bound (Old_Node)));
9653 Append_To (New_Alts, New_Node);
9657 -- If empty list, replace by False
9659 if Is_Empty_List (New_Alts) then
9660 Rewrite (Expr, New_Occurrence_Of (Standard_False, Loc));
9662 -- Else replace by set membership test
9667 Left_Opnd => Make_Identifier (Loc, Nam),
9668 Right_Opnd => Empty,
9669 Alternatives => New_Alts));
9671 -- Resolve new expression in function context
9673 Install_Formals (Predicate_Function (Typ));
9674 Push_Scope (Predicate_Function (Typ));
9675 Analyze_And_Resolve (Expr, Standard_Boolean);
9680 end Build_Discrete_Static_Predicate;
9682 --------------------------------
9683 -- Build_Export_Import_Pragma --
9684 --------------------------------
9686 function Build_Export_Import_Pragma
9688 Id : Entity_Id) return Node_Id
9690 Asp_Id : constant Aspect_Id := Get_Aspect_Id (Asp);
9691 Expr : constant Node_Id := Expression (Asp);
9692 Loc : constant Source_Ptr := Sloc (Asp);
9703 Create_Pragma : Boolean := False;
9704 -- This flag is set when the aspect form is such that it warrants the
9705 -- creation of a corresponding pragma.
9708 if Present (Expr) then
9709 if Error_Posted (Expr) then
9712 elsif Is_True (Expr_Value (Expr)) then
9713 Create_Pragma := True;
9716 -- Otherwise the aspect defaults to True
9719 Create_Pragma := True;
9722 -- Nothing to do when the expression is False or is erroneous
9724 if not Create_Pragma then
9728 -- Obtain all interfacing aspects that apply to the related entity
9730 Get_Interfacing_Aspects
9734 Expo_Asp => Dummy_1,
9740 -- Handle the convention argument
9742 if Present (Conv) then
9743 Conv_Arg := New_Copy_Tree (Expression (Conv));
9745 -- Assume convention "Ada' when aspect Convention is missing
9748 Conv_Arg := Make_Identifier (Loc, Name_Ada);
9752 Make_Pragma_Argument_Association (Loc,
9753 Chars => Name_Convention,
9754 Expression => Conv_Arg));
9756 -- Handle the entity argument
9759 Make_Pragma_Argument_Association (Loc,
9760 Chars => Name_Entity,
9761 Expression => New_Occurrence_Of (Id, Loc)));
9763 -- Handle the External_Name argument
9765 if Present (EN) then
9767 Make_Pragma_Argument_Association (Loc,
9768 Chars => Name_External_Name,
9769 Expression => New_Copy_Tree (Expression (EN))));
9772 -- Handle the Link_Name argument
9774 if Present (LN) then
9776 Make_Pragma_Argument_Association (Loc,
9777 Chars => Name_Link_Name,
9778 Expression => New_Copy_Tree (Expression (LN))));
9782 -- pragma Export/Import
9783 -- (Convention => <Conv>/Ada,
9785 -- [External_Name => <EN>,]
9786 -- [Link_Name => <LN>]);
9790 Pragma_Identifier =>
9791 Make_Identifier (Loc, Chars (Identifier (Asp))),
9792 Pragma_Argument_Associations => Args);
9794 -- Decorate the relevant aspect and the pragma
9796 Set_Aspect_Rep_Item (Asp, Prag);
9798 Set_Corresponding_Aspect (Prag, Asp);
9799 Set_From_Aspect_Specification (Prag);
9800 Set_Parent (Prag, Asp);
9802 if Asp_Id = Aspect_Import and then Is_Subprogram (Id) then
9803 Set_Import_Pragma (Id, Prag);
9807 end Build_Export_Import_Pragma;
9809 ------------------------------
9810 -- Build_Predicate_Function --
9811 ------------------------------
9813 -- The function constructed here has the form:
9815 -- function typPredicate (Ixxx : typ) return Boolean is
9818 -- typ1Predicate (typ1 (Ixxx))
9819 -- and then typ2Predicate (typ2 (Ixxx))
9821 -- and then exp1 and then exp2 and then ...;
9822 -- end typPredicate;
9824 -- If Predicate_Function_Needs_Membership_Parameter is true, then this
9825 -- function takes an additional boolean parameter; the parameter
9826 -- indicates whether the predicate evaluation is part of a membership
9827 -- test. This parameter is used in two cases: 1) It is passed along
9828 -- if another predicate function is called and that predicate function
9829 -- expects to be passed a boolean parameter. 2) If the Predicate_Failure
9830 -- aspect is directly specified for typ, then we replace the return
9831 -- expression described above with
9832 -- (if <expression described above> then True
9833 -- elsif For_Membership_Test then False
9834 -- else (raise Assertion_Error
9835 -- with <Predicate_Failure expression>))
9836 -- Here exp1, and exp2 are expressions from Predicate pragmas. Note that
9837 -- this is the point at which these expressions get analyzed, providing the
9838 -- required delay, and typ1, typ2, are entities from which predicates are
9839 -- inherited. Note that we do NOT generate Check pragmas, that's because we
9840 -- use this function even if checks are off, e.g. for membership tests.
9842 -- Note that the inherited predicates are evaluated first, as required by
9845 -- Note that Sem_Eval.Real_Or_String_Static_Predicate_Matches depends on
9846 -- the form of this return expression.
9848 -- WARNING: This routine manages Ghost regions. Return statements must be
9849 -- replaced by gotos which jump to the end of the routine and restore the
9852 procedure Build_Predicate_Function (Typ : Entity_Id; N : Node_Id) is
9853 Loc : constant Source_Ptr := Sloc (Typ);
9856 -- This is the expression for the result of the function. It is
9857 -- is build by connecting the component predicates with AND THEN.
9859 Object_Name : Name_Id;
9860 -- Name for argument of Predicate procedure. Note that we use the same
9861 -- name for both predicate functions. That way the reference within the
9862 -- predicate expression is the same in both functions.
9864 Object_Entity : Entity_Id;
9865 -- Entity for argument of Predicate procedure
9868 -- The function declaration
9873 Ancestor_Predicate_Function_Called : Boolean := False;
9874 -- Does this predicate function include a call to the
9875 -- predication function of an ancestor subtype?
9877 procedure Add_Condition (Cond : Node_Id);
9878 -- Append Cond to Expr using "and then" (or just copy Cond to Expr if
9881 procedure Add_Predicates;
9882 -- Appends expressions for any Predicate pragmas in the rep item chain
9883 -- Typ to Expr. Note that we look only at items for this exact entity.
9884 -- Inheritance of predicates for the parent type is done by calling the
9885 -- Predicate_Function of the parent type, using Add_Call above.
9887 procedure Add_Call (T : Entity_Id);
9888 -- Includes a call to the predicate function for type T in Expr if
9889 -- Predicate_Function (T) is non-empty.
9891 procedure Replace_Current_Instance_References
9892 (N : Node_Id; Typ, New_Entity : Entity_Id);
9893 -- Replace all references to Typ in the tree rooted at N with
9894 -- references to Param. [New_Entity will be a formal parameter of a
9895 -- predicate function.]
9901 procedure Add_Call (T : Entity_Id) is
9905 if Present (Predicate_Function (T)) then
9906 pragma Assert (Has_Predicates (Typ));
9908 -- Build the call to the predicate function of T. The type may be
9909 -- derived, so use an unchecked conversion for the actual.
9912 Dynamic_Mem : Node_Id := Empty;
9913 Second_Formal : constant Entity_Id :=
9914 Next_Entity (Object_Entity);
9916 -- Some predicate functions require a second parameter;
9917 -- If one predicate function calls another and the second
9918 -- requires two parameters, then the first should also
9919 -- take two parameters (so that the first function has
9920 -- something to pass to the second function).
9921 if Predicate_Function_Needs_Membership_Parameter (T) then
9922 pragma Assert (Present (Second_Formal));
9923 Dynamic_Mem := New_Occurrence_Of (Second_Formal, Loc);
9930 Unchecked_Convert_To (T,
9931 Make_Identifier (Loc, Object_Name)),
9932 Dynamic_Mem => Dynamic_Mem);
9935 -- "and"-in the call to evolving expression
9937 Add_Condition (Exp);
9938 Ancestor_Predicate_Function_Called := True;
9940 -- Output info message on inheritance if required. Note we do not
9941 -- give this information for generic actual types, since it is
9942 -- unwelcome noise in that case in instantiations. We also
9943 -- generally suppress the message in instantiations, and also
9944 -- if it involves internal names.
9946 if List_Inherited_Aspects
9947 and then not Is_Generic_Actual_Type (Typ)
9948 and then Instantiation_Location (Sloc (Typ)) = No_Location
9949 and then not Is_Internal_Name (Chars (T))
9950 and then not Is_Internal_Name (Chars (Typ))
9952 Error_Msg_Sloc := Sloc (Predicate_Function (T));
9953 Error_Msg_Node_2 := T;
9954 Error_Msg_N ("info: & inherits predicate from & #?.l?", Typ);
9963 procedure Add_Condition (Cond : Node_Id) is
9965 -- This is the first predicate expression
9970 -- Otherwise concatenate to the existing predicate expressions by
9971 -- using "and then".
9976 Left_Opnd => Relocate_Node (Expr),
9977 Right_Opnd => Cond);
9981 --------------------
9982 -- Add_Predicates --
9983 --------------------
9985 procedure Add_Predicates is
9986 procedure Add_Predicate (Prag : Node_Id);
9987 -- Concatenate the expression of predicate pragma Prag to Expr by
9988 -- using a short circuit "and then" operator.
9994 procedure Add_Predicate (Prag : Node_Id) is
9997 Asp : constant Node_Id := Corresponding_Aspect (Prag);
10001 -- Start of processing for Add_Predicate
10004 -- Mark corresponding SCO as enabled
10006 Set_SCO_Pragma_Enabled (Sloc (Prag));
10008 -- Extract the arguments of the pragma
10010 Arg1 := First (Pragma_Argument_Associations (Prag));
10011 Arg2 := Next (Arg1);
10013 Arg1 := Get_Pragma_Arg (Arg1);
10014 Arg2 := Get_Pragma_Arg (Arg2);
10016 -- When the predicate pragma applies to the current type or its
10017 -- full view, replace all occurrences of the subtype name with
10018 -- references to the formal parameter of the predicate function.
10020 if Entity (Arg1) = Typ
10021 or else Full_View (Entity (Arg1)) = Typ
10024 Arg2_Copy : constant Node_Id := New_Copy_Tree (Arg2);
10026 Replace_Current_Instance_References
10027 (Arg2_Copy, Typ => Typ, New_Entity => Object_Entity);
10029 -- If the predicate pragma comes from an aspect, replace the
10030 -- saved expression because we need the subtype references
10031 -- replaced for the calls to Preanalyze_Spec_Expression in
10032 -- Check_Aspect_At_xxx routines.
10034 if Present (Asp) then
10035 Set_Entity (Identifier (Asp), New_Copy_Tree (Arg2_Copy));
10038 -- "and"-in the Arg2 condition to evolving expression
10040 Add_Condition (Arg2_Copy);
10049 -- Start of processing for Add_Predicates
10052 Ritem := First_Rep_Item (Typ);
10054 -- If the type is private, check whether full view has inherited
10057 if Is_Private_Type (Typ)
10058 and then No (Ritem)
10059 and then Present (Full_View (Typ))
10061 Ritem := First_Rep_Item (Full_View (Typ));
10064 while Present (Ritem) loop
10065 if Nkind (Ritem) = N_Pragma
10066 and then Pragma_Name (Ritem) = Name_Predicate
10068 Add_Predicate (Ritem);
10070 -- If the type is declared in an inner package it may be frozen
10071 -- outside of the package, and the generated pragma has not been
10072 -- analyzed yet, so capture the expression for the predicate
10073 -- function at this point.
10075 elsif Nkind (Ritem) = N_Aspect_Specification
10076 and then Present (Aspect_Rep_Item (Ritem))
10077 and then Scope_Depth (Scope (Typ)) > Scope_Depth (Current_Scope)
10080 Prag : constant Node_Id := Aspect_Rep_Item (Ritem);
10083 if Nkind (Prag) = N_Pragma
10084 and then Pragma_Name (Prag) = Name_Predicate
10086 Add_Predicate (Prag);
10091 Next_Rep_Item (Ritem);
10093 end Add_Predicates;
10095 -----------------------------------------
10096 -- Replace_Current_Instance_References --
10097 -----------------------------------------
10099 procedure Replace_Current_Instance_References
10100 (N : Node_Id; Typ, New_Entity : Entity_Id)
10102 Root : Node_Id renames N;
10104 procedure Replace_One_Reference (N : Node_Id);
10105 -- Actual parameter for Replace_Type_References_Generic instance
10107 ---------------------------
10108 -- Replace_One_Reference --
10109 ---------------------------
10111 procedure Replace_One_Reference (N : Node_Id) is
10112 pragma Assert (In_Subtree (N, Root => Root));
10114 Rewrite (N, New_Occurrence_Of (New_Entity, Sloc (N)));
10115 -- Use the Sloc of the usage name, not the defining name
10116 end Replace_One_Reference;
10118 procedure Replace_Type_References is
10119 new Replace_Type_References_Generic (Replace_One_Reference);
10121 Replace_Type_References (N, Typ);
10122 end Replace_Current_Instance_References;
10126 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
10127 Saved_IGR : constant Node_Id := Ignored_Ghost_Region;
10128 -- Save the Ghost-related attributes to restore on exit
10130 -- Start of processing for Build_Predicate_Function
10133 -- Return if already built, if type does not have predicates,
10134 -- or if type is a constructed subtype that will inherit a
10135 -- predicate function from its ancestor. In a generic context
10136 -- the predicated parent may not have a predicate function yet
10137 -- but we don't want to build a new one for the subtype. This can
10138 -- happen in an instance body which is nested within a generic
10139 -- unit, in which case Within_A_Generic may be false, SId is
10140 -- Empty, but uses of Typ will receive a predicate check in a
10141 -- context where expansion and tests are enabled.
10143 SId := Predicate_Function (Typ);
10144 if not Has_Predicates (Typ)
10145 or else (Present (SId) and then Has_Completion (SId))
10148 and then not Comes_From_Source (Typ)
10149 and then Ekind (Typ) in E_Array_Subtype
10151 | E_Record_Subtype_With_Private
10152 and then Present (Predicated_Parent (Typ)))
10156 -- Do not generate predicate bodies within a generic unit. The
10157 -- expressions have been analyzed already, and the bodies play no role
10158 -- if not within an executable unit. However, if a static predicate is
10159 -- present it must be processed for legality checks such as case
10160 -- coverage in an expression.
10162 elsif Inside_A_Generic
10163 and then not Has_Static_Predicate_Aspect (Typ)
10168 -- The related type may be subject to pragma Ghost. Set the mode now to
10169 -- ensure that the predicate functions are properly marked as Ghost.
10171 Set_Ghost_Mode (Typ);
10173 -- Prepare to construct predicate expression
10177 if Present (SId) then
10178 FDecl := Unit_Declaration_Node (SId);
10181 FDecl := Build_Predicate_Function_Declaration (Typ);
10182 SId := Defining_Entity (FDecl);
10185 -- Recover name of formal parameter of function that replaces references
10186 -- to the type in predicate expressions.
10189 Defining_Identifier
10190 (First (Parameter_Specifications (Specification (FDecl))));
10192 Object_Name := Chars (Object_Entity);
10194 -- Add predicates for ancestor if present. These must come before the
10195 -- ones for the current type, as required by AI12-0071-1.
10197 -- Looks like predicates aren't added for case of inheriting from
10198 -- multiple progenitors???
10203 Atyp := Nearest_Ancestor (Typ);
10205 -- The type may be private but the full view may inherit predicates
10207 if No (Atyp) and then Is_Private_Type (Typ) then
10208 Atyp := Nearest_Ancestor (Full_View (Typ));
10211 if Present (Atyp) then
10216 -- Add Predicates for the current type
10220 -- Case where predicates are present
10222 if Present (Expr) then
10224 -- Build the main predicate function
10227 SIdB : constant Entity_Id :=
10228 Make_Defining_Identifier (Loc,
10229 Chars => New_External_Name (Chars (Typ), "Predicate"));
10230 -- The entity for the function body
10236 Mutate_Ekind (SIdB, E_Function);
10237 Set_Is_Predicate_Function (SIdB);
10239 -- Build function body
10242 Param_Specs : constant List_Id := New_List (
10243 Make_Parameter_Specification (Loc,
10244 Defining_Identifier =>
10245 Make_Defining_Identifier (Loc, Object_Name),
10247 New_Occurrence_Of (Typ, Loc)));
10249 -- if Spec has 2 parameters, then body should too
10250 if Present (Next_Entity (Object_Entity)) then
10251 Append (Make_Parameter_Specification (Loc,
10252 Defining_Identifier =>
10253 Make_Defining_Identifier
10254 (Loc, Chars (Next_Entity (Object_Entity))),
10256 New_Occurrence_Of (Standard_Boolean, Loc)),
10261 Make_Function_Specification (Loc,
10262 Defining_Unit_Name => SIdB,
10263 Parameter_Specifications => Param_Specs,
10264 Result_Definition =>
10265 New_Occurrence_Of (Standard_Boolean, Loc));
10268 -- The Predicate_Expression attribute is used by SPARK.
10270 -- If Ancestor_Predicate_Function_Called is True, then
10271 -- we try to exclude that call to the ancestor's
10272 -- predicate function by calling Right_Opnd.
10273 -- The call is not excluded in the case where
10274 -- it is not "and"ed with anything else (so we don't have
10275 -- an N_And_Then node). This exclusion is required if the
10276 -- Predicate_Failure aspect is specified for Typ because
10277 -- in that case we are going to drop the N_And_Then node
10278 -- on the floor. Otherwise, it is a question of what is
10279 -- most convenient for SPARK.
10281 Set_Predicate_Expression
10282 (SId, (if Ancestor_Predicate_Function_Called
10283 and then Nkind (Expr) = N_And_Then
10284 then Right_Opnd (Expr)
10288 Result_Expr : Node_Id := Expr;
10289 PF_Expr : Node_Id := Predicate_Failure_Expression
10290 (Typ, Inherited_OK => False);
10291 PF_Expr_Copy : Node_Id;
10292 Second_Formal : constant Entity_Id :=
10293 Next_Entity (Object_Entity);
10295 -- In GNATprove mode we are only interested in the predicate
10296 -- expression itself and don't want a raise expression that
10297 -- comes from the Predicate_Failure. Ditto for CodePeer.
10298 -- And an illegal Predicate_Failure aspect can lead to cases
10299 -- we want to avoid.
10301 if Present (PF_Expr)
10302 and then not GNATprove_Mode
10303 and then not CodePeer_Mode
10304 and then Serious_Errors_Detected = 0
10306 pragma Assert (Present (Second_Formal));
10308 -- This is an ugly hack to cope with an ugly situation.
10309 -- PF_Expr may have children whose Parent attribute
10310 -- does not point back to PF_Expr. If we pass such a
10311 -- tree to New_Copy_Tree, then it does not make a deep
10312 -- copy. But we need a deep copy. So we need to find a
10313 -- tree for which New_Copy_Tree *will* make a deep copy.
10316 function Check_Node_Parent (Parent_Node, Node : Node_Id)
10317 return Traverse_Result;
10318 function Check_Node_Parent (Parent_Node, Node : Node_Id)
10319 return Traverse_Result is
10321 if Parent_Node = PF_Expr
10322 and then not Is_List_Member (Node)
10325 (Nkind (PF_Expr) = Nkind (Parent (Node)));
10327 -- We need PF_Expr to be a node for which
10328 -- New_Copy_Tree will make a deep copy.
10329 PF_Expr := Parent (Node);
10333 end Check_Node_Parent;
10334 procedure Check_Parentage is
10335 new Traverse_Proc_With_Parent (Check_Node_Parent);
10337 Check_Parentage (PF_Expr);
10338 PF_Expr_Copy := New_Copy_Tree (PF_Expr);
10341 -- Current instance uses need to have their Entity
10342 -- fields set so that Replace_Current_Instance_References
10343 -- can find them. So we preanalyze. Just for purposes of
10344 -- calls to Is_Current_Instance during this preanalysis,
10345 -- we set the Parent field.
10346 Set_Parent (PF_Expr_Copy, Parent (PF_Expr));
10347 Preanalyze (PF_Expr_Copy);
10348 Set_Parent (PF_Expr_Copy, Empty);
10350 Replace_Current_Instance_References
10351 (PF_Expr_Copy, Typ => Typ, New_Entity => Object_Entity);
10353 if Ancestor_Predicate_Function_Called then
10354 -- If the call to an ancestor predicate function
10355 -- returns False, we do not want to raise an
10356 -- exception here. Our Predicate_Failure aspect does
10357 -- not apply in that case. So we have to build a
10358 -- more complicated result expression:
10359 -- (if not Ancestor_Predicate_Function (...) then False
10360 -- elsif Noninherited_Predicates (...) then True
10361 -- elsif Is_Membership_Test then False
10362 -- else (raise Assertion_Error with PF text))
10365 Ancestor_Call : constant Node_Id :=
10366 Left_Opnd (Result_Expr);
10367 Local_Preds : constant Node_Id :=
10368 Right_Opnd (Result_Expr);
10371 Make_If_Expression (Loc,
10372 Expressions => New_List (
10373 Make_Op_Not (Loc, Ancestor_Call),
10374 New_Occurrence_Of (Standard_False, Loc),
10375 Make_If_Expression (Loc,
10377 Expressions => New_List (
10379 New_Occurrence_Of (Standard_True, Loc),
10380 Make_If_Expression (Loc,
10382 Expressions => New_List (
10383 New_Occurrence_Of (Second_Formal, Loc),
10384 New_Occurrence_Of (Standard_False, Loc),
10385 Make_Raise_Expression (Loc,
10386 New_Occurrence_Of (RTE
10387 (RE_Assert_Failure), Loc),
10388 PF_Expr_Copy)))))));
10392 -- Build a conditional expression:
10393 -- (if <predicate evaluates to True> then True
10394 -- elsif Is_Membership_Test then False
10395 -- else (raise Assertion_Error with PF text))
10398 Make_If_Expression (Loc,
10399 Expressions => New_List (
10401 New_Occurrence_Of (Standard_True, Loc),
10402 Make_If_Expression (Loc,
10404 Expressions => New_List (
10405 New_Occurrence_Of (Second_Formal, Loc),
10406 New_Occurrence_Of (Standard_False, Loc),
10407 Make_Raise_Expression (Loc,
10408 New_Occurrence_Of (RTE
10409 (RE_Assert_Failure), Loc),
10415 Make_Subprogram_Body (Loc,
10416 Specification => Spec,
10417 Declarations => Empty_List,
10418 Handled_Statement_Sequence =>
10419 Make_Handled_Sequence_Of_Statements (Loc,
10420 Statements => New_List (
10421 Make_Simple_Return_Statement (Loc,
10422 Expression => Result_Expr))));
10425 -- The declaration has been analyzed when created, and placed
10426 -- after type declaration. Insert body itself after freeze node,
10427 -- unless subprogram declaration is already there, in which case
10428 -- body better be placed afterwards.
10430 if FDecl = Next (N) then
10431 Insert_After_And_Analyze (FDecl, FBody);
10433 Insert_After_And_Analyze (N, FBody);
10436 -- The defining identifier of a quantified expression carries the
10437 -- scope in which the type appears, but when unnesting we need
10438 -- to indicate that its proper scope is the constructed predicate
10439 -- function. The quantified expressions have been converted into
10440 -- loops during analysis and expansion.
10443 function Reset_Quantified_Variable_Scope
10444 (N : Node_Id) return Traverse_Result;
10446 procedure Reset_Quantified_Variables_Scope is
10447 new Traverse_Proc (Reset_Quantified_Variable_Scope);
10449 -------------------------------------
10450 -- Reset_Quantified_Variable_Scope --
10451 -------------------------------------
10453 function Reset_Quantified_Variable_Scope
10454 (N : Node_Id) return Traverse_Result is
10456 if Nkind (N) in N_Iterator_Specification
10457 | N_Loop_Parameter_Specification
10459 Set_Scope (Defining_Identifier (N),
10460 Predicate_Function (Typ));
10464 end Reset_Quantified_Variable_Scope;
10467 if Unnest_Subprogram_Mode then
10468 Reset_Quantified_Variables_Scope (Expr);
10472 -- Within a generic unit, prevent a double analysis of the body
10473 -- which will not be marked analyzed yet. This will happen when
10474 -- the freeze node is created during the preanalysis of an
10475 -- expression function.
10477 if Inside_A_Generic then
10478 Set_Analyzed (FBody);
10481 -- Static predicate functions are always side-effect free, and
10482 -- in most cases dynamic predicate functions are as well. Mark
10483 -- them as such whenever possible, so redundant predicate checks
10484 -- can be optimized. If there is a variable reference within the
10485 -- expression, the function is not pure.
10487 if Expander_Active then
10489 Side_Effect_Free (Expr, Variable_Ref => True));
10490 Set_Is_Inlined (SId);
10494 -- See if we have a static predicate. Note that the answer may be
10495 -- yes even if we have an explicit Dynamic_Predicate present.
10502 if not Is_Scalar_Type (Typ) and then not Is_String_Type (Typ) then
10505 PS := Is_Predicate_Static (Expr, Object_Name);
10508 -- Case where we have a predicate-static aspect
10512 -- We don't set Has_Static_Predicate_Aspect, since we can have
10513 -- any of the three cases (Predicate, Dynamic_Predicate, or
10514 -- Static_Predicate) generating a predicate with an expression
10515 -- that is predicate-static. We just indicate that we have a
10516 -- predicate that can be treated as static.
10518 Set_Has_Static_Predicate (Typ);
10520 -- For discrete subtype, build the static predicate list
10522 if Is_Discrete_Type (Typ) then
10523 Build_Discrete_Static_Predicate (Typ, Expr, Object_Name);
10525 -- If we don't get a static predicate list, it means that we
10526 -- have a case where this is not possible, most typically in
10527 -- the case where we inherit a dynamic predicate. We do not
10528 -- consider this an error, we just leave the predicate as
10529 -- dynamic. But if we do succeed in building the list, then
10530 -- we mark the predicate as static.
10532 if No (Static_Discrete_Predicate (Typ)) then
10533 Set_Has_Static_Predicate (Typ, False);
10536 -- For real or string subtype, save predicate expression
10538 elsif Is_Real_Type (Typ) or else Is_String_Type (Typ) then
10539 Set_Static_Real_Or_String_Predicate (Typ, Expr);
10542 -- Case of dynamic predicate (expression is not predicate-static)
10545 -- Again, we don't set Has_Dynamic_Predicate_Aspect, since that
10546 -- is only set if we have an explicit Dynamic_Predicate aspect
10547 -- given. Here we may simply have a Predicate aspect where the
10548 -- expression happens not to be predicate-static.
10550 -- Emit an error when the predicate is categorized as static
10551 -- but its expression is not predicate-static.
10553 -- First a little fiddling to get a nice location for the
10554 -- message. If the expression is of the form (A and then B),
10555 -- where A is an inherited predicate, then use the right
10556 -- operand for the Sloc. This avoids getting confused by a call
10557 -- to an inherited predicate with a less convenient source
10561 while Nkind (EN) = N_And_Then
10562 and then Nkind (Left_Opnd (EN)) = N_Function_Call
10563 and then Is_Predicate_Function
10564 (Entity (Name (Left_Opnd (EN))))
10566 EN := Right_Opnd (EN);
10569 -- Now post appropriate message
10571 if Has_Static_Predicate_Aspect (Typ) then
10572 if Is_Scalar_Type (Typ) or else Is_String_Type (Typ) then
10574 ("expression is not predicate-static (RM 3.2.4(16-22))",
10578 ("static predicate requires scalar or string type", EN);
10585 Restore_Ghost_Region (Saved_GM, Saved_IGR);
10586 end Build_Predicate_Function;
10588 ------------------------------------------
10589 -- Build_Predicate_Function_Declaration --
10590 ------------------------------------------
10592 -- WARNING: This routine manages Ghost regions. Return statements must be
10593 -- replaced by gotos which jump to the end of the routine and restore the
10596 function Build_Predicate_Function_Declaration
10597 (Typ : Entity_Id) return Node_Id
10599 Loc : constant Source_Ptr := Sloc (Typ);
10601 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
10602 Saved_IGR : constant Node_Id := Ignored_Ghost_Region;
10603 -- Save the Ghost-related attributes to restore on exit
10605 Func_Decl : Node_Id;
10606 Func_Id : Entity_Id;
10609 CRec_Typ : Entity_Id;
10610 -- The corresponding record type of Full_Typ
10612 Full_Typ : Entity_Id;
10613 -- The full view of Typ
10615 Priv_Typ : Entity_Id;
10616 -- The partial view of Typ
10618 UFull_Typ : Entity_Id;
10619 -- The underlying full view of Full_Typ
10622 -- The related type may be subject to pragma Ghost. Set the mode now to
10623 -- ensure that the predicate functions are properly marked as Ghost.
10625 Set_Ghost_Mode (Typ);
10628 Make_Defining_Identifier (Loc,
10629 Chars => New_External_Name (Chars (Typ), "Predicate"));
10631 Mutate_Ekind (Func_Id, E_Function);
10632 Set_Etype (Func_Id, Standard_Boolean);
10633 Set_Is_Internal (Func_Id);
10634 Set_Is_Predicate_Function (Func_Id);
10635 Set_Predicate_Function (Typ, Func_Id);
10637 -- The predicate function requires debug info when the predicates are
10638 -- subject to Source Coverage Obligations.
10640 if Opt.Generate_SCO then
10641 Set_Debug_Info_Needed (Func_Id);
10644 -- Obtain all views of the input type
10646 Get_Views (Typ, Priv_Typ, Full_Typ, UFull_Typ, CRec_Typ);
10648 -- Associate the predicate function and various flags with all views
10650 Propagate_Predicate_Attributes (Priv_Typ, From_Typ => Typ);
10651 Propagate_Predicate_Attributes (Full_Typ, From_Typ => Typ);
10652 Propagate_Predicate_Attributes (UFull_Typ, From_Typ => Typ);
10653 Propagate_Predicate_Attributes (CRec_Typ, From_Typ => Typ);
10656 Param_Specs : constant List_Id := New_List (
10657 Make_Parameter_Specification (Loc,
10658 Defining_Identifier => Make_Temporary (Loc, 'I'),
10659 Parameter_Type => New_Occurrence_Of (Typ, Loc)));
10661 if Predicate_Function_Needs_Membership_Parameter (Typ) then
10662 -- Add Boolean-valued For_Membership_Test param
10663 Append (Make_Parameter_Specification (Loc,
10664 Defining_Identifier => Make_Temporary (Loc, 'M'),
10666 New_Occurrence_Of (Standard_Boolean, Loc)),
10671 Make_Function_Specification (Loc,
10672 Defining_Unit_Name => Func_Id,
10673 Parameter_Specifications => Param_Specs,
10674 Result_Definition =>
10675 New_Occurrence_Of (Standard_Boolean, Loc));
10678 Func_Decl := Make_Subprogram_Declaration (Loc, Specification => Spec);
10680 Insert_After (Parent (Typ), Func_Decl);
10681 Analyze (Func_Decl);
10683 Restore_Ghost_Region (Saved_GM, Saved_IGR);
10686 end Build_Predicate_Function_Declaration;
10688 -----------------------------------------
10689 -- Check_Aspect_At_End_Of_Declarations --
10690 -----------------------------------------
10692 procedure Check_Aspect_At_End_Of_Declarations (ASN : Node_Id) is
10693 Ent : constant Entity_Id := Entity (ASN);
10694 Ident : constant Node_Id := Identifier (ASN);
10695 A_Id : constant Aspect_Id := Get_Aspect_Id (Chars (Ident));
10697 End_Decl_Expr : constant Node_Id := Entity (Ident);
10698 -- Expression to be analyzed at end of declarations
10700 Freeze_Expr : constant Node_Id := Expression (ASN);
10701 -- Expression from call to Check_Aspect_At_Freeze_Point.
10703 T : constant Entity_Id :=
10704 (if Present (Freeze_Expr) and (A_Id /= Aspect_Stable_Properties)
10705 then Etype (Original_Node (Freeze_Expr))
10707 -- Type required for preanalyze call. We use the original expression to
10708 -- get the proper type, to prevent cascaded errors when the expression
10709 -- is constant-folded. For Stable_Properties, the aspect value is
10710 -- not semantically an expression (although it is syntactically);
10711 -- in particular, it has no type.
10714 -- Set True if error
10716 -- On entry to this procedure, Entity (Ident) contains a copy of the
10717 -- original expression from the aspect, saved for this purpose, and
10718 -- but Expression (Ident) is a preanalyzed copy of the expression,
10719 -- preanalyzed just after the freeze point.
10721 procedure Check_Overloaded_Name;
10722 -- For aspects whose expression is simply a name, this routine checks if
10723 -- the name is overloaded or not. If so, it verifies there is an
10724 -- interpretation that matches the entity obtained at the freeze point,
10725 -- otherwise the compiler complains.
10727 ---------------------------
10728 -- Check_Overloaded_Name --
10729 ---------------------------
10731 procedure Check_Overloaded_Name is
10733 if not Is_Overloaded (End_Decl_Expr) then
10734 Err := not Is_Entity_Name (End_Decl_Expr)
10735 or else Entity (End_Decl_Expr) /= Entity (Freeze_Expr);
10741 Index : Interp_Index;
10745 Get_First_Interp (End_Decl_Expr, Index, It);
10746 while Present (It.Typ) loop
10747 if It.Nam = Entity (Freeze_Expr) then
10752 Get_Next_Interp (Index, It);
10756 end Check_Overloaded_Name;
10758 -- Start of processing for Check_Aspect_At_End_Of_Declarations
10761 -- In an instance we do not perform the consistency check between freeze
10762 -- point and end of declarations, because it was done already in the
10763 -- analysis of the generic. Furthermore, the delayed analysis of an
10764 -- aspect of the instance may produce spurious errors when the generic
10765 -- is a child unit that references entities in the parent (which might
10766 -- not be in scope at the freeze point of the instance).
10768 if In_Instance then
10771 -- The enclosing scope may have been rewritten during expansion (.e.g. a
10772 -- task body is rewritten as a procedure) after this conformance check
10773 -- has been performed, so do not perform it again (it may not easily be
10774 -- done if full visibility of local entities is not available).
10776 elsif not Comes_From_Source (Current_Scope) then
10779 -- Case of aspects Dimension, Dimension_System and Synchronization
10781 elsif A_Id = Aspect_Synchronization then
10784 -- Case of stream attributes and Put_Image, just have to compare
10785 -- entities. However, the expression is just a possibly-overloaded
10786 -- name, so we need to verify that one of these interpretations is
10787 -- the one available at at the freeze point.
10789 elsif A_Id in Aspect_Input
10795 Analyze (End_Decl_Expr);
10796 Check_Overloaded_Name;
10798 elsif A_Id in Aspect_Variable_Indexing
10799 | Aspect_Constant_Indexing
10800 | Aspect_Default_Iterator
10801 | Aspect_Iterator_Element
10802 | Aspect_Integer_Literal
10803 | Aspect_Real_Literal
10804 | Aspect_String_Literal
10806 -- Make type unfrozen before analysis, to prevent spurious errors
10807 -- about late attributes.
10809 Set_Is_Frozen (Ent, False);
10810 Analyze (End_Decl_Expr);
10811 Set_Is_Frozen (Ent, True);
10813 -- If the end of declarations comes before any other freeze point,
10814 -- the Freeze_Expr is not analyzed: no check needed.
10816 if Analyzed (Freeze_Expr) and then not In_Instance then
10817 Check_Overloaded_Name;
10825 -- In a generic context freeze nodes are not always generated, so
10826 -- analyze the expression now. If the aspect is for a type, we must
10827 -- also make its potential components accessible.
10829 if not Analyzed (Freeze_Expr) and then Inside_A_Generic then
10830 if A_Id in Aspect_Dynamic_Predicate | Aspect_Predicate |
10831 Aspect_Static_Predicate
10834 Preanalyze_Spec_Expression (Freeze_Expr, Standard_Boolean);
10837 elsif A_Id = Aspect_Priority then
10839 Preanalyze_Spec_Expression (Freeze_Expr, Any_Integer);
10843 Preanalyze (Freeze_Expr);
10847 -- Indicate that the expression comes from an aspect specification,
10848 -- which is used in subsequent analysis even if expansion is off.
10850 if Present (End_Decl_Expr) then
10851 Set_Parent (End_Decl_Expr, ASN);
10854 -- In a generic context the original aspect expressions have not
10855 -- been preanalyzed, so do it now. There are no conformance checks
10856 -- to perform in this case. As before, we have to make components
10857 -- visible for aspects that may reference them.
10859 if Present (Freeze_Expr) and then No (T) then
10860 if A_Id in Aspect_Dynamic_Predicate
10863 | Aspect_Static_Predicate
10866 Check_Aspect_At_Freeze_Point (ASN);
10870 Check_Aspect_At_Freeze_Point (ASN);
10874 -- The default values attributes may be defined in the private part,
10875 -- and the analysis of the expression may take place when only the
10876 -- partial view is visible. The expression must be scalar, so use
10877 -- the full view to resolve.
10879 elsif A_Id in Aspect_Default_Component_Value | Aspect_Default_Value
10880 and then Is_Private_Type (T)
10882 Preanalyze_Spec_Expression (End_Decl_Expr, Full_View (T));
10884 -- The following aspect expressions may contain references to
10885 -- components and discriminants of the type.
10887 elsif A_Id in Aspect_CPU
10888 | Aspect_Dynamic_Predicate
10891 | Aspect_Static_Predicate
10894 Preanalyze_Spec_Expression (End_Decl_Expr, T);
10897 elsif A_Id = Aspect_Predicate_Failure then
10898 Preanalyze_Spec_Expression (End_Decl_Expr, Standard_String);
10899 elsif Present (End_Decl_Expr) then
10900 Preanalyze_Spec_Expression (End_Decl_Expr, T);
10904 not Fully_Conformant_Expressions
10905 (End_Decl_Expr, Freeze_Expr, Report => True);
10908 -- Output error message if error. Force error on aspect specification
10909 -- even if there is an error on the expression itself.
10913 ("!visibility of aspect for& changes after freeze point",
10916 ("info: & is frozen here, (RM 13.1.1 (13/3))??",
10917 Freeze_Node (Ent), Ent);
10919 end Check_Aspect_At_End_Of_Declarations;
10921 ----------------------------------
10922 -- Check_Aspect_At_Freeze_Point --
10923 ----------------------------------
10925 procedure Check_Aspect_At_Freeze_Point (ASN : Node_Id) is
10926 Ident : constant Node_Id := Identifier (ASN);
10927 -- Identifier (use Entity field to save expression)
10929 Expr : constant Node_Id := Expression (ASN);
10930 -- For cases where using Entity (Identifier) doesn't work
10932 A_Id : constant Aspect_Id := Get_Aspect_Id (Chars (Ident));
10934 T : Entity_Id := Empty;
10935 -- Type required for preanalyze call
10938 -- On entry to this procedure, Entity (Ident) contains a copy of the
10939 -- original expression from the aspect, saved for this purpose.
10941 -- On exit from this procedure Entity (Ident) is unchanged, still
10942 -- containing that copy, but Expression (Ident) is a preanalyzed copy
10943 -- of the expression, preanalyzed just after the freeze point.
10945 -- Make a copy of the expression to be preanalyzed
10947 Set_Expression (ASN, New_Copy_Tree (Entity (Ident)));
10949 -- Find type for preanalyze call
10953 -- No_Aspect should be impossible
10956 raise Program_Error;
10958 -- Aspects taking an optional boolean argument
10960 when Boolean_Aspects
10961 | Library_Unit_Aspects
10963 T := Standard_Boolean;
10965 -- Aspects corresponding to attribute definition clauses
10967 when Aspect_Address =>
10968 T := RTE (RE_Address);
10970 when Aspect_Attach_Handler =>
10971 T := RTE (RE_Interrupt_ID);
10973 when Aspect_Bit_Order
10974 | Aspect_Scalar_Storage_Order
10976 T := RTE (RE_Bit_Order);
10978 when Aspect_Convention =>
10982 T := RTE (RE_CPU_Range);
10984 -- Default_Component_Value is resolved with the component type
10986 when Aspect_Default_Component_Value =>
10987 T := Component_Type (Entity (ASN));
10989 when Aspect_Default_Storage_Pool =>
10990 T := Class_Wide_Type (RTE (RE_Root_Storage_Pool));
10992 -- Default_Value is resolved with the type entity in question
10994 when Aspect_Default_Value =>
10997 when Aspect_Dispatching_Domain =>
10998 T := RTE (RE_Dispatching_Domain);
11000 when Aspect_External_Tag =>
11001 T := Standard_String;
11003 when Aspect_External_Name =>
11004 T := Standard_String;
11006 when Aspect_Link_Name =>
11007 T := Standard_String;
11009 when Aspect_Interrupt_Priority
11012 T := Standard_Integer;
11014 when Aspect_Relative_Deadline =>
11015 T := RTE (RE_Time_Span);
11017 when Aspect_Secondary_Stack_Size =>
11018 T := Standard_Integer;
11020 when Aspect_Small =>
11022 -- Note that the expression can be of any real type (not just a
11023 -- real universal literal) as long as it is a static constant.
11027 -- For a simple storage pool, we have to retrieve the type of the
11028 -- pool object associated with the aspect's corresponding attribute
11029 -- definition clause.
11031 when Aspect_Simple_Storage_Pool =>
11032 T := Etype (Expression (Aspect_Rep_Item (ASN)));
11034 when Aspect_Storage_Pool =>
11035 T := Class_Wide_Type (RTE (RE_Root_Storage_Pool));
11037 when Aspect_Alignment
11038 | Aspect_Component_Size
11039 | Aspect_Machine_Radix
11040 | Aspect_Object_Size
11042 | Aspect_Storage_Size
11043 | Aspect_Stream_Size
11044 | Aspect_Value_Size
11048 when Aspect_Linker_Section =>
11049 T := Standard_String;
11051 when Aspect_Synchronization =>
11054 -- Special case, the expression of these aspects is just an entity
11055 -- that does not need any resolution, so just analyze.
11064 Analyze (Expression (ASN));
11067 -- Same for Iterator aspects, where the expression is a function
11068 -- name. Legality rules are checked separately.
11070 when Aspect_Constant_Indexing
11071 | Aspect_Default_Iterator
11072 | Aspect_Iterator_Element
11073 | Aspect_Variable_Indexing
11075 Analyze (Expression (ASN));
11078 -- Same for Literal aspects, where the expression is a function
11079 -- name. Legality rules are checked separately. Use Expr to avoid
11080 -- losing track of the previous resolution of Expression.
11082 when Aspect_Integer_Literal
11083 | Aspect_Real_Literal
11084 | Aspect_String_Literal
11086 Set_Entity (Expression (ASN), Entity (Expr));
11087 Set_Etype (Expression (ASN), Etype (Expr));
11088 Set_Is_Overloaded (Expression (ASN), False);
11089 Analyze (Expression (ASN));
11092 -- Ditto for Iterable, legality checks in Validate_Iterable_Aspect.
11094 when Aspect_Iterable =>
11098 Cursor : constant Entity_Id := Get_Cursor_Type (ASN, T);
11103 if Cursor = Any_Type then
11107 Assoc := First (Component_Associations (Expression (ASN)));
11108 while Present (Assoc) loop
11109 Expr := Expression (Assoc);
11112 if not Error_Posted (Expr) then
11113 Resolve_Iterable_Operation
11114 (Expr, Cursor, T, Chars (First (Choices (Assoc))));
11123 when Aspect_Aggregate =>
11124 Resolve_Aspect_Aggregate (Entity (ASN), Expression (ASN));
11127 when Aspect_Stable_Properties =>
11128 Resolve_Aspect_Stable_Properties
11129 (Entity (ASN), Expression (ASN),
11130 Class_Present => Class_Present (ASN));
11133 -- Invariant/Predicate take boolean expressions
11135 when Aspect_Dynamic_Predicate
11138 | Aspect_Static_Predicate
11139 | Aspect_Type_Invariant
11141 T := Standard_Boolean;
11143 when Aspect_Predicate_Failure =>
11144 T := Standard_String;
11146 -- As for some other aspects above, the expression of this aspect is
11147 -- just an entity that does not need any resolution, so just analyze.
11149 when Aspect_Designated_Storage_Model =>
11150 Analyze (Expression (ASN));
11153 when Aspect_Storage_Model_Type =>
11155 -- The aggregate argument of Storage_Model_Type is optional, and
11156 -- when not present the aspect defaults to the native storage
11157 -- model (where the address type is System.Address, and other
11158 -- arguments default to corresponding native storage operations).
11160 if No (Expression (ASN)) then
11169 Addr_Type : Entity_Id := Empty;
11172 Assoc := First (Component_Associations (Expression (ASN)));
11173 while Present (Assoc) loop
11174 Expr := Expression (Assoc);
11177 if not Error_Posted (Expr) then
11178 Resolve_Storage_Model_Type_Argument
11179 (Expr, T, Addr_Type, Chars (First (Choices (Assoc))));
11188 -- Here is the list of aspects that don't require delay analysis
11190 when Aspect_Abstract_State
11192 | Aspect_Async_Readers
11193 | Aspect_Async_Writers
11194 | Aspect_Constant_After_Elaboration
11195 | Aspect_Contract_Cases
11196 | Aspect_Default_Initial_Condition
11199 | Aspect_Dimension_System
11200 | Aspect_Effective_Reads
11201 | Aspect_Effective_Writes
11202 | Aspect_Extensions_Visible
11205 | Aspect_GNAT_Annotate
11206 | Aspect_Implicit_Dereference
11207 | Aspect_Initial_Condition
11208 | Aspect_Initializes
11209 | Aspect_Max_Entry_Queue_Depth
11210 | Aspect_Max_Entry_Queue_Length
11211 | Aspect_Max_Queue_Length
11212 | Aspect_No_Caching
11213 | Aspect_No_Controlled_Parts
11214 | Aspect_No_Task_Parts
11215 | Aspect_Obsolescent
11218 | Aspect_Postcondition
11220 | Aspect_Precondition
11221 | Aspect_Refined_Depends
11222 | Aspect_Refined_Global
11223 | Aspect_Refined_Post
11224 | Aspect_Refined_State
11225 | Aspect_Relaxed_Initialization
11226 | Aspect_SPARK_Mode
11227 | Aspect_Subprogram_Variant
11230 | Aspect_Unimplemented
11231 | Aspect_Unsuppress
11232 | Aspect_Volatile_Function
11234 raise Program_Error;
11238 -- Do the preanalyze call
11240 if Present (Expression (ASN)) then
11241 Preanalyze_Spec_Expression (Expression (ASN), T);
11243 end Check_Aspect_At_Freeze_Point;
11245 -----------------------------------
11246 -- Check_Constant_Address_Clause --
11247 -----------------------------------
11249 procedure Check_Constant_Address_Clause
11253 procedure Check_At_Constant_Address (Nod : Node_Id);
11254 -- Checks that the given node N represents a name whose 'Address is
11255 -- constant (in the same sense as OK_Constant_Address_Clause, i.e. the
11256 -- address value is the same at the point of declaration of U_Ent and at
11257 -- the time of elaboration of the address clause.
11259 procedure Check_Expr_Constants (Nod : Node_Id);
11260 -- Checks that Nod meets the requirements for a constant address clause
11261 -- in the sense of the enclosing procedure.
11263 procedure Check_List_Constants (Lst : List_Id);
11264 -- Check that all elements of list Lst meet the requirements for a
11265 -- constant address clause in the sense of the enclosing procedure.
11267 -------------------------------
11268 -- Check_At_Constant_Address --
11269 -------------------------------
11271 procedure Check_At_Constant_Address (Nod : Node_Id) is
11273 if Is_Entity_Name (Nod) then
11274 if Present (Address_Clause (Entity ((Nod)))) then
11276 ("invalid address clause for initialized object &!",
11279 ("address for& cannot depend on another address clause! "
11280 & "(RM 13.1(22))!", Nod, U_Ent);
11282 elsif In_Same_Source_Unit (Entity (Nod), U_Ent)
11283 and then Sloc (U_Ent) < Sloc (Entity (Nod))
11286 ("invalid address clause for initialized object &!",
11288 Error_Msg_Node_2 := U_Ent;
11290 ("\& must be defined before & (RM 13.1(22))!",
11291 Nod, Entity (Nod));
11294 elsif Nkind (Nod) = N_Selected_Component then
11296 T : constant Entity_Id := Etype (Prefix (Nod));
11299 if (Is_Record_Type (T)
11300 and then Has_Discriminants (T))
11302 (Is_Access_Type (T)
11303 and then Is_Record_Type (Designated_Type (T))
11304 and then Has_Discriminants (Designated_Type (T)))
11307 ("invalid address clause for initialized object &!",
11310 ("\address cannot depend on component of discriminated "
11311 & "record (RM 13.1(22))!", Nod);
11313 Check_At_Constant_Address (Prefix (Nod));
11317 elsif Nkind (Nod) = N_Indexed_Component then
11318 Check_At_Constant_Address (Prefix (Nod));
11319 Check_List_Constants (Expressions (Nod));
11322 Check_Expr_Constants (Nod);
11324 end Check_At_Constant_Address;
11326 --------------------------
11327 -- Check_Expr_Constants --
11328 --------------------------
11330 procedure Check_Expr_Constants (Nod : Node_Id) is
11331 Loc_U_Ent : constant Source_Ptr := Sloc (U_Ent);
11332 Ent : Entity_Id := Empty;
11335 if Nkind (Nod) in N_Has_Etype
11336 and then Etype (Nod) = Any_Type
11341 case Nkind (Nod) is
11347 when N_Expanded_Name
11350 Ent := Entity (Nod);
11352 -- We need to look at the original node if it is different
11353 -- from the node, since we may have rewritten things and
11354 -- substituted an identifier representing the rewrite.
11356 if Is_Rewrite_Substitution (Nod) then
11357 Check_Expr_Constants (Original_Node (Nod));
11359 -- If the node is an object declaration without initial
11360 -- value, some code has been expanded, and the expression
11361 -- is not constant, even if the constituents might be
11362 -- acceptable, as in A'Address + offset.
11364 if Ekind (Ent) = E_Variable
11366 Nkind (Declaration_Node (Ent)) = N_Object_Declaration
11368 No (Expression (Declaration_Node (Ent)))
11371 ("invalid address clause for initialized object &!",
11374 -- If entity is constant, it may be the result of expanding
11375 -- a check. We must verify that its declaration appears
11376 -- before the object in question, else we also reject the
11379 elsif Ekind (Ent) = E_Constant
11380 and then In_Same_Source_Unit (Ent, U_Ent)
11381 and then Sloc (Ent) > Loc_U_Ent
11384 ("invalid address clause for initialized object &!",
11391 -- Otherwise look at the identifier and see if it is OK
11393 if Is_Named_Number (Ent) or else Is_Type (Ent) then
11396 elsif Ekind (Ent) in E_Constant | E_In_Parameter then
11398 -- This is the case where we must have Ent defined before
11399 -- U_Ent. Clearly if they are in different units this
11400 -- requirement is met since the unit containing Ent is
11401 -- already processed.
11403 if not In_Same_Source_Unit (Ent, U_Ent) then
11406 -- Otherwise location of Ent must be before the location
11407 -- of U_Ent, that's what prior defined means.
11409 elsif Sloc (Ent) < Loc_U_Ent then
11414 ("invalid address clause for initialized object &!",
11416 Error_Msg_Node_2 := U_Ent;
11418 ("\& must be defined before & (RM 13.1(22))!",
11422 elsif Nkind (Original_Node (Nod)) = N_Function_Call then
11423 Check_Expr_Constants (Original_Node (Nod));
11427 ("invalid address clause for initialized object &!",
11430 if Comes_From_Source (Ent) then
11432 ("\reference to variable& not allowed"
11433 & " (RM 13.1(22))!", Nod, Ent);
11436 ("non-static expression not allowed"
11437 & " (RM 13.1(22))!", Nod);
11441 when N_Integer_Literal =>
11443 -- If this is a rewritten unchecked conversion, in a system
11444 -- where Address is an integer type, always use the base type
11445 -- for a literal value. This is user-friendly and prevents
11446 -- order-of-elaboration issues with instances of unchecked
11449 if Nkind (Original_Node (Nod)) = N_Function_Call then
11450 Set_Etype (Nod, Base_Type (Etype (Nod)));
11453 when N_Character_Literal
11460 Check_Expr_Constants (Low_Bound (Nod));
11461 Check_Expr_Constants (High_Bound (Nod));
11463 when N_Explicit_Dereference =>
11464 Check_Expr_Constants (Prefix (Nod));
11466 when N_Indexed_Component =>
11467 Check_Expr_Constants (Prefix (Nod));
11468 Check_List_Constants (Expressions (Nod));
11471 Check_Expr_Constants (Prefix (Nod));
11472 Check_Expr_Constants (Discrete_Range (Nod));
11474 when N_Selected_Component =>
11475 Check_Expr_Constants (Prefix (Nod));
11477 when N_Attribute_Reference =>
11478 if Attribute_Name (Nod) in Name_Address
11480 | Name_Unchecked_Access
11481 | Name_Unrestricted_Access
11483 Check_At_Constant_Address (Prefix (Nod));
11485 -- Normally, System'To_Address will have been transformed into
11486 -- an Unchecked_Conversion, but in -gnatc mode, it will not,
11487 -- and we don't want to give an error, because the whole point
11488 -- of 'To_Address is that it is static.
11490 elsif Attribute_Name (Nod) = Name_To_Address then
11491 pragma Assert (Operating_Mode = Check_Semantics);
11495 Check_Expr_Constants (Prefix (Nod));
11496 Check_List_Constants (Expressions (Nod));
11499 when N_Aggregate =>
11500 Check_List_Constants (Component_Associations (Nod));
11501 Check_List_Constants (Expressions (Nod));
11503 when N_Component_Association =>
11504 Check_Expr_Constants (Expression (Nod));
11506 when N_Extension_Aggregate =>
11507 Check_Expr_Constants (Ancestor_Part (Nod));
11508 Check_List_Constants (Component_Associations (Nod));
11509 Check_List_Constants (Expressions (Nod));
11515 | N_Membership_Test
11518 Check_Expr_Constants (Left_Opnd (Nod));
11519 Check_Expr_Constants (Right_Opnd (Nod));
11522 Check_Expr_Constants (Right_Opnd (Nod));
11525 | N_Qualified_Expression
11526 | N_Type_Conversion
11527 | N_Unchecked_Type_Conversion
11529 Check_Expr_Constants (Expression (Nod));
11531 when N_Function_Call =>
11532 if not Is_Pure (Entity (Name (Nod))) then
11534 ("invalid address clause for initialized object &!",
11538 ("\function & is not pure (RM 13.1(22))!",
11539 Nod, Entity (Name (Nod)));
11542 Check_List_Constants (Parameter_Associations (Nod));
11545 when N_Parameter_Association =>
11546 Check_Expr_Constants (Explicit_Actual_Parameter (Nod));
11550 ("invalid address clause for initialized object &!",
11553 ("\must be constant defined before& (RM 13.1(22))!",
11556 end Check_Expr_Constants;
11558 --------------------------
11559 -- Check_List_Constants --
11560 --------------------------
11562 procedure Check_List_Constants (Lst : List_Id) is
11566 Nod1 := First (Lst);
11567 while Present (Nod1) loop
11568 Check_Expr_Constants (Nod1);
11571 end Check_List_Constants;
11573 -- Start of processing for Check_Constant_Address_Clause
11576 -- If rep_clauses are to be ignored, no need for legality checks. In
11577 -- particular, no need to pester user about rep clauses that violate the
11578 -- rule on constant addresses, given that these clauses will be removed
11579 -- by Freeze before they reach the back end. Similarly in CodePeer mode,
11580 -- we want to relax these checks.
11582 if not Ignore_Rep_Clauses and not CodePeer_Mode then
11583 Check_Expr_Constants (Expr);
11585 end Check_Constant_Address_Clause;
11587 ---------------------------
11588 -- Check_Pool_Size_Clash --
11589 ---------------------------
11591 procedure Check_Pool_Size_Clash (Ent : Entity_Id; SP, SS : Node_Id) is
11595 -- We need to find out which one came first. Note that in the case of
11596 -- aspects mixed with pragmas there are cases where the processing order
11597 -- is reversed, which is why we do the check here.
11599 if Sloc (SP) < Sloc (SS) then
11600 Error_Msg_Sloc := Sloc (SP);
11602 Error_Msg_NE ("Storage_Pool previously given for&#", Post, Ent);
11605 Error_Msg_Sloc := Sloc (SS);
11607 Error_Msg_NE ("Storage_Size previously given for&#", Post, Ent);
11611 ("\cannot have Storage_Size and Storage_Pool (RM 13.11(3))", Post);
11612 end Check_Pool_Size_Clash;
11614 ----------------------------------------
11615 -- Check_Record_Representation_Clause --
11616 ----------------------------------------
11618 procedure Check_Record_Representation_Clause (N : Node_Id) is
11619 Loc : constant Source_Ptr := Sloc (N);
11620 Ident : constant Node_Id := Identifier (N);
11621 Rectype : Entity_Id;
11624 Fbit : Uint := No_Uint;
11625 Lbit : Uint := No_Uint;
11626 Hbit : Uint := Uint_0;
11630 Max_Bit_So_Far : Uint;
11631 -- Records the maximum bit position so far. If all field positions
11632 -- are monotonically increasing, then we can skip the circuit for
11633 -- checking for overlap, since no overlap is possible.
11635 Tagged_Parent : Entity_Id := Empty;
11636 -- This is set in the case of an extension for which we have either a
11637 -- size clause or Is_Fully_Repped_Tagged_Type True (indicating that all
11638 -- components are positioned by record representation clauses) on the
11639 -- parent type. In this case we check for overlap between components of
11640 -- this tagged type and the parent component. Tagged_Parent will point
11641 -- to this parent type. For all other cases, Tagged_Parent is Empty.
11643 Parent_Last_Bit : Uint := No_Uint; -- init to avoid warning
11644 -- Relevant only if Tagged_Parent is set, Parent_Last_Bit indicates the
11645 -- last bit position for any field in the parent type. We only need to
11646 -- check overlap for fields starting below this point.
11648 Overlap_Check_Required : Boolean;
11649 -- Used to keep track of whether or not an overlap check is required
11651 Overlap_Detected : Boolean := False;
11652 -- Set True if an overlap is detected
11654 Ccount : Natural := 0;
11655 -- Number of component clauses in record rep clause
11657 procedure Check_Component_Overlap (C1_Ent, C2_Ent : Entity_Id);
11658 -- Given two entities for record components or discriminants, checks
11659 -- if they have overlapping component clauses and issues errors if so.
11661 procedure Find_Component;
11662 -- Finds component entity corresponding to current component clause (in
11663 -- CC), and sets Comp to the entity, and Fbit/Lbit to the zero origin
11664 -- start/stop bits for the field. If there is no matching component or
11665 -- if the matching component does not have a component clause, then
11666 -- that's an error and Comp is set to Empty, but no error message is
11667 -- issued, since the message was already given. Comp is also set to
11668 -- Empty if the current "component clause" is in fact a pragma.
11670 procedure Record_Hole_Check
11671 (Rectype : Entity_Id; After_Last : out Uint; Warn : Boolean);
11672 -- Checks for gaps in the given Rectype. Compute After_Last, the bit
11673 -- number after the last component. Warn is True on the initial call,
11674 -- and warnings are given for gaps. For a type extension, this is called
11675 -- recursively to compute After_Last for the parent type; in this case
11676 -- Warn is False and the warnings are suppressed.
11678 procedure Component_Order_Check (Rectype : Entity_Id);
11679 -- Check that the order of component clauses agrees with the order of
11680 -- component declarations, and that the component clauses are given in
11681 -- increasing order of bit offset.
11683 -----------------------------
11684 -- Check_Component_Overlap --
11685 -----------------------------
11687 procedure Check_Component_Overlap (C1_Ent, C2_Ent : Entity_Id) is
11688 CC1 : constant Node_Id := Component_Clause (C1_Ent);
11689 CC2 : constant Node_Id := Component_Clause (C2_Ent);
11692 if Present (CC1) and then Present (CC2) then
11694 -- Exclude odd case where we have two tag components in the same
11695 -- record, both at location zero. This seems a bit strange, but
11696 -- it seems to happen in some circumstances, perhaps on an error.
11698 if Chars (C1_Ent) = Name_uTag then
11702 -- Here we check if the two fields overlap
11705 S1 : constant Uint := Component_Bit_Offset (C1_Ent);
11706 S2 : constant Uint := Component_Bit_Offset (C2_Ent);
11707 E1 : constant Uint := S1 + Esize (C1_Ent);
11708 E2 : constant Uint := S2 + Esize (C2_Ent);
11711 if E2 <= S1 or else E1 <= S2 then
11714 Error_Msg_Node_2 := Component_Name (CC2);
11715 Error_Msg_Sloc := Sloc (Error_Msg_Node_2);
11716 Error_Msg_Node_1 := Component_Name (CC1);
11718 ("component& overlaps & #", Component_Name (CC1));
11719 Overlap_Detected := True;
11723 end Check_Component_Overlap;
11725 ---------------------------
11726 -- Component_Order_Check --
11727 ---------------------------
11729 procedure Component_Order_Check (Rectype : Entity_Id) is
11730 Comp : Entity_Id := First_Component (Rectype);
11731 Clause : Node_Id := First (Component_Clauses (N));
11732 Prev_Bit_Offset : Uint := Uint_0;
11733 OOO : constant String :=
11734 "?_r?component clause out of order with respect to declaration";
11737 -- Step Comp through components and Clause through component clauses,
11738 -- skipping pragmas. We ignore discriminants and variant parts,
11739 -- because we get most of the benefit from the plain vanilla
11740 -- component cases, without the extra complexity. If we find a Comp
11741 -- and Clause that don't match, give a warning on both and quit. If
11742 -- we find two subsequent clauses out of order by bit layout, give
11743 -- warning and quit. On each iteration, Prev_Bit_Offset is the one
11744 -- from the previous iteration (or 0 to start).
11746 while Present (Comp) and then Present (Clause) loop
11747 if Nkind (Clause) = N_Component_Clause
11748 and then Ekind (Entity (Component_Name (Clause))) = E_Component
11750 if Entity (Component_Name (Clause)) /= Comp then
11751 Error_Msg_N (OOO, Comp);
11752 Error_Msg_N (OOO, Clause);
11756 if not Reverse_Bit_Order (Rectype)
11757 and then not Reverse_Storage_Order (Rectype)
11758 and then Component_Bit_Offset (Comp) < Prev_Bit_Offset
11760 Error_Msg_N ("?_r?memory layout out of order", Clause);
11764 Prev_Bit_Offset := Component_Bit_Offset (Comp);
11765 Next_Component (Comp);
11770 end Component_Order_Check;
11772 --------------------
11773 -- Find_Component --
11774 --------------------
11776 procedure Find_Component is
11778 procedure Search_Component (R : Entity_Id);
11779 -- Search components of R for a match. If found, Comp is set
11781 ----------------------
11782 -- Search_Component --
11783 ----------------------
11785 procedure Search_Component (R : Entity_Id) is
11787 Comp := First_Component_Or_Discriminant (R);
11788 while Present (Comp) loop
11790 -- Ignore error of attribute name for component name (we
11791 -- already gave an error message for this, so no need to
11794 if Nkind (Component_Name (CC)) = N_Attribute_Reference then
11797 exit when Chars (Comp) = Chars (Component_Name (CC));
11800 Next_Component_Or_Discriminant (Comp);
11802 end Search_Component;
11804 -- Start of processing for Find_Component
11807 -- Return with Comp set to Empty if we have a pragma
11809 if Nkind (CC) = N_Pragma then
11814 -- Search current record for matching component
11816 Search_Component (Rectype);
11818 -- If not found, maybe component of base type discriminant that is
11819 -- absent from statically constrained first subtype.
11822 Search_Component (Base_Type (Rectype));
11825 -- If no component, or the component does not reference the component
11826 -- clause in question, then there was some previous error for which
11827 -- we already gave a message, so just return with Comp Empty.
11829 if No (Comp) or else Component_Clause (Comp) /= CC then
11830 Check_Error_Detected;
11833 -- Normal case where we have a component clause
11836 Fbit := Component_Bit_Offset (Comp);
11837 Lbit := Fbit + Esize (Comp) - 1;
11839 end Find_Component;
11841 -----------------------
11842 -- Record_Hole_Check --
11843 -----------------------
11845 procedure Record_Hole_Check
11846 (Rectype : Entity_Id; After_Last : out Uint; Warn : Boolean)
11848 Decl : constant Node_Id := Declaration_Node (Base_Type (Rectype));
11849 -- Full declaration of record type
11851 procedure Check_Component_List
11856 -- Check component list CL for holes. DS is a list of discriminant
11857 -- specifications to be included in the consideration of components.
11858 -- Sbit is the starting bit, which is zero if there are no preceding
11859 -- components (before a variant part, or a parent type, or a tag
11860 -- field). If there are preceding components, Sbit is the bit just
11861 -- after the last such component. Abit is set to the bit just after
11862 -- the last component of DS and CL.
11864 --------------------------
11865 -- Check_Component_List --
11866 --------------------------
11868 procedure Check_Component_List
11877 Compl := Integer (List_Length (Component_Items (CL)));
11879 if DS /= No_List then
11880 Compl := Compl + Integer (List_Length (DS));
11884 Comps : array (Natural range 0 .. Compl) of Entity_Id;
11885 -- Gather components (zero entry is for sort routine)
11887 Ncomps : Natural := 0;
11888 -- Number of entries stored in Comps (starting at Comps (1))
11891 -- One component item or discriminant specification
11894 -- Starting bit for next component
11897 -- Component entity
11902 function Lt (Op1, Op2 : Natural) return Boolean;
11903 -- Compare routine for Sort
11905 procedure Move (From : Natural; To : Natural);
11906 -- Move routine for Sort
11908 package Sorting is new GNAT.Heap_Sort_G (Move, Lt);
11914 function Lt (Op1, Op2 : Natural) return Boolean is
11915 K1 : constant Boolean :=
11916 Known_Component_Bit_Offset (Comps (Op1));
11917 K2 : constant Boolean :=
11918 Known_Component_Bit_Offset (Comps (Op2));
11919 -- Record representation clauses can be incomplete, so the
11920 -- Component_Bit_Offsets can be unknown.
11924 return Component_Bit_Offset (Comps (Op1))
11925 < Component_Bit_Offset (Comps (Op2));
11938 procedure Move (From : Natural; To : Natural) is
11940 Comps (To) := Comps (From);
11944 -- Gather discriminants into Comp
11946 Citem := First (DS);
11947 while Present (Citem) loop
11948 if Nkind (Citem) = N_Discriminant_Specification then
11950 Ent : constant Entity_Id :=
11951 Defining_Identifier (Citem);
11953 if Ekind (Ent) = E_Discriminant then
11954 Ncomps := Ncomps + 1;
11955 Comps (Ncomps) := Ent;
11963 -- Gather component entities into Comp
11965 Citem := First (Component_Items (CL));
11966 while Present (Citem) loop
11967 if Nkind (Citem) = N_Component_Declaration then
11968 Ncomps := Ncomps + 1;
11969 Comps (Ncomps) := Defining_Identifier (Citem);
11975 -- Now sort the component entities based on the first bit.
11976 -- Note we already know there are no overlapping components.
11978 Sorting.Sort (Ncomps);
11980 -- Loop through entries checking for holes
11983 for J in 1 .. Ncomps loop
11985 pragma Annotate (CodePeer, Modified, CEnt);
11988 CBO : constant Uint := Component_Bit_Offset (CEnt);
11991 -- Skip components with unknown offsets
11993 if Present (CBO) and then CBO >= 0 then
11994 Error_Msg_Uint_1 := CBO - Nbit;
11996 if Warn and then Error_Msg_Uint_1 > 0 then
11998 ("?.h?^-bit gap before component&",
11999 Component_Name (Component_Clause (CEnt)),
12003 Nbit := CBO + Esize (CEnt);
12008 -- Set Abit to just after the last nonvariant component
12012 -- Process variant parts recursively if present. Set Abit to
12013 -- the maximum for all variant parts.
12015 if Present (Variant_Part (CL)) then
12017 Var_Start : constant Uint := Nbit;
12019 Variant := First (Variants (Variant_Part (CL)));
12020 while Present (Variant) loop
12021 Check_Component_List
12022 (No_List, Component_List (Variant), Var_Start, Nbit);
12024 if Nbit > Abit then
12031 end Check_Component_List;
12036 -- Starting bit for call to Check_Component_List. Zero for an
12037 -- untagged type. The size of the Tag for a nonderived tagged
12038 -- type. Parent size for a type extension.
12040 Record_Definition : Node_Id;
12041 -- Record_Definition containing Component_List to pass to
12042 -- Check_Component_List.
12044 -- Start of processing for Record_Hole_Check
12047 if Is_Tagged_Type (Rectype) then
12048 Sbit := UI_From_Int (System_Address_Size);
12053 After_Last := Uint_0;
12055 if Nkind (Decl) = N_Full_Type_Declaration then
12056 Record_Definition := Type_Definition (Decl);
12058 -- If we have a record extension, set Sbit to point after the last
12059 -- component of the parent type, by calling Record_Hole_Check
12062 if Nkind (Record_Definition) = N_Derived_Type_Definition then
12063 Record_Definition := Record_Extension_Part (Record_Definition);
12064 Record_Hole_Check (Underlying_Type (Parent_Subtype (Rectype)),
12065 After_Last => Sbit, Warn => False);
12068 if Nkind (Record_Definition) = N_Record_Definition then
12069 Check_Component_List
12070 (Discriminant_Specifications (Decl),
12071 Component_List (Record_Definition),
12075 end Record_Hole_Check;
12077 -- Start of processing for Check_Record_Representation_Clause
12081 Rectype := Entity (Ident);
12083 if Rectype = Any_Type then
12087 Rectype := Underlying_Type (Rectype);
12089 -- See if we have a fully repped derived tagged type
12092 PS : constant Entity_Id := Parent_Subtype (Rectype);
12095 if Present (PS) and then Known_Static_RM_Size (PS) then
12096 Tagged_Parent := PS;
12097 Parent_Last_Bit := RM_Size (PS) - 1;
12099 elsif Present (PS) and then Is_Fully_Repped_Tagged_Type (PS) then
12100 Tagged_Parent := PS;
12102 -- Find maximum bit of any component of the parent type
12104 Parent_Last_Bit := UI_From_Int (System_Address_Size - 1);
12105 Pcomp := First_Component_Or_Discriminant (Tagged_Parent);
12106 while Present (Pcomp) loop
12107 if Present (Component_Bit_Offset (Pcomp))
12108 and then Known_Static_Esize (Pcomp)
12113 Component_Bit_Offset (Pcomp) + Esize (Pcomp) - 1);
12116 Next_Component_Or_Discriminant (Pcomp);
12121 -- All done if no component clauses
12123 CC := First (Component_Clauses (N));
12129 -- If a tag is present, then create a component clause that places it
12130 -- at the start of the record (otherwise gigi may place it after other
12131 -- fields that have rep clauses).
12133 Fent := First_Entity (Rectype);
12135 if Nkind (Fent) = N_Defining_Identifier
12136 and then Chars (Fent) = Name_uTag
12138 Set_Component_Bit_Offset (Fent, Uint_0);
12139 Set_Normalized_Position (Fent, Uint_0);
12140 Set_Normalized_First_Bit (Fent, Uint_0);
12141 Set_Esize (Fent, UI_From_Int (System_Address_Size));
12143 Set_Component_Clause (Fent,
12144 Make_Component_Clause (Loc,
12145 Component_Name => Make_Identifier (Loc, Name_uTag),
12147 Position => Make_Integer_Literal (Loc, Uint_0),
12148 First_Bit => Make_Integer_Literal (Loc, Uint_0),
12150 Make_Integer_Literal (Loc,
12151 UI_From_Int (System_Address_Size - 1))));
12153 Ccount := Ccount + 1;
12156 Max_Bit_So_Far := Uint_Minus_1;
12157 Overlap_Check_Required := False;
12159 -- Process the component clauses
12161 while Present (CC) loop
12164 if Present (Comp) then
12165 Ccount := Ccount + 1;
12167 -- We need a full overlap check if record positions non-monotonic
12169 if Fbit <= Max_Bit_So_Far then
12170 Overlap_Check_Required := True;
12173 Max_Bit_So_Far := Lbit;
12175 -- Check bit position out of range of specified size
12177 if Has_Size_Clause (Rectype)
12178 and then RM_Size (Rectype) <= Lbit
12180 Error_Msg_Uint_1 := RM_Size (Rectype);
12181 Error_Msg_Uint_2 := Lbit + 1;
12182 Error_Msg_N ("bit number out of range of specified "
12183 & "size (expected ^, got ^)",
12186 -- Check for overlap with tag or parent component
12189 if Is_Tagged_Type (Rectype)
12190 and then Fbit < System_Address_Size
12193 ("component overlaps tag field of&",
12194 Component_Name (CC), Rectype);
12195 Overlap_Detected := True;
12197 elsif Present (Tagged_Parent)
12198 and then Fbit <= Parent_Last_Bit
12201 ("component overlaps parent field of&",
12202 Component_Name (CC), Rectype);
12203 Overlap_Detected := True;
12206 if Hbit < Lbit then
12215 -- Now that we have processed all the component clauses, check for
12216 -- overlap. We have to leave this till last, since the components can
12217 -- appear in any arbitrary order in the representation clause.
12219 -- We do not need this check if all specified ranges were monotonic,
12220 -- as recorded by Overlap_Check_Required being False at this stage.
12222 -- This first section checks if there are any overlapping entries at
12223 -- all. It does this by sorting all entries and then seeing if there are
12224 -- any overlaps. If there are none, then that is decisive, but if there
12225 -- are overlaps, they may still be OK (they may result from fields in
12226 -- different variants).
12228 if Overlap_Check_Required then
12229 Overlap_Check1 : declare
12231 OC_Fbit : array (0 .. Ccount) of Uint;
12232 -- First-bit values for component clauses, the value is the offset
12233 -- of the first bit of the field from start of record. The zero
12234 -- entry is for use in sorting.
12236 OC_Lbit : array (0 .. Ccount) of Uint;
12237 -- Last-bit values for component clauses, the value is the offset
12238 -- of the last bit of the field from start of record. The zero
12239 -- entry is for use in sorting.
12241 OC_Count : Natural := 0;
12242 -- Count of entries in OC_Fbit and OC_Lbit
12244 function OC_Lt (Op1, Op2 : Natural) return Boolean;
12245 -- Compare routine for Sort
12247 procedure OC_Move (From : Natural; To : Natural);
12248 -- Move routine for Sort
12250 package Sorting is new GNAT.Heap_Sort_G (OC_Move, OC_Lt);
12256 function OC_Lt (Op1, Op2 : Natural) return Boolean is
12258 return OC_Fbit (Op1) < OC_Fbit (Op2);
12265 procedure OC_Move (From : Natural; To : Natural) is
12267 OC_Fbit (To) := OC_Fbit (From);
12268 OC_Lbit (To) := OC_Lbit (From);
12271 -- Start of processing for Overlap_Check
12274 CC := First (Component_Clauses (N));
12275 while Present (CC) loop
12277 -- Exclude component clause already marked in error
12279 if not Error_Posted (CC) then
12282 if Present (Comp) then
12283 OC_Count := OC_Count + 1;
12284 OC_Fbit (OC_Count) := Fbit;
12285 OC_Lbit (OC_Count) := Lbit;
12292 Sorting.Sort (OC_Count);
12294 Overlap_Check_Required := False;
12295 for J in 1 .. OC_Count - 1 loop
12296 if OC_Lbit (J) >= OC_Fbit (J + 1) then
12297 Overlap_Check_Required := True;
12301 end Overlap_Check1;
12304 -- If Overlap_Check_Required is still True, then we have to do the full
12305 -- scale overlap check, since we have at least two fields that do
12306 -- overlap, and we need to know if that is OK since they are in
12307 -- different variant, or whether we have a definite problem.
12309 if Overlap_Check_Required then
12310 Overlap_Check2 : declare
12311 C1_Ent, C2_Ent : Entity_Id;
12312 -- Entities of components being checked for overlap
12315 -- Component_List node whose Component_Items are being checked
12318 -- Component declaration for component being checked
12321 C1_Ent := First_Entity (Base_Type (Rectype));
12323 -- Loop through all components in record. For each component check
12324 -- for overlap with any of the preceding elements on the component
12325 -- list containing the component and also, if the component is in
12326 -- a variant, check against components outside the case structure.
12327 -- This latter test is repeated recursively up the variant tree.
12329 Main_Component_Loop : while Present (C1_Ent) loop
12330 if Ekind (C1_Ent) not in E_Component | E_Discriminant then
12331 goto Continue_Main_Component_Loop;
12334 -- Skip overlap check if entity has no declaration node. This
12335 -- happens with discriminants in constrained derived types.
12336 -- Possibly we are missing some checks as a result, but that
12337 -- does not seem terribly serious.
12339 if No (Declaration_Node (C1_Ent)) then
12340 goto Continue_Main_Component_Loop;
12343 Clist := Parent (List_Containing (Declaration_Node (C1_Ent)));
12345 -- Loop through component lists that need checking. Check the
12346 -- current component list and all lists in variants above us.
12348 Component_List_Loop : loop
12350 -- If derived type definition, go to full declaration
12351 -- If at outer level, check discriminants if there are any.
12353 if Nkind (Clist) = N_Derived_Type_Definition then
12354 Clist := Parent (Clist);
12357 -- Outer level of record definition, check discriminants
12358 -- but be careful not to flag a non-stored discriminant
12359 -- and the stored discriminant it renames as overlapping.
12361 if Nkind (Clist) in N_Full_Type_Declaration
12362 | N_Private_Type_Declaration
12364 if Has_Discriminants (Defining_Identifier (Clist)) then
12366 First_Discriminant (Defining_Identifier (Clist));
12367 while Present (C2_Ent) loop
12369 Original_Record_Component (C1_Ent) =
12370 Original_Record_Component (C2_Ent);
12371 Check_Component_Overlap (C1_Ent, C2_Ent);
12372 Next_Discriminant (C2_Ent);
12376 -- Record extension case
12378 elsif Nkind (Clist) = N_Derived_Type_Definition then
12381 -- Otherwise check one component list
12384 Citem := First (Component_Items (Clist));
12385 while Present (Citem) loop
12386 if Nkind (Citem) = N_Component_Declaration then
12387 C2_Ent := Defining_Identifier (Citem);
12388 exit when C1_Ent = C2_Ent;
12389 Check_Component_Overlap (C1_Ent, C2_Ent);
12396 -- Check for variants above us (the parent of the Clist can
12397 -- be a variant, in which case its parent is a variant part,
12398 -- and the parent of the variant part is a component list
12399 -- whose components must all be checked against the current
12400 -- component for overlap).
12402 if Nkind (Parent (Clist)) = N_Variant then
12403 Clist := Parent (Parent (Parent (Clist)));
12405 -- Check for possible discriminant part in record, this
12406 -- is treated essentially as another level in the
12407 -- recursion. For this case the parent of the component
12408 -- list is the record definition, and its parent is the
12409 -- full type declaration containing the discriminant
12412 elsif Nkind (Parent (Clist)) = N_Record_Definition then
12413 Clist := Parent (Parent ((Clist)));
12415 -- If neither of these two cases, we are at the top of
12419 exit Component_List_Loop;
12421 end loop Component_List_Loop;
12423 <<Continue_Main_Component_Loop>>
12424 Next_Entity (C1_Ent);
12426 end loop Main_Component_Loop;
12427 end Overlap_Check2;
12430 -- Skip the following warnings if overlap was detected; programmer
12431 -- should fix the errors first. Also skip the warnings for types in
12432 -- generics, because their representation information is not fully
12435 if not Overlap_Detected and then not In_Generic_Scope (Rectype) then
12436 -- Check for record holes (gaps)
12438 if Warn_On_Record_Holes then
12442 Record_Hole_Check (Rectype, After_Last => Ignore, Warn => True);
12446 -- Check for out-of-order component clauses
12448 if Warn_On_Component_Order then
12449 Component_Order_Check (Rectype);
12453 -- For records that have component clauses for all components, and whose
12454 -- size is less than or equal to 32, and which can be fully packed, we
12455 -- need to know the size in the front end to activate possible packed
12456 -- array processing where the component type is a record.
12458 -- At this stage Hbit + 1 represents the first unused bit from all the
12459 -- component clauses processed, so if the component clauses are
12460 -- complete, then this is the length of the record.
12462 -- For records longer than System.Storage_Unit, and for those where not
12463 -- all components have component clauses, the back end determines the
12464 -- length (it may for example be appropriate to round up the size
12465 -- to some convenient boundary, based on alignment considerations, etc).
12467 if not Known_RM_Size (Rectype)
12468 and then Hbit + 1 <= 32
12469 and then not Strict_Alignment (Rectype)
12472 -- Nothing to do if at least one component has no component clause
12474 Comp := First_Component_Or_Discriminant (Rectype);
12475 while Present (Comp) loop
12476 exit when No (Component_Clause (Comp));
12477 Next_Component_Or_Discriminant (Comp);
12480 -- If we fall out of loop, all components have component clauses
12481 -- and so we can set the size to the maximum value.
12484 Set_RM_Size (Rectype, Hbit + 1);
12487 end Check_Record_Representation_Clause;
12493 procedure Check_Size
12497 Biased : out Boolean)
12499 procedure Size_Too_Small_Error (Min_Siz : Uint);
12500 -- Emit an error concerning illegal size Siz. Min_Siz denotes the
12503 --------------------------
12504 -- Size_Too_Small_Error --
12505 --------------------------
12507 procedure Size_Too_Small_Error (Min_Siz : Uint) is
12509 Error_Msg_Uint_1 := Min_Siz;
12510 Error_Msg_NE (Size_Too_Small_Message, N, T);
12511 end Size_Too_Small_Error;
12515 UT : constant Entity_Id := Underlying_Type (T);
12518 -- Start of processing for Check_Size
12523 -- Reject patently improper size values
12525 if Is_Elementary_Type (T)
12526 and then Siz > Int'Last
12528 Error_Msg_N ("Size value too large for elementary type", N);
12530 if Nkind (Original_Node (N)) = N_Op_Expon then
12532 ("\maybe '* was meant, rather than '*'*", Original_Node (N));
12536 -- Dismiss generic types
12538 if Is_Generic_Type (T)
12540 Is_Generic_Type (UT)
12542 Is_Generic_Type (Root_Type (UT))
12546 -- Guard against previous errors
12548 elsif No (UT) or else UT = Any_Type then
12549 Check_Error_Detected;
12552 -- Check case of bit packed array
12554 elsif Is_Array_Type (UT)
12555 and then Known_Static_Component_Size (UT)
12556 and then Is_Bit_Packed_Array (UT)
12564 Asiz := Component_Size (UT);
12565 Indx := First_Index (UT);
12567 Ityp := Etype (Indx);
12569 -- If non-static bound, then we are not in the business of
12570 -- trying to check the length, and indeed an error will be
12571 -- issued elsewhere, since sizes of non-static array types
12572 -- cannot be set implicitly or explicitly.
12574 if not Is_OK_Static_Subtype (Ityp) then
12578 -- Otherwise accumulate next dimension
12580 Asiz := Asiz * (Expr_Value (Type_High_Bound (Ityp)) -
12581 Expr_Value (Type_Low_Bound (Ityp)) +
12585 exit when No (Indx);
12588 if Asiz <= Siz then
12592 Size_Too_Small_Error (Asiz);
12596 -- All other composite types are ignored
12598 elsif Is_Composite_Type (UT) then
12601 -- For fixed-point types, don't check minimum if type is not frozen,
12602 -- since we don't know all the characteristics of the type that can
12603 -- affect the size (e.g. a specified small) till freeze time.
12605 elsif Is_Fixed_Point_Type (UT) and then not Is_Frozen (UT) then
12608 -- Cases for which a minimum check is required
12611 -- Ignore if specified size is correct for the type
12613 if Known_Esize (UT) and then Siz = Esize (UT) then
12617 -- Otherwise get minimum size
12619 M := UI_From_Int (Minimum_Size (UT));
12623 -- Size is less than minimum size, but one possibility remains
12624 -- that we can manage with the new size if we bias the type.
12626 M := UI_From_Int (Minimum_Size (UT, Biased => True));
12629 Size_Too_Small_Error (M);
12637 --------------------------
12638 -- Freeze_Entity_Checks --
12639 --------------------------
12641 procedure Freeze_Entity_Checks (N : Node_Id) is
12642 procedure Hide_Non_Overridden_Subprograms (Typ : Entity_Id);
12643 -- Inspect the primitive operations of type Typ and hide all pairs of
12644 -- implicitly declared non-overridden non-fully conformant homographs
12645 -- (Ada RM 8.3 12.3/2).
12647 -------------------------------------
12648 -- Hide_Non_Overridden_Subprograms --
12649 -------------------------------------
12651 procedure Hide_Non_Overridden_Subprograms (Typ : Entity_Id) is
12652 procedure Hide_Matching_Homographs
12653 (Subp_Id : Entity_Id;
12654 Start_Elmt : Elmt_Id);
12655 -- Inspect a list of primitive operations starting with Start_Elmt
12656 -- and find matching implicitly declared non-overridden non-fully
12657 -- conformant homographs of Subp_Id. If found, all matches along
12658 -- with Subp_Id are hidden from all visibility.
12660 function Is_Non_Overridden_Or_Null_Procedure
12661 (Subp_Id : Entity_Id) return Boolean;
12662 -- Determine whether subprogram Subp_Id is implicitly declared non-
12663 -- overridden subprogram or an implicitly declared null procedure.
12665 ------------------------------
12666 -- Hide_Matching_Homographs --
12667 ------------------------------
12669 procedure Hide_Matching_Homographs
12670 (Subp_Id : Entity_Id;
12671 Start_Elmt : Elmt_Id)
12674 Prim_Elmt : Elmt_Id;
12677 Prim_Elmt := Start_Elmt;
12678 while Present (Prim_Elmt) loop
12679 Prim := Node (Prim_Elmt);
12681 -- The current primitive is implicitly declared non-overridden
12682 -- non-fully conformant homograph of Subp_Id. Both subprograms
12683 -- must be hidden from visibility.
12685 if Chars (Prim) = Chars (Subp_Id)
12686 and then Is_Non_Overridden_Or_Null_Procedure (Prim)
12687 and then not Fully_Conformant (Prim, Subp_Id)
12689 Set_Is_Hidden_Non_Overridden_Subpgm (Prim);
12690 Set_Is_Immediately_Visible (Prim, False);
12691 Set_Is_Potentially_Use_Visible (Prim, False);
12693 Set_Is_Hidden_Non_Overridden_Subpgm (Subp_Id);
12694 Set_Is_Immediately_Visible (Subp_Id, False);
12695 Set_Is_Potentially_Use_Visible (Subp_Id, False);
12698 Next_Elmt (Prim_Elmt);
12700 end Hide_Matching_Homographs;
12702 -----------------------------------------
12703 -- Is_Non_Overridden_Or_Null_Procedure --
12704 -----------------------------------------
12706 function Is_Non_Overridden_Or_Null_Procedure
12707 (Subp_Id : Entity_Id) return Boolean
12709 Alias_Id : Entity_Id;
12712 -- The subprogram is inherited (implicitly declared), it does not
12713 -- override and does not cover a primitive of an interface.
12715 if Ekind (Subp_Id) in E_Function | E_Procedure
12716 and then Present (Alias (Subp_Id))
12717 and then No (Interface_Alias (Subp_Id))
12718 and then No (Overridden_Operation (Subp_Id))
12720 Alias_Id := Alias (Subp_Id);
12722 if Requires_Overriding (Alias_Id) then
12725 elsif Nkind (Parent (Alias_Id)) = N_Procedure_Specification
12726 and then Null_Present (Parent (Alias_Id))
12733 end Is_Non_Overridden_Or_Null_Procedure;
12737 Prim_Ops : constant Elist_Id := Direct_Primitive_Operations (Typ);
12739 Prim_Elmt : Elmt_Id;
12741 -- Start of processing for Hide_Non_Overridden_Subprograms
12744 -- Inspect the list of primitives looking for non-overridden
12747 if Present (Prim_Ops) then
12748 Prim_Elmt := First_Elmt (Prim_Ops);
12749 while Present (Prim_Elmt) loop
12750 Prim := Node (Prim_Elmt);
12751 Next_Elmt (Prim_Elmt);
12753 if Is_Non_Overridden_Or_Null_Procedure (Prim) then
12754 Hide_Matching_Homographs
12756 Start_Elmt => Prim_Elmt);
12760 end Hide_Non_Overridden_Subprograms;
12764 E : constant Entity_Id := Entity (N);
12766 Nongeneric_Case : constant Boolean := Nkind (N) = N_Freeze_Entity;
12767 -- True in nongeneric case. Some of the processing here is skipped
12768 -- for the generic case since it is not needed. Basically in the
12769 -- generic case, we only need to do stuff that might generate error
12770 -- messages or warnings.
12772 -- Start of processing for Freeze_Entity_Checks
12775 -- Remember that we are processing a freezing entity. Required to
12776 -- ensure correct decoration of internal entities associated with
12777 -- interfaces (see New_Overloaded_Entity).
12779 Inside_Freezing_Actions := Inside_Freezing_Actions + 1;
12781 -- For tagged types covering interfaces add internal entities that link
12782 -- the primitives of the interfaces with the primitives that cover them.
12783 -- Note: These entities were originally generated only when generating
12784 -- code because their main purpose was to provide support to initialize
12785 -- the secondary dispatch tables. They are also used to locate
12786 -- primitives covering interfaces when processing generics (see
12787 -- Derive_Subprograms).
12789 -- This is not needed in the generic case
12791 if Ada_Version >= Ada_2005
12792 and then Nongeneric_Case
12793 and then Ekind (E) = E_Record_Type
12794 and then Is_Tagged_Type (E)
12795 and then not Is_Interface (E)
12796 and then Has_Interfaces (E)
12798 -- This would be a good common place to call the routine that checks
12799 -- overriding of interface primitives (and thus factorize calls to
12800 -- Check_Abstract_Overriding located at different contexts in the
12801 -- compiler). However, this is not possible because it causes
12802 -- spurious errors in case of late overriding.
12804 Add_Internal_Interface_Entities (E);
12807 -- After all forms of overriding have been resolved, a tagged type may
12808 -- be left with a set of implicitly declared and possibly erroneous
12809 -- abstract subprograms, null procedures and subprograms that require
12810 -- overriding. If this set contains fully conformant homographs, then
12811 -- one is chosen arbitrarily (already done during resolution), otherwise
12812 -- all remaining non-fully conformant homographs are hidden from
12813 -- visibility (Ada RM 8.3 12.3/2).
12815 if Is_Tagged_Type (E) then
12816 Hide_Non_Overridden_Subprograms (E);
12821 if Ekind (E) = E_Record_Type
12822 and then Is_CPP_Class (E)
12823 and then Is_Tagged_Type (E)
12824 and then Tagged_Type_Expansion
12826 if CPP_Num_Prims (E) = 0 then
12828 -- If the CPP type has user defined components then it must import
12829 -- primitives from C++. This is required because if the C++ class
12830 -- has no primitives then the C++ compiler does not added the _tag
12831 -- component to the type.
12833 if First_Entity (E) /= Last_Entity (E) then
12835 ("'C'P'P type must import at least one primitive from C++??",
12840 -- Check that all its primitives are abstract or imported from C++.
12841 -- Check also availability of the C++ constructor.
12844 Has_Constructors : constant Boolean := Has_CPP_Constructors (E);
12846 Error_Reported : Boolean := False;
12850 Elmt := First_Elmt (Primitive_Operations (E));
12851 while Present (Elmt) loop
12852 Prim := Node (Elmt);
12854 if Comes_From_Source (Prim) then
12855 if Is_Abstract_Subprogram (Prim) then
12858 elsif not Is_Imported (Prim)
12859 or else Convention (Prim) /= Convention_CPP
12862 ("primitives of 'C'P'P types must be imported from C++ "
12863 & "or abstract??", Prim);
12865 elsif not Has_Constructors
12866 and then not Error_Reported
12868 Error_Msg_Name_1 := Chars (E);
12870 ("??'C'P'P constructor required for type %", Prim);
12871 Error_Reported := True;
12880 -- Check Ada derivation of CPP type
12882 if Expander_Active -- why? losing errors in -gnatc mode???
12883 and then Present (Etype (E)) -- defend against errors
12884 and then Tagged_Type_Expansion
12885 and then Ekind (E) = E_Record_Type
12886 and then Etype (E) /= E
12887 and then Is_CPP_Class (Etype (E))
12888 and then CPP_Num_Prims (Etype (E)) > 0
12889 and then not Is_CPP_Class (E)
12890 and then not Has_CPP_Constructors (Etype (E))
12892 -- If the parent has C++ primitives but it has no constructor then
12893 -- check that all the primitives are overridden in this derivation;
12894 -- otherwise the constructor of the parent is needed to build the
12902 Elmt := First_Elmt (Primitive_Operations (E));
12903 while Present (Elmt) loop
12904 Prim := Node (Elmt);
12906 if not Is_Abstract_Subprogram (Prim)
12907 and then No (Interface_Alias (Prim))
12908 and then Find_Dispatching_Type (Ultimate_Alias (Prim)) /= E
12910 Error_Msg_Name_1 := Chars (Etype (E));
12912 ("'C'P'P constructor required for parent type %", E);
12921 Inside_Freezing_Actions := Inside_Freezing_Actions - 1;
12923 -- For a record type, deal with variant parts. This has to be delayed to
12924 -- this point, because of the issue of statically predicated subtypes,
12925 -- which we have to ensure are frozen before checking choices, since we
12926 -- need to have the static choice list set.
12928 if Is_Record_Type (E) then
12929 Check_Variant_Part : declare
12930 D : constant Node_Id := Declaration_Node (E);
12935 Others_Present : Boolean;
12936 pragma Warnings (Off, Others_Present);
12937 -- Indicates others present, not used in this case
12939 procedure Non_Static_Choice_Error (Choice : Node_Id);
12940 -- Error routine invoked by the generic instantiation below when
12941 -- the variant part has a non static choice.
12943 procedure Process_Declarations (Variant : Node_Id);
12944 -- Processes declarations associated with a variant. We analyzed
12945 -- the declarations earlier (in Sem_Ch3.Analyze_Variant_Part),
12946 -- but we still need the recursive call to Check_Choices for any
12947 -- nested variant to get its choices properly processed. This is
12948 -- also where we expand out the choices if expansion is active.
12950 package Variant_Choices_Processing is new
12951 Generic_Check_Choices
12952 (Process_Empty_Choice => No_OP,
12953 Process_Non_Static_Choice => Non_Static_Choice_Error,
12954 Process_Associated_Node => Process_Declarations);
12955 use Variant_Choices_Processing;
12957 -----------------------------
12958 -- Non_Static_Choice_Error --
12959 -----------------------------
12961 procedure Non_Static_Choice_Error (Choice : Node_Id) is
12963 Flag_Non_Static_Expr
12964 ("choice given in variant part is not static!", Choice);
12965 end Non_Static_Choice_Error;
12967 --------------------------
12968 -- Process_Declarations --
12969 --------------------------
12971 procedure Process_Declarations (Variant : Node_Id) is
12972 CL : constant Node_Id := Component_List (Variant);
12976 -- Check for static predicate present in this variant
12978 if Has_SP_Choice (Variant) then
12980 -- Here we expand. You might expect to find this call in
12981 -- Expand_N_Variant_Part, but that is called when we first
12982 -- see the variant part, and we cannot do this expansion
12983 -- earlier than the freeze point, since for statically
12984 -- predicated subtypes, the predicate is not known till
12985 -- the freeze point.
12987 -- Furthermore, we do this expansion even if the expander
12988 -- is not active, because other semantic processing, e.g.
12989 -- for aggregates, requires the expanded list of choices.
12991 -- If the expander is not active, then we can't just clobber
12992 -- the list since it would invalidate the tree.
12993 -- So we have to rewrite the variant part with a Rewrite
12994 -- call that replaces it with a copy and clobber the copy.
12996 if not Expander_Active then
12998 NewV : constant Node_Id := New_Copy (Variant);
13000 Set_Discrete_Choices
13001 (NewV, New_Copy_List (Discrete_Choices (Variant)));
13002 Rewrite (Variant, NewV);
13006 Expand_Static_Predicates_In_Choices (Variant);
13009 -- We don't need to worry about the declarations in the variant
13010 -- (since they were analyzed by Analyze_Choices when we first
13011 -- encountered the variant), but we do need to take care of
13012 -- expansion of any nested variants.
13014 if not Null_Present (CL) then
13015 VP := Variant_Part (CL);
13017 if Present (VP) then
13019 (VP, Variants (VP), Etype (Name (VP)), Others_Present);
13022 end Process_Declarations;
13024 -- Start of processing for Check_Variant_Part
13027 -- Find component list
13031 if Nkind (D) = N_Full_Type_Declaration then
13032 T := Type_Definition (D);
13034 if Nkind (T) = N_Record_Definition then
13035 C := Component_List (T);
13037 elsif Nkind (T) = N_Derived_Type_Definition
13038 and then Present (Record_Extension_Part (T))
13040 C := Component_List (Record_Extension_Part (T));
13044 -- Case of variant part present
13046 if Present (C) and then Present (Variant_Part (C)) then
13047 VP := Variant_Part (C);
13052 (VP, Variants (VP), Etype (Name (VP)), Others_Present);
13054 -- If the last variant does not contain the Others choice,
13055 -- replace it with an N_Others_Choice node since Gigi always
13056 -- wants an Others. Note that we do not bother to call Analyze
13057 -- on the modified variant part, since its only effect would be
13058 -- to compute the Others_Discrete_Choices node laboriously, and
13059 -- of course we already know the list of choices corresponding
13060 -- to the others choice (it's the list we're replacing).
13062 -- We only want to do this if the expander is active, since
13063 -- we do not want to clobber the tree.
13065 if Expander_Active then
13067 Last_Var : constant Node_Id :=
13068 Last_Non_Pragma (Variants (VP));
13070 Others_Node : Node_Id;
13073 if Nkind (First (Discrete_Choices (Last_Var))) /=
13076 Others_Node := Make_Others_Choice (Sloc (Last_Var));
13077 Set_Others_Discrete_Choices
13078 (Others_Node, Discrete_Choices (Last_Var));
13079 Set_Discrete_Choices
13080 (Last_Var, New_List (Others_Node));
13085 end Check_Variant_Part;
13088 -- If we have a type with predicates, build predicate function. This is
13089 -- not needed in the generic case, nor within e.g. TSS subprograms and
13090 -- other predefined primitives. For a derived type, ensure that the
13091 -- parent type is already frozen so that its predicate function has been
13092 -- constructed already. This is necessary if the parent is declared
13093 -- in a nested package and its own freeze point has not been reached.
13096 and then Nongeneric_Case
13097 and then Has_Predicates (E)
13098 and then Predicate_Check_In_Scope (N)
13101 Atyp : constant Entity_Id := Nearest_Ancestor (E);
13105 and then Has_Predicates (Atyp)
13106 and then not Is_Frozen (Atyp)
13108 Freeze_Before (N, Atyp);
13112 -- Before we build a predicate function, ensure that discriminant
13113 -- checking functions are available. The predicate function might
13114 -- need to call these functions if the predicate references any
13115 -- components declared in a variant part.
13117 if Ekind (E) = E_Record_Type and then Has_Discriminants (E) then
13118 Build_Or_Copy_Discr_Checking_Funcs (Parent (E));
13121 Build_Predicate_Function (E, N);
13124 -- If type has delayed aspects, this is where we do the preanalysis at
13125 -- the freeze point, as part of the consistent visibility check. Note
13126 -- that this must be done after calling Build_Predicate_Function or
13127 -- Build_Invariant_Procedure since these subprograms fix occurrences of
13128 -- the subtype name in the saved expression so that they will not cause
13129 -- trouble in the preanalysis.
13131 -- This is also not needed in the generic case
13134 and then Has_Delayed_Aspects (E)
13135 and then Scope (E) = Current_Scope
13141 -- Look for aspect specification entries for this entity
13143 Ritem := First_Rep_Item (E);
13144 while Present (Ritem) loop
13145 if Nkind (Ritem) = N_Aspect_Specification
13146 and then Entity (Ritem) = E
13147 and then Is_Delayed_Aspect (Ritem)
13149 if Get_Aspect_Id (Ritem) in Aspect_CPU
13150 | Aspect_Dynamic_Predicate
13152 | Aspect_Static_Predicate
13155 -- Retrieve the visibility to components and discriminants
13156 -- in order to properly analyze the aspects.
13159 Check_Aspect_At_Freeze_Point (Ritem);
13161 -- In the case of predicate aspects, there will be
13162 -- a corresponding Predicate pragma associated with
13163 -- the aspect, and the expression of the pragma also
13164 -- needs to be analyzed at this point, to ensure that
13165 -- Save_Global_References will capture global refs in
13166 -- expressions that occur in generic bodies, for proper
13167 -- later resolution of the pragma in instantiations.
13170 and then Inside_A_Generic
13171 and then Has_Predicates (E)
13172 and then Present (Aspect_Rep_Item (Ritem))
13175 Pragma_Args : constant List_Id :=
13176 Pragma_Argument_Associations
13177 (Aspect_Rep_Item (Ritem));
13178 Pragma_Expr : constant Node_Id :=
13179 Expression (Next (First (Pragma_Args)));
13181 if Present (Pragma_Expr) then
13182 Analyze_And_Resolve
13183 (Pragma_Expr, Standard_Boolean);
13191 Check_Aspect_At_Freeze_Point (Ritem);
13194 -- A pragma Predicate should be checked like one of the
13195 -- corresponding aspects, wrt possible misuse of ghost
13198 elsif Nkind (Ritem) = N_Pragma
13199 and then No (Corresponding_Aspect (Ritem))
13201 Get_Pragma_Id (Pragma_Name (Ritem)) = Pragma_Predicate
13203 -- Retrieve the visibility to components and discriminants
13204 -- in order to properly analyze the pragma.
13207 Arg : constant Node_Id :=
13208 Next (First (Pragma_Argument_Associations (Ritem)));
13211 Preanalyze_Spec_Expression
13212 (Expression (Arg), Standard_Boolean);
13217 Next_Rep_Item (Ritem);
13222 if not In_Generic_Scope (E)
13223 and then Ekind (E) = E_Record_Type
13224 and then Is_Tagged_Type (E)
13226 Process_Class_Conditions_At_Freeze_Point (E);
13228 end Freeze_Entity_Checks;
13230 -------------------------
13231 -- Get_Alignment_Value --
13232 -------------------------
13234 function Get_Alignment_Value (Expr : Node_Id) return Uint is
13235 Align : constant Uint := Static_Integer (Expr);
13241 elsif Align < 0 then
13242 Error_Msg_N ("alignment value must be positive", Expr);
13245 -- If Alignment is specified to be 0, we treat it the same as 1
13247 elsif Align = 0 then
13251 for J in Int range 0 .. 64 loop
13253 M : constant Uint := Uint_2 ** J;
13256 exit when M = Align;
13259 Error_Msg_N ("alignment value must be power of 2", Expr);
13267 end Get_Alignment_Value;
13269 -----------------------------------
13270 -- Has_Compatible_Representation --
13271 -----------------------------------
13273 function Has_Compatible_Representation
13274 (Target_Typ, Operand_Typ : Entity_Id) return Boolean
13276 -- The subtype-specific representation attributes (Size and Alignment)
13277 -- do not affect representation from the point of view of this function.
13279 T1 : constant Entity_Id := Implementation_Base_Type (Target_Typ);
13280 T2 : constant Entity_Id := Implementation_Base_Type (Operand_Typ);
13283 -- Return true immediately for the same base type
13288 -- Tagged types always have the same representation, because it is not
13289 -- possible to specify different representations for common fields.
13291 elsif Is_Tagged_Type (T1) then
13294 -- Representations are definitely different if conventions differ
13296 elsif Convention (T1) /= Convention (T2) then
13299 -- Representations are different if component alignments or scalar
13300 -- storage orders differ.
13302 elsif (Is_Record_Type (T1) or else Is_Array_Type (T1))
13304 (Is_Record_Type (T2) or else Is_Array_Type (T2))
13305 and then (Component_Alignment (T1) /= Component_Alignment (T2)
13307 Reverse_Storage_Order (T1) /= Reverse_Storage_Order (T2))
13312 -- For arrays, the only real issue is component size. If we know the
13313 -- component size for both arrays, and it is the same, then that's
13314 -- good enough to know we don't have a change of representation.
13316 if Is_Array_Type (T1) then
13318 -- In a view conversion, if the target type is an array type having
13319 -- aliased components and the operand type is an array type having
13320 -- unaliased components, then a new object is created (4.6(58.3/4)).
13322 if Has_Aliased_Components (T1)
13323 and then not Has_Aliased_Components (T2)
13328 if Known_Component_Size (T1)
13329 and then Known_Component_Size (T2)
13330 and then Component_Size (T1) = Component_Size (T2)
13335 -- For records, representations are different if reordering differs
13337 elsif Is_Record_Type (T1)
13338 and then Is_Record_Type (T2)
13339 and then No_Reordering (T1) /= No_Reordering (T2)
13344 -- Types definitely have same representation if neither has non-standard
13345 -- representation since default representations are always consistent.
13346 -- If only one has non-standard representation, and the other does not,
13347 -- then we consider that they do not have the same representation. They
13348 -- might, but there is no way of telling early enough.
13350 if Has_Non_Standard_Rep (T1) then
13351 if not Has_Non_Standard_Rep (T2) then
13355 return not Has_Non_Standard_Rep (T2);
13358 -- Here the two types both have non-standard representation, and we need
13359 -- to determine if they have the same non-standard representation.
13361 -- For arrays, we simply need to test if the component sizes are the
13362 -- same. Pragma Pack is reflected in modified component sizes, so this
13363 -- check also deals with pragma Pack.
13365 if Is_Array_Type (T1) then
13366 return Component_Size (T1) = Component_Size (T2);
13368 -- Case of record types
13370 elsif Is_Record_Type (T1) then
13372 -- Packed status must conform
13374 if Is_Packed (T1) /= Is_Packed (T2) then
13377 -- If the operand type is derived from the target type and no clause
13378 -- has been given after the derivation, then the representations are
13379 -- the same since the derived type inherits that of the parent type.
13381 elsif Is_Derived_Type (T2)
13382 and then Etype (T2) = T1
13383 and then not Has_Record_Rep_Clause (T2)
13387 -- Otherwise we must check components. Typ2 maybe a constrained
13388 -- subtype with fewer components, so we compare the components
13389 -- of the base types.
13392 Record_Case : declare
13393 CD1, CD2 : Entity_Id;
13395 function Same_Rep return Boolean;
13396 -- CD1 and CD2 are either components or discriminants. This
13397 -- function tests whether they have the same representation.
13403 function Same_Rep return Boolean is
13405 if No (Component_Clause (CD1)) then
13406 return No (Component_Clause (CD2));
13408 -- Note: at this point, component clauses have been
13409 -- normalized to the default bit order, so that the
13410 -- comparison of Component_Bit_Offsets is meaningful.
13413 Present (Component_Clause (CD2))
13415 Component_Bit_Offset (CD1) = Component_Bit_Offset (CD2)
13417 Esize (CD1) = Esize (CD2);
13421 -- Start of processing for Record_Case
13424 if Has_Discriminants (T1) then
13426 -- The number of discriminants may be different if the
13427 -- derived type has fewer (constrained by values). The
13428 -- invisible discriminants retain the representation of
13429 -- the original, so the discrepancy does not per se
13430 -- indicate a different representation.
13432 CD1 := First_Discriminant (T1);
13433 CD2 := First_Discriminant (T2);
13434 while Present (CD1) and then Present (CD2) loop
13435 if not Same_Rep then
13438 Next_Discriminant (CD1);
13439 Next_Discriminant (CD2);
13444 CD1 := First_Component (Underlying_Type (Base_Type (T1)));
13445 CD2 := First_Component (Underlying_Type (Base_Type (T2)));
13446 while Present (CD1) loop
13447 if not Same_Rep then
13450 Next_Component (CD1);
13451 Next_Component (CD2);
13459 -- For enumeration types, we must check each literal to see if the
13460 -- representation is the same. Note that we do not permit enumeration
13461 -- representation clauses for Character and Wide_Character, so these
13462 -- cases were already dealt with.
13464 elsif Is_Enumeration_Type (T1) then
13465 Enumeration_Case : declare
13466 L1, L2 : Entity_Id;
13469 L1 := First_Literal (T1);
13470 L2 := First_Literal (T2);
13471 while Present (L1) loop
13472 if Enumeration_Rep (L1) /= Enumeration_Rep (L2) then
13481 end Enumeration_Case;
13483 -- Any other types have the same representation for these purposes
13488 end Has_Compatible_Representation;
13490 -------------------------------------
13491 -- Inherit_Aspects_At_Freeze_Point --
13492 -------------------------------------
13494 procedure Inherit_Aspects_At_Freeze_Point (Typ : Entity_Id) is
13495 function Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
13496 (Rep_Item : Node_Id) return Boolean;
13497 -- This routine checks if Rep_Item is either a pragma or an aspect
13498 -- specification node whose corresponding pragma (if any) is present in
13499 -- the Rep Item chain of the entity it has been specified to.
13501 --------------------------------------------------
13502 -- Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item --
13503 --------------------------------------------------
13505 function Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
13506 (Rep_Item : Node_Id) return Boolean
13510 Nkind (Rep_Item) = N_Pragma
13512 Present_In_Rep_Item (Entity (Rep_Item), Aspect_Rep_Item (Rep_Item));
13513 end Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item;
13515 -- Start of processing for Inherit_Aspects_At_Freeze_Point
13518 -- A representation item is either subtype-specific (Size and Alignment
13519 -- clauses) or type-related (all others). Subtype-specific aspects may
13520 -- differ for different subtypes of the same type (RM 13.1.8).
13522 -- A derived type inherits each type-related representation aspect of
13523 -- its parent type that was directly specified before the declaration of
13524 -- the derived type (RM 13.1.15).
13526 -- A derived subtype inherits each subtype-specific representation
13527 -- aspect of its parent subtype that was directly specified before the
13528 -- declaration of the derived type (RM 13.1.15).
13530 -- The general processing involves inheriting a representation aspect
13531 -- from a parent type whenever the first rep item (aspect specification,
13532 -- attribute definition clause, pragma) corresponding to the given
13533 -- representation aspect in the rep item chain of Typ, if any, isn't
13534 -- directly specified to Typ but to one of its parents.
13536 -- In addition, Convention must be propagated from base type to subtype,
13537 -- because the subtype may have been declared on an incomplete view.
13539 if Nkind (Parent (Typ)) = N_Private_Extension_Declaration then
13545 if not Has_Rep_Item (Typ, Name_Ada_05, Name_Ada_2005, False)
13546 and then Has_Rep_Item (Typ, Name_Ada_05, Name_Ada_2005)
13547 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
13548 (Get_Rep_Item (Typ, Name_Ada_05, Name_Ada_2005))
13550 Set_Is_Ada_2005_Only (Typ);
13555 if not Has_Rep_Item (Typ, Name_Ada_12, Name_Ada_2012, False)
13556 and then Has_Rep_Item (Typ, Name_Ada_12, Name_Ada_2012)
13557 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
13558 (Get_Rep_Item (Typ, Name_Ada_12, Name_Ada_2012))
13560 Set_Is_Ada_2012_Only (Typ);
13565 if not Has_Rep_Item (Typ, Name_Ada_2022, False)
13566 and then Has_Rep_Item (Typ, Name_Ada_2022)
13567 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
13568 (Get_Rep_Item (Typ, Name_Ada_2022))
13570 Set_Is_Ada_2022_Only (Typ);
13575 if not Has_Rep_Item (Typ, Name_Atomic, Name_Shared, False)
13576 and then Has_Rep_Pragma (Typ, Name_Atomic, Name_Shared)
13577 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
13578 (Get_Rep_Item (Typ, Name_Atomic, Name_Shared))
13580 Set_Is_Atomic (Typ);
13581 Set_Is_Volatile (Typ);
13582 Set_Treat_As_Volatile (Typ);
13587 if Is_Record_Type (Typ)
13588 and then Typ /= Base_Type (Typ) and then Is_Frozen (Base_Type (Typ))
13590 Set_Convention (Typ, Convention (Base_Type (Typ)));
13593 -- Default_Component_Value
13595 -- Verify that there is no rep_item declared for the type, and there
13596 -- is one coming from an ancestor.
13598 if Is_Array_Type (Typ)
13599 and then Is_Base_Type (Typ)
13600 and then not Has_Rep_Item (Typ, Name_Default_Component_Value, False)
13601 and then Has_Rep_Item (Typ, Name_Default_Component_Value)
13607 E := Entity (Get_Rep_Item (Typ, Name_Default_Component_Value));
13609 -- Deal with private types
13611 if Is_Private_Type (E) then
13612 E := Full_View (E);
13615 Set_Default_Aspect_Component_Value (Typ,
13616 Default_Aspect_Component_Value (E));
13622 if Is_Scalar_Type (Typ)
13623 and then Is_Base_Type (Typ)
13624 and then not Has_Rep_Item (Typ, Name_Default_Value, False)
13625 and then Has_Rep_Item (Typ, Name_Default_Value)
13627 Set_Has_Default_Aspect (Typ);
13633 E := Entity (Get_Rep_Item (Typ, Name_Default_Value));
13635 -- Deal with private types
13637 if Is_Private_Type (E) then
13638 E := Full_View (E);
13641 Set_Default_Aspect_Value (Typ, Default_Aspect_Value (E));
13647 if not Has_Rep_Item (Typ, Name_Discard_Names, False)
13648 and then Has_Rep_Item (Typ, Name_Discard_Names)
13649 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
13650 (Get_Rep_Item (Typ, Name_Discard_Names))
13652 Set_Discard_Names (Typ);
13657 if not Has_Rep_Item (Typ, Name_Volatile, False)
13658 and then Has_Rep_Item (Typ, Name_Volatile)
13659 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
13660 (Get_Rep_Item (Typ, Name_Volatile))
13662 Set_Is_Volatile (Typ);
13663 Set_Treat_As_Volatile (Typ);
13666 -- Volatile_Full_Access and Full_Access_Only
13668 if not Has_Rep_Item (Typ, Name_Volatile_Full_Access, False)
13669 and then not Has_Rep_Item (Typ, Name_Full_Access_Only, False)
13670 and then (Has_Rep_Item (Typ, Name_Volatile_Full_Access)
13671 or else Has_Rep_Item (Typ, Name_Full_Access_Only))
13672 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
13673 (Get_Rep_Item (Typ, Name_Volatile_Full_Access))
13675 Set_Is_Volatile_Full_Access (Typ);
13676 Set_Is_Volatile (Typ);
13677 Set_Treat_As_Volatile (Typ);
13680 -- Inheritance for derived types only
13682 if Is_Derived_Type (Typ) then
13684 Bas_Typ : constant Entity_Id := Base_Type (Typ);
13685 Imp_Bas_Typ : constant Entity_Id := Implementation_Base_Type (Typ);
13688 -- Atomic_Components
13690 if not Has_Rep_Item (Typ, Name_Atomic_Components, False)
13691 and then Has_Rep_Item (Typ, Name_Atomic_Components)
13692 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
13693 (Get_Rep_Item (Typ, Name_Atomic_Components))
13695 Set_Has_Atomic_Components (Imp_Bas_Typ);
13698 -- Volatile_Components
13700 if not Has_Rep_Item (Typ, Name_Volatile_Components, False)
13701 and then Has_Rep_Item (Typ, Name_Volatile_Components)
13702 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
13703 (Get_Rep_Item (Typ, Name_Volatile_Components))
13705 Set_Has_Volatile_Components (Imp_Bas_Typ);
13708 -- Finalize_Storage_Only
13710 if not Has_Rep_Pragma (Typ, Name_Finalize_Storage_Only, False)
13711 and then Has_Rep_Pragma (Typ, Name_Finalize_Storage_Only)
13713 Set_Finalize_Storage_Only (Bas_Typ);
13716 -- Universal_Aliasing
13718 if not Has_Rep_Item (Typ, Name_Universal_Aliasing, False)
13719 and then Has_Rep_Item (Typ, Name_Universal_Aliasing)
13720 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
13721 (Get_Rep_Item (Typ, Name_Universal_Aliasing))
13723 Set_Universal_Aliasing (Imp_Bas_Typ);
13728 if Is_Record_Type (Typ) and then Typ = Bas_Typ then
13729 if not Has_Rep_Item (Typ, Name_Bit_Order, False)
13730 and then Has_Rep_Item (Typ, Name_Bit_Order)
13732 Set_Reverse_Bit_Order (Bas_Typ,
13734 (Implementation_Base_Type (Etype (Bas_Typ))));
13738 -- Scalar_Storage_Order
13740 if (Is_Record_Type (Typ) or else Is_Array_Type (Typ))
13741 and then Typ = Bas_Typ
13743 -- For a type extension, always inherit from parent; otherwise
13744 -- inherit if no default applies. Note: we do not check for
13745 -- an explicit rep item on the parent type when inheriting,
13746 -- because the parent SSO may itself have been set by default.
13748 if not Has_Rep_Item (First_Subtype (Typ),
13749 Name_Scalar_Storage_Order, False)
13750 and then (Is_Tagged_Type (Bas_Typ)
13751 or else not (SSO_Set_Low_By_Default (Bas_Typ)
13753 SSO_Set_High_By_Default (Bas_Typ)))
13755 Set_Reverse_Storage_Order (Bas_Typ,
13756 Reverse_Storage_Order
13757 (Implementation_Base_Type (Etype (Bas_Typ))));
13759 -- Clear default SSO indications, since the inherited aspect
13760 -- which was set explicitly overrides the default.
13762 Set_SSO_Set_Low_By_Default (Bas_Typ, False);
13763 Set_SSO_Set_High_By_Default (Bas_Typ, False);
13768 end Inherit_Aspects_At_Freeze_Point;
13770 ---------------------------------
13771 -- Inherit_Delayed_Rep_Aspects --
13772 ---------------------------------
13774 procedure Inherit_Delayed_Rep_Aspects (Typ : Entity_Id) is
13780 -- Find the first aspect that has been inherited
13782 N := First_Rep_Item (Typ);
13783 while Present (N) loop
13784 if Nkind (N) = N_Aspect_Specification then
13785 exit when Entity (N) /= Typ;
13791 -- There must be one if we reach here
13793 pragma Assert (Present (N));
13796 -- Loop through delayed aspects for the parent type
13798 while Present (N) loop
13799 if Nkind (N) = N_Aspect_Specification then
13800 exit when Entity (N) /= P;
13802 if Is_Delayed_Aspect (N) then
13803 A := Get_Aspect_Id (N);
13805 -- Process delayed rep aspect. For Boolean attributes it is
13806 -- not possible to cancel an attribute once set (the attempt
13807 -- to use an aspect with xxx => False is an error) for a
13808 -- derived type. So for those cases, we do not have to check
13809 -- if a clause has been given for the derived type, since it
13810 -- is harmless to set it again if it is already set.
13816 when Aspect_Alignment =>
13817 if not Has_Alignment_Clause (Typ) then
13818 Set_Alignment (Typ, Alignment (P));
13823 when Aspect_Atomic =>
13824 if Is_Atomic (P) then
13825 Set_Is_Atomic (Typ);
13828 -- Atomic_Components
13830 when Aspect_Atomic_Components =>
13831 if Has_Atomic_Components (P) then
13832 Set_Has_Atomic_Components (Base_Type (Typ));
13837 when Aspect_Bit_Order =>
13838 if Is_Record_Type (Typ)
13839 and then No (Get_Attribute_Definition_Clause
13840 (Typ, Attribute_Bit_Order))
13841 and then Reverse_Bit_Order (P)
13843 Set_Reverse_Bit_Order (Base_Type (Typ));
13848 when Aspect_Component_Size =>
13849 if Is_Array_Type (Typ)
13850 and then not Has_Component_Size_Clause (Typ)
13853 (Base_Type (Typ), Component_Size (P));
13858 when Aspect_Machine_Radix =>
13859 if Is_Decimal_Fixed_Point_Type (Typ)
13860 and then not Has_Machine_Radix_Clause (Typ)
13862 Set_Machine_Radix_10 (Typ, Machine_Radix_10 (P));
13865 -- Object_Size (also Size which also sets Object_Size)
13867 when Aspect_Object_Size
13870 if not Has_Size_Clause (Typ)
13872 No (Get_Attribute_Definition_Clause
13873 (Typ, Attribute_Object_Size))
13875 Set_Esize (Typ, Esize (P));
13880 when Aspect_Pack =>
13881 if not Is_Packed (Typ) then
13882 Set_Is_Packed (Base_Type (Typ));
13884 if Is_Bit_Packed_Array (P) then
13885 Set_Is_Bit_Packed_Array (Base_Type (Typ));
13886 Set_Packed_Array_Impl_Type
13887 (Typ, Packed_Array_Impl_Type (P));
13891 -- Scalar_Storage_Order
13893 when Aspect_Scalar_Storage_Order =>
13894 if (Is_Record_Type (Typ) or else Is_Array_Type (Typ))
13895 and then No (Get_Attribute_Definition_Clause
13896 (Typ, Attribute_Scalar_Storage_Order))
13897 and then Reverse_Storage_Order (P)
13899 Set_Reverse_Storage_Order (Base_Type (Typ));
13901 -- Clear default SSO indications, since the aspect
13902 -- overrides the default.
13904 Set_SSO_Set_Low_By_Default (Base_Type (Typ), False);
13905 Set_SSO_Set_High_By_Default (Base_Type (Typ), False);
13910 when Aspect_Small =>
13911 if Is_Fixed_Point_Type (Typ)
13912 and then not Has_Small_Clause (Typ)
13914 Set_Small_Value (Typ, Small_Value (P));
13919 when Aspect_Storage_Size =>
13920 if (Is_Access_Type (Typ) or else Is_Task_Type (Typ))
13921 and then not Has_Storage_Size_Clause (Typ)
13923 Set_Storage_Size_Variable
13924 (Base_Type (Typ), Storage_Size_Variable (P));
13929 when Aspect_Value_Size =>
13931 -- Value_Size is never inherited, it is either set by
13932 -- default, or it is explicitly set for the derived
13933 -- type. So nothing to do here.
13939 when Aspect_Volatile =>
13940 if Is_Volatile (P) then
13941 Set_Is_Volatile (Typ);
13944 -- Volatile_Full_Access (also Full_Access_Only)
13946 when Aspect_Volatile_Full_Access
13947 | Aspect_Full_Access_Only
13949 if Is_Volatile_Full_Access (P) then
13950 Set_Is_Volatile_Full_Access (Typ);
13953 -- Volatile_Components
13955 when Aspect_Volatile_Components =>
13956 if Has_Volatile_Components (P) then
13957 Set_Has_Volatile_Components (Base_Type (Typ));
13960 -- That should be all the Rep Aspects
13963 pragma Assert (Aspect_Delay (A) /= Rep_Aspect);
13971 end Inherit_Delayed_Rep_Aspects;
13977 procedure Initialize is
13979 Address_Clause_Checks.Init;
13980 Unchecked_Conversions.Init;
13982 -- The following might be needed in the future for some non-GCC back
13984 -- if AAMP_On_Target then
13985 -- Independence_Checks.Init;
13989 ---------------------------
13990 -- Install_Discriminants --
13991 ---------------------------
13993 procedure Install_Discriminants (E : Entity_Id) is
13997 Disc := First_Discriminant (E);
13998 while Present (Disc) loop
13999 Prev := Current_Entity (Disc);
14000 Set_Current_Entity (Disc);
14001 Set_Is_Immediately_Visible (Disc);
14002 Set_Homonym (Disc, Prev);
14003 Next_Discriminant (Disc);
14005 end Install_Discriminants;
14007 -------------------------
14008 -- Is_Operational_Item --
14009 -------------------------
14011 function Is_Operational_Item (N : Node_Id) return Boolean is
14013 -- List of operational items is given in AARM 13.1(8.mm/1). It is
14014 -- clearly incomplete, as it does not include iterator aspects, among
14017 return Nkind (N) = N_Attribute_Definition_Clause
14019 Get_Attribute_Id (Chars (N)) in Attribute_Constant_Indexing
14020 | Attribute_External_Tag
14021 | Attribute_Default_Iterator
14022 | Attribute_Implicit_Dereference
14024 | Attribute_Iterable
14025 | Attribute_Iterator_Element
14027 | Attribute_Put_Image
14029 | Attribute_Variable_Indexing
14031 end Is_Operational_Item;
14033 -------------------------
14034 -- Is_Predicate_Static --
14035 -------------------------
14037 -- Note: the basic legality of the expression has already been checked, so
14038 -- we don't need to worry about cases or ranges on strings for example.
14040 function Is_Predicate_Static
14043 Warn : Boolean := True) return Boolean
14045 function All_Static_Case_Alternatives (L : List_Id) return Boolean;
14046 -- Given a list of case expression alternatives, returns True if all
14047 -- the alternatives are static (have all static choices, and a static
14050 function Is_Type_Ref (N : Node_Id) return Boolean;
14051 pragma Inline (Is_Type_Ref);
14052 -- Returns True if N is a reference to the type for the predicate in the
14053 -- expression (i.e. if it is an identifier whose Chars field matches the
14054 -- Nam given in the call). N must not be parenthesized, if the type name
14055 -- appears in parens, this routine will return False.
14057 -- The routine also returns True for function calls generated during the
14058 -- expansion of comparison operators on strings, which are intended to
14059 -- be legal in static predicates, and are converted into calls to array
14060 -- comparison routines in the body of the corresponding predicate
14063 ----------------------------------
14064 -- All_Static_Case_Alternatives --
14065 ----------------------------------
14067 function All_Static_Case_Alternatives (L : List_Id) return Boolean is
14072 while Present (N) loop
14073 if not (All_Static_Choices (Discrete_Choices (N))
14074 and then Is_OK_Static_Expression (Expression (N)))
14083 end All_Static_Case_Alternatives;
14089 function Is_Type_Ref (N : Node_Id) return Boolean is
14091 return (Nkind (N) = N_Identifier
14092 and then Chars (N) = Nam
14093 and then Paren_Count (N) = 0);
14096 -- helper function for recursive calls
14097 function Is_Predicate_Static_Aux (Expr : Node_Id) return Boolean is
14098 (Is_Predicate_Static (Expr, Nam, Warn => False));
14100 -- Start of processing for Is_Predicate_Static
14103 -- Handle cases like
14104 -- subtype S is Integer with Static_Predicate =>
14105 -- (Some_Integer_Variable in Integer) and then (S /= 0);
14106 -- where the predicate (which should be rejected) might have been
14107 -- transformed into just "(S /= 0)", which would appear to be
14108 -- a predicate-static expression (and therefore legal).
14110 if Original_Node (Expr) /= Expr then
14112 -- Emit warnings for predicates that are always True or always False
14113 -- and were not originally expressed as Boolean literals.
14115 return Result : constant Boolean :=
14116 Is_Predicate_Static_Aux (Original_Node (Expr))
14118 if Result and then Warn and then Is_Entity_Name (Expr) then
14119 if Entity (Expr) = Standard_True then
14120 Error_Msg_N ("predicate is redundant (always True)?", Expr);
14121 elsif Entity (Expr) = Standard_False then
14123 ("predicate is unsatisfiable (always False)?", Expr);
14129 -- Predicate_Static means one of the following holds. Numbers are the
14130 -- corresponding paragraph numbers in (RM 3.2.4(16-22)).
14132 -- 16: A static expression
14134 if Is_OK_Static_Expression (Expr) then
14137 -- 17: A membership test whose simple_expression is the current
14138 -- instance, and whose membership_choice_list meets the requirements
14139 -- for a static membership test.
14141 elsif Nkind (Expr) in N_Membership_Test
14142 and then Is_Type_Ref (Left_Opnd (Expr))
14143 and then All_Membership_Choices_Static (Expr)
14147 -- 18. A case_expression whose selecting_expression is the current
14148 -- instance, and whose dependent expressions are static expressions.
14150 elsif Nkind (Expr) = N_Case_Expression
14151 and then Is_Type_Ref (Expression (Expr))
14152 and then All_Static_Case_Alternatives (Alternatives (Expr))
14156 -- 19. A call to a predefined equality or ordering operator, where one
14157 -- operand is the current instance, and the other is a static
14160 -- Note: the RM is clearly wrong here in not excluding string types.
14161 -- Without this exclusion, we would allow expressions like X > "ABC"
14162 -- to be considered as predicate-static, which is clearly not intended,
14163 -- since the idea is for predicate-static to be a subset of normal
14164 -- static expressions (and "DEF" > "ABC" is not a static expression).
14166 -- However, we do allow internally generated (not from source) equality
14167 -- and inequality operations to be valid on strings (this helps deal
14168 -- with cases where we transform A in "ABC" to A = "ABC).
14170 -- In fact, it appears that the intent of the ARG is to extend static
14171 -- predicates to strings, and that the extension should probably apply
14172 -- to static expressions themselves. The code below accepts comparison
14173 -- operators that apply to static strings.
14175 elsif Nkind (Expr) in N_Op_Compare
14176 and then ((Is_Type_Ref (Left_Opnd (Expr))
14177 and then Is_OK_Static_Expression (Right_Opnd (Expr)))
14179 (Is_Type_Ref (Right_Opnd (Expr))
14180 and then Is_OK_Static_Expression (Left_Opnd (Expr))))
14184 -- 20. A call to a predefined boolean logical operator, where each
14185 -- operand is predicate-static.
14187 elsif (Nkind (Expr) in N_Op_And | N_Op_Or | N_Op_Xor
14188 and then Is_Predicate_Static_Aux (Left_Opnd (Expr))
14189 and then Is_Predicate_Static_Aux (Right_Opnd (Expr)))
14191 (Nkind (Expr) = N_Op_Not
14192 and then Is_Predicate_Static_Aux (Right_Opnd (Expr)))
14196 -- 21. A short-circuit control form where both operands are
14197 -- predicate-static.
14199 elsif Nkind (Expr) in N_Short_Circuit
14200 and then Is_Predicate_Static_Aux (Left_Opnd (Expr))
14201 and then Is_Predicate_Static_Aux (Right_Opnd (Expr))
14205 -- 22. A parenthesized predicate-static expression. This does not
14206 -- require any special test, since we just ignore paren levels in
14207 -- all the cases above.
14209 -- One more test that is an implementation artifact caused by the fact
14210 -- that we are analyzing not the original expression, but the generated
14211 -- expression in the body of the predicate function. This can include
14212 -- references to inherited predicates, so that the expression we are
14213 -- processing looks like:
14215 -- xxPredicate (typ (Inns)) and then expression
14217 -- Where the call is to a Predicate function for an inherited predicate.
14218 -- We simply ignore such a call, which could be to either a dynamic or
14219 -- a static predicate. Note that if the parent predicate is dynamic then
14220 -- eventually this type will be marked as dynamic, but you are allowed
14221 -- to specify a static predicate for a subtype which is inheriting a
14222 -- dynamic predicate, so the static predicate validation here ignores
14223 -- the inherited predicate even if it is dynamic.
14224 -- In all cases, a static predicate can only apply to a scalar type.
14226 elsif Nkind (Expr) = N_Function_Call
14227 and then Is_Predicate_Function (Entity (Name (Expr)))
14228 and then Is_Scalar_Type (Etype (First_Entity (Entity (Name (Expr)))))
14232 -- That's an exhaustive list of tests, all other cases are not
14233 -- predicate-static, so we return False.
14238 end Is_Predicate_Static;
14240 ----------------------
14241 -- Is_Static_Choice --
14242 ----------------------
14244 function Is_Static_Choice (N : Node_Id) return Boolean is
14246 return Nkind (N) = N_Others_Choice
14247 or else Is_OK_Static_Expression (N)
14248 or else (Is_Entity_Name (N) and then Is_Type (Entity (N))
14249 and then Is_OK_Static_Subtype (Entity (N)))
14250 or else (Nkind (N) = N_Subtype_Indication
14251 and then Is_OK_Static_Subtype (Entity (N)))
14252 or else (Nkind (N) = N_Range and then Is_OK_Static_Range (N));
14253 end Is_Static_Choice;
14255 ------------------------------
14256 -- Is_Type_Related_Rep_Item --
14257 ------------------------------
14259 function Is_Type_Related_Rep_Item (N : Node_Id) return Boolean is
14262 when N_Attribute_Definition_Clause =>
14263 -- See AARM 13.1(8.f-8.x) list items that end in "clause"
14264 -- ???: include any GNAT-defined attributes here?
14265 return Get_Attribute_Id (Chars (N)) in Attribute_Bit_Order
14266 | Attribute_Component_Size
14267 | Attribute_Machine_Radix
14268 | Attribute_Storage_Pool
14269 | Attribute_Stream_Size;
14272 case Get_Pragma_Id (N) is
14273 -- See AARM 13.1(8.f-8.x) list items that start with "pragma"
14274 -- ???: include any GNAT-defined pragmas here?
14278 | Pragma_Convention
14280 | Pragma_Independent
14282 | Pragma_Atomic_Components
14283 | Pragma_Independent_Components
14284 | Pragma_Volatile_Components
14285 | Pragma_Discard_Names
14292 when N_Enumeration_Representation_Clause
14293 | N_Record_Representation_Clause
14302 end Is_Type_Related_Rep_Item;
14304 ---------------------
14305 -- Kill_Rep_Clause --
14306 ---------------------
14308 procedure Kill_Rep_Clause (N : Node_Id) is
14310 pragma Assert (Ignore_Rep_Clauses);
14312 -- Note: we use Replace rather than Rewrite, because we don't want
14313 -- tools to be able to use Original_Node to dig out the (undecorated)
14314 -- rep clause that is being replaced.
14316 Replace (N, Make_Null_Statement (Sloc (N)));
14318 -- The null statement must be marked as not coming from source. This is
14319 -- so that tools ignore it, and also the back end does not expect bogus
14320 -- "from source" null statements in weird places (e.g. in declarative
14321 -- regions where such null statements are not allowed).
14323 Set_Comes_From_Source (N, False);
14324 end Kill_Rep_Clause;
14330 function Minimum_Size
14332 Biased : Boolean := False) return Int
14334 Lo : Uint := No_Uint;
14335 Hi : Uint := No_Uint;
14336 LoR : Ureal := No_Ureal;
14337 HiR : Ureal := No_Ureal;
14338 LoSet : Boolean := False;
14339 HiSet : Boolean := False;
14342 Ancest : Entity_Id;
14343 R_Typ : constant Entity_Id := Root_Type (T);
14348 if T = Any_Type then
14349 return Unknown_Minimum_Size;
14351 -- For generic types, just return unknown. There cannot be any
14352 -- legitimate need to know such a size, but this routine may be
14353 -- called with a generic type as part of normal processing.
14355 elsif Is_Generic_Type (R_Typ) or else R_Typ = Any_Type then
14356 return Unknown_Minimum_Size;
14358 -- Access types (cannot have size smaller than System.Address)
14360 elsif Is_Access_Type (T) then
14361 return System_Address_Size;
14363 -- Floating-point types
14365 elsif Is_Floating_Point_Type (T) then
14366 return UI_To_Int (Esize (R_Typ));
14370 elsif Is_Discrete_Type (T) then
14372 -- The following loop is looking for the nearest compile time known
14373 -- bounds following the ancestor subtype chain. The idea is to find
14374 -- the most restrictive known bounds information.
14378 if Ancest = Any_Type or else Etype (Ancest) = Any_Type then
14379 return Unknown_Minimum_Size;
14383 if Compile_Time_Known_Value (Type_Low_Bound (Ancest)) then
14384 Lo := Expr_Rep_Value (Type_Low_Bound (Ancest));
14391 if Compile_Time_Known_Value (Type_High_Bound (Ancest)) then
14392 Hi := Expr_Rep_Value (Type_High_Bound (Ancest));
14398 Ancest := Ancestor_Subtype (Ancest);
14400 if No (Ancest) then
14401 Ancest := Base_Type (T);
14403 if Is_Generic_Type (Ancest) then
14404 return Unknown_Minimum_Size;
14409 -- Fixed-point types. We can't simply use Expr_Value to get the
14410 -- Corresponding_Integer_Value values of the bounds, since these do not
14411 -- get set till the type is frozen, and this routine can be called
14412 -- before the type is frozen. Similarly the test for bounds being static
14413 -- needs to include the case where we have unanalyzed real literals for
14414 -- the same reason.
14416 elsif Is_Fixed_Point_Type (T) then
14418 -- The following loop is looking for the nearest compile time known
14419 -- bounds following the ancestor subtype chain. The idea is to find
14420 -- the most restrictive known bounds information.
14424 if Ancest = Any_Type or else Etype (Ancest) = Any_Type then
14425 return Unknown_Minimum_Size;
14428 -- Note: In the following two tests for LoSet and HiSet, it may
14429 -- seem redundant to test for N_Real_Literal here since normally
14430 -- one would assume that the test for the value being known at
14431 -- compile time includes this case. However, there is a glitch.
14432 -- If the real literal comes from folding a non-static expression,
14433 -- then we don't consider any non- static expression to be known
14434 -- at compile time if we are in configurable run time mode (needed
14435 -- in some cases to give a clearer definition of what is and what
14436 -- is not accepted). So the test is indeed needed. Without it, we
14437 -- would set neither Lo_Set nor Hi_Set and get an infinite loop.
14440 if Nkind (Type_Low_Bound (Ancest)) = N_Real_Literal
14441 or else Compile_Time_Known_Value (Type_Low_Bound (Ancest))
14443 LoR := Expr_Value_R (Type_Low_Bound (Ancest));
14450 if Nkind (Type_High_Bound (Ancest)) = N_Real_Literal
14451 or else Compile_Time_Known_Value (Type_High_Bound (Ancest))
14453 HiR := Expr_Value_R (Type_High_Bound (Ancest));
14459 Ancest := Ancestor_Subtype (Ancest);
14461 if No (Ancest) then
14462 Ancest := Base_Type (T);
14464 if Is_Generic_Type (Ancest) then
14465 return Unknown_Minimum_Size;
14470 Lo := UR_To_Uint (LoR / Small_Value (T));
14471 Hi := UR_To_Uint (HiR / Small_Value (T));
14473 -- No other types allowed
14476 raise Program_Error;
14479 -- Fall through with Hi and Lo set. Deal with biased case
14482 and then not Is_Fixed_Point_Type (T)
14483 and then not (Is_Enumeration_Type (T)
14484 and then Has_Non_Standard_Rep (T)))
14485 or else Has_Biased_Representation (T)
14491 -- Null range case, size is always zero. We only do this in the discrete
14492 -- type case, since that's the odd case that came up. Probably we should
14493 -- also do this in the fixed-point case, but doing so causes peculiar
14494 -- gigi failures, and it is not worth worrying about this incredibly
14495 -- marginal case (explicit null-range fixed-point type declarations).
14497 if Lo > Hi and then Is_Discrete_Type (T) then
14500 -- Signed case. Note that we consider types like range 1 .. -1 to be
14501 -- signed for the purpose of computing the size, since the bounds have
14502 -- to be accommodated in the base type.
14504 elsif Lo < 0 or else Hi < 0 then
14508 -- S = size, B = 2 ** (size - 1) (can accommodate -B .. +(B - 1))
14509 -- Note that we accommodate the case where the bounds cross. This
14510 -- can happen either because of the way the bounds are declared
14511 -- or because of the algorithm in Freeze_Fixed_Point_Type.
14525 -- If both bounds are positive, make sure that both are represen-
14526 -- table in the case where the bounds are crossed. This can happen
14527 -- either because of the way the bounds are declared, or because of
14528 -- the algorithm in Freeze_Fixed_Point_Type.
14534 -- S = size, (can accommodate 0 .. (2**size - 1))
14537 while Hi >= Uint_2 ** S loop
14545 ------------------------------
14546 -- New_Put_Image_Subprogram --
14547 ------------------------------
14549 procedure New_Put_Image_Subprogram
14554 Loc : constant Source_Ptr := Sloc (N);
14555 Sname : constant Name_Id :=
14556 Make_TSS_Name (Base_Type (Ent), TSS_Put_Image);
14557 Subp_Id : Entity_Id;
14558 Subp_Decl : Node_Id;
14562 Defer_Declaration : constant Boolean :=
14563 Is_Tagged_Type (Ent) or else Is_Private_Type (Ent);
14564 -- For a tagged type, there is a declaration at the freeze point, and
14565 -- we must generate only a completion of this declaration. We do the
14566 -- same for private types, because the full view might be tagged.
14567 -- Otherwise we generate a declaration at the point of the attribute
14568 -- definition clause. If the attribute definition comes from an aspect
14569 -- specification the declaration is part of the freeze actions of the
14572 function Build_Spec return Node_Id;
14573 -- Used for declaration and renaming declaration, so that this is
14574 -- treated as a renaming_as_body.
14580 function Build_Spec return Node_Id is
14583 T_Ref : constant Node_Id := New_Occurrence_Of (Etyp, Loc);
14586 Subp_Id := Make_Defining_Identifier (Loc, Sname);
14588 -- S : Root_Buffer_Type'Class
14590 Formals := New_List (
14591 Make_Parameter_Specification (Loc,
14592 Defining_Identifier =>
14593 Make_Defining_Identifier (Loc, Name_S),
14594 In_Present => True,
14595 Out_Present => True,
14597 New_Occurrence_Of (Etype (F), Loc)));
14601 Append_To (Formals,
14602 Make_Parameter_Specification (Loc,
14603 Defining_Identifier => Make_Defining_Identifier (Loc, Name_V),
14604 Parameter_Type => T_Ref));
14607 Make_Procedure_Specification (Loc,
14608 Defining_Unit_Name => Subp_Id,
14609 Parameter_Specifications => Formals);
14614 -- Start of processing for New_Put_Image_Subprogram
14617 F := First_Formal (Subp);
14619 Etyp := Etype (Next_Formal (F));
14621 -- Prepare subprogram declaration and insert it as an action on the
14622 -- clause node. The visibility for this entity is used to test for
14623 -- visibility of the attribute definition clause (in the sense of
14624 -- 8.3(23) as amended by AI-195).
14626 if not Defer_Declaration then
14628 Make_Subprogram_Declaration (Loc,
14629 Specification => Build_Spec);
14631 -- For a tagged type, there is always a visible declaration for the
14632 -- Put_Image TSS (it is a predefined primitive operation), and the
14633 -- completion of this declaration occurs at the freeze point, which is
14634 -- not always visible at places where the attribute definition clause is
14635 -- visible. So, we create a dummy entity here for the purpose of
14636 -- tracking the visibility of the attribute definition clause itself.
14640 Make_Defining_Identifier (Loc, New_External_Name (Sname, 'V'));
14642 Make_Object_Declaration (Loc,
14643 Defining_Identifier => Subp_Id,
14644 Object_Definition => New_Occurrence_Of (Standard_Boolean, Loc));
14647 if not Defer_Declaration
14648 and then From_Aspect_Specification (N)
14649 and then Has_Delayed_Freeze (Ent)
14651 Append_Freeze_Action (Ent, Subp_Decl);
14654 Insert_Action (N, Subp_Decl);
14655 Set_Entity (N, Subp_Id);
14659 Make_Subprogram_Renaming_Declaration (Loc,
14660 Specification => Build_Spec,
14661 Name => New_Occurrence_Of (Subp, Loc));
14663 if Defer_Declaration then
14664 Set_TSS (Base_Type (Ent), Subp_Id);
14667 if From_Aspect_Specification (N) then
14668 Append_Freeze_Action (Ent, Subp_Decl);
14670 Insert_Action (N, Subp_Decl);
14673 Copy_TSS (Subp_Id, Base_Type (Ent));
14675 end New_Put_Image_Subprogram;
14677 ---------------------------
14678 -- New_Stream_Subprogram --
14679 ---------------------------
14681 procedure New_Stream_Subprogram
14685 Nam : TSS_Name_Type)
14687 Loc : constant Source_Ptr := Sloc (N);
14688 Sname : constant Name_Id := Make_TSS_Name (Base_Type (Ent), Nam);
14689 Subp_Id : Entity_Id;
14690 Subp_Decl : Node_Id;
14694 Defer_Declaration : constant Boolean :=
14695 Is_Tagged_Type (Ent) or else Is_Private_Type (Ent);
14696 -- For a tagged type, there is a declaration for each stream attribute
14697 -- at the freeze point, and we must generate only a completion of this
14698 -- declaration. We do the same for private types, because the full view
14699 -- might be tagged. Otherwise we generate a declaration at the point of
14700 -- the attribute definition clause. If the attribute definition comes
14701 -- from an aspect specification the declaration is part of the freeze
14702 -- actions of the type.
14704 function Build_Spec return Node_Id;
14705 -- Used for declaration and renaming declaration, so that this is
14706 -- treated as a renaming_as_body.
14712 function Build_Spec return Node_Id is
14713 Out_P : constant Boolean := (Nam = TSS_Stream_Read);
14716 T_Ref : constant Node_Id := New_Occurrence_Of (Etyp, Loc);
14719 Subp_Id := Make_Defining_Identifier (Loc, Sname);
14721 -- S : access Root_Stream_Type'Class
14723 Formals := New_List (
14724 Make_Parameter_Specification (Loc,
14725 Defining_Identifier =>
14726 Make_Defining_Identifier (Loc, Name_S),
14728 Make_Access_Definition (Loc,
14730 New_Occurrence_Of (
14731 Designated_Type (Etype (F)), Loc))));
14733 if Nam = TSS_Stream_Input then
14735 Make_Function_Specification (Loc,
14736 Defining_Unit_Name => Subp_Id,
14737 Parameter_Specifications => Formals,
14738 Result_Definition => T_Ref);
14742 Append_To (Formals,
14743 Make_Parameter_Specification (Loc,
14744 Defining_Identifier => Make_Defining_Identifier (Loc, Name_V),
14745 Out_Present => Out_P,
14746 Parameter_Type => T_Ref));
14749 Make_Procedure_Specification (Loc,
14750 Defining_Unit_Name => Subp_Id,
14751 Parameter_Specifications => Formals);
14757 -- Start of processing for New_Stream_Subprogram
14760 F := First_Formal (Subp);
14762 if Ekind (Subp) = E_Procedure then
14763 Etyp := Etype (Next_Formal (F));
14765 Etyp := Etype (Subp);
14768 -- Prepare subprogram declaration and insert it as an action on the
14769 -- clause node. The visibility for this entity is used to test for
14770 -- visibility of the attribute definition clause (in the sense of
14771 -- 8.3(23) as amended by AI-195).
14773 if not Defer_Declaration then
14775 Make_Subprogram_Declaration (Loc,
14776 Specification => Build_Spec);
14778 -- For a tagged type, there is always a visible declaration for each
14779 -- stream TSS (it is a predefined primitive operation), and the
14780 -- completion of this declaration occurs at the freeze point, which is
14781 -- not always visible at places where the attribute definition clause is
14782 -- visible. So, we create a dummy entity here for the purpose of
14783 -- tracking the visibility of the attribute definition clause itself.
14787 Make_Defining_Identifier (Loc, New_External_Name (Sname, 'V'));
14789 Make_Object_Declaration (Loc,
14790 Defining_Identifier => Subp_Id,
14791 Object_Definition => New_Occurrence_Of (Standard_Boolean, Loc));
14794 if not Defer_Declaration
14795 and then From_Aspect_Specification (N)
14796 and then Has_Delayed_Freeze (Ent)
14798 Append_Freeze_Action (Ent, Subp_Decl);
14801 Insert_Action (N, Subp_Decl);
14802 Set_Entity (N, Subp_Id);
14806 Make_Subprogram_Renaming_Declaration (Loc,
14807 Specification => Build_Spec,
14808 Name => New_Occurrence_Of (Subp, Loc));
14810 if Defer_Declaration then
14811 Set_TSS (Base_Type (Ent), Subp_Id);
14814 if From_Aspect_Specification (N) then
14815 Append_Freeze_Action (Ent, Subp_Decl);
14817 Insert_Action (N, Subp_Decl);
14820 Copy_TSS (Subp_Id, Base_Type (Ent));
14822 end New_Stream_Subprogram;
14824 ----------------------
14825 -- No_Type_Rep_Item --
14826 ----------------------
14828 procedure No_Type_Rep_Item (N : Node_Id) is
14830 Error_Msg_N ("|type-related representation item not permitted!", N);
14831 end No_Type_Rep_Item;
14837 procedure Pop_Type (E : Entity_Id) is
14839 if Ekind (E) = E_Record_Type and then E = Current_Scope then
14843 and then Has_Discriminants (E)
14844 and then Nkind (Parent (E)) /= N_Subtype_Declaration
14846 Uninstall_Discriminants (E);
14855 procedure Push_Type (E : Entity_Id) is
14859 if Ekind (E) = E_Record_Type then
14862 Comp := First_Component (E);
14863 while Present (Comp) loop
14864 Install_Entity (Comp);
14865 Next_Component (Comp);
14868 if Has_Discriminants (E) then
14869 Install_Discriminants (E);
14873 and then Has_Discriminants (E)
14874 and then Nkind (Parent (E)) /= N_Subtype_Declaration
14877 Install_Discriminants (E);
14881 -----------------------------------
14882 -- Register_Address_Clause_Check --
14883 -----------------------------------
14885 procedure Register_Address_Clause_Check
14892 ACS : constant Boolean := Scope_Suppress.Suppress (Alignment_Check);
14894 Address_Clause_Checks.Append ((N, X, A, Y, Off, ACS));
14895 end Register_Address_Clause_Check;
14897 ------------------------
14898 -- Rep_Item_Too_Early --
14899 ------------------------
14901 function Rep_Item_Too_Early (T : Entity_Id; N : Node_Id) return Boolean is
14902 function Has_Generic_Parent (E : Entity_Id) return Boolean;
14903 -- Return True if R or any ancestor is a generic type
14905 ------------------------
14906 -- Has_Generic_Parent --
14907 ------------------------
14909 function Has_Generic_Parent (E : Entity_Id) return Boolean is
14910 Ancestor_Type : Entity_Id := Etype (E);
14913 if Is_Generic_Type (E) then
14917 while Present (Ancestor_Type)
14918 and then not Is_Generic_Type (Ancestor_Type)
14919 and then Etype (Ancestor_Type) /= Ancestor_Type
14921 Ancestor_Type := Etype (Ancestor_Type);
14925 Present (Ancestor_Type) and then Is_Generic_Type (Ancestor_Type);
14926 end Has_Generic_Parent;
14928 -- Start of processing for Rep_Item_Too_Early
14931 -- Cannot apply non-operational rep items to generic types
14933 if Is_Operational_Item (N) then
14937 and then Has_Generic_Parent (T)
14938 and then (Nkind (N) /= N_Pragma
14939 or else Get_Pragma_Id (N) /= Pragma_Convention)
14941 if Ada_Version < Ada_2022 then
14943 ("representation item not allowed for generic type", N);
14950 -- Otherwise check for incomplete type
14952 if Is_Incomplete_Or_Private_Type (T)
14953 and then No (Underlying_Type (T))
14955 (Nkind (N) /= N_Pragma
14956 or else Get_Pragma_Id (N) /= Pragma_Import)
14959 ("representation item must be after full type declaration", N);
14962 -- If the type has incomplete components, a representation clause is
14963 -- illegal but stream attributes and Convention pragmas are correct.
14965 elsif Has_Private_Component (T) then
14966 if Nkind (N) = N_Pragma then
14971 ("representation item must appear after type is fully defined",
14978 end Rep_Item_Too_Early;
14980 -----------------------
14981 -- Rep_Item_Too_Late --
14982 -----------------------
14984 function Rep_Item_Too_Late
14987 FOnly : Boolean := False) return Boolean
14989 procedure Too_Late;
14990 -- Output message for an aspect being specified too late
14992 -- Note that neither of the above errors is considered a serious one,
14993 -- since the effect is simply that we ignore the representation clause
14995 -- Is this really true? In any case if we make this change we must
14996 -- document the requirement in the spec of Rep_Item_Too_Late that
14997 -- if True is returned, then the rep item must be completely ignored???
15003 procedure Too_Late is
15005 -- Other compilers seem more relaxed about rep items appearing too
15006 -- late. Since analysis tools typically don't care about rep items
15007 -- anyway, no reason to be too strict about this.
15009 if not Relaxed_RM_Semantics then
15010 Error_Msg_N ("|representation item appears too late!", N);
15016 Parent_Type : Entity_Id;
15019 -- Start of processing for Rep_Item_Too_Late
15022 -- First make sure entity is not frozen (RM 13.1(9))
15026 -- Exclude imported types, which may be frozen if they appear in a
15027 -- representation clause for a local type.
15029 and then not From_Limited_With (T)
15031 -- Exclude generated entities (not coming from source). The common
15032 -- case is when we generate a renaming which prematurely freezes the
15033 -- renamed internal entity, but we still want to be able to set copies
15034 -- of attribute values such as Size/Alignment.
15036 and then Comes_From_Source (T)
15038 -- A self-referential aspect is illegal if it forces freezing the
15039 -- entity before the corresponding pragma has been analyzed.
15041 if Nkind (N) in N_Attribute_Definition_Clause | N_Pragma
15042 and then From_Aspect_Specification (N)
15045 ("aspect specification causes premature freezing of&", N, T);
15046 Set_Has_Delayed_Freeze (T, False);
15051 S := First_Subtype (T);
15053 if Present (Freeze_Node (S)) then
15054 if not Relaxed_RM_Semantics then
15056 ("??no more representation items for }", Freeze_Node (S), S);
15062 -- Check for case of untagged derived type whose parent either has
15063 -- primitive operations (pre Ada 2022), or is a by-reference type (RM
15064 -- 13.1(10)). In this case we do not output a Too_Late message, since
15065 -- there is no earlier point where the rep item could be placed to make
15067 -- ??? Confirming representation clauses should be allowed here.
15071 and then Is_Derived_Type (T)
15072 and then not Is_Tagged_Type (T)
15074 Parent_Type := Etype (Base_Type (T));
15076 if Relaxed_RM_Semantics then
15079 elsif Ada_Version <= Ada_2012
15080 and then Has_Primitive_Operations (Parent_Type)
15083 ("|representation item not permitted before Ada 2022!", N);
15085 ("\parent type & has primitive operations!", N, Parent_Type);
15088 elsif Is_By_Reference_Type (Parent_Type) then
15089 No_Type_Rep_Item (N);
15091 ("\parent type & is a by-reference type!", N, Parent_Type);
15096 -- No error, but one more warning to consider. The RM (surprisingly)
15097 -- allows this pattern in some cases:
15100 -- primitive operations for S
15101 -- type R is new S;
15102 -- rep clause for S
15104 -- Meaning that calls on the primitive operations of S for values of
15105 -- type R may require possibly expensive implicit conversion operations.
15106 -- So even when this is not an error, it is still worth a warning.
15108 if not Relaxed_RM_Semantics and then Is_Type (T) then
15110 DTL : constant Entity_Id := Derived_Type_Link (Base_Type (T));
15115 -- For now, do not generate this warning for the case of
15116 -- aspect specification using Ada 2012 syntax, since we get
15117 -- wrong messages we do not understand. The whole business
15118 -- of derived types and rep items seems a bit confused when
15119 -- aspects are used, since the aspects are not evaluated
15120 -- till freeze time. However, AI12-0109 confirms (in an AARM
15121 -- ramification) that inheritance in this case is required
15124 and then not From_Aspect_Specification (N)
15126 if Is_By_Reference_Type (T)
15127 and then not Is_Tagged_Type (T)
15128 and then Is_Type_Related_Rep_Item (N)
15129 and then (Ada_Version >= Ada_2012
15130 or else Has_Primitive_Operations (Base_Type (T)))
15132 -- Treat as hard error (AI12-0109, binding interpretation).
15133 -- Implementing a change of representation is not really
15134 -- an option in the case of a by-reference type, so we
15135 -- take this path for all Ada dialects if primitive
15136 -- operations are present.
15137 Error_Msg_Sloc := Sloc (DTL);
15139 ("representation item for& appears after derived type "
15140 & "declaration#", N);
15142 elsif Has_Primitive_Operations (Base_Type (T)) then
15143 Error_Msg_Sloc := Sloc (DTL);
15146 ("representation item for& appears after derived type "
15147 & "declaration#??", N);
15149 ("\may result in implicit conversions for primitive "
15150 & "operations of&??", N, T);
15152 ("\to change representations when called with arguments "
15153 & "of type&??", N, DTL);
15159 -- No error, link item into head of chain of rep items for the entity,
15160 -- but avoid chaining if we have an overloadable entity, and the pragma
15161 -- is one that can apply to multiple overloaded entities.
15163 if Is_Overloadable (T) and then Nkind (N) = N_Pragma then
15165 Pname : constant Name_Id := Pragma_Name (N);
15167 if Pname in Name_Convention | Name_Import | Name_Export
15168 | Name_External | Name_Interface
15175 Record_Rep_Item (T, N);
15177 end Rep_Item_Too_Late;
15179 -------------------------------------
15180 -- Replace_Type_References_Generic --
15181 -------------------------------------
15183 procedure Replace_Type_References_Generic (N : Node_Id; T : Entity_Id) is
15184 TName : constant Name_Id := Chars (T);
15186 function Replace_Type_Ref (N : Node_Id) return Traverse_Result;
15187 -- Processes a single node in the traversal procedure below, checking
15188 -- if node N should be replaced, and if so, doing the replacement.
15190 function Visible_Component (Comp : Name_Id) return Entity_Id;
15191 -- Given an identifier in the expression, check whether there is a
15192 -- discriminant, component, protected procedure, or entry of the type
15193 -- that is directy visible, and rewrite it as the corresponding selected
15194 -- component of the formal of the subprogram.
15196 ----------------------
15197 -- Replace_Type_Ref --
15198 ----------------------
15200 function Replace_Type_Ref (N : Node_Id) return Traverse_Result is
15201 Loc : constant Source_Ptr := Sloc (N);
15203 procedure Add_Prefix (Ref : Node_Id; Comp : Entity_Id);
15204 -- Add the proper prefix to a reference to a component of the type
15205 -- when it is not already a selected component.
15211 procedure Add_Prefix (Ref : Node_Id; Comp : Entity_Id) is
15214 Make_Selected_Component (Loc,
15215 Prefix => New_Occurrence_Of (T, Loc),
15216 Selector_Name => New_Occurrence_Of (Comp, Loc)));
15217 Replace_Type_Reference (Prefix (Ref));
15226 -- Start of processing for Replace_Type_Ref
15229 if Nkind (N) = N_Identifier then
15231 -- If not the type name, check whether it is a reference to some
15232 -- other type, which must be frozen before the predicate function
15233 -- is analyzed, i.e. before the freeze node of the type to which
15234 -- the predicate applies.
15236 if Chars (N) /= TName then
15237 if Present (Current_Entity (N))
15238 and then Is_Type (Current_Entity (N))
15240 Freeze_Before (Freeze_Node (T), Current_Entity (N));
15243 -- The components of the type are directly visible and can
15244 -- be referenced in the source code without a prefix.
15245 -- If a name denoting a component doesn't already have a
15246 -- prefix, then normalize it by adding a reference to the
15247 -- current instance of the type as a prefix.
15249 -- This isn't right in the pathological corner case of an
15250 -- object-declaring expression (e.g., a quantified expression
15251 -- or a declare expression) that declares an object with the
15252 -- same name as a visible component declaration, thereby hiding
15253 -- the component within that expression. For example, given a
15254 -- record with a Boolean component "C" and a dynamic predicate
15255 -- "C = (for some C in Character => Some_Function (C))", only
15256 -- the first of the two uses of C should have a prefix added
15257 -- here; instead, both will get prefixes.
15259 if Nkind (Parent (N)) /= N_Selected_Component
15260 or else N /= Selector_Name (Parent (N))
15262 Comp := Visible_Component (Chars (N));
15264 if Present (Comp) then
15265 Add_Prefix (N, Comp);
15271 -- Otherwise do the replacement if this is not a qualified
15272 -- reference to a homograph of the type itself. Note that the
15273 -- current instance could not appear in such a context, e.g.
15274 -- the prefix of a type conversion.
15277 if Nkind (Parent (N)) /= N_Selected_Component
15278 or else N /= Selector_Name (Parent (N))
15280 Replace_Type_Reference (N);
15286 -- Case of selected component, which may be a subcomponent of the
15287 -- current instance, or an expanded name which is still unanalyzed.
15289 elsif Nkind (N) = N_Selected_Component then
15291 -- If selector name is not our type, keep going (we might still
15292 -- have an occurrence of the type in the prefix). If it is a
15293 -- subcomponent of the current entity, add prefix.
15295 if Nkind (Selector_Name (N)) /= N_Identifier
15296 or else Chars (Selector_Name (N)) /= TName
15298 if Nkind (Prefix (N)) = N_Identifier then
15299 Comp := Visible_Component (Chars (Prefix (N)));
15301 if Present (Comp) then
15302 Add_Prefix (Prefix (N), Comp);
15308 -- Selector name is our type, check qualification
15311 -- Loop through scopes and prefixes, doing comparison
15313 Scop := Current_Scope;
15314 Pref := Prefix (N);
15316 -- Continue if no more scopes or scope with no name
15318 if No (Scop) or else Nkind (Scop) not in N_Has_Chars then
15322 -- Do replace if prefix is an identifier matching the scope
15323 -- that we are currently looking at.
15325 if Nkind (Pref) = N_Identifier
15326 and then Chars (Pref) = Chars (Scop)
15328 Replace_Type_Reference (N);
15332 -- Go check scope above us if prefix is itself of the form
15333 -- of a selected component, whose selector matches the scope
15334 -- we are currently looking at.
15336 if Nkind (Pref) = N_Selected_Component
15337 and then Nkind (Selector_Name (Pref)) = N_Identifier
15338 and then Chars (Selector_Name (Pref)) = Chars (Scop)
15340 Scop := Scope (Scop);
15341 Pref := Prefix (Pref);
15343 -- For anything else, we don't have a match, so keep on
15344 -- going, there are still some weird cases where we may
15345 -- still have a replacement within the prefix.
15353 -- Continue for any other node kind
15358 end Replace_Type_Ref;
15360 procedure Replace_Type_Refs is new Traverse_Proc (Replace_Type_Ref);
15362 -----------------------
15363 -- Visible_Component --
15364 -----------------------
15366 function Visible_Component (Comp : Name_Id) return Entity_Id is
15369 -- Types with nameable components are record, task, and protected
15370 -- types, and discriminated private types.
15372 if Ekind (T) in E_Record_Type
15375 or else (Is_Private_Type (T) and then Has_Discriminants (T))
15377 -- This is a sequential search, which seems acceptable
15378 -- efficiency-wise, given the typical size of component
15379 -- lists, protected operation lists, task item lists, and
15380 -- check expressions.
15382 E := First_Entity (T);
15383 while Present (E) loop
15384 if Comes_From_Source (E) and then Chars (E) = Comp then
15392 -- Nothing by that name
15395 end Visible_Component;
15397 -- Start of processing for Replace_Type_References_Generic
15400 Replace_Type_Refs (N);
15401 end Replace_Type_References_Generic;
15403 --------------------------------
15404 -- Resolve_Aspect_Expressions --
15405 --------------------------------
15407 procedure Resolve_Aspect_Expressions (E : Entity_Id) is
15408 function Resolve_Name (N : Node_Id) return Traverse_Result;
15409 -- Verify that all identifiers in the expression, with the exception
15410 -- of references to the current entity, denote visible entities. This
15411 -- is done only to detect visibility errors, as the expression will be
15412 -- properly analyzed/expanded during analysis of the predicate function
15413 -- body. We omit quantified expressions from this test, given that they
15414 -- introduce a local identifier that would require proper expansion to
15415 -- handle properly.
15421 function Resolve_Name (N : Node_Id) return Traverse_Result is
15422 Dummy : Traverse_Result;
15425 if Nkind (N) = N_Selected_Component then
15426 if Nkind (Prefix (N)) = N_Identifier
15427 and then Chars (Prefix (N)) /= Chars (E)
15429 Find_Selected_Component (N);
15434 -- Resolve identifiers that are not selectors in parameter
15435 -- associations (these are never resolved by visibility).
15437 elsif Nkind (N) = N_Identifier
15438 and then Chars (N) /= Chars (E)
15439 and then (Nkind (Parent (N)) /= N_Parameter_Association
15440 or else N /= Selector_Name (Parent (N)))
15442 Find_Direct_Name (N);
15444 -- Reset the Entity if N is overloaded since the entity may not
15445 -- be the correct one.
15447 if Is_Overloaded (N) then
15448 Set_Entity (N, Empty);
15451 -- The name in a component association needs no resolution
15453 elsif Nkind (N) = N_Component_Association then
15454 Dummy := Resolve_Name (Expression (N));
15457 elsif Nkind (N) = N_Quantified_Expression then
15464 procedure Resolve_Aspect_Expression is new Traverse_Proc (Resolve_Name);
15468 ASN : Node_Id := First_Rep_Item (E);
15470 -- Start of processing for Resolve_Aspect_Expressions
15473 while Present (ASN) loop
15474 if Nkind (ASN) = N_Aspect_Specification and then Entity (ASN) = E then
15476 A_Id : constant Aspect_Id := Get_Aspect_Id (ASN);
15477 Expr : constant Node_Id := Expression (ASN);
15482 when Aspect_Aggregate =>
15483 Resolve_Aspect_Aggregate (Entity (ASN), Expr);
15485 when Aspect_Stable_Properties =>
15486 Resolve_Aspect_Stable_Properties
15487 (Entity (ASN), Expr, Class_Present (ASN));
15489 -- For now we only deal with aspects that do not generate
15490 -- subprograms, or that may mention current instances of
15491 -- types. These will require special handling???.
15493 when Aspect_Invariant
15494 | Aspect_Predicate_Failure
15498 when Aspect_Dynamic_Predicate
15499 | Aspect_Static_Predicate
15502 -- Preanalyze expression after type replacement to catch
15503 -- name resolution errors if the predicate function has
15504 -- not been built yet.
15505 -- Note that we cannot use Preanalyze_Spec_Expression
15506 -- because of the special handling required for
15507 -- quantifiers, see comments on Resolve_Aspect_Expression
15510 if No (Predicate_Function (E)) then
15512 Resolve_Aspect_Expression (Expr);
15516 when Pre_Post_Aspects =>
15519 when Aspect_Iterable =>
15520 if Nkind (Expr) = N_Aggregate then
15525 Assoc := First (Component_Associations (Expr));
15526 while Present (Assoc) loop
15527 if Nkind (Expression (Assoc)) in N_Has_Entity
15529 Find_Direct_Name (Expression (Assoc));
15537 -- The expression for Default_Value is a static expression
15538 -- of the type, but this expression does not freeze the
15539 -- type, so it can still appear in a representation clause
15540 -- before the actual freeze point.
15542 when Aspect_Default_Value =>
15543 Set_Must_Not_Freeze (Expr);
15544 Preanalyze_Spec_Expression (Expr, E);
15546 when Aspect_Priority =>
15548 Preanalyze_Spec_Expression (Expr, Any_Integer);
15551 -- Ditto for Storage_Size. Any other aspects that carry
15552 -- expressions that should not freeze ??? This is only
15553 -- relevant to the misuse of deferred constants.
15555 when Aspect_Storage_Size =>
15556 Set_Must_Not_Freeze (Expr);
15557 Preanalyze_Spec_Expression (Expr, Any_Integer);
15560 if Present (Expr) then
15561 case Aspect_Argument (A_Id) is
15563 | Optional_Expression
15565 Analyze_And_Resolve (Expr);
15570 if Nkind (Expr) = N_Identifier then
15571 Find_Direct_Name (Expr);
15573 elsif Nkind (Expr) = N_Selected_Component then
15574 Find_Selected_Component (Expr);
15582 Next_Rep_Item (ASN);
15584 end Resolve_Aspect_Expressions;
15586 ----------------------------
15587 -- Parse_Aspect_Aggregate --
15588 ----------------------------
15590 procedure Parse_Aspect_Aggregate
15592 Empty_Subp : in out Node_Id;
15593 Add_Named_Subp : in out Node_Id;
15594 Add_Unnamed_Subp : in out Node_Id;
15595 New_Indexed_Subp : in out Node_Id;
15596 Assign_Indexed_Subp : in out Node_Id)
15598 Assoc : Node_Id := First (Component_Associations (N));
15603 while Present (Assoc) loop
15604 Subp := Expression (Assoc);
15605 Op_Name := Chars (First (Choices (Assoc)));
15606 if Op_Name = Name_Empty then
15607 Empty_Subp := Subp;
15609 elsif Op_Name = Name_Add_Named then
15610 Add_Named_Subp := Subp;
15612 elsif Op_Name = Name_Add_Unnamed then
15613 Add_Unnamed_Subp := Subp;
15615 elsif Op_Name = Name_New_Indexed then
15616 New_Indexed_Subp := Subp;
15618 elsif Op_Name = Name_Assign_Indexed then
15619 Assign_Indexed_Subp := Subp;
15624 end Parse_Aspect_Aggregate;
15626 ------------------------------------
15627 -- Parse_Aspect_Stable_Properties --
15628 ------------------------------------
15630 function Parse_Aspect_Stable_Properties
15631 (Aspect_Spec : Node_Id; Negated : out Boolean) return Subprogram_List
15633 function Extract_Entity (Expr : Node_Id) return Entity_Id;
15634 -- Given an element of a Stable_Properties aspect spec, return the
15635 -- associated entity.
15636 -- This function updates the Negated flag as a side-effect.
15638 --------------------
15639 -- Extract_Entity --
15640 --------------------
15642 function Extract_Entity (Expr : Node_Id) return Entity_Id is
15645 if Nkind (Expr) = N_Op_Not then
15647 Name := Right_Opnd (Expr);
15652 if Nkind (Name) in N_Has_Entity then
15653 return Entity (Name);
15657 end Extract_Entity;
15664 -- Start of processing for Parse_Aspect_Stable_Properties
15669 if Nkind (Aspect_Spec) /= N_Aggregate then
15670 return (1 => Extract_Entity (Aspect_Spec));
15672 L := Expressions (Aspect_Spec);
15675 return Result : Subprogram_List (1 .. List_Length (L)) do
15676 for I in Result'Range loop
15677 Result (I) := Extract_Entity (Id);
15679 if No (Result (I)) then
15680 pragma Assert (Serious_Errors_Detected > 0);
15681 goto Ignore_Aspect;
15689 <<Ignore_Aspect>> return (1 .. 0 => <>);
15690 end Parse_Aspect_Stable_Properties;
15692 -------------------------------
15693 -- Validate_Aspect_Aggregate --
15694 -------------------------------
15696 procedure Validate_Aspect_Aggregate (N : Node_Id) is
15697 Empty_Subp : Node_Id := Empty;
15698 Add_Named_Subp : Node_Id := Empty;
15699 Add_Unnamed_Subp : Node_Id := Empty;
15700 New_Indexed_Subp : Node_Id := Empty;
15701 Assign_Indexed_Subp : Node_Id := Empty;
15704 Error_Msg_Ada_2022_Feature ("aspect Aggregate", Sloc (N));
15706 if Nkind (N) /= N_Aggregate
15707 or else Present (Expressions (N))
15708 or else No (Component_Associations (N))
15710 Error_Msg_N ("aspect Aggregate requires an aggregate "
15711 & "with component associations", N);
15715 Parse_Aspect_Aggregate (N,
15716 Empty_Subp, Add_Named_Subp, Add_Unnamed_Subp,
15717 New_Indexed_Subp, Assign_Indexed_Subp);
15719 if No (Empty_Subp) then
15720 Error_Msg_N ("missing specification for Empty in aggregate", N);
15723 if Present (Add_Named_Subp) then
15724 if Present (Add_Unnamed_Subp)
15725 or else Present (Assign_Indexed_Subp)
15728 ("conflicting operations for aggregate (RM 4.3.5)", N);
15732 elsif Present (New_Indexed_Subp) /= Present (Assign_Indexed_Subp) then
15733 Error_Msg_N ("incomplete specification for indexed aggregate", N);
15735 end Validate_Aspect_Aggregate;
15737 -------------------------------
15738 -- Validate_Aspect_Stable_Properties --
15739 -------------------------------
15741 procedure Validate_Aspect_Stable_Properties
15742 (E : Entity_Id; N : Node_Id; Class_Present : Boolean)
15744 Is_Aspect_Of_Type : constant Boolean := Is_Type (E);
15746 type Permission is (Forbidden, Optional, Required);
15747 Modifier_Permission : Permission :=
15748 (if Is_Aspect_Of_Type then Forbidden else Optional);
15749 Modifier_Error_Called : Boolean := False;
15751 procedure Check_Property_Function_Arg (PF_Arg : Node_Id);
15752 -- Check syntax of a property function argument
15754 ----------------------------------
15755 -- Check_Property_Function_Arg --
15756 ----------------------------------
15758 procedure Check_Property_Function_Arg (PF_Arg : Node_Id) is
15759 procedure Modifier_Error;
15760 -- Generate message about bad "not" modifier if no message already
15761 -- generated. Errors include specifying "not" for an aspect of
15762 -- of a type and specifying "not" for some but not all of the
15763 -- names in a list.
15765 --------------------
15766 -- Modifier_Error --
15767 --------------------
15769 procedure Modifier_Error is
15771 if Modifier_Error_Called then
15772 return; -- error message already generated
15775 Modifier_Error_Called := True;
15777 if Is_Aspect_Of_Type then
15779 ("NOT modifier not allowed for Stable_Properties aspect"
15780 & " of a type", PF_Arg);
15782 Error_Msg_N ("mixed use of NOT modifiers", PF_Arg);
15784 end Modifier_Error;
15786 PF_Name : Node_Id := PF_Arg;
15788 -- Start of processing for Check_Property_Function_Arg
15791 if Nkind (PF_Arg) = N_Op_Not then
15792 PF_Name := Right_Opnd (PF_Arg);
15794 case Modifier_Permission is
15798 Modifier_Permission := Required;
15803 case Modifier_Permission is
15807 Modifier_Permission := Forbidden;
15813 if Nkind (PF_Name) not in
15814 N_Identifier | N_Operator_Symbol | N_Selected_Component
15816 Error_Msg_N ("bad property function name", PF_Name);
15818 end Check_Property_Function_Arg;
15820 -- Start of processing for Validate_Aspect_Stable_Properties
15823 Error_Msg_Ada_2022_Feature ("aspect Stable_Properties", Sloc (N));
15825 if (not Is_Aspect_Of_Type) and then (not Is_Subprogram (E)) then
15826 Error_Msg_N ("Stable_Properties aspect can only be specified for "
15827 & "a type or a subprogram", N);
15828 elsif Class_Present then
15829 if Is_Aspect_Of_Type then
15830 if not Is_Tagged_Type (E) then
15832 ("Stable_Properties''Class aspect cannot be specified for "
15833 & "an untagged type", N);
15836 if not Is_Dispatching_Operation (E) then
15838 ("Stable_Properties''Class aspect cannot be specified for "
15839 & "a subprogram that is not a primitive subprogram "
15840 & "of a tagged type", N);
15845 if Nkind (N) = N_Aggregate then
15846 if Present (Component_Associations (N))
15847 or else Null_Record_Present (N)
15848 or else not Present (Expressions (N))
15850 Error_Msg_N ("bad Stable_Properties aspect specification", N);
15855 PF_Arg : Node_Id := First (Expressions (N));
15857 while Present (PF_Arg) loop
15858 Check_Property_Function_Arg (PF_Arg);
15863 Check_Property_Function_Arg (N);
15865 end Validate_Aspect_Stable_Properties;
15867 --------------------------------
15868 -- Resolve_Iterable_Operation --
15869 --------------------------------
15871 procedure Resolve_Iterable_Operation
15873 Cursor : Entity_Id;
15882 if not Is_Overloaded (N) then
15883 if not Is_Entity_Name (N)
15884 or else Ekind (Entity (N)) /= E_Function
15885 or else Scope (Entity (N)) /= Scope (Typ)
15886 or else No (First_Formal (Entity (N)))
15887 or else Etype (First_Formal (Entity (N))) /= Typ
15890 ("iterable primitive must be local function name whose first "
15891 & "formal is an iterable type", N);
15896 F1 := First_Formal (Ent);
15897 F2 := Next_Formal (F1);
15899 if Nam = Name_First then
15901 -- First (Container) => Cursor
15903 if Etype (Ent) /= Cursor then
15904 Error_Msg_N ("primitive for First must yield a cursor", N);
15905 elsif Present (F2) then
15906 Error_Msg_N ("no match for First iterable primitive", N);
15909 elsif Nam = Name_Last then
15911 -- Last (Container) => Cursor
15913 if Etype (Ent) /= Cursor then
15914 Error_Msg_N ("primitive for Last must yield a cursor", N);
15915 elsif Present (F2) then
15916 Error_Msg_N ("no match for Last iterable primitive", N);
15919 elsif Nam = Name_Next then
15921 -- Next (Container, Cursor) => Cursor
15924 or else Etype (F2) /= Cursor
15925 or else Etype (Ent) /= Cursor
15926 or else Present (Next_Formal (F2))
15928 Error_Msg_N ("no match for Next iterable primitive", N);
15931 elsif Nam = Name_Previous then
15933 -- Previous (Container, Cursor) => Cursor
15936 or else Etype (F2) /= Cursor
15937 or else Etype (Ent) /= Cursor
15938 or else Present (Next_Formal (F2))
15940 Error_Msg_N ("no match for Previous iterable primitive", N);
15943 elsif Nam = Name_Has_Element then
15945 -- Has_Element (Container, Cursor) => Boolean
15948 or else Etype (F2) /= Cursor
15949 or else Etype (Ent) /= Standard_Boolean
15950 or else Present (Next_Formal (F2))
15952 Error_Msg_N ("no match for Has_Element iterable primitive", N);
15955 elsif Nam = Name_Element then
15957 -- Element (Container, Cursor) => Element_Type;
15960 or else Etype (F2) /= Cursor
15961 or else Present (Next_Formal (F2))
15963 Error_Msg_N ("no match for Element iterable primitive", N);
15967 raise Program_Error;
15971 -- Overloaded case: find subprogram with proper signature. Caller
15972 -- will report error if no match is found.
15979 Get_First_Interp (N, I, It);
15980 while Present (It.Typ) loop
15981 if Ekind (It.Nam) = E_Function
15982 and then Scope (It.Nam) = Scope (Typ)
15983 and then Present (First_Formal (It.Nam))
15984 and then Etype (First_Formal (It.Nam)) = Typ
15986 F1 := First_Formal (It.Nam);
15988 if Nam = Name_First then
15989 if Etype (It.Nam) = Cursor
15990 and then No (Next_Formal (F1))
15992 Set_Entity (N, It.Nam);
15996 elsif Nam = Name_Next then
15997 F2 := Next_Formal (F1);
16000 and then No (Next_Formal (F2))
16001 and then Etype (F2) = Cursor
16002 and then Etype (It.Nam) = Cursor
16004 Set_Entity (N, It.Nam);
16008 elsif Nam = Name_Has_Element then
16009 F2 := Next_Formal (F1);
16012 and then No (Next_Formal (F2))
16013 and then Etype (F2) = Cursor
16014 and then Etype (It.Nam) = Standard_Boolean
16016 Set_Entity (N, It.Nam);
16017 F2 := Next_Formal (F1);
16021 elsif Nam = Name_Element then
16022 F2 := Next_Formal (F1);
16025 and then No (Next_Formal (F2))
16026 and then Etype (F2) = Cursor
16028 Set_Entity (N, It.Nam);
16034 Get_Next_Interp (I, It);
16038 end Resolve_Iterable_Operation;
16040 ------------------------------
16041 -- Resolve_Aspect_Aggregate --
16042 ------------------------------
16044 procedure Resolve_Aspect_Aggregate
16048 function Valid_Empty (E : Entity_Id) return Boolean;
16049 function Valid_Add_Named (E : Entity_Id) return Boolean;
16050 function Valid_Add_Unnamed (E : Entity_Id) return Boolean;
16051 function Valid_New_Indexed (E : Entity_Id) return Boolean;
16052 function Valid_Assign_Indexed (E : Entity_Id) return Boolean;
16053 -- Predicates that establish the legality of each possible operation in
16054 -- an Aggregate aspect.
16057 with function Pred (Id : Node_Id) return Boolean;
16058 procedure Resolve_Operation (Subp_Id : Node_Id);
16059 -- Common processing to resolve each aggregate operation.
16061 ------------------------
16062 -- Valid_Assign_Index --
16063 ------------------------
16065 function Valid_Assign_Indexed (E : Entity_Id) return Boolean is
16067 -- The profile must be the same as for Add_Named, with the added
16068 -- requirement that the key_type be a discrete type.
16070 if Valid_Add_Named (E) then
16071 return Is_Discrete_Type (Etype (Next_Formal (First_Formal (E))));
16075 end Valid_Assign_Indexed;
16081 function Valid_Empty (E : Entity_Id) return Boolean is
16083 if Etype (E) /= Typ or else Scope (E) /= Scope (Typ) then
16086 elsif Ekind (E) = E_Constant then
16089 elsif Ekind (E) = E_Function then
16090 return No (First_Formal (E))
16092 (Is_Integer_Type (Etype (First_Formal (E)))
16093 and then No (Next_Formal (First_Formal (E))));
16099 ---------------------
16100 -- Valid_Add_Named --
16101 ---------------------
16103 function Valid_Add_Named (E : Entity_Id) return Boolean is
16104 F2, F3 : Entity_Id;
16106 if Ekind (E) = E_Procedure
16107 and then Scope (E) = Scope (Typ)
16108 and then Number_Formals (E) = 3
16109 and then Etype (First_Formal (E)) = Typ
16110 and then Ekind (First_Formal (E)) = E_In_Out_Parameter
16112 F2 := Next_Formal (First_Formal (E));
16113 F3 := Next_Formal (F2);
16114 return Ekind (F2) = E_In_Parameter
16115 and then Ekind (F3) = E_In_Parameter
16116 and then not Is_Limited_Type (Etype (F2))
16117 and then not Is_Limited_Type (Etype (F3));
16121 end Valid_Add_Named;
16123 -----------------------
16124 -- Valid_Add_Unnamed --
16125 -----------------------
16127 function Valid_Add_Unnamed (E : Entity_Id) return Boolean is
16129 return Ekind (E) = E_Procedure
16130 and then Scope (E) = Scope (Typ)
16131 and then Number_Formals (E) = 2
16132 and then Etype (First_Formal (E)) = Typ
16133 and then Ekind (First_Formal (E)) = E_In_Out_Parameter
16135 not Is_Limited_Type (Etype (Next_Formal (First_Formal (E))));
16136 end Valid_Add_Unnamed;
16138 -----------------------
16139 -- Valid_Nmw_Indexed --
16140 -----------------------
16142 function Valid_New_Indexed (E : Entity_Id) return Boolean is
16144 return Ekind (E) = E_Function
16145 and then Scope (E) = Scope (Typ)
16146 and then Etype (E) = Typ
16147 and then Number_Formals (E) = 2
16148 and then Is_Discrete_Type (Etype (First_Formal (E)))
16149 and then Etype (First_Formal (E)) =
16150 Etype (Next_Formal (First_Formal (E)));
16151 end Valid_New_Indexed;
16153 -----------------------
16154 -- Resolve_Operation --
16155 -----------------------
16157 procedure Resolve_Operation (Subp_Id : Node_Id) is
16164 if not Is_Overloaded (Subp_Id) then
16165 Subp := Entity (Subp_Id);
16166 if not Pred (Subp) then
16168 ("improper aggregate operation for&", Subp_Id, Typ);
16172 Set_Entity (Subp_Id, Empty);
16173 Get_First_Interp (Subp_Id, I, It);
16174 while Present (It.Nam) loop
16175 if Pred (It.Nam) then
16176 Set_Is_Overloaded (Subp_Id, False);
16177 Set_Entity (Subp_Id, It.Nam);
16181 Get_Next_Interp (I, It);
16184 if No (Entity (Subp_Id)) then
16186 ("improper aggregate operation for&", Subp_Id, Typ);
16189 end Resolve_Operation;
16195 procedure Resolve_Empty is new Resolve_Operation (Valid_Empty);
16196 procedure Resolve_Unnamed is new Resolve_Operation (Valid_Add_Unnamed);
16197 procedure Resolve_Named is new Resolve_Operation (Valid_Add_Named);
16198 procedure Resolve_Indexed is new Resolve_Operation (Valid_New_Indexed);
16199 procedure Resolve_Assign_Indexed
16200 is new Resolve_Operation
16201 (Valid_Assign_Indexed);
16203 -- Start of processing for Resolve_Aspect_Aggregate
16206 Assoc := First (Component_Associations (Expr));
16208 while Present (Assoc) loop
16209 Op_Name := Chars (First (Choices (Assoc)));
16211 -- When verifying the consistency of aspects between the freeze point
16212 -- and the end of declarqtions, we use a copy which is not analyzed
16213 -- yet, so do it now.
16215 Subp_Id := Expression (Assoc);
16216 if No (Etype (Subp_Id)) then
16220 if Op_Name = Name_Empty then
16221 Resolve_Empty (Subp_Id);
16223 elsif Op_Name = Name_Add_Named then
16224 Resolve_Named (Subp_Id);
16226 elsif Op_Name = Name_Add_Unnamed then
16227 Resolve_Unnamed (Subp_Id);
16229 elsif Op_Name = Name_New_Indexed then
16230 Resolve_Indexed (Subp_Id);
16232 elsif Op_Name = Name_Assign_Indexed then
16233 Resolve_Assign_Indexed (Subp_Id);
16238 end Resolve_Aspect_Aggregate;
16240 --------------------------------------
16241 -- Resolve_Aspect_Stable_Properties --
16242 --------------------------------------
16244 procedure Resolve_Aspect_Stable_Properties
16245 (Typ_Or_Subp : Entity_Id; Expr : Node_Id; Class_Present : Boolean)
16247 Is_Aspect_Of_Type : constant Boolean := Is_Type (Typ_Or_Subp);
16249 Singleton : constant Boolean := Nkind (Expr) /= N_Aggregate;
16250 Subp_Name : Node_Id := (if Singleton
16252 else First (Expressions (Expr)));
16255 if Is_Aspect_Of_Type
16256 and then Has_Private_Declaration (Typ_Or_Subp)
16257 and then not Is_Private_Type (Typ_Or_Subp)
16260 ("Stable_Properties aspect cannot be specified " &
16261 "for the completion of a private type", Typ_Or_Subp);
16264 -- Analogous checks that the aspect is not specified for a completion
16265 -- in the subprogram case are not performed here because they are not
16266 -- specific to this particular aspect. Right ???
16269 Has_Not := Nkind (Subp_Name) = N_Op_Not;
16271 Set_Analyzed (Subp_Name); -- ???
16272 Subp_Name := Right_Opnd (Subp_Name);
16275 if No (Etype (Subp_Name)) then
16276 Analyze (Subp_Name);
16280 Subp : Entity_Id := Empty;
16285 function Is_Property_Function (E : Entity_Id) return Boolean;
16286 -- Implements RM 7.3.4 definition of "property function".
16288 function Is_Property_Function (E : Entity_Id) return Boolean is
16290 if Ekind (E) not in E_Function | E_Operator
16291 or else Number_Formals (E) /= 1
16297 Param_Type : constant Entity_Id :=
16298 Base_Type (Etype (First_Formal (E)));
16300 function Matches_Param_Type (Typ : Entity_Id)
16302 ((Base_Type (Typ) = Param_Type)
16304 (Is_Class_Wide_Type (Param_Type)
16305 and then Is_Ancestor (Root_Type (Param_Type),
16306 Base_Type (Typ))));
16308 if Is_Aspect_Of_Type then
16309 if Matches_Param_Type (Typ_Or_Subp) then
16312 elsif Is_Primitive (Typ_Or_Subp) then
16314 Formal : Entity_Id := First_Formal (Typ_Or_Subp);
16316 while Present (Formal) loop
16317 if Matches_Param_Type (Etype (Formal)) then
16319 -- Test whether Typ_Or_Subp (which is a subp
16320 -- in this case) is primitive op of the type
16321 -- of this parameter.
16322 if Scope (Typ_Or_Subp) = Scope (Param_Type) then
16326 Next_Formal (Formal);
16333 end Is_Property_Function;
16335 if not Is_Overloaded (Subp_Name) then
16336 Subp := Entity (Subp_Name);
16337 if not Is_Property_Function (Subp) then
16338 Error_Msg_NE ("improper property function for&",
16339 Subp_Name, Typ_Or_Subp);
16343 Set_Entity (Subp_Name, Empty);
16344 Get_First_Interp (Subp_Name, I, It);
16345 while Present (It.Nam) loop
16346 if Is_Property_Function (It.Nam) then
16347 if Present (Subp) then
16349 ("ambiguous property function name for&",
16350 Subp_Name, Typ_Or_Subp);
16355 Set_Is_Overloaded (Subp_Name, False);
16356 Set_Entity (Subp_Name, Subp);
16359 Get_Next_Interp (I, It);
16363 Error_Msg_NE ("improper property function for&",
16364 Subp_Name, Typ_Or_Subp);
16369 -- perform legality (as opposed to name resolution) Subp checks
16371 if Is_Limited_Type (Etype (Subp)) then
16373 ("result type of property function for& is limited",
16374 Subp_Name, Typ_Or_Subp);
16377 if Ekind (First_Formal (Subp)) /= E_In_Parameter then
16379 ("mode of parameter of property function for& is not IN",
16380 Subp_Name, Typ_Or_Subp);
16383 if Is_Class_Wide_Type (Etype (First_Formal (Subp))) then
16384 if not Covers (Etype (First_Formal (Subp)), Typ_Or_Subp) then
16386 ("class-wide parameter type of property function " &
16387 "for& does not cover the type",
16388 Subp_Name, Typ_Or_Subp);
16390 -- ??? This test is slightly stricter than 7.3.4(12/5);
16391 -- some legal corner cases may be incorrectly rejected.
16392 elsif Scope (Subp) /= Scope (Etype (First_Formal (Subp)))
16395 ("property function for& not declared in same scope " &
16396 "as parameter type",
16397 Subp_Name, Typ_Or_Subp);
16399 elsif Is_Aspect_Of_Type and then
16400 Scope (Subp) /= Scope (Typ_Or_Subp) and then
16401 Scope (Subp) /= Standard_Standard -- e.g., derived type's "abs"
16404 ("property function for& " &
16405 "not a primitive function of the type",
16406 Subp_Name, Typ_Or_Subp);
16410 -- check that Subp was mentioned in param type's aspect spec
16412 Param_Type : constant Entity_Id :=
16413 Base_Type (Etype (First_Formal (Subp)));
16414 Aspect_Spec : constant Node_Id :=
16415 Find_Value_Of_Aspect
16416 (Param_Type, Aspect_Stable_Properties,
16417 Class_Present => Class_Present);
16418 Found : Boolean := False;
16420 if Present (Aspect_Spec) then
16423 SPF_List : constant Subprogram_List :=
16424 Parse_Aspect_Stable_Properties
16425 (Aspect_Spec, Negated => Ignored);
16427 Found := (for some E of SPF_List => E = Subp);
16428 -- look through renamings ???
16433 CW_Modifier : constant String :=
16434 (if Class_Present then "class-wide " else "");
16438 & "property function for& mentioned after NOT "
16441 & "stable property function of its parameter type",
16442 Subp_Name, Typ_Or_Subp);
16449 exit when Singleton;
16451 Next ((if Has_Not then Parent (Subp_Name) else Subp_Name));
16452 exit when No (Subp_Name);
16455 Set_Analyzed (Expr);
16456 end Resolve_Aspect_Stable_Properties;
16458 -----------------------------------------
16459 -- Resolve_Storage_Model_Type_Argument --
16460 -----------------------------------------
16462 procedure Resolve_Storage_Model_Type_Argument
16465 Addr_Type : in out Entity_Id;
16469 type Formal_Profile is record
16471 Mode : Formal_Kind;
16474 type Formal_Profiles is array (Positive range <>) of Formal_Profile;
16476 function Aspect_Argument_Profile_Matches
16478 Profiles : Formal_Profiles;
16479 Result_Subt : Entity_Id;
16480 Err_On_Mismatch : Boolean) return Boolean;
16481 -- Checks that the formal parameters of subprogram Subp conform to the
16482 -- subtypes and modes specified by Profiles, as well as to the result
16483 -- subtype Result_Subt when that is nonempty.
16485 function Aspect_Argument_Profile_Matches
16487 Profiles : Formal_Profiles;
16488 Result_Subt : Entity_Id;
16489 Err_On_Mismatch : Boolean) return Boolean
16492 procedure Report_Argument_Error
16494 Formal : Entity_Id := Empty;
16495 Subt : Entity_Id := Empty);
16496 -- If Err_On_Mismatch is True, reports an argument error given by Msg
16497 -- associated with Formal and/or Subt.
16499 procedure Report_Argument_Error
16501 Formal : Entity_Id := Empty;
16502 Subt : Entity_Id := Empty)
16505 if Err_On_Mismatch then
16506 if Present (Formal) then
16507 if Present (Subt) then
16508 Error_Msg_Node_2 := Subt;
16510 Error_Msg_NE (Msg, N, Formal);
16512 elsif Present (Subt) then
16513 Error_Msg_NE (Msg, N, Subt);
16516 Error_Msg_N (Msg, N);
16519 end Report_Argument_Error;
16523 Formal : Entity_Id := First_Formal (Subp);
16524 Is_Error : Boolean := False;
16526 -- Start of processing for Aspect_Argument_Profile_Matches
16529 for FP of Profiles loop
16530 if No (Formal) then
16532 Report_Argument_Error ("missing formal of }", Subt => FP.Subt);
16535 elsif not Subtypes_Statically_Match
16536 (Etype (Formal), FP.Subt)
16539 Report_Argument_Error
16540 ("formal& must be of subtype&",
16541 Formal => Formal, Subt => FP.Subt);
16544 elsif Ekind (Formal) /= FP.Mode then
16546 Report_Argument_Error
16547 ("formal& has wrong mode", Formal => Formal);
16551 Formal := Next_Formal (Formal);
16555 and then Present (Formal)
16558 Report_Argument_Error
16559 ("too many formals for subprogram in aspect");
16563 and then Present (Result_Subt)
16564 and then not Subtypes_Statically_Match (Etype (Subp), Result_Subt)
16567 Report_Argument_Error
16568 ("subprogram must have result}", Subt => Result_Subt);
16571 return not Is_Error;
16572 end Aspect_Argument_Profile_Matches;
16578 Storage_Count_Type : constant Entity_Id := RTE (RE_Storage_Count);
16579 System_Address_Type : constant Entity_Id := RTE (RE_Address);
16581 -- Start of processing for Resolve_Storage_Model_Type_Argument
16584 if Nam = Name_Address_Type then
16585 if not Is_Entity_Name (N)
16586 or else not Is_Type (Entity (N))
16587 or else (Root_Type (Entity (N)) /= System_Address_Type
16588 and then not Is_Integer_Type (Entity (N)))
16590 Error_Msg_N ("named entity must be a descendant of System.Address "
16591 & "or an integer type", N);
16594 Addr_Type := Entity (N);
16598 -- If Addr_Type is not present as the first association, then we default
16599 -- it to System.Address.
16601 elsif No (Addr_Type) then
16602 Addr_Type := RTE (RE_Address);
16605 if Nam = Name_Null_Address then
16606 if not Is_Entity_Name (N)
16607 or else not Is_Constant_Object (Entity (N))
16609 not Subtypes_Statically_Match (Etype (Entity (N)), Addr_Type)
16612 ("named entity must be constant of subtype}", N, Addr_Type);
16617 elsif not Is_Overloaded (N) then
16618 if not Is_Entity_Name (N)
16619 or else Ekind (Entity (N)) not in E_Function | E_Procedure
16620 or else Scope (Entity (N)) /= Scope (Typ)
16622 Error_Msg_N ("argument must be local subprogram name", N);
16628 if Nam = Name_Allocate then
16629 if not Aspect_Argument_Profile_Matches
16632 ((Typ, E_In_Out_Parameter),
16633 (Addr_Type, E_Out_Parameter),
16634 (Storage_Count_Type, E_In_Parameter),
16635 (Storage_Count_Type, E_In_Parameter)),
16636 Result_Subt => Empty,
16637 Err_On_Mismatch => True)
16639 Error_Msg_N ("no match for Allocate operation", N);
16642 elsif Nam = Name_Deallocate then
16643 if not Aspect_Argument_Profile_Matches
16646 ((Typ, E_In_Out_Parameter),
16647 (Addr_Type, E_In_Parameter),
16648 (Storage_Count_Type, E_In_Parameter),
16649 (Storage_Count_Type, E_In_Parameter)),
16650 Result_Subt => Empty,
16651 Err_On_Mismatch => True)
16653 Error_Msg_N ("no match for Deallocate operation", N);
16656 elsif Nam = Name_Copy_From then
16657 if not Aspect_Argument_Profile_Matches
16660 ((Typ, E_In_Out_Parameter),
16661 (System_Address_Type, E_In_Parameter),
16662 (Addr_Type, E_In_Parameter),
16663 (Storage_Count_Type, E_In_Parameter)),
16664 Result_Subt => Empty,
16665 Err_On_Mismatch => True)
16667 Error_Msg_N ("no match for Copy_From operation", N);
16670 elsif Nam = Name_Copy_To then
16671 if not Aspect_Argument_Profile_Matches
16674 ((Typ, E_In_Out_Parameter),
16675 (Addr_Type, E_In_Parameter),
16676 (System_Address_Type, E_In_Parameter),
16677 (Storage_Count_Type, E_In_Parameter)),
16678 Result_Subt => Empty,
16679 Err_On_Mismatch => True)
16681 Error_Msg_N ("no match for Copy_To operation", N);
16684 elsif Nam = Name_Storage_Size then
16685 if not Aspect_Argument_Profile_Matches
16687 Profiles => (1 => (Typ, E_In_Parameter)),
16688 Result_Subt => Storage_Count_Type,
16689 Err_On_Mismatch => True)
16691 Error_Msg_N ("no match for Storage_Size operation", N);
16695 null; -- Error will be caught in Validate_Storage_Model_Type_Aspect
16699 -- Overloaded case: find subprogram with proper signature
16704 Found_Match : Boolean := False;
16707 Get_First_Interp (N, I, It);
16708 while Present (It.Typ) loop
16709 if Ekind (It.Nam) in E_Function | E_Procedure
16710 and then Scope (It.Nam) = Scope (Typ)
16712 if Nam = Name_Allocate then
16714 Aspect_Argument_Profile_Matches
16717 ((Typ, E_In_Out_Parameter),
16718 (Addr_Type, E_Out_Parameter),
16719 (Storage_Count_Type, E_In_Parameter),
16720 (Storage_Count_Type, E_In_Parameter)),
16721 Result_Subt => Empty,
16722 Err_On_Mismatch => False);
16724 elsif Nam = Name_Deallocate then
16726 Aspect_Argument_Profile_Matches
16729 ((Typ, E_In_Out_Parameter),
16730 (Addr_Type, E_In_Parameter),
16731 (Storage_Count_Type, E_In_Parameter),
16732 (Storage_Count_Type, E_In_Parameter)),
16733 Result_Subt => Empty,
16734 Err_On_Mismatch => False);
16736 elsif Nam = Name_Copy_From then
16738 Aspect_Argument_Profile_Matches
16741 ((Typ, E_In_Out_Parameter),
16742 (System_Address_Type, E_In_Parameter),
16743 (Addr_Type, E_In_Parameter),
16744 (Storage_Count_Type, E_In_Parameter),
16745 (Storage_Count_Type, E_In_Parameter)),
16746 Result_Subt => Empty,
16747 Err_On_Mismatch => False);
16749 elsif Nam = Name_Copy_To then
16751 Aspect_Argument_Profile_Matches
16754 ((Typ, E_In_Out_Parameter),
16755 (Addr_Type, E_In_Parameter),
16756 (Storage_Count_Type, E_In_Parameter),
16757 (System_Address_Type, E_In_Parameter),
16758 (Storage_Count_Type, E_In_Parameter)),
16759 Result_Subt => Empty,
16760 Err_On_Mismatch => False);
16762 elsif Nam = Name_Storage_Size then
16764 Aspect_Argument_Profile_Matches
16766 Profiles => (1 => (Typ, E_In_Parameter)),
16767 Result_Subt => Storage_Count_Type,
16768 Err_On_Mismatch => False);
16771 if Found_Match then
16772 Set_Entity (N, It.Nam);
16777 Get_Next_Interp (I, It);
16780 if not Found_Match then
16782 ("no match found for Storage_Model_Type operation", N);
16786 end Resolve_Storage_Model_Type_Argument;
16792 procedure Set_Biased
16796 Biased : Boolean := True)
16800 Set_Has_Biased_Representation (E);
16802 if Warn_On_Biased_Representation then
16804 ("?.b?" & Msg & " forces biased representation for&", N, E);
16809 --------------------
16810 -- Set_Enum_Esize --
16811 --------------------
16813 procedure Set_Enum_Esize (T : Entity_Id) is
16819 Reinit_Alignment (T);
16821 -- Find the minimum standard size (8,16,32,64,128) that fits
16823 Lo := Enumeration_Rep (Entity (Type_Low_Bound (T)));
16824 Hi := Enumeration_Rep (Entity (Type_High_Bound (T)));
16827 if Lo >= -Uint_2**7 and then Hi < Uint_2**7 then
16828 Sz := UI_From_Int (Standard_Character_Size);
16829 -- Might be > 8 on some targets
16831 elsif Lo >= -Uint_2**15 and then Hi < Uint_2**15 then
16834 elsif Lo >= -Uint_2**31 and then Hi < Uint_2**31 then
16837 elsif Lo >= -Uint_2**63 and then Hi < Uint_2**63 then
16840 else pragma Assert (Lo >= -Uint_2**127 and then Hi < Uint_2**127);
16845 if Hi < Uint_2**8 then
16846 Sz := UI_From_Int (Standard_Character_Size);
16848 elsif Hi < Uint_2**16 then
16851 elsif Hi < Uint_2**32 then
16854 elsif Hi < Uint_2**64 then
16857 else pragma Assert (Hi < Uint_2**128);
16862 -- That minimum is the proper size unless we have a foreign convention
16863 -- and the size required is 32 or less, in which case we bump the size
16864 -- up to 32. This is required for C and C++ and seems reasonable for
16865 -- all other foreign conventions.
16867 if Has_Foreign_Convention (T)
16868 and then Esize (T) < Standard_Integer_Size
16870 -- Don't do this if Short_Enums on target
16872 and then not Target_Short_Enums
16874 Set_Esize (T, UI_From_Int (Standard_Integer_Size));
16878 end Set_Enum_Esize;
16880 -----------------------------
16881 -- Uninstall_Discriminants --
16882 -----------------------------
16884 procedure Uninstall_Discriminants (E : Entity_Id) is
16890 -- Discriminants have been made visible for type declarations and
16891 -- protected type declarations, not for subtype declarations.
16893 if Nkind (Parent (E)) /= N_Subtype_Declaration then
16894 Disc := First_Discriminant (E);
16895 while Present (Disc) loop
16896 if Disc /= Current_Entity (Disc) then
16897 Prev := Current_Entity (Disc);
16898 while Present (Prev)
16899 and then Present (Homonym (Prev))
16900 and then Homonym (Prev) /= Disc
16902 Prev := Homonym (Prev);
16908 Set_Is_Immediately_Visible (Disc, False);
16910 Outer := Homonym (Disc);
16911 while Present (Outer) and then Scope (Outer) = E loop
16912 Outer := Homonym (Outer);
16915 -- Reset homonym link of other entities, but do not modify link
16916 -- between entities in current scope, so that the back end can
16917 -- have a proper count of local overloadings.
16920 Set_Name_Entity_Id (Chars (Disc), Outer);
16922 elsif Scope (Prev) /= Scope (Disc) then
16923 Set_Homonym (Prev, Outer);
16926 Next_Discriminant (Disc);
16929 end Uninstall_Discriminants;
16931 ------------------------------
16932 -- Validate_Address_Clauses --
16933 ------------------------------
16935 procedure Validate_Address_Clauses is
16936 function Offset_Value (Expr : Node_Id) return Uint;
16937 -- Given an Address attribute reference, return the value in bits of its
16938 -- offset from the first bit of the underlying entity, or 0 if it is not
16939 -- known at compile time.
16945 function Offset_Value (Expr : Node_Id) return Uint is
16946 N : Node_Id := Prefix (Expr);
16948 Val : Uint := Uint_0;
16951 -- Climb the prefix chain and compute the cumulative offset
16954 if Is_Entity_Name (N) then
16957 elsif Nkind (N) = N_Selected_Component then
16958 Off := Component_Bit_Offset (Entity (Selector_Name (N)));
16959 if Present (Off) and then Off >= Uint_0 then
16966 elsif Nkind (N) = N_Indexed_Component then
16967 Off := Indexed_Component_Bit_Offset (N);
16968 if Present (Off) then
16981 -- Start of processing for Validate_Address_Clauses
16984 for J in Address_Clause_Checks.First .. Address_Clause_Checks.Last loop
16986 ACCR : Address_Clause_Check_Record
16987 renames Address_Clause_Checks.Table (J);
16991 X_Alignment : Uint;
16992 Y_Alignment : Uint := Uint_0;
16995 Y_Size : Uint := Uint_0;
17000 -- Skip processing of this entry if warning already posted, or if
17001 -- alignments are not set.
17003 if not Address_Warning_Posted (ACCR.N)
17004 and then Known_Alignment (ACCR.X)
17005 and then Known_Alignment (ACCR.Y)
17007 Expr := Original_Node (Expression (ACCR.N));
17009 -- Get alignments, sizes and offset, if any
17011 X_Alignment := Alignment (ACCR.X);
17012 X_Size := Esize (ACCR.X);
17014 if Present (ACCR.Y) then
17015 Y_Alignment := Alignment (ACCR.Y);
17017 (if Known_Esize (ACCR.Y) then Esize (ACCR.Y) else Uint_0);
17021 and then Nkind (Expr) = N_Attribute_Reference
17022 and then Attribute_Name (Expr) = Name_Address
17024 X_Offs := Offset_Value (Expr);
17029 -- Check for known value not multiple of alignment
17031 if No (ACCR.Y) then
17032 if not Alignment_Checks_Suppressed (ACCR)
17033 and then X_Alignment /= 0
17034 and then ACCR.A mod X_Alignment /= 0
17037 ("??specified address for& is inconsistent with "
17038 & "alignment", ACCR.N, ACCR.X);
17040 ("\??program execution may be erroneous (RM 13.3(27))",
17043 Error_Msg_Uint_1 := X_Alignment;
17044 Error_Msg_NE ("\??alignment of & is ^", ACCR.N, ACCR.X);
17047 -- Check for large object overlaying smaller one
17049 elsif Y_Size > Uint_0
17050 and then X_Size > Uint_0
17051 and then X_Offs + X_Size > Y_Size
17053 Error_Msg_NE ("??& overlays smaller object", ACCR.N, ACCR.X);
17055 ("\??program execution may be erroneous", ACCR.N);
17057 Error_Msg_Uint_1 := X_Size;
17058 Error_Msg_NE ("\??size of & is ^", ACCR.N, ACCR.X);
17060 Error_Msg_Uint_1 := Y_Size;
17061 Error_Msg_NE ("\??size of & is ^", ACCR.N, ACCR.Y);
17063 if Y_Size >= X_Size then
17064 Error_Msg_Uint_1 := X_Offs;
17065 Error_Msg_NE ("\??but offset of & is ^", ACCR.N, ACCR.X);
17068 -- Check for inadequate alignment, both of the base object
17069 -- and of the offset, if any. We only do this check if the
17070 -- run-time Alignment_Check is active. No point in warning
17071 -- if this check has been suppressed (or is suppressed by
17072 -- default in the non-strict alignment machine case).
17074 -- Note: we do not check the alignment if we gave a size
17075 -- warning, since it would likely be redundant.
17077 elsif not Alignment_Checks_Suppressed (ACCR)
17078 and then Y_Alignment /= Uint_0
17080 (Y_Alignment < X_Alignment
17083 and then Nkind (Expr) = N_Attribute_Reference
17084 and then Attribute_Name (Expr) = Name_Address
17085 and then Has_Compatible_Alignment
17086 (ACCR.X, Prefix (Expr), True) /=
17090 ("??specified address for& may be inconsistent with "
17091 & "alignment", ACCR.N, ACCR.X);
17093 ("\??program execution may be erroneous (RM 13.3(27))",
17096 Error_Msg_Uint_1 := X_Alignment;
17097 Error_Msg_NE ("\??alignment of & is ^", ACCR.N, ACCR.X);
17099 Error_Msg_Uint_1 := Y_Alignment;
17100 Error_Msg_NE ("\??alignment of & is ^", ACCR.N, ACCR.Y);
17102 if Y_Alignment >= X_Alignment then
17104 ("\??but offset is not multiple of alignment", ACCR.N);
17110 end Validate_Address_Clauses;
17112 ------------------------------
17113 -- Validate_Iterable_Aspect --
17114 ------------------------------
17116 procedure Validate_Iterable_Aspect (Typ : Entity_Id; ASN : Node_Id) is
17117 Aggr : constant Node_Id := Expression (ASN);
17122 Cursor : Entity_Id;
17124 First_Id : Entity_Id := Empty;
17125 Last_Id : Entity_Id := Empty;
17126 Next_Id : Entity_Id := Empty;
17127 Has_Element_Id : Entity_Id := Empty;
17128 Element_Id : Entity_Id := Empty;
17131 if Nkind (Aggr) /= N_Aggregate then
17132 Error_Msg_N ("aspect Iterable must be an aggregate", Aggr);
17136 Cursor := Get_Cursor_Type (ASN, Typ);
17138 -- If previous error aspect is unusable
17140 if Cursor = Any_Type then
17144 if not Is_Empty_List (Expressions (Aggr)) then
17146 ("illegal positional association", First (Expressions (Aggr)));
17149 -- Each expression must resolve to a function with the proper signature
17151 Assoc := First (Component_Associations (Aggr));
17152 while Present (Assoc) loop
17153 Expr := Expression (Assoc);
17156 Prim := First (Choices (Assoc));
17158 if Nkind (Prim) /= N_Identifier or else Present (Next (Prim)) then
17159 Error_Msg_N ("illegal name in association", Prim);
17161 elsif Chars (Prim) = Name_First then
17162 Resolve_Iterable_Operation (Expr, Cursor, Typ, Name_First);
17163 First_Id := Entity (Expr);
17165 elsif Chars (Prim) = Name_Last then
17166 Resolve_Iterable_Operation (Expr, Cursor, Typ, Name_Last);
17167 Last_Id := Entity (Expr);
17169 elsif Chars (Prim) = Name_Previous then
17170 Resolve_Iterable_Operation (Expr, Cursor, Typ, Name_Previous);
17171 Last_Id := Entity (Expr);
17173 elsif Chars (Prim) = Name_Next then
17174 Resolve_Iterable_Operation (Expr, Cursor, Typ, Name_Next);
17175 Next_Id := Entity (Expr);
17177 elsif Chars (Prim) = Name_Has_Element then
17178 Resolve_Iterable_Operation (Expr, Cursor, Typ, Name_Has_Element);
17179 Has_Element_Id := Entity (Expr);
17181 elsif Chars (Prim) = Name_Element then
17182 Resolve_Iterable_Operation (Expr, Cursor, Typ, Name_Element);
17183 Element_Id := Entity (Expr);
17186 Error_Msg_N ("invalid name for iterable function", Prim);
17192 if No (First_Id) then
17193 Error_Msg_N ("match for First primitive not found", ASN);
17195 elsif No (Next_Id) then
17196 Error_Msg_N ("match for Next primitive not found", ASN);
17198 elsif No (Has_Element_Id) then
17199 Error_Msg_N ("match for Has_Element primitive not found", ASN);
17201 elsif No (Element_Id) or else No (Last_Id) then
17204 end Validate_Iterable_Aspect;
17206 ------------------------------
17207 -- Validate_Literal_Aspect --
17208 ------------------------------
17210 procedure Validate_Literal_Aspect (Typ : Entity_Id; ASN : Node_Id) is
17211 A_Id : constant Aspect_Id := Get_Aspect_Id (ASN);
17212 pragma Assert (A_Id in Aspect_Integer_Literal |
17213 Aspect_Real_Literal | Aspect_String_Literal);
17214 Func_Name : constant Node_Id := Expression (ASN);
17215 Overloaded : Boolean := Is_Overloaded (Func_Name);
17217 I : Interp_Index := 0;
17219 Param_Type : Entity_Id;
17220 Match_Found : Boolean := False;
17221 Match2_Found : Boolean := False;
17222 Is_Match : Boolean;
17224 Match2 : Entity_Id := Empty;
17227 (Param_Id : Entity_Id; Param_Type : Entity_Id) return Boolean;
17228 -- Return True if Param_Id is a non aliased in parameter whose base type
17236 (Param_Id : Entity_Id; Param_Type : Entity_Id) return Boolean is
17238 return Base_Type (Etype (Param_Id)) = Param_Type
17239 and then Ekind (Param_Id) = E_In_Parameter
17240 and then not Is_Aliased (Param_Id);
17244 if not Is_Type (Typ) then
17245 Error_Msg_N ("aspect can only be specified for a type", ASN);
17248 elsif not Is_First_Subtype (Typ) then
17249 Error_Msg_N ("aspect cannot be specified for a subtype", ASN);
17253 if A_Id = Aspect_String_Literal then
17254 if Is_String_Type (Typ) then
17255 Error_Msg_N ("aspect cannot be specified for a string type", ASN);
17259 Param_Type := Standard_Wide_Wide_String;
17262 if Is_Numeric_Type (Typ) then
17263 Error_Msg_N ("aspect cannot be specified for a numeric type", ASN);
17267 Param_Type := Standard_String;
17270 if not Overloaded and then No (Entity (Func_Name)) then
17271 -- The aspect is specified by a subprogram name, which
17272 -- may be an operator name given originally by a string.
17274 if Is_Operator_Name (Chars (Func_Name)) then
17275 Analyze_Operator_Symbol (Func_Name);
17277 Analyze (Func_Name);
17280 Overloaded := Is_Overloaded (Func_Name);
17284 Get_First_Interp (Func_Name, I => I, It => It);
17286 -- only one possible interpretation
17287 It.Nam := Entity (Func_Name);
17288 pragma Assert (Present (It.Nam));
17291 while It.Nam /= Empty loop
17294 if Ekind (It.Nam) = E_Function
17295 and then Base_Type (Etype (It.Nam)) = Base_Type (Typ)
17298 Params : constant List_Id :=
17299 Parameter_Specifications (Parent (It.Nam));
17300 Param_Spec : Node_Id;
17303 if List_Length (Params) = 1 then
17304 Param_Spec := First (Params);
17306 Matching (Defining_Identifier (Param_Spec), Param_Type);
17308 -- Look for the optional overloaded 2-param Real_Literal
17310 elsif List_Length (Params) = 2
17311 and then A_Id = Aspect_Real_Literal
17313 Param_Spec := First (Params);
17315 if Matching (Defining_Identifier (Param_Spec), Param_Type)
17317 Param_Spec := Next (Param_Spec);
17319 if Matching (Defining_Identifier (Param_Spec), Param_Type)
17321 if No (Match2) then
17323 Match2_Found := True;
17325 -- If we find more than one possible match then
17326 -- do not take any into account here: since the
17327 -- 2-parameter version of Real_Literal is optional
17328 -- we cannot generate an error here, so let
17329 -- standard resolution fail later if we do need to
17330 -- call this variant.
17332 Match2_Found := False;
17341 if Match_Found then
17342 Error_Msg_N ("aspect specification is ambiguous", ASN);
17346 Match_Found := True;
17350 exit when not Overloaded;
17352 if not Is_Match then
17353 Remove_Interp (I => I);
17356 Get_Next_Interp (I => I, It => It);
17359 if not Match_Found then
17361 ("function name in aspect specification cannot be resolved", ASN);
17365 Set_Entity (Func_Name, Match.Nam);
17366 Set_Etype (Func_Name, Etype (Match.Nam));
17367 Set_Is_Overloaded (Func_Name, False);
17369 -- Record the match for 2-parameter function if found
17371 if Match2_Found then
17372 Set_Related_Expression (Match.Nam, Match2);
17374 end Validate_Literal_Aspect;
17376 ----------------------------------------
17377 -- Validate_Storage_Model_Type_Aspect --
17378 ----------------------------------------
17380 procedure Validate_Storage_Model_Type_Aspect
17381 (Typ : Entity_Id; ASN : Node_Id)
17384 Choice : Entity_Id;
17385 Choice_Name : Name_Id;
17388 Address_Type_Id : Entity_Id := Empty;
17389 Null_Address_Id : Entity_Id := Empty;
17390 Allocate_Id : Entity_Id := Empty;
17391 Deallocate_Id : Entity_Id := Empty;
17392 Copy_From_Id : Entity_Id := Empty;
17393 Copy_To_Id : Entity_Id := Empty;
17394 Storage_Size_Id : Entity_Id := Empty;
17396 procedure Check_And_Resolve_Storage_Model_Type_Argument
17399 Argument_Id : in out Entity_Id;
17401 -- Checks that the subaspect for Nam has not already been specified for
17402 -- Typ's Storage_Model_Type aspect (i.e., checks Argument_Id = Empty),
17403 -- resolves Expr, and sets Argument_Id to the entity resolved for Expr.
17405 procedure Check_And_Resolve_Storage_Model_Type_Argument
17408 Argument_Id : in out Entity_Id;
17411 Name_String : String := Get_Name_String (Nam);
17414 To_Mixed (Name_String);
17416 if Present (Argument_Id) then
17417 Error_Msg_String (1 .. Name_String'Length) := Name_String;
17418 Error_Msg_Strlen := Name_String'Length;
17420 Error_Msg_N ("~ already specified", Expr);
17423 Resolve_Storage_Model_Type_Argument (Expr, Typ, Address_Type_Id, Nam);
17424 Argument_Id := Entity (Expr);
17425 end Check_And_Resolve_Storage_Model_Type_Argument;
17427 -- Start of processing for Validate_Storage_Model_Type_Aspect
17430 -- The aggregate argument of Storage_Model_Type is optional, and when
17431 -- not present the aspect defaults to the native storage model (where
17432 -- the address type is System.Address, and other arguments default to
17433 -- the corresponding native storage operations).
17435 if No (Expression (ASN)) then
17439 -- Each expression must resolve to an entity of the right kind or proper
17442 Assoc := First (Component_Associations (Expression (ASN)));
17443 while Present (Assoc) loop
17444 Expr := Expression (Assoc);
17447 Choice := First (Choices (Assoc));
17449 Choice_Name := Chars (Choice);
17451 if Nkind (Choice) /= N_Identifier or else Present (Next (Choice)) then
17452 Error_Msg_N ("illegal name in association", Choice);
17454 elsif Choice_Name = Name_Address_Type then
17455 if Assoc /= First (Component_Associations (Expression (ASN))) then
17456 Error_Msg_N ("Address_Type must be first association", Choice);
17459 Check_And_Resolve_Storage_Model_Type_Argument
17460 (Expr, Typ, Address_Type_Id, Name_Address_Type);
17463 -- It's allowed to leave out the Address_Type argument, in which
17464 -- case the address type is defined to default to System.Address.
17466 if No (Address_Type_Id) then
17467 Address_Type_Id := RTE (RE_Address);
17470 if Choice_Name = Name_Null_Address then
17471 Check_And_Resolve_Storage_Model_Type_Argument
17472 (Expr, Typ, Null_Address_Id, Name_Null_Address);
17474 elsif Choice_Name = Name_Allocate then
17475 Check_And_Resolve_Storage_Model_Type_Argument
17476 (Expr, Typ, Allocate_Id, Name_Allocate);
17478 elsif Choice_Name = Name_Deallocate then
17479 Check_And_Resolve_Storage_Model_Type_Argument
17480 (Expr, Typ, Deallocate_Id, Name_Deallocate);
17482 elsif Choice_Name = Name_Copy_From then
17483 Check_And_Resolve_Storage_Model_Type_Argument
17484 (Expr, Typ, Copy_From_Id, Name_Copy_From);
17486 elsif Choice_Name = Name_Copy_To then
17487 Check_And_Resolve_Storage_Model_Type_Argument
17488 (Expr, Typ, Copy_To_Id, Name_Copy_To);
17490 elsif Choice_Name = Name_Storage_Size then
17491 Check_And_Resolve_Storage_Model_Type_Argument
17492 (Expr, Typ, Storage_Size_Id, Name_Storage_Size);
17496 ("invalid name for Storage_Model_Type argument", Choice);
17503 -- If Address_Type has been specified as or defaults to System.Address,
17504 -- then other "subaspect" arguments can be specified, but are optional.
17505 -- Otherwise, all other arguments are required and an error is flagged
17506 -- about any that are missing.
17508 if Address_Type_Id = RTE (RE_Address) then
17511 elsif No (Null_Address_Id) then
17512 Error_Msg_N ("match for Null_Address primitive not found", ASN);
17514 elsif No (Allocate_Id) then
17515 Error_Msg_N ("match for Allocate primitive not found", ASN);
17517 elsif No (Deallocate_Id) then
17518 Error_Msg_N ("match for Deallocate primitive not found", ASN);
17520 elsif No (Copy_From_Id) then
17521 Error_Msg_N ("match for Copy_From primitive not found", ASN);
17523 elsif No (Copy_To_Id) then
17524 Error_Msg_N ("match for Copy_To primitive not found", ASN);
17526 elsif No (Storage_Size_Id) then
17527 Error_Msg_N ("match for Storage_Size primitive not found", ASN);
17529 end Validate_Storage_Model_Type_Aspect;
17531 -----------------------------------
17532 -- Validate_Unchecked_Conversion --
17533 -----------------------------------
17535 procedure Validate_Unchecked_Conversion
17537 Act_Unit : Entity_Id)
17539 Source : Entity_Id;
17540 Target : Entity_Id;
17542 procedure Warn_Nonportable (RE : RE_Id);
17543 -- Warn if either source or target of the conversion is a predefined
17544 -- private type, whose representation might differ between releases and
17545 -- targets of the compiler.
17547 ----------------------
17548 -- Warn_Nonportable --
17549 ----------------------
17551 procedure Warn_Nonportable (RE : RE_Id) is
17553 if Is_RTE (Source, RE) or else Is_RTE (Target, RE) then
17554 pragma Assert (Is_Private_Type (RTE (RE)));
17556 ("?z?representation of & values may change between "
17557 & "'G'N'A'T versions", N, RTE (RE));
17559 end Warn_Nonportable;
17565 -- Start of processing for Validate_Unchecked_Conversion
17568 -- Obtain source and target types. Note that we call Ancestor_Subtype
17569 -- here because the processing for generic instantiation always makes
17570 -- subtypes, and we want the original frozen actual types.
17572 Source := Ancestor_Subtype (Etype (First_Formal (Act_Unit)));
17573 Target := Ancestor_Subtype (Etype (Act_Unit));
17575 -- If either type is generic, the instantiation happens within a generic
17576 -- unit, and there is nothing to check. The proper check will happen
17577 -- when the enclosing generic is instantiated.
17579 if Is_Generic_Type (Source) or else Is_Generic_Type (Target) then
17583 -- Warn if one of the operands is a private type declared in
17584 -- Ada.Calendar or Ada.Real_Time. Do not emit a warning when compiling
17585 -- GNAT-related sources.
17587 if Warn_On_Unchecked_Conversion
17588 and then not In_Predefined_Unit (N)
17590 Warn_Nonportable (RO_CA_Time);
17591 Warn_Nonportable (RO_RT_Time);
17592 Warn_Nonportable (RE_Time_Span);
17595 -- If we are dealing with private types, then do the check on their
17596 -- fully declared counterparts if the full declarations have been
17597 -- encountered (they don't have to be visible, but they must exist).
17599 if Is_Private_Type (Source)
17600 and then Present (Underlying_Type (Source))
17602 Source := Underlying_Type (Source);
17605 if Is_Private_Type (Target)
17606 and then Present (Underlying_Type (Target))
17608 Target := Underlying_Type (Target);
17611 -- Source may be unconstrained array, but not target, except in relaxed
17614 if Is_Array_Type (Target)
17615 and then not Is_Constrained (Target)
17616 and then not Relaxed_RM_Semantics
17619 ("unchecked conversion to unconstrained array not allowed", N);
17623 -- Warn if conversion between two different convention pointers
17625 if Is_Access_Type (Target)
17626 and then Is_Access_Type (Source)
17627 and then Convention (Target) /= Convention (Source)
17628 and then Warn_On_Unchecked_Conversion
17630 -- Give warnings for subprogram pointers only on most targets
17632 if Is_Access_Subprogram_Type (Target)
17633 or else Is_Access_Subprogram_Type (Source)
17636 ("?z?conversion between pointers with different conventions!",
17641 -- Make entry in unchecked conversion table for later processing by
17642 -- Validate_Unchecked_Conversions, which will check sizes and alignments
17643 -- (using values set by the back end where possible). This is only done
17644 -- if the appropriate warning is active.
17646 if Warn_On_Unchecked_Conversion then
17647 Unchecked_Conversions.Append
17648 (New_Val => UC_Entry'(Eloc => Sloc (N),
17651 Act_Unit => Act_Unit));
17653 -- If both sizes are known statically now, then back-end annotation
17654 -- is not required to do a proper check but if either size is not
17655 -- known statically, then we need the annotation.
17657 if Known_Static_RM_Size (Source)
17659 Known_Static_RM_Size (Target)
17663 Back_Annotate_Rep_Info := True;
17667 -- If unchecked conversion to access type, and access type is declared
17668 -- in the same unit as the unchecked conversion, then set the flag
17669 -- No_Strict_Aliasing (no strict aliasing is implicit here)
17671 if Is_Access_Type (Target)
17672 and then In_Same_Source_Unit (Target, N)
17674 Set_No_Strict_Aliasing (Implementation_Base_Type (Target));
17677 -- If the unchecked conversion is between Address and an access
17678 -- subprogram type, show that we shouldn't use an internal
17679 -- representation for the access subprogram type.
17681 if Is_Access_Subprogram_Type (Target)
17682 and then Is_Descendant_Of_Address (Source)
17683 and then In_Same_Source_Unit (Target, N)
17685 Set_Can_Use_Internal_Rep (Target, False);
17686 elsif Is_Access_Subprogram_Type (Source)
17687 and then Is_Descendant_Of_Address (Target)
17688 and then In_Same_Source_Unit (Source, N)
17690 Set_Can_Use_Internal_Rep (Source, False);
17693 -- Generate N_Validate_Unchecked_Conversion node for back end in case
17694 -- the back end needs to perform special validation checks.
17696 -- Shouldn't this be in Exp_Ch13, since the check only gets done if we
17697 -- have full expansion and the back end is called ???
17700 Make_Validate_Unchecked_Conversion (Sloc (N));
17701 Set_Source_Type (Vnode, Source);
17702 Set_Target_Type (Vnode, Target);
17704 -- If the unchecked conversion node is in a list, just insert before it.
17705 -- If not we have some strange case, not worth bothering about.
17707 if Is_List_Member (N) then
17708 Insert_After (N, Vnode);
17710 end Validate_Unchecked_Conversion;
17712 ------------------------------------
17713 -- Validate_Unchecked_Conversions --
17714 ------------------------------------
17716 procedure Validate_Unchecked_Conversions is
17717 function Is_Null_Array (T : Entity_Id) return Boolean;
17718 -- We want to warn in the case of converting to a wrong-sized array of
17719 -- bytes, including the zero-size case. This returns True in that case,
17720 -- which is necessary because a size of 0 is used to indicate both an
17721 -- unknown size and a size of 0. It's OK for this to return True in
17722 -- other zero-size cases, but we don't go out of our way; for example,
17723 -- we don't bother with multidimensional arrays.
17725 function Is_Null_Array (T : Entity_Id) return Boolean is
17727 if Is_Array_Type (T) and then Is_Constrained (T) then
17729 Index : constant Node_Id := First_Index (T);
17730 R : Node_Id; -- N_Range
17732 case Nkind (Index) is
17735 when N_Subtype_Indication =>
17736 R := Range_Expression (Constraint (Index));
17737 when N_Identifier | N_Expanded_Name =>
17738 R := Scalar_Range (Entity (Index));
17740 raise Program_Error;
17743 return Is_Null_Range (Low_Bound (R), High_Bound (R));
17751 for N in Unchecked_Conversions.First .. Unchecked_Conversions.Last loop
17753 T : UC_Entry renames Unchecked_Conversions.Table (N);
17755 Act_Unit : constant Entity_Id := T.Act_Unit;
17756 Eloc : constant Source_Ptr := T.Eloc;
17757 Source : constant Entity_Id := T.Source;
17758 Target : constant Entity_Id := T.Target;
17764 -- Skip if function marked as warnings off
17766 if Has_Warnings_Off (Act_Unit)
17767 or else Serious_Errors_Detected > 0
17772 -- Don't do the check if warnings off for either type, note the
17773 -- deliberate use of OR here instead of OR ELSE to get the flag
17774 -- Warnings_Off_Used set for both types if appropriate.
17776 if Has_Warnings_Off (Source) or Has_Warnings_Off (Target) then
17780 if (Known_Static_RM_Size (Source)
17781 and then Known_Static_RM_Size (Target))
17782 or else Is_Null_Array (Target)
17784 -- This validation check, which warns if we have unequal sizes
17785 -- for unchecked conversion, and thus implementation dependent
17786 -- semantics, is one of the few occasions on which we use the
17787 -- official RM size instead of Esize. See description in Einfo
17788 -- "Handling of Type'Size Values" for details.
17790 Source_Siz := RM_Size (Source);
17791 Target_Siz := RM_Size (Target);
17793 if Present (Source_Siz) and then Present (Target_Siz)
17794 and then Source_Siz /= Target_Siz
17797 ("?z?types for unchecked conversion have different sizes!",
17800 if All_Errors_Mode then
17801 Error_Msg_Name_1 := Chars (Source);
17802 Error_Msg_Uint_1 := Source_Siz;
17803 Error_Msg_Name_2 := Chars (Target);
17804 Error_Msg_Uint_2 := Target_Siz;
17805 Error_Msg ("\size of % is ^, size of % is ^?z?", Eloc);
17807 Error_Msg_Uint_1 := UI_Abs (Source_Siz - Target_Siz);
17809 if Is_Discrete_Type (Source)
17811 Is_Discrete_Type (Target)
17813 if Source_Siz > Target_Siz then
17815 ("\?z?^ high order bits of source will "
17816 & "be ignored!", Eloc);
17818 elsif Is_Unsigned_Type (Source) then
17820 ("\?z?source will be extended with ^ high order "
17821 & "zero bits!", Eloc);
17825 ("\?z?source will be extended with ^ high order "
17826 & "sign bits!", Eloc);
17829 elsif Source_Siz < Target_Siz then
17830 if Is_Discrete_Type (Target) then
17831 if Bytes_Big_Endian then
17833 ("\?z?target value will include ^ undefined "
17834 & "low order bits!", Eloc, Act_Unit);
17837 ("\?z?target value will include ^ undefined "
17838 & "high order bits!", Eloc, Act_Unit);
17843 ("\?z?^ trailing bits of target value will be "
17844 & "undefined!", Eloc, Act_Unit);
17847 else pragma Assert (Source_Siz > Target_Siz);
17848 if Is_Discrete_Type (Source) then
17849 if Bytes_Big_Endian then
17851 ("\?z?^ low order bits of source will be "
17852 & "ignored!", Eloc, Act_Unit);
17855 ("\?z?^ high order bits of source will be "
17856 & "ignored!", Eloc, Act_Unit);
17861 ("\?z?^ trailing bits of source will be "
17862 & "ignored!", Eloc, Act_Unit);
17869 -- If both types are access types, we need to check the alignment.
17870 -- If the alignment of both is specified, we can do it here.
17872 if Serious_Errors_Detected = 0
17873 and then Is_Access_Type (Source)
17874 and then Is_Access_Type (Target)
17875 and then Target_Strict_Alignment
17876 and then Present (Designated_Type (Source))
17877 and then Present (Designated_Type (Target))
17880 D_Source : constant Entity_Id := Designated_Type (Source);
17881 D_Target : constant Entity_Id := Designated_Type (Target);
17884 if Known_Alignment (D_Source)
17886 Known_Alignment (D_Target)
17889 Source_Align : constant Uint := Alignment (D_Source);
17890 Target_Align : constant Uint := Alignment (D_Target);
17893 if Source_Align < Target_Align
17894 and then not Is_Tagged_Type (D_Source)
17896 -- Suppress warning if warnings suppressed on either
17897 -- type or either designated type. Note the use of
17898 -- OR here instead of OR ELSE. That is intentional,
17899 -- we would like to set flag Warnings_Off_Used in
17900 -- all types for which warnings are suppressed.
17902 and then not (Has_Warnings_Off (D_Source)
17904 Has_Warnings_Off (D_Target)
17906 Has_Warnings_Off (Source)
17908 Has_Warnings_Off (Target))
17910 Error_Msg_Uint_1 := Target_Align;
17911 Error_Msg_Uint_2 := Source_Align;
17912 Error_Msg_Node_1 := D_Target;
17913 Error_Msg_Node_2 := D_Source;
17915 ("?z?alignment of & (^) is stricter than "
17916 & "alignment of & (^)!", Eloc, Act_Unit);
17918 ("\?z?resulting access value may have invalid "
17919 & "alignment!", Eloc, Act_Unit);
17930 end Validate_Unchecked_Conversions;