1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2021, 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 Ada
.Characters
.Latin_1
; use Ada
.Characters
.Latin_1
;
28 with Aspects
; use Aspects
;
29 with Atree
; use Atree
;
30 with Casing
; use Casing
;
31 with Checks
; use Checks
;
32 with Debug
; use Debug
;
33 with Einfo
; use Einfo
;
34 with Einfo
.Entities
; use Einfo
.Entities
;
35 with Einfo
.Utils
; use Einfo
.Utils
;
36 with Elists
; use Elists
;
37 with Errout
; use Errout
;
39 with Exp_Dist
; use Exp_Dist
;
40 with Exp_Util
; use Exp_Util
;
41 with Expander
; use Expander
;
42 with Freeze
; use Freeze
;
43 with Gnatvsn
; use Gnatvsn
;
44 with Itypes
; use Itypes
;
46 with Lib
.Xref
; use Lib
.Xref
;
47 with Nlists
; use Nlists
;
48 with Nmake
; use Nmake
;
50 with Restrict
; use Restrict
;
51 with Rident
; use Rident
;
52 with Rtsfind
; use Rtsfind
;
55 with Sem_Aux
; use Sem_Aux
;
56 with Sem_Cat
; use Sem_Cat
;
57 with Sem_Ch6
; use Sem_Ch6
;
58 with Sem_Ch8
; use Sem_Ch8
;
59 with Sem_Ch10
; use Sem_Ch10
;
60 with Sem_Dim
; use Sem_Dim
;
61 with Sem_Dist
; use Sem_Dist
;
62 with Sem_Elab
; use Sem_Elab
;
63 with Sem_Elim
; use Sem_Elim
;
64 with Sem_Eval
; use Sem_Eval
;
65 with Sem_Prag
; use Sem_Prag
;
66 with Sem_Res
; use Sem_Res
;
67 with Sem_Type
; use Sem_Type
;
68 with Sem_Util
; use Sem_Util
;
70 with Stand
; use Stand
;
71 with Sinfo
; use Sinfo
;
72 with Sinfo
.Nodes
; use Sinfo
.Nodes
;
73 with Sinfo
.Utils
; use Sinfo
.Utils
;
74 with Sinput
; use Sinput
;
76 with Stringt
; use Stringt
;
78 with Stylesw
; use Stylesw
;
79 with Targparm
; use Targparm
;
80 with Ttypes
; use Ttypes
;
81 with Tbuild
; use Tbuild
;
82 with Uintp
; use Uintp
;
83 with Uname
; use Uname
;
84 with Urealp
; use Urealp
;
86 with System
.CRC32
; use System
.CRC32
;
88 package body Sem_Attr
is
90 True_Value
: constant Uint
:= Uint_1
;
91 False_Value
: constant Uint
:= Uint_0
;
92 -- Synonyms to be used when these constants are used as Boolean values
94 Bad_Attribute
: exception;
95 -- Exception raised if an error is detected during attribute processing,
96 -- used so that we can abandon the processing so we don't run into
97 -- trouble with cascaded errors.
99 -- The following array is the list of attributes defined in the Ada 83 RM.
100 -- In Ada 83 mode, these are the only recognized attributes. In other Ada
101 -- modes all these attributes are recognized, even if removed in Ada 95.
103 Attribute_83
: constant Attribute_Class_Array
:= Attribute_Class_Array
'(
106 Attribute_Alignment |
109 Attribute_Constrained |
116 Attribute_First_Bit |
122 Attribute_Leading_Part |
124 Attribute_Machine_Emax |
125 Attribute_Machine_Emin |
126 Attribute_Machine_Mantissa |
127 Attribute_Machine_Overflows |
128 Attribute_Machine_Radix |
129 Attribute_Machine_Rounds |
135 Attribute_Safe_Emax |
136 Attribute_Safe_Large |
137 Attribute_Safe_Small |
140 Attribute_Storage_Size |
142 Attribute_Terminated |
145 Attribute_Width => True,
148 -- The following array is the list of attributes defined in the Ada 2005
149 -- RM which are not defined in Ada 95. These are recognized in Ada 95 mode,
150 -- but in Ada 95 they are considered to be implementation defined.
152 Attribute_05 : constant Attribute_Class_Array := Attribute_Class_Array'(
153 Attribute_Machine_Rounding |
156 Attribute_Stream_Size |
157 Attribute_Wide_Wide_Width
=> True,
160 -- The following array is the list of attributes defined in the Ada 2012
161 -- RM which are not defined in Ada 2005. These are recognized in Ada 95
162 -- and Ada 2005 modes, but are considered to be implementation defined.
164 Attribute_12
: constant Attribute_Class_Array
:= Attribute_Class_Array
'(
165 Attribute_First_Valid |
166 Attribute_Has_Same_Storage |
167 Attribute_Last_Valid |
168 Attribute_Max_Alignment_For_Allocation => True,
171 -- The following array is the list of attributes defined in the Ada 2022
172 -- RM which are not defined in Ada 2012. These are recognized in Ada
173 -- 95/2005/2012 modes, but are considered to be implementation defined.
175 Attribute_22 : constant Attribute_Class_Array := Attribute_Class_Array'(
177 Attribute_Enum_Val
=> True,
178 Attribute_Preelaborable_Initialization
=> True,
181 -- The following array contains all attributes that imply a modification
182 -- of their prefixes or result in an access value. Such prefixes can be
183 -- considered as lvalues.
185 Attribute_Name_Implies_Lvalue_Prefix
: constant Attribute_Class_Array
:=
186 Attribute_Class_Array
'(
191 Attribute_Unchecked_Access |
192 Attribute_Unrestricted_Access => True,
195 -----------------------
196 -- Local_Subprograms --
197 -----------------------
199 procedure Eval_Attribute (N : Node_Id);
200 -- Performs compile time evaluation of attributes where possible, leaving
201 -- the Is_Static_Expression/Raises_Constraint_Error flags appropriately
202 -- set, and replacing the node with a literal node if the value can be
203 -- computed at compile time. All static attribute references are folded,
204 -- as well as a number of cases of non-static attributes that can always
205 -- be computed at compile time (e.g. floating-point model attributes that
206 -- are applied to non-static subtypes). Of course in such cases, the
207 -- Is_Static_Expression flag will not be set on the resulting literal.
208 -- Note that the only required action of this procedure is to catch the
209 -- static expression cases as described in the RM. Folding of other cases
210 -- is done where convenient, but some additional non-static folding is in
211 -- Expand_N_Attribute_Reference in cases where this is more convenient.
213 function Is_Anonymous_Tagged_Base
215 Typ : Entity_Id) return Boolean;
216 -- For derived tagged types that constrain parent discriminants we build
217 -- an anonymous unconstrained base type. We need to recognize the relation
218 -- between the two when analyzing an access attribute for a constrained
219 -- component, before the full declaration for Typ has been analyzed, and
220 -- where therefore the prefix of the attribute does not match the enclosing
223 procedure Set_Boolean_Result (N : Node_Id; B : Boolean);
224 -- Rewrites node N with an occurrence of either Standard_False or
225 -- Standard_True, depending on the value of the parameter B. The
226 -- result is marked as a static expression.
228 -----------------------
229 -- Analyze_Attribute --
230 -----------------------
232 procedure Analyze_Attribute (N : Node_Id) is
233 Loc : constant Source_Ptr := Sloc (N);
234 Aname : constant Name_Id := Attribute_Name (N);
235 Exprs : constant List_Id := Expressions (N);
236 Attr_Id : constant Attribute_Id := Get_Attribute_Id (Aname);
237 P_Old : constant Node_Id := Prefix (N);
239 P : Node_Id := P_Old;
243 P_Type : Entity_Id := Empty;
244 -- Type of prefix after analysis
246 P_Base_Type : Entity_Id := Empty;
247 -- Base type of prefix after analysis
249 -----------------------
250 -- Local Subprograms --
251 -----------------------
253 procedure Address_Checks;
254 -- Semantic checks for valid use of Address attribute. This was made
255 -- a separate routine with the idea of using it for unrestricted access
256 -- which seems like it should follow the same rules, but that turned
257 -- out to be impractical. So now this is only used for Address.
259 procedure Analyze_Access_Attribute;
260 -- Used for Access, Unchecked_Access, Unrestricted_Access attributes.
261 -- Internally, Id distinguishes which of the three cases is involved.
263 procedure Analyze_Attribute_Old_Result
264 (Legal : out Boolean;
265 Spec_Id : out Entity_Id);
266 -- Common processing for attributes 'Old
and 'Result. The routine checks
267 -- that the attribute appears in a postcondition-like aspect or pragma
268 -- associated with a suitable subprogram or a body. Flag Legal is set
269 -- when the above criteria are met. Spec_Id denotes the entity of the
270 -- subprogram [body] or Empty if the attribute is illegal.
272 procedure Analyze_Image_Attribute (Str_Typ : Entity_Id);
273 -- Common processing for attributes 'Img
, 'Image, 'Wide_Image
, and
274 -- 'Wide_Wide_Image. The routine checks that the prefix is valid and
275 -- sets the type of the attribute to the one specified by Str_Typ (e.g.
276 -- Standard_String for 'Image and Standard_Wide_String for 'Wide_Image).
278 procedure Bad_Attribute_For_Predicate
;
279 -- Output error message for use of a predicate (First, Last, Range) not
280 -- allowed with a type that has predicates. If the type is a generic
281 -- actual, then the message is a warning, and we generate code to raise
282 -- program error with an appropriate reason. No error message is given
283 -- for internally generated uses of the attributes. This legality rule
284 -- only applies to scalar types.
286 procedure Check_Array_Or_Scalar_Type
;
287 -- Common procedure used by First, Last, Range attribute to check
288 -- that the prefix is a constrained array or scalar type, or a name
289 -- of an array object, and that an argument appears only if appropriate
290 -- (i.e. only in the array case).
292 procedure Check_Array_Type
;
293 -- Common semantic checks for all array attributes. Checks that the
294 -- prefix is a constrained array type or the name of an array object.
295 -- The error message for non-arrays is specialized appropriately.
297 procedure Check_Asm_Attribute
;
298 -- Common semantic checks for Asm_Input and Asm_Output attributes
300 procedure Check_Component
;
301 -- Common processing for Bit_Position, First_Bit, Last_Bit, and
302 -- Position. Checks prefix is an appropriate selected component.
304 procedure Check_Decimal_Fixed_Point_Type
;
305 -- Check that prefix of attribute N is a decimal fixed-point type
307 procedure Check_Dereference
;
308 -- If the prefix of attribute is an object of an access type, then
309 -- introduce an explicit dereference, and adjust P_Type accordingly.
311 procedure Check_Discrete_Type
;
312 -- Verify that prefix of attribute N is a discrete type
315 -- Check that no attribute arguments are present
317 procedure Check_Either_E0_Or_E1
;
318 -- Check that there are zero or one attribute arguments present
321 -- Check that exactly one attribute argument is present
324 -- Check that two attribute arguments are present
326 procedure Check_Enum_Image
(Check_Enumeration_Maps
: Boolean := False);
327 -- Common processing for the Image and Value family of attributes,
328 -- including their Wide and Wide_Wide versions, Enum_Val, Img,
331 -- If the prefix type of an attribute is an enumeration type, set all
332 -- its literals as referenced, since the attribute function can
333 -- indirectly reference any of the literals. Set the referenced flag
334 -- only if the attribute is in the main code unit; otherwise an
335 -- improperly set reference when analyzing an inlined body will lose a
336 -- proper warning on a useless with_clause.
338 -- If Check_Enumeration_Maps is True, then the attribute expansion
339 -- requires enumeration maps, so check whether restriction
340 -- No_Enumeration_Maps is active.
342 procedure Check_First_Last_Valid
;
343 -- Perform all checks for First_Valid and Last_Valid attributes
345 procedure Check_Fixed_Point_Type
;
346 -- Verify that prefix of attribute N is a fixed type
348 procedure Check_Fixed_Point_Type_0
;
349 -- Verify that prefix of attribute N is a fixed type and that
350 -- no attribute expressions are present.
352 procedure Check_Floating_Point_Type
;
353 -- Verify that prefix of attribute N is a float type
355 procedure Check_Floating_Point_Type_0
;
356 -- Verify that prefix of attribute N is a float type and that
357 -- no attribute expressions are present.
359 procedure Check_Floating_Point_Type_1
;
360 -- Verify that prefix of attribute N is a float type and that
361 -- exactly one attribute expression is present.
363 procedure Check_Floating_Point_Type_2
;
364 -- Verify that prefix of attribute N is a float type and that
365 -- two attribute expressions are present.
367 procedure Check_Integer_Type
;
368 -- Verify that prefix of attribute N is an integer type
370 procedure Check_Modular_Integer_Type
;
371 -- Verify that prefix of attribute N is a modular integer type
373 procedure Check_Not_CPP_Type
;
374 -- Check that P (the prefix of the attribute) is not an CPP type
375 -- for which no Ada predefined primitive is available.
377 procedure Check_Not_Incomplete_Type
;
378 -- Check that P (the prefix of the attribute) is not an incomplete
379 -- type or a private type for which no full view has been given.
381 procedure Check_Object_Reference
(P
: Node_Id
);
382 -- Check that P is an object reference
384 procedure Check_PolyORB_Attribute
;
385 -- Validity checking for PolyORB/DSA attribute
387 procedure Check_Program_Unit
;
388 -- Verify that prefix of attribute N is a program unit
390 procedure Check_Real_Type
;
391 -- Verify that prefix of attribute N is fixed or float type
393 procedure Check_Enumeration_Type
;
394 -- Verify that prefix of attribute N is an enumeration type
396 procedure Check_Scalar_Type
;
397 -- Verify that prefix of attribute N is a scalar type
399 procedure Check_Standard_Prefix
;
400 -- Verify that prefix of attribute N is package Standard. Also checks
401 -- that there are no arguments.
403 procedure Check_Stream_Attribute
(Nam
: TSS_Name_Type
);
404 -- Validity checking for stream attribute. Nam is the TSS name of the
405 -- corresponding possible defined attribute function (e.g. for the
406 -- Read attribute, Nam will be TSS_Stream_Read).
408 procedure Check_Put_Image_Attribute
;
409 -- Validity checking for Put_Image attribute
411 procedure Check_System_Prefix
;
412 -- Verify that prefix of attribute N is package System
414 procedure Check_Task_Prefix
;
415 -- Verify that prefix of attribute N is a task or task type
417 procedure Check_Type
;
418 -- Verify that the prefix of attribute N is a type
420 procedure Check_Unit_Name
(Nod
: Node_Id
);
421 -- Check that Nod is of the form of a library unit name, i.e that
422 -- it is an identifier, or a selected component whose prefix is
423 -- itself of the form of a library unit name. Note that this is
424 -- quite different from Check_Program_Unit, since it only checks
425 -- the syntactic form of the name, not the semantic identity. This
426 -- is because it is used with attributes (Elab_Body, Elab_Spec and
427 -- Elaborated) which can refer to non-visible unit.
429 procedure Error_Attr
(Msg
: String; Error_Node
: Node_Id
);
430 pragma No_Return
(Error_Attr
);
431 procedure Error_Attr
;
432 pragma No_Return
(Error_Attr
);
433 -- Posts error using Error_Msg_N at given node, sets type of attribute
434 -- node to Any_Type, and then raises Bad_Attribute to avoid any further
435 -- semantic processing. The message typically contains a % insertion
436 -- character which is replaced by the attribute name. The call with
437 -- no arguments is used when the caller has already generated the
438 -- required error messages.
440 procedure Error_Attr_P
(Msg
: String; Msg_Cont
: String := "");
441 pragma No_Return
(Error_Attr_P
);
442 -- Like Error_Attr, but error is posted at the start of the prefix. The
443 -- second message Msg_Cont is useful to issue a continuation message
444 -- before raising Bad_Attribute.
446 procedure Legal_Formal_Attribute
;
447 -- Common processing for attributes Definite and Has_Discriminants.
448 -- Checks that prefix is generic indefinite formal type.
450 procedure Max_Alignment_For_Allocation_Max_Size_In_Storage_Elements
;
451 -- Common processing for attributes Max_Alignment_For_Allocation and
452 -- Max_Size_In_Storage_Elements.
455 -- Common processing for attributes Max and Min
457 procedure Standard_Attribute
(Val
: Int
);
458 -- Used to process attributes whose prefix is package Standard which
459 -- yield values of type Universal_Integer. The attribute reference
460 -- node is rewritten with an integer literal of the given value which
461 -- is marked as static.
463 procedure Uneval_Old_Msg
;
464 -- Called when Loop_Entry or Old is used in a potentially unevaluated
465 -- expression. Generates appropriate message or warning depending on
466 -- the setting of Opt.Uneval_Old (or flags in an N_Aspect_Specification
467 -- node in the aspect case).
469 procedure Unexpected_Argument
(En
: Node_Id
);
470 pragma No_Return
(Unexpected_Argument
);
471 -- Signal unexpected attribute argument (En is the argument), and then
472 -- raises Bad_Attribute to avoid any further semantic processing.
474 procedure Validate_Non_Static_Attribute_Function_Call
;
475 -- Called when processing an attribute that is a function call to a
476 -- non-static function, i.e. an attribute function that either takes
477 -- non-scalar arguments or returns a non-scalar result. Verifies that
478 -- such a call does not appear in a preelaborable context.
484 procedure Address_Checks
is
486 -- An Address attribute created by expansion is legal even when it
487 -- applies to other entity-denoting expressions.
489 if not Comes_From_Source
(N
) then
492 -- Address attribute on a protected object self reference is legal
494 elsif Is_Protected_Self_Reference
(P
) then
497 -- Address applied to an entity
499 elsif Is_Entity_Name
(P
) then
501 Ent
: constant Entity_Id
:= Entity
(P
);
504 if Is_Subprogram
(Ent
) then
505 Set_Address_Taken
(Ent
);
506 Kill_Current_Values
(Ent
);
508 -- An Address attribute is accepted when generated by the
509 -- compiler for dispatching operation, and an error is
510 -- issued once the subprogram is frozen (to avoid confusing
511 -- errors about implicit uses of Address in the dispatch
512 -- table initialization).
514 if Has_Pragma_Inline_Always
(Entity
(P
))
515 and then Comes_From_Source
(P
)
518 ("prefix of % attribute cannot be Inline_Always "
521 -- It is illegal to apply 'Address to an intrinsic
522 -- subprogram. This is now formalized in AI05-0095.
523 -- In an instance, an attempt to obtain 'Address of an
524 -- intrinsic subprogram (e.g the renaming of a predefined
525 -- operator that is an actual) raises Program_Error.
527 elsif Convention
(Ent
) = Convention_Intrinsic
then
530 Make_Raise_Program_Error
(Loc
,
531 Reason
=> PE_Address_Of_Intrinsic
));
534 Error_Msg_Name_1
:= Aname
;
536 ("cannot take % of intrinsic subprogram", N
);
539 -- Issue an error if prefix denotes an eliminated subprogram
542 Check_For_Eliminated_Subprogram
(P
, Ent
);
545 -- Object or label reference
547 elsif Is_Object_Reference
(P
) or else Ekind
(Ent
) = E_Label
then
548 Set_Address_Taken
(Ent
);
550 -- Deal with No_Implicit_Aliasing restriction
552 if Restriction_Check_Required
(No_Implicit_Aliasing
) then
553 if not Is_Aliased_View
(P
) then
554 Check_Restriction
(No_Implicit_Aliasing
, P
);
556 Check_No_Implicit_Aliasing
(P
);
560 -- If we have an address of an object, and the attribute
561 -- comes from source, then set the object as potentially
562 -- source modified. We do this because the resulting address
563 -- can potentially be used to modify the variable and we
564 -- might not detect this, leading to some junk warnings.
566 Set_Never_Set_In_Source
(Ent
, False);
568 -- Allow Address to be applied to task or protected type,
569 -- returning null address (what is that about???)
571 elsif (Is_Concurrent_Type
(Etype
(Ent
))
572 and then Etype
(Ent
) = Base_Type
(Ent
))
573 or else Ekind
(Ent
) = E_Package
574 or else Is_Generic_Unit
(Ent
)
577 New_Occurrence_Of
(RTE
(RE_Null_Address
), Sloc
(N
)));
579 -- Anything else is illegal
582 Error_Attr
("invalid prefix for % attribute", P
);
588 elsif Is_Object_Reference
(P
) then
591 -- Subprogram called using dot notation
593 elsif Nkind
(P
) = N_Selected_Component
594 and then Is_Subprogram
(Entity
(Selector_Name
(P
)))
598 -- What exactly are we allowing here ??? and is this properly
599 -- documented in the sinfo documentation for this node ???
601 elsif Relaxed_RM_Semantics
602 and then Nkind
(P
) = N_Attribute_Reference
606 -- All other non-entity name cases are illegal
609 Error_Attr
("invalid prefix for % attribute", P
);
613 ------------------------------
614 -- Analyze_Access_Attribute --
615 ------------------------------
617 procedure Analyze_Access_Attribute
is
618 Acc_Type
: Entity_Id
;
623 function Build_Access_Object_Type
(DT
: Entity_Id
) return Entity_Id
;
624 -- Build an access-to-object type whose designated type is DT,
625 -- and whose Ekind is appropriate to the attribute type. The
626 -- type that is constructed is returned as the result.
628 procedure Build_Access_Subprogram_Type
(P
: Node_Id
);
629 -- Build an access to subprogram whose designated type is the type of
630 -- the prefix. If prefix is overloaded, so is the node itself. The
631 -- result is stored in Acc_Type.
633 function OK_Self_Reference
return Boolean;
634 -- An access reference whose prefix is a type can legally appear
635 -- within an aggregate, where it is obtained by expansion of
636 -- a defaulted aggregate. The enclosing aggregate that contains
637 -- the self-referenced is flagged so that the self-reference can
638 -- be expanded into a reference to the target object (see exp_aggr).
640 ------------------------------
641 -- Build_Access_Object_Type --
642 ------------------------------
644 function Build_Access_Object_Type
(DT
: Entity_Id
) return Entity_Id
is
645 Typ
: constant Entity_Id
:=
647 (E_Access_Attribute_Type
, Current_Scope
, Loc
, 'A');
649 Set_Etype
(Typ
, Typ
);
651 Set_Associated_Node_For_Itype
(Typ
, N
);
652 Set_Directly_Designated_Type
(Typ
, DT
);
654 end Build_Access_Object_Type
;
656 ----------------------------------
657 -- Build_Access_Subprogram_Type --
658 ----------------------------------
660 procedure Build_Access_Subprogram_Type
(P
: Node_Id
) is
661 Index
: Interp_Index
;
664 procedure Check_Local_Access
(E
: Entity_Id
);
665 -- Deal with possible access to local subprogram. If we have such
666 -- an access, we set a flag to kill all tracked values on any call
667 -- because this access value may be passed around, and any called
668 -- code might use it to access a local procedure which clobbers a
669 -- tracked value. If the scope is a loop or block, indicate that
670 -- value tracking is disabled for the enclosing subprogram.
672 function Get_Convention
(E
: Entity_Id
) return Convention_Id
;
673 function Get_Kind
(E
: Entity_Id
) return Entity_Kind
;
674 -- Distinguish between access to regular/protected subprograms
676 ------------------------
677 -- Check_Local_Access --
678 ------------------------
680 procedure Check_Local_Access
(E
: Entity_Id
) is
682 if not Is_Library_Level_Entity
(E
) then
683 Set_Suppress_Value_Tracking_On_Call
(Current_Scope
);
684 Set_Suppress_Value_Tracking_On_Call
685 (Nearest_Dynamic_Scope
(Current_Scope
));
687 end Check_Local_Access
;
693 function Get_Convention
(E
: Entity_Id
) return Convention_Id
is
695 -- Restrict handling by_protected_procedure access subprograms
696 -- to source entities; required to avoid building access to
697 -- subprogram types with convention protected when building
700 if Comes_From_Source
(P
)
701 and then Is_By_Protected_Procedure
(E
)
703 return Convention_Protected
;
705 return Convention
(E
);
713 function Get_Kind
(E
: Entity_Id
) return Entity_Kind
is
715 if Get_Convention
(E
) = Convention_Protected
then
716 return E_Access_Protected_Subprogram_Type
;
718 return E_Access_Subprogram_Type
;
722 -- Start of processing for Build_Access_Subprogram_Type
725 -- In the case of an access to subprogram, use the name of the
726 -- subprogram itself as the designated type. Type-checking in
727 -- this case compares the signatures of the designated types.
729 -- Note: This fragment of the tree is temporarily malformed
730 -- because the correct tree requires an E_Subprogram_Type entity
731 -- as the designated type. In most cases this designated type is
732 -- later overridden by the semantics with the type imposed by the
733 -- context during the resolution phase. In the specific case of
734 -- the expression Address!(Prim'Unrestricted_Access), used to
735 -- initialize slots of dispatch tables, this work will be done by
736 -- the expander (see Exp_Aggr).
738 -- The reason to temporarily add this kind of node to the tree
739 -- instead of a proper E_Subprogram_Type itype, is the following:
740 -- in case of errors found in the source file we report better
741 -- error messages. For example, instead of generating the
744 -- "expected access to subprogram with profile
745 -- defined at line X"
747 -- we currently generate:
749 -- "expected access to function Z defined at line X"
751 Set_Etype
(N
, Any_Type
);
753 if not Is_Overloaded
(P
) then
754 Check_Local_Access
(Entity
(P
));
756 if not Is_Intrinsic_Subprogram
(Entity
(P
)) then
757 Acc_Type
:= Create_Itype
(Get_Kind
(Entity
(P
)), N
);
758 Set_Is_Public
(Acc_Type
, False);
759 Set_Etype
(Acc_Type
, Acc_Type
);
760 Set_Convention
(Acc_Type
, Get_Convention
(Entity
(P
)));
761 Set_Directly_Designated_Type
(Acc_Type
, Entity
(P
));
762 Set_Etype
(N
, Acc_Type
);
763 Freeze_Before
(N
, Acc_Type
);
767 Get_First_Interp
(P
, Index
, It
);
768 while Present
(It
.Nam
) loop
769 Check_Local_Access
(It
.Nam
);
771 if not Is_Intrinsic_Subprogram
(It
.Nam
) then
772 Acc_Type
:= Create_Itype
(Get_Kind
(It
.Nam
), N
);
773 Set_Is_Public
(Acc_Type
, False);
774 Set_Etype
(Acc_Type
, Acc_Type
);
775 Set_Convention
(Acc_Type
, Get_Convention
(It
.Nam
));
776 Set_Directly_Designated_Type
(Acc_Type
, It
.Nam
);
777 Add_One_Interp
(N
, Acc_Type
, Acc_Type
);
778 Freeze_Before
(N
, Acc_Type
);
781 Get_Next_Interp
(Index
, It
);
785 -- Cannot be applied to intrinsic. Looking at the tests above,
786 -- the only way Etype (N) can still be set to Any_Type is if
787 -- Is_Intrinsic_Subprogram was True for some referenced entity.
789 if Etype
(N
) = Any_Type
then
790 Error_Attr_P
("prefix of % attribute cannot be intrinsic");
792 end Build_Access_Subprogram_Type
;
794 ----------------------
795 -- OK_Self_Reference --
796 ----------------------
798 function OK_Self_Reference
return Boolean is
802 -- If N does not come from source, the reference is assumed to be
805 if not Comes_From_Source
(N
) then
812 (Nkind
(Par
) = N_Component_Association
813 or else Nkind
(Par
) in N_Subexpr
)
815 if Nkind
(Par
) in N_Aggregate | N_Extension_Aggregate
then
816 if Etype
(Par
) = Typ
then
817 Set_Has_Self_Reference
(Par
);
819 -- Check the context: the aggregate must be part of the
820 -- initialization of a type or component, or it is the
821 -- resulting expansion in an initialization procedure.
823 if Is_Init_Proc
(Current_Scope
) then
827 while Present
(Par
) loop
828 if Nkind
(Par
) = N_Full_Type_Declaration
then
843 -- No enclosing aggregate, or not a self-reference
846 end OK_Self_Reference
;
848 -- Start of processing for Analyze_Access_Attribute
851 -- Access and Unchecked_Access are illegal in declare_expressions,
852 -- according to the RM. We also make the GNAT Unrestricted_Access
853 -- attribute illegal if it comes from source.
855 if In_Declare_Expr
> 0
856 and then (Attr_Id
/= Attribute_Unrestricted_Access
857 or else Comes_From_Source
(N
))
859 Error_Attr
("% attribute cannot occur in a declare_expression", N
);
864 if Nkind
(P
) = N_Character_Literal
then
866 ("prefix of % attribute cannot be enumeration literal");
869 -- Preserve relevant elaboration-related attributes of the context
870 -- which are no longer available or very expensive to recompute once
871 -- analysis, resolution, and expansion are over.
873 Mark_Elaboration_Attributes
879 -- Save the scenario for later examination by the ABE Processing
882 Record_Elaboration_Scenario
(N
);
884 -- Case of access to subprogram
886 if Is_Entity_Name
(P
) and then Is_Overloadable
(Entity
(P
)) then
887 if Has_Pragma_Inline_Always
(Entity
(P
)) then
889 ("prefix of % attribute cannot be Inline_Always subprogram");
891 elsif Aname
= Name_Unchecked_Access
then
892 Error_Attr
("attribute% cannot be applied to a subprogram", P
);
895 -- Issue an error if the prefix denotes an eliminated subprogram
897 Check_For_Eliminated_Subprogram
(P
, Entity
(P
));
899 -- Check for obsolescent subprogram reference
901 Check_Obsolescent_2005_Entity
(Entity
(P
), P
);
903 -- Build the appropriate subprogram type
905 Build_Access_Subprogram_Type
(P
);
907 -- For P'Access or P'Unrestricted_Access, where P is a nested
908 -- subprogram, we might be passing P to another subprogram (but we
909 -- don't check that here), which might call P. P could modify
910 -- local variables, so we need to kill current values. It is
911 -- important not to do this for library-level subprograms, because
912 -- Kill_Current_Values is very inefficient in the case of library
913 -- level packages with lots of tagged types.
915 if Is_Library_Level_Entity
(Entity
(Prefix
(N
))) then
918 -- Do not kill values on nodes initializing dispatch tables
919 -- slots. The construct Prim_Ptr!(Prim'Unrestricted_Access)
920 -- is currently generated by the expander only for this
921 -- purpose. Done to keep the quality of warnings currently
922 -- generated by the compiler (otherwise any declaration of
923 -- a tagged type cleans constant indications from its scope).
925 elsif Nkind
(Parent
(N
)) = N_Unchecked_Type_Conversion
926 and then (Is_RTE
(Etype
(Parent
(N
)), RE_Prim_Ptr
)
928 Is_RTE
(Etype
(Parent
(N
)), RE_Size_Ptr
))
929 and then Is_Dispatching_Operation
930 (Directly_Designated_Type
(Etype
(N
)))
938 -- In the static elaboration model, treat the attribute reference
939 -- as a subprogram call for elaboration purposes. Suppress this
940 -- treatment under debug flag. In any case, we are all done.
942 if Legacy_Elaboration_Checks
943 and not Dynamic_Elaboration_Checks
944 and not Debug_Flag_Dot_UU
951 -- Component is an operation of a protected type
953 elsif Nkind
(P
) = N_Selected_Component
954 and then Is_Overloadable
(Entity
(Selector_Name
(P
)))
956 if Ekind
(Entity
(Selector_Name
(P
))) = E_Entry
then
957 Error_Attr_P
("prefix of % attribute must be subprogram");
960 Build_Access_Subprogram_Type
(Selector_Name
(P
));
964 -- Deal with incorrect reference to a type, but note that some
965 -- accesses are allowed: references to the current type instance,
966 -- or in Ada 2005 self-referential pointer in a default-initialized
969 if Is_Entity_Name
(P
) then
972 -- The reference may appear in an aggregate that has been expanded
973 -- into a loop. Locate scope of type definition, if any.
975 Scop
:= Current_Scope
;
976 while Ekind
(Scop
) = E_Loop
loop
977 Scop
:= Scope
(Scop
);
980 if Is_Type
(Typ
) then
982 -- OK if we are within the scope of a limited type
983 -- let's mark the component as having per object constraint
985 if Is_Anonymous_Tagged_Base
(Scop
, Typ
) then
993 Q
: Node_Id
:= Parent
(N
);
997 and then Nkind
(Q
) /= N_Component_Declaration
1003 Set_Has_Per_Object_Constraint
1004 (Defining_Identifier
(Q
), True);
1008 if Nkind
(P
) = N_Expanded_Name
then
1010 ("current instance prefix must be a direct name", P
);
1013 -- If a current instance attribute appears in a component
1014 -- constraint it must appear alone; other contexts (spec-
1015 -- expressions, within a task body) are not subject to this
1018 if not In_Spec_Expression
1019 and then not Has_Completion
(Scop
)
1021 Nkind
(Parent
(N
)) not in
1022 N_Discriminant_Association |
1023 N_Index_Or_Discriminant_Constraint
1026 ("current instance attribute must appear alone", N
);
1029 if Is_CPP_Class
(Root_Type
(Typ
)) then
1031 ("??current instance unsupported for derivations of "
1032 & "'C'P'P types", N
);
1035 -- OK if we are in initialization procedure for the type
1036 -- in question, in which case the reference to the type
1037 -- is rewritten as a reference to the current object.
1039 elsif Ekind
(Scop
) = E_Procedure
1040 and then Is_Init_Proc
(Scop
)
1041 and then Etype
(First_Formal
(Scop
)) = Typ
1044 Make_Attribute_Reference
(Loc
,
1045 Prefix
=> Make_Identifier
(Loc
, Name_uInit
),
1046 Attribute_Name
=> Name_Unrestricted_Access
));
1050 -- OK if a task type, this test needs sharpening up ???
1052 elsif Is_Task_Type
(Typ
) then
1055 -- OK if self-reference in an aggregate in Ada 2005, and
1056 -- the reference comes from a copied default expression.
1058 -- Note that we check legality of self-reference even if the
1059 -- expression comes from source, e.g. when a single component
1060 -- association in an aggregate has a box association.
1062 elsif Ada_Version
>= Ada_2005
and then OK_Self_Reference
then
1065 -- OK if reference to current instance of a protected object
1067 elsif Is_Protected_Self_Reference
(P
) then
1070 -- Otherwise we have an error case
1073 Error_Attr
("% attribute cannot be applied to type", P
);
1079 -- If we fall through, we have a normal access to object case
1081 -- Unrestricted_Access is (for now) legal wherever an allocator would
1082 -- be legal, so its Etype is set to E_Allocator. The expected type
1083 -- of the other attributes is a general access type, and therefore
1084 -- we label them with E_Access_Attribute_Type.
1086 if not Is_Overloaded
(P
) then
1087 Acc_Type
:= Build_Access_Object_Type
(P_Type
);
1088 Set_Etype
(N
, Acc_Type
);
1092 Index
: Interp_Index
;
1095 Set_Etype
(N
, Any_Type
);
1096 Get_First_Interp
(P
, Index
, It
);
1097 while Present
(It
.Typ
) loop
1098 Acc_Type
:= Build_Access_Object_Type
(It
.Typ
);
1099 Add_One_Interp
(N
, Acc_Type
, Acc_Type
);
1100 Get_Next_Interp
(Index
, It
);
1105 -- Special cases when we can find a prefix that is an entity name
1114 if Is_Entity_Name
(PP
) then
1117 -- If we have an access to an object, and the attribute
1118 -- comes from source, then set the object as potentially
1119 -- source modified. We do this because the resulting access
1120 -- pointer can be used to modify the variable, and we might
1121 -- not detect this, leading to some junk warnings.
1123 -- We only do this for source references, since otherwise
1124 -- we can suppress warnings, e.g. from the unrestricted
1125 -- access generated for validity checks in -gnatVa mode.
1127 if Comes_From_Source
(N
) then
1128 Set_Never_Set_In_Source
(Ent
, False);
1131 -- Mark entity as address taken in the case of
1132 -- 'Unrestricted_Access or subprograms, and kill current
1135 if Aname
= Name_Unrestricted_Access
1136 or else Is_Subprogram
(Ent
)
1138 Set_Address_Taken
(Ent
);
1141 Kill_Current_Values
(Ent
);
1144 elsif Nkind
(PP
) in N_Selected_Component | N_Indexed_Component
1153 end Analyze_Access_Attribute
;
1155 ----------------------------------
1156 -- Analyze_Attribute_Old_Result --
1157 ----------------------------------
1159 procedure Analyze_Attribute_Old_Result
1160 (Legal
: out Boolean;
1161 Spec_Id
: out Entity_Id
)
1163 procedure Check_Placement_In_Check
(Prag
: Node_Id
);
1164 -- Verify that the attribute appears within pragma Check that mimics
1167 procedure Check_Placement_In_Contract_Cases
(Prag
: Node_Id
);
1168 -- Verify that the attribute appears within a consequence of aspect
1169 -- or pragma Contract_Cases denoted by Prag.
1171 procedure Check_Placement_In_Test_Case
(Prag
: Node_Id
);
1172 -- Verify that the attribute appears within the "Ensures" argument of
1173 -- aspect or pragma Test_Case denoted by Prag.
1177 Encl_Nod
: Node_Id
) return Boolean;
1178 -- Subsidiary to Check_Placemenet_In_XXX. Determine whether arbitrary
1179 -- node Nod is within enclosing node Encl_Nod.
1181 procedure Placement_Error
;
1182 pragma No_Return
(Placement_Error
);
1183 -- Emit a general error when the attributes does not appear in a
1184 -- postcondition-like aspect or pragma, and then raises Bad_Attribute
1185 -- to avoid any further semantic processing.
1187 ------------------------------
1188 -- Check_Placement_In_Check --
1189 ------------------------------
1191 procedure Check_Placement_In_Check
(Prag
: Node_Id
) is
1192 Args
: constant List_Id
:= Pragma_Argument_Associations
(Prag
);
1193 Nam
: constant Name_Id
:= Chars
(Get_Pragma_Arg
(First
(Args
)));
1196 -- The "Name" argument of pragma Check denotes a postcondition
1200 | Name_Postcondition
1205 -- Otherwise the placement of the attribute is illegal
1210 end Check_Placement_In_Check
;
1212 ---------------------------------------
1213 -- Check_Placement_In_Contract_Cases --
1214 ---------------------------------------
1216 procedure Check_Placement_In_Contract_Cases
(Prag
: Node_Id
) is
1222 -- Obtain the argument of the aspect or pragma
1224 if Nkind
(Prag
) = N_Aspect_Specification
then
1227 Arg
:= First
(Pragma_Argument_Associations
(Prag
));
1230 Cases
:= Expression
(Arg
);
1232 if Present
(Component_Associations
(Cases
)) then
1233 CCase
:= First
(Component_Associations
(Cases
));
1234 while Present
(CCase
) loop
1236 -- Detect whether the attribute appears within the
1237 -- consequence of the current contract case.
1239 if Nkind
(CCase
) = N_Component_Association
1240 and then Is_Within
(N
, Expression
(CCase
))
1249 -- Otherwise aspect or pragma Contract_Cases is either malformed
1250 -- or the attribute does not appear within a consequence.
1253 ("attribute % must appear in the consequence of a contract case",
1255 end Check_Placement_In_Contract_Cases
;
1257 ----------------------------------
1258 -- Check_Placement_In_Test_Case --
1259 ----------------------------------
1261 procedure Check_Placement_In_Test_Case
(Prag
: Node_Id
) is
1262 Arg
: constant Node_Id
:=
1265 Arg_Nam
=> Name_Ensures
,
1266 From_Aspect
=> Nkind
(Prag
) = N_Aspect_Specification
);
1269 -- Detect whether the attribute appears within the "Ensures"
1270 -- expression of aspect or pragma Test_Case.
1272 if Present
(Arg
) and then Is_Within
(N
, Arg
) then
1277 ("attribute % must appear in the ensures expression of a "
1280 end Check_Placement_In_Test_Case
;
1288 Encl_Nod
: Node_Id
) return Boolean
1294 while Present
(Par
) loop
1295 if Par
= Encl_Nod
then
1298 -- Prevent the search from going too far
1300 elsif Is_Body_Or_Package_Declaration
(Par
) then
1304 Par
:= Parent
(Par
);
1310 ---------------------
1311 -- Placement_Error --
1312 ---------------------
1314 procedure Placement_Error
is
1316 if Aname
= Name_Old
then
1317 Error_Attr
("attribute % can only appear in postcondition", P
);
1319 -- Specialize the error message for attribute 'Result
1323 ("attribute % can only appear in postcondition of function",
1326 end Placement_Error
;
1332 Subp_Decl
: Node_Id
;
1334 -- Start of processing for Analyze_Attribute_Old_Result
1337 -- Assume that the attribute is illegal
1342 -- Traverse the parent chain to find the aspect or pragma where the
1343 -- attribute resides.
1346 while Present
(Prag
) loop
1347 if Nkind
(Prag
) in N_Aspect_Specification | N_Pragma
then
1350 -- Prevent the search from going too far
1352 elsif Is_Body_Or_Package_Declaration
(Prag
) then
1356 Prag
:= Parent
(Prag
);
1359 -- The attribute is allowed to appear only in postcondition-like
1360 -- aspects or pragmas.
1362 if Nkind
(Prag
) in N_Aspect_Specification | N_Pragma
then
1363 if Nkind
(Prag
) = N_Aspect_Specification
then
1364 Prag_Nam
:= Chars
(Identifier
(Prag
));
1366 Prag_Nam
:= Pragma_Name
(Prag
);
1369 if Prag_Nam
= Name_Check
then
1370 Check_Placement_In_Check
(Prag
);
1372 elsif Prag_Nam
= Name_Contract_Cases
then
1373 Check_Placement_In_Contract_Cases
(Prag
);
1375 -- Attribute 'Result is allowed to appear in aspect or pragma
1376 -- [Refined_]Depends (SPARK RM 6.1.5(11)).
1378 elsif Prag_Nam
in Name_Depends | Name_Refined_Depends
1379 and then Aname
= Name_Result
1383 -- Attribute 'Result is allowed to appear in aspect
1384 -- Relaxed_Initialization (SPARK RM 6.10).
1386 elsif Prag_Nam
= Name_Relaxed_Initialization
1387 and then Aname
= Name_Result
1391 elsif Prag_Nam
in Name_Post
1393 | Name_Postcondition
1398 elsif Prag_Nam
= Name_Test_Case
then
1399 Check_Placement_In_Test_Case
(Prag
);
1406 -- Otherwise the placement of the attribute is illegal
1413 -- Find the related subprogram subject to the aspect or pragma
1415 if Nkind
(Prag
) = N_Aspect_Specification
then
1416 Subp_Decl
:= Parent
(Prag
);
1418 Subp_Decl
:= Find_Related_Declaration_Or_Body
(Prag
);
1421 -- The aspect or pragma where the attribute resides should be
1422 -- associated with a subprogram declaration or a body. If this is not
1423 -- the case, then the aspect or pragma is illegal. Return as analysis
1424 -- cannot be carried out. Note that it is legal to have the aspect
1425 -- appear on a subprogram renaming, when the renamed entity is an
1426 -- attribute reference.
1428 -- Generating C code the internally built nested _postcondition
1429 -- subprograms are inlined; after expanded, inlined aspects are
1430 -- located in the internal block generated by the frontend.
1432 if Nkind
(Subp_Decl
) = N_Block_Statement
1433 and then Modify_Tree_For_C
1434 and then In_Inlined_Body
1438 elsif Nkind
(Subp_Decl
) not in N_Abstract_Subprogram_Declaration
1439 | N_Entry_Declaration
1440 | N_Expression_Function
1441 | N_Generic_Subprogram_Declaration
1443 | N_Subprogram_Body_Stub
1444 | N_Subprogram_Declaration
1445 | N_Subprogram_Renaming_Declaration
1450 -- If we get here, then the attribute is legal
1453 Spec_Id
:= Unique_Defining_Entity
(Subp_Decl
);
1455 -- When generating C code, nested _postcondition subprograms are
1456 -- inlined by the front end to avoid problems (when unnested) with
1457 -- referenced itypes. Handle that here, since as part of inlining the
1458 -- expander nests subprogram within a dummy procedure named _parent
1459 -- (see Build_Postconditions_Procedure and Build_Body_To_Inline).
1460 -- Hence, in this context, the spec_id of _postconditions is the
1463 if Modify_Tree_For_C
1464 and then Chars
(Spec_Id
) = Name_uParent
1465 and then Chars
(Scope
(Spec_Id
)) = Name_uPostconditions
1467 -- This situation occurs only when preanalyzing the inlined body
1469 pragma Assert
(not Full_Analysis
);
1471 Spec_Id
:= Scope
(Spec_Id
);
1472 pragma Assert
(Is_Inlined
(Spec_Id
));
1474 end Analyze_Attribute_Old_Result
;
1476 -----------------------------
1477 -- Analyze_Image_Attribute --
1478 -----------------------------
1480 procedure Analyze_Image_Attribute
(Str_Typ
: Entity_Id
) is
1481 procedure Check_Image_Type
(Image_Type
: Entity_Id
);
1482 -- Check that Image_Type is legal as the type of a prefix of 'Image.
1483 -- Legality depends on the Ada language version.
1485 ----------------------
1486 -- Check_Image_Type --
1487 ----------------------
1489 procedure Check_Image_Type
(Image_Type
: Entity_Id
) is
1491 -- Image_Type may be empty in case of another error detected,
1492 -- or if an N_Raise_xxx_Error node is a parent of N.
1494 if Ada_Version
< Ada_2022
1495 and then Present
(Image_Type
)
1496 and then not Is_Scalar_Type
(Image_Type
)
1498 Error_Msg_Ada_2022_Feature
("nonscalar ''Image", Sloc
(P
));
1501 end Check_Image_Type
;
1503 -- Start of processing for Analyze_Image_Attribute
1506 -- AI12-0124: The ARG has adopted the GNAT semantics of 'Img for
1507 -- scalar types, so that the prefix can be an object, a named value,
1508 -- or a type. If the prefix is an object, there is no argument.
1510 if Is_Object_Image
(P
) then
1512 Set_Etype
(N
, Str_Typ
);
1513 Check_Image_Type
(Etype
(P
));
1515 if Attr_Id
/= Attribute_Img
then
1516 Error_Msg_Ada_2012_Feature
("|Object''Image", Sloc
(P
));
1520 Set_Etype
(N
, Str_Typ
);
1522 pragma Assert
(Is_Entity_Name
(P
) and then Is_Type
(Entity
(P
)));
1524 if Ekind
(Entity
(P
)) = E_Incomplete_Type
1525 and then Present
(Full_View
(Entity
(P
)))
1527 P_Type
:= Full_View
(Entity
(P
));
1528 P_Base_Type
:= Base_Type
(P_Type
);
1529 Set_Entity
(P
, P_Type
);
1532 Check_Image_Type
(P_Type
);
1533 Resolve
(E1
, P_Base_Type
);
1534 Validate_Non_Static_Attribute_Function_Call
;
1537 Check_Enum_Image
(Check_Enumeration_Maps
=> True);
1539 -- Check restriction No_Fixed_IO. Note the check of Comes_From_Source
1540 -- to avoid giving a duplicate message for when Image attributes
1541 -- applied to object references get expanded into type-based Image
1544 if Restriction_Check_Required
(No_Fixed_IO
)
1545 and then Comes_From_Source
(N
)
1546 and then Is_Fixed_Point_Type
(P_Type
)
1548 Check_Restriction
(No_Fixed_IO
, P
);
1550 end Analyze_Image_Attribute
;
1552 ---------------------------------
1553 -- Bad_Attribute_For_Predicate --
1554 ---------------------------------
1556 procedure Bad_Attribute_For_Predicate
is
1558 if Is_Scalar_Type
(P_Type
)
1559 and then Comes_From_Source
(N
)
1561 Error_Msg_Name_1
:= Aname
;
1562 Bad_Predicated_Subtype_Use
1563 ("type& has predicates, attribute % not allowed", N
, P_Type
);
1565 end Bad_Attribute_For_Predicate
;
1567 --------------------------------
1568 -- Check_Array_Or_Scalar_Type --
1569 --------------------------------
1571 procedure Check_Array_Or_Scalar_Type
is
1572 function In_Aspect_Specification
return Boolean;
1573 -- A current instance of a type in an aspect specification is an
1574 -- object and not a type, and therefore cannot be of a scalar type
1575 -- in the prefix of one of the array attributes if the attribute
1576 -- reference is part of an aspect expression.
1578 -----------------------------
1579 -- In_Aspect_Specification --
1580 -----------------------------
1582 function In_Aspect_Specification
return Boolean is
1587 while Present
(P
) loop
1588 if Nkind
(P
) = N_Aspect_Specification
then
1589 return P_Type
= Entity
(P
);
1591 elsif Nkind
(P
) in N_Declaration
then
1599 end In_Aspect_Specification
;
1605 -- Start of processing for Check_Array_Or_Scalar_Type
1608 -- Case of string literal or string literal subtype. These cases
1609 -- cannot arise from legal Ada code, but the expander is allowed
1610 -- to generate them. They require special handling because string
1611 -- literal subtypes do not have standard bounds (the whole idea
1612 -- of these subtypes is to avoid having to generate the bounds)
1614 if Ekind
(P_Type
) = E_String_Literal_Subtype
then
1615 Set_Etype
(N
, Etype
(First_Index
(P_Base_Type
)));
1620 elsif Is_Scalar_Type
(P_Type
) then
1623 if Present
(E1
) then
1624 Error_Attr
("invalid argument in % attribute", E1
);
1626 elsif In_Aspect_Specification
then
1628 ("prefix of % attribute cannot be the current instance of a "
1629 & "scalar type", P
);
1632 Set_Etype
(N
, P_Base_Type
);
1636 -- The following is a special test to allow 'First to apply to
1637 -- private scalar types if the attribute comes from generated
1638 -- code. This occurs in the case of Normalize_Scalars code.
1640 elsif Is_Private_Type
(P_Type
)
1641 and then Present
(Full_View
(P_Type
))
1642 and then Is_Scalar_Type
(Full_View
(P_Type
))
1643 and then not Comes_From_Source
(N
)
1645 Set_Etype
(N
, Implementation_Base_Type
(P_Type
));
1647 -- Array types other than string literal subtypes handled above
1652 -- We know prefix is an array type, or the name of an array
1653 -- object, and that the expression, if present, is static
1654 -- and within the range of the dimensions of the type.
1656 pragma Assert
(Is_Array_Type
(P_Type
));
1657 Index
:= First_Index
(P_Base_Type
);
1661 -- First dimension assumed
1663 Set_Etype
(N
, Base_Type
(Etype
(Index
)));
1667 Udims
: constant Uint
:= Expr_Value
(E1
);
1668 Dims
: constant Int
:= UI_To_Int
(Udims
);
1670 for J
in 1 .. Dims
- 1 loop
1675 Set_Etype
(N
, Base_Type
(Etype
(Index
)));
1678 end Check_Array_Or_Scalar_Type
;
1680 ----------------------
1681 -- Check_Array_Type --
1682 ----------------------
1684 procedure Check_Array_Type
is
1686 -- Dimension number for array attributes
1689 -- If the type is a string literal type, then this must be generated
1690 -- internally, and no further check is required on its legality.
1692 if Ekind
(P_Type
) = E_String_Literal_Subtype
then
1695 -- If the type is a composite, it is an illegal aggregate, no point
1698 elsif P_Type
= Any_Composite
then
1699 raise Bad_Attribute
;
1702 -- Normal case of array type or subtype. Note that if the
1703 -- prefix is a current instance of a type declaration it
1704 -- appears within an aspect specification and is legal.
1706 Check_Either_E0_Or_E1
;
1709 if Is_Array_Type
(P_Type
) then
1710 if not Is_Constrained
(P_Type
)
1711 and then Is_Entity_Name
(P
)
1712 and then Is_Type
(Entity
(P
))
1713 and then not Is_Current_Instance
(P
)
1715 -- Note: we do not call Error_Attr here, since we prefer to
1716 -- continue, using the relevant index type of the array,
1717 -- even though it is unconstrained. This gives better error
1718 -- recovery behavior.
1720 Error_Msg_Name_1
:= Aname
;
1722 ("prefix for % attribute must be constrained array", P
);
1725 -- The attribute reference freezes the type, and thus the
1726 -- component type, even if the attribute may not depend on the
1727 -- component. Diagnose arrays with incomplete components now.
1728 -- If the prefix is an access to array, this does not freeze
1729 -- the designated type.
1731 if Nkind
(P
) /= N_Explicit_Dereference
then
1732 Check_Fully_Declared
(Component_Type
(P_Type
), P
);
1735 D
:= Number_Dimensions
(P_Type
);
1738 if Is_Private_Type
(P_Type
) then
1739 Error_Attr_P
("prefix for % attribute may not be private type");
1741 elsif Is_Access_Type
(P_Type
)
1742 and then Is_Array_Type
(Designated_Type
(P_Type
))
1743 and then Is_Entity_Name
(P
)
1744 and then Is_Type
(Entity
(P
))
1746 Error_Attr_P
("prefix of % attribute cannot be access type");
1748 elsif Attr_Id
= Attribute_First
1750 Attr_Id
= Attribute_Last
1752 Error_Attr
("invalid prefix for % attribute", P
);
1755 Error_Attr_P
("prefix for % attribute must be array");
1759 if Present
(E1
) then
1760 Resolve
(E1
, Any_Integer
);
1761 Set_Etype
(E1
, Standard_Integer
);
1763 if not Is_OK_Static_Expression
(E1
)
1764 or else Raises_Constraint_Error
(E1
)
1766 Flag_Non_Static_Expr
1767 ("expression for dimension must be static!", E1
);
1770 elsif Expr_Value
(E1
) > D
or else Expr_Value
(E1
) < 1 then
1771 Error_Attr
("invalid dimension number for array type", E1
);
1775 if (Style_Check
and Style_Check_Array_Attribute_Index
)
1776 and then Comes_From_Source
(N
)
1778 Style
.Check_Array_Attribute_Index
(N
, E1
, D
);
1780 end Check_Array_Type
;
1782 -------------------------
1783 -- Check_Asm_Attribute --
1784 -------------------------
1786 procedure Check_Asm_Attribute
is
1791 -- Check first argument is static string expression
1793 Analyze_And_Resolve
(E1
, Standard_String
);
1795 if Etype
(E1
) = Any_Type
then
1798 elsif not Is_OK_Static_Expression
(E1
) then
1799 Flag_Non_Static_Expr
1800 ("constraint argument must be static string expression!", E1
);
1804 -- Check second argument is right type
1806 Analyze_And_Resolve
(E2
, Entity
(P
));
1808 -- Note: that is all we need to do, we don't need to check
1809 -- that it appears in a correct context. The Ada type system
1810 -- will do that for us.
1812 end Check_Asm_Attribute
;
1814 ---------------------
1815 -- Check_Component --
1816 ---------------------
1818 procedure Check_Component
is
1822 if Nkind
(P
) /= N_Selected_Component
1824 (Ekind
(Entity
(Selector_Name
(P
))) /= E_Component
1826 Ekind
(Entity
(Selector_Name
(P
))) /= E_Discriminant
)
1828 Error_Attr_P
("prefix for % attribute must be selected component");
1830 end Check_Component
;
1832 ------------------------------------
1833 -- Check_Decimal_Fixed_Point_Type --
1834 ------------------------------------
1836 procedure Check_Decimal_Fixed_Point_Type
is
1840 if not Is_Decimal_Fixed_Point_Type
(P_Type
) then
1841 Error_Attr_P
("prefix of % attribute must be decimal type");
1843 end Check_Decimal_Fixed_Point_Type
;
1845 -----------------------
1846 -- Check_Dereference --
1847 -----------------------
1849 procedure Check_Dereference
is
1852 -- Case of a subtype mark
1854 if Is_Entity_Name
(P
) and then Is_Type
(Entity
(P
)) then
1858 -- Case of an expression
1862 if Is_Access_Type
(P_Type
) then
1864 -- If there is an implicit dereference, then we must freeze the
1865 -- designated type of the access type, since the type of the
1866 -- referenced array is this type (see AI95-00106).
1868 -- As done elsewhere, freezing must not happen when preanalyzing
1869 -- a pre- or postcondition or a default value for an object or for
1870 -- a formal parameter.
1872 if not In_Spec_Expression
then
1873 Freeze_Before
(N
, Designated_Type
(P_Type
));
1877 Make_Explicit_Dereference
(Sloc
(P_Old
),
1878 Prefix
=> Relocate_Node
(P_Old
)));
1880 Analyze_And_Resolve
(P_Old
);
1881 P_Type
:= Etype
(P_Old
);
1883 if P_Type
= Any_Type
then
1884 raise Bad_Attribute
;
1887 P_Base_Type
:= Base_Type
(P_Type
);
1889 end Check_Dereference
;
1891 -------------------------
1892 -- Check_Discrete_Type --
1893 -------------------------
1895 procedure Check_Discrete_Type
is
1899 if not Is_Discrete_Type
(P_Type
) then
1900 Error_Attr_P
("prefix of % attribute must be discrete type");
1902 end Check_Discrete_Type
;
1908 procedure Check_E0
is
1910 if Present
(E1
) then
1911 Unexpected_Argument
(E1
);
1919 procedure Check_E1
is
1921 Check_Either_E0_Or_E1
;
1925 -- Special-case attributes that are functions and that appear as
1926 -- the prefix of another attribute. Error is posted on parent.
1928 if Nkind
(Parent
(N
)) = N_Attribute_Reference
1929 and then Attribute_Name
(Parent
(N
)) in Name_Address
1933 Error_Msg_Name_1
:= Attribute_Name
(Parent
(N
));
1934 Error_Msg_N
("illegal prefix for % attribute", Parent
(N
));
1935 Set_Etype
(Parent
(N
), Any_Type
);
1936 Set_Entity
(Parent
(N
), Any_Type
);
1937 raise Bad_Attribute
;
1940 Error_Attr
("missing argument for % attribute", N
);
1949 procedure Check_E2
is
1952 Error_Attr
("missing arguments for % attribute (2 required)", N
);
1954 Error_Attr
("missing argument for % attribute (2 required)", N
);
1958 ---------------------------
1959 -- Check_Either_E0_Or_E1 --
1960 ---------------------------
1962 procedure Check_Either_E0_Or_E1
is
1964 if Present
(E2
) then
1965 Unexpected_Argument
(E2
);
1967 end Check_Either_E0_Or_E1
;
1969 ----------------------
1970 -- Check_Enum_Image --
1971 ----------------------
1973 procedure Check_Enum_Image
(Check_Enumeration_Maps
: Boolean := False) is
1977 -- Ensure that Check_Enumeration_Maps parameter is set precisely for
1978 -- attributes whose implementation requires enumeration maps.
1981 (Check_Enumeration_Maps
= (Attr_Id
in Attribute_Image
1983 | Attribute_Valid_Value
1985 | Attribute_Wide_Image
1986 | Attribute_Wide_Value
1987 | Attribute_Wide_Wide_Image
1988 | Attribute_Wide_Wide_Value
));
1990 -- When an enumeration type appears in an attribute reference, all
1991 -- literals of the type are marked as referenced. This must only be
1992 -- done if the attribute reference appears in the current source.
1993 -- Otherwise the information on references may differ between a
1994 -- normal compilation and one that performs inlining.
1996 if Is_Enumeration_Type
(P_Base_Type
)
1997 and then In_Extended_Main_Code_Unit
(N
)
1999 if Check_Enumeration_Maps
then
2000 Check_Restriction
(No_Enumeration_Maps
, N
);
2003 Lit
:= First_Literal
(P_Base_Type
);
2004 while Present
(Lit
) loop
2005 Set_Referenced
(Lit
);
2009 end Check_Enum_Image
;
2011 ----------------------------
2012 -- Check_First_Last_Valid --
2013 ----------------------------
2015 procedure Check_First_Last_Valid
is
2017 Check_Discrete_Type
;
2019 -- Freeze the subtype now, so that the following test for predicates
2020 -- works (we set the predicates stuff up at freeze time)
2022 Insert_Actions
(N
, Freeze_Entity
(P_Type
, P
));
2024 -- Now test for dynamic predicate
2026 if Has_Predicates
(P_Type
)
2027 and then not (Has_Static_Predicate
(P_Type
))
2030 ("prefix of % attribute may not have dynamic predicate");
2033 -- Check non-static subtype
2035 if not Is_OK_Static_Subtype
(P_Type
) then
2036 Error_Attr_P
("prefix of % attribute must be a static subtype");
2039 -- Test case for no values
2041 if Expr_Value
(Type_Low_Bound
(P_Type
)) >
2042 Expr_Value
(Type_High_Bound
(P_Type
))
2043 or else (Has_Predicates
(P_Type
)
2045 Is_Empty_List
(Static_Discrete_Predicate
(P_Type
)))
2048 ("prefix of % attribute must be subtype with at least one "
2051 end Check_First_Last_Valid
;
2053 ----------------------------
2054 -- Check_Fixed_Point_Type --
2055 ----------------------------
2057 procedure Check_Fixed_Point_Type
is
2061 if not Is_Fixed_Point_Type
(P_Type
) then
2062 Error_Attr_P
("prefix of % attribute must be fixed point type");
2064 end Check_Fixed_Point_Type
;
2066 ------------------------------
2067 -- Check_Fixed_Point_Type_0 --
2068 ------------------------------
2070 procedure Check_Fixed_Point_Type_0
is
2072 Check_Fixed_Point_Type
;
2074 end Check_Fixed_Point_Type_0
;
2076 -------------------------------
2077 -- Check_Floating_Point_Type --
2078 -------------------------------
2080 procedure Check_Floating_Point_Type
is
2084 if not Is_Floating_Point_Type
(P_Type
) then
2085 Error_Attr_P
("prefix of % attribute must be float type");
2087 end Check_Floating_Point_Type
;
2089 ---------------------------------
2090 -- Check_Floating_Point_Type_0 --
2091 ---------------------------------
2093 procedure Check_Floating_Point_Type_0
is
2095 Check_Floating_Point_Type
;
2097 end Check_Floating_Point_Type_0
;
2099 ---------------------------------
2100 -- Check_Floating_Point_Type_1 --
2101 ---------------------------------
2103 procedure Check_Floating_Point_Type_1
is
2105 Check_Floating_Point_Type
;
2107 end Check_Floating_Point_Type_1
;
2109 ---------------------------------
2110 -- Check_Floating_Point_Type_2 --
2111 ---------------------------------
2113 procedure Check_Floating_Point_Type_2
is
2115 Check_Floating_Point_Type
;
2117 end Check_Floating_Point_Type_2
;
2119 ------------------------
2120 -- Check_Integer_Type --
2121 ------------------------
2123 procedure Check_Integer_Type
is
2127 if not Is_Integer_Type
(P_Type
) then
2128 Error_Attr_P
("prefix of % attribute must be integer type");
2130 end Check_Integer_Type
;
2132 --------------------------------
2133 -- Check_Modular_Integer_Type --
2134 --------------------------------
2136 procedure Check_Modular_Integer_Type
is
2140 if not Is_Modular_Integer_Type
(P_Type
) then
2142 ("prefix of % attribute must be modular integer type");
2144 end Check_Modular_Integer_Type
;
2146 ------------------------
2147 -- Check_Not_CPP_Type --
2148 ------------------------
2150 procedure Check_Not_CPP_Type
is
2152 if Is_Tagged_Type
(Etype
(P
))
2153 and then Convention
(Etype
(P
)) = Convention_CPP
2154 and then Is_CPP_Class
(Root_Type
(Etype
(P
)))
2157 ("invalid use of % attribute with 'C'P'P tagged type");
2159 end Check_Not_CPP_Type
;
2161 -------------------------------
2162 -- Check_Not_Incomplete_Type --
2163 -------------------------------
2165 procedure Check_Not_Incomplete_Type
is
2170 -- Ada 2005 (AI-50217, AI-326): If the prefix is an explicit
2171 -- dereference we have to check wrong uses of incomplete types
2172 -- (other wrong uses are checked at their freezing point).
2174 -- In Ada 2012, incomplete types can appear in subprogram
2175 -- profiles, but formals with incomplete types cannot be the
2176 -- prefix of attributes.
2178 -- Example 1: Limited-with
2180 -- limited with Pkg;
2182 -- type Acc is access Pkg.T;
2184 -- S : Integer := X.all'Size; -- ERROR
2187 -- Example 2: Tagged incomplete
2189 -- type T is tagged;
2190 -- type Acc is access all T;
2192 -- S : constant Integer := X.all'Size; -- ERROR
2193 -- procedure Q (Obj : Integer := X.all'Alignment); -- ERROR
2195 if Ada_Version
>= Ada_2005
2196 and then Nkind
(P
) = N_Explicit_Dereference
2199 while Nkind
(E
) = N_Explicit_Dereference
loop
2205 if From_Limited_With
(Typ
) then
2207 ("prefix of % attribute cannot be an incomplete type");
2209 -- If the prefix is an access type check the designated type
2211 elsif Is_Access_Type
(Typ
)
2212 and then Nkind
(P
) = N_Explicit_Dereference
2214 Typ
:= Directly_Designated_Type
(Typ
);
2217 if Is_Class_Wide_Type
(Typ
) then
2218 Typ
:= Root_Type
(Typ
);
2221 -- A legal use of a shadow entity occurs only when the unit where
2222 -- the non-limited view resides is imported via a regular with
2223 -- clause in the current body. Such references to shadow entities
2224 -- may occur in subprogram formals.
2226 if Is_Incomplete_Type
(Typ
)
2227 and then From_Limited_With
(Typ
)
2228 and then Present
(Non_Limited_View
(Typ
))
2229 and then Is_Legal_Shadow_Entity_In_Body
(Typ
)
2231 Typ
:= Non_Limited_View
(Typ
);
2234 -- If still incomplete, it can be a local incomplete type, or a
2235 -- limited view whose scope is also a limited view.
2237 if Ekind
(Typ
) = E_Incomplete_Type
then
2238 if not From_Limited_With
(Typ
)
2239 and then No
(Full_View
(Typ
))
2242 ("prefix of % attribute cannot be an incomplete type");
2244 -- The limited view may be available indirectly through
2245 -- an intermediate unit. If the non-limited view is available
2246 -- the attribute reference is legal.
2248 elsif From_Limited_With
(Typ
)
2250 (No
(Non_Limited_View
(Typ
))
2251 or else Is_Incomplete_Type
(Non_Limited_View
(Typ
)))
2254 ("prefix of % attribute cannot be an incomplete type");
2258 -- Ada 2012 : formals in bodies may be incomplete, but no attribute
2261 elsif Is_Entity_Name
(P
)
2262 and then Is_Formal
(Entity
(P
))
2263 and then Is_Incomplete_Type
(Etype
(Etype
(P
)))
2266 ("prefix of % attribute cannot be an incomplete type");
2269 if not Is_Entity_Name
(P
)
2270 or else not Is_Type
(Entity
(P
))
2271 or else In_Spec_Expression
2275 Check_Fully_Declared
(P_Type
, P
);
2277 end Check_Not_Incomplete_Type
;
2279 ----------------------------
2280 -- Check_Object_Reference --
2281 ----------------------------
2283 procedure Check_Object_Reference
(P
: Node_Id
) is
2287 -- If we need an object, and we have a prefix that is the name of a
2288 -- function entity, convert it into a function call.
2290 if Is_Entity_Name
(P
)
2291 and then Ekind
(Entity
(P
)) = E_Function
2293 Rtyp
:= Etype
(Entity
(P
));
2296 Make_Function_Call
(Sloc
(P
),
2297 Name
=> Relocate_Node
(P
)));
2299 Analyze_And_Resolve
(P
, Rtyp
);
2301 -- Otherwise we must have an object reference
2303 elsif not Is_Object_Reference
(P
) then
2304 Error_Attr_P
("prefix of % attribute must be object");
2306 end Check_Object_Reference
;
2308 ----------------------------
2309 -- Check_PolyORB_Attribute --
2310 ----------------------------
2312 procedure Check_PolyORB_Attribute
is
2314 Validate_Non_Static_Attribute_Function_Call
;
2319 if Get_PCS_Name
/= Name_PolyORB_DSA
then
2321 ("attribute% requires the 'Poly'O'R'B 'P'C'S", N
);
2323 end Check_PolyORB_Attribute
;
2325 ------------------------
2326 -- Check_Program_Unit --
2327 ------------------------
2329 procedure Check_Program_Unit
is
2331 if Is_Entity_Name
(P
) then
2333 E
: constant Entity_Id
:= Entity
(P
);
2335 if Ekind
(E
) in E_Protected_Type
2341 or else Is_Single_Concurrent_Object
(E
)
2348 Error_Attr_P
("prefix of % attribute must be program unit");
2349 end Check_Program_Unit
;
2351 ---------------------
2352 -- Check_Real_Type --
2353 ---------------------
2355 procedure Check_Real_Type
is
2359 if not Is_Real_Type
(P_Type
) then
2360 Error_Attr_P
("prefix of % attribute must be real type");
2362 end Check_Real_Type
;
2364 ----------------------------
2365 -- Check_Enumeration_Type --
2366 ----------------------------
2368 procedure Check_Enumeration_Type
is
2372 if not Is_Enumeration_Type
(P_Type
) then
2373 Error_Attr_P
("prefix of % attribute must be enumeration type");
2375 end Check_Enumeration_Type
;
2377 -----------------------
2378 -- Check_Scalar_Type --
2379 -----------------------
2381 procedure Check_Scalar_Type
is
2385 if not Is_Scalar_Type
(P_Type
) then
2386 Error_Attr_P
("prefix of % attribute must be scalar type");
2388 end Check_Scalar_Type
;
2390 ---------------------------
2391 -- Check_Standard_Prefix --
2392 ---------------------------
2394 procedure Check_Standard_Prefix
is
2398 if Nkind
(P
) /= N_Identifier
or else Chars
(P
) /= Name_Standard
then
2399 Error_Attr
("only allowed prefix for % attribute is Standard", P
);
2401 end Check_Standard_Prefix
;
2403 -------------------------------
2404 -- Check_Put_Image_Attribute --
2405 -------------------------------
2407 procedure Check_Put_Image_Attribute
is
2409 -- Put_Image is a procedure, and can only appear at the position of a
2410 -- procedure call. If it's a list member and it's parent is a
2411 -- procedure call or aggregate, then this is appearing as an actual
2412 -- parameter or component association, which is wrong.
2414 if Is_List_Member
(N
)
2415 and then Nkind
(Parent
(N
)) not in
2416 N_Procedure_Call_Statement | N_Aggregate
2421 ("invalid context for attribute%, which is a procedure", N
);
2425 Analyze_And_Resolve
(E1
);
2427 -- Check that the first argument is
2428 -- Ada.Strings.Text_Buffers.Root_Buffer_Type'Class.
2430 -- Note: the double call to Root_Type here is needed because the
2431 -- root type of a class-wide type is the corresponding type (e.g.
2432 -- X for X'Class, and we really want to go to the root.)
2434 if not Is_RTE
(Root_Type
(Root_Type
(Etype
(E1
))),
2435 RE_Root_Buffer_Type
)
2438 ("expected Ada.Strings.Text_Buffers.Root_Buffer_Type''Class",
2442 -- Check that the second argument is of the right type
2445 Resolve
(E2
, P_Type
);
2446 end Check_Put_Image_Attribute
;
2448 ----------------------------
2449 -- Check_Stream_Attribute --
2450 ----------------------------
2452 procedure Check_Stream_Attribute
(Nam
: TSS_Name_Type
) is
2456 In_Shared_Var_Procs
: Boolean;
2457 -- True when compiling System.Shared_Storage.Shared_Var_Procs body.
2458 -- For this runtime package (always compiled in GNAT mode), we allow
2459 -- stream attributes references for limited types for the case where
2460 -- shared passive objects are implemented using stream attributes,
2461 -- which is the default in GNAT's persistent storage implementation.
2464 Validate_Non_Static_Attribute_Function_Call
;
2466 -- With the exception of 'Input, Stream attributes are procedures,
2467 -- and can only appear at the position of procedure calls. We check
2468 -- for this here, before they are rewritten, to give a more precise
2471 if Nam
= TSS_Stream_Input
then
2474 elsif Is_List_Member
(N
)
2475 and then Nkind
(Parent
(N
)) not in
2476 N_Procedure_Call_Statement | N_Aggregate
2482 ("invalid context for attribute%, which is a procedure", N
);
2486 Btyp
:= Implementation_Base_Type
(P_Type
);
2488 -- Stream attributes not allowed on limited types unless the
2489 -- attribute reference was generated by the expander (in which
2490 -- case the underlying type will be used, as described in Sinfo),
2491 -- or the attribute was specified explicitly for the type itself
2492 -- or one of its ancestors (taking visibility rules into account if
2493 -- in Ada 2005 mode), or a pragma Stream_Convert applies to Btyp
2494 -- (with no visibility restriction).
2497 Gen_Body
: constant Node_Id
:= Enclosing_Generic_Body
(N
);
2499 if Present
(Gen_Body
) then
2500 In_Shared_Var_Procs
:=
2501 Is_RTE
(Corresponding_Spec
(Gen_Body
), RE_Shared_Var_Procs
);
2503 In_Shared_Var_Procs
:= False;
2507 if (Comes_From_Source
(N
)
2508 and then not (In_Shared_Var_Procs
or In_Instance
))
2509 and then not Stream_Attribute_Available
(P_Type
, Nam
)
2510 and then not Has_Rep_Pragma
(Btyp
, Name_Stream_Convert
)
2512 Error_Msg_Name_1
:= Aname
;
2514 if Is_Limited_Type
(P_Type
) then
2516 ("limited type& has no% attribute", P
, P_Type
);
2517 Explain_Limited_Type
(P_Type
, P
);
2520 ("attribute% for type& is not available", P
, P_Type
);
2524 -- Check for no stream operations allowed from No_Tagged_Streams
2526 if Is_Tagged_Type
(P_Type
)
2527 and then Present
(No_Tagged_Streams_Pragma
(P_Type
))
2529 Error_Msg_Sloc
:= Sloc
(No_Tagged_Streams_Pragma
(P_Type
));
2531 ("no stream operations for & (No_Tagged_Streams #)", N
, P_Type
);
2535 -- Check restriction violations
2537 -- First check the No_Streams restriction, which prohibits the use
2538 -- of explicit stream attributes in the source program. We do not
2539 -- prevent the occurrence of stream attributes in generated code,
2540 -- for instance those generated implicitly for dispatching purposes.
2542 if Comes_From_Source
(N
) then
2543 Check_Restriction
(No_Streams
, P
);
2546 -- AI05-0057: if restriction No_Default_Stream_Attributes is active,
2547 -- it is illegal to use a predefined elementary type stream attribute
2548 -- either by itself, or more importantly as part of the attribute
2549 -- subprogram for a composite type. However, if the broader
2550 -- restriction No_Streams is active, stream operations are not
2551 -- generated, and there is no error.
2553 if Restriction_Active
(No_Default_Stream_Attributes
)
2554 and then not Restriction_Active
(No_Streams
)
2560 if Nam
= TSS_Stream_Input
2562 Nam
= TSS_Stream_Read
2565 Type_Without_Stream_Operation
(P_Type
, TSS_Stream_Read
);
2568 Type_Without_Stream_Operation
(P_Type
, TSS_Stream_Write
);
2572 Check_Restriction
(No_Default_Stream_Attributes
, N
);
2575 ("missing user-defined Stream Read or Write for type&",
2577 if not Is_Elementary_Type
(P_Type
) then
2579 ("\which is a component of type&", N
, P_Type
);
2585 -- Check special case of Exception_Id and Exception_Occurrence which
2586 -- are not allowed for restriction No_Exception_Registration.
2588 if Restriction_Check_Required
(No_Exception_Registration
)
2589 and then (Is_RTE
(P_Type
, RE_Exception_Id
)
2591 Is_RTE
(P_Type
, RE_Exception_Occurrence
))
2593 Check_Restriction
(No_Exception_Registration
, P
);
2596 -- Here we must check that the first argument is an access type
2597 -- that is compatible with Ada.Streams.Root_Stream_Type'Class.
2599 Analyze_And_Resolve
(E1
);
2602 -- Note: the double call to Root_Type here is needed because the
2603 -- root type of a class-wide type is the corresponding type (e.g.
2604 -- X for X'Class, and we really want to go to the root.)
2606 if not Is_Access_Type
(Etyp
)
2607 or else not Is_RTE
(Root_Type
(Root_Type
(Designated_Type
(Etyp
))),
2608 RE_Root_Stream_Type
)
2611 ("expected access to Ada.Streams.Root_Stream_Type''Class", E1
);
2614 -- Check that the second argument is of the right type if there is
2615 -- one (the Input attribute has only one argument so this is skipped)
2617 if Present
(E2
) then
2620 if Nam
= TSS_Stream_Read
2621 and then not Is_OK_Variable_For_Out_Formal
(E2
)
2624 ("second argument of % attribute must be a variable", E2
);
2627 Resolve
(E2
, P_Type
);
2631 end Check_Stream_Attribute
;
2633 -------------------------
2634 -- Check_System_Prefix --
2635 -------------------------
2637 procedure Check_System_Prefix
is
2639 if Nkind
(P
) /= N_Identifier
or else Chars
(P
) /= Name_System
then
2640 Error_Attr
("only allowed prefix for % attribute is System", P
);
2642 end Check_System_Prefix
;
2644 -----------------------
2645 -- Check_Task_Prefix --
2646 -----------------------
2648 procedure Check_Task_Prefix
is
2652 -- Ada 2005 (AI-345): Attribute 'Terminated can be applied to
2653 -- task interface class-wide types.
2655 if Is_Task_Type
(Etype
(P
))
2656 or else (Is_Access_Type
(Etype
(P
))
2657 and then Is_Task_Type
(Designated_Type
(Etype
(P
))))
2658 or else (Ada_Version
>= Ada_2005
2659 and then Ekind
(Etype
(P
)) = E_Class_Wide_Type
2660 and then Is_Interface
(Etype
(P
))
2661 and then Is_Task_Interface
(Etype
(P
)))
2666 if Ada_Version
>= Ada_2005
then
2668 ("prefix of % attribute must be a task or a task " &
2669 "interface class-wide object");
2672 Error_Attr_P
("prefix of % attribute must be a task");
2675 end Check_Task_Prefix
;
2681 -- The possibilities are an entity name denoting a type, or an
2682 -- attribute reference that denotes a type (Base or Class). If
2683 -- the type is incomplete, replace it with its full view.
2685 procedure Check_Type
is
2687 if not Is_Entity_Name
(P
)
2688 or else not Is_Type
(Entity
(P
))
2690 Error_Attr_P
("prefix of % attribute must be a type");
2692 elsif Is_Protected_Self_Reference
(P
) then
2694 ("prefix of % attribute denotes current instance "
2695 & "(RM 9.4(21/2))");
2697 elsif Ekind
(Entity
(P
)) = E_Incomplete_Type
2698 and then Present
(Full_View
(Entity
(P
)))
2700 P_Type
:= Full_View
(Entity
(P
));
2701 Set_Entity
(P
, P_Type
);
2705 ---------------------
2706 -- Check_Unit_Name --
2707 ---------------------
2709 procedure Check_Unit_Name
(Nod
: Node_Id
) is
2711 if Nkind
(Nod
) = N_Identifier
then
2714 elsif Nkind
(Nod
) in N_Selected_Component | N_Expanded_Name
then
2715 Check_Unit_Name
(Prefix
(Nod
));
2717 if Nkind
(Selector_Name
(Nod
)) = N_Identifier
then
2722 Error_Attr
("argument for % attribute must be unit name", P
);
2723 end Check_Unit_Name
;
2729 procedure Error_Attr
is
2731 Set_Etype
(N
, Any_Type
);
2732 Set_Entity
(N
, Any_Type
);
2733 raise Bad_Attribute
;
2736 procedure Error_Attr
(Msg
: String; Error_Node
: Node_Id
) is
2738 Error_Msg_Name_1
:= Aname
;
2739 Error_Msg_N
(Msg
, Error_Node
);
2747 procedure Error_Attr_P
(Msg
: String; Msg_Cont
: String := "") is
2749 Error_Msg_Name_1
:= Aname
;
2750 Error_Msg_F
(Msg
, P
);
2751 if Msg_Cont
/= "" then
2752 Error_Msg_F
(Msg_Cont
, P
);
2757 ----------------------------
2758 -- Legal_Formal_Attribute --
2759 ----------------------------
2761 procedure Legal_Formal_Attribute
is
2765 if not Is_Entity_Name
(P
)
2766 or else not Is_Type
(Entity
(P
))
2768 Error_Attr_P
("prefix of % attribute must be generic type");
2770 elsif Is_Generic_Actual_Type
(Entity
(P
))
2772 or else In_Inlined_Body
2776 elsif Is_Generic_Type
(Entity
(P
)) then
2777 if Is_Definite_Subtype
(Entity
(P
)) then
2779 ("prefix of % attribute must be indefinite generic type");
2784 ("prefix of % attribute must be indefinite generic type");
2787 Set_Etype
(N
, Standard_Boolean
);
2788 end Legal_Formal_Attribute
;
2790 ---------------------------------------------------------------
2791 -- Max_Alignment_For_Allocation_Max_Size_In_Storage_Elements --
2792 ---------------------------------------------------------------
2794 procedure Max_Alignment_For_Allocation_Max_Size_In_Storage_Elements
is
2798 Check_Not_Incomplete_Type
;
2799 Set_Etype
(N
, Universal_Integer
);
2800 end Max_Alignment_For_Allocation_Max_Size_In_Storage_Elements
;
2806 procedure Min_Max
is
2808 -- Attribute can appear as function name in a reduction.
2809 -- Semantic checks are performed later.
2811 if Nkind
(Parent
(N
)) = N_Attribute_Reference
2812 and then Attribute_Name
(Parent
(N
)) = Name_Reduce
2814 Set_Etype
(N
, P_Base_Type
);
2820 Resolve
(E1
, P_Base_Type
);
2821 Resolve
(E2
, P_Base_Type
);
2822 Set_Etype
(N
, P_Base_Type
);
2824 -- Check for comparison on unordered enumeration type
2826 if Bad_Unordered_Enumeration_Reference
(N
, P_Base_Type
) then
2827 Error_Msg_Sloc
:= Sloc
(P_Base_Type
);
2829 ("comparison on unordered enumeration type& declared#?U?",
2834 ------------------------
2835 -- Standard_Attribute --
2836 ------------------------
2838 procedure Standard_Attribute
(Val
: Int
) is
2840 Check_Standard_Prefix
;
2841 Rewrite
(N
, Make_Integer_Literal
(Loc
, Val
));
2843 Set_Is_Static_Expression
(N
, True);
2844 end Standard_Attribute
;
2846 --------------------
2847 -- Uneval_Old_Msg --
2848 --------------------
2850 procedure Uneval_Old_Msg
is
2851 Uneval_Old_Setting
: Character;
2855 -- If from aspect, then Uneval_Old_Setting comes from flags in the
2856 -- N_Aspect_Specification node that corresponds to the attribute.
2858 -- First find the pragma in which we appear (note that at this stage,
2859 -- even if we appeared originally within an aspect specification, we
2860 -- are now within the corresponding pragma).
2864 Prag
:= Parent
(Prag
);
2865 exit when No
(Prag
) or else Nkind
(Prag
) = N_Pragma
;
2868 if Present
(Prag
) then
2869 if Uneval_Old_Accept
(Prag
) then
2870 Uneval_Old_Setting
:= 'A';
2871 elsif Uneval_Old_Warn
(Prag
) then
2872 Uneval_Old_Setting
:= 'W';
2874 Uneval_Old_Setting
:= 'E';
2877 -- If we did not find the pragma, that's odd, just use the setting
2878 -- from Opt.Uneval_Old. Perhaps this is due to a previous error?
2881 Uneval_Old_Setting
:= Opt
.Uneval_Old
;
2884 -- Processing depends on the setting of Uneval_Old
2886 case Uneval_Old_Setting
is
2888 -- ??? In the case where Ada_Version is < Ada_2022 and
2889 -- an illegal 'Old prefix would be legal in Ada_2022,
2890 -- we'd like to call Error_Msg_Ada_2022_Feature.
2891 -- Identifying that case involves some work.
2894 ("prefix of attribute % that is potentially "
2895 & "unevaluated must statically name an entity"
2897 -- further text needed for accuracy if Ada_2022
2898 & (if Ada_Version
>= Ada_2022
2899 and then Attr_Id
= Attribute_Old
2900 then " or be eligible for conditional evaluation"
2901 & " (RM 6.1.1 (27))"
2904 "\using pragma Unevaluated_Use_Of_Old (Allow) will make "
2908 Error_Msg_Name_1
:= Aname
;
2910 ("??prefix of attribute % appears in potentially "
2911 & "unevaluated context, exception may be raised", P
);
2917 raise Program_Error
;
2921 -------------------------
2922 -- Unexpected Argument --
2923 -------------------------
2925 procedure Unexpected_Argument
(En
: Node_Id
) is
2927 Error_Attr
("unexpected argument for % attribute", En
);
2928 end Unexpected_Argument
;
2930 -------------------------------------------------
2931 -- Validate_Non_Static_Attribute_Function_Call --
2932 -------------------------------------------------
2934 -- This function should be moved to Sem_Dist ???
2936 procedure Validate_Non_Static_Attribute_Function_Call
is
2938 if In_Preelaborated_Unit
2939 and then not In_Subprogram_Or_Concurrent_Unit
2941 Flag_Non_Static_Expr
2942 ("non-static function call in preelaborated unit!", N
);
2944 end Validate_Non_Static_Attribute_Function_Call
;
2946 -- Start of processing for Analyze_Attribute
2949 -- Immediate return if unrecognized attribute (already diagnosed by
2950 -- parser, so there is nothing more that we need to do).
2952 if not Is_Attribute_Name
(Aname
) then
2953 raise Bad_Attribute
;
2956 Check_Restriction_No_Use_Of_Attribute
(N
);
2958 -- Deal with Ada 83 issues
2960 if Comes_From_Source
(N
) then
2961 if not Attribute_83
(Attr_Id
) then
2962 if Ada_Version
= Ada_83
and then Comes_From_Source
(N
) then
2963 Error_Msg_Name_1
:= Aname
;
2964 Error_Msg_N
("(Ada 83) attribute% is not standard??", N
);
2967 if Attribute_Impl_Def
(Attr_Id
) then
2968 Check_Restriction
(No_Implementation_Attributes
, N
);
2973 -- Deal with Ada 2005 attributes that are implementation attributes
2974 -- because they appear in a version of Ada before Ada 2005, ditto for
2975 -- Ada 2012 and Ada 2022 attributes appearing in an earlier version.
2977 if (Attribute_05
(Attr_Id
) and then Ada_Version
< Ada_2005
)
2979 (Attribute_12
(Attr_Id
) and then Ada_Version
< Ada_2012
)
2981 (Attribute_22
(Attr_Id
) and then Ada_Version
< Ada_2022
)
2983 Check_Restriction
(No_Implementation_Attributes
, N
);
2986 -- Remote access to subprogram type access attribute reference needs
2987 -- unanalyzed copy for tree transformation. The analyzed copy is used
2988 -- for its semantic information (whether prefix is a remote subprogram
2989 -- name), the unanalyzed copy is used to construct new subtree rooted
2990 -- with N_Aggregate which represents a fat pointer aggregate.
2992 if Aname
= Name_Access
then
2993 Discard_Node
(Copy_Separate_Tree
(N
));
2996 -- Analyze prefix and exit if error in analysis. If the prefix is an
2997 -- incomplete type, use full view if available. Note that there are
2998 -- some attributes for which we do not analyze the prefix, since the
2999 -- prefix is not a normal name, or else needs special handling.
3001 if Aname
/= Name_Elab_Body
and then
3002 Aname
/= Name_Elab_Spec
and then
3003 Aname
/= Name_Elab_Subp_Body
and then
3004 Aname
/= Name_Enabled
and then
3008 P_Type
:= Etype
(P
);
3010 if Is_Entity_Name
(P
)
3011 and then Present
(Entity
(P
))
3012 and then Is_Type
(Entity
(P
))
3014 if Ekind
(Entity
(P
)) = E_Incomplete_Type
then
3015 P_Type
:= Get_Full_View
(P_Type
);
3016 Set_Entity
(P
, P_Type
);
3017 Set_Etype
(P
, P_Type
);
3019 elsif Entity
(P
) = Current_Scope
3020 and then Is_Record_Type
(Entity
(P
))
3022 -- Use of current instance within the type. Verify that if the
3023 -- attribute appears within a constraint, it yields an access
3024 -- type, other uses are illegal.
3032 and then Nkind
(Parent
(Par
)) /= N_Component_Definition
3034 Par
:= Parent
(Par
);
3038 and then Nkind
(Par
) = N_Subtype_Indication
3040 if Attr_Id
/= Attribute_Access
3041 and then Attr_Id
/= Attribute_Unchecked_Access
3042 and then Attr_Id
/= Attribute_Unrestricted_Access
3045 ("in a constraint the current instance can only "
3046 & "be used with an access attribute", N
);
3053 if P_Type
= Any_Type
then
3054 raise Bad_Attribute
;
3057 P_Base_Type
:= Base_Type
(P_Type
);
3060 -- Analyze expressions that may be present, exiting if an error occurs
3067 E1
:= First
(Exprs
);
3069 -- Skip analysis for case of Restriction_Set, we do not expect
3070 -- the argument to be analyzed in this case.
3072 if Aname
/= Name_Restriction_Set
then
3075 -- Check for missing/bad expression (result of previous error)
3077 if No
(E1
) or else Etype
(E1
) = Any_Type
then
3078 raise Bad_Attribute
;
3084 if Present
(E2
) then
3087 if Etype
(E2
) = Any_Type
then
3088 raise Bad_Attribute
;
3091 if Present
(Next
(E2
)) then
3092 Unexpected_Argument
(Next
(E2
));
3097 -- Cases where prefix must be resolvable by itself
3099 if Is_Overloaded
(P
)
3100 and then Aname
/= Name_Access
3101 and then Aname
/= Name_Address
3102 and then Aname
/= Name_Code_Address
3103 and then Aname
/= Name_Result
3104 and then Aname
/= Name_Unchecked_Access
3106 -- The prefix must be resolvable by itself, without reference to the
3107 -- attribute. One case that requires special handling is a prefix
3108 -- that is a function name, where one interpretation may be a
3109 -- parameterless call. Entry attributes are handled specially below.
3111 if Is_Entity_Name
(P
)
3112 and then Aname
not in Name_Count | Name_Caller
3114 Check_Parameterless_Call
(P
);
3117 if Is_Overloaded
(P
) then
3119 -- Ada 2005 (AI-345): Since protected and task types have
3120 -- primitive entry wrappers, the attributes Count, and Caller
3121 -- require a context check
3123 if Aname
in Name_Count | Name_Caller
then
3125 Count
: Natural := 0;
3130 Get_First_Interp
(P
, I
, It
);
3131 while Present
(It
.Nam
) loop
3132 if Comes_From_Source
(It
.Nam
) then
3138 Get_Next_Interp
(I
, It
);
3142 Error_Attr
("ambiguous prefix for % attribute", P
);
3144 Set_Is_Overloaded
(P
, False);
3149 Error_Attr
("ambiguous prefix for % attribute", P
);
3154 -- If the prefix was rewritten as a raise node, then rewrite N as a
3155 -- raise node, to avoid creating inconsistent trees. We still need to
3156 -- perform legality checks on the original tree.
3158 if Nkind
(P
) in N_Raise_xxx_Error
then
3159 Rewrite
(N
, Relocate_Node
(P
));
3160 P
:= Original_Node
(P_Old
);
3163 -- Remaining processing depends on attribute
3167 -- Attributes related to Ada 2012 iterators. Attribute specifications
3168 -- exist for these, but they cannot be queried.
3170 when Attribute_Constant_Indexing
3171 | Attribute_Default_Iterator
3172 | Attribute_Implicit_Dereference
3173 | Attribute_Iterator_Element
3174 | Attribute_Iterable
3175 | Attribute_Variable_Indexing
3177 Error_Msg_N
("illegal attribute", N
);
3179 -- Internal attributes used to deal with Ada 2012 delayed aspects. These
3180 -- were already rejected by the parser. Thus they shouldn't appear here.
3182 when Internal_Attribute_Id
=>
3183 raise Program_Error
;
3189 when Attribute_Abort_Signal
=>
3190 Check_Standard_Prefix
;
3191 Rewrite
(N
, New_Occurrence_Of
(Stand
.Abort_Signal
, Loc
));
3198 when Attribute_Access
=>
3199 Analyze_Access_Attribute
;
3200 Check_Not_Incomplete_Type
;
3206 when Attribute_Address
=>
3209 Check_Not_Incomplete_Type
;
3210 Set_Etype
(N
, RTE
(RE_Address
));
3216 when Attribute_Address_Size
=>
3217 Standard_Attribute
(System_Address_Size
);
3223 when Attribute_Adjacent
3224 | Attribute_Copy_Sign
3225 | Attribute_Remainder
3227 Check_Floating_Point_Type_2
;
3228 Set_Etype
(N
, P_Base_Type
);
3229 Resolve
(E1
, P_Base_Type
);
3230 Resolve
(E2
, P_Base_Type
);
3236 when Attribute_Aft
=>
3237 Check_Fixed_Point_Type_0
;
3238 Set_Etype
(N
, Universal_Integer
);
3244 when Attribute_Alignment
=>
3246 -- Don't we need more checking here, cf Size ???
3249 Check_Not_Incomplete_Type
;
3251 Set_Etype
(N
, Universal_Integer
);
3257 when Attribute_Asm_Input
=>
3258 Check_Asm_Attribute
;
3260 -- The back end may need to take the address of E2
3262 if Is_Entity_Name
(E2
) then
3263 Set_Address_Taken
(Entity
(E2
));
3266 Set_Etype
(N
, RTE
(RE_Asm_Input_Operand
));
3272 when Attribute_Asm_Output
=>
3273 Check_Asm_Attribute
;
3275 if Etype
(E2
) = Any_Type
then
3278 elsif Aname
= Name_Asm_Output
then
3279 if not Is_Variable
(E2
) then
3281 ("second argument for Asm_Output is not variable", E2
);
3285 Note_Possible_Modification
(E2
, Sure
=> True);
3287 -- The back end may need to take the address of E2
3289 if Is_Entity_Name
(E2
) then
3290 Set_Address_Taken
(Entity
(E2
));
3293 Set_Etype
(N
, RTE
(RE_Asm_Output_Operand
));
3295 -----------------------------
3296 -- Atomic_Always_Lock_Free --
3297 -----------------------------
3299 when Attribute_Atomic_Always_Lock_Free
=>
3302 Set_Etype
(N
, Standard_Boolean
);
3308 -- Note: when the base attribute appears in the context of a subtype
3309 -- mark, the analysis is done by Sem_Ch8.Find_Type, rather than by
3310 -- the following circuit.
3312 when Attribute_Base
=> Base
: declare
3320 if Ada_Version
>= Ada_95
3321 and then not Is_Scalar_Type
(Typ
)
3322 and then not Is_Generic_Type
(Typ
)
3324 Error_Attr_P
("prefix of Base attribute must be scalar type");
3326 elsif Sloc
(Typ
) = Standard_Location
3327 and then Base_Type
(Typ
) = Typ
3328 and then Warn_On_Redundant_Constructs
3330 Error_Msg_NE
-- CODEFIX
3331 ("?r?redundant attribute, & is its own base type", N
, Typ
);
3334 Set_Etype
(N
, Base_Type
(Entity
(P
)));
3335 Set_Entity
(N
, Base_Type
(Entity
(P
)));
3336 Rewrite
(N
, New_Occurrence_Of
(Entity
(N
), Loc
));
3344 when Attribute_Bit
=>
3347 if not Is_Object_Reference
(P
) then
3348 Error_Attr_P
("prefix of % attribute must be object");
3350 -- What about the access object cases ???
3356 Set_Etype
(N
, Universal_Integer
);
3362 when Attribute_Bit_Order
=>
3366 if not Is_Record_Type
(P_Type
) then
3367 Error_Attr_P
("prefix of % attribute must be record type");
3370 if Bytes_Big_Endian
xor Reverse_Bit_Order
(P_Type
) then
3372 New_Occurrence_Of
(RTE
(RE_High_Order_First
), Loc
));
3375 New_Occurrence_Of
(RTE
(RE_Low_Order_First
), Loc
));
3378 Set_Etype
(N
, RTE
(RE_Bit_Order
));
3381 -- Reset incorrect indication of staticness
3383 Set_Is_Static_Expression
(N
, False);
3389 -- Note: in generated code, we can have a Bit_Position attribute
3390 -- applied to a (naked) record component (i.e. the prefix is an
3391 -- identifier that references an E_Component or E_Discriminant
3392 -- entity directly, and this is interpreted as expected by Gigi.
3393 -- The following code will not tolerate such usage, but when the
3394 -- expander creates this special case, it marks it as analyzed
3395 -- immediately and sets an appropriate type.
3397 when Attribute_Bit_Position
=>
3398 if Comes_From_Source
(N
) then
3402 Set_Etype
(N
, Universal_Integer
);
3408 when Attribute_Body_Version
=>
3411 Set_Etype
(N
, RTE
(RE_Version_String
));
3417 when Attribute_Callable
3418 | Attribute_Terminated
3421 Set_Etype
(N
, Standard_Boolean
);
3428 when Attribute_Caller
=> Caller
: declare
3435 if Nkind
(P
) in N_Identifier | N_Expanded_Name
then
3438 if not Is_Entry
(Ent
) then
3439 Error_Attr
("invalid entry name", N
);
3443 Error_Attr
("invalid entry name", N
);
3447 for J
in reverse 0 .. Scope_Stack
.Last
loop
3448 S
:= Scope_Stack
.Table
(J
).Entity
;
3450 if S
= Scope
(Ent
) then
3451 Error_Attr
("Caller must appear in matching accept or body", N
);
3457 Set_Etype
(N
, RTE
(RO_AT_Task_Id
));
3464 when Attribute_Ceiling
3466 | Attribute_Fraction
3468 | Attribute_Machine_Rounding
3470 | Attribute_Rounding
3471 | Attribute_Truncation
3472 | Attribute_Unbiased_Rounding
3474 Check_Floating_Point_Type_1
;
3475 Set_Etype
(N
, P_Base_Type
);
3476 Resolve
(E1
, P_Base_Type
);
3482 when Attribute_Class
=>
3483 Check_Restriction
(No_Dispatch
, N
);
3487 -- Applying Class to untagged incomplete type is obsolescent in Ada
3488 -- 2005. Note that we can't test Is_Tagged_Type here on P_Type, since
3489 -- this flag gets set by Find_Type in this situation.
3491 if Restriction_Check_Required
(No_Obsolescent_Features
)
3492 and then Ada_Version
>= Ada_2005
3493 and then Ekind
(P_Type
) = E_Incomplete_Type
3496 DN
: constant Node_Id
:= Declaration_Node
(P_Type
);
3498 if Nkind
(DN
) = N_Incomplete_Type_Declaration
3499 and then not Tagged_Present
(DN
)
3501 Check_Restriction
(No_Obsolescent_Features
, P
);
3510 when Attribute_Code_Address
=>
3513 if Nkind
(P
) = N_Attribute_Reference
3514 and then Attribute_Name
(P
) in Name_Elab_Body | Name_Elab_Spec
3518 elsif not Is_Entity_Name
(P
)
3519 or else (Ekind
(Entity
(P
)) /= E_Function
3521 Ekind
(Entity
(P
)) /= E_Procedure
)
3523 Error_Attr
("invalid prefix for % attribute", P
);
3524 Set_Address_Taken
(Entity
(P
));
3526 -- Issue an error if the prefix denotes an eliminated subprogram
3529 Check_For_Eliminated_Subprogram
(P
, Entity
(P
));
3532 Set_Etype
(N
, RTE
(RE_Address
));
3534 ----------------------
3535 -- Compiler_Version --
3536 ----------------------
3538 when Attribute_Compiler_Version
=>
3540 Check_Standard_Prefix
;
3541 Rewrite
(N
, Make_String_Literal
(Loc
, "GNAT " & Gnat_Version_String
));
3542 Analyze_And_Resolve
(N
, Standard_String
);
3543 Set_Is_Static_Expression
(N
, True);
3545 --------------------
3546 -- Component_Size --
3547 --------------------
3549 when Attribute_Component_Size
=>
3551 Set_Etype
(N
, Universal_Integer
);
3553 -- Note: unlike other array attributes, unconstrained arrays are OK
3555 if Is_Array_Type
(P_Type
) and then not Is_Constrained
(P_Type
) then
3565 when Attribute_Compose
3566 | Attribute_Leading_Part
3569 Check_Floating_Point_Type_2
;
3570 Set_Etype
(N
, P_Base_Type
);
3571 Resolve
(E1
, P_Base_Type
);
3572 Resolve
(E2
, Any_Integer
);
3578 when Attribute_Constrained
=>
3580 Set_Etype
(N
, Standard_Boolean
);
3582 -- Case from RM J.4(2) of constrained applied to private type
3584 if Is_Entity_Name
(P
) and then Is_Type
(Entity
(P
)) then
3585 Check_Restriction
(No_Obsolescent_Features
, P
);
3587 if Warn_On_Obsolescent_Feature
then
3589 ("constrained for private type is an obsolescent feature "
3590 & "(RM J.4)?j?", N
);
3593 -- If we are within an instance, the attribute must be legal
3594 -- because it was valid in the generic unit. Ditto if this is
3595 -- an inlining of a function declared in an instance.
3597 if In_Instance
or else In_Inlined_Body
then
3600 -- For sure OK if we have a real private type itself, but must
3601 -- be completed, cannot apply Constrained to incomplete type.
3603 elsif Is_Private_Type
(Entity
(P
)) then
3605 -- Note: this is one of the Annex J features that does not
3606 -- generate a warning from -gnatwj, since in fact it seems
3607 -- very useful, and is used in the GNAT runtime.
3609 Check_Not_Incomplete_Type
;
3613 -- Normal (non-obsolescent case) of application to object or value of
3614 -- a discriminated type.
3617 -- AI12-0068: In a type or subtype aspect, a prefix denoting the
3618 -- current instance of the (sub)type is defined to be a value,
3619 -- not an object, so the Constrained attribute is always True
3620 -- (see RM 8.6(18/5) and RM 3.7.2(3/5)). We issue a warning about
3621 -- this unintuitive result, to help avoid confusion.
3623 if Is_Current_Instance_Reference_In_Type_Aspect
(P
) then
3624 Error_Msg_Name_1
:= Aname
;
3626 ("current instance attribute % in subtype aspect always " &
3630 Check_Object_Reference
(P
);
3633 -- If N does not come from source, then we allow the
3634 -- the attribute prefix to be of a private type whose
3635 -- full type has discriminants. This occurs in cases
3636 -- involving expanded calls to stream attributes.
3638 if not Comes_From_Source
(N
) then
3639 P_Type
:= Underlying_Type
(P_Type
);
3642 -- Must have discriminants or be an access type designating a type
3643 -- with discriminants. If it is a class-wide type it has unknown
3646 if Has_Discriminants
(P_Type
)
3647 or else Has_Unknown_Discriminants
(P_Type
)
3649 (Is_Access_Type
(P_Type
)
3650 and then Has_Discriminants
(Designated_Type
(P_Type
)))
3654 -- The rule given in 3.7.2 is part of static semantics, but the
3655 -- intent is clearly that it be treated as a legality rule, and
3656 -- rechecked in the visible part of an instance. Nevertheless
3657 -- the intent also seems to be it should legally apply to the
3658 -- actual of a formal with unknown discriminants, regardless of
3659 -- whether the actual has discriminants, in which case the value
3660 -- of the attribute is determined using the J.4 rules. This choice
3661 -- seems the most useful, and is compatible with existing tests.
3663 elsif In_Instance
then
3666 -- Also allow an object of a generic type if extensions allowed
3667 -- and allow this for any type at all.
3669 elsif (Is_Generic_Type
(P_Type
)
3670 or else Is_Generic_Actual_Type
(P_Type
))
3671 and then Extensions_Allowed
3677 -- Fall through if bad prefix
3680 ("prefix of % attribute must be object of discriminated type");
3686 -- Shares processing with Adjacent attribute
3692 when Attribute_Count
=> Count
: declare
3700 if Nkind
(P
) in N_Identifier | N_Expanded_Name
then
3703 if Ekind
(Ent
) /= E_Entry
then
3704 Error_Attr
("invalid entry name", N
);
3707 elsif Nkind
(P
) = N_Indexed_Component
then
3708 if not Is_Entity_Name
(Prefix
(P
))
3709 or else No
(Entity
(Prefix
(P
)))
3710 or else Ekind
(Entity
(Prefix
(P
))) /= E_Entry_Family
3712 if Nkind
(Prefix
(P
)) = N_Selected_Component
3713 and then Present
(Entity
(Selector_Name
(Prefix
(P
))))
3714 and then Ekind
(Entity
(Selector_Name
(Prefix
(P
)))) =
3718 ("attribute % must apply to entry of current task", P
);
3721 Error_Attr
("invalid entry family name", P
);
3726 Ent
:= Entity
(Prefix
(P
));
3729 elsif Nkind
(P
) = N_Selected_Component
3730 and then Present
(Entity
(Selector_Name
(P
)))
3731 and then Ekind
(Entity
(Selector_Name
(P
))) = E_Entry
3734 ("attribute % must apply to entry of current task", P
);
3737 Error_Attr
("invalid entry name", N
);
3741 for J
in reverse 0 .. Scope_Stack
.Last
loop
3742 S
:= Scope_Stack
.Table
(J
).Entity
;
3744 if S
= Scope
(Ent
) then
3745 if Nkind
(P
) = N_Expanded_Name
then
3746 Tsk
:= Entity
(Prefix
(P
));
3748 -- The prefix denotes either the task type, or else a
3749 -- single task whose task type is being analyzed.
3751 if (Is_Type
(Tsk
) and then Tsk
= S
)
3752 or else (not Is_Type
(Tsk
)
3753 and then Etype
(Tsk
) = S
3754 and then not (Comes_From_Source
(S
)))
3759 ("attribute % must apply to entry of current task", N
);
3765 elsif Ekind
(Scope
(Ent
)) in Task_Kind
3766 and then Ekind
(S
) not in E_Block
3771 Error_Attr
("attribute % cannot appear in inner unit", N
);
3773 elsif Ekind
(Scope
(Ent
)) = E_Protected_Type
3774 and then not Has_Completion
(Scope
(Ent
))
3776 Error_Attr
("attribute % can only be used inside body", N
);
3780 if Is_Overloaded
(P
) then
3782 Index
: Interp_Index
;
3786 Get_First_Interp
(P
, Index
, It
);
3787 while Present
(It
.Nam
) loop
3788 if It
.Nam
= Ent
then
3791 -- Ada 2005 (AI-345): Do not consider primitive entry
3792 -- wrappers generated for task or protected types.
3794 elsif Ada_Version
>= Ada_2005
3795 and then not Comes_From_Source
(It
.Nam
)
3800 Error_Attr
("ambiguous entry name", N
);
3803 Get_Next_Interp
(Index
, It
);
3808 Set_Etype
(N
, Universal_Integer
);
3811 -----------------------
3812 -- Default_Bit_Order --
3813 -----------------------
3815 when Attribute_Default_Bit_Order
=> Default_Bit_Order
: declare
3816 Target_Default_Bit_Order
: System
.Bit_Order
;
3819 Check_Standard_Prefix
;
3821 if Bytes_Big_Endian
then
3822 Target_Default_Bit_Order
:= System
.High_Order_First
;
3824 Target_Default_Bit_Order
:= System
.Low_Order_First
;
3828 Make_Integer_Literal
(Loc
,
3829 UI_From_Int
(System
.Bit_Order
'Pos (Target_Default_Bit_Order
))));
3831 Set_Etype
(N
, Universal_Integer
);
3832 Set_Is_Static_Expression
(N
);
3833 end Default_Bit_Order
;
3835 ----------------------------------
3836 -- Default_Scalar_Storage_Order --
3837 ----------------------------------
3839 when Attribute_Default_Scalar_Storage_Order
=> Default_SSO
: declare
3840 RE_Default_SSO
: RE_Id
;
3843 Check_Standard_Prefix
;
3845 case Opt
.Default_SSO
is
3847 if Bytes_Big_Endian
then
3848 RE_Default_SSO
:= RE_High_Order_First
;
3850 RE_Default_SSO
:= RE_Low_Order_First
;
3854 RE_Default_SSO
:= RE_High_Order_First
;
3857 RE_Default_SSO
:= RE_Low_Order_First
;
3860 raise Program_Error
;
3863 Rewrite
(N
, New_Occurrence_Of
(RTE
(RE_Default_SSO
), Loc
));
3870 when Attribute_Definite
=>
3871 Legal_Formal_Attribute
;
3877 when Attribute_Delta
=>
3878 Check_Fixed_Point_Type_0
;
3879 Set_Etype
(N
, Universal_Real
);
3885 when Attribute_Denorm
3886 | Attribute_Signed_Zeros
3888 Check_Floating_Point_Type_0
;
3889 Set_Etype
(N
, Standard_Boolean
);
3895 when Attribute_Deref
=>
3898 Resolve
(E1
, RTE
(RE_Address
));
3899 Set_Etype
(N
, P_Type
);
3901 ---------------------
3902 -- Descriptor_Size --
3903 ---------------------
3905 when Attribute_Descriptor_Size
=>
3908 if not Is_Entity_Name
(P
) or else not Is_Type
(Entity
(P
)) then
3909 Error_Attr_P
("prefix of attribute % must denote a type");
3912 Set_Etype
(N
, Universal_Integer
);
3918 when Attribute_Digits
=>
3922 if not Is_Floating_Point_Type
(P_Type
)
3923 and then not Is_Decimal_Fixed_Point_Type
(P_Type
)
3926 ("prefix of % attribute must be float or decimal type");
3929 Set_Etype
(N
, Universal_Integer
);
3935 -- Also handles processing for Elab_Spec and Elab_Subp_Body
3937 when Attribute_Elab_Body
3938 | Attribute_Elab_Spec
3939 | Attribute_Elab_Subp_Body
3942 Check_Unit_Name
(P
);
3943 Set_Etype
(N
, Standard_Void_Type
);
3945 -- We have to manually call the expander in this case to get
3946 -- the necessary expansion (normally attributes that return
3947 -- entities are not expanded).
3955 -- Shares processing with Elab_Body attribute
3961 when Attribute_Elaborated
=>
3963 Check_Unit_Name
(P
);
3964 Set_Etype
(N
, Standard_Boolean
);
3971 | Attribute_Machine_Emax
3972 | Attribute_Machine_Emin
3973 | Attribute_Machine_Mantissa
3974 | Attribute_Model_Emin
3975 | Attribute_Model_Mantissa
3976 | Attribute_Safe_Emax
3978 Check_Floating_Point_Type_0
;
3979 Set_Etype
(N
, Universal_Integer
);
3985 when Attribute_Enabled
=>
3986 Check_Either_E0_Or_E1
;
3988 if Present
(E1
) then
3989 if not Is_Entity_Name
(E1
) or else No
(Entity
(E1
)) then
3990 Error_Msg_N
("entity name expected for Enabled attribute", E1
);
3995 if Nkind
(P
) /= N_Identifier
then
3996 Error_Msg_N
("identifier expected (check name)", P
);
3997 elsif Get_Check_Id
(Chars
(P
)) = No_Check_Id
then
3998 Error_Msg_N
("& is not a recognized check name", P
);
4001 Set_Etype
(N
, Standard_Boolean
);
4007 when Attribute_Enum_Rep
=>
4009 -- T'Enum_Rep (X) case
4011 if Present
(E1
) then
4013 Check_Discrete_Type
;
4014 Resolve
(E1
, P_Base_Type
);
4016 -- X'Enum_Rep case. X must be an object or enumeration literal
4017 -- (including an attribute reference), and it must be of a
4021 ((Is_Object_Reference
(P
)
4024 and then Ekind
(Entity
(P
)) = E_Enumeration_Literal
)
4025 or else Nkind
(P
) = N_Attribute_Reference
)
4026 and then Is_Discrete_Type
(Etype
(P
)))
4028 Error_Attr_P
("prefix of % attribute must be discrete object");
4031 Set_Etype
(N
, Universal_Integer
);
4037 when Attribute_Enum_Val
=>
4041 if not Is_Enumeration_Type
(P_Type
) then
4042 Error_Attr_P
("prefix of % attribute must be enumeration type");
4045 -- If the enumeration type has a standard representation, the effect
4046 -- is the same as 'Val, so rewrite the attribute as a 'Val.
4048 if not Has_Non_Standard_Rep
(P_Base_Type
) then
4050 Make_Attribute_Reference
(Loc
,
4051 Prefix
=> Relocate_Node
(Prefix
(N
)),
4052 Attribute_Name
=> Name_Val
,
4053 Expressions
=> New_List
(Relocate_Node
(E1
))));
4054 Analyze_And_Resolve
(N
, P_Base_Type
);
4056 -- Non-standard representation case (enumeration with holes)
4060 Resolve
(E1
, Any_Integer
);
4061 Set_Etype
(N
, P_Base_Type
);
4068 when Attribute_Epsilon
4069 | Attribute_Model_Epsilon
4070 | Attribute_Model_Small
4071 | Attribute_Safe_First
4072 | Attribute_Safe_Last
4074 Check_Floating_Point_Type_0
;
4075 Set_Etype
(N
, Universal_Real
);
4081 when Attribute_Exponent
=>
4082 Check_Floating_Point_Type_1
;
4083 Set_Etype
(N
, Universal_Integer
);
4084 Resolve
(E1
, P_Base_Type
);
4090 when Attribute_External_Tag
=>
4094 Set_Etype
(N
, Standard_String
);
4096 if not Is_Tagged_Type
(P_Type
) then
4097 Error_Attr_P
("prefix of % attribute must be tagged");
4104 when Attribute_Fast_Math
=>
4105 Check_Standard_Prefix
;
4106 Rewrite
(N
, New_Occurrence_Of
(Boolean_Literals
(Fast_Math
), Loc
));
4108 -----------------------
4109 -- Finalization_Size --
4110 -----------------------
4112 when Attribute_Finalization_Size
=>
4115 -- The prefix denotes an object
4117 if Is_Object_Reference
(P
) then
4118 Check_Object_Reference
(P
);
4120 -- The prefix denotes a type
4122 elsif Is_Entity_Name
(P
) and then Is_Type
(Entity
(P
)) then
4124 Check_Not_Incomplete_Type
;
4126 -- Attribute 'Finalization_Size is not defined for class-wide
4127 -- types because it is not possible to know statically whether
4128 -- a definite type will have controlled components or not.
4130 if Is_Class_Wide_Type
(Etype
(P
)) then
4132 ("prefix of % attribute cannot denote a class-wide type");
4135 -- The prefix denotes an illegal construct
4139 ("prefix of % attribute must be a definite type or an object");
4142 Set_Etype
(N
, Universal_Integer
);
4148 when Attribute_First
4151 Check_Array_Or_Scalar_Type
;
4152 Bad_Attribute_For_Predicate
;
4158 when Attribute_First_Bit
4159 | Attribute_Last_Bit
4160 | Attribute_Position
4163 Set_Etype
(N
, Universal_Integer
);
4169 when Attribute_First_Valid
4170 | Attribute_Last_Valid
4172 Check_First_Last_Valid
;
4173 Set_Etype
(N
, P_Type
);
4179 when Attribute_Fixed_Value
=>
4180 Check_Fixed_Point_Type
;
4182 Resolve
(E1
, Any_Integer
);
4183 Set_Etype
(N
, P_Base_Type
);
4189 -- Shares processing with Ceiling attribute
4195 when Attribute_Fore
=>
4196 Check_Fixed_Point_Type_0
;
4197 Set_Etype
(N
, Universal_Integer
);
4203 -- Shares processing with Ceiling attribute
4209 when Attribute_From_Any
=>
4211 Check_PolyORB_Attribute
;
4212 Set_Etype
(N
, P_Base_Type
);
4214 -----------------------
4215 -- Has_Access_Values --
4216 -----------------------
4218 when Attribute_Has_Access_Values
4219 | Attribute_Has_Tagged_Values
4223 Set_Etype
(N
, Standard_Boolean
);
4225 ----------------------
4226 -- Has_Same_Storage --
4227 ----------------------
4229 when Attribute_Has_Same_Storage
=>
4232 -- The arguments must be objects of any type
4234 Analyze_And_Resolve
(P
);
4235 Analyze_And_Resolve
(E1
);
4236 Check_Object_Reference
(P
);
4237 Check_Object_Reference
(E1
);
4238 Set_Etype
(N
, Standard_Boolean
);
4240 -----------------------
4241 -- Has_Tagged_Values --
4242 -----------------------
4244 -- Shares processing with Has_Access_Values attribute
4246 -----------------------
4247 -- Has_Discriminants --
4248 -----------------------
4250 when Attribute_Has_Discriminants
=>
4251 Legal_Formal_Attribute
;
4257 when Attribute_Identity
=>
4261 if Etype
(P
) = Standard_Exception_Type
then
4262 Set_Etype
(N
, RTE
(RE_Exception_Id
));
4264 -- Ada 2005 (AI-345): Attribute 'Identity may be applied to task
4265 -- interface class-wide types.
4267 elsif Is_Task_Type
(Etype
(P
))
4268 or else (Is_Access_Type
(Etype
(P
))
4269 and then Is_Task_Type
(Designated_Type
(Etype
(P
))))
4270 or else (Ada_Version
>= Ada_2005
4271 and then Ekind
(Etype
(P
)) = E_Class_Wide_Type
4272 and then Is_Interface
(Etype
(P
))
4273 and then Is_Task_Interface
(Etype
(P
)))
4276 Set_Etype
(N
, RTE
(RO_AT_Task_Id
));
4279 if Ada_Version
>= Ada_2005
then
4281 ("prefix of % attribute must be an exception, a task or a "
4282 & "task interface class-wide object");
4285 ("prefix of % attribute must be a task or an exception");
4293 when Attribute_Image
=>
4294 if Is_Real_Type
(P_Type
) then
4295 if Ada_Version
= Ada_83
and then Comes_From_Source
(N
) then
4296 Error_Msg_Name_1
:= Aname
;
4298 ("(Ada 83) % attribute not allowed for real types", N
);
4302 Analyze_Image_Attribute
(Standard_String
);
4308 when Attribute_Img
=>
4309 Analyze_Image_Attribute
(Standard_String
);
4315 when Attribute_Initialized
=>
4318 if Comes_From_Source
(N
) then
4320 -- This attribute be prefixed with references to objects or
4321 -- values (such as a current instance value given within a type
4322 -- or subtype aspect).
4324 if not Is_Object_Reference
(P
)
4325 and then not Is_Current_Instance_Reference_In_Type_Aspect
(P
)
4327 Error_Attr_P
("prefix of % attribute must be object");
4331 Set_Etype
(N
, Standard_Boolean
);
4337 when Attribute_Input
=>
4339 Check_Stream_Attribute
(TSS_Stream_Input
);
4340 Set_Etype
(N
, P_Base_Type
);
4346 when Attribute_Integer_Value
=>
4349 Resolve
(E1
, Any_Fixed
);
4351 -- Signal an error if argument type is not a specific fixed-point
4352 -- subtype. An error has been signalled already if the argument
4353 -- was not of a fixed-point type.
4355 if Etype
(E1
) = Any_Fixed
and then not Error_Posted
(E1
) then
4356 Error_Attr
("argument of % must be of a fixed-point type", E1
);
4359 Set_Etype
(N
, P_Base_Type
);
4365 when Attribute_Invalid_Value
=>
4368 Set_Etype
(N
, P_Base_Type
);
4369 Invalid_Value_Used
:= True;
4375 when Attribute_Large
4377 | Attribute_Safe_Large
4378 | Attribute_Safe_Small
4382 Set_Etype
(N
, Universal_Real
);
4388 -- Shares processing with First attribute
4394 -- Shares processing with First_Bit attribute
4400 -- Shares processing with First_Valid attribute
4406 -- Shares processing with Compose attribute
4412 when Attribute_Length
=>
4414 Set_Etype
(N
, Universal_Integer
);
4420 when Attribute_Library_Level
=>
4423 if not Is_Entity_Name
(P
) then
4424 Error_Attr_P
("prefix of % attribute must be an entity name");
4427 if not Inside_A_Generic
then
4428 Set_Boolean_Result
(N
,
4429 Is_Library_Level_Entity
(Entity
(P
)));
4432 Set_Etype
(N
, Standard_Boolean
);
4438 when Attribute_Lock_Free
=>
4440 Set_Etype
(N
, Standard_Boolean
);
4442 if not Is_Protected_Type
(P_Type
) then
4444 ("prefix of % attribute must be a protected object");
4451 when Attribute_Loop_Entry
=> Loop_Entry
: declare
4452 procedure Check_References_In_Prefix
(Loop_Id
: Entity_Id
);
4453 -- Inspect the prefix for any uses of entities declared within the
4454 -- related loop. Loop_Id denotes the loop identifier.
4456 --------------------------------
4457 -- Check_References_In_Prefix --
4458 --------------------------------
4460 procedure Check_References_In_Prefix
(Loop_Id
: Entity_Id
) is
4461 Loop_Decl
: constant Node_Id
:= Label_Construct
(Parent
(Loop_Id
));
4463 function Check_Reference
(Nod
: Node_Id
) return Traverse_Result
;
4464 -- Determine whether a reference mentions an entity declared
4465 -- within the related loop.
4467 function Declared_Within
(Nod
: Node_Id
) return Boolean;
4468 -- Determine whether Nod appears in the subtree of Loop_Decl but
4469 -- not within the subtree of the prefix P itself.
4471 ---------------------
4472 -- Check_Reference --
4473 ---------------------
4475 function Check_Reference
(Nod
: Node_Id
) return Traverse_Result
is
4477 if Nkind
(Nod
) = N_Identifier
4478 and then Present
(Entity
(Nod
))
4479 and then Declared_Within
(Declaration_Node
(Entity
(Nod
)))
4482 ("prefix of attribute % cannot reference local entities",
4488 end Check_Reference
;
4490 procedure Check_References
is new Traverse_Proc
(Check_Reference
);
4492 ---------------------
4493 -- Declared_Within --
4494 ---------------------
4496 function Declared_Within
(Nod
: Node_Id
) return Boolean is
4501 while Present
(Stmt
) loop
4502 if Stmt
= Loop_Decl
then
4508 -- Prevent the search from going too far
4510 elsif Is_Body_Or_Package_Declaration
(Stmt
) then
4514 Stmt
:= Parent
(Stmt
);
4518 end Declared_Within
;
4520 -- Start of processing for Check_Prefix_For_Local_References
4523 Check_References
(P
);
4524 end Check_References_In_Prefix
;
4528 Context
: constant Node_Id
:= Parent
(N
);
4530 Encl_Loop
: Node_Id
:= Empty
;
4531 Encl_Prag
: Node_Id
:= Empty
;
4532 Loop_Id
: Entity_Id
:= Empty
;
4536 -- Start of processing for Loop_Entry
4541 -- Set the type of the attribute now to ensure the successful
4542 -- continuation of analysis even if the attribute is misplaced.
4544 Set_Etype
(Attr
, P_Type
);
4546 -- Attribute 'Loop_Entry may appear in several flavors:
4548 -- * Prefix'Loop_Entry - in this form, the attribute applies to the
4549 -- nearest enclosing loop.
4551 -- * Prefix'Loop_Entry (Expr) - depending on what Expr denotes, the
4552 -- attribute may be related to a loop denoted by label Expr or
4553 -- the prefix may denote an array object and Expr may act as an
4554 -- indexed component.
4556 -- * Prefix'Loop_Entry (Expr1, ..., ExprN) - the attribute applies
4557 -- to the nearest enclosing loop, all expressions are part of
4558 -- an indexed component.
4560 -- * Prefix'Loop_Entry (Expr) (...) (...) - depending on what Expr
4561 -- denotes, the attribute may be related to a loop denoted by
4562 -- label Expr or the prefix may denote a multidimensional array
4563 -- array object and Expr along with the rest of the expressions
4564 -- may act as indexed components.
4566 -- Regardless of variations, the attribute reference does not have an
4567 -- expression list. Instead, all available expressions are stored as
4568 -- indexed components.
4570 -- When the attribute is part of an indexed component, find the first
4571 -- expression as it will determine the semantics of 'Loop_Entry.
4573 -- If the attribute is itself an index in an indexed component, i.e.
4574 -- a member of a list, the context itself is not relevant (the code
4575 -- below would lead to an infinite loop) and the attribute applies
4576 -- to the enclosing loop.
4578 if Nkind
(Context
) = N_Indexed_Component
4579 and then not Is_List_Member
(N
)
4581 E1
:= First
(Expressions
(Context
));
4584 -- The attribute reference appears in the following form:
4586 -- Prefix'Loop_Entry (Exp1, Expr2, ..., ExprN) [(...)]
4588 -- In this case, the loop name is omitted and no rewriting is
4591 if Present
(E2
) then
4594 -- The form of the attribute is:
4596 -- Prefix'Loop_Entry (Expr) [(...)]
4598 -- If Expr denotes a loop entry, the whole attribute and indexed
4599 -- component will have to be rewritten to reflect this relation.
4602 pragma Assert
(Present
(E1
));
4604 -- Do not expand the expression as it may have side effects.
4605 -- Simply preanalyze to determine whether it is a loop name or
4608 Preanalyze_And_Resolve
(E1
);
4610 if Is_Entity_Name
(E1
)
4611 and then Present
(Entity
(E1
))
4612 and then Ekind
(Entity
(E1
)) = E_Loop
4614 Loop_Id
:= Entity
(E1
);
4616 -- Transform the attribute and enclosing indexed component
4618 Set_Expressions
(N
, Expressions
(Context
));
4619 Rewrite
(Context
, N
);
4620 Set_Etype
(Context
, P_Type
);
4627 -- The prefix must denote an object
4629 if not Is_Object_Reference
(P
) then
4630 Error_Attr_P
("prefix of attribute % must denote an object");
4633 -- The prefix cannot be of a limited type because the expansion of
4634 -- Loop_Entry must create a constant initialized by the evaluated
4637 if Is_Limited_View
(Etype
(P
)) then
4638 Error_Attr_P
("prefix of attribute % cannot be limited");
4641 -- Climb the parent chain to verify the location of the attribute and
4642 -- find the enclosing loop.
4645 while Present
(Stmt
) loop
4647 -- Locate the corresponding enclosing pragma. Note that in the
4648 -- case of Assert[And_Cut] and Assume, we have already checked
4649 -- that the pragma appears in an appropriate loop location.
4651 if Nkind
(Original_Node
(Stmt
)) = N_Pragma
4653 Pragma_Name_Unmapped
(Original_Node
(Stmt
))
4654 in Name_Loop_Invariant
4657 | Name_Assert_And_Cut
4660 Encl_Prag
:= Original_Node
(Stmt
);
4662 -- Locate the enclosing loop (if any). Note that Ada 2012 array
4663 -- iteration may be expanded into several nested loops, we are
4664 -- interested in the outermost one which has the loop identifier,
4665 -- and comes from source.
4667 elsif Nkind
(Stmt
) = N_Loop_Statement
4668 and then Present
(Identifier
(Stmt
))
4669 and then Comes_From_Source
(Original_Node
(Stmt
))
4670 and then Nkind
(Original_Node
(Stmt
)) = N_Loop_Statement
4674 -- The original attribute reference may lack a loop name. Use
4675 -- the name of the enclosing loop because it is the related
4678 if No
(Loop_Id
) then
4679 Loop_Id
:= Entity
(Identifier
(Encl_Loop
));
4684 -- Prevent the search from going too far
4686 elsif Is_Body_Or_Package_Declaration
(Stmt
) then
4690 Stmt
:= Parent
(Stmt
);
4693 -- Loop_Entry must appear within a Loop_Assertion pragma (Assert,
4694 -- Assert_And_Cut, Assume count as loop assertion pragmas for this
4695 -- purpose if they appear in an appropriate location in a loop,
4696 -- which was already checked by the top level pragma circuit).
4698 -- Loop_Entry also denotes a value and as such can appear within an
4699 -- expression that is an argument for another loop aspect. In that
4700 -- case it will have been expanded into the corresponding assignment.
4703 and then Nkind
(Parent
(N
)) = N_Assignment_Statement
4704 and then not Comes_From_Source
(Parent
(N
))
4708 elsif No
(Encl_Prag
) then
4709 Error_Attr
("attribute% must appear within appropriate pragma", N
);
4712 -- A Loop_Entry that applies to a given loop statement must not
4713 -- appear within a body of accept statement, if this construct is
4714 -- itself enclosed by the given loop statement.
4716 for Index
in reverse 0 .. Scope_Stack
.Last
loop
4717 Scop
:= Scope_Stack
.Table
(Index
).Entity
;
4719 if Ekind
(Scop
) = E_Loop
and then Scop
= Loop_Id
then
4721 elsif Ekind
(Scop
) in E_Block | E_Loop | E_Return_Statement
then
4725 ("attribute % cannot appear in body or accept statement", N
);
4730 -- The prefix cannot mention entities declared within the related
4731 -- loop because they will not be visible once the prefix is moved
4732 -- outside the loop.
4734 Check_References_In_Prefix
(Loop_Id
);
4736 -- The prefix must statically name an object if the pragma does not
4737 -- apply to the innermost enclosing loop statement, or if it appears
4738 -- within a potentially unevaluated expression.
4740 if Is_Entity_Name
(P
)
4741 or else Nkind
(Parent
(P
)) = N_Object_Renaming_Declaration
4742 or else Statically_Names_Object
(P
)
4746 elsif Present
(Encl_Loop
)
4747 and then Entity
(Identifier
(Encl_Loop
)) /= Loop_Id
4750 ("prefix of attribute % that applies to outer loop must denote "
4753 elsif Is_Potentially_Unevaluated
(P
) then
4757 -- Replace the Loop_Entry attribute reference by its prefix if the
4758 -- related pragma is ignored. This transformation is OK with respect
4759 -- to typing because Loop_Entry's type is that of its prefix. This
4760 -- early transformation also avoids the generation of a useless loop
4763 if Present
(Encl_Prag
) and then Is_Ignored
(Encl_Prag
) then
4764 Rewrite
(N
, Relocate_Node
(P
));
4765 Preanalyze_And_Resolve
(N
);
4768 Preanalyze_And_Resolve
(P
);
4776 -- Shares processing with Ceiling attribute
4782 -- Shares processing with Emax attribute
4788 -- Shares processing with Emax attribute
4790 ----------------------
4791 -- Machine_Mantissa --
4792 ----------------------
4794 -- Shares processing with Emax attribute
4796 -----------------------
4797 -- Machine_Overflows --
4798 -----------------------
4800 when Attribute_Machine_Overflows
4801 | Attribute_Machine_Rounds
4805 Set_Etype
(N
, Standard_Boolean
);
4811 when Attribute_Machine_Radix
4812 | Attribute_Mantissa
4816 Set_Etype
(N
, Universal_Integer
);
4818 ----------------------
4819 -- Machine_Rounding --
4820 ----------------------
4822 -- Shares processing with Ceiling attribute
4824 --------------------
4825 -- Machine_Rounds --
4826 --------------------
4828 -- Shares processing with Machine_Overflows attribute
4834 when Attribute_Machine_Size
4835 | Attribute_Object_Size
4836 | Attribute_Value_Size
4840 Check_Not_Incomplete_Type
;
4841 Set_Etype
(N
, Universal_Integer
);
4847 -- Shares processing with Machine_Radix attribute
4853 when Attribute_Max
=>
4856 ----------------------------------
4857 -- Max_Alignment_For_Allocation --
4858 ----------------------------------
4860 when Attribute_Max_Size_In_Storage_Elements
=>
4861 Max_Alignment_For_Allocation_Max_Size_In_Storage_Elements
;
4863 ----------------------
4864 -- Max_Integer_Size --
4865 ----------------------
4867 when Attribute_Max_Integer_Size
=>
4868 Standard_Attribute
(System_Max_Integer_Size
);
4870 ----------------------------------
4871 -- Max_Size_In_Storage_Elements --
4872 ----------------------------------
4874 when Attribute_Max_Alignment_For_Allocation
=>
4875 Max_Alignment_For_Allocation_Max_Size_In_Storage_Elements
;
4877 -----------------------
4878 -- Maximum_Alignment --
4879 -----------------------
4881 when Attribute_Maximum_Alignment
=>
4882 Standard_Attribute
(Ttypes
.Maximum_Alignment
);
4884 --------------------
4885 -- Mechanism_Code --
4886 --------------------
4888 when Attribute_Mechanism_Code
=>
4889 if not Is_Entity_Name
(P
)
4890 or else not Is_Subprogram
(Entity
(P
))
4892 Error_Attr_P
("prefix of % attribute must be subprogram");
4895 Check_Either_E0_Or_E1
;
4897 if Present
(E1
) then
4898 Resolve
(E1
, Any_Integer
);
4899 Set_Etype
(E1
, Standard_Integer
);
4901 if not Is_OK_Static_Expression
(E1
) then
4902 Flag_Non_Static_Expr
4903 ("expression for parameter number must be static!", E1
);
4906 elsif UI_To_Int
(Intval
(E1
)) > Number_Formals
(Entity
(P
))
4907 or else Intval
(E1
) < 0
4909 Error_Attr
("invalid parameter number for % attribute", E1
);
4913 Set_Etype
(N
, Universal_Integer
);
4919 when Attribute_Min
=>
4926 when Attribute_Mod
=>
4928 -- Note: this attribute is only allowed in Ada 2005 mode, but
4929 -- we do not need to test that here, since Mod is only recognized
4930 -- as an attribute name in Ada 2005 mode during the parse.
4933 Check_Modular_Integer_Type
;
4934 Resolve
(E1
, Any_Integer
);
4935 Set_Etype
(N
, P_Base_Type
);
4941 -- Shares processing with Ceiling attribute
4947 -- Shares processing with Emax attribute
4953 -- Shares processing with Epsilon attribute
4955 --------------------
4956 -- Model_Mantissa --
4957 --------------------
4959 -- Shares processing with Emax attribute
4965 -- Shares processing with Epsilon attribute
4971 when Attribute_Modulus
=>
4973 Check_Modular_Integer_Type
;
4974 Set_Etype
(N
, Universal_Integer
);
4976 --------------------
4977 -- Null_Parameter --
4978 --------------------
4980 when Attribute_Null_Parameter
=> Null_Parameter
: declare
4981 Parnt
: constant Node_Id
:= Parent
(N
);
4982 GParnt
: constant Node_Id
:= Parent
(Parnt
);
4984 procedure Bad_Null_Parameter
(Msg
: String);
4985 -- Used if bad Null parameter attribute node is found. Issues
4986 -- given error message, and also sets the type to Any_Type to
4987 -- avoid blowups later on from dealing with a junk node.
4989 procedure Must_Be_Imported
(Proc_Ent
: Entity_Id
);
4990 -- Called to check that Proc_Ent is imported subprogram
4992 ------------------------
4993 -- Bad_Null_Parameter --
4994 ------------------------
4996 procedure Bad_Null_Parameter
(Msg
: String) is
4998 Error_Msg_N
(Msg
, N
);
4999 Set_Etype
(N
, Any_Type
);
5000 end Bad_Null_Parameter
;
5002 ----------------------
5003 -- Must_Be_Imported --
5004 ----------------------
5006 procedure Must_Be_Imported
(Proc_Ent
: Entity_Id
) is
5007 Pent
: constant Entity_Id
:= Ultimate_Alias
(Proc_Ent
);
5010 -- Ignore check if procedure not frozen yet (we will get
5011 -- another chance when the default parameter is reanalyzed)
5013 if not Is_Frozen
(Pent
) then
5016 elsif not Is_Imported
(Pent
) then
5018 ("Null_Parameter can only be used with imported subprogram");
5023 end Must_Be_Imported
;
5025 -- Start of processing for Null_Parameter
5030 Set_Etype
(N
, P_Type
);
5032 -- Case of attribute used as default expression
5034 if Nkind
(Parnt
) = N_Parameter_Specification
then
5035 Must_Be_Imported
(Defining_Entity
(GParnt
));
5037 -- Case of attribute used as actual for subprogram (positional)
5039 elsif Nkind
(Parnt
) in N_Subprogram_Call
5040 and then Is_Entity_Name
(Name
(Parnt
))
5042 Must_Be_Imported
(Entity
(Name
(Parnt
)));
5044 -- Case of attribute used as actual for subprogram (named)
5046 elsif Nkind
(Parnt
) = N_Parameter_Association
5047 and then Nkind
(GParnt
) in N_Subprogram_Call
5048 and then Is_Entity_Name
(Name
(GParnt
))
5050 Must_Be_Imported
(Entity
(Name
(GParnt
)));
5052 -- Not an allowed case
5056 ("Null_Parameter must be actual or default parameter");
5064 -- Shares processing with Machine_Size attribute
5070 when Attribute_Old
=> Old
: declare
5071 procedure Check_References_In_Prefix
(Subp_Id
: Entity_Id
);
5072 -- Inspect the contents of the prefix and detect illegal uses of a
5073 -- nested 'Old, attribute 'Result or a use of an entity declared in
5074 -- the related postcondition expression. Subp_Id is the subprogram to
5075 -- which the related postcondition applies.
5077 --------------------------------
5078 -- Check_References_In_Prefix --
5079 --------------------------------
5081 procedure Check_References_In_Prefix
(Subp_Id
: Entity_Id
) is
5082 function Check_Reference
(Nod
: Node_Id
) return Traverse_Result
;
5083 -- Detect attribute 'Old, attribute 'Result of a use of an entity
5084 -- and perform the appropriate semantic check.
5086 ---------------------
5087 -- Check_Reference --
5088 ---------------------
5090 function Check_Reference
(Nod
: Node_Id
) return Traverse_Result
is
5092 -- Attributes 'Old and 'Result cannot appear in the prefix of
5093 -- another attribute 'Old.
5095 if Nkind
(Nod
) = N_Attribute_Reference
5096 and then Attribute_Name
(Nod
) in Name_Old | Name_Result
5098 Error_Msg_Name_1
:= Attribute_Name
(Nod
);
5099 Error_Msg_Name_2
:= Name_Old
;
5101 ("attribute % cannot appear in the prefix of attribute %",
5105 -- Entities mentioned within the prefix of attribute 'Old must
5106 -- be global to the related postcondition. If this is not the
5107 -- case, then the scope of the local entity is nested within
5108 -- that of the subprogram.
5110 elsif Is_Entity_Name
(Nod
)
5111 and then Present
(Entity
(Nod
))
5112 and then Scope_Within
(Scope
(Entity
(Nod
)), Subp_Id
)
5115 ("prefix of attribute % cannot reference local entities",
5119 -- Otherwise keep inspecting the prefix
5124 end Check_Reference
;
5126 procedure Check_References
is new Traverse_Proc
(Check_Reference
);
5128 -- Start of processing for Check_References_In_Prefix
5131 Check_References
(P
);
5132 end Check_References_In_Prefix
;
5137 Pref_Id
: Entity_Id
;
5138 Pref_Typ
: Entity_Id
;
5139 Spec_Id
: Entity_Id
;
5141 -- Start of processing for Old
5144 -- The attribute reference is a primary. If any expressions follow,
5145 -- then the attribute reference is an indexable object. Transform the
5146 -- attribute into an indexed component and analyze it.
5148 if Present
(E1
) then
5150 Make_Indexed_Component
(Loc
,
5152 Make_Attribute_Reference
(Loc
,
5153 Prefix
=> Relocate_Node
(P
),
5154 Attribute_Name
=> Name_Old
),
5155 Expressions
=> Expressions
(N
)));
5160 Analyze_Attribute_Old_Result
(Legal
, Spec_Id
);
5162 -- The aspect or pragma where attribute 'Old resides should be
5163 -- associated with a subprogram declaration or a body. If this is not
5164 -- the case, then the aspect or pragma is illegal. Return as analysis
5165 -- cannot be carried out.
5167 -- The exception to this rule is when generating C since in this case
5168 -- postconditions are inlined.
5171 and then Modify_Tree_For_C
5172 and then In_Inlined_Body
5174 Spec_Id
:= Entity
(P
);
5176 elsif not Legal
then
5180 -- The prefix must be preanalyzed as the full analysis will take
5181 -- place during expansion.
5183 Preanalyze_And_Resolve
(P
);
5185 -- Ensure that the prefix does not contain attributes 'Old or 'Result
5187 Check_References_In_Prefix
(Spec_Id
);
5189 -- Set the type of the attribute now to prevent cascaded errors
5191 Pref_Typ
:= Etype
(P
);
5192 Set_Etype
(N
, Pref_Typ
);
5196 if Is_Limited_Type
(Pref_Typ
) then
5197 Error_Attr
("attribute % cannot apply to limited objects", P
);
5200 -- The prefix is a simple name
5202 if Is_Entity_Name
(P
) and then Present
(Entity
(P
)) then
5203 Pref_Id
:= Entity
(P
);
5205 -- Emit a warning when the prefix is a constant. Note that the use
5206 -- of Error_Attr would reset the type of N to Any_Type even though
5207 -- this is a warning. Use Error_Msg_XXX instead.
5209 if Is_Constant_Object
(Pref_Id
) then
5210 Error_Msg_Name_1
:= Name_Old
;
5212 ("??attribute % applied to constant has no effect", P
);
5215 -- Otherwise the prefix is not a simple name
5218 -- Ensure that the prefix of attribute 'Old is an entity when it
5219 -- is potentially unevaluated (6.1.1 (27/3)). This rule is
5220 -- relaxed in Ada 2022 - this relaxation is reflected in the
5221 -- call (below) to Eligible_For_Conditional_Evaluation.
5223 if Is_Potentially_Unevaluated
(N
)
5224 and then not Statically_Names_Object
(P
)
5226 Old_Attr_Util
.Conditional_Evaluation
5227 .Eligible_For_Conditional_Evaluation
(N
)
5231 -- Detect a possible infinite recursion when the prefix denotes
5232 -- the related function.
5234 -- function Func (...) return ...
5235 -- with Post => Func'Old ...;
5237 -- The function may be specified in qualified form X.Y where X is
5238 -- a protected object and Y is a protected function. In that case
5239 -- ensure that the qualified form has an entity.
5241 elsif Nkind
(P
) = N_Function_Call
5242 and then Nkind
(Name
(P
)) in N_Has_Entity
5244 Pref_Id
:= Entity
(Name
(P
));
5246 if Ekind
(Spec_Id
) in E_Function | E_Generic_Function
5247 and then Pref_Id
= Spec_Id
5249 Error_Msg_Warn
:= SPARK_Mode
/= On
;
5250 Error_Msg_N
("!possible infinite recursion<<", P
);
5251 Error_Msg_N
("\!??Storage_Error ]<<", P
);
5255 -- The prefix of attribute 'Old may refer to a component of a
5256 -- formal parameter. In this case its expansion may generate
5257 -- actual subtypes that are referenced in an inner context and
5258 -- that must be elaborated within the subprogram itself. If the
5259 -- prefix includes a function call, it may involve finalization
5260 -- actions that should be inserted when the attribute has been
5261 -- rewritten as a declaration. Create a declaration for the prefix
5262 -- and insert it at the start of the enclosing subprogram. This is
5263 -- an expansion activity that has to be performed now to prevent
5264 -- out-of-order issues.
5266 -- This expansion is both harmful and not needed in SPARK mode,
5267 -- since the formal verification back end relies on the types of
5268 -- nodes (hence is not robust w.r.t. a change to base type here),
5269 -- and does not suffer from the out-of-order issue described
5270 -- above. Thus, this expansion is skipped in SPARK mode.
5272 -- The expansion is not relevant for discrete types, which will
5273 -- not generate extra declarations, and where use of the base type
5274 -- may lead to spurious errors if context is a case.
5276 if not GNATprove_Mode
then
5277 if not Is_Discrete_Type
(Pref_Typ
) then
5278 Pref_Typ
:= Base_Type
(Pref_Typ
);
5281 Set_Etype
(N
, Pref_Typ
);
5282 Set_Etype
(P
, Pref_Typ
);
5284 Analyze_Dimension
(N
);
5290 ----------------------
5291 -- Overlaps_Storage --
5292 ----------------------
5294 when Attribute_Overlaps_Storage
=>
5297 -- Both arguments must be objects of any type
5299 Analyze_And_Resolve
(P
);
5300 Analyze_And_Resolve
(E1
);
5301 Check_Object_Reference
(P
);
5302 Check_Object_Reference
(E1
);
5303 Set_Etype
(N
, Standard_Boolean
);
5309 when Attribute_Output
=>
5311 Check_Stream_Attribute
(TSS_Stream_Output
);
5312 Set_Etype
(N
, Standard_Void_Type
);
5313 Resolve
(N
, Standard_Void_Type
);
5319 when Attribute_Partition_ID
=>
5322 if P_Type
/= Any_Type
then
5323 if not Is_Library_Level_Entity
(Entity
(P
)) then
5325 ("prefix of % attribute must be library-level entity");
5327 -- The defining entity of prefix should not be declared inside a
5328 -- Pure unit. RM E.1(8). Is_Pure was set during declaration.
5330 elsif Is_Entity_Name
(P
)
5331 and then Is_Pure
(Entity
(P
))
5333 Error_Attr_P
("prefix of% attribute must not be declared pure");
5337 Set_Etype
(N
, Universal_Integer
);
5339 -------------------------
5340 -- Passed_By_Reference --
5341 -------------------------
5343 when Attribute_Passed_By_Reference
=>
5346 Check_Not_Incomplete_Type
;
5347 Set_Etype
(N
, Standard_Boolean
);
5353 when Attribute_Pool_Address
=>
5355 Set_Etype
(N
, RTE
(RE_Address
));
5361 when Attribute_Pos
=>
5362 Check_Discrete_Type
;
5364 Resolve
(E1
, P_Base_Type
);
5365 Set_Etype
(N
, Universal_Integer
);
5371 -- Shares processing with First_Bit attribute
5382 Resolve
(E1
, P_Base_Type
);
5383 Set_Etype
(N
, P_Base_Type
);
5385 -- Since Pred/Succ work on the base type, we normally do no check for
5386 -- the floating-point case, since the base type is unconstrained. But
5387 -- we make an exception in Check_Float_Overflow mode.
5389 if Is_Floating_Point_Type
(P_Type
) then
5390 if not Range_Checks_Suppressed
(P_Base_Type
) then
5391 Set_Do_Range_Check
(E1
);
5394 -- If not modular type, test for overflow check required
5397 if not Is_Modular_Integer_Type
(P_Type
)
5398 and then not Range_Checks_Suppressed
(P_Base_Type
)
5400 Enable_Range_Check
(E1
);
5404 ----------------------------------
5405 -- Preelaborable_Initialization --
5406 ----------------------------------
5408 when Attribute_Preelaborable_Initialization
=>
5412 -- If we're in an instance, we know that the legality of the
5413 -- attribute prefix type was already checked in the generic.
5415 if not In_Instance
then
5417 -- If the prefix type is a generic formal type, then it must be
5418 -- either a formal private type or a formal derived type.
5420 if Is_Generic_Type
(P_Type
) then
5421 if not Is_Private_Type
(P_Type
)
5422 and then not Is_Derived_Type
(P_Type
)
5424 Error_Attr_P
("formal type prefix of % attribute must be "
5425 & "formal private or formal derived type");
5428 -- Otherwise, the prefix type must be a nonformal composite
5429 -- type declared within the visible part of a package or
5432 elsif not Is_Composite_Type
(P_Type
)
5433 or else not Original_View_In_Visible_Part
(P_Type
)
5436 ("prefix of % attribute must be composite type declared "
5437 & "in visible part of a package or generic package");
5441 Set_Etype
(N
, Standard_Boolean
);
5447 -- Ada 2005 (AI-327): Dynamic ceiling priorities
5449 when Attribute_Priority
=>
5450 if Ada_Version
< Ada_2005
then
5451 Error_Attr
("% attribute is allowed only in Ada 2005 mode", P
);
5456 Check_Restriction
(No_Dynamic_Priorities
, N
);
5458 -- The prefix must be a protected object (AARM D.5.2 (2/2))
5462 if Is_Protected_Type
(Etype
(P
))
5463 or else (Is_Access_Type
(Etype
(P
))
5464 and then Is_Protected_Type
(Designated_Type
(Etype
(P
))))
5468 Error_Attr_P
("prefix of % attribute must be a protected object");
5471 Set_Etype
(N
, Standard_Integer
);
5473 -- Must be called from within a protected procedure or entry of the
5474 -- protected object.
5481 while S
/= Etype
(P
)
5482 and then S
/= Standard_Standard
5487 if S
= Standard_Standard
then
5488 Error_Attr
("the attribute % is only allowed inside protected "
5493 Validate_Non_Static_Attribute_Function_Call
;
5499 when Attribute_Put_Image
=>
5501 Check_Put_Image_Attribute
;
5502 Set_Etype
(N
, Standard_Void_Type
);
5503 Resolve
(N
, Standard_Void_Type
);
5509 when Attribute_Range
=>
5510 Check_Array_Or_Scalar_Type
;
5511 Bad_Attribute_For_Predicate
;
5513 if Ada_Version
= Ada_83
5514 and then Is_Scalar_Type
(P_Type
)
5515 and then Comes_From_Source
(N
)
5518 ("(Ada 83) % attribute not allowed for scalar type", P
);
5525 when Attribute_Result
=> Result
: declare
5526 function Denote_Same_Function
5527 (Pref_Id
: Entity_Id
;
5528 Spec_Id
: Entity_Id
) return Boolean;
5529 -- Determine whether the entity of the prefix Pref_Id denotes the
5530 -- same entity as that of the related subprogram Spec_Id.
5532 --------------------------
5533 -- Denote_Same_Function --
5534 --------------------------
5536 function Denote_Same_Function
5537 (Pref_Id
: Entity_Id
;
5538 Spec_Id
: Entity_Id
) return Boolean
5540 Over_Id
: constant Entity_Id
:= Overridden_Operation
(Spec_Id
);
5541 Subp_Spec
: constant Node_Id
:= Parent
(Spec_Id
);
5544 -- The prefix denotes the related subprogram
5546 if Pref_Id
= Spec_Id
then
5549 -- Account for a special case when attribute 'Result appears in
5550 -- the postcondition of a generic function.
5553 -- function Gen_Func return ...
5554 -- with Post => Gen_Func'Result ...;
5556 -- When the generic function is instantiated, the Chars field of
5557 -- the instantiated prefix still denotes the name of the generic
5558 -- function. Note that any preemptive transformation is impossible
5559 -- without a proper analysis. The structure of the wrapper package
5562 -- package Anon_Gen_Pack is
5563 -- <subtypes and renamings>
5564 -- function Subp_Decl return ...; -- (!)
5565 -- pragma Postcondition (Gen_Func'Result ...); -- (!)
5566 -- function Gen_Func ... renames Subp_Decl;
5567 -- end Anon_Gen_Pack;
5569 elsif Nkind
(Subp_Spec
) = N_Function_Specification
5570 and then Present
(Generic_Parent
(Subp_Spec
))
5571 and then Ekind
(Pref_Id
) in E_Generic_Function | E_Function
5573 if Generic_Parent
(Subp_Spec
) = Pref_Id
then
5576 elsif Present
(Alias
(Pref_Id
))
5577 and then Alias
(Pref_Id
) = Spec_Id
5582 -- Account for a special case where a primitive of a tagged type
5583 -- inherits a class-wide postcondition from a parent type. In this
5584 -- case the prefix of attribute 'Result denotes the overriding
5587 elsif Present
(Over_Id
) and then Pref_Id
= Over_Id
then
5591 -- Otherwise the prefix does not denote the related subprogram
5594 end Denote_Same_Function
;
5598 In_Inlined_C_Postcondition
: constant Boolean :=
5600 and then In_Inlined_Body
;
5603 Pref_Id
: Entity_Id
;
5604 Spec_Id
: Entity_Id
;
5606 -- Start of processing for Result
5609 -- The attribute reference is a primary. If any expressions follow,
5610 -- then the attribute reference is an indexable object. Transform the
5611 -- attribute into an indexed component and analyze it.
5613 if Present
(E1
) then
5615 Make_Indexed_Component
(Loc
,
5617 Make_Attribute_Reference
(Loc
,
5618 Prefix
=> Relocate_Node
(P
),
5619 Attribute_Name
=> Name_Result
),
5620 Expressions
=> Expressions
(N
)));
5625 Analyze_Attribute_Old_Result
(Legal
, Spec_Id
);
5627 -- The aspect or pragma where attribute 'Result resides should be
5628 -- associated with a subprogram declaration or a body. If this is not
5629 -- the case, then the aspect or pragma is illegal. Return as analysis
5630 -- cannot be carried out.
5632 -- The exception to this rule is when generating C since in this case
5633 -- postconditions are inlined.
5635 if No
(Spec_Id
) and then In_Inlined_C_Postcondition
then
5636 Spec_Id
:= Entity
(P
);
5638 elsif not Legal
then
5639 Error_Attr
("prefix of % attribute must be a function", P
);
5643 -- Attribute 'Result is part of a _Postconditions procedure. There is
5644 -- no need to perform the semantic checks below as they were already
5645 -- verified when the attribute was analyzed in its original context.
5646 -- Instead, rewrite the attribute as a reference to formal parameter
5647 -- _Result of the _Postconditions procedure.
5649 if Chars
(Spec_Id
) = Name_uPostconditions
5651 (In_Inlined_C_Postcondition
5652 and then Nkind
(Parent
(Spec_Id
)) = N_Block_Statement
)
5654 Rewrite
(N
, Make_Identifier
(Loc
, Name_uResult
));
5656 -- The type of formal parameter _Result is that of the function
5657 -- encapsulating the _Postconditions procedure. Resolution must
5658 -- be carried out against the function return type.
5660 Analyze_And_Resolve
(N
, Etype
(Scope
(Spec_Id
)));
5662 -- Otherwise attribute 'Result appears in its original context and
5663 -- all semantic checks should be carried out.
5666 -- Verify the legality of the prefix. It must denotes the entity
5667 -- of the related [generic] function.
5669 if Is_Entity_Name
(P
) then
5670 Pref_Id
:= Entity
(P
);
5672 -- Either both the prefix and the annotated spec must be
5673 -- generic functions, or they both must be nongeneric
5674 -- functions, or the prefix must be generic and the spec
5675 -- must be nongeneric (i.e. it must denote an instance).
5677 if (Ekind
(Pref_Id
) in E_Function | E_Generic_Function
5678 and then Ekind
(Pref_Id
) = Ekind
(Spec_Id
))
5680 (Ekind
(Pref_Id
) = E_Generic_Function
5681 and then Ekind
(Spec_Id
) = E_Function
)
5683 if Denote_Same_Function
(Pref_Id
, Spec_Id
) then
5685 -- Correct the prefix of the attribute when the context
5686 -- is a generic function.
5688 if Pref_Id
/= Spec_Id
then
5689 Rewrite
(P
, New_Occurrence_Of
(Spec_Id
, Loc
));
5693 Set_Etype
(N
, Etype
(Spec_Id
));
5695 -- Otherwise the prefix denotes some unrelated function
5698 Error_Msg_Name_2
:= Chars
(Spec_Id
);
5700 ("incorrect prefix for attribute %, expected %", P
);
5703 -- Otherwise the prefix denotes some other form of subprogram
5708 ("attribute % can only appear in postcondition of "
5712 -- Otherwise the prefix is illegal
5715 Error_Msg_Name_2
:= Chars
(Spec_Id
);
5716 Error_Attr
("incorrect prefix for attribute %, expected %", P
);
5725 when Attribute_Range_Length
=>
5727 Check_Discrete_Type
;
5728 Set_Etype
(N
, Universal_Integer
);
5734 when Attribute_Reduce
=>
5737 if not Extensions_Allowed
then
5739 ("% attribute only supported under -gnatX", P
);
5743 Stream
: constant Node_Id
:= Prefix
(N
);
5746 if Nkind
(Stream
) /= N_Aggregate
then
5747 -- Prefix is a name, as for other attributes.
5749 -- If the object is a function we asume that it is not
5750 -- overloaded. AI12-242 does not suggest a name resolution
5751 -- rule for that case, but we can suppose that the expected
5752 -- type of the reduction is the expected type of the component
5755 Analyze_And_Resolve
(Stream
);
5756 Typ
:= Etype
(Stream
);
5758 -- Verify that prefix can be iterated upon.
5760 if Is_Array_Type
(Typ
)
5761 or else Present
(Find_Aspect
(Typ
, Aspect_Default_Iterator
))
5762 or else Present
(Find_Aspect
(Typ
, Aspect_Iterable
))
5767 ("cannot apply Reduce to object of type&", N
, Typ
);
5770 elsif Present
(Expressions
(Stream
))
5771 or else No
(Component_Associations
(Stream
))
5772 or else Nkind
(First
(Component_Associations
(Stream
))) /=
5773 N_Iterated_Component_Association
5776 ("prefix of Reduce must be an iterated component", N
);
5781 Set_Etype
(N
, Etype
(E2
));
5788 when Attribute_Read
=>
5790 Check_Stream_Attribute
(TSS_Stream_Read
);
5791 Set_Etype
(N
, Standard_Void_Type
);
5792 Resolve
(N
, Standard_Void_Type
);
5793 Note_Possible_Modification
(E2
, Sure
=> True);
5799 when Attribute_Ref
=>
5803 if Nkind
(P
) /= N_Expanded_Name
5804 or else not Is_RTE
(P_Type
, RE_Address
)
5806 Error_Attr_P
("prefix of % attribute must be System.Address");
5809 Analyze_And_Resolve
(E1
, Any_Integer
);
5810 Set_Etype
(N
, RTE
(RE_Address
));
5816 -- Shares processing with Adjacent attribute
5818 ---------------------
5819 -- Restriction_Set --
5820 ---------------------
5822 when Attribute_Restriction_Set
=> Restriction_Set
: declare
5825 Unam
: Unit_Name_Type
;
5830 Check_System_Prefix
;
5832 -- No_Dependence case
5834 if Nkind
(E1
) = N_Parameter_Association
then
5835 pragma Assert
(Chars
(Selector_Name
(E1
)) = Name_No_Dependence
);
5836 U
:= Explicit_Actual_Parameter
(E1
);
5838 if not OK_No_Dependence_Unit_Name
(U
) then
5839 Set_Boolean_Result
(N
, False);
5843 -- See if there is an entry already in the table. That's the
5844 -- case in which we can return True.
5846 for J
in No_Dependences
.First
.. No_Dependences
.Last
loop
5847 if Designate_Same_Unit
(U
, No_Dependences
.Table
(J
).Unit
)
5848 and then No_Dependences
.Table
(J
).Warn
= False
5850 Set_Boolean_Result
(N
, True);
5855 -- If not in the No_Dependence table, result is False
5857 Set_Boolean_Result
(N
, False);
5859 -- In this case, we must ensure that the binder will reject any
5860 -- other unit in the partition that sets No_Dependence for this
5861 -- unit. We do that by making an entry in the special table kept
5862 -- for this purpose (if the entry is not there already).
5864 Unam
:= Get_Spec_Name
(Get_Unit_Name
(U
));
5866 for J
in Restriction_Set_Dependences
.First
..
5867 Restriction_Set_Dependences
.Last
5869 if Restriction_Set_Dependences
.Table
(J
) = Unam
then
5874 Restriction_Set_Dependences
.Append
(Unam
);
5876 -- Normal restriction case
5879 if Nkind
(E1
) /= N_Identifier
then
5880 Set_Boolean_Result
(N
, False);
5881 Error_Attr
("attribute % requires restriction identifier", E1
);
5884 R
:= Get_Restriction_Id
(Process_Restriction_Synonyms
(E1
));
5886 if R
= Not_A_Restriction_Id
then
5887 Set_Boolean_Result
(N
, False);
5888 Error_Msg_Node_1
:= E1
;
5889 Error_Attr
("invalid restriction identifier &", E1
);
5891 elsif R
not in Partition_Boolean_Restrictions
then
5892 Set_Boolean_Result
(N
, False);
5893 Error_Msg_Node_1
:= E1
;
5895 ("& is not a boolean partition-wide restriction", E1
);
5898 if Restriction_Active
(R
) then
5899 Set_Boolean_Result
(N
, True);
5901 Check_Restriction
(R
, N
);
5902 Set_Boolean_Result
(N
, False);
5906 end Restriction_Set
;
5912 when Attribute_Round
=>
5914 Check_Decimal_Fixed_Point_Type
;
5915 Set_Etype
(N
, P_Base_Type
);
5917 -- Because the context is universal_real (3.5.10(12)) it is a
5918 -- legal context for a universal fixed expression. This is the
5919 -- only attribute whose functional description involves U_R.
5921 if Etype
(E1
) = Universal_Fixed
then
5923 Conv
: constant Node_Id
:= Make_Type_Conversion
(Loc
,
5924 Subtype_Mark
=> New_Occurrence_Of
(Universal_Real
, Loc
),
5925 Expression
=> Relocate_Node
(E1
));
5933 Resolve
(E1
, Any_Real
);
5939 -- Shares processing with Ceiling attribute
5945 -- Shares processing with Emax attribute
5951 -- Shares processing with Epsilon attribute
5957 -- Shares processing with Large attribute
5963 -- Shares processing with Epsilon attribute
5969 -- Shares processing with Large attribute
5971 --------------------------
5972 -- Scalar_Storage_Order --
5973 --------------------------
5975 when Attribute_Scalar_Storage_Order
=> Scalar_Storage_Order
: declare
5976 Ent
: Entity_Id
:= Empty
;
5982 if not (Is_Record_Type
(P_Type
) or else Is_Array_Type
(P_Type
)) then
5984 -- The attribute applies to generic private types (in which case
5985 -- the legality rule is applied in the instance) as well as to
5986 -- composite types. For noncomposite types it always returns the
5987 -- default bit order for the target.
5988 -- Allowing formal private types was originally introduced in
5989 -- GNAT_Mode only, to compile instances of Sequential_IO, but
5990 -- users find it more generally useful in generic units.
5992 if not (Is_Generic_Type
(P_Type
) and then Is_Private_Type
(P_Type
))
5993 and then not In_Instance
5996 ("prefix of % attribute must be record or array type");
5998 elsif not Is_Generic_Type
(P_Type
) then
5999 if Bytes_Big_Endian
then
6000 Ent
:= RTE
(RE_High_Order_First
);
6002 Ent
:= RTE
(RE_Low_Order_First
);
6006 elsif Bytes_Big_Endian
xor Reverse_Storage_Order
(P_Type
) then
6007 Ent
:= RTE
(RE_High_Order_First
);
6010 Ent
:= RTE
(RE_Low_Order_First
);
6013 if Present
(Ent
) then
6014 Rewrite
(N
, New_Occurrence_Of
(Ent
, Loc
));
6017 Set_Etype
(N
, RTE
(RE_Bit_Order
));
6020 -- Reset incorrect indication of staticness
6022 Set_Is_Static_Expression
(N
, False);
6023 end Scalar_Storage_Order
;
6029 when Attribute_Scale
=>
6031 Check_Decimal_Fixed_Point_Type
;
6032 Set_Etype
(N
, Universal_Integer
);
6038 -- Shares processing with Compose attribute
6044 -- Shares processing with Denorm attribute
6051 | Attribute_VADS_Size
6055 -- If prefix is parameterless function call, rewrite and resolve
6058 if Is_Entity_Name
(P
)
6059 and then Ekind
(Entity
(P
)) = E_Function
6063 -- Similar processing for a protected function call
6065 elsif Nkind
(P
) = N_Selected_Component
6066 and then Ekind
(Entity
(Selector_Name
(P
))) = E_Function
6071 if Is_Object_Reference
(P
) then
6072 Check_Object_Reference
(P
);
6074 elsif Is_Entity_Name
(P
)
6075 and then (Is_Type
(Entity
(P
))
6076 or else Ekind
(Entity
(P
)) = E_Enumeration_Literal
)
6080 elsif Nkind
(P
) = N_Type_Conversion
6081 and then not Comes_From_Source
(P
)
6085 -- Some other compilers allow dubious use of X'???'Size
6087 elsif Relaxed_RM_Semantics
6088 and then Nkind
(P
) = N_Attribute_Reference
6093 Error_Attr_P
("invalid prefix for % attribute");
6096 Check_Not_Incomplete_Type
;
6098 Set_Etype
(N
, Universal_Integer
);
6100 -- If we are processing pragmas Compile_Time_Warning and Compile_
6101 -- Time_Errors after the back end has been called and this occurrence
6102 -- of 'Size is known at compile time then it is safe to perform this
6103 -- evaluation. Needed to perform the static evaluation of the full
6104 -- boolean expression of these pragmas. Note that Known_RM_Size is
6105 -- sometimes True when Size_Known_At_Compile_Time is False, when the
6106 -- back end has computed it.
6108 if In_Compile_Time_Warning_Or_Error
6109 and then Is_Entity_Name
(P
)
6110 and then (Is_Type
(Entity
(P
))
6111 or else Ekind
(Entity
(P
)) = E_Enumeration_Literal
)
6112 and then (Known_RM_Size
(Entity
(P
))
6113 or else Size_Known_At_Compile_Time
(Entity
(P
)))
6119 if Known_Static_RM_Size
(Entity
(P
)) then
6120 Siz
:= RM_Size
(Entity
(P
));
6122 Siz
:= Esize
(Entity
(P
));
6125 Rewrite
(N
, Make_Integer_Literal
(Sloc
(N
), Siz
));
6134 -- Shares processing with Large attribute
6136 ---------------------------------------
6137 -- Small_Denominator/Small_Numerator --
6138 ---------------------------------------
6140 when Attribute_Small_Denominator
6141 | Attribute_Small_Numerator
6143 Check_Fixed_Point_Type_0
;
6144 Set_Etype
(N
, Universal_Integer
);
6150 when Attribute_Storage_Pool
6151 | Attribute_Simple_Storage_Pool
6155 if Is_Access_Type
(P_Type
) then
6156 if Ekind
(P_Type
) = E_Access_Subprogram_Type
then
6158 ("cannot use % attribute for access-to-subprogram type");
6161 -- Set appropriate entity
6163 if Present
(Associated_Storage_Pool
(Root_Type
(P_Type
))) then
6164 Set_Entity
(N
, Associated_Storage_Pool
(Root_Type
(P_Type
)));
6166 Set_Entity
(N
, RTE
(RE_Global_Pool_Object
));
6169 if Attr_Id
= Attribute_Storage_Pool
then
6170 if Present
(Get_Rep_Pragma
(Etype
(Entity
(N
)),
6171 Name_Simple_Storage_Pool_Type
))
6173 Error_Msg_Name_1
:= Aname
;
6174 Error_Msg_Warn
:= SPARK_Mode
/= On
;
6176 ("cannot use % attribute for type with simple storage "
6178 Error_Msg_N
("\Program_Error [<<", N
);
6181 (N
, Make_Raise_Program_Error
6182 (Sloc
(N
), Reason
=> PE_Explicit_Raise
));
6185 Set_Etype
(N
, Class_Wide_Type
(RTE
(RE_Root_Storage_Pool
)));
6187 -- In the Simple_Storage_Pool case, verify that the pool entity is
6188 -- actually of a simple storage pool type, and set the attribute's
6189 -- type to the pool object's type.
6192 if not Present
(Get_Rep_Pragma
(Etype
(Entity
(N
)),
6193 Name_Simple_Storage_Pool_Type
))
6196 ("cannot use % attribute for type without simple " &
6200 Set_Etype
(N
, Etype
(Entity
(N
)));
6203 -- Validate_Remote_Access_To_Class_Wide_Type for attribute
6204 -- Storage_Pool since this attribute is not defined for such
6205 -- types (RM E.2.2(17)).
6207 Validate_Remote_Access_To_Class_Wide_Type
(N
);
6210 Error_Attr_P
("prefix of % attribute must be access type");
6217 when Attribute_Storage_Size
=>
6220 if Is_Task_Type
(P_Type
) then
6221 Set_Etype
(N
, Universal_Integer
);
6223 -- Use with tasks is an obsolescent feature
6225 Check_Restriction
(No_Obsolescent_Features
, P
);
6227 elsif Is_Access_Type
(P_Type
) then
6228 Set_Etype
(N
, Universal_Integer
);
6230 if Ekind
(P_Type
) = E_Access_Subprogram_Type
then
6232 ("cannot use % attribute for access-to-subprogram type");
6235 if Is_Entity_Name
(P
)
6236 and then Is_Type
(Entity
(P
))
6240 -- Validate_Remote_Access_To_Class_Wide_Type for attribute
6241 -- Storage_Size since this attribute is not defined for
6242 -- such types (RM E.2.2(17)).
6244 Validate_Remote_Access_To_Class_Wide_Type
(N
);
6246 -- The prefix is allowed to be an implicit dereference of an
6247 -- access value designating a task.
6254 Error_Attr_P
("prefix of % attribute must be access or task type");
6261 when Attribute_Storage_Unit
=>
6262 Standard_Attribute
(Ttypes
.System_Storage_Unit
);
6268 when Attribute_Stream_Size
=>
6272 if Is_Entity_Name
(P
)
6273 and then Is_Elementary_Type
(Entity
(P
))
6275 Set_Etype
(N
, Universal_Integer
);
6277 Error_Attr_P
("invalid prefix for % attribute");
6284 when Attribute_Stub_Type
=>
6288 if Is_Remote_Access_To_Class_Wide_Type
(Base_Type
(P_Type
)) then
6290 -- For a real RACW [sub]type, use corresponding stub type
6292 if not Is_Generic_Type
(P_Type
) then
6295 (Corresponding_Stub_Type
(Base_Type
(P_Type
)), Loc
));
6297 -- For a generic type (that has been marked as an RACW using the
6298 -- Remote_Access_Type aspect or pragma), use a generic RACW stub
6299 -- type. Note that if the actual is not a remote access type, the
6300 -- instantiation will fail.
6303 -- Note: we go to the underlying type here because the view
6304 -- returned by RTE (RE_RACW_Stub_Type) might be incomplete.
6308 (Underlying_Type
(RTE
(RE_RACW_Stub_Type
)), Loc
));
6313 ("prefix of% attribute must be remote access-to-class-wide");
6320 -- Shares processing with Pred attribute
6322 --------------------------------
6323 -- System_Allocator_Alignment --
6324 --------------------------------
6326 when Attribute_System_Allocator_Alignment
=>
6327 Standard_Attribute
(Ttypes
.System_Allocator_Alignment
);
6333 when Attribute_Tag
=>
6337 if not Is_Tagged_Type
(P_Type
) then
6338 Error_Attr_P
("prefix of % attribute must be tagged");
6340 -- Next test does not apply to generated code why not, and what does
6341 -- the illegal reference mean???
6343 elsif Is_Object_Reference
(P
)
6344 and then not Is_Class_Wide_Type
(P_Type
)
6345 and then Comes_From_Source
(N
)
6348 ("% attribute can only be applied to objects " &
6349 "of class-wide type");
6352 -- The prefix cannot be an incomplete type. However, references to
6353 -- 'Tag can be generated when expanding interface conversions, and
6356 if Comes_From_Source
(N
) then
6357 Check_Not_Incomplete_Type
;
6359 -- 'Tag requires visibility on the corresponding package holding
6360 -- the tag, so record a reference here, to avoid spurious unused
6361 -- with_clause reported when compiling the main unit.
6363 if In_Extended_Main_Source_Unit
(Current_Scope
) then
6364 Set_Referenced
(P_Type
, True);
6365 Set_Referenced
(Scope
(P_Type
), True);
6369 -- Set appropriate type
6371 Set_Etype
(N
, RTE
(RE_Tag
));
6377 when Attribute_Target_Name
=> Target_Name
: declare
6378 TN
: constant String := Sdefault
.Target_Name
.all;
6382 Check_Standard_Prefix
;
6386 if TN
(TL
) = '/' or else TN
(TL
) = '\' then
6391 Make_String_Literal
(Loc
,
6392 Strval
=> TN
(TN
'First .. TL
)));
6393 Analyze_And_Resolve
(N
, Standard_String
);
6394 Set_Is_Static_Expression
(N
, True);
6401 -- Shares processing with Callable attribute
6407 when Attribute_To_Address
=> To_Address
: declare
6412 Check_System_Prefix
;
6414 Generate_Reference
(RTE
(RE_Address
), P
);
6415 Analyze_And_Resolve
(E1
, Any_Integer
);
6416 Set_Etype
(N
, RTE
(RE_Address
));
6417 Set_Is_Static_Expression
(N
, Is_Static_Expression
(E1
));
6419 -- OK static expression case, check range and set appropriate type
6421 if Is_OK_Static_Expression
(E1
) then
6422 Val
:= Expr_Value
(E1
);
6424 if Val
< -(Uint_2
** (System_Address_Size
- 1))
6426 Val
> Uint_2
** System_Address_Size
- 1
6428 Error_Attr
("address value out of range for % attribute", E1
);
6431 -- In most cases the expression is a numeric literal or some other
6432 -- address expression, but if it is a declared constant it may be
6433 -- of a compatible type that must be left on the node.
6435 if Is_Entity_Name
(E1
) then
6438 -- Set type to universal integer if negative
6441 Set_Etype
(E1
, Universal_Integer
);
6443 -- Otherwise set type to Unsigned_64 to accommodate large values
6446 Set_Etype
(E1
, Standard_Unsigned_64
);
6455 when Attribute_To_Any
=>
6457 Check_PolyORB_Attribute
;
6458 Set_Etype
(N
, RTE
(RE_Any
));
6464 -- Shares processing with Ceiling attribute
6470 when Attribute_Type_Class
=>
6473 Check_Not_Incomplete_Type
;
6474 Set_Etype
(N
, RTE
(RE_Type_Class
));
6480 when Attribute_TypeCode
=>
6482 Check_PolyORB_Attribute
;
6483 Set_Etype
(N
, RTE
(RE_TypeCode
));
6489 when Attribute_Type_Key
=> Type_Key
: declare
6490 Full_Name
: constant String_Id
:=
6491 Fully_Qualified_Name_String
(Entity
(P
));
6494 -- The computed signature for the type
6497 -- To simplify the handling of mutually recursive types, follow a
6498 -- single dereference link in a composite type.
6500 procedure Compute_Type_Key
(T
: Entity_Id
);
6501 -- Create a CRC integer from the declaration of the type. For a
6502 -- composite type, fold in the representation of its components in
6503 -- recursive fashion. We use directly the source representation of
6504 -- the types involved.
6506 ----------------------
6507 -- Compute_Type_Key --
6508 ----------------------
6510 procedure Compute_Type_Key
(T
: Entity_Id
) is
6511 Buffer
: Source_Buffer_Ptr
;
6515 SFI
: Source_File_Index
;
6517 procedure Process_One_Declaration
;
6518 -- Update CRC with the characters of one type declaration, or a
6519 -- representation pragma that applies to the type.
6521 -----------------------------
6522 -- Process_One_Declaration --
6523 -----------------------------
6525 procedure Process_One_Declaration
is
6527 -- Scan type declaration, skipping blanks
6529 for Ptr
in P_Min
.. P_Max
loop
6530 if Buffer
(Ptr
) /= ' ' then
6531 System
.CRC32
.Update
(CRC
, Buffer
(Ptr
));
6534 end Process_One_Declaration
;
6536 -- Start of processing for Compute_Type_Key
6539 if Is_Itype
(T
) then
6543 -- If the type is declared in Standard, there is no source, so
6544 -- just use its name.
6546 if Scope
(T
) = Standard_Standard
then
6548 Name
: constant String := Get_Name_String
(Chars
(T
));
6550 for J
in Name
'Range loop
6551 System
.CRC32
.Update
(CRC
, Name
(J
));
6558 Sloc_Range
(Enclosing_Declaration
(T
), P_Min
, P_Max
);
6559 SFI
:= Get_Source_File_Index
(P_Min
);
6560 pragma Assert
(SFI
= Get_Source_File_Index
(P_Max
));
6561 Buffer
:= Source_Text
(SFI
);
6563 Process_One_Declaration
;
6565 -- Recurse on relevant component types
6567 if Is_Array_Type
(T
) then
6568 Compute_Type_Key
(Component_Type
(T
));
6570 elsif Is_Access_Type
(T
) then
6573 Compute_Type_Key
(Designated_Type
(T
));
6576 elsif Is_Derived_Type
(T
) then
6577 Compute_Type_Key
(Etype
(T
));
6579 elsif Is_Record_Type
(T
) then
6583 Comp
:= First_Component
(T
);
6584 while Present
(Comp
) loop
6585 Compute_Type_Key
(Etype
(Comp
));
6586 Next_Component
(Comp
);
6591 if Is_First_Subtype
(T
) then
6593 -- Fold in representation aspects for the type, which appear in
6594 -- the same source buffer. If the representation aspects are in
6595 -- a different source file, then skip them; they apply to some
6596 -- other type, perhaps one we're derived from.
6598 Rep
:= First_Rep_Item
(T
);
6600 while Present
(Rep
) loop
6601 if Comes_From_Source
(Rep
) then
6602 Sloc_Range
(Rep
, P_Min
, P_Max
);
6604 if SFI
= Get_Source_File_Index
(P_Min
) then
6605 pragma Assert
(SFI
= Get_Source_File_Index
(P_Max
));
6606 Process_One_Declaration
;
6610 Next_Rep_Item
(Rep
);
6613 end Compute_Type_Key
;
6615 -- Start of processing for Type_Key
6624 -- Copy all characters in Full_Name but the trailing NUL
6626 for J
in 1 .. String_Length
(Full_Name
) - 1 loop
6627 Store_String_Char
(Get_String_Char
(Full_Name
, Pos
(J
)));
6630 -- Compute CRC and convert it to string one character at a time, so
6631 -- as not to use Image within the compiler.
6634 Compute_Type_Key
(Entity
(P
));
6636 if not Is_Frozen
(Entity
(P
)) then
6637 Error_Msg_N
("premature usage of Type_Key?", N
);
6641 Store_String_Char
(Character'Val (48 + (CRC
rem 10)));
6645 Rewrite
(N
, Make_String_Literal
(Loc
, End_String
));
6646 Analyze_And_Resolve
(N
, Standard_String
);
6649 -----------------------
6650 -- Unbiased_Rounding --
6651 -----------------------
6653 -- Shares processing with Ceiling attribute
6655 ----------------------
6656 -- Unchecked_Access --
6657 ----------------------
6659 when Attribute_Unchecked_Access
=>
6660 if Comes_From_Source
(N
) then
6661 Check_Restriction
(No_Unchecked_Access
, N
);
6664 Analyze_Access_Attribute
;
6665 Check_Not_Incomplete_Type
;
6667 -------------------------
6668 -- Unconstrained_Array --
6669 -------------------------
6671 when Attribute_Unconstrained_Array
=>
6674 Check_Not_Incomplete_Type
;
6675 Set_Etype
(N
, Standard_Boolean
);
6676 Set_Is_Static_Expression
(N
, True);
6678 ------------------------------
6679 -- Universal_Literal_String --
6680 ------------------------------
6682 -- This is a GNAT specific attribute whose prefix must be a named
6683 -- number where the expression is either a single numeric literal,
6684 -- or a numeric literal immediately preceded by a minus sign. The
6685 -- result is equivalent to a string literal containing the text of
6686 -- the literal as it appeared in the source program with a possible
6687 -- leading minus sign.
6689 when Attribute_Universal_Literal_String
=>
6692 if not Is_Entity_Name
(P
)
6693 or else not Is_Named_Number
(Entity
(P
))
6695 Error_Attr_P
("prefix for % attribute must be named number");
6702 Src
: Source_Buffer_Ptr
;
6705 Expr
:= Original_Node
(Expression
(Parent
(Entity
(P
))));
6707 if Nkind
(Expr
) = N_Op_Minus
then
6709 Expr
:= Original_Node
(Right_Opnd
(Expr
));
6714 if Nkind
(Expr
) not in N_Integer_Literal | N_Real_Literal
then
6716 ("named number for % attribute must be simple literal", N
);
6719 -- Build string literal corresponding to source literal text
6724 Store_String_Char
(Get_Char_Code
('-'));
6728 Src
:= Source_Text
(Get_Source_File_Index
(S
));
6730 while Src
(S
) /= ';' and then Src
(S
) /= ' ' loop
6731 Store_String_Char
(Get_Char_Code
(Src
(S
)));
6735 -- Now we rewrite the attribute with the string literal
6738 Make_String_Literal
(Loc
, End_String
));
6740 Set_Is_Static_Expression
(N
, True);
6744 -------------------------
6745 -- Unrestricted_Access --
6746 -------------------------
6748 -- This is a GNAT specific attribute which is like Access except that
6749 -- all scope checks and checks for aliased views are omitted. It is
6750 -- documented as being equivalent to the use of the Address attribute
6751 -- followed by an unchecked conversion to the target access type.
6753 when Attribute_Unrestricted_Access
=>
6755 -- If from source, deal with relevant restrictions
6757 if Comes_From_Source
(N
) then
6758 Check_Restriction
(No_Unchecked_Access
, N
);
6760 if Nkind
(P
) in N_Has_Entity
6761 and then Present
(Entity
(P
))
6762 and then Is_Object
(Entity
(P
))
6764 Check_Restriction
(No_Implicit_Aliasing
, N
);
6768 if Is_Entity_Name
(P
) then
6769 Set_Address_Taken
(Entity
(P
));
6772 -- It might seem reasonable to call Address_Checks here to apply the
6773 -- same set of semantic checks that we enforce for 'Address (after
6774 -- all we document Unrestricted_Access as being equivalent to the
6775 -- use of Address followed by an Unchecked_Conversion). However, if
6776 -- we do enable these checks, we get multiple failures in both the
6777 -- compiler run-time and in our regression test suite, so we leave
6778 -- out these checks for now. To be investigated further some time???
6782 -- Now complete analysis using common access processing
6784 Analyze_Access_Attribute
;
6790 when Attribute_Update
=> Update
: declare
6791 Common_Typ
: Entity_Id
;
6792 -- The common type of a multiple component update for a record
6794 Comps
: Elist_Id
:= No_Elist
;
6795 -- A list used in the resolution of a record update. It contains the
6796 -- entities of all record components processed so far.
6798 procedure Analyze_Array_Component_Update
(Assoc
: Node_Id
);
6799 -- Analyze and resolve array_component_association Assoc against the
6800 -- index of array type P_Type.
6802 procedure Analyze_Record_Component_Update
(Comp
: Node_Id
);
6803 -- Analyze and resolve record_component_association Comp against
6804 -- record type P_Type.
6806 ------------------------------------
6807 -- Analyze_Array_Component_Update --
6808 ------------------------------------
6810 procedure Analyze_Array_Component_Update
(Assoc
: Node_Id
) is
6814 Index_Typ
: Entity_Id
;
6818 -- The current association contains a sequence of indexes denoting
6819 -- an element of a multidimensional array:
6821 -- (Index_1, ..., Index_N)
6823 -- Examine each individual index and resolve it against the proper
6824 -- index type of the array.
6826 if Nkind
(First
(Choices
(Assoc
))) = N_Aggregate
then
6827 Expr
:= First
(Choices
(Assoc
));
6828 while Present
(Expr
) loop
6830 -- The use of others is illegal (SPARK RM 4.4.1(12))
6832 if Nkind
(Expr
) = N_Others_Choice
then
6834 ("OTHERS choice not allowed in attribute %", Expr
);
6836 -- Otherwise analyze and resolve all indexes
6839 Index
:= First
(Expressions
(Expr
));
6840 Index_Typ
:= First_Index
(P_Type
);
6841 while Present
(Index
) and then Present
(Index_Typ
) loop
6842 Analyze_And_Resolve
(Index
, Etype
(Index_Typ
));
6844 Next_Index
(Index_Typ
);
6847 -- Detect a case where the association either lacks an
6848 -- index or contains an extra index.
6850 if Present
(Index
) or else Present
(Index_Typ
) then
6852 ("dimension mismatch in index list", Assoc
);
6859 -- The current association denotes either a single component or a
6860 -- range of components of a one dimensional array:
6864 -- Resolve the index or its high and low bounds (if range) against
6865 -- the proper index type of the array.
6868 Index
:= First
(Choices
(Assoc
));
6869 Index_Typ
:= First_Index
(P_Type
);
6871 if Present
(Next_Index
(Index_Typ
)) then
6872 Error_Msg_N
("too few subscripts in array reference", Assoc
);
6875 while Present
(Index
) loop
6877 -- The use of others is illegal (SPARK RM 4.4.1(12))
6879 if Nkind
(Index
) = N_Others_Choice
then
6881 ("OTHERS choice not allowed in attribute %", Index
);
6883 -- The index denotes a range of elements
6885 elsif Nkind
(Index
) = N_Range
then
6886 Low
:= Low_Bound
(Index
);
6887 High
:= High_Bound
(Index
);
6889 Analyze_And_Resolve
(Low
, Etype
(Index_Typ
));
6890 Analyze_And_Resolve
(High
, Etype
(Index_Typ
));
6892 -- Otherwise the index denotes a single element
6895 Analyze_And_Resolve
(Index
, Etype
(Index_Typ
));
6901 end Analyze_Array_Component_Update
;
6903 -------------------------------------
6904 -- Analyze_Record_Component_Update --
6905 -------------------------------------
6907 procedure Analyze_Record_Component_Update
(Comp
: Node_Id
) is
6908 Comp_Name
: constant Name_Id
:= Chars
(Comp
);
6909 Base_Typ
: Entity_Id
;
6910 Comp_Or_Discr
: Entity_Id
;
6913 -- Find the discriminant or component whose name corresponds to
6914 -- Comp. A simple character comparison is sufficient because all
6915 -- visible names within a record type are unique.
6917 Comp_Or_Discr
:= First_Entity
(P_Type
);
6918 while Present
(Comp_Or_Discr
) loop
6919 if Chars
(Comp_Or_Discr
) = Comp_Name
then
6921 -- Decorate the component reference by setting its entity
6922 -- and type for resolution purposes.
6924 Set_Entity
(Comp
, Comp_Or_Discr
);
6925 Set_Etype
(Comp
, Etype
(Comp_Or_Discr
));
6929 Next_Entity
(Comp_Or_Discr
);
6932 -- Diagnose an illegal reference
6934 if Present
(Comp_Or_Discr
) then
6935 if Ekind
(Comp_Or_Discr
) = E_Discriminant
then
6937 ("attribute % may not modify record discriminants", Comp
);
6939 else pragma Assert
(Ekind
(Comp_Or_Discr
) = E_Component
);
6940 if Contains
(Comps
, Comp_Or_Discr
) then
6941 Error_Msg_N
("component & already updated", Comp
);
6943 -- Mark this component as processed
6946 Append_New_Elmt
(Comp_Or_Discr
, Comps
);
6950 -- The update aggregate mentions an entity that does not belong to
6954 Error_Msg_N
("& is not a component of aggregate subtype", Comp
);
6957 -- Verify the consistency of types when the current component is
6958 -- part of a multiple component update.
6960 -- Comp_1 | ... | Comp_N => <value>
6962 if Present
(Etype
(Comp
)) then
6963 Base_Typ
:= Base_Type
(Etype
(Comp
));
6965 -- Save the type of the first component reference as the
6966 -- remaning references (if any) must resolve to this type.
6968 if No
(Common_Typ
) then
6969 Common_Typ
:= Base_Typ
;
6971 elsif Base_Typ
/= Common_Typ
then
6973 ("components in choice list must have same type", Comp
);
6976 end Analyze_Record_Component_Update
;
6983 -- Start of processing for Update
6986 if Warn_On_Obsolescent_Feature
then
6987 Error_Msg_N
("?j?attribute Update is an obsolescent feature", N
);
6988 Error_Msg_N
("\?j?use a delta aggregate instead", N
);
6993 if not Is_Object_Reference
(P
) then
6994 Error_Attr_P
("prefix of attribute % must denote an object");
6996 elsif not Is_Array_Type
(P_Type
)
6997 and then not Is_Record_Type
(P_Type
)
6999 Error_Attr_P
("prefix of attribute % must be a record or array");
7001 elsif Is_Limited_View
(P_Type
) then
7002 Error_Attr
("prefix of attribute % cannot be limited", N
);
7004 elsif Nkind
(E1
) /= N_Aggregate
then
7005 Error_Attr
("attribute % requires component association list", N
);
7007 elsif Present
(Expressions
(E1
)) then
7008 Error_Attr
("attribute % requires named component associations",
7009 First
(Expressions
(E1
)));
7013 -- Inspect the update aggregate, looking at all the associations and
7014 -- choices. Perform the following checks:
7016 -- 1) Legality of "others" in all cases
7017 -- 2) Legality of <>
7018 -- 3) Component legality for arrays
7019 -- 4) Component legality for records
7021 -- The remaining checks are performed on the expanded attribute
7023 Assoc
:= First
(Component_Associations
(E1
));
7024 while Present
(Assoc
) loop
7026 -- The use of <> is illegal (SPARK RM 4.4.1(1))
7028 if Box_Present
(Assoc
) then
7030 ("default initialization not allowed in attribute %", Assoc
);
7032 -- Otherwise process the association
7035 Analyze
(Expression
(Assoc
));
7037 if Is_Array_Type
(P_Type
) then
7038 Analyze_Array_Component_Update
(Assoc
);
7040 elsif Is_Record_Type
(P_Type
) then
7042 -- Reset the common type used in a multiple component update
7043 -- as we are processing the contents of a new association.
7045 Common_Typ
:= Empty
;
7047 Comp
:= First
(Choices
(Assoc
));
7048 while Present
(Comp
) loop
7049 if Nkind
(Comp
) = N_Identifier
then
7050 Analyze_Record_Component_Update
(Comp
);
7052 -- The use of others is illegal (SPARK RM 4.4.1(5))
7054 elsif Nkind
(Comp
) = N_Others_Choice
then
7056 ("OTHERS choice not allowed in attribute %", Comp
);
7058 -- The name of a record component cannot appear in any
7063 ("name should be identifier or OTHERS", Comp
);
7074 -- The type of attribute 'Update is that of the prefix
7076 Set_Etype
(N
, P_Type
);
7078 Sem_Warn
.Warn_On_Suspicious_Update
(N
);
7085 when Attribute_Val
=>
7087 Check_Discrete_Type
;
7089 -- Note, we need a range check in general, but we wait for the
7090 -- Resolve call to do this, since we want to let Eval_Attribute
7091 -- have a chance to find an static illegality first.
7093 Resolve
(E1
, Any_Integer
);
7094 Set_Etype
(N
, P_Base_Type
);
7100 when Attribute_Valid
=> Valid
: declare
7101 Pred_Func
: constant Entity_Id
:= Predicate_Function
(P_Type
);
7106 -- Ignore check for object if we have a 'Valid reference generated
7107 -- by the expanded code, since in some cases valid checks can occur
7108 -- on items that are names, but are not objects (e.g. attributes).
7110 if Comes_From_Source
(N
) then
7111 Check_Object_Reference
(P
);
7113 if not Is_Scalar_Type
(P_Type
) then
7114 Error_Attr_P
("object for % attribute must be of scalar type");
7117 -- If the attribute appears within the subtype's own predicate
7118 -- function, then issue a warning that this will cause infinite
7121 if Present
(Pred_Func
) and then Current_Scope
= Pred_Func
then
7122 Error_Msg_N
("attribute Valid requires a predicate check??", N
);
7123 Error_Msg_N
("\and will result in infinite recursion??", N
);
7127 Set_Etype
(N
, Standard_Boolean
);
7134 when Attribute_Valid_Value
=>
7136 Check_Enumeration_Type
;
7137 Check_Enum_Image
(Check_Enumeration_Maps
=> True);
7138 Set_Etype
(N
, Standard_Boolean
);
7139 Validate_Non_Static_Attribute_Function_Call
;
7141 if P_Type
in Standard_Boolean
7142 | Standard_Character
7143 | Standard_Wide_Character
7144 | Standard_Wide_Wide_Character
7147 ("prefix of % attribute must not be a type in Standard");
7150 if Discard_Names
(First_Subtype
(P_Type
)) then
7152 ("prefix of % attribute must not have Discard_Names");
7159 when Attribute_Valid_Scalars
=> Valid_Scalars
: declare
7163 if Comes_From_Source
(N
) then
7164 Check_Object_Reference
(P
);
7166 -- Do not emit any diagnostics related to private types to avoid
7167 -- disclosing the structure of the type.
7169 if Is_Private_Type
(P_Type
) then
7171 -- Attribute 'Valid_Scalars is not supported on private tagged
7172 -- types due to a code generation issue. Is_Visible_Component
7173 -- does not allow for a component of a private tagged type to
7174 -- be successfully retrieved.
7175 -- ??? This attribute should simply ignore type privacy
7176 -- (see Validated_View). It should examine components of the
7177 -- tagged type extensions (if any) and recursively examine
7178 -- 'Valid_Scalars of the parent's type (if any).
7180 -- Do not use Error_Attr_P because this bypasses any subsequent
7181 -- processing and leaves the attribute with type Any_Type. This
7182 -- in turn prevents the proper expansion of the attribute into
7185 if Is_Tagged_Type
(P_Type
) then
7186 Error_Msg_Name_1
:= Aname
;
7187 Error_Msg_N
("??effects of attribute % are ignored", N
);
7190 -- Otherwise the type is not private
7193 if not Scalar_Part_Present
(P_Type
) then
7194 Error_Msg_Name_1
:= Aname
;
7196 ("??attribute % always True, no scalars to check", P
);
7197 Set_Boolean_Result
(N
, True);
7200 -- Attribute 'Valid_Scalars is illegal on unchecked union types
7201 -- because it is not always guaranteed that the components are
7202 -- retrievable based on whether the discriminants are inferable
7204 if Has_Unchecked_Union
(P_Type
) then
7206 ("attribute % not allowed for Unchecked_Union type");
7211 Set_Etype
(N
, Standard_Boolean
);
7218 when Attribute_Value
7219 | Attribute_Wide_Value
7220 | Attribute_Wide_Wide_Value
7224 Check_Enum_Image
(Check_Enumeration_Maps
=> True);
7226 -- Set Etype before resolving expression because expansion of
7227 -- expression may require enclosing type. Note that the type
7228 -- returned by 'Value is the base type of the prefix type.
7230 Set_Etype
(N
, P_Base_Type
);
7231 Validate_Non_Static_Attribute_Function_Call
;
7233 -- Check restriction No_Fixed_IO
7235 if Restriction_Check_Required
(No_Fixed_IO
)
7236 and then Is_Fixed_Point_Type
(P_Type
)
7238 Check_Restriction
(No_Fixed_IO
, P
);
7245 -- Shares processing with Machine_Size attribute
7251 when Attribute_Version
=>
7254 Set_Etype
(N
, RTE
(RE_Version_String
));
7260 when Attribute_Wchar_T_Size
=>
7261 Standard_Attribute
(Interfaces_Wchar_T_Size
);
7267 when Attribute_Wide_Image
=>
7268 Analyze_Image_Attribute
(Standard_Wide_String
);
7270 ---------------------
7271 -- Wide_Wide_Image --
7272 ---------------------
7274 when Attribute_Wide_Wide_Image
=>
7275 Analyze_Image_Attribute
(Standard_Wide_Wide_String
);
7281 -- Shares processing with Value attribute
7283 ---------------------
7284 -- Wide_Wide_Value --
7285 ---------------------
7287 -- Shares processing with Value attribute
7289 ---------------------
7290 -- Wide_Wide_Width --
7291 ---------------------
7293 when Attribute_Wide_Wide_Width
7294 | Attribute_Wide_Width
7299 Set_Etype
(N
, Universal_Integer
);
7305 -- Shares processing with Wide_Wide_Width attribute
7311 -- Shares processing with Wide_Wide_Width attribute
7317 when Attribute_Word_Size
=>
7318 Standard_Attribute
(System_Word_Size
);
7324 when Attribute_Write
=>
7326 Check_Stream_Attribute
(TSS_Stream_Write
);
7327 Set_Etype
(N
, Standard_Void_Type
);
7328 Resolve
(N
, Standard_Void_Type
);
7332 -- In SPARK certain attributes (see below) depend on Tasking_State.
7333 -- Ensure that the entity is available for gnat2why by loading it.
7334 -- See SPARK RM 9(18) for the relevant rule.
7336 if GNATprove_Mode
then
7338 when Attribute_Callable
7341 | Attribute_Terminated
7343 SPARK_Implicit_Load
(RE_Tasking_State
);
7350 -- All errors raise Bad_Attribute, so that we get out before any further
7351 -- damage occurs when an error is detected (for example, if we check for
7352 -- one attribute expression, and the check succeeds, we want to be able
7353 -- to proceed securely assuming that an expression is in fact present.
7355 -- Note: we set the attribute analyzed in this case to prevent any
7356 -- attempt at reanalysis which could generate spurious error msgs.
7359 when Bad_Attribute
=>
7361 Set_Etype
(N
, Any_Type
);
7363 end Analyze_Attribute
;
7365 --------------------
7366 -- Eval_Attribute --
7367 --------------------
7369 procedure Eval_Attribute
(N
: Node_Id
) is
7370 Loc
: constant Source_Ptr
:= Sloc
(N
);
7372 C_Type
: constant Entity_Id
:= Etype
(N
);
7373 -- The type imposed by the context
7376 -- Attribute_Name (N) after verification of validity of N
7379 -- Get_Attribute_Id (Aname) after Aname is set
7382 -- Prefix (N) after verification of validity of N
7385 -- First expression, or Empty if none
7388 -- Second expression, or Empty if none
7390 P_Entity
: Entity_Id
;
7391 -- Entity denoted by prefix
7394 -- The type of the prefix
7396 P_Base_Type
: Entity_Id
;
7397 -- The base type of the prefix type
7399 P_Root_Type
: Entity_Id
;
7400 -- The root type of the prefix type
7402 Static
: Boolean := False;
7403 -- True if the result is Static. This is set by the general processing
7404 -- to true if the prefix is static, and all expressions are static. It
7405 -- can be reset as processing continues for particular attributes. This
7406 -- flag can still be True if the reference raises a constraint error.
7407 -- Is_Static_Expression (N) is set to follow this value as it is set
7408 -- and we could always reference this, but it is convenient to have a
7409 -- simple short name to use, since it is frequently referenced.
7411 Lo_Bound
, Hi_Bound
: Node_Id
;
7412 -- Expressions for low and high bounds of type or array index referenced
7413 -- by First, Last, or Length attribute for array, set by Set_Bounds.
7416 -- Constraint error node used if we have an attribute reference has
7417 -- an argument that raises a constraint error. In this case we replace
7418 -- the attribute with a raise constraint_error node. This is important
7419 -- processing, since otherwise gigi might see an attribute which it is
7420 -- unprepared to deal with.
7422 procedure Check_Concurrent_Discriminant
(Bound
: Node_Id
);
7423 -- If Bound is a reference to a discriminant of a task or protected type
7424 -- occurring within the object's body, rewrite attribute reference into
7425 -- a reference to the corresponding discriminal. Use for the expansion
7426 -- of checks against bounds of entry family index subtypes.
7428 procedure Check_Expressions
;
7429 -- In case where the attribute is not foldable, the expressions, if
7430 -- any, of the attribute, are in a non-static context. This procedure
7431 -- performs the required additional checks.
7433 function Compile_Time_Known_Bounds
(Typ
: Entity_Id
) return Boolean;
7434 -- Determines if the given type has compile time known bounds. Note
7435 -- that we enter the case statement even in cases where the prefix
7436 -- type does NOT have known bounds, so it is important to guard any
7437 -- attempt to evaluate both bounds with a call to this function.
7439 procedure Compile_Time_Known_Attribute
(N
: Node_Id
; Val
: Uint
);
7440 -- This procedure is called when the attribute N has a non-static
7441 -- but compile time known value given by Val. It includes the
7442 -- necessary checks for out of range values.
7444 function Fore_Value
return Nat
;
7445 -- Computes the Fore value for the current attribute prefix, which is
7446 -- known to be a static fixed-point type. Used by Fore and Width.
7448 function Mantissa
return Uint
;
7449 -- Returns the Mantissa value for the prefix type
7451 procedure Set_Bounds
;
7452 -- Used for First, Last and Length attributes applied to an array or
7453 -- array subtype. Sets the variables Lo_Bound and Hi_Bound to the low
7454 -- and high bound expressions for the index referenced by the attribute
7455 -- designator (i.e. the first index if no expression is present, and the
7456 -- N'th index if the value N is present as an expression). Also used for
7457 -- First and Last of scalar types and for First_Valid and Last_Valid.
7458 -- Static is reset to False if the type or index type is not statically
7461 -----------------------------------
7462 -- Check_Concurrent_Discriminant --
7463 -----------------------------------
7465 procedure Check_Concurrent_Discriminant
(Bound
: Node_Id
) is
7467 -- The concurrent (task or protected) type
7470 if Nkind
(Bound
) = N_Identifier
7471 and then Ekind
(Entity
(Bound
)) = E_Discriminant
7472 and then Is_Concurrent_Record_Type
(Scope
(Entity
(Bound
)))
7474 Tsk
:= Corresponding_Concurrent_Type
(Scope
(Entity
(Bound
)));
7476 if In_Open_Scopes
(Tsk
) and then Has_Completion
(Tsk
) then
7478 -- Find discriminant of original concurrent type, and use
7479 -- its current discriminal, which is the renaming within
7480 -- the task/protected body.
7484 (Find_Body_Discriminal
(Entity
(Bound
)), Loc
));
7487 end Check_Concurrent_Discriminant
;
7489 -----------------------
7490 -- Check_Expressions --
7491 -----------------------
7493 procedure Check_Expressions
is
7497 while Present
(E
) loop
7498 Check_Non_Static_Context
(E
);
7501 end Check_Expressions
;
7503 ----------------------------------
7504 -- Compile_Time_Known_Attribute --
7505 ----------------------------------
7507 procedure Compile_Time_Known_Attribute
(N
: Node_Id
; Val
: Uint
) is
7508 T
: constant Entity_Id
:= Etype
(N
);
7511 Fold_Uint
(N
, Val
, False);
7513 -- Check that result is in bounds of the type if it is static
7515 if Is_In_Range
(N
, T
, Assume_Valid
=> False) then
7518 elsif Is_Out_Of_Range
(N
, T
) then
7519 Apply_Compile_Time_Constraint_Error
7520 (N
, "value not in range of}??", CE_Range_Check_Failed
);
7522 elsif not Range_Checks_Suppressed
(T
) then
7523 Enable_Range_Check
(N
);
7526 Set_Do_Range_Check
(N
, False);
7528 end Compile_Time_Known_Attribute
;
7530 -------------------------------
7531 -- Compile_Time_Known_Bounds --
7532 -------------------------------
7534 function Compile_Time_Known_Bounds
(Typ
: Entity_Id
) return Boolean is
7537 Compile_Time_Known_Value
(Type_Low_Bound
(Typ
))
7539 Compile_Time_Known_Value
(Type_High_Bound
(Typ
));
7540 end Compile_Time_Known_Bounds
;
7546 -- Note that the Fore calculation is based on the actual values
7547 -- of the bounds, and does not take into account possible rounding.
7549 function Fore_Value
return Nat
is
7550 Lo
: constant Uint
:= Expr_Value
(Type_Low_Bound
(P_Type
));
7551 Hi
: constant Uint
:= Expr_Value
(Type_High_Bound
(P_Type
));
7552 Small
: constant Ureal
:= Small_Value
(P_Type
);
7553 Lo_Real
: constant Ureal
:= Lo
* Small
;
7554 Hi_Real
: constant Ureal
:= Hi
* Small
;
7559 -- Bounds are given in terms of small units, so first compute
7560 -- proper values as reals.
7562 T
:= UR_Max
(abs Lo_Real
, abs Hi_Real
);
7565 -- Loop to compute proper value if more than one digit required
7567 while T
>= Ureal_10
loop
7579 -- Table of mantissa values accessed by function Computed using
7582 -- T'Mantissa = integer next above (D * log(10)/log(2)) + 1)
7584 -- where D is T'Digits (RM83 3.5.7)
7586 Mantissa_Value
: constant array (Nat
range 1 .. 40) of Nat
:= (
7628 function Mantissa
return Uint
is
7631 UI_From_Int
(Mantissa_Value
(UI_To_Int
(Digits_Value
(P_Type
))));
7638 procedure Set_Bounds
is
7644 -- For a string literal subtype, we have to construct the bounds.
7645 -- Valid Ada code never applies attributes to string literals, but
7646 -- it is convenient to allow the expander to generate attribute
7647 -- references of this type (e.g. First and Last applied to a string
7650 -- Note that the whole point of the E_String_Literal_Subtype is to
7651 -- avoid this construction of bounds, but the cases in which we
7652 -- have to materialize them are rare enough that we don't worry.
7654 -- The low bound is simply the low bound of the base type. The
7655 -- high bound is computed from the length of the string and this
7658 if Ekind
(P_Type
) = E_String_Literal_Subtype
then
7659 Ityp
:= Etype
(First_Index
(Base_Type
(P_Type
)));
7660 Lo_Bound
:= Type_Low_Bound
(Ityp
);
7663 Make_Integer_Literal
(Sloc
(P
),
7665 Expr_Value
(Lo_Bound
) + String_Literal_Length
(P_Type
) - 1);
7667 Set_Parent
(Hi_Bound
, P
);
7668 Analyze_And_Resolve
(Hi_Bound
, Etype
(Lo_Bound
));
7671 -- For non-array case, just get bounds of scalar type
7673 elsif Is_Scalar_Type
(P_Type
) then
7676 -- For a fixed-point type, we must freeze to get the attributes
7677 -- of the fixed-point type set now so we can reference them.
7679 if Is_Fixed_Point_Type
(P_Type
)
7680 and then not Is_Frozen
(Base_Type
(P_Type
))
7681 and then Compile_Time_Known_Value
(Type_Low_Bound
(P_Type
))
7682 and then Compile_Time_Known_Value
(Type_High_Bound
(P_Type
))
7684 Freeze_Fixed_Point_Type
(Base_Type
(P_Type
));
7687 -- For array case, get type of proper index
7693 Ndim
:= UI_To_Int
(Expr_Value
(E1
));
7696 Indx
:= First_Index
(P_Type
);
7697 for J
in 1 .. Ndim
- 1 loop
7701 -- If no index type, get out (some other error occurred, and
7702 -- we don't have enough information to complete the job).
7710 Ityp
:= Etype
(Indx
);
7713 -- A discrete range in an index constraint is allowed to be a
7714 -- subtype indication. This is syntactically a pain, but should
7715 -- not propagate to the entity for the corresponding index subtype.
7716 -- After checking that the subtype indication is legal, the range
7717 -- of the subtype indication should be transfered to the entity.
7718 -- The attributes for the bounds should remain the simple retrievals
7719 -- that they are now.
7721 Lo_Bound
:= Type_Low_Bound
(Ityp
);
7722 Hi_Bound
:= Type_High_Bound
(Ityp
);
7724 -- If subtype is non-static, result is definitely non-static
7726 if not Is_Static_Subtype
(Ityp
) then
7728 Set_Is_Static_Expression
(N
, False);
7730 -- Subtype is static, does it raise CE?
7732 elsif not Is_OK_Static_Subtype
(Ityp
) then
7733 Set_Raises_Constraint_Error
(N
);
7737 -- Start of processing for Eval_Attribute
7740 -- Return immediately if e.g. N has been rewritten or is malformed due
7741 -- to previous errors.
7743 if Nkind
(N
) /= N_Attribute_Reference
then
7747 Aname
:= Attribute_Name
(N
);
7748 Id
:= Get_Attribute_Id
(Aname
);
7751 -- The To_Address attribute can be static, but it cannot be evaluated at
7752 -- compile time, so just return.
7754 if Id
= Attribute_To_Address
then
7758 -- Initialize result as non-static, will be reset if appropriate
7760 Set_Is_Static_Expression
(N
, False);
7762 -- Acquire first two expressions (at the moment, no attributes take more
7763 -- than two expressions in any case).
7765 if Present
(Expressions
(N
)) then
7766 E1
:= First
(Expressions
(N
));
7773 -- Special processing for Enabled attribute. This attribute has a very
7774 -- special prefix, and the easiest way to avoid lots of special checks
7775 -- to protect this special prefix from causing trouble is to deal with
7776 -- this attribute immediately and be done with it.
7778 if Id
= Attribute_Enabled
then
7780 -- We skip evaluation if the expander is not active. This is not just
7781 -- an optimization. It is of key importance that we not rewrite the
7782 -- attribute in a generic template, since we want to pick up the
7783 -- setting of the check in the instance.
7785 if not Inside_A_Generic
then
7787 C
: constant Check_Id
:= Get_Check_Id
(Chars
(P
));
7792 if C
in Predefined_Check_Id
then
7793 R
:= Scope_Suppress
.Suppress
(C
);
7795 R
:= Is_Check_Suppressed
(Empty
, C
);
7799 R
:= Is_Check_Suppressed
(Entity
(E1
), C
);
7802 Rewrite
(N
, New_Occurrence_Of
(Boolean_Literals
(not R
), Loc
));
7809 -- Attribute 'Img applied to a static enumeration value is static, and
7810 -- we will do the folding right here (things get confused if we let this
7811 -- case go through the normal circuitry).
7813 if Id
= Attribute_Img
7814 and then Is_Entity_Name
(P
)
7815 and then Is_Enumeration_Type
(Etype
(Entity
(P
)))
7816 and then Is_OK_Static_Expression
(P
)
7819 Lit
: constant Entity_Id
:= Expr_Value_E
(P
);
7824 Get_Unqualified_Decoded_Name_String
(Chars
(Lit
));
7825 Set_Casing
(All_Upper_Case
);
7826 Store_String_Chars
(Name_Buffer
(1 .. Name_Len
));
7829 Rewrite
(N
, Make_String_Literal
(Loc
, Strval
=> Str
));
7830 Analyze_And_Resolve
(N
, Standard_String
);
7831 Set_Is_Static_Expression
(N
, True);
7837 -- Special processing for cases where the prefix is an object or value,
7838 -- including string literals (attributes of string literals can only
7839 -- appear in generated code) and current instance prefixes in type or
7842 if Is_Object_Reference
(P
)
7843 or else Is_Current_Instance_Reference_In_Type_Aspect
(P
)
7844 or else Nkind
(P
) = N_String_Literal
7845 or else (Is_Entity_Name
(P
)
7846 and then Ekind
(Entity
(P
)) = E_Enumeration_Literal
)
7848 -- For Alignment, give alignment of object if available, otherwise we
7849 -- cannot fold Alignment.
7851 if Id
= Attribute_Alignment
then
7852 if Is_Entity_Name
(P
) and then Known_Alignment
(Entity
(P
)) then
7853 Compile_Time_Known_Attribute
(N
, Alignment
(Entity
(P
)));
7860 -- For Component_Size, the prefix is an array object, and we apply
7861 -- the attribute to the type of the object. This is allowed for both
7862 -- unconstrained and constrained arrays, since the bounds have no
7863 -- influence on the value of this attribute.
7865 elsif Id
= Attribute_Component_Size
then
7866 P_Entity
:= Etype
(P
);
7868 -- For Enum_Rep, evaluation depends on the nature of the prefix and
7869 -- the optional argument.
7871 elsif Id
= Attribute_Enum_Rep
then
7872 if Is_Entity_Name
(P
) then
7875 Enum_Expr
: Node_Id
;
7876 -- The enumeration-type expression of interest
7881 if Ekind
(Entity
(P
)) in E_Constant | E_Enumeration_Literal
7885 -- Enum_Type'Enum_Rep (E1) case
7887 elsif Is_Enumeration_Type
(Entity
(P
)) then
7890 -- Otherwise the attribute must be expanded into a
7891 -- conversion and evaluated at run time.
7898 -- We can fold if the expression is an enumeration
7899 -- literal, or if it denotes a constant whose value
7900 -- is known at compile time.
7902 if Nkind
(Enum_Expr
) in N_Has_Entity
7903 and then (Ekind
(Entity
(Enum_Expr
)) =
7904 E_Enumeration_Literal
7906 (Ekind
(Entity
(Enum_Expr
)) = E_Constant
7907 and then Nkind
(Parent
(Entity
(Enum_Expr
))) =
7908 N_Object_Declaration
7910 (Expression
(Parent
(Entity
(P
))))
7911 and then Compile_Time_Known_Value
7912 (Expression
(Parent
(Entity
(P
))))))
7914 P_Entity
:= Etype
(P
);
7921 -- Otherwise the attribute is illegal, do not attempt to perform
7922 -- any kind of folding.
7928 -- For Bit_Position, give Component_Bit_Offset of object if available
7929 -- otherwise we cannot fold Bit_Position. Note that the attribute can
7930 -- be applied to a naked record component in generated code, in which
7931 -- case the prefix is an identifier that references the component or
7932 -- discriminant entity.
7934 elsif Id
= Attribute_Bit_Position
then
7939 if Is_Entity_Name
(P
) then
7942 CE
:= Entity
(Selector_Name
(P
));
7945 if Known_Static_Component_Bit_Offset
(CE
) then
7946 Compile_Time_Known_Attribute
7947 (N
, Component_Bit_Offset
(CE
));
7955 -- For Position, in Ada 2005 (or later) if we have the non-default
7956 -- bit order, we return the original value as given in the component
7957 -- clause (RM 2005 13.5.2(3/2)). Otherwise (Ada 83/95, or later with
7958 -- default bit order) return the value if it is known statically.
7960 elsif Id
= Attribute_Position
then
7962 CE
: constant Entity_Id
:= Entity
(Selector_Name
(P
));
7965 if Present
(Component_Clause
(CE
))
7966 and then Ada_Version
>= Ada_2005
7967 and then Reverse_Bit_Order
(Scope
(CE
))
7969 Compile_Time_Known_Attribute
7970 (N
, Expr_Value
(Position
(Component_Clause
(CE
))));
7972 elsif Known_Static_Component_Bit_Offset
(CE
) then
7973 Compile_Time_Known_Attribute
7974 (N
, Component_Bit_Offset
(CE
) / System_Storage_Unit
);
7983 -- For First_Bit, in Ada 2005 (or later) if we have the non-default
7984 -- bit order, we return the original value as given in the component
7985 -- clause (RM 2005 13.5.2(3/2)). Otherwise (Ada 83/95, or later with
7986 -- default bit order) return the value if it is known statically.
7988 elsif Id
= Attribute_First_Bit
then
7990 CE
: constant Entity_Id
:= Entity
(Selector_Name
(P
));
7993 if Present
(Component_Clause
(CE
))
7994 and then Ada_Version
>= Ada_2005
7995 and then Reverse_Bit_Order
(Scope
(CE
))
7997 Compile_Time_Known_Attribute
7998 (N
, Expr_Value
(First_Bit
(Component_Clause
(CE
))));
8000 elsif Known_Static_Component_Bit_Offset
(CE
) then
8001 Compile_Time_Known_Attribute
8002 (N
, Component_Bit_Offset
(CE
) mod System_Storage_Unit
);
8011 -- For Last_Bit, in Ada 2005 (or later) if we have the non-default
8012 -- bit order, we return the original value as given in the component
8013 -- clause (RM 2005 13.5.2(3/2)). Otherwise (Ada 83/95, or later with
8014 -- default bit order) return the value if it is known statically.
8016 elsif Id
= Attribute_Last_Bit
then
8018 CE
: constant Entity_Id
:= Entity
(Selector_Name
(P
));
8021 if Present
(Component_Clause
(CE
))
8022 and then Ada_Version
>= Ada_2005
8023 and then Reverse_Bit_Order
(Scope
(CE
))
8025 Compile_Time_Known_Attribute
8026 (N
, Expr_Value
(Last_Bit
(Component_Clause
(CE
))));
8028 elsif Known_Static_Component_Bit_Offset
(CE
)
8029 and then Known_Static_Esize
(CE
)
8031 Compile_Time_Known_Attribute
8032 (N
, (Component_Bit_Offset
(CE
) mod System_Storage_Unit
)
8041 -- For First, Last and Length, the prefix is an array object, and we
8042 -- apply the attribute to its type, but we need a constrained type
8043 -- for this, so we use the actual subtype if available.
8045 elsif Id
= Attribute_First
8046 or else Id
= Attribute_Last
8047 or else Id
= Attribute_Length
8050 AS
: constant Entity_Id
:= Get_Actual_Subtype_If_Available
(P
);
8053 if Present
(AS
) and then Is_Constrained
(AS
) then
8056 -- If we have an unconstrained type we cannot fold
8064 elsif Id
= Attribute_Size
then
8065 -- For Enum_Lit'Size, use Enum_Type'Object_Size. Taking the 'Size
8066 -- of a literal is kind of a strange thing to do, so we don't want
8067 -- to pass this oddity on to the back end. Note that Etype of an
8068 -- enumeration literal is always a (base) type, never a
8069 -- constrained subtype, so the Esize is always known.
8071 if Is_Entity_Name
(P
)
8072 and then Ekind
(Entity
(P
)) = E_Enumeration_Literal
8074 pragma Assert
(Known_Static_Esize
(Etype
(P
)));
8075 Compile_Time_Known_Attribute
(N
, Esize
(Etype
(P
)));
8077 -- Otherwise, if Size is available, use that
8079 elsif Is_Entity_Name
(P
) and then Known_Static_Esize
(Entity
(P
))
8081 Compile_Time_Known_Attribute
(N
, Esize
(Entity
(P
)));
8083 -- Otherwise, we cannot fold
8091 -- For Lock_Free, we apply the attribute to the type of the object.
8092 -- This is allowed since we have already verified that the type is a
8095 elsif Id
= Attribute_Lock_Free
then
8096 P_Entity
:= Etype
(P
);
8098 -- No other attributes for objects are folded
8105 -- Cases where P is not an object. Cannot do anything if P is not the
8106 -- name of an entity.
8108 elsif not Is_Entity_Name
(P
) then
8112 -- Otherwise get prefix entity
8115 P_Entity
:= Entity
(P
);
8118 -- If we are asked to evaluate an attribute where the prefix is a
8119 -- non-frozen generic actual type whose RM_Size has not been set,
8120 -- then abandon the effort.
8122 if Is_Type
(P_Entity
)
8123 and then (not Is_Frozen
(P_Entity
)
8124 and then Is_Generic_Actual_Type
(P_Entity
)
8125 and then not Known_RM_Size
(P_Entity
))
8127 -- However, the attribute Unconstrained_Array must be evaluated,
8128 -- since it is documented to be a static attribute (and can for
8129 -- example appear in a Compile_Time_Warning pragma). The frozen
8130 -- status of the type does not affect its evaluation.
8132 and then Id
/= Attribute_Unconstrained_Array
8137 -- At this stage P_Entity is the entity to which the attribute
8138 -- is to be applied. This is usually simply the entity of the
8139 -- prefix, except in some cases of attributes for objects, where
8140 -- as described above, we apply the attribute to the object type.
8142 -- Here is where we make sure that static attributes are properly
8143 -- marked as such. These are attributes whose prefix is a static
8144 -- scalar subtype, whose result is scalar, and whose arguments, if
8145 -- present, are static scalar expressions. Note that such references
8146 -- are static expressions even if they raise Constraint_Error.
8148 -- For example, Boolean'Pos (1/0 = 0) is a static expression, even
8149 -- though evaluating it raises constraint error. This means that a
8150 -- declaration like:
8152 -- X : constant := (if True then 1 else Boolean'Pos (1/0 = 0));
8154 -- is legal, since here this expression appears in a statically
8155 -- unevaluated position, so it does not actually raise an exception.
8157 -- T'Descriptor_Size is never static, even if T is static.
8159 if Is_Scalar_Type
(P_Entity
)
8160 and then not Is_Generic_Type
(P_Entity
)
8161 and then Is_Static_Subtype
(P_Entity
)
8162 and then Is_Scalar_Type
(Etype
(N
))
8165 or else (Is_Static_Expression
(E1
)
8166 and then Is_Scalar_Type
(Etype
(E1
))))
8169 or else (Is_Static_Expression
(E2
)
8170 and then Is_Scalar_Type
(Etype
(E1
))))
8171 and then Id
/= Attribute_Descriptor_Size
8174 Set_Is_Static_Expression
(N
, True);
8177 -- First foldable possibility is a scalar or array type (RM 4.9(7))
8178 -- that is not generic (generic types are eliminated by RM 4.9(25)).
8179 -- Note we allow nonstatic nongeneric types at this stage as further
8182 if Is_Type
(P_Entity
)
8183 and then (Is_Scalar_Type
(P_Entity
) or Is_Array_Type
(P_Entity
))
8184 and then not Is_Generic_Type
(P_Entity
)
8188 -- Second foldable possibility is an array object (RM 4.9(8))
8190 elsif Ekind
(P_Entity
) in E_Variable | E_Constant
8191 and then Is_Array_Type
(Etype
(P_Entity
))
8192 and then not Is_Generic_Type
(Etype
(P_Entity
))
8194 P_Type
:= Etype
(P_Entity
);
8196 -- If the entity is an array constant with an unconstrained nominal
8197 -- subtype then get the type from the initial value. If the value has
8198 -- been expanded into assignments, there is no expression and the
8199 -- attribute reference remains dynamic.
8201 -- We could do better here and retrieve the type ???
8203 if Ekind
(P_Entity
) = E_Constant
8204 and then not Is_Constrained
(P_Type
)
8206 if No
(Constant_Value
(P_Entity
)) then
8209 P_Type
:= Etype
(Constant_Value
(P_Entity
));
8213 -- Definite must be folded if the prefix is not a generic type, that
8214 -- is to say if we are within an instantiation. Same processing applies
8215 -- to selected GNAT attributes.
8217 elsif (Id
= Attribute_Atomic_Always_Lock_Free
or else
8218 Id
= Attribute_Definite
or else
8219 Id
= Attribute_Descriptor_Size
or else
8220 Id
= Attribute_Has_Access_Values
or else
8221 Id
= Attribute_Has_Discriminants
or else
8222 Id
= Attribute_Has_Tagged_Values
or else
8223 Id
= Attribute_Lock_Free
or else
8224 Id
= Attribute_Preelaborable_Initialization
or else
8225 Id
= Attribute_Type_Class
or else
8226 Id
= Attribute_Unconstrained_Array
or else
8227 Id
= Attribute_Max_Alignment_For_Allocation
)
8228 and then not Is_Generic_Type
(P_Entity
)
8232 -- We can fold 'Size applied to a type if the size is known (as happens
8233 -- for a size from an attribute definition clause). At this stage, this
8234 -- can happen only for types (e.g. record types) for which the size is
8235 -- always non-static. We exclude generic types from consideration (since
8236 -- they have bogus sizes set within templates). We can also fold
8237 -- Max_Size_In_Storage_Elements in the same cases.
8239 elsif (Id
= Attribute_Size
or
8240 Id
= Attribute_Max_Size_In_Storage_Elements
)
8241 and then Is_Type
(P_Entity
)
8242 and then not Is_Generic_Type
(P_Entity
)
8243 and then Known_Static_RM_Size
(P_Entity
)
8246 Attr_Value
: Uint
:= RM_Size
(P_Entity
);
8248 if Id
= Attribute_Max_Size_In_Storage_Elements
then
8249 Attr_Value
:= (Attr_Value
+ System_Storage_Unit
- 1)
8250 / System_Storage_Unit
;
8252 Compile_Time_Known_Attribute
(N
, Attr_Value
);
8256 -- We can fold 'Alignment applied to a type if the alignment is known
8257 -- (as happens for an alignment from an attribute definition clause).
8258 -- At this stage, this can happen only for types (e.g. record types) for
8259 -- which the size is always non-static. We exclude generic types from
8260 -- consideration (since they have bogus sizes set within templates).
8262 elsif Id
= Attribute_Alignment
8263 and then Is_Type
(P_Entity
)
8264 and then not Is_Generic_Type
(P_Entity
)
8265 and then Known_Alignment
(P_Entity
)
8267 Compile_Time_Known_Attribute
(N
, Alignment
(P_Entity
));
8270 -- If this is an access attribute that is known to fail accessibility
8271 -- check, rewrite accordingly.
8273 elsif Id
= Attribute_Address
8274 and then Raises_Constraint_Error
(N
)
8277 Make_Raise_Program_Error
(Loc
,
8278 Reason
=> PE_Accessibility_Check_Failed
));
8279 Set_Etype
(N
, C_Type
);
8282 -- No other cases are foldable (they certainly aren't static, and at
8283 -- the moment we don't try to fold any cases other than the ones above).
8290 -- If either attribute or the prefix is Any_Type, then propagate
8291 -- Any_Type to the result and don't do anything else at all.
8293 if P_Type
= Any_Type
8294 or else (Present
(E1
) and then Etype
(E1
) = Any_Type
)
8295 or else (Present
(E2
) and then Etype
(E2
) = Any_Type
)
8297 Set_Etype
(N
, Any_Type
);
8301 -- Scalar subtype case. We have not yet enforced the static requirement
8302 -- of (RM 4.9(7)) and we don't intend to just yet, since there are cases
8303 -- of non-static attribute references (e.g. S'Digits for a non-static
8304 -- floating-point type, which we can compute at compile time).
8306 -- Note: this folding of non-static attributes is not simply a case of
8307 -- optimization. For many of the attributes affected, Gigi cannot handle
8308 -- the attribute and depends on the front end having folded them away.
8310 -- Note: although we don't require staticness at this stage, we do set
8311 -- the Static variable to record the staticness, for easy reference by
8312 -- those attributes where it matters (e.g. Succ and Pred), and also to
8313 -- be used to ensure that non-static folded things are not marked as
8314 -- being static (a check that is done right at the end).
8316 P_Root_Type
:= Root_Type
(P_Type
);
8317 P_Base_Type
:= Base_Type
(P_Type
);
8319 -- If the root type or base type is generic, then we cannot fold. This
8320 -- test is needed because subtypes of generic types are not always
8321 -- marked as being generic themselves (which seems odd???)
8323 if Is_Generic_Type
(P_Root_Type
)
8324 or else Is_Generic_Type
(P_Base_Type
)
8329 if Is_Scalar_Type
(P_Type
) then
8330 if not Is_Static_Subtype
(P_Type
) then
8332 Set_Is_Static_Expression
(N
, False);
8333 elsif not Is_OK_Static_Subtype
(P_Type
) then
8334 Set_Raises_Constraint_Error
(N
);
8337 -- Array case. We enforce the constrained requirement of (RM 4.9(7-8))
8338 -- since we can't do anything with unconstrained arrays. In addition,
8339 -- only the First, Last and Length attributes are possibly static.
8341 -- Atomic_Always_Lock_Free, Definite, Descriptor_Size, Has_Access_Values
8342 -- Has_Discriminants, Has_Tagged_Values, Lock_Free, Type_Class, and
8343 -- Unconstrained_Array are again exceptions, because they apply as well
8344 -- to unconstrained types.
8346 -- In addition Component_Size is an exception since it is possibly
8347 -- foldable, even though it is never static, and it does apply to
8348 -- unconstrained arrays. Furthermore, it is essential to fold this
8349 -- in the packed case, since otherwise the value will be incorrect.
8351 -- Folding can also be done for Preelaborable_Initialization based on
8352 -- whether the prefix type has preelaborable initialization, even though
8353 -- the attribute is nonstatic.
8355 elsif Id
= Attribute_Atomic_Always_Lock_Free
or else
8356 Id
= Attribute_Definite
or else
8357 Id
= Attribute_Descriptor_Size
or else
8358 Id
= Attribute_Has_Access_Values
or else
8359 Id
= Attribute_Has_Discriminants
or else
8360 Id
= Attribute_Has_Tagged_Values
or else
8361 Id
= Attribute_Lock_Free
or else
8362 Id
= Attribute_Preelaborable_Initialization
or else
8363 Id
= Attribute_Type_Class
or else
8364 Id
= Attribute_Unconstrained_Array
or else
8365 Id
= Attribute_Component_Size
8368 Set_Is_Static_Expression
(N
, False);
8370 elsif Id
/= Attribute_Max_Alignment_For_Allocation
then
8371 if not Is_Constrained
(P_Type
)
8372 or else (Id
/= Attribute_First
and then
8373 Id
/= Attribute_Last
and then
8374 Id
/= Attribute_Length
)
8380 -- The rules in (RM 4.9(7,8)) require a static array, but as in the
8381 -- scalar case, we hold off on enforcing staticness, since there are
8382 -- cases which we can fold at compile time even though they are not
8383 -- static (e.g. 'Length applied to a static index, even though other
8384 -- non-static indexes make the array type non-static). This is only
8385 -- an optimization, but it falls out essentially free, so why not.
8386 -- Again we compute the variable Static for easy reference later
8387 -- (note that no array attributes are static in Ada 83).
8389 -- We also need to set Static properly for subsequent legality checks
8390 -- which might otherwise accept non-static constants in contexts
8391 -- where they are not legal.
8394 Ada_Version
>= Ada_95
and then Statically_Denotes_Entity
(P
);
8395 Set_Is_Static_Expression
(N
, Static
);
8401 Nod
:= First_Index
(P_Type
);
8403 -- The expression is static if the array type is constrained
8404 -- by given bounds, and not by an initial expression. Constant
8405 -- strings are static in any case.
8407 if Root_Type
(P_Type
) /= Standard_String
then
8409 Static
and then not Is_Constr_Subt_For_U_Nominal
(P_Type
);
8410 Set_Is_Static_Expression
(N
, Static
);
8413 while Present
(Nod
) loop
8414 if not Is_Static_Subtype
(Etype
(Nod
)) then
8416 Set_Is_Static_Expression
(N
, False);
8418 elsif not Is_OK_Static_Subtype
(Etype
(Nod
)) then
8419 Set_Raises_Constraint_Error
(N
);
8421 Set_Is_Static_Expression
(N
, False);
8424 -- If however the index type is generic, or derived from
8425 -- one, attributes cannot be folded.
8427 if Is_Generic_Type
(Root_Type
(Etype
(Nod
)))
8428 and then Id
/= Attribute_Component_Size
8438 -- Check any expressions that are present. Note that these expressions,
8439 -- depending on the particular attribute type, are either part of the
8440 -- attribute designator, or they are arguments in a case where the
8441 -- attribute reference returns a function. In the latter case, the
8442 -- rule in (RM 4.9(22)) applies and in particular requires the type
8443 -- of the expressions to be scalar in order for the attribute to be
8444 -- considered to be static.
8452 while Present
(E
) loop
8454 -- If expression is not static, then the attribute reference
8455 -- result certainly cannot be static.
8457 if not Is_Static_Expression
(E
) then
8459 Set_Is_Static_Expression
(N
, False);
8462 if Raises_Constraint_Error
(E
) then
8463 Set_Raises_Constraint_Error
(N
);
8466 -- If the result is not known at compile time, or is not of
8467 -- a scalar type, then the result is definitely not static,
8468 -- so we can quit now.
8470 if not Compile_Time_Known_Value
(E
)
8471 or else not Is_Scalar_Type
(Etype
(E
))
8476 -- If the expression raises a constraint error, then so does
8477 -- the attribute reference. We keep going in this case because
8478 -- we are still interested in whether the attribute reference
8479 -- is static even if it is not static.
8481 elsif Raises_Constraint_Error
(E
) then
8482 Set_Raises_Constraint_Error
(N
);
8488 if Raises_Constraint_Error
(Prefix
(N
)) then
8489 Set_Is_Static_Expression
(N
, False);
8494 -- Deal with the case of a static attribute reference that raises
8495 -- constraint error. The Raises_Constraint_Error flag will already
8496 -- have been set, and the Static flag shows whether the attribute
8497 -- reference is static. In any case we certainly can't fold such an
8498 -- attribute reference.
8500 -- Note that the rewriting of the attribute node with the constraint
8501 -- error node is essential in this case, because otherwise Gigi might
8502 -- blow up on one of the attributes it never expects to see.
8504 -- The constraint_error node must have the type imposed by the context,
8505 -- to avoid spurious errors in the enclosing expression.
8507 if Raises_Constraint_Error
(N
) then
8509 Make_Raise_Constraint_Error
(Sloc
(N
),
8510 Reason
=> CE_Range_Check_Failed
);
8511 Set_Etype
(CE_Node
, Etype
(N
));
8512 Set_Raises_Constraint_Error
(CE_Node
);
8514 Rewrite
(N
, Relocate_Node
(CE_Node
));
8515 Set_Raises_Constraint_Error
(N
, True);
8519 -- At this point we have a potentially foldable attribute reference.
8520 -- If Static is set, then the attribute reference definitely obeys
8521 -- the requirements in (RM 4.9(7,8,22)), and it definitely can be
8522 -- folded. If Static is not set, then the attribute may or may not
8523 -- be foldable, and the individual attribute processing routines
8524 -- test Static as required in cases where it makes a difference.
8526 -- In the case where Static is not set, we do know that all the
8527 -- expressions present are at least known at compile time (we assumed
8528 -- above that if this was not the case, then there was no hope of static
8529 -- evaluation). However, we did not require that the bounds of the
8530 -- prefix type be compile time known, let alone static). That's because
8531 -- there are many attributes that can be computed at compile time on
8532 -- non-static subtypes, even though such references are not static
8535 -- For VAX float, the root type is an IEEE type. So make sure to use the
8536 -- base type instead of the root-type for floating point attributes.
8540 -- Attributes related to Ada 2012 iterators; nothing to evaluate for
8543 when Attribute_Constant_Indexing
8544 | Attribute_Default_Iterator
8545 | Attribute_Implicit_Dereference
8546 | Attribute_Iterator_Element
8547 | Attribute_Iterable
8549 | Attribute_Variable_Indexing
8553 -- Internal attributes used to deal with Ada 2012 delayed aspects.
8554 -- These were already rejected by the parser. Thus they shouldn't
8557 when Internal_Attribute_Id
=>
8558 raise Program_Error
;
8564 when Attribute_Adjacent
=>
8568 (P_Base_Type
, Expr_Value_R
(E1
), Expr_Value_R
(E2
)),
8575 when Attribute_Aft
=>
8576 Fold_Uint
(N
, Aft_Value
(P_Type
), Static
);
8582 when Attribute_Alignment
=> Alignment_Block
: declare
8583 P_TypeA
: constant Entity_Id
:= Underlying_Type
(P_Type
);
8586 -- Fold if alignment is set and not otherwise
8588 if Known_Alignment
(P_TypeA
) then
8589 Fold_Uint
(N
, Alignment
(P_TypeA
), Static
);
8591 end Alignment_Block
;
8593 -----------------------------
8594 -- Atomic_Always_Lock_Free --
8595 -----------------------------
8597 -- Atomic_Always_Lock_Free attribute is a Boolean, thus no need to fold
8600 when Attribute_Atomic_Always_Lock_Free
=> Atomic_Always_Lock_Free
:
8602 V
: constant Entity_Id
:=
8604 (Support_Atomic_Primitives_On_Target
8605 and then Support_Atomic_Primitives
(P_Type
));
8608 Rewrite
(N
, New_Occurrence_Of
(V
, Loc
));
8610 -- Analyze and resolve as boolean. Note that this attribute is a
8611 -- static attribute in GNAT.
8613 Analyze_And_Resolve
(N
, Standard_Boolean
);
8615 Set_Is_Static_Expression
(N
);
8616 end Atomic_Always_Lock_Free
;
8622 -- Bit can never be folded
8624 when Attribute_Bit
=>
8631 -- Body_version can never be static
8633 when Attribute_Body_Version
=>
8640 when Attribute_Ceiling
=>
8642 (N
, Eval_Fat
.Ceiling
(P_Base_Type
, Expr_Value_R
(E1
)), Static
);
8644 --------------------
8645 -- Component_Size --
8646 --------------------
8648 -- Fold Component_Size if it is known at compile time, which is always
8649 -- true in the packed array case. It is important that the packed array
8650 -- case is handled here since the back end would otherwise get confused
8651 -- by the equivalent packed array type.
8653 when Attribute_Component_Size
=>
8654 if Known_Static_Component_Size
(P_Type
) then
8655 Fold_Uint
(N
, Component_Size
(P_Type
), Static
);
8662 when Attribute_Compose
=>
8665 Eval_Fat
.Compose
(P_Base_Type
, Expr_Value_R
(E1
), Expr_Value
(E2
)),
8672 -- Constrained is never folded for now, there may be cases that
8673 -- could be handled at compile time. To be looked at later.
8675 when Attribute_Constrained
=>
8677 -- The expander might fold it and set the static flag accordingly,
8678 -- but with expansion disabled, it remains as an attribute reference,
8679 -- and this reference is not static.
8681 Set_Is_Static_Expression
(N
, False);
8687 when Attribute_Copy_Sign
=>
8691 (P_Base_Type
, Expr_Value_R
(E1
), Expr_Value_R
(E2
)),
8698 when Attribute_Definite
=>
8699 Rewrite
(N
, New_Occurrence_Of
(
8700 Boolean_Literals
(Is_Definite_Subtype
(P_Entity
)), Loc
));
8701 Analyze_And_Resolve
(N
, Standard_Boolean
);
8707 when Attribute_Delta
=>
8708 Fold_Ureal
(N
, Delta_Value
(P_Type
), True);
8714 when Attribute_Denorm
=>
8716 (N
, UI_From_Int
(Boolean'Pos (Has_Denormals
(P_Type
))), Static
);
8718 ---------------------
8719 -- Descriptor_Size --
8720 ---------------------
8722 -- Descriptor_Size is nonnull only for unconstrained array types
8724 when Attribute_Descriptor_Size
=>
8725 if not Is_Array_Type
(P_Type
) or else Is_Constrained
(P_Type
) then
8726 Fold_Uint
(N
, Uint_0
, Static
);
8733 when Attribute_Digits
=>
8734 Fold_Uint
(N
, Digits_Value
(P_Type
), Static
);
8740 when Attribute_Emax
=>
8742 -- Ada 83 attribute is defined as (RM83 3.5.8)
8744 -- T'Emax = 4 * T'Mantissa
8746 Fold_Uint
(N
, 4 * Mantissa
, Static
);
8752 when Attribute_Enum_Rep
=> Enum_Rep
: declare
8756 -- The attribute appears in the form:
8758 -- Enum_Typ'Enum_Rep (Const)
8759 -- Enum_Typ'Enum_Rep (Enum_Lit)
8761 if Present
(E1
) then
8764 -- Otherwise the prefix denotes a constant or enumeration literal:
8767 -- Enum_Lit'Enum_Rep
8773 -- For an enumeration type with a non-standard representation use
8774 -- the Enumeration_Rep field of the proper constant. Note that this
8775 -- will not work for types Character/Wide_[Wide-]Character, since no
8776 -- real entities are created for the enumeration literals, but that
8777 -- does not matter since these two types do not have non-standard
8778 -- representations anyway.
8780 if Is_Enumeration_Type
(P_Type
)
8781 and then Has_Non_Standard_Rep
(P_Type
)
8783 Fold_Uint
(N
, Enumeration_Rep
(Expr_Value_E
(Val
)), Static
);
8785 -- For enumeration types with standard representations and all other
8786 -- cases (i.e. all integer and modular types), Enum_Rep is equivalent
8790 Fold_Uint
(N
, Expr_Value
(Val
), Static
);
8798 when Attribute_Enum_Val
=> Enum_Val
: declare
8802 -- We have something like Enum_Type'Enum_Val (23), so search for a
8803 -- corresponding value in the list of Enum_Rep values for the type.
8805 Lit
:= First_Literal
(P_Base_Type
);
8807 if Enumeration_Rep
(Lit
) = Expr_Value
(E1
) then
8808 Fold_Uint
(N
, Enumeration_Pos
(Lit
), Static
);
8815 Apply_Compile_Time_Constraint_Error
8816 (N
, "no representation value matches",
8817 CE_Range_Check_Failed
,
8818 Warn
=> not Static
);
8828 when Attribute_Epsilon
=>
8830 -- Ada 83 attribute is defined as (RM83 3.5.8)
8832 -- T'Epsilon = 2.0**(1 - T'Mantissa)
8834 Fold_Ureal
(N
, Ureal_2
** (1 - Mantissa
), True);
8840 when Attribute_Exponent
=>
8842 Eval_Fat
.Exponent
(P_Base_Type
, Expr_Value_R
(E1
)), Static
);
8844 -----------------------
8845 -- Finalization_Size --
8846 -----------------------
8848 when Attribute_Finalization_Size
=>
8855 when Attribute_First
=>
8858 if Compile_Time_Known_Value
(Lo_Bound
) then
8859 if Is_Real_Type
(P_Type
) then
8860 Fold_Ureal
(N
, Expr_Value_R
(Lo_Bound
), Static
);
8862 Fold_Uint
(N
, Expr_Value
(Lo_Bound
), Static
);
8866 Check_Concurrent_Discriminant
(Lo_Bound
);
8873 when Attribute_First_Valid
=>
8874 if Has_Predicates
(P_Type
)
8875 and then Has_Static_Predicate
(P_Type
)
8878 FirstN
: constant Node_Id
:=
8879 First
(Static_Discrete_Predicate
(P_Type
));
8881 if Nkind
(FirstN
) = N_Range
then
8882 Fold_Uint
(N
, Expr_Value
(Low_Bound
(FirstN
)), Static
);
8884 Fold_Uint
(N
, Expr_Value
(FirstN
), Static
);
8890 Fold_Uint
(N
, Expr_Value
(Lo_Bound
), Static
);
8897 when Attribute_Fixed_Value
=>
8904 when Attribute_Floor
=>
8906 (N
, Eval_Fat
.Floor
(P_Base_Type
, Expr_Value_R
(E1
)), Static
);
8912 when Attribute_Fore
=>
8913 if Compile_Time_Known_Bounds
(P_Type
) then
8914 Fold_Uint
(N
, UI_From_Int
(Fore_Value
), Static
);
8921 when Attribute_Fraction
=>
8923 (N
, Eval_Fat
.Fraction
(P_Base_Type
, Expr_Value_R
(E1
)), Static
);
8925 -----------------------
8926 -- Has_Access_Values --
8927 -----------------------
8929 when Attribute_Has_Access_Values
=>
8930 Rewrite
(N
, New_Occurrence_Of
8931 (Boolean_Literals
(Has_Access_Values
(P_Root_Type
)), Loc
));
8932 Analyze_And_Resolve
(N
, Standard_Boolean
);
8934 -----------------------
8935 -- Has_Discriminants --
8936 -----------------------
8938 when Attribute_Has_Discriminants
=>
8939 Rewrite
(N
, New_Occurrence_Of
(
8940 Boolean_Literals
(Has_Discriminants
(P_Entity
)), Loc
));
8941 Analyze_And_Resolve
(N
, Standard_Boolean
);
8943 ----------------------
8944 -- Has_Same_Storage --
8945 ----------------------
8947 when Attribute_Has_Same_Storage
=>
8950 -----------------------
8951 -- Has_Tagged_Values --
8952 -----------------------
8954 when Attribute_Has_Tagged_Values
=>
8955 Rewrite
(N
, New_Occurrence_Of
8956 (Boolean_Literals
(Has_Tagged_Component
(P_Root_Type
)), Loc
));
8957 Analyze_And_Resolve
(N
, Standard_Boolean
);
8963 when Attribute_Identity
=>
8970 -- Image is a scalar attribute, but is never static, because it is
8971 -- not a static function (having a non-scalar argument (RM 4.9(22))
8972 -- However, we can constant-fold the image of an enumeration literal
8973 -- if names are available.
8975 when Attribute_Image
=>
8976 if Is_Entity_Name
(E1
)
8977 and then Ekind
(Entity
(E1
)) = E_Enumeration_Literal
8978 and then not Discard_Names
(First_Subtype
(Etype
(E1
)))
8979 and then not Global_Discard_Names
8982 Lit
: constant Entity_Id
:= Entity
(E1
);
8986 Get_Unqualified_Decoded_Name_String
(Chars
(Lit
));
8987 Set_Casing
(All_Upper_Case
);
8988 Store_String_Chars
(Name_Buffer
(1 .. Name_Len
));
8990 Rewrite
(N
, Make_String_Literal
(Loc
, Strval
=> Str
));
8991 Analyze_And_Resolve
(N
, Standard_String
);
8992 Set_Is_Static_Expression
(N
, False);
9000 -- We never try to fold Integer_Value (though perhaps we could???)
9002 when Attribute_Integer_Value
=>
9009 -- Invalid_Value is a scalar attribute that is never static, because
9010 -- the value is by design out of range.
9012 when Attribute_Invalid_Value
=>
9019 when Attribute_Large
=>
9021 -- For fixed-point, we use the identity:
9023 -- T'Large = (2.0**T'Mantissa - 1.0) * T'Small
9025 if Is_Fixed_Point_Type
(P_Type
) then
9027 Make_Op_Multiply
(Loc
,
9029 Make_Op_Subtract
(Loc
,
9033 Make_Real_Literal
(Loc
, Ureal_2
),
9035 Make_Attribute_Reference
(Loc
,
9037 Attribute_Name
=> Name_Mantissa
)),
9038 Right_Opnd
=> Make_Real_Literal
(Loc
, Ureal_1
)),
9041 Make_Real_Literal
(Loc
, Small_Value
(Entity
(P
)))));
9043 Analyze_And_Resolve
(N
, C_Type
);
9045 -- Floating-point (Ada 83 compatibility)
9048 -- Ada 83 attribute is defined as (RM83 3.5.8)
9050 -- T'Large = 2.0**T'Emax * (1.0 - 2.0**(-T'Mantissa))
9054 -- T'Emax = 4 * T'Mantissa
9058 Ureal_2
** (4 * Mantissa
) * (Ureal_1
- Ureal_2
** (-Mantissa
)),
9066 when Attribute_Lock_Free
=> Lock_Free
: declare
9067 V
: constant Entity_Id
:= Boolean_Literals
(Uses_Lock_Free
(P_Type
));
9070 Rewrite
(N
, New_Occurrence_Of
(V
, Loc
));
9072 -- Analyze and resolve as boolean. Note that this attribute is a
9073 -- static attribute in GNAT.
9075 Analyze_And_Resolve
(N
, Standard_Boolean
);
9077 Set_Is_Static_Expression
(N
);
9084 when Attribute_Last
=>
9087 if Compile_Time_Known_Value
(Hi_Bound
) then
9088 if Is_Real_Type
(P_Type
) then
9089 Fold_Ureal
(N
, Expr_Value_R
(Hi_Bound
), Static
);
9091 Fold_Uint
(N
, Expr_Value
(Hi_Bound
), Static
);
9095 Check_Concurrent_Discriminant
(Hi_Bound
);
9102 when Attribute_Last_Valid
=>
9103 if Has_Predicates
(P_Type
)
9104 and then Has_Static_Predicate
(P_Type
)
9107 LastN
: constant Node_Id
:=
9108 Last
(Static_Discrete_Predicate
(P_Type
));
9110 if Nkind
(LastN
) = N_Range
then
9111 Fold_Uint
(N
, Expr_Value
(High_Bound
(LastN
)), Static
);
9113 Fold_Uint
(N
, Expr_Value
(LastN
), Static
);
9119 Fold_Uint
(N
, Expr_Value
(Hi_Bound
), Static
);
9126 when Attribute_Leading_Part
=>
9129 Eval_Fat
.Leading_Part
9130 (P_Base_Type
, Expr_Value_R
(E1
), Expr_Value
(E2
)),
9137 when Attribute_Length
=> Length
: declare
9141 -- If any index type is a formal type, or derived from one, the
9142 -- bounds are not static. Treating them as static can produce
9143 -- spurious warnings or improper constant folding.
9145 Ind
:= First_Index
(P_Type
);
9146 while Present
(Ind
) loop
9147 if Is_Generic_Type
(Root_Type
(Etype
(Ind
))) then
9156 -- For two compile time values, we can compute length
9158 if Compile_Time_Known_Value
(Lo_Bound
)
9159 and then Compile_Time_Known_Value
(Hi_Bound
)
9162 UI_Max
(0, 1 + (Expr_Value
(Hi_Bound
) - Expr_Value
(Lo_Bound
))),
9166 -- One more case is where Hi_Bound and Lo_Bound are compile-time
9167 -- comparable, and we can figure out the difference between them.
9170 Diff
: aliased Uint
;
9174 Compile_Time_Compare
9175 (Lo_Bound
, Hi_Bound
, Diff
'Access, Assume_Valid
=> False)
9178 Fold_Uint
(N
, Uint_1
, Static
);
9181 Fold_Uint
(N
, Uint_0
, Static
);
9184 if Present
(Diff
) then
9185 Fold_Uint
(N
, Diff
+ 1, Static
);
9198 -- Loop_Entry acts as an alias of a constant initialized to the prefix
9199 -- of the said attribute at the point of entry into the related loop. As
9200 -- such, the attribute reference does not need to be evaluated because
9201 -- the prefix is the one that is evaluted.
9203 when Attribute_Loop_Entry
=>
9210 -- We use the same rounding mode as the one used for RM 4.9(38)
9212 when Attribute_Machine
=>
9216 (P_Base_Type
, Expr_Value_R
(E1
), Eval_Fat
.Round_Even
, N
),
9223 when Attribute_Machine_Emax
=>
9224 Fold_Uint
(N
, Machine_Emax_Value
(P_Type
), Static
);
9230 when Attribute_Machine_Emin
=>
9231 Fold_Uint
(N
, Machine_Emin_Value
(P_Type
), Static
);
9233 ----------------------
9234 -- Machine_Mantissa --
9235 ----------------------
9237 when Attribute_Machine_Mantissa
=>
9238 Fold_Uint
(N
, Machine_Mantissa_Value
(P_Type
), Static
);
9240 -----------------------
9241 -- Machine_Overflows --
9242 -----------------------
9244 when Attribute_Machine_Overflows
=>
9246 -- Always true for fixed-point
9248 if Is_Fixed_Point_Type
(P_Type
) then
9249 Fold_Uint
(N
, True_Value
, Static
);
9251 -- Floating point case
9255 UI_From_Int
(Boolean'Pos (Machine_Overflows_On_Target
)),
9263 when Attribute_Machine_Radix
=>
9264 if Is_Fixed_Point_Type
(P_Type
) then
9265 if Is_Decimal_Fixed_Point_Type
(P_Type
)
9266 and then Machine_Radix_10
(P_Type
)
9268 Fold_Uint
(N
, Uint_10
, Static
);
9270 Fold_Uint
(N
, Uint_2
, Static
);
9273 -- All floating-point type always have radix 2
9276 Fold_Uint
(N
, Uint_2
, Static
);
9279 ----------------------
9280 -- Machine_Rounding --
9281 ----------------------
9283 -- Note: for the folding case, it is fine to treat Machine_Rounding
9284 -- exactly the same way as Rounding, since this is one of the allowed
9285 -- behaviors, and performance is not an issue here. It might be a bit
9286 -- better to give the same result as it would give at run time, even
9287 -- though the non-determinism is certainly permitted.
9289 when Attribute_Machine_Rounding
=>
9291 (N
, Eval_Fat
.Rounding
(P_Base_Type
, Expr_Value_R
(E1
)), Static
);
9293 --------------------
9294 -- Machine_Rounds --
9295 --------------------
9297 when Attribute_Machine_Rounds
=>
9299 -- Always False for fixed-point
9301 if Is_Fixed_Point_Type
(P_Type
) then
9302 Fold_Uint
(N
, False_Value
, Static
);
9304 -- Else yield proper floating-point result
9308 (N
, UI_From_Int
(Boolean'Pos (Machine_Rounds_On_Target
)),
9316 -- Note: Machine_Size is identical to Object_Size
9318 when Attribute_Machine_Size
=> Machine_Size
: declare
9319 P_TypeA
: constant Entity_Id
:= Underlying_Type
(P_Type
);
9322 if Known_Esize
(P_TypeA
) then
9323 Fold_Uint
(N
, Esize
(P_TypeA
), Static
);
9331 when Attribute_Mantissa
=>
9333 -- Fixed-point mantissa
9335 if Is_Fixed_Point_Type
(P_Type
) then
9337 -- Compile time foldable case
9339 if Compile_Time_Known_Value
(Type_Low_Bound
(P_Type
))
9341 Compile_Time_Known_Value
(Type_High_Bound
(P_Type
))
9343 -- The calculation of the obsolete Ada 83 attribute Mantissa
9344 -- is annoying, because of AI00143, quoted here:
9346 -- !question 84-01-10
9348 -- Consider the model numbers for F:
9350 -- type F is delta 1.0 range -7.0 .. 8.0;
9352 -- The wording requires that F'MANTISSA be the SMALLEST
9353 -- integer number for which each bound of the specified
9354 -- range is either a model number or lies at most small
9355 -- distant from a model number. This means F'MANTISSA
9356 -- is required to be 3 since the range -7.0 .. 7.0 fits
9357 -- in 3 signed bits, and 8 is "at most" 1.0 from a model
9358 -- number, namely, 7. Is this analysis correct? Note that
9359 -- this implies the upper bound of the range is not
9360 -- represented as a model number.
9362 -- !response 84-03-17
9364 -- The analysis is correct. The upper and lower bounds for
9365 -- a fixed point type can lie outside the range of model
9376 LBound
:= Expr_Value_R
(Type_Low_Bound
(P_Type
));
9377 UBound
:= Expr_Value_R
(Type_High_Bound
(P_Type
));
9378 Bound
:= UR_Max
(UR_Abs
(LBound
), UR_Abs
(UBound
));
9379 Max_Man
:= UR_Trunc
(Bound
/ Small_Value
(P_Type
));
9381 -- If the Bound is exactly a model number, i.e. a multiple
9382 -- of Small, then we back it off by one to get the integer
9383 -- value that must be representable.
9385 if Small_Value
(P_Type
) * Max_Man
= Bound
then
9386 Max_Man
:= Max_Man
- 1;
9389 -- Now find corresponding size = Mantissa value
9392 while 2 ** Siz
< Max_Man
loop
9396 Fold_Uint
(N
, Siz
, Static
);
9400 -- The case of dynamic bounds cannot be evaluated at compile
9401 -- time. Instead we use a runtime routine (see Exp_Attr).
9406 -- Floating-point Mantissa
9409 Fold_Uint
(N
, Mantissa
, Static
);
9416 when Attribute_Max
=>
9417 if Is_Real_Type
(P_Type
) then
9419 (N
, UR_Max
(Expr_Value_R
(E1
), Expr_Value_R
(E2
)), Static
);
9421 Fold_Uint
(N
, UI_Max
(Expr_Value
(E1
), Expr_Value
(E2
)), Static
);
9424 ----------------------------------
9425 -- Max_Alignment_For_Allocation --
9426 ----------------------------------
9428 -- Max_Alignment_For_Allocation is usually the Alignment. However,
9429 -- arrays are allocated with dope, so we need to take into account both
9430 -- the alignment of the array, which comes from the component alignment,
9431 -- and the alignment of the dope. Also, if the alignment is unknown, we
9432 -- use the max (it's OK to be pessimistic).
9434 when Attribute_Max_Alignment_For_Allocation
=> Max_Align
: declare
9435 A
: Uint
:= UI_From_Int
(Ttypes
.Maximum_Alignment
);
9437 if Known_Alignment
(P_Type
)
9438 and then (not Is_Array_Type
(P_Type
) or else Alignment
(P_Type
) > A
)
9440 A
:= Alignment
(P_Type
);
9443 Fold_Uint
(N
, A
, Static
);
9446 ----------------------------------
9447 -- Max_Size_In_Storage_Elements --
9448 ----------------------------------
9450 -- Max_Size_In_Storage_Elements is simply the Size rounded up to a
9451 -- Storage_Unit boundary. We can fold any cases for which the size
9452 -- is known by the front end.
9454 when Attribute_Max_Size_In_Storage_Elements
=>
9455 if Known_Esize
(P_Type
) then
9457 (Esize
(P_Type
) + System_Storage_Unit
- 1) /
9458 System_Storage_Unit
,
9462 --------------------
9463 -- Mechanism_Code --
9464 --------------------
9466 when Attribute_Mechanism_Code
=> Mechanism_Code
: declare
9468 Mech
: Mechanism_Type
;
9473 Mech
:= Mechanism
(P_Entity
);
9476 Val
:= UI_To_Int
(Expr_Value
(E1
));
9478 Formal
:= First_Formal
(P_Entity
);
9479 for J
in 1 .. Val
- 1 loop
9480 Next_Formal
(Formal
);
9483 Mech
:= Mechanism
(Formal
);
9487 Fold_Uint
(N
, UI_From_Int
(Int
(-Mech
)), Static
);
9495 when Attribute_Min
=>
9496 if Is_Real_Type
(P_Type
) then
9498 (N
, UR_Min
(Expr_Value_R
(E1
), Expr_Value_R
(E2
)), Static
);
9501 (N
, UI_Min
(Expr_Value
(E1
), Expr_Value
(E2
)), Static
);
9508 when Attribute_Mod
=>
9510 (N
, UI_Mod
(Expr_Value
(E1
), Modulus
(P_Base_Type
)), Static
);
9516 when Attribute_Model
=>
9518 (N
, Eval_Fat
.Model
(P_Base_Type
, Expr_Value_R
(E1
)), Static
);
9524 when Attribute_Model_Emin
=>
9525 Fold_Uint
(N
, Model_Emin_Value
(P_Base_Type
), Static
);
9531 when Attribute_Model_Epsilon
=>
9532 Fold_Ureal
(N
, Model_Epsilon_Value
(P_Base_Type
), Static
);
9534 --------------------
9535 -- Model_Mantissa --
9536 --------------------
9538 when Attribute_Model_Mantissa
=>
9539 Fold_Uint
(N
, Model_Mantissa_Value
(P_Base_Type
), Static
);
9545 when Attribute_Model_Small
=>
9546 Fold_Ureal
(N
, Model_Small_Value
(P_Base_Type
), Static
);
9552 when Attribute_Modulus
=>
9553 Fold_Uint
(N
, Modulus
(P_Type
), Static
);
9555 --------------------
9556 -- Null_Parameter --
9557 --------------------
9559 -- Cannot fold, we know the value sort of, but the whole point is
9560 -- that there is no way to talk about this imaginary value except
9561 -- by using the attribute, so we leave it the way it is.
9563 when Attribute_Null_Parameter
=>
9570 -- The Object_Size attribute for a type returns the Esize of the
9571 -- type and can be folded if this value is known.
9573 when Attribute_Object_Size
=> Object_Size
: declare
9574 P_TypeA
: constant Entity_Id
:= Underlying_Type
(P_Type
);
9577 if Known_Esize
(P_TypeA
) then
9578 Fold_Uint
(N
, Esize
(P_TypeA
), Static
);
9582 ----------------------
9583 -- Overlaps_Storage --
9584 ----------------------
9586 when Attribute_Overlaps_Storage
=>
9589 -------------------------
9590 -- Passed_By_Reference --
9591 -------------------------
9593 -- Scalar types are never passed by reference
9595 when Attribute_Passed_By_Reference
=>
9596 Fold_Uint
(N
, False_Value
, Static
);
9602 when Attribute_Pos
=>
9603 Fold_Uint
(N
, Expr_Value
(E1
), Static
);
9609 when Attribute_Pred
=>
9611 -- Floating-point case
9613 if Is_Floating_Point_Type
(P_Type
) then
9615 (N
, Eval_Fat
.Pred
(P_Base_Type
, Expr_Value_R
(E1
)), Static
);
9619 elsif Is_Fixed_Point_Type
(P_Type
) then
9621 (N
, Expr_Value_R
(E1
) - Small_Value
(P_Type
), True);
9623 -- Modular integer case (wraps)
9625 elsif Is_Modular_Integer_Type
(P_Type
) then
9626 Fold_Uint
(N
, (Expr_Value
(E1
) - 1) mod Modulus
(P_Type
), Static
);
9628 -- Other scalar cases
9631 pragma Assert
(Is_Scalar_Type
(P_Type
));
9633 if Is_Enumeration_Type
(P_Type
)
9634 and then Expr_Value
(E1
) =
9635 Expr_Value
(Type_Low_Bound
(P_Base_Type
))
9637 Apply_Compile_Time_Constraint_Error
9638 (N
, "Pred of `&''First`",
9639 CE_Overflow_Check_Failed
,
9641 Warn
=> not Static
);
9647 Fold_Uint
(N
, Expr_Value
(E1
) - 1, Static
);
9650 ----------------------------------
9651 -- Preelaborable_Initialization --
9652 ----------------------------------
9654 when Attribute_Preelaborable_Initialization
=>
9658 (Boolean'Pos (Has_Preelaborable_Initialization
(P_Type
))),
9665 -- No processing required, because by this stage, Range has been
9666 -- replaced by First .. Last, so this branch can never be taken.
9668 when Attribute_Range
=>
9669 raise Program_Error
;
9675 when Attribute_Range_Length
=> Range_Length
: declare
9676 Diff
: aliased Uint
;
9681 -- Can fold if both bounds are compile time known
9683 if Compile_Time_Known_Value
(Hi_Bound
)
9684 and then Compile_Time_Known_Value
(Lo_Bound
)
9688 (0, Expr_Value
(Hi_Bound
) - Expr_Value
(Lo_Bound
) + 1),
9692 -- One more case is where Hi_Bound and Lo_Bound are compile-time
9693 -- comparable, and we can figure out the difference between them.
9695 case Compile_Time_Compare
9696 (Lo_Bound
, Hi_Bound
, Diff
'Access, Assume_Valid
=> False)
9699 Fold_Uint
(N
, Uint_1
, Static
);
9702 Fold_Uint
(N
, Uint_0
, Static
);
9705 if Present
(Diff
) then
9706 Fold_Uint
(N
, Diff
+ 1, Static
);
9718 when Attribute_Ref
=>
9719 Fold_Uint
(N
, Expr_Value
(E1
), Static
);
9725 when Attribute_Remainder
=> Remainder
: declare
9726 X
: constant Ureal
:= Expr_Value_R
(E1
);
9727 Y
: constant Ureal
:= Expr_Value_R
(E2
);
9730 if UR_Is_Zero
(Y
) then
9731 Apply_Compile_Time_Constraint_Error
9732 (N
, "division by zero in Remainder",
9733 CE_Overflow_Check_Failed
,
9734 Warn
=> not Static
);
9740 Fold_Ureal
(N
, Eval_Fat
.Remainder
(P_Base_Type
, X
, Y
), Static
);
9747 when Attribute_Restriction_Set
=>
9748 Rewrite
(N
, New_Occurrence_Of
(Standard_False
, Loc
));
9749 Set_Is_Static_Expression
(N
);
9755 when Attribute_Round
=> Round
: declare
9760 -- First we get the (exact result) in units of small
9762 Sr
:= Expr_Value_R
(E1
) / Small_Value
(C_Type
);
9764 -- Now round that exactly to an integer
9766 Si
:= UR_To_Uint
(Sr
);
9768 -- Finally the result is obtained by converting back to real
9770 Fold_Ureal
(N
, Si
* Small_Value
(C_Type
), Static
);
9777 when Attribute_Rounding
=>
9779 (N
, Eval_Fat
.Rounding
(P_Base_Type
, Expr_Value_R
(E1
)), Static
);
9785 when Attribute_Safe_Emax
=>
9786 Fold_Uint
(N
, Safe_Emax_Value
(P_Type
), Static
);
9792 when Attribute_Safe_First
=>
9793 Fold_Ureal
(N
, Safe_First_Value
(P_Type
), Static
);
9799 when Attribute_Safe_Large
=>
9800 if Is_Fixed_Point_Type
(P_Type
) then
9802 (N
, Expr_Value_R
(Type_High_Bound
(P_Base_Type
)), Static
);
9804 Fold_Ureal
(N
, Safe_Last_Value
(P_Type
), Static
);
9811 when Attribute_Safe_Last
=>
9812 Fold_Ureal
(N
, Safe_Last_Value
(P_Type
), Static
);
9818 when Attribute_Safe_Small
=>
9820 -- In Ada 95, the old Ada 83 attribute Safe_Small is redundant
9821 -- for fixed-point, since is the same as Small, but we implement
9822 -- it for backwards compatibility.
9824 if Is_Fixed_Point_Type
(P_Type
) then
9825 Fold_Ureal
(N
, Small_Value
(P_Type
), Static
);
9827 -- Ada 83 Safe_Small for floating-point cases
9830 Fold_Ureal
(N
, Model_Small_Value
(P_Type
), Static
);
9837 when Attribute_Scale
=>
9838 Fold_Uint
(N
, Scale_Value
(P_Type
), Static
);
9844 when Attribute_Scaling
=>
9848 (P_Base_Type
, Expr_Value_R
(E1
), Expr_Value
(E2
)),
9855 when Attribute_Signed_Zeros
=>
9857 (N
, UI_From_Int
(Boolean'Pos (Has_Signed_Zeros
(P_Type
))), Static
);
9863 -- Size attribute returns the RM size. All scalar types can be folded,
9864 -- as well as any types for which the size is known by the front end,
9865 -- including any type for which a size attribute is specified. This is
9866 -- one of the places where it is annoying that a size of zero means two
9867 -- things (zero size for scalars, unspecified size for non-scalars).
9870 | Attribute_VADS_Size
9873 P_TypeA
: constant Entity_Id
:= Underlying_Type
(P_Type
);
9877 (if Is_Scalar_Type
(P_TypeA
) then Known_RM_Size
(P_TypeA
));
9878 if Known_RM_Size
(P_TypeA
) then
9881 if Id
= Attribute_VADS_Size
or else Use_VADS_Size
then
9883 S
: constant Node_Id
:= Size_Clause
(P_TypeA
);
9886 -- If a size clause applies, then use the size from it.
9887 -- This is one of the rare cases where we can use the
9888 -- Size_Clause field for a subtype when Has_Size_Clause
9889 -- is False. Consider:
9891 -- type x is range 1 .. 64;
9892 -- for x'size use 12;
9893 -- subtype y is x range 0 .. 3;
9895 -- Here y has a size clause inherited from x, but
9896 -- normally it does not apply, and y'size is 2. However,
9897 -- y'VADS_Size is indeed 12 and not 2.
9900 and then Is_OK_Static_Expression
(Expression
(S
))
9902 Fold_Uint
(N
, Expr_Value
(Expression
(S
)), Static
);
9904 -- If no size is specified, then we simply use the object
9905 -- size in the VADS_Size case (e.g. Natural'Size is equal
9906 -- to Integer'Size, not one less).
9909 Fold_Uint
(N
, Esize
(P_TypeA
), Static
);
9913 -- Normal case (Size) in which case we want the RM_Size
9916 Fold_Uint
(N
, RM_Size
(P_TypeA
), Static
);
9925 when Attribute_Small
=>
9927 -- The floating-point case is present only for Ada 83 compatibility.
9928 -- Note that strictly this is an illegal addition, since we are
9929 -- extending an Ada 95 defined attribute, but we anticipate an
9930 -- ARG ruling that will permit this.
9932 if Is_Floating_Point_Type
(P_Type
) then
9934 -- Ada 83 attribute is defined as (RM83 3.5.8)
9936 -- T'Small = 2.0**(-T'Emax - 1)
9940 -- T'Emax = 4 * T'Mantissa
9942 Fold_Ureal
(N
, Ureal_2
** ((-(4 * Mantissa
)) - 1), Static
);
9944 -- Normal Ada 95 fixed-point case
9947 Fold_Ureal
(N
, Small_Value
(P_Type
), True);
9950 -----------------------
9951 -- Small_Denominator --
9952 -----------------------
9954 when Attribute_Small_Denominator
=>
9955 Fold_Uint
(N
, Norm_Den
(Small_Value
(P_Type
)), True);
9957 ---------------------
9958 -- Small_Numerator --
9959 ---------------------
9961 when Attribute_Small_Numerator
=>
9962 Fold_Uint
(N
, Norm_Num
(Small_Value
(P_Type
)), True);
9968 when Attribute_Stream_Size
=>
9975 when Attribute_Succ
=>
9976 -- Floating-point case
9978 if Is_Floating_Point_Type
(P_Type
) then
9980 (N
, Eval_Fat
.Succ
(P_Base_Type
, Expr_Value_R
(E1
)), Static
);
9984 elsif Is_Fixed_Point_Type
(P_Type
) then
9985 Fold_Ureal
(N
, Expr_Value_R
(E1
) + Small_Value
(P_Type
), Static
);
9987 -- Modular integer case (wraps)
9989 elsif Is_Modular_Integer_Type
(P_Type
) then
9990 Fold_Uint
(N
, (Expr_Value
(E1
) + 1) mod Modulus
(P_Type
), Static
);
9992 -- Other scalar cases
9995 pragma Assert
(Is_Scalar_Type
(P_Type
));
9997 if Is_Enumeration_Type
(P_Type
)
9998 and then Expr_Value
(E1
) =
9999 Expr_Value
(Type_High_Bound
(P_Base_Type
))
10001 Apply_Compile_Time_Constraint_Error
10002 (N
, "Succ of `&''Last`",
10003 CE_Overflow_Check_Failed
,
10004 Ent
=> P_Base_Type
,
10005 Warn
=> not Static
);
10010 Fold_Uint
(N
, Expr_Value
(E1
) + 1, Static
);
10018 when Attribute_Truncation
=>
10021 Eval_Fat
.Truncation
(P_Base_Type
, Expr_Value_R
(E1
)),
10028 when Attribute_Type_Class
=> Type_Class
: declare
10029 Typ
: constant Entity_Id
:= Underlying_Type
(P_Base_Type
);
10033 if Is_Descendant_Of_Address
(Typ
) then
10034 Id
:= RE_Type_Class_Address
;
10036 elsif Is_Enumeration_Type
(Typ
) then
10037 Id
:= RE_Type_Class_Enumeration
;
10039 elsif Is_Integer_Type
(Typ
) then
10040 Id
:= RE_Type_Class_Integer
;
10042 elsif Is_Fixed_Point_Type
(Typ
) then
10043 Id
:= RE_Type_Class_Fixed_Point
;
10045 elsif Is_Floating_Point_Type
(Typ
) then
10046 Id
:= RE_Type_Class_Floating_Point
;
10048 elsif Is_Array_Type
(Typ
) then
10049 Id
:= RE_Type_Class_Array
;
10051 elsif Is_Record_Type
(Typ
) then
10052 Id
:= RE_Type_Class_Record
;
10054 elsif Is_Access_Type
(Typ
) then
10055 Id
:= RE_Type_Class_Access
;
10057 elsif Is_Task_Type
(Typ
) then
10058 Id
:= RE_Type_Class_Task
;
10060 -- We treat protected types like task types. It would make more
10061 -- sense to have another enumeration value, but after all the
10062 -- whole point of this feature is to be exactly DEC compatible,
10063 -- and changing the type Type_Class would not meet this requirement.
10065 elsif Is_Protected_Type
(Typ
) then
10066 Id
:= RE_Type_Class_Task
;
10068 -- Not clear if there are any other possibilities, but if there
10069 -- are, then we will treat them as the address case.
10072 Id
:= RE_Type_Class_Address
;
10075 Rewrite
(N
, New_Occurrence_Of
(RTE
(Id
), Loc
));
10078 -----------------------
10079 -- Unbiased_Rounding --
10080 -----------------------
10082 when Attribute_Unbiased_Rounding
=>
10085 Eval_Fat
.Unbiased_Rounding
(P_Base_Type
, Expr_Value_R
(E1
)),
10088 -------------------------
10089 -- Unconstrained_Array --
10090 -------------------------
10092 when Attribute_Unconstrained_Array
=> Unconstrained_Array
: declare
10093 Typ
: constant Entity_Id
:= Underlying_Type
(P_Type
);
10096 Rewrite
(N
, New_Occurrence_Of
(
10098 Is_Array_Type
(P_Type
)
10099 and then not Is_Constrained
(Typ
)), Loc
));
10101 -- Analyze and resolve as boolean, note that this attribute is
10102 -- a static attribute in GNAT.
10104 Analyze_And_Resolve
(N
, Standard_Boolean
);
10106 Set_Is_Static_Expression
(N
, True);
10107 end Unconstrained_Array
;
10109 -- Attribute Update is never static
10111 when Attribute_Update
=>
10118 -- Processing is shared with Size
10124 when Attribute_Val
=>
10125 if Expr_Value
(E1
) < Expr_Value
(Type_Low_Bound
(P_Base_Type
))
10127 Expr_Value
(E1
) > Expr_Value
(Type_High_Bound
(P_Base_Type
))
10129 Apply_Compile_Time_Constraint_Error
10130 (N
, "Val expression out of range",
10131 CE_Range_Check_Failed
,
10132 Warn
=> not Static
);
10138 Fold_Uint
(N
, Expr_Value
(E1
), Static
);
10145 -- The Value_Size attribute for a type returns the RM size of the type.
10146 -- This an always be folded for scalar types, and can also be folded for
10147 -- non-scalar types if the size is set. This is one of the places where
10148 -- it is annoying that a size of zero means two things!
10150 when Attribute_Value_Size
=> Value_Size
: declare
10151 P_TypeA
: constant Entity_Id
:= Underlying_Type
(P_Type
);
10155 (if Is_Scalar_Type
(P_TypeA
) then Known_RM_Size
(P_TypeA
));
10156 if Known_RM_Size
(P_TypeA
) then
10157 Fold_Uint
(N
, RM_Size
(P_TypeA
), Static
);
10165 -- Version can never be static
10167 when Attribute_Version
=>
10174 -- Wide_Image is a scalar attribute, but is never static, because it
10175 -- is not a static function (having a non-scalar argument (RM 4.9(22))
10177 when Attribute_Wide_Image
=>
10180 ---------------------
10181 -- Wide_Wide_Image --
10182 ---------------------
10184 -- Wide_Wide_Image is a scalar attribute but is never static, because it
10185 -- is not a static function (having a non-scalar argument (RM 4.9(22)).
10187 when Attribute_Wide_Wide_Image
=>
10190 ---------------------
10191 -- Wide_Wide_Width --
10192 ---------------------
10194 -- Processing for Wide_Wide_Width is combined with Width
10200 -- Processing for Wide_Width is combined with Width
10206 -- This processing also handles the case of Wide_[Wide_]Width
10208 when Attribute_Width
10209 | Attribute_Wide_Width
10210 | Attribute_Wide_Wide_Width
10212 if Compile_Time_Known_Bounds
(P_Type
) then
10214 -- Floating-point types
10216 if Is_Floating_Point_Type
(P_Type
) then
10218 -- Width is zero for a null range (RM 3.5 (38))
10220 if Expr_Value_R
(Type_High_Bound
(P_Type
)) <
10221 Expr_Value_R
(Type_Low_Bound
(P_Type
))
10223 Fold_Uint
(N
, Uint_0
, Static
);
10226 -- For floating-point, we have +N.dddE+nnn where length
10227 -- of ddd is determined by type'Digits - 1, but is one
10228 -- if Digits is one (RM 3.5 (33)).
10230 -- nnn is set to 2 for Short_Float and Float (32 bit
10231 -- floats), and 3 for Long_Float and Long_Long_Float.
10232 -- For machines where Long_Long_Float is the IEEE
10233 -- extended precision type, the exponent takes 4 digits.
10237 Int
'Max (2, UI_To_Int
(Digits_Value
(P_Type
)));
10240 if Esize
(P_Type
) <= 32 then
10242 elsif Esize
(P_Type
) = 64 then
10248 Fold_Uint
(N
, UI_From_Int
(Len
), Static
);
10252 -- Fixed-point types
10254 elsif Is_Fixed_Point_Type
(P_Type
) then
10256 -- Width is zero for a null range (RM 3.5 (38))
10258 if Expr_Value
(Type_High_Bound
(P_Type
)) <
10259 Expr_Value
(Type_Low_Bound
(P_Type
))
10261 Fold_Uint
(N
, Uint_0
, Static
);
10263 -- The non-null case depends on the specific real type
10266 -- For fixed-point type width is Fore + 1 + Aft (RM 3.5(34))
10269 (N
, UI_From_Int
(Fore_Value
+ 1) + Aft_Value
(P_Type
),
10277 R
: constant Entity_Id
:= Root_Type
(P_Type
);
10278 Lo
: constant Uint
:= Expr_Value
(Type_Low_Bound
(P_Type
));
10279 Hi
: constant Uint
:= Expr_Value
(Type_High_Bound
(P_Type
));
10292 -- Width for types derived from Standard.Character
10293 -- and Standard.Wide_[Wide_]Character.
10295 elsif Is_Standard_Character_Type
(P_Type
) then
10298 -- Set W larger if needed
10300 for J
in UI_To_Int
(Lo
) .. UI_To_Int
(Hi
) loop
10302 -- All wide characters look like Hex_hhhhhhhh
10306 -- No need to compute this more than once
10311 C
:= Character'Val (J
);
10313 -- Test for all cases where Character'Image
10314 -- yields an image that is longer than three
10315 -- characters. First the cases of Reserved_xxx
10316 -- names (length = 12).
10393 when Space
.. Tilde
10394 | No_Break_Space
.. LC_Y_Diaeresis
10396 -- Special case of soft hyphen in Ada 2005
10398 if C
= Character'Val (16#AD#
)
10399 and then Ada_Version
>= Ada_2005
10407 W
:= Int
'Max (W
, Wt
);
10411 -- Width for types derived from Standard.Boolean
10413 elsif R
= Standard_Boolean
then
10420 -- Width for integer types
10422 elsif Is_Integer_Type
(P_Type
) then
10423 T
:= UI_Max
(abs Lo
, abs Hi
);
10431 -- User declared enum type with discard names
10433 elsif Discard_Names
(R
) then
10435 -- If range is null, result is zero, that has already
10436 -- been dealt with, so what we need is the power of ten
10437 -- that accommodates the Pos of the largest value, which
10438 -- is the high bound of the range + one for the space.
10447 -- Only remaining possibility is user declared enum type
10448 -- with normal case of Discard_Names not active.
10451 pragma Assert
(Is_Enumeration_Type
(P_Type
));
10454 L
:= First_Literal
(P_Type
);
10455 while Present
(L
) loop
10457 -- Only pay attention to in range characters
10459 if Lo
<= Enumeration_Pos
(L
)
10460 and then Enumeration_Pos
(L
) <= Hi
10462 -- For Width case, use decoded name
10464 if Id
= Attribute_Width
then
10465 Get_Decoded_Name_String
(Chars
(L
));
10466 Wt
:= Nat
(Name_Len
);
10468 -- For Wide_[Wide_]Width, use encoded name, and
10469 -- then adjust for the encoding.
10472 Get_Name_String
(Chars
(L
));
10474 -- Character literals are always of length 3
10476 if Name_Buffer
(1) = 'Q' then
10479 -- Otherwise loop to adjust for upper/wide chars
10482 Wt
:= Nat
(Name_Len
);
10484 for J
in 1 .. Name_Len
loop
10485 if Name_Buffer
(J
) = 'U' then
10487 elsif Name_Buffer
(J
) = 'W' then
10494 W
:= Int
'Max (W
, Wt
);
10501 Fold_Uint
(N
, UI_From_Int
(W
), Static
);
10506 -- The following attributes denote functions that cannot be folded
10508 when Attribute_From_Any
10510 | Attribute_TypeCode
10514 -- The following attributes can never be folded, and furthermore we
10515 -- should not even have entered the case statement for any of these.
10516 -- Note that in some cases, the values have already been folded as
10517 -- a result of the processing in Analyze_Attribute or earlier in
10520 when Attribute_Abort_Signal
10522 | Attribute_Address
10523 | Attribute_Address_Size
10524 | Attribute_Asm_Input
10525 | Attribute_Asm_Output
10527 | Attribute_Bit_Order
10528 | Attribute_Bit_Position
10529 | Attribute_Callable
10532 | Attribute_Code_Address
10533 | Attribute_Compiler_Version
10535 | Attribute_Default_Bit_Order
10536 | Attribute_Default_Scalar_Storage_Order
10538 | Attribute_Elaborated
10539 | Attribute_Elab_Body
10540 | Attribute_Elab_Spec
10541 | Attribute_Elab_Subp_Body
10542 | Attribute_Enabled
10543 | Attribute_External_Tag
10544 | Attribute_Fast_Math
10545 | Attribute_First_Bit
10548 | Attribute_Initialized
10549 | Attribute_Last_Bit
10550 | Attribute_Library_Level
10551 | Attribute_Max_Integer_Size
10552 | Attribute_Maximum_Alignment
10555 | Attribute_Partition_ID
10556 | Attribute_Pool_Address
10557 | Attribute_Position
10558 | Attribute_Priority
10559 | Attribute_Put_Image
10562 | Attribute_Scalar_Storage_Order
10563 | Attribute_Simple_Storage_Pool
10564 | Attribute_Storage_Pool
10565 | Attribute_Storage_Size
10566 | Attribute_Storage_Unit
10567 | Attribute_Stub_Type
10568 | Attribute_System_Allocator_Alignment
10570 | Attribute_Target_Name
10571 | Attribute_Terminated
10572 | Attribute_To_Address
10573 | Attribute_Type_Key
10574 | Attribute_Unchecked_Access
10575 | Attribute_Universal_Literal_String
10576 | Attribute_Unrestricted_Access
10578 | Attribute_Valid_Scalars
10579 | Attribute_Valid_Value
10581 | Attribute_Wchar_T_Size
10582 | Attribute_Wide_Value
10583 | Attribute_Wide_Wide_Value
10584 | Attribute_Word_Size
10587 raise Program_Error
;
10590 -- At the end of the case, one more check. If we did a static evaluation
10591 -- so that the result is now a literal, then set Is_Static_Expression
10592 -- in the constant only if the prefix type is a static subtype. For
10593 -- non-static subtypes, the folding is still OK, but not static.
10595 -- An exception is the GNAT attribute Constrained_Array which is
10596 -- defined to be a static attribute in all cases.
10598 if Nkind
(N
) in N_Integer_Literal
10600 | N_Character_Literal
10602 or else (Is_Entity_Name
(N
)
10603 and then Ekind
(Entity
(N
)) = E_Enumeration_Literal
)
10605 Set_Is_Static_Expression
(N
, Static
);
10607 -- If this is still an attribute reference, then it has not been folded
10608 -- and that means that its expressions are in a non-static context.
10610 elsif Nkind
(N
) = N_Attribute_Reference
then
10613 -- Note: the else case not covered here are odd cases where the
10614 -- processing has transformed the attribute into something other
10615 -- than a constant. Nothing more to do in such cases.
10620 end Eval_Attribute
;
10622 ------------------------------
10623 -- Is_Anonymous_Tagged_Base --
10624 ------------------------------
10626 function Is_Anonymous_Tagged_Base
10628 Typ
: Entity_Id
) return Boolean
10632 Anon
= Current_Scope
10633 and then Is_Itype
(Anon
)
10634 and then Associated_Node_For_Itype
(Anon
) = Parent
(Typ
);
10635 end Is_Anonymous_Tagged_Base
;
10637 --------------------------------
10638 -- Name_Implies_Lvalue_Prefix --
10639 --------------------------------
10641 function Name_Implies_Lvalue_Prefix
(Nam
: Name_Id
) return Boolean is
10642 pragma Assert
(Is_Attribute_Name
(Nam
));
10644 return Attribute_Name_Implies_Lvalue_Prefix
(Get_Attribute_Id
(Nam
));
10645 end Name_Implies_Lvalue_Prefix
;
10647 -----------------------
10648 -- Resolve_Attribute --
10649 -----------------------
10651 procedure Resolve_Attribute
(N
: Node_Id
; Typ
: Entity_Id
) is
10652 Loc
: constant Source_Ptr
:= Sloc
(N
);
10653 P
: constant Node_Id
:= Prefix
(N
);
10654 Aname
: constant Name_Id
:= Attribute_Name
(N
);
10655 Attr_Id
: constant Attribute_Id
:= Get_Attribute_Id
(Aname
);
10656 Btyp
: constant Entity_Id
:= Base_Type
(Typ
);
10657 Des_Btyp
: Entity_Id
;
10658 Index
: Interp_Index
;
10660 Nom_Subt
: Entity_Id
;
10662 procedure Accessibility_Message
;
10663 -- Error, or warning within an instance, if the static accessibility
10664 -- rules of 3.10.2 are violated.
10666 function Declared_Within_Generic_Unit
10667 (Entity
: Entity_Id
;
10668 Generic_Unit
: Node_Id
) return Boolean;
10669 -- Returns True if Declared_Entity is declared within the declarative
10670 -- region of Generic_Unit; otherwise returns False.
10672 function Prefix_With_Safe_Accessibility_Level
return Boolean;
10673 -- Return True if the prefix does not have a value conversion of an
10674 -- array because a value conversion is like an aggregate with respect
10675 -- to determining accessibility level (RM 3.10.2); even if evaluation
10676 -- of a value conversion is guaranteed to not create a new object,
10677 -- accessibility rules are defined as if it might.
10679 ---------------------------
10680 -- Accessibility_Message --
10681 ---------------------------
10683 procedure Accessibility_Message
is
10684 Indic
: Node_Id
:= Parent
(Parent
(N
));
10687 -- In an instance, this is a runtime check, but one we
10688 -- know will fail, so generate an appropriate warning.
10690 if In_Instance_Body
then
10691 Error_Msg_Warn
:= SPARK_Mode
/= On
;
10693 ("non-local pointer cannot point to local object<<", P
);
10694 Error_Msg_F
("\Program_Error [<<", P
);
10696 Make_Raise_Program_Error
(Loc
,
10697 Reason
=> PE_Accessibility_Check_Failed
));
10698 Set_Etype
(N
, Typ
);
10702 Error_Msg_F
("non-local pointer cannot point to local object", P
);
10704 -- Check for case where we have a missing access definition
10706 if Is_Record_Type
(Current_Scope
)
10708 Nkind
(Parent
(N
)) in N_Discriminant_Association
10709 | N_Index_Or_Discriminant_Constraint
10711 Indic
:= Parent
(Parent
(N
));
10712 while Present
(Indic
)
10713 and then Nkind
(Indic
) /= N_Subtype_Indication
10715 Indic
:= Parent
(Indic
);
10718 if Present
(Indic
) then
10720 ("\use an access definition for" &
10721 " the access discriminant of&",
10722 N
, Entity
(Subtype_Mark
(Indic
)));
10726 end Accessibility_Message
;
10728 ----------------------------------
10729 -- Declared_Within_Generic_Unit --
10730 ----------------------------------
10732 function Declared_Within_Generic_Unit
10733 (Entity
: Entity_Id
;
10734 Generic_Unit
: Node_Id
) return Boolean
10736 Generic_Encloser
: Node_Id
:= Enclosing_Generic_Unit
(Entity
);
10739 while Present
(Generic_Encloser
) loop
10740 if Generic_Encloser
= Generic_Unit
then
10744 -- We have to step to the scope of the generic's entity, because
10745 -- otherwise we'll just get back the same generic.
10747 Generic_Encloser
:=
10748 Enclosing_Generic_Unit
10749 (Scope
(Defining_Entity
(Generic_Encloser
)));
10753 end Declared_Within_Generic_Unit
;
10755 ------------------------------------------
10756 -- Prefix_With_Safe_Accessibility_Level --
10757 ------------------------------------------
10759 function Prefix_With_Safe_Accessibility_Level
return Boolean is
10760 function Safe_Value_Conversions
return Boolean;
10761 -- Return False if the prefix has a value conversion of an array type
10763 ----------------------------
10764 -- Safe_Value_Conversions --
10765 ----------------------------
10767 function Safe_Value_Conversions
return Boolean is
10772 if Nkind
(PP
) in N_Selected_Component | N_Indexed_Component
then
10775 elsif Comes_From_Source
(PP
)
10776 and then Nkind
(PP
) in N_Type_Conversion
10777 | N_Unchecked_Type_Conversion
10778 and then Is_Array_Type
(Etype
(PP
))
10782 elsif Comes_From_Source
(PP
)
10783 and then Nkind
(PP
) = N_Qualified_Expression
10784 and then Is_Array_Type
(Etype
(PP
))
10785 and then Nkind
(Original_Node
(Expression
(PP
))) in
10786 N_Aggregate | N_Extension_Aggregate
10796 end Safe_Value_Conversions
;
10798 -- Start of processing for Prefix_With_Safe_Accessibility_Level
10801 -- No check required for unchecked and unrestricted access
10803 if Attr_Id
= Attribute_Unchecked_Access
10804 or else Attr_Id
= Attribute_Unrestricted_Access
10808 -- Check value conversions
10810 elsif Ekind
(Btyp
) = E_General_Access_Type
10811 and then not Safe_Value_Conversions
10817 end Prefix_With_Safe_Accessibility_Level
;
10819 -- Start of processing for Resolve_Attribute
10822 -- If error during analysis, no point in continuing, except for array
10823 -- types, where we get better recovery by using unconstrained indexes
10824 -- than nothing at all (see Check_Array_Type).
10826 if Error_Posted
(N
)
10827 and then Attr_Id
/= Attribute_First
10828 and then Attr_Id
/= Attribute_Last
10829 and then Attr_Id
/= Attribute_Length
10830 and then Attr_Id
/= Attribute_Range
10835 -- If attribute was universal type, reset to actual type
10837 if Is_Universal_Numeric_Type
(Etype
(N
)) then
10838 Set_Etype
(N
, Typ
);
10841 -- Remaining processing depends on attribute
10849 -- For access attributes, if the prefix denotes an entity, it is
10850 -- interpreted as a name, never as a call. It may be overloaded,
10851 -- in which case resolution uses the profile of the context type.
10852 -- Otherwise prefix must be resolved.
10854 when Attribute_Access
10855 | Attribute_Unchecked_Access
10856 | Attribute_Unrestricted_Access
10858 -- Note possible modification if we have a variable
10860 if Is_Variable
(P
) then
10862 PN
: constant Node_Id
:= Parent
(N
);
10865 Note
: Boolean := True;
10866 -- Skip this for the case of Unrestricted_Access occurring
10867 -- in the context of a Valid check, since this otherwise
10868 -- leads to a missed warning (the Valid check does not
10869 -- really modify!) If this case, Note will be reset to
10872 -- Skip it as well if the type is an Access_To_Constant,
10873 -- given that no use of the value can modify the prefix.
10876 if Attr_Id
= Attribute_Unrestricted_Access
10877 and then Nkind
(PN
) = N_Function_Call
10881 if Nkind
(Nm
) = N_Expanded_Name
10882 and then Chars
(Nm
) = Name_Valid
10883 and then Nkind
(Prefix
(Nm
)) = N_Identifier
10884 and then Chars
(Prefix
(Nm
)) = Name_Attr_Long_Float
10889 elsif Is_Access_Constant
(Typ
) then
10894 Note_Possible_Modification
(P
, Sure
=> False);
10899 -- Case where prefix is an entity name
10901 if Is_Entity_Name
(P
) then
10903 -- Deal with case where prefix itself is overloaded
10905 if Is_Overloaded
(P
) then
10906 Get_First_Interp
(P
, Index
, It
);
10907 while Present
(It
.Nam
) loop
10908 if Type_Conformant
(Designated_Type
(Typ
), It
.Nam
) then
10909 Set_Entity
(P
, It
.Nam
);
10911 -- The prefix is definitely NOT overloaded anymore at
10912 -- this point, so we reset the Is_Overloaded flag to
10913 -- avoid any confusion when reanalyzing the node.
10915 Set_Is_Overloaded
(P
, False);
10916 Set_Is_Overloaded
(N
, False);
10917 Generate_Reference
(Entity
(P
), P
);
10921 Get_Next_Interp
(Index
, It
);
10924 -- If Prefix is a subprogram name, this reference freezes,
10925 -- but not if within spec expression mode. The profile of
10926 -- the subprogram is not frozen at this point.
10928 if not In_Spec_Expression
then
10929 Freeze_Before
(N
, Entity
(P
), Do_Freeze_Profile
=> False);
10932 -- If it is a type, there is nothing to resolve.
10933 -- If it is a subprogram, do not freeze its profile.
10934 -- If it is an object, complete its resolution.
10936 elsif Is_Overloadable
(Entity
(P
)) then
10937 if not In_Spec_Expression
then
10938 Freeze_Before
(N
, Entity
(P
), Do_Freeze_Profile
=> False);
10941 -- Nothing to do if prefix is a type name
10943 elsif Is_Type
(Entity
(P
)) then
10946 -- Otherwise non-overloaded other case, resolve the prefix
10952 -- Some further error checks
10954 Error_Msg_Name_1
:= Aname
;
10956 if not Is_Entity_Name
(P
) then
10959 elsif Is_Overloadable
(Entity
(P
))
10960 and then Is_Abstract_Subprogram
(Entity
(P
))
10962 Error_Msg_F
("prefix of % attribute cannot be abstract", P
);
10963 Set_Etype
(N
, Any_Type
);
10965 elsif Ekind
(Entity
(P
)) = E_Enumeration_Literal
then
10967 ("prefix of % attribute cannot be enumeration literal", P
);
10968 Set_Etype
(N
, Any_Type
);
10970 -- An attempt to take 'Access of a function that renames an
10971 -- enumeration literal. Issue a specialized error message.
10973 elsif Ekind
(Entity
(P
)) = E_Function
10974 and then Present
(Alias
(Entity
(P
)))
10975 and then Ekind
(Alias
(Entity
(P
))) = E_Enumeration_Literal
10978 ("prefix of % attribute cannot be function renaming "
10979 & "an enumeration literal", P
);
10980 Set_Etype
(N
, Any_Type
);
10982 elsif Convention
(Entity
(P
)) = Convention_Intrinsic
then
10983 Error_Msg_F
("prefix of % attribute cannot be intrinsic", P
);
10984 Set_Etype
(N
, Any_Type
);
10987 -- Assignments, return statements, components of aggregates,
10988 -- generic instantiations will require convention checks if
10989 -- the type is an access to subprogram. Given that there will
10990 -- also be accessibility checks on those, this is where the
10991 -- checks can eventually be centralized ???
10993 if Ekind
(Btyp
) in E_Access_Protected_Subprogram_Type
10994 | E_Access_Subprogram_Type
10995 | E_Anonymous_Access_Protected_Subprogram_Type
10996 | E_Anonymous_Access_Subprogram_Type
10998 -- Deal with convention mismatch
11000 if Convention
(Designated_Type
(Btyp
)) /=
11001 Convention
(Entity
(P
))
11004 ("subprogram & has wrong convention", P
, Entity
(P
));
11005 Error_Msg_Sloc
:= Sloc
(Btyp
);
11006 Error_Msg_FE
("\does not match & declared#", P
, Btyp
);
11008 if not Is_Itype
(Btyp
)
11009 and then not Has_Convention_Pragma
(Btyp
)
11010 and then Convention
(Entity
(P
)) /= Convention_Intrinsic
11013 ("\probable missing pragma Convention for &",
11018 Check_Subtype_Conformant
11019 (New_Id
=> Entity
(P
),
11020 Old_Id
=> Designated_Type
(Btyp
),
11024 if Attr_Id
= Attribute_Unchecked_Access
then
11025 Error_Msg_Name_1
:= Aname
;
11027 ("attribute% cannot be applied to a subprogram", P
);
11029 elsif Aname
= Name_Unrestricted_Access
then
11030 null; -- Nothing to check
11032 -- Check the static accessibility rule of 3.10.2(32).
11033 -- This rule also applies within the private part of an
11034 -- instantiation. This rule does not apply to anonymous
11035 -- access-to-subprogram types in access parameters.
11037 elsif Attr_Id
= Attribute_Access
11038 and then not In_Instance_Body
11040 (Ekind
(Btyp
) = E_Access_Subprogram_Type
11041 or else Is_Local_Anonymous_Access
(Btyp
))
11042 and then Subprogram_Access_Level
(Entity
(P
)) >
11043 Type_Access_Level
(Btyp
)
11046 ("subprogram must not be deeper than access type", P
);
11048 -- Check the restriction of 3.10.2(32) that disallows the
11049 -- access attribute within a generic body when the ultimate
11050 -- ancestor of the type of the attribute is declared outside
11051 -- of the generic unit and the subprogram is declared within
11052 -- that generic unit. This includes any such attribute that
11053 -- occurs within the body of a generic unit that is a child
11054 -- of the generic unit where the subprogram is declared.
11056 -- The rule also prohibits applying the attribute when the
11057 -- access type is a generic formal access type (since the
11058 -- level of the actual type is not known). This restriction
11059 -- does not apply when the attribute type is an anonymous
11060 -- access-to-subprogram type. Note that this check was
11061 -- revised by AI-229, because the original Ada 95 rule
11062 -- was too lax. The original rule only applied when the
11063 -- subprogram was declared within the body of the generic,
11064 -- which allowed the possibility of dangling references).
11065 -- The rule was also too strict in some cases, in that it
11066 -- didn't permit the access to be declared in the generic
11067 -- spec, whereas the revised rule does (as long as it's not
11070 -- There are a couple of subtleties of the test for applying
11071 -- the check that are worth noting. First, we only apply it
11072 -- when the levels of the subprogram and access type are the
11073 -- same (the case where the subprogram is statically deeper
11074 -- was applied above, and the case where the type is deeper
11075 -- is always safe). Second, we want the check to apply
11076 -- within nested generic bodies and generic child unit
11077 -- bodies, but not to apply to an attribute that appears in
11078 -- the generic unit's specification. This is done by testing
11079 -- that the attribute's innermost enclosing generic body is
11080 -- not the same as the innermost generic body enclosing the
11081 -- generic unit where the subprogram is declared (we don't
11082 -- want the check to apply when the access attribute is in
11083 -- the spec and there's some other generic body enclosing
11084 -- generic). Finally, there's no point applying the check
11085 -- when within an instance, because any violations will have
11086 -- been caught by the compilation of the generic unit.
11088 -- We relax this check in Relaxed_RM_Semantics mode for
11089 -- compatibility with legacy code for use by Ada source
11090 -- code analyzers (e.g. CodePeer).
11092 elsif Attr_Id
= Attribute_Access
11093 and then not Relaxed_RM_Semantics
11094 and then not In_Instance
11095 and then Present
(Enclosing_Generic_Unit
(Entity
(P
)))
11096 and then Present
(Enclosing_Generic_Body
(N
))
11097 and then Enclosing_Generic_Body
(N
) /=
11098 Enclosing_Generic_Body
11099 (Enclosing_Generic_Unit
(Entity
(P
)))
11100 and then Subprogram_Access_Level
(Entity
(P
)) =
11101 Type_Access_Level
(Btyp
)
11102 and then Ekind
(Btyp
) /=
11103 E_Anonymous_Access_Subprogram_Type
11104 and then Ekind
(Btyp
) /=
11105 E_Anonymous_Access_Protected_Subprogram_Type
11107 -- The attribute type's ultimate ancestor must be
11108 -- declared within the same generic unit as the
11109 -- subprogram is declared (including within another
11110 -- nested generic unit). The error message is
11111 -- specialized to say "ancestor" for the case where the
11112 -- access type is not its own ancestor, since saying
11113 -- simply "access type" would be very confusing.
11115 if not Declared_Within_Generic_Unit
11117 Enclosing_Generic_Unit
(Entity
(P
)))
11120 ("''Access attribute not allowed in generic body",
11123 if Root_Type
(Btyp
) = Btyp
then
11126 "access type & is declared outside " &
11127 "generic unit (RM 3.10.2(32))", N
, Btyp
);
11130 ("\because ancestor of " &
11131 "access type & is declared outside " &
11132 "generic unit (RM 3.10.2(32))", N
, Btyp
);
11136 ("\move ''Access to private part, or " &
11137 "(Ada 2005) use anonymous access type instead of &",
11140 -- If the ultimate ancestor of the attribute's type is
11141 -- a formal type, then the attribute is illegal because
11142 -- the actual type might be declared at a higher level.
11143 -- The error message is specialized to say "ancestor"
11144 -- for the case where the access type is not its own
11145 -- ancestor, since saying simply "access type" would be
11148 elsif Is_Generic_Type
(Root_Type
(Btyp
)) then
11149 if Root_Type
(Btyp
) = Btyp
then
11151 ("access type must not be a generic formal type",
11155 ("ancestor access type must not be a generic " &
11162 -- If this is a renaming, an inherited operation, or a
11163 -- subprogram instance, use the original entity. This may make
11164 -- the node type-inconsistent, so this transformation can only
11165 -- be done if the node will not be reanalyzed. In particular,
11166 -- if it is within a default expression, the transformation
11167 -- must be delayed until the default subprogram is created for
11168 -- it, when the enclosing subprogram is frozen.
11170 if Is_Entity_Name
(P
)
11171 and then Is_Overloadable
(Entity
(P
))
11172 and then Present
(Alias
(Entity
(P
)))
11173 and then Expander_Active
11176 New_Occurrence_Of
(Alias
(Entity
(P
)), Sloc
(P
)));
11179 elsif Nkind
(P
) = N_Selected_Component
11180 and then Is_Overloadable
(Entity
(Selector_Name
(P
)))
11182 -- Protected operation. If operation is overloaded, must
11183 -- disambiguate. Prefix that denotes protected object itself
11184 -- is resolved with its own type.
11186 if Attr_Id
= Attribute_Unchecked_Access
then
11187 Error_Msg_Name_1
:= Aname
;
11189 ("attribute% cannot be applied to protected operation", P
);
11192 Resolve
(Prefix
(P
));
11194 if not Is_Overloaded
(P
) then
11195 Generate_Reference
(Entity
(Selector_Name
(P
)), P
);
11198 Get_First_Interp
(P
, Index
, It
);
11199 while Present
(It
.Nam
) loop
11200 if Type_Conformant
(Designated_Type
(Typ
), It
.Nam
) then
11201 Set_Entity
(Selector_Name
(P
), It
.Nam
);
11203 -- The prefix is definitely NOT overloaded anymore at
11204 -- this point, so we reset the Is_Overloaded flag to
11205 -- avoid any confusion when reanalyzing the node.
11207 Set_Is_Overloaded
(P
, False);
11208 Set_Is_Overloaded
(N
, False);
11209 Generate_Reference
(Entity
(Selector_Name
(P
)), P
);
11213 Get_Next_Interp
(Index
, It
);
11217 -- Implement check implied by 3.10.2 (18.1/2) : F.all'access is
11218 -- statically illegal if F is an anonymous access to subprogram.
11220 elsif Nkind
(P
) = N_Explicit_Dereference
11221 and then Is_Entity_Name
(Prefix
(P
))
11222 and then Ekind
(Etype
(Entity
(Prefix
(P
)))) =
11223 E_Anonymous_Access_Subprogram_Type
11225 Error_Msg_N
("anonymous access to subprogram "
11226 & "has deeper accessibility than any master", P
);
11228 elsif Is_Overloaded
(P
) then
11230 -- Use the designated type of the context to disambiguate
11231 -- Note that this was not strictly conformant to Ada 95,
11232 -- but was the implementation adopted by most Ada 95 compilers.
11233 -- The use of the context type to resolve an Access attribute
11234 -- reference is now mandated in AI-235 for Ada 2005.
11237 Index
: Interp_Index
;
11241 Get_First_Interp
(P
, Index
, It
);
11242 while Present
(It
.Typ
) loop
11243 if Covers
(Designated_Type
(Typ
), It
.Typ
) then
11244 Resolve
(P
, It
.Typ
);
11248 Get_Next_Interp
(Index
, It
);
11255 -- X'Access is illegal if X denotes a constant and the access type
11256 -- is access-to-variable. Same for 'Unchecked_Access. The rule
11257 -- does not apply to 'Unrestricted_Access. If the reference is a
11258 -- default-initialized aggregate component for a self-referential
11259 -- type the reference is legal.
11261 if not (Ekind
(Btyp
) = E_Access_Subprogram_Type
11262 or else Ekind
(Btyp
) = E_Anonymous_Access_Subprogram_Type
11263 or else (Is_Record_Type
(Btyp
)
11265 Present
(Corresponding_Remote_Type
(Btyp
)))
11266 or else Ekind
(Btyp
) = E_Access_Protected_Subprogram_Type
11267 or else Ekind
(Btyp
)
11268 = E_Anonymous_Access_Protected_Subprogram_Type
11269 or else Is_Access_Constant
(Btyp
)
11270 or else Is_Variable
(P
)
11271 or else Attr_Id
= Attribute_Unrestricted_Access
)
11273 if Is_Entity_Name
(P
)
11274 and then Is_Type
(Entity
(P
))
11276 -- Legality of a self-reference through an access
11277 -- attribute has been verified in Analyze_Access_Attribute.
11281 elsif Comes_From_Source
(N
) then
11282 Error_Msg_F
("access-to-variable designates constant", P
);
11286 Des_Btyp
:= Designated_Type
(Btyp
);
11288 if Ada_Version
>= Ada_2005
11289 and then Is_Incomplete_Type
(Des_Btyp
)
11291 -- Ada 2005 (AI-412): If the (sub)type is a limited view of an
11292 -- imported entity, and the non-limited view is visible, make
11293 -- use of it. If it is an incomplete subtype, use the base type
11296 if From_Limited_With
(Des_Btyp
)
11297 and then Present
(Non_Limited_View
(Des_Btyp
))
11299 Des_Btyp
:= Non_Limited_View
(Des_Btyp
);
11301 elsif Ekind
(Des_Btyp
) = E_Incomplete_Subtype
then
11302 Des_Btyp
:= Etype
(Des_Btyp
);
11306 if (Attr_Id
= Attribute_Access
11308 Attr_Id
= Attribute_Unchecked_Access
)
11309 and then (Ekind
(Btyp
) = E_General_Access_Type
11310 or else Ekind
(Btyp
) = E_Anonymous_Access_Type
)
11312 -- Ada 2005 (AI-230): Check the accessibility of anonymous
11313 -- access types for stand-alone objects, record and array
11314 -- components, and return objects. For a component definition
11315 -- the level is the same of the enclosing composite type.
11317 if Ada_Version
>= Ada_2005
11318 and then (Is_Local_Anonymous_Access
(Btyp
)
11320 -- Handle cases where Btyp is the anonymous access
11321 -- type of an Ada 2012 stand-alone object.
11323 or else Nkind
(Associated_Node_For_Itype
(Btyp
)) =
11324 N_Object_Declaration
)
11325 and then Attr_Id
= Attribute_Access
11327 -- Verify that static checking is OK (namely that we aren't
11328 -- in a specific context requiring dynamic checks on
11329 -- expicitly aliased parameters), and then check the level.
11331 -- Otherwise a check will be generated later when the return
11332 -- statement gets expanded.
11334 and then not Is_Special_Aliased_Formal_Access
(N
)
11336 Static_Accessibility_Level
(N
, Zero_On_Dynamic_Level
) >
11337 Deepest_Type_Access_Level
(Btyp
)
11339 -- In an instance, this is a runtime check, but one we know
11340 -- will fail, so generate an appropriate warning. As usual,
11341 -- this kind of warning is an error in SPARK mode.
11343 if In_Instance_Body
then
11347 not No_Dynamic_Accessibility_Checks_Enabled
(P
);
11350 ("non-local pointer cannot point to local object<<", P
);
11351 Error_Msg_F
("\Program_Error [<<", P
);
11354 Make_Raise_Program_Error
(Loc
,
11355 Reason
=> PE_Accessibility_Check_Failed
));
11356 Set_Etype
(N
, Typ
);
11360 ("non-local pointer cannot point to local object", P
);
11364 if Is_Dependent_Component_Of_Mutable_Object
(P
) then
11366 ("illegal attribute for discriminant-dependent component",
11370 -- Check static matching rule of 3.10.2(27). Nominal subtype
11371 -- of the prefix must statically match the designated type.
11373 Nom_Subt
:= Etype
(P
);
11375 if Is_Constr_Subt_For_U_Nominal
(Nom_Subt
) then
11376 Nom_Subt
:= Base_Type
(Nom_Subt
);
11379 if Is_Tagged_Type
(Designated_Type
(Typ
)) then
11381 -- If the attribute is in the context of an access
11382 -- parameter, then the prefix is allowed to be of
11383 -- the class-wide type (by AI-127).
11385 if Ekind
(Typ
) = E_Anonymous_Access_Type
then
11386 if not Covers
(Designated_Type
(Typ
), Nom_Subt
)
11387 and then not Covers
(Nom_Subt
, Designated_Type
(Typ
))
11393 Desig
:= Designated_Type
(Typ
);
11395 if Is_Class_Wide_Type
(Desig
) then
11396 Desig
:= Etype
(Desig
);
11399 if Is_Anonymous_Tagged_Base
(Nom_Subt
, Desig
) then
11404 ("type of prefix: & not compatible",
11407 ("\with &, the expected designated type",
11408 P
, Designated_Type
(Typ
));
11413 elsif not Covers
(Designated_Type
(Typ
), Nom_Subt
)
11415 (not Is_Class_Wide_Type
(Designated_Type
(Typ
))
11416 and then Is_Class_Wide_Type
(Nom_Subt
))
11419 ("type of prefix: & is not covered", P
, Nom_Subt
);
11421 ("\by &, the expected designated type" &
11422 " (RM 3.10.2 (27))", P
, Designated_Type
(Typ
));
11425 if Is_Class_Wide_Type
(Designated_Type
(Typ
))
11426 and then Has_Discriminants
(Etype
(Designated_Type
(Typ
)))
11427 and then Is_Constrained
(Etype
(Designated_Type
(Typ
)))
11428 and then Designated_Type
(Typ
) /= Nom_Subt
11430 Apply_Discriminant_Check
11431 (N
, Etype
(Designated_Type
(Typ
)));
11434 -- Ada 2005 (AI-363): Require static matching when designated
11435 -- type has discriminants and a constrained partial view, since
11436 -- in general objects of such types are mutable, so we can't
11437 -- allow the access value to designate a constrained object
11438 -- (because access values must be assumed to designate mutable
11439 -- objects when designated type does not impose a constraint).
11441 elsif Subtypes_Statically_Match
(Des_Btyp
, Nom_Subt
) then
11444 elsif Has_Discriminants
(Designated_Type
(Typ
))
11445 and then not Is_Constrained
(Des_Btyp
)
11447 (Ada_Version
< Ada_2005
11449 not Object_Type_Has_Constrained_Partial_View
11450 (Typ
=> Designated_Type
(Base_Type
(Typ
)),
11451 Scop
=> Current_Scope
))
11457 ("object subtype must statically match "
11458 & "designated subtype", P
);
11460 if Is_Entity_Name
(P
)
11461 and then Is_Array_Type
(Designated_Type
(Typ
))
11464 D
: constant Node_Id
:= Declaration_Node
(Entity
(P
));
11467 ("aliased object has explicit bounds??", D
);
11469 ("\declare without bounds (and with explicit "
11470 & "initialization)??", D
);
11472 ("\for use with unconstrained access??", D
);
11477 -- Check the static accessibility rule of 3.10.2(28). Note that
11478 -- this check is not performed for the case of an anonymous
11479 -- access type, since the access attribute is always legal
11480 -- in such a context - unless the restriction
11481 -- No_Dynamic_Accessibility_Checks is active.
11483 if Attr_Id
/= Attribute_Unchecked_Access
11485 (Ekind
(Btyp
) = E_General_Access_Type
11486 or else No_Dynamic_Accessibility_Checks_Enabled
(Btyp
))
11488 -- Call Accessibility_Level directly to avoid returning zero
11489 -- on cases where the prefix is an explicitly aliased
11490 -- parameter in a return statement, instead of using the
11491 -- normal Static_Accessibility_Level function.
11493 -- Shouldn't this be handled somehow in
11494 -- Static_Accessibility_Level ???
11496 and then Nkind
(Accessibility_Level
(P
, Dynamic_Level
))
11497 = N_Integer_Literal
11499 Intval
(Accessibility_Level
(P
, Dynamic_Level
))
11500 > Deepest_Type_Access_Level
(Btyp
)
11502 Accessibility_Message
;
11507 if Ekind
(Btyp
) in E_Access_Protected_Subprogram_Type
11508 | E_Anonymous_Access_Protected_Subprogram_Type
11510 if Is_Entity_Name
(P
)
11511 and then not Is_Protected_Type
(Scope
(Entity
(P
)))
11513 Error_Msg_F
("context requires a protected subprogram", P
);
11515 -- Check accessibility of protected object against that of the
11516 -- access type, but only on user code, because the expander
11517 -- creates access references for handlers. If the context is an
11518 -- anonymous_access_to_protected, there are no accessibility
11519 -- checks either. Omit check entirely for Unrestricted_Access.
11521 elsif Static_Accessibility_Level
(P
, Zero_On_Dynamic_Level
)
11522 > Deepest_Type_Access_Level
(Btyp
)
11523 and then Comes_From_Source
(N
)
11524 and then Ekind
(Btyp
) = E_Access_Protected_Subprogram_Type
11525 and then Attr_Id
/= Attribute_Unrestricted_Access
11527 Accessibility_Message
;
11530 -- AI05-0225: If the context is not an access to protected
11531 -- function, the prefix must be a variable, given that it may
11532 -- be used subsequently in a protected call.
11534 elsif Nkind
(P
) = N_Selected_Component
11535 and then not Is_Variable
(Prefix
(P
))
11536 and then Ekind
(Entity
(Selector_Name
(P
))) /= E_Function
11539 ("target object of access to protected procedure "
11540 & "must be variable", N
);
11542 elsif Is_Entity_Name
(P
) then
11543 Check_Internal_Protected_Use
(N
, Entity
(P
));
11546 elsif Ekind
(Btyp
) in E_Access_Subprogram_Type
11547 | E_Anonymous_Access_Subprogram_Type
11548 and then Ekind
(Etype
(N
)) = E_Access_Protected_Subprogram_Type
11550 Error_Msg_F
("context requires a non-protected subprogram", P
);
11553 -- AI12-0412: The rule in RM 6.1.1(18.2/5) disallows applying
11554 -- attribute Access to a primitive of an abstract type when the
11555 -- primitive has any Pre'Class or Post'Class aspects specified
11556 -- with nonstatic expressions.
11558 if Attr_Id
= Attribute_Access
11559 and then Ekind
(Btyp
) in E_Access_Subprogram_Type
11560 | E_Anonymous_Access_Subprogram_Type
11561 and then Is_Entity_Name
(P
)
11562 and then Is_Dispatching_Operation
(Entity
(P
))
11564 Is_Prim_Of_Abst_Type_With_Nonstatic_CW_Pre_Post
(Entity
(P
))
11567 ("attribute not allowed for primitive of abstract type with "
11568 & "nonstatic class-wide pre/postconditions",
11572 -- The context cannot be a pool-specific type, but this is a
11573 -- legality rule, not a resolution rule, so it must be checked
11574 -- separately, after possibly disambiguation (see AI-245).
11576 if Ekind
(Btyp
) = E_Access_Type
11577 and then Attr_Id
/= Attribute_Unrestricted_Access
11579 Wrong_Type
(N
, Typ
);
11582 -- The context may be a constrained access type (however ill-
11583 -- advised such subtypes might be) so in order to generate a
11584 -- constraint check we need to set the type of the attribute
11585 -- reference to the base type of the context.
11587 Set_Etype
(N
, Btyp
);
11589 -- Check for incorrect atomic/volatile/VFA reference (RM C.6(12))
11591 if Attr_Id
/= Attribute_Unrestricted_Access
then
11592 if Is_Atomic_Object
(P
)
11593 and then not Is_Atomic
(Designated_Type
(Typ
))
11596 ("access to atomic object cannot yield access-to-" &
11597 "non-atomic type", P
);
11599 elsif Is_Volatile_Object_Ref
(P
)
11600 and then not Is_Volatile
(Designated_Type
(Typ
))
11603 ("access to volatile object cannot yield access-to-" &
11604 "non-volatile type", P
);
11606 elsif Is_Volatile_Full_Access_Object_Ref
(P
)
11607 and then not Is_Volatile_Full_Access
(Designated_Type
(Typ
))
11610 ("access to full access object cannot yield access-to-" &
11611 "non-full-access type", P
);
11614 -- Check for nonatomic subcomponent of a full access object
11615 -- in Ada 2022 (RM C.6 (12)).
11617 if Ada_Version
>= Ada_2022
11618 and then Is_Subcomponent_Of_Full_Access_Object
(P
)
11619 and then not Is_Atomic_Object
(P
)
11622 ("cannot have access attribute with prefix &", N
, P
);
11624 ("\nonatomic subcomponent of full access object "
11625 & "(RM C.6(12))", N
);
11629 -- Check for aliased view. We allow a nonaliased prefix when in
11630 -- an instance because the prefix may have been a tagged formal
11631 -- object, which is defined to be aliased even when the actual
11632 -- might not be (other instance cases will have been caught in
11633 -- the generic). Similarly, within an inlined body we know that
11634 -- the attribute is legal in the original subprogram, therefore
11635 -- legal in the expansion.
11637 if not (Is_Entity_Name
(P
)
11638 and then Is_Overloadable
(Entity
(P
)))
11639 and then not (Nkind
(P
) = N_Selected_Component
11641 Is_Overloadable
(Entity
(Selector_Name
(P
))))
11642 and then not Is_Aliased_View
(Original_Node
(P
))
11643 and then not In_Instance
11644 and then not In_Inlined_Body
11645 and then Comes_From_Source
(N
)
11647 -- Here we have a non-aliased view. This is illegal unless we
11648 -- have the case of Unrestricted_Access, where for now we allow
11649 -- this (we will reject later if expected type is access to an
11650 -- unconstrained array with a thin pointer).
11652 -- No need for an error message on a generated access reference
11653 -- for the controlling argument in a dispatching call: error
11654 -- will be reported when resolving the call.
11656 if Attr_Id
/= Attribute_Unrestricted_Access
then
11657 Error_Msg_Name_1
:= Aname
;
11658 Error_Msg_N
("prefix of % attribute must be aliased", P
);
11660 -- Check for unrestricted access where expected type is a thin
11661 -- pointer to an unconstrained array.
11663 elsif Has_Size_Clause
(Typ
)
11664 and then RM_Size
(Typ
) = System_Address_Size
11667 DT
: constant Entity_Id
:= Designated_Type
(Typ
);
11669 if Is_Array_Type
(DT
)
11670 and then not Is_Constrained
(DT
)
11673 ("illegal use of Unrestricted_Access attribute", P
);
11675 ("\attempt to generate thin pointer to unaliased "
11682 -- Check that the prefix does not have a value conversion of an
11683 -- array type since a value conversion is like an aggregate with
11684 -- respect to determining accessibility level (RM 3.10.2).
11686 if not Prefix_With_Safe_Accessibility_Level
then
11687 Accessibility_Message
;
11691 -- Mark that address of entity is taken in case of
11692 -- 'Unrestricted_Access or in case of a subprogram.
11694 if Is_Entity_Name
(P
)
11695 and then (Attr_Id
= Attribute_Unrestricted_Access
11696 or else Is_Subprogram
(Entity
(P
)))
11698 Set_Address_Taken
(Entity
(P
));
11701 -- Deal with possible elaboration check
11703 if Is_Entity_Name
(P
) and then Is_Subprogram
(Entity
(P
)) then
11705 Subp_Id
: constant Entity_Id
:= Entity
(P
);
11706 Scop
: constant Entity_Id
:= Scope
(Subp_Id
);
11707 Subp_Decl
: constant Node_Id
:=
11708 Unit_Declaration_Node
(Subp_Id
);
11709 Flag_Id
: Entity_Id
;
11710 Subp_Body
: Node_Id
;
11712 -- If the access has been taken and the body of the subprogram
11713 -- has not been see yet, indirect calls must be protected with
11714 -- elaboration checks. We have the proper elaboration machinery
11715 -- for subprograms declared in packages, but within a block or
11716 -- a subprogram the body will appear in the same declarative
11717 -- part, and we must insert a check in the eventual body itself
11718 -- using the elaboration flag that we generate now. The check
11719 -- is then inserted when the body is expanded. This processing
11720 -- is not needed for a stand alone expression function because
11721 -- the internally generated spec and body are always inserted
11722 -- as a pair in the same declarative list.
11726 and then Comes_From_Source
(Subp_Id
)
11727 and then Comes_From_Source
(N
)
11728 and then In_Open_Scopes
(Scop
)
11729 and then Ekind
(Scop
) in E_Block | E_Procedure | E_Function
11730 and then not Has_Completion
(Subp_Id
)
11731 and then No
(Elaboration_Entity
(Subp_Id
))
11732 and then Nkind
(Subp_Decl
) = N_Subprogram_Declaration
11733 and then Nkind
(Original_Node
(Subp_Decl
)) /=
11734 N_Expression_Function
11736 -- Create elaboration variable for it
11738 Flag_Id
:= Make_Temporary
(Loc
, 'E');
11739 Set_Elaboration_Entity
(Subp_Id
, Flag_Id
);
11740 Set_Is_Frozen
(Flag_Id
);
11742 -- Insert declaration for flag after subprogram
11743 -- declaration. Note that attribute reference may
11744 -- appear within a nested scope.
11746 Insert_After_And_Analyze
(Subp_Decl
,
11747 Make_Object_Declaration
(Loc
,
11748 Defining_Identifier
=> Flag_Id
,
11749 Object_Definition
=>
11750 New_Occurrence_Of
(Standard_Short_Integer
, Loc
),
11752 Make_Integer_Literal
(Loc
, Uint_0
)));
11754 -- The above sets the Scope of the flag entity to the
11755 -- current scope, in which the attribute appears, but
11756 -- the flag declaration has been inserted after that
11757 -- of Subp_Id, so the scope of the flag is the same as
11758 -- that of Subp_Id. This is relevant when unnesting,
11759 -- where processing depends on correct scope setting.
11761 Set_Scope
(Flag_Id
, Scop
);
11764 -- Taking the 'Access of an expression function freezes its
11765 -- expression (RM 13.14 10.3/3). This does not apply to an
11766 -- expression function that acts as a completion because the
11767 -- generated body is immediately analyzed and the expression
11768 -- is automatically frozen.
11770 if Is_Expression_Function
(Subp_Id
)
11771 and then Present
(Corresponding_Body
(Subp_Decl
))
11774 Unit_Declaration_Node
(Corresponding_Body
(Subp_Decl
));
11776 -- The body has already been analyzed when the expression
11777 -- function acts as a completion.
11779 if Analyzed
(Subp_Body
) then
11782 -- Attribute 'Access may appear within the generated body
11783 -- of the expression function subject to the attribute:
11785 -- function F is (... F'Access ...);
11787 -- If the expression function is on the scope stack, then
11788 -- the body is currently being analyzed. Do not reanalyze
11789 -- it because this will lead to infinite recursion.
11791 elsif In_Open_Scopes
(Subp_Id
) then
11794 -- If reference to the expression function appears in an
11795 -- inner scope, for example as an actual in an instance,
11796 -- this is not a freeze point either.
11798 elsif Scope
(Subp_Id
) /= Current_Scope
then
11801 -- Analyze the body of the expression function to freeze
11802 -- the expression. This takes care of the case where the
11803 -- 'Access is part of dispatch table initialization and
11804 -- the generated body of the expression function has not
11805 -- been analyzed yet.
11808 Analyze
(Subp_Body
);
11818 -- Deal with resolving the type for Address attribute, overloading
11819 -- is not permitted here, since there is no context to resolve it.
11821 when Attribute_Address
11822 | Attribute_Code_Address
11824 -- To be safe, assume that if the address of a variable is taken,
11825 -- it may be modified via this address, so note modification.
11827 if Is_Variable
(P
) then
11828 Note_Possible_Modification
(P
, Sure
=> False);
11831 if Nkind
(P
) in N_Subexpr
11832 and then Is_Overloaded
(P
)
11834 Get_First_Interp
(P
, Index
, It
);
11835 Get_Next_Interp
(Index
, It
);
11837 if Present
(It
.Nam
) then
11838 Error_Msg_Name_1
:= Aname
;
11840 ("prefix of % attribute cannot be overloaded", P
);
11844 if not Is_Entity_Name
(P
)
11845 or else not Is_Overloadable
(Entity
(P
))
11847 if not Is_Task_Type
(Etype
(P
))
11848 or else Nkind
(P
) = N_Explicit_Dereference
11854 -- If this is the name of a derived subprogram, or that of a
11855 -- generic actual, the address is that of the original entity.
11857 if Is_Entity_Name
(P
)
11858 and then Is_Overloadable
(Entity
(P
))
11859 and then Present
(Alias
(Entity
(P
)))
11862 New_Occurrence_Of
(Alias
(Entity
(P
)), Sloc
(P
)));
11865 if Is_Entity_Name
(P
) then
11866 Set_Address_Taken
(Entity
(P
));
11869 if Nkind
(P
) = N_Slice
then
11871 -- Arr (X .. Y)'address is identical to Arr (X)'address,
11872 -- even if the array is packed and the slice itself is not
11873 -- addressable. Transform the prefix into an indexed component.
11875 -- Note that the transformation is safe only if we know that
11876 -- the slice is non-null. That is because a null slice can have
11877 -- an out of bounds index value.
11879 -- Right now, gigi blows up if given 'Address on a slice as a
11880 -- result of some incorrect freeze nodes generated by the front
11881 -- end, and this covers up that bug in one case, but the bug is
11882 -- likely still there in the cases not handled by this code ???
11884 -- It's not clear what 'Address *should* return for a null
11885 -- slice with out of bounds indexes, this might be worth an ARG
11888 -- One approach would be to do a length check unconditionally,
11889 -- and then do the transformation below unconditionally, but
11890 -- analyze with checks off, avoiding the problem of the out of
11891 -- bounds index. This approach would interpret the address of
11892 -- an out of bounds null slice as being the address where the
11893 -- array element would be if there was one, which is probably
11894 -- as reasonable an interpretation as any ???
11897 Loc
: constant Source_Ptr
:= Sloc
(P
);
11898 D
: constant Node_Id
:= Discrete_Range
(P
);
11902 if Is_Entity_Name
(D
)
11905 (Type_Low_Bound
(Entity
(D
)),
11906 Type_High_Bound
(Entity
(D
)))
11909 Make_Attribute_Reference
(Loc
,
11910 Prefix
=> (New_Occurrence_Of
(Entity
(D
), Loc
)),
11911 Attribute_Name
=> Name_First
);
11913 elsif Nkind
(D
) = N_Range
11914 and then Not_Null_Range
(Low_Bound
(D
), High_Bound
(D
))
11916 Lo
:= Low_Bound
(D
);
11922 if Present
(Lo
) then
11924 Make_Indexed_Component
(Loc
,
11925 Prefix
=> Relocate_Node
(Prefix
(P
)),
11926 Expressions
=> New_List
(Lo
)));
11928 Analyze_And_Resolve
(P
);
11937 -- Prefix of Body_Version attribute can be a subprogram name which
11938 -- must not be resolved, since this is not a call.
11940 when Attribute_Body_Version
=>
11947 -- Prefix of Caller attribute is an entry name which must not
11948 -- be resolved, since this is definitely not an entry call.
11950 when Attribute_Caller
=>
11957 -- Shares processing with Address attribute
11963 -- If the prefix of the Count attribute is an entry name it must not
11964 -- be resolved, since this is definitely not an entry call. However,
11965 -- if it is an element of an entry family, the index itself may
11966 -- have to be resolved because it can be a general expression.
11968 when Attribute_Count
=>
11969 if Nkind
(P
) = N_Indexed_Component
11970 and then Is_Entity_Name
(Prefix
(P
))
11973 Indx
: constant Node_Id
:= First
(Expressions
(P
));
11974 Fam
: constant Entity_Id
:= Entity
(Prefix
(P
));
11976 Resolve
(Indx
, Entry_Index_Type
(Fam
));
11977 Apply_Scalar_Range_Check
(Indx
, Entry_Index_Type
(Fam
));
11985 -- Prefix of the Elaborated attribute is a subprogram name which
11986 -- must not be resolved, since this is definitely not a call. Note
11987 -- that it is a library unit, so it cannot be overloaded here.
11989 when Attribute_Elaborated
=>
11996 -- Prefix of Enabled attribute is a check name, which must be treated
11997 -- specially and not touched by Resolve.
11999 when Attribute_Enabled
=>
12006 -- Do not resolve the prefix of Loop_Entry, instead wait until the
12007 -- attribute has been expanded (see Expand_Loop_Entry_Attributes).
12008 -- The delay ensures that any generated checks or temporaries are
12009 -- inserted before the relocated prefix.
12011 when Attribute_Loop_Entry
=>
12014 --------------------
12015 -- Mechanism_Code --
12016 --------------------
12018 -- Prefix of the Mechanism_Code attribute is a function name
12019 -- which must not be resolved. Should we check for overloaded ???
12021 when Attribute_Mechanism_Code
=>
12028 -- Most processing is done in sem_dist, after determining the
12029 -- context type. Node is rewritten as a conversion to a runtime call.
12031 when Attribute_Partition_ID
=>
12032 Process_Partition_Id
(N
);
12039 when Attribute_Pool_Address
=>
12046 -- We replace the Range attribute node with a range expression whose
12047 -- bounds are the 'First and 'Last attributes applied to the same
12048 -- prefix. The reason that we do this transformation here instead of
12049 -- in the expander is that it simplifies other parts of the semantic
12050 -- analysis which assume that the Range has been replaced; thus it
12051 -- must be done even when in semantic-only mode (note that the RM
12052 -- specifically mentions this equivalence, we take care that the
12053 -- prefix is only evaluated once).
12055 when Attribute_Range
=> Range_Attribute
: declare
12061 if not Is_Entity_Name
(P
) or else not Is_Type
(Entity
(P
)) then
12064 -- If the prefix is a function call returning on the secondary
12065 -- stack, we must make sure to mark/release the stack.
12067 if Nkind
(P
) = N_Function_Call
12068 and then Nkind
(Parent
(N
)) = N_Loop_Parameter_Specification
12069 and then Requires_Transient_Scope
(Etype
(P
))
12071 Set_Uses_Sec_Stack
(Scope
(Current_Scope
));
12075 Dims
:= Expressions
(N
);
12078 Make_Attribute_Reference
(Loc
,
12079 Prefix
=> Duplicate_Subexpr
(P
, Name_Req
=> True),
12080 Attribute_Name
=> Name_Last
,
12081 Expressions
=> Dims
);
12084 Make_Attribute_Reference
(Loc
,
12086 Attribute_Name
=> Name_First
,
12087 Expressions
=> (Dims
));
12089 -- Do not share the dimension indicator, if present. Even though
12090 -- it is a static constant, its source location may be modified
12091 -- when printing expanded code and node sharing will lead to chaos
12094 if Present
(Dims
) then
12095 Set_Expressions
(LB
, New_List
(New_Copy_Tree
(First
(Dims
))));
12098 -- If the original was marked as Must_Not_Freeze (see code in
12099 -- Sem_Ch3.Make_Index), then make sure the rewriting does not
12102 if Must_Not_Freeze
(N
) then
12103 Set_Must_Not_Freeze
(HB
);
12104 Set_Must_Not_Freeze
(LB
);
12105 Set_Must_Not_Freeze
(Prefix
(HB
));
12106 Set_Must_Not_Freeze
(Prefix
(LB
));
12109 if Raises_Constraint_Error
(Prefix
(N
)) then
12111 -- Preserve Sloc of prefix in the new bounds, so that the
12112 -- posted warning can be removed if we are within unreachable
12115 Set_Sloc
(LB
, Sloc
(Prefix
(N
)));
12116 Set_Sloc
(HB
, Sloc
(Prefix
(N
)));
12119 Rewrite
(N
, Make_Range
(Loc
, LB
, HB
));
12120 Analyze_And_Resolve
(N
, Typ
);
12122 -- Ensure that the expanded range does not have side effects
12124 Force_Evaluation
(LB
);
12125 Force_Evaluation
(HB
);
12127 -- Normally after resolving attribute nodes, Eval_Attribute
12128 -- is called to do any possible static evaluation of the node.
12129 -- However, here since the Range attribute has just been
12130 -- transformed into a range expression it is no longer an
12131 -- attribute node and therefore the call needs to be avoided
12132 -- and is accomplished by simply returning from the procedure.
12135 end Range_Attribute
;
12141 when Attribute_Reduce
=>
12143 E1
: constant Node_Id
:= First
(Expressions
(N
));
12144 E2
: constant Node_Id
:= Next
(E1
);
12145 Op
: Entity_Id
:= Empty
;
12147 Index
: Interp_Index
;
12149 function Proper_Op
(Op
: Entity_Id
) return Boolean;
12155 function Proper_Op
(Op
: Entity_Id
) return Boolean is
12156 F1
, F2
: Entity_Id
;
12159 F1
:= First_Formal
(Op
);
12163 F2
:= Next_Formal
(F1
);
12165 or else Present
(Next_Formal
(F2
))
12169 elsif Ekind
(Op
) = E_Procedure
then
12170 return Ekind
(F1
) = E_In_Out_Parameter
12171 and then Covers
(Typ
, Etype
(F1
));
12175 (Ekind
(Op
) = E_Operator
12176 and then Scope
(Op
) = Standard_Standard
)
12177 or else Covers
(Typ
, Etype
(Op
));
12184 if Is_Overloaded
(E1
) then
12185 Get_First_Interp
(E1
, Index
, It
);
12186 while Present
(It
.Nam
) loop
12187 if Proper_Op
(It
.Nam
) then
12189 Set_Entity
(E1
, Op
);
12193 Get_Next_Interp
(Index
, It
);
12196 elsif Nkind
(E1
) = N_Attribute_Reference
12197 and then (Attribute_Name
(E1
) = Name_Max
12198 or else Attribute_Name
(E1
) = Name_Min
)
12202 elsif Proper_Op
(Entity
(E1
)) then
12204 Set_Etype
(N
, Typ
);
12208 Error_Msg_N
("No visible subprogram for reduction", E1
);
12216 -- We will only come here during the prescan of a spec expression
12217 -- containing a Result attribute. In that case the proper Etype has
12218 -- already been set, and nothing more needs to be done here.
12220 when Attribute_Result
=>
12223 ----------------------
12224 -- Unchecked_Access --
12225 ----------------------
12227 -- Processing is shared with Access
12229 -------------------------
12230 -- Unrestricted_Access --
12231 -------------------------
12233 -- Processing is shared with Access
12239 -- Resolve aggregate components in component associations
12241 when Attribute_Update
=> Update
: declare
12242 Aggr
: constant Node_Id
:= First
(Expressions
(N
));
12243 Typ
: constant Entity_Id
:= Etype
(Prefix
(N
));
12249 -- Set the Etype of the aggregate to that of the prefix, even
12250 -- though the aggregate may not be a proper representation of a
12251 -- value of the type (missing or duplicated associations, etc.)
12252 -- Complete resolution of the prefix. Note that in Ada 2012 it
12253 -- can be a qualified expression that is e.g. an aggregate.
12255 Set_Etype
(Aggr
, Typ
);
12256 Resolve
(Prefix
(N
), Typ
);
12258 -- For an array type, resolve expressions with the component type
12259 -- of the array, and apply constraint checks when needed.
12261 if Is_Array_Type
(Typ
) then
12262 Assoc
:= First
(Component_Associations
(Aggr
));
12263 while Present
(Assoc
) loop
12264 Expr
:= Expression
(Assoc
);
12265 Resolve
(Expr
, Component_Type
(Typ
));
12267 -- The choices in the association are static constants,
12268 -- or static aggregates each of whose components belongs
12269 -- to the proper index type. However, they must also
12270 -- belong to the index subtype (s) of the prefix, which
12271 -- may be a subtype (e.g. given by a slice).
12273 -- Choices may also be identifiers with no staticness
12274 -- requirements, in which case they must resolve to the
12283 C
:= First
(Choices
(Assoc
));
12284 while Present
(C
) loop
12285 Indx
:= First_Index
(Etype
(Prefix
(N
)));
12287 if Nkind
(C
) /= N_Aggregate
then
12288 Analyze_And_Resolve
(C
, Etype
(Indx
));
12290 C_E
:= First
(Expressions
(C
));
12291 while Present
(C_E
) loop
12292 Analyze_And_Resolve
(C_E
, Etype
(Indx
));
12306 -- For a record type, use type of each component, which is
12307 -- recorded during analysis.
12310 Assoc
:= First
(Component_Associations
(Aggr
));
12311 while Present
(Assoc
) loop
12312 Comp
:= First
(Choices
(Assoc
));
12313 Expr
:= Expression
(Assoc
);
12315 if Nkind
(Comp
) /= N_Others_Choice
12316 and then not Error_Posted
(Comp
)
12318 Resolve
(Expr
, Etype
(Entity
(Comp
)));
12330 -- Apply range check. Note that we did not do this during the
12331 -- analysis phase, since we wanted Eval_Attribute to have a
12332 -- chance at finding an illegal out of range value.
12334 when Attribute_Val
=>
12336 -- Note that we do our own Eval_Attribute call here rather than
12337 -- use the common one, because we need to do processing after
12338 -- the call, as per above comment.
12340 Eval_Attribute
(N
);
12342 -- Eval_Attribute may replace the node with a raise CE, or
12343 -- fold it to a constant. Obviously we only apply a scalar
12344 -- range check if this did not happen.
12346 if Nkind
(N
) = N_Attribute_Reference
12347 and then Attribute_Name
(N
) = Name_Val
12349 Apply_Scalar_Range_Check
(First
(Expressions
(N
)), Btyp
);
12358 -- Prefix of Version attribute can be a subprogram name which
12359 -- must not be resolved, since this is not a call.
12361 when Attribute_Version
=>
12364 ----------------------
12365 -- Other Attributes --
12366 ----------------------
12368 -- For other attributes, resolve prefix unless it is a type. If
12369 -- the attribute reference itself is a type name ('Base and 'Class)
12370 -- then this is only legal within a task or protected record.
12373 if not Is_Entity_Name
(P
) or else not Is_Type
(Entity
(P
)) then
12377 -- If the attribute reference itself is a type name ('Base,
12378 -- 'Class) then this is only legal within a task or protected
12379 -- record. What is this all about ???
12381 if Is_Entity_Name
(N
) and then Is_Type
(Entity
(N
)) then
12382 if Is_Concurrent_Type
(Entity
(N
))
12383 and then In_Open_Scopes
(Entity
(P
))
12388 ("invalid use of subtype name in expression or call", N
);
12392 -- For attributes whose argument may be a string, complete
12393 -- resolution of argument now. This avoids premature expansion
12394 -- (and the creation of transient scopes) before the attribute
12395 -- reference is resolved.
12398 when Attribute_Valid_Value | Attribute_Value
=>
12399 Resolve
(First
(Expressions
(N
)), Standard_String
);
12401 when Attribute_Wide_Value
=>
12402 Resolve
(First
(Expressions
(N
)), Standard_Wide_String
);
12404 when Attribute_Wide_Wide_Value
=>
12405 Resolve
(First
(Expressions
(N
)), Standard_Wide_Wide_String
);
12407 when others => null;
12410 -- If the prefix of the attribute is a class-wide type then it
12411 -- will be expanded into a dispatching call to a predefined
12412 -- primitive. Therefore we must check for potential violation
12413 -- of such restriction.
12415 if Is_Class_Wide_Type
(Etype
(P
)) then
12416 Check_Restriction
(No_Dispatching_Calls
, N
);
12420 -- Mark use clauses of the original prefix if the attribute is applied
12423 if Nkind
(Original_Node
(P
)) in N_Has_Entity
12424 and then Present
(Entity
(Original_Node
(P
)))
12426 Mark_Use_Clauses
(Original_Node
(P
));
12429 -- Normally the Freezing is done by Resolve but sometimes the Prefix
12430 -- is not resolved, in which case the freezing must be done now.
12432 -- For an elaboration check on a subprogram, we do not freeze its type.
12433 -- It may be declared in an unrelated scope, in particular in the case
12434 -- of a generic function whose type may remain unelaborated.
12436 if Attr_Id
= Attribute_Elaborated
then
12439 -- Should this be restricted to Expander_Active???
12442 Freeze_Expression
(P
);
12445 -- Finally perform static evaluation on the attribute reference
12447 Analyze_Dimension
(N
);
12448 Eval_Attribute
(N
);
12449 end Resolve_Attribute
;
12451 ------------------------
12452 -- Set_Boolean_Result --
12453 ------------------------
12455 procedure Set_Boolean_Result
(N
: Node_Id
; B
: Boolean) is
12456 Loc
: constant Source_Ptr
:= Sloc
(N
);
12459 Rewrite
(N
, New_Occurrence_Of
(Standard_True
, Loc
));
12461 Rewrite
(N
, New_Occurrence_Of
(Standard_False
, Loc
));
12463 end Set_Boolean_Result
;
12465 --------------------------------
12466 -- Stream_Attribute_Available --
12467 --------------------------------
12469 function Stream_Attribute_Available
12471 Nam
: TSS_Name_Type
;
12472 Partial_View
: Entity_Id
:= Empty
) return Boolean
12474 Etyp
: Entity_Id
:= Typ
;
12476 -- Start of processing for Stream_Attribute_Available
12479 -- We need some comments in this body ???
12481 if Has_Stream_Attribute_Definition
(Typ
, Nam
) then
12485 if Is_Class_Wide_Type
(Typ
) then
12486 return not Is_Limited_Type
(Typ
)
12487 or else Stream_Attribute_Available
(Etype
(Typ
), Nam
);
12490 if Nam
= TSS_Stream_Input
12491 and then Is_Abstract_Type
(Typ
)
12492 and then not Is_Class_Wide_Type
(Typ
)
12497 if not (Is_Limited_Type
(Typ
)
12498 or else (Present
(Partial_View
)
12499 and then Is_Limited_Type
(Partial_View
)))
12504 -- In Ada 2005, Input can invoke Read, and Output can invoke Write
12506 if Nam
= TSS_Stream_Input
12507 and then Ada_Version
>= Ada_2005
12508 and then Stream_Attribute_Available
(Etyp
, TSS_Stream_Read
)
12512 elsif Nam
= TSS_Stream_Output
12513 and then Ada_Version
>= Ada_2005
12514 and then Stream_Attribute_Available
(Etyp
, TSS_Stream_Write
)
12519 -- Case of Read and Write: check for attribute definition clause that
12520 -- applies to an ancestor type.
12522 while Etype
(Etyp
) /= Etyp
loop
12524 Derived_Type
: constant Entity_Id
:= Etyp
;
12526 Etyp
:= Etype
(Etyp
);
12528 if Has_Stream_Attribute_Definition
(Etyp
, Nam
) then
12529 if not Derivation_Too_Early_To_Inherit
(Derived_Type
, Nam
) then
12536 if Ada_Version
< Ada_2005
then
12538 -- In Ada 95 mode, also consider a non-visible definition
12541 Btyp
: constant Entity_Id
:= Implementation_Base_Type
(Typ
);
12544 and then Stream_Attribute_Available
12545 (Btyp
, Nam
, Partial_View
=> Typ
);
12550 end Stream_Attribute_Available
;