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 -- Skip processing during preanalysis of class-wide preconditions and
1343 -- postconditions since at this stage the expression is not installed
1344 -- yet on its definite context.
1346 if Inside_Class_Condition_Preanalysis
then
1348 Spec_Id
:= Current_Scope
;
1352 -- Traverse the parent chain to find the aspect or pragma where the
1353 -- attribute resides.
1356 while Present
(Prag
) loop
1357 if Nkind
(Prag
) in N_Aspect_Specification | N_Pragma
then
1360 -- Prevent the search from going too far
1362 elsif Is_Body_Or_Package_Declaration
(Prag
) then
1366 Prag
:= Parent
(Prag
);
1369 -- The attribute is allowed to appear only in postcondition-like
1370 -- aspects or pragmas.
1372 if Nkind
(Prag
) in N_Aspect_Specification | N_Pragma
then
1373 if Nkind
(Prag
) = N_Aspect_Specification
then
1374 Prag_Nam
:= Chars
(Identifier
(Prag
));
1376 Prag_Nam
:= Pragma_Name
(Prag
);
1379 if Prag_Nam
= Name_Check
then
1380 Check_Placement_In_Check
(Prag
);
1382 elsif Prag_Nam
= Name_Contract_Cases
then
1383 Check_Placement_In_Contract_Cases
(Prag
);
1385 -- Attribute 'Result is allowed to appear in aspect or pragma
1386 -- [Refined_]Depends (SPARK RM 6.1.5(11)).
1388 elsif Prag_Nam
in Name_Depends | Name_Refined_Depends
1389 and then Aname
= Name_Result
1393 -- Attribute 'Result is allowed to appear in aspect
1394 -- Relaxed_Initialization (SPARK RM 6.10).
1396 elsif Prag_Nam
= Name_Relaxed_Initialization
1397 and then Aname
= Name_Result
1401 elsif Prag_Nam
in Name_Post
1403 | Name_Postcondition
1408 elsif Prag_Nam
= Name_Test_Case
then
1409 Check_Placement_In_Test_Case
(Prag
);
1416 -- 'Old attribute reference ok in a _Postconditions procedure
1418 elsif Nkind
(Prag
) = N_Subprogram_Body
1419 and then not Comes_From_Source
(Prag
)
1420 and then Nkind
(Corresponding_Spec
(Prag
)) = N_Defining_Identifier
1421 and then Chars
(Corresponding_Spec
(Prag
)) = Name_uPostconditions
1425 -- Otherwise the placement of the attribute is illegal
1432 -- Find the related subprogram subject to the aspect or pragma
1434 if Nkind
(Prag
) = N_Aspect_Specification
then
1435 Subp_Decl
:= Parent
(Prag
);
1436 elsif Nkind
(Prag
) = N_Subprogram_Body
then
1438 Enclosing_Scope
: constant Node_Id
:=
1439 Scope
(Corresponding_Spec
(Prag
));
1441 pragma Assert
(Postconditions_Proc
(Enclosing_Scope
)
1442 = Corresponding_Spec
(Prag
));
1443 Subp_Decl
:= Parent
(Parent
(Enclosing_Scope
));
1446 Subp_Decl
:= Find_Related_Declaration_Or_Body
(Prag
);
1449 -- The aspect or pragma where the attribute resides should be
1450 -- associated with a subprogram declaration or a body. If this is not
1451 -- the case, then the aspect or pragma is illegal. Return as analysis
1452 -- cannot be carried out. Note that it is legal to have the aspect
1453 -- appear on a subprogram renaming, when the renamed entity is an
1454 -- attribute reference.
1456 -- Generating C code the internally built nested _postcondition
1457 -- subprograms are inlined; after expanded, inlined aspects are
1458 -- located in the internal block generated by the frontend.
1460 if Nkind
(Subp_Decl
) = N_Block_Statement
1461 and then Modify_Tree_For_C
1462 and then In_Inlined_Body
1466 elsif Nkind
(Subp_Decl
) not in N_Abstract_Subprogram_Declaration
1467 | N_Entry_Declaration
1468 | N_Expression_Function
1469 | N_Generic_Subprogram_Declaration
1471 | N_Subprogram_Body_Stub
1472 | N_Subprogram_Declaration
1473 | N_Subprogram_Renaming_Declaration
1478 -- If we get here, then the attribute is legal
1481 Spec_Id
:= Unique_Defining_Entity
(Subp_Decl
);
1483 -- When generating C code, nested _postcondition subprograms are
1484 -- inlined by the front end to avoid problems (when unnested) with
1485 -- referenced itypes. Handle that here, since as part of inlining the
1486 -- expander nests subprogram within a dummy procedure named _parent
1487 -- (see Build_Postconditions_Procedure and Build_Body_To_Inline).
1488 -- Hence, in this context, the spec_id of _postconditions is the
1491 if Modify_Tree_For_C
1492 and then Chars
(Spec_Id
) = Name_uParent
1493 and then Chars
(Scope
(Spec_Id
)) = Name_uPostconditions
1495 -- This situation occurs only when preanalyzing the inlined body
1497 pragma Assert
(not Full_Analysis
);
1499 Spec_Id
:= Scope
(Spec_Id
);
1500 pragma Assert
(Is_Inlined
(Spec_Id
));
1502 end Analyze_Attribute_Old_Result
;
1504 -----------------------------
1505 -- Analyze_Image_Attribute --
1506 -----------------------------
1508 procedure Analyze_Image_Attribute
(Str_Typ
: Entity_Id
) is
1509 procedure Check_Image_Type
(Image_Type
: Entity_Id
);
1510 -- Check that Image_Type is legal as the type of a prefix of 'Image.
1511 -- Legality depends on the Ada language version.
1513 ----------------------
1514 -- Check_Image_Type --
1515 ----------------------
1517 procedure Check_Image_Type
(Image_Type
: Entity_Id
) is
1519 -- Image_Type may be empty in case of another error detected,
1520 -- or if an N_Raise_xxx_Error node is a parent of N.
1522 if Ada_Version
< Ada_2022
1523 and then Present
(Image_Type
)
1524 and then not Is_Scalar_Type
(Image_Type
)
1526 Error_Msg_Ada_2022_Feature
("nonscalar ''Image", Sloc
(P
));
1529 end Check_Image_Type
;
1531 -- Start of processing for Analyze_Image_Attribute
1534 -- AI12-0124: The ARG has adopted the GNAT semantics of 'Img for
1535 -- scalar types, so that the prefix can be an object, a named value,
1536 -- or a type. If the prefix is an object, there is no argument.
1538 if Is_Object_Image
(P
) then
1540 Set_Etype
(N
, Str_Typ
);
1541 Check_Image_Type
(Etype
(P
));
1543 if Attr_Id
/= Attribute_Img
then
1544 Error_Msg_Ada_2012_Feature
("|Object''Image", Sloc
(P
));
1548 Set_Etype
(N
, Str_Typ
);
1550 pragma Assert
(Is_Entity_Name
(P
) and then Is_Type
(Entity
(P
)));
1552 if Ekind
(Entity
(P
)) = E_Incomplete_Type
1553 and then Present
(Full_View
(Entity
(P
)))
1555 P_Type
:= Full_View
(Entity
(P
));
1556 P_Base_Type
:= Base_Type
(P_Type
);
1557 Set_Entity
(P
, P_Type
);
1560 Check_Image_Type
(P_Type
);
1561 Resolve
(E1
, P_Base_Type
);
1562 Validate_Non_Static_Attribute_Function_Call
;
1565 Check_Enum_Image
(Check_Enumeration_Maps
=> True);
1567 -- Check restriction No_Fixed_IO. Note the check of Comes_From_Source
1568 -- to avoid giving a duplicate message for when Image attributes
1569 -- applied to object references get expanded into type-based Image
1572 if Restriction_Check_Required
(No_Fixed_IO
)
1573 and then Comes_From_Source
(N
)
1574 and then Is_Fixed_Point_Type
(P_Type
)
1576 Check_Restriction
(No_Fixed_IO
, P
);
1578 end Analyze_Image_Attribute
;
1580 ---------------------------------
1581 -- Bad_Attribute_For_Predicate --
1582 ---------------------------------
1584 procedure Bad_Attribute_For_Predicate
is
1586 if Is_Scalar_Type
(P_Type
)
1587 and then Comes_From_Source
(N
)
1589 Error_Msg_Name_1
:= Aname
;
1590 Bad_Predicated_Subtype_Use
1591 ("type& has predicates, attribute % not allowed", N
, P_Type
);
1593 end Bad_Attribute_For_Predicate
;
1595 --------------------------------
1596 -- Check_Array_Or_Scalar_Type --
1597 --------------------------------
1599 procedure Check_Array_Or_Scalar_Type
is
1600 function In_Aspect_Specification
return Boolean;
1601 -- A current instance of a type in an aspect specification is an
1602 -- object and not a type, and therefore cannot be of a scalar type
1603 -- in the prefix of one of the array attributes if the attribute
1604 -- reference is part of an aspect expression.
1606 -----------------------------
1607 -- In_Aspect_Specification --
1608 -----------------------------
1610 function In_Aspect_Specification
return Boolean is
1615 while Present
(P
) loop
1616 if Nkind
(P
) = N_Aspect_Specification
then
1617 return P_Type
= Entity
(P
);
1619 elsif Nkind
(P
) in N_Declaration
then
1627 end In_Aspect_Specification
;
1633 -- Start of processing for Check_Array_Or_Scalar_Type
1636 -- Case of string literal or string literal subtype. These cases
1637 -- cannot arise from legal Ada code, but the expander is allowed
1638 -- to generate them. They require special handling because string
1639 -- literal subtypes do not have standard bounds (the whole idea
1640 -- of these subtypes is to avoid having to generate the bounds)
1642 if Ekind
(P_Type
) = E_String_Literal_Subtype
then
1643 Set_Etype
(N
, Etype
(First_Index
(P_Base_Type
)));
1648 elsif Is_Scalar_Type
(P_Type
) then
1651 if Present
(E1
) then
1652 Error_Attr
("invalid argument in % attribute", E1
);
1654 elsif In_Aspect_Specification
then
1656 ("prefix of % attribute cannot be the current instance of a "
1657 & "scalar type", P
);
1660 Set_Etype
(N
, P_Base_Type
);
1664 -- The following is a special test to allow 'First to apply to
1665 -- private scalar types if the attribute comes from generated
1666 -- code. This occurs in the case of Normalize_Scalars code.
1668 elsif Is_Private_Type
(P_Type
)
1669 and then Present
(Full_View
(P_Type
))
1670 and then Is_Scalar_Type
(Full_View
(P_Type
))
1671 and then not Comes_From_Source
(N
)
1673 Set_Etype
(N
, Implementation_Base_Type
(P_Type
));
1675 -- Array types other than string literal subtypes handled above
1680 -- We know prefix is an array type, or the name of an array
1681 -- object, and that the expression, if present, is static
1682 -- and within the range of the dimensions of the type.
1684 pragma Assert
(Is_Array_Type
(P_Type
));
1685 Index
:= First_Index
(P_Base_Type
);
1689 -- First dimension assumed
1691 Set_Etype
(N
, Base_Type
(Etype
(Index
)));
1695 Udims
: constant Uint
:= Expr_Value
(E1
);
1696 Dims
: constant Int
:= UI_To_Int
(Udims
);
1698 for J
in 1 .. Dims
- 1 loop
1703 Set_Etype
(N
, Base_Type
(Etype
(Index
)));
1706 end Check_Array_Or_Scalar_Type
;
1708 ----------------------
1709 -- Check_Array_Type --
1710 ----------------------
1712 procedure Check_Array_Type
is
1714 -- Dimension number for array attributes
1717 -- If the type is a string literal type, then this must be generated
1718 -- internally, and no further check is required on its legality.
1720 if Ekind
(P_Type
) = E_String_Literal_Subtype
then
1723 -- If the type is a composite, it is an illegal aggregate, no point
1726 elsif P_Type
= Any_Composite
then
1727 raise Bad_Attribute
;
1730 -- Normal case of array type or subtype. Note that if the
1731 -- prefix is a current instance of a type declaration it
1732 -- appears within an aspect specification and is legal.
1734 Check_Either_E0_Or_E1
;
1737 if Is_Array_Type
(P_Type
) then
1738 if not Is_Constrained
(P_Type
)
1739 and then Is_Entity_Name
(P
)
1740 and then Is_Type
(Entity
(P
))
1741 and then not Is_Current_Instance
(P
)
1743 -- Note: we do not call Error_Attr here, since we prefer to
1744 -- continue, using the relevant index type of the array,
1745 -- even though it is unconstrained. This gives better error
1746 -- recovery behavior.
1748 Error_Msg_Name_1
:= Aname
;
1750 ("prefix for % attribute must be constrained array", P
);
1753 -- The attribute reference freezes the type, and thus the
1754 -- component type, even if the attribute may not depend on the
1755 -- component. Diagnose arrays with incomplete components now.
1756 -- If the prefix is an access to array, this does not freeze
1757 -- the designated type.
1759 if Nkind
(P
) /= N_Explicit_Dereference
then
1760 Check_Fully_Declared
(Component_Type
(P_Type
), P
);
1763 D
:= Number_Dimensions
(P_Type
);
1766 if Is_Private_Type
(P_Type
) then
1767 Error_Attr_P
("prefix for % attribute may not be private type");
1769 elsif Is_Access_Type
(P_Type
)
1770 and then Is_Array_Type
(Designated_Type
(P_Type
))
1771 and then Is_Entity_Name
(P
)
1772 and then Is_Type
(Entity
(P
))
1774 Error_Attr_P
("prefix of % attribute cannot be access type");
1776 elsif Attr_Id
= Attribute_First
1778 Attr_Id
= Attribute_Last
1780 Error_Attr
("invalid prefix for % attribute", P
);
1783 Error_Attr_P
("prefix for % attribute must be array");
1787 if Present
(E1
) then
1788 Resolve
(E1
, Any_Integer
);
1789 Set_Etype
(E1
, Standard_Integer
);
1791 if not Is_OK_Static_Expression
(E1
)
1792 or else Raises_Constraint_Error
(E1
)
1794 Flag_Non_Static_Expr
1795 ("expression for dimension must be static!", E1
);
1798 elsif Expr_Value
(E1
) > D
or else Expr_Value
(E1
) < 1 then
1799 Error_Attr
("invalid dimension number for array type", E1
);
1803 if (Style_Check
and Style_Check_Array_Attribute_Index
)
1804 and then Comes_From_Source
(N
)
1806 Style
.Check_Array_Attribute_Index
(N
, E1
, D
);
1808 end Check_Array_Type
;
1810 -------------------------
1811 -- Check_Asm_Attribute --
1812 -------------------------
1814 procedure Check_Asm_Attribute
is
1819 -- Check first argument is static string expression
1821 Analyze_And_Resolve
(E1
, Standard_String
);
1823 if Etype
(E1
) = Any_Type
then
1826 elsif not Is_OK_Static_Expression
(E1
) then
1827 Flag_Non_Static_Expr
1828 ("constraint argument must be static string expression!", E1
);
1832 -- Check second argument is right type
1834 Analyze_And_Resolve
(E2
, Entity
(P
));
1836 -- Note: that is all we need to do, we don't need to check
1837 -- that it appears in a correct context. The Ada type system
1838 -- will do that for us.
1840 end Check_Asm_Attribute
;
1842 ---------------------
1843 -- Check_Component --
1844 ---------------------
1846 procedure Check_Component
is
1850 if Nkind
(P
) /= N_Selected_Component
1852 (Ekind
(Entity
(Selector_Name
(P
))) /= E_Component
1854 Ekind
(Entity
(Selector_Name
(P
))) /= E_Discriminant
)
1856 Error_Attr_P
("prefix for % attribute must be selected component");
1858 end Check_Component
;
1860 ------------------------------------
1861 -- Check_Decimal_Fixed_Point_Type --
1862 ------------------------------------
1864 procedure Check_Decimal_Fixed_Point_Type
is
1868 if not Is_Decimal_Fixed_Point_Type
(P_Type
) then
1869 Error_Attr_P
("prefix of % attribute must be decimal type");
1871 end Check_Decimal_Fixed_Point_Type
;
1873 -----------------------
1874 -- Check_Dereference --
1875 -----------------------
1877 procedure Check_Dereference
is
1880 -- Case of a subtype mark
1882 if Is_Entity_Name
(P
) and then Is_Type
(Entity
(P
)) then
1886 -- Case of an expression
1890 if Is_Access_Type
(P_Type
) then
1892 -- If there is an implicit dereference, then we must freeze the
1893 -- designated type of the access type, since the type of the
1894 -- referenced array is this type (see AI95-00106).
1896 -- As done elsewhere, freezing must not happen when preanalyzing
1897 -- a pre- or postcondition or a default value for an object or for
1898 -- a formal parameter.
1900 if not In_Spec_Expression
then
1901 Freeze_Before
(N
, Designated_Type
(P_Type
));
1905 Make_Explicit_Dereference
(Sloc
(P_Old
),
1906 Prefix
=> Relocate_Node
(P_Old
)));
1908 Analyze_And_Resolve
(P_Old
);
1909 P_Type
:= Etype
(P_Old
);
1911 if P_Type
= Any_Type
then
1912 raise Bad_Attribute
;
1915 P_Base_Type
:= Base_Type
(P_Type
);
1917 end Check_Dereference
;
1919 -------------------------
1920 -- Check_Discrete_Type --
1921 -------------------------
1923 procedure Check_Discrete_Type
is
1927 if not Is_Discrete_Type
(P_Type
) then
1928 Error_Attr_P
("prefix of % attribute must be discrete type");
1930 end Check_Discrete_Type
;
1936 procedure Check_E0
is
1938 if Present
(E1
) then
1939 Unexpected_Argument
(E1
);
1947 procedure Check_E1
is
1949 Check_Either_E0_Or_E1
;
1953 -- Special-case attributes that are functions and that appear as
1954 -- the prefix of another attribute. Error is posted on parent.
1956 if Nkind
(Parent
(N
)) = N_Attribute_Reference
1957 and then Attribute_Name
(Parent
(N
)) in Name_Address
1961 Error_Msg_Name_1
:= Attribute_Name
(Parent
(N
));
1962 Error_Msg_N
("illegal prefix for % attribute", Parent
(N
));
1963 Set_Etype
(Parent
(N
), Any_Type
);
1964 Set_Entity
(Parent
(N
), Any_Type
);
1965 raise Bad_Attribute
;
1968 Error_Attr
("missing argument for % attribute", N
);
1977 procedure Check_E2
is
1980 Error_Attr
("missing arguments for % attribute (2 required)", N
);
1982 Error_Attr
("missing argument for % attribute (2 required)", N
);
1986 ---------------------------
1987 -- Check_Either_E0_Or_E1 --
1988 ---------------------------
1990 procedure Check_Either_E0_Or_E1
is
1992 if Present
(E2
) then
1993 Unexpected_Argument
(E2
);
1995 end Check_Either_E0_Or_E1
;
1997 ----------------------
1998 -- Check_Enum_Image --
1999 ----------------------
2001 procedure Check_Enum_Image
(Check_Enumeration_Maps
: Boolean := False) is
2005 -- Ensure that Check_Enumeration_Maps parameter is set precisely for
2006 -- attributes whose implementation requires enumeration maps.
2009 (Check_Enumeration_Maps
= (Attr_Id
in Attribute_Image
2011 | Attribute_Valid_Value
2013 | Attribute_Wide_Image
2014 | Attribute_Wide_Value
2015 | Attribute_Wide_Wide_Image
2016 | Attribute_Wide_Wide_Value
));
2018 -- When an enumeration type appears in an attribute reference, all
2019 -- literals of the type are marked as referenced. This must only be
2020 -- done if the attribute reference appears in the current source.
2021 -- Otherwise the information on references may differ between a
2022 -- normal compilation and one that performs inlining.
2024 if Is_Enumeration_Type
(P_Base_Type
)
2025 and then In_Extended_Main_Code_Unit
(N
)
2027 if Check_Enumeration_Maps
then
2028 Check_Restriction
(No_Enumeration_Maps
, N
);
2031 Lit
:= First_Literal
(P_Base_Type
);
2032 while Present
(Lit
) loop
2033 Set_Referenced
(Lit
);
2037 end Check_Enum_Image
;
2039 ----------------------------
2040 -- Check_First_Last_Valid --
2041 ----------------------------
2043 procedure Check_First_Last_Valid
is
2045 Check_Discrete_Type
;
2047 -- Freeze the subtype now, so that the following test for predicates
2048 -- works (we set the predicates stuff up at freeze time)
2050 Insert_Actions
(N
, Freeze_Entity
(P_Type
, P
));
2052 -- Now test for dynamic predicate
2054 if Has_Predicates
(P_Type
)
2055 and then not (Has_Static_Predicate
(P_Type
))
2058 ("prefix of % attribute may not have dynamic predicate");
2061 -- Check non-static subtype
2063 if not Is_OK_Static_Subtype
(P_Type
) then
2064 Error_Attr_P
("prefix of % attribute must be a static subtype");
2067 -- Test case for no values
2069 if Expr_Value
(Type_Low_Bound
(P_Type
)) >
2070 Expr_Value
(Type_High_Bound
(P_Type
))
2071 or else (Has_Predicates
(P_Type
)
2073 Is_Empty_List
(Static_Discrete_Predicate
(P_Type
)))
2076 ("prefix of % attribute must be subtype with at least one "
2079 end Check_First_Last_Valid
;
2081 ----------------------------
2082 -- Check_Fixed_Point_Type --
2083 ----------------------------
2085 procedure Check_Fixed_Point_Type
is
2089 if not Is_Fixed_Point_Type
(P_Type
) then
2090 Error_Attr_P
("prefix of % attribute must be fixed point type");
2092 end Check_Fixed_Point_Type
;
2094 ------------------------------
2095 -- Check_Fixed_Point_Type_0 --
2096 ------------------------------
2098 procedure Check_Fixed_Point_Type_0
is
2100 Check_Fixed_Point_Type
;
2102 end Check_Fixed_Point_Type_0
;
2104 -------------------------------
2105 -- Check_Floating_Point_Type --
2106 -------------------------------
2108 procedure Check_Floating_Point_Type
is
2112 if not Is_Floating_Point_Type
(P_Type
) then
2113 Error_Attr_P
("prefix of % attribute must be float type");
2115 end Check_Floating_Point_Type
;
2117 ---------------------------------
2118 -- Check_Floating_Point_Type_0 --
2119 ---------------------------------
2121 procedure Check_Floating_Point_Type_0
is
2123 Check_Floating_Point_Type
;
2125 end Check_Floating_Point_Type_0
;
2127 ---------------------------------
2128 -- Check_Floating_Point_Type_1 --
2129 ---------------------------------
2131 procedure Check_Floating_Point_Type_1
is
2133 Check_Floating_Point_Type
;
2135 end Check_Floating_Point_Type_1
;
2137 ---------------------------------
2138 -- Check_Floating_Point_Type_2 --
2139 ---------------------------------
2141 procedure Check_Floating_Point_Type_2
is
2143 Check_Floating_Point_Type
;
2145 end Check_Floating_Point_Type_2
;
2147 ------------------------
2148 -- Check_Integer_Type --
2149 ------------------------
2151 procedure Check_Integer_Type
is
2155 if not Is_Integer_Type
(P_Type
) then
2156 Error_Attr_P
("prefix of % attribute must be integer type");
2158 end Check_Integer_Type
;
2160 --------------------------------
2161 -- Check_Modular_Integer_Type --
2162 --------------------------------
2164 procedure Check_Modular_Integer_Type
is
2168 if not Is_Modular_Integer_Type
(P_Type
) then
2170 ("prefix of % attribute must be modular integer type");
2172 end Check_Modular_Integer_Type
;
2174 ------------------------
2175 -- Check_Not_CPP_Type --
2176 ------------------------
2178 procedure Check_Not_CPP_Type
is
2180 if Is_Tagged_Type
(Etype
(P
))
2181 and then Convention
(Etype
(P
)) = Convention_CPP
2182 and then Is_CPP_Class
(Root_Type
(Etype
(P
)))
2185 ("invalid use of % attribute with 'C'P'P tagged type");
2187 end Check_Not_CPP_Type
;
2189 -------------------------------
2190 -- Check_Not_Incomplete_Type --
2191 -------------------------------
2193 procedure Check_Not_Incomplete_Type
is
2198 -- Ada 2005 (AI-50217, AI-326): If the prefix is an explicit
2199 -- dereference we have to check wrong uses of incomplete types
2200 -- (other wrong uses are checked at their freezing point).
2202 -- In Ada 2012, incomplete types can appear in subprogram
2203 -- profiles, but formals with incomplete types cannot be the
2204 -- prefix of attributes.
2206 -- Example 1: Limited-with
2208 -- limited with Pkg;
2210 -- type Acc is access Pkg.T;
2212 -- S : Integer := X.all'Size; -- ERROR
2215 -- Example 2: Tagged incomplete
2217 -- type T is tagged;
2218 -- type Acc is access all T;
2220 -- S : constant Integer := X.all'Size; -- ERROR
2221 -- procedure Q (Obj : Integer := X.all'Alignment); -- ERROR
2223 if Ada_Version
>= Ada_2005
2224 and then Nkind
(P
) = N_Explicit_Dereference
2227 while Nkind
(E
) = N_Explicit_Dereference
loop
2233 if From_Limited_With
(Typ
) then
2235 ("prefix of % attribute cannot be an incomplete type");
2237 -- If the prefix is an access type check the designated type
2239 elsif Is_Access_Type
(Typ
)
2240 and then Nkind
(P
) = N_Explicit_Dereference
2242 Typ
:= Directly_Designated_Type
(Typ
);
2245 if Is_Class_Wide_Type
(Typ
) then
2246 Typ
:= Root_Type
(Typ
);
2249 -- A legal use of a shadow entity occurs only when the unit where
2250 -- the non-limited view resides is imported via a regular with
2251 -- clause in the current body. Such references to shadow entities
2252 -- may occur in subprogram formals.
2254 if Is_Incomplete_Type
(Typ
)
2255 and then From_Limited_With
(Typ
)
2256 and then Present
(Non_Limited_View
(Typ
))
2257 and then Is_Legal_Shadow_Entity_In_Body
(Typ
)
2259 Typ
:= Non_Limited_View
(Typ
);
2262 -- If still incomplete, it can be a local incomplete type, or a
2263 -- limited view whose scope is also a limited view.
2265 if Ekind
(Typ
) = E_Incomplete_Type
then
2266 if not From_Limited_With
(Typ
)
2267 and then No
(Full_View
(Typ
))
2270 ("prefix of % attribute cannot be an incomplete type");
2272 -- The limited view may be available indirectly through
2273 -- an intermediate unit. If the non-limited view is available
2274 -- the attribute reference is legal.
2276 elsif From_Limited_With
(Typ
)
2278 (No
(Non_Limited_View
(Typ
))
2279 or else Is_Incomplete_Type
(Non_Limited_View
(Typ
)))
2282 ("prefix of % attribute cannot be an incomplete type");
2286 -- Ada 2012 : formals in bodies may be incomplete, but no attribute
2289 elsif Is_Entity_Name
(P
)
2290 and then Is_Formal
(Entity
(P
))
2291 and then Is_Incomplete_Type
(Etype
(Etype
(P
)))
2294 ("prefix of % attribute cannot be an incomplete type");
2297 if not Is_Entity_Name
(P
)
2298 or else not Is_Type
(Entity
(P
))
2299 or else In_Spec_Expression
2303 Check_Fully_Declared
(P_Type
, P
);
2305 end Check_Not_Incomplete_Type
;
2307 ----------------------------
2308 -- Check_Object_Reference --
2309 ----------------------------
2311 procedure Check_Object_Reference
(P
: Node_Id
) is
2315 -- If we need an object, and we have a prefix that is the name of a
2316 -- function entity, convert it into a function call.
2318 if Is_Entity_Name
(P
)
2319 and then Ekind
(Entity
(P
)) = E_Function
2321 Rtyp
:= Etype
(Entity
(P
));
2324 Make_Function_Call
(Sloc
(P
),
2325 Name
=> Relocate_Node
(P
)));
2327 Analyze_And_Resolve
(P
, Rtyp
);
2329 -- Otherwise we must have an object reference
2331 elsif not Is_Object_Reference
(P
) then
2332 Error_Attr_P
("prefix of % attribute must be object");
2334 end Check_Object_Reference
;
2336 ----------------------------
2337 -- Check_PolyORB_Attribute --
2338 ----------------------------
2340 procedure Check_PolyORB_Attribute
is
2342 Validate_Non_Static_Attribute_Function_Call
;
2347 if Get_PCS_Name
/= Name_PolyORB_DSA
then
2349 ("attribute% requires the 'Poly'O'R'B 'P'C'S", N
);
2351 end Check_PolyORB_Attribute
;
2353 ------------------------
2354 -- Check_Program_Unit --
2355 ------------------------
2357 procedure Check_Program_Unit
is
2359 if Is_Entity_Name
(P
) then
2361 E
: constant Entity_Id
:= Entity
(P
);
2363 if Ekind
(E
) in E_Protected_Type
2369 or else Is_Single_Concurrent_Object
(E
)
2376 Error_Attr_P
("prefix of % attribute must be program unit");
2377 end Check_Program_Unit
;
2379 ---------------------
2380 -- Check_Real_Type --
2381 ---------------------
2383 procedure Check_Real_Type
is
2387 if not Is_Real_Type
(P_Type
) then
2388 Error_Attr_P
("prefix of % attribute must be real type");
2390 end Check_Real_Type
;
2392 ----------------------------
2393 -- Check_Enumeration_Type --
2394 ----------------------------
2396 procedure Check_Enumeration_Type
is
2400 if not Is_Enumeration_Type
(P_Type
) then
2401 Error_Attr_P
("prefix of % attribute must be enumeration type");
2403 end Check_Enumeration_Type
;
2405 -----------------------
2406 -- Check_Scalar_Type --
2407 -----------------------
2409 procedure Check_Scalar_Type
is
2413 if not Is_Scalar_Type
(P_Type
) then
2414 Error_Attr_P
("prefix of % attribute must be scalar type");
2416 end Check_Scalar_Type
;
2418 ---------------------------
2419 -- Check_Standard_Prefix --
2420 ---------------------------
2422 procedure Check_Standard_Prefix
is
2426 if Nkind
(P
) /= N_Identifier
or else Chars
(P
) /= Name_Standard
then
2427 Error_Attr
("only allowed prefix for % attribute is Standard", P
);
2429 end Check_Standard_Prefix
;
2431 -------------------------------
2432 -- Check_Put_Image_Attribute --
2433 -------------------------------
2435 procedure Check_Put_Image_Attribute
is
2437 -- Put_Image is a procedure, and can only appear at the position of a
2438 -- procedure call. If it's a list member and it's parent is a
2439 -- procedure call or aggregate, then this is appearing as an actual
2440 -- parameter or component association, which is wrong.
2442 if Is_List_Member
(N
)
2443 and then Nkind
(Parent
(N
)) not in
2444 N_Procedure_Call_Statement | N_Aggregate
2449 ("invalid context for attribute%, which is a procedure", N
);
2453 Analyze_And_Resolve
(E1
);
2455 -- Check that the first argument is
2456 -- Ada.Strings.Text_Buffers.Root_Buffer_Type'Class.
2458 -- Note: the double call to Root_Type here is needed because the
2459 -- root type of a class-wide type is the corresponding type (e.g.
2460 -- X for X'Class, and we really want to go to the root.)
2462 if not Is_RTE
(Root_Type
(Root_Type
(Etype
(E1
))),
2463 RE_Root_Buffer_Type
)
2466 ("expected Ada.Strings.Text_Buffers.Root_Buffer_Type''Class",
2470 -- Check that the second argument is of the right type
2473 Resolve
(E2
, P_Type
);
2474 end Check_Put_Image_Attribute
;
2476 ----------------------------
2477 -- Check_Stream_Attribute --
2478 ----------------------------
2480 procedure Check_Stream_Attribute
(Nam
: TSS_Name_Type
) is
2484 In_Shared_Var_Procs
: Boolean;
2485 -- True when compiling System.Shared_Storage.Shared_Var_Procs body.
2486 -- For this runtime package (always compiled in GNAT mode), we allow
2487 -- stream attributes references for limited types for the case where
2488 -- shared passive objects are implemented using stream attributes,
2489 -- which is the default in GNAT's persistent storage implementation.
2492 Validate_Non_Static_Attribute_Function_Call
;
2494 -- With the exception of 'Input, Stream attributes are procedures,
2495 -- and can only appear at the position of procedure calls. We check
2496 -- for this here, before they are rewritten, to give a more precise
2499 if Nam
= TSS_Stream_Input
then
2502 elsif Is_List_Member
(N
)
2503 and then Nkind
(Parent
(N
)) not in
2504 N_Procedure_Call_Statement | N_Aggregate
2510 ("invalid context for attribute%, which is a procedure", N
);
2514 Btyp
:= Implementation_Base_Type
(P_Type
);
2516 -- Stream attributes not allowed on limited types unless the
2517 -- attribute reference was generated by the expander (in which
2518 -- case the underlying type will be used, as described in Sinfo),
2519 -- or the attribute was specified explicitly for the type itself
2520 -- or one of its ancestors (taking visibility rules into account if
2521 -- in Ada 2005 mode), or a pragma Stream_Convert applies to Btyp
2522 -- (with no visibility restriction).
2525 Gen_Body
: constant Node_Id
:= Enclosing_Generic_Body
(N
);
2527 if Present
(Gen_Body
) then
2528 In_Shared_Var_Procs
:=
2529 Is_RTE
(Corresponding_Spec
(Gen_Body
), RE_Shared_Var_Procs
);
2531 In_Shared_Var_Procs
:= False;
2535 if (Comes_From_Source
(N
)
2536 and then not (In_Shared_Var_Procs
or In_Instance
))
2537 and then not Stream_Attribute_Available
(P_Type
, Nam
)
2538 and then not Has_Rep_Pragma
(Btyp
, Name_Stream_Convert
)
2540 Error_Msg_Name_1
:= Aname
;
2542 if Is_Limited_Type
(P_Type
) then
2544 ("limited type& has no% attribute", P
, P_Type
);
2545 Explain_Limited_Type
(P_Type
, P
);
2548 ("attribute% for type& is not available", P
, P_Type
);
2552 -- Check for no stream operations allowed from No_Tagged_Streams
2554 if Is_Tagged_Type
(P_Type
)
2555 and then Present
(No_Tagged_Streams_Pragma
(P_Type
))
2557 Error_Msg_Sloc
:= Sloc
(No_Tagged_Streams_Pragma
(P_Type
));
2559 ("no stream operations for & (No_Tagged_Streams #)", N
, P_Type
);
2563 -- Check restriction violations
2565 -- First check the No_Streams restriction, which prohibits the use
2566 -- of explicit stream attributes in the source program. We do not
2567 -- prevent the occurrence of stream attributes in generated code,
2568 -- for instance those generated implicitly for dispatching purposes.
2570 if Comes_From_Source
(N
) then
2571 Check_Restriction
(No_Streams
, P
);
2574 -- AI05-0057: if restriction No_Default_Stream_Attributes is active,
2575 -- it is illegal to use a predefined elementary type stream attribute
2576 -- either by itself, or more importantly as part of the attribute
2577 -- subprogram for a composite type. However, if the broader
2578 -- restriction No_Streams is active, stream operations are not
2579 -- generated, and there is no error.
2581 if Restriction_Active
(No_Default_Stream_Attributes
)
2582 and then not Restriction_Active
(No_Streams
)
2588 if Nam
= TSS_Stream_Input
2590 Nam
= TSS_Stream_Read
2593 Type_Without_Stream_Operation
(P_Type
, TSS_Stream_Read
);
2596 Type_Without_Stream_Operation
(P_Type
, TSS_Stream_Write
);
2600 Check_Restriction
(No_Default_Stream_Attributes
, N
);
2603 ("missing user-defined Stream Read or Write for type&",
2605 if not Is_Elementary_Type
(P_Type
) then
2607 ("\which is a component of type&", N
, P_Type
);
2613 -- Check special case of Exception_Id and Exception_Occurrence which
2614 -- are not allowed for restriction No_Exception_Registration.
2616 if Restriction_Check_Required
(No_Exception_Registration
)
2617 and then (Is_RTE
(P_Type
, RE_Exception_Id
)
2619 Is_RTE
(P_Type
, RE_Exception_Occurrence
))
2621 Check_Restriction
(No_Exception_Registration
, P
);
2624 -- Here we must check that the first argument is an access type
2625 -- that is compatible with Ada.Streams.Root_Stream_Type'Class.
2627 Analyze_And_Resolve
(E1
);
2630 -- Note: the double call to Root_Type here is needed because the
2631 -- root type of a class-wide type is the corresponding type (e.g.
2632 -- X for X'Class, and we really want to go to the root.)
2634 if not Is_Access_Type
(Etyp
)
2635 or else not Is_RTE
(Root_Type
(Root_Type
(Designated_Type
(Etyp
))),
2636 RE_Root_Stream_Type
)
2639 ("expected access to Ada.Streams.Root_Stream_Type''Class", E1
);
2642 -- Check that the second argument is of the right type if there is
2643 -- one (the Input attribute has only one argument so this is skipped)
2645 if Present
(E2
) then
2648 if Nam
= TSS_Stream_Read
2649 and then not Is_OK_Variable_For_Out_Formal
(E2
)
2652 ("second argument of % attribute must be a variable", E2
);
2655 Resolve
(E2
, P_Type
);
2659 end Check_Stream_Attribute
;
2661 -------------------------
2662 -- Check_System_Prefix --
2663 -------------------------
2665 procedure Check_System_Prefix
is
2667 if Nkind
(P
) /= N_Identifier
or else Chars
(P
) /= Name_System
then
2668 Error_Attr
("only allowed prefix for % attribute is System", P
);
2670 end Check_System_Prefix
;
2672 -----------------------
2673 -- Check_Task_Prefix --
2674 -----------------------
2676 procedure Check_Task_Prefix
is
2680 -- Ada 2005 (AI-345): Attribute 'Terminated can be applied to
2681 -- task interface class-wide types.
2683 if Is_Task_Type
(Etype
(P
))
2684 or else (Is_Access_Type
(Etype
(P
))
2685 and then Is_Task_Type
(Designated_Type
(Etype
(P
))))
2686 or else (Ada_Version
>= Ada_2005
2687 and then Ekind
(Etype
(P
)) = E_Class_Wide_Type
2688 and then Is_Interface
(Etype
(P
))
2689 and then Is_Task_Interface
(Etype
(P
)))
2694 if Ada_Version
>= Ada_2005
then
2696 ("prefix of % attribute must be a task or a task " &
2697 "interface class-wide object");
2700 Error_Attr_P
("prefix of % attribute must be a task");
2703 end Check_Task_Prefix
;
2709 -- The possibilities are an entity name denoting a type, or an
2710 -- attribute reference that denotes a type (Base or Class). If
2711 -- the type is incomplete, replace it with its full view.
2713 procedure Check_Type
is
2715 if not Is_Entity_Name
(P
)
2716 or else not Is_Type
(Entity
(P
))
2718 Error_Attr_P
("prefix of % attribute must be a type");
2720 elsif Is_Protected_Self_Reference
(P
) then
2722 ("prefix of % attribute denotes current instance "
2723 & "(RM 9.4(21/2))");
2725 elsif Ekind
(Entity
(P
)) = E_Incomplete_Type
2726 and then Present
(Full_View
(Entity
(P
)))
2728 P_Type
:= Full_View
(Entity
(P
));
2729 Set_Entity
(P
, P_Type
);
2733 ---------------------
2734 -- Check_Unit_Name --
2735 ---------------------
2737 procedure Check_Unit_Name
(Nod
: Node_Id
) is
2739 if Nkind
(Nod
) = N_Identifier
then
2742 elsif Nkind
(Nod
) in N_Selected_Component | N_Expanded_Name
then
2743 Check_Unit_Name
(Prefix
(Nod
));
2745 if Nkind
(Selector_Name
(Nod
)) = N_Identifier
then
2750 Error_Attr
("argument for % attribute must be unit name", P
);
2751 end Check_Unit_Name
;
2757 procedure Error_Attr
is
2759 Set_Etype
(N
, Any_Type
);
2760 Set_Entity
(N
, Any_Type
);
2761 raise Bad_Attribute
;
2764 procedure Error_Attr
(Msg
: String; Error_Node
: Node_Id
) is
2766 Error_Msg_Name_1
:= Aname
;
2767 Error_Msg_N
(Msg
, Error_Node
);
2775 procedure Error_Attr_P
(Msg
: String; Msg_Cont
: String := "") is
2777 Error_Msg_Name_1
:= Aname
;
2778 Error_Msg_F
(Msg
, P
);
2779 if Msg_Cont
/= "" then
2780 Error_Msg_F
(Msg_Cont
, P
);
2785 ----------------------------
2786 -- Legal_Formal_Attribute --
2787 ----------------------------
2789 procedure Legal_Formal_Attribute
is
2793 if not Is_Entity_Name
(P
)
2794 or else not Is_Type
(Entity
(P
))
2796 Error_Attr_P
("prefix of % attribute must be generic type");
2798 elsif Is_Generic_Actual_Type
(Entity
(P
))
2800 or else In_Inlined_Body
2804 elsif Is_Generic_Type
(Entity
(P
)) then
2805 if Is_Definite_Subtype
(Entity
(P
)) then
2807 ("prefix of % attribute must be indefinite generic type");
2812 ("prefix of % attribute must be indefinite generic type");
2815 Set_Etype
(N
, Standard_Boolean
);
2816 end Legal_Formal_Attribute
;
2818 ---------------------------------------------------------------
2819 -- Max_Alignment_For_Allocation_Max_Size_In_Storage_Elements --
2820 ---------------------------------------------------------------
2822 procedure Max_Alignment_For_Allocation_Max_Size_In_Storage_Elements
is
2826 Check_Not_Incomplete_Type
;
2827 Set_Etype
(N
, Universal_Integer
);
2828 end Max_Alignment_For_Allocation_Max_Size_In_Storage_Elements
;
2834 procedure Min_Max
is
2836 -- Attribute can appear as function name in a reduction.
2837 -- Semantic checks are performed later.
2839 if Nkind
(Parent
(N
)) = N_Attribute_Reference
2840 and then Attribute_Name
(Parent
(N
)) = Name_Reduce
2842 Set_Etype
(N
, P_Base_Type
);
2848 Resolve
(E1
, P_Base_Type
);
2849 Resolve
(E2
, P_Base_Type
);
2850 Set_Etype
(N
, P_Base_Type
);
2852 -- Check for comparison on unordered enumeration type
2854 if Bad_Unordered_Enumeration_Reference
(N
, P_Base_Type
) then
2855 Error_Msg_Sloc
:= Sloc
(P_Base_Type
);
2857 ("comparison on unordered enumeration type& declared#?U?",
2862 ------------------------
2863 -- Standard_Attribute --
2864 ------------------------
2866 procedure Standard_Attribute
(Val
: Int
) is
2868 Check_Standard_Prefix
;
2869 Rewrite
(N
, Make_Integer_Literal
(Loc
, Val
));
2871 Set_Is_Static_Expression
(N
, True);
2872 end Standard_Attribute
;
2874 --------------------
2875 -- Uneval_Old_Msg --
2876 --------------------
2878 procedure Uneval_Old_Msg
is
2879 Uneval_Old_Setting
: Character;
2883 -- If from aspect, then Uneval_Old_Setting comes from flags in the
2884 -- N_Aspect_Specification node that corresponds to the attribute.
2886 -- First find the pragma in which we appear (note that at this stage,
2887 -- even if we appeared originally within an aspect specification, we
2888 -- are now within the corresponding pragma).
2892 Prag
:= Parent
(Prag
);
2893 exit when No
(Prag
) or else Nkind
(Prag
) = N_Pragma
;
2896 if Present
(Prag
) then
2897 if Uneval_Old_Accept
(Prag
) then
2898 Uneval_Old_Setting
:= 'A';
2899 elsif Uneval_Old_Warn
(Prag
) then
2900 Uneval_Old_Setting
:= 'W';
2902 Uneval_Old_Setting
:= 'E';
2905 -- If we did not find the pragma, that's odd, just use the setting
2906 -- from Opt.Uneval_Old. Perhaps this is due to a previous error?
2909 Uneval_Old_Setting
:= Opt
.Uneval_Old
;
2912 -- Processing depends on the setting of Uneval_Old
2914 case Uneval_Old_Setting
is
2916 -- ??? In the case where Ada_Version is < Ada_2022 and
2917 -- an illegal 'Old prefix would be legal in Ada_2022,
2918 -- we'd like to call Error_Msg_Ada_2022_Feature.
2919 -- Identifying that case involves some work.
2922 ("prefix of attribute % that is potentially "
2923 & "unevaluated must statically name an entity"
2925 -- further text needed for accuracy if Ada_2022
2926 & (if Ada_Version
>= Ada_2022
2927 and then Attr_Id
= Attribute_Old
2928 then " or be eligible for conditional evaluation"
2929 & " (RM 6.1.1 (27))"
2932 "\using pragma Unevaluated_Use_Of_Old (Allow) will make "
2936 Error_Msg_Name_1
:= Aname
;
2938 ("??prefix of attribute % appears in potentially "
2939 & "unevaluated context, exception may be raised", P
);
2945 raise Program_Error
;
2949 -------------------------
2950 -- Unexpected Argument --
2951 -------------------------
2953 procedure Unexpected_Argument
(En
: Node_Id
) is
2955 Error_Attr
("unexpected argument for % attribute", En
);
2956 end Unexpected_Argument
;
2958 -------------------------------------------------
2959 -- Validate_Non_Static_Attribute_Function_Call --
2960 -------------------------------------------------
2962 -- This function should be moved to Sem_Dist ???
2964 procedure Validate_Non_Static_Attribute_Function_Call
is
2966 if In_Preelaborated_Unit
2967 and then not In_Subprogram_Or_Concurrent_Unit
2969 Flag_Non_Static_Expr
2970 ("non-static function call in preelaborated unit!", N
);
2972 end Validate_Non_Static_Attribute_Function_Call
;
2974 -- Start of processing for Analyze_Attribute
2977 -- Immediate return if unrecognized attribute (already diagnosed by
2978 -- parser, so there is nothing more that we need to do).
2980 if not Is_Attribute_Name
(Aname
) then
2981 raise Bad_Attribute
;
2984 Check_Restriction_No_Use_Of_Attribute
(N
);
2986 -- Deal with Ada 83 issues
2988 if Comes_From_Source
(N
) then
2989 if not Attribute_83
(Attr_Id
) then
2990 if Ada_Version
= Ada_83
and then Comes_From_Source
(N
) then
2991 Error_Msg_Name_1
:= Aname
;
2992 Error_Msg_N
("(Ada 83) attribute% is not standard??", N
);
2995 if Attribute_Impl_Def
(Attr_Id
) then
2996 Check_Restriction
(No_Implementation_Attributes
, N
);
3001 -- Deal with Ada 2005 attributes that are implementation attributes
3002 -- because they appear in a version of Ada before Ada 2005, ditto for
3003 -- Ada 2012 and Ada 2022 attributes appearing in an earlier version.
3005 if (Attribute_05
(Attr_Id
) and then Ada_Version
< Ada_2005
)
3007 (Attribute_12
(Attr_Id
) and then Ada_Version
< Ada_2012
)
3009 (Attribute_22
(Attr_Id
) and then Ada_Version
< Ada_2022
)
3011 Check_Restriction
(No_Implementation_Attributes
, N
);
3014 -- Remote access to subprogram type access attribute reference needs
3015 -- unanalyzed copy for tree transformation. The analyzed copy is used
3016 -- for its semantic information (whether prefix is a remote subprogram
3017 -- name), the unanalyzed copy is used to construct new subtree rooted
3018 -- with N_Aggregate which represents a fat pointer aggregate.
3020 if Aname
= Name_Access
then
3021 Discard_Node
(Copy_Separate_Tree
(N
));
3024 -- Analyze prefix and exit if error in analysis. If the prefix is an
3025 -- incomplete type, use full view if available. Note that there are
3026 -- some attributes for which we do not analyze the prefix, since the
3027 -- prefix is not a normal name, or else needs special handling.
3029 if Aname
/= Name_Elab_Body
and then
3030 Aname
/= Name_Elab_Spec
and then
3031 Aname
/= Name_Elab_Subp_Body
and then
3032 Aname
/= Name_Enabled
and then
3036 P_Type
:= Etype
(P
);
3038 if Is_Entity_Name
(P
)
3039 and then Present
(Entity
(P
))
3040 and then Is_Type
(Entity
(P
))
3042 if Ekind
(Entity
(P
)) = E_Incomplete_Type
then
3043 P_Type
:= Get_Full_View
(P_Type
);
3044 Set_Entity
(P
, P_Type
);
3045 Set_Etype
(P
, P_Type
);
3047 elsif Entity
(P
) = Current_Scope
3048 and then Is_Record_Type
(Entity
(P
))
3050 -- Use of current instance within the type. Verify that if the
3051 -- attribute appears within a constraint, it yields an access
3052 -- type, other uses are illegal.
3060 and then Nkind
(Parent
(Par
)) /= N_Component_Definition
3062 Par
:= Parent
(Par
);
3066 and then Nkind
(Par
) = N_Subtype_Indication
3068 if Attr_Id
/= Attribute_Access
3069 and then Attr_Id
/= Attribute_Unchecked_Access
3070 and then Attr_Id
/= Attribute_Unrestricted_Access
3073 ("in a constraint the current instance can only "
3074 & "be used with an access attribute", N
);
3081 if P_Type
= Any_Type
then
3082 raise Bad_Attribute
;
3085 P_Base_Type
:= Base_Type
(P_Type
);
3088 -- Analyze expressions that may be present, exiting if an error occurs
3095 E1
:= First
(Exprs
);
3097 -- Skip analysis for case of Restriction_Set, we do not expect
3098 -- the argument to be analyzed in this case.
3100 if Aname
/= Name_Restriction_Set
then
3103 -- Check for missing/bad expression (result of previous error)
3105 if No
(E1
) or else Etype
(E1
) = Any_Type
then
3106 raise Bad_Attribute
;
3112 if Present
(E2
) then
3115 if Etype
(E2
) = Any_Type
then
3116 raise Bad_Attribute
;
3119 if Present
(Next
(E2
)) then
3120 Unexpected_Argument
(Next
(E2
));
3125 -- Cases where prefix must be resolvable by itself
3127 if Is_Overloaded
(P
)
3128 and then Aname
/= Name_Access
3129 and then Aname
/= Name_Address
3130 and then Aname
/= Name_Code_Address
3131 and then Aname
/= Name_Result
3132 and then Aname
/= Name_Unchecked_Access
3134 -- The prefix must be resolvable by itself, without reference to the
3135 -- attribute. One case that requires special handling is a prefix
3136 -- that is a function name, where one interpretation may be a
3137 -- parameterless call. Entry attributes are handled specially below.
3139 if Is_Entity_Name
(P
)
3140 and then Aname
not in Name_Count | Name_Caller
3142 Check_Parameterless_Call
(P
);
3145 if Is_Overloaded
(P
) then
3147 -- Ada 2005 (AI-345): Since protected and task types have
3148 -- primitive entry wrappers, the attributes Count, and Caller
3149 -- require a context check
3151 if Aname
in Name_Count | Name_Caller
then
3153 Count
: Natural := 0;
3158 Get_First_Interp
(P
, I
, It
);
3159 while Present
(It
.Nam
) loop
3160 if Comes_From_Source
(It
.Nam
) then
3166 Get_Next_Interp
(I
, It
);
3170 Error_Attr
("ambiguous prefix for % attribute", P
);
3172 Set_Is_Overloaded
(P
, False);
3177 Error_Attr
("ambiguous prefix for % attribute", P
);
3182 -- If the prefix was rewritten as a raise node, then rewrite N as a
3183 -- raise node, to avoid creating inconsistent trees. We still need to
3184 -- perform legality checks on the original tree.
3186 if Nkind
(P
) in N_Raise_xxx_Error
then
3187 Rewrite
(N
, Relocate_Node
(P
));
3188 P
:= Original_Node
(P_Old
);
3191 -- Remaining processing depends on attribute
3195 -- Attributes related to Ada 2012 iterators. Attribute specifications
3196 -- exist for these, but they cannot be queried.
3198 when Attribute_Constant_Indexing
3199 | Attribute_Default_Iterator
3200 | Attribute_Implicit_Dereference
3201 | Attribute_Iterator_Element
3202 | Attribute_Iterable
3203 | Attribute_Variable_Indexing
3205 Error_Msg_N
("illegal attribute", N
);
3207 -- Internal attributes used to deal with Ada 2012 delayed aspects. These
3208 -- were already rejected by the parser. Thus they shouldn't appear here.
3210 when Internal_Attribute_Id
=>
3211 raise Program_Error
;
3217 when Attribute_Abort_Signal
=>
3218 Check_Standard_Prefix
;
3219 Rewrite
(N
, New_Occurrence_Of
(Stand
.Abort_Signal
, Loc
));
3226 when Attribute_Access
=>
3227 Analyze_Access_Attribute
;
3228 Check_Not_Incomplete_Type
;
3234 when Attribute_Address
=>
3237 Check_Not_Incomplete_Type
;
3238 Set_Etype
(N
, RTE
(RE_Address
));
3244 when Attribute_Address_Size
=>
3245 Standard_Attribute
(System_Address_Size
);
3251 when Attribute_Adjacent
3252 | Attribute_Copy_Sign
3253 | Attribute_Remainder
3255 Check_Floating_Point_Type_2
;
3256 Set_Etype
(N
, P_Base_Type
);
3257 Resolve
(E1
, P_Base_Type
);
3258 Resolve
(E2
, P_Base_Type
);
3264 when Attribute_Aft
=>
3265 Check_Fixed_Point_Type_0
;
3266 Set_Etype
(N
, Universal_Integer
);
3272 when Attribute_Alignment
=>
3274 -- Don't we need more checking here, cf Size ???
3277 Check_Not_Incomplete_Type
;
3279 Set_Etype
(N
, Universal_Integer
);
3285 when Attribute_Asm_Input
=>
3286 Check_Asm_Attribute
;
3288 -- The back end may need to take the address of E2
3290 if Is_Entity_Name
(E2
) then
3291 Set_Address_Taken
(Entity
(E2
));
3294 Set_Etype
(N
, RTE
(RE_Asm_Input_Operand
));
3300 when Attribute_Asm_Output
=>
3301 Check_Asm_Attribute
;
3303 if Etype
(E2
) = Any_Type
then
3306 elsif Aname
= Name_Asm_Output
then
3307 if not Is_Variable
(E2
) then
3309 ("second argument for Asm_Output is not variable", E2
);
3313 Note_Possible_Modification
(E2
, Sure
=> True);
3315 -- The back end may need to take the address of E2
3317 if Is_Entity_Name
(E2
) then
3318 Set_Address_Taken
(Entity
(E2
));
3321 Set_Etype
(N
, RTE
(RE_Asm_Output_Operand
));
3323 -----------------------------
3324 -- Atomic_Always_Lock_Free --
3325 -----------------------------
3327 when Attribute_Atomic_Always_Lock_Free
=>
3330 Set_Etype
(N
, Standard_Boolean
);
3336 -- Note: when the base attribute appears in the context of a subtype
3337 -- mark, the analysis is done by Sem_Ch8.Find_Type, rather than by
3338 -- the following circuit.
3340 when Attribute_Base
=> Base
: declare
3348 if Ada_Version
>= Ada_95
3349 and then not Is_Scalar_Type
(Typ
)
3350 and then not Is_Generic_Type
(Typ
)
3352 Error_Attr_P
("prefix of Base attribute must be scalar type");
3354 elsif Sloc
(Typ
) = Standard_Location
3355 and then Base_Type
(Typ
) = Typ
3356 and then Warn_On_Redundant_Constructs
3358 Error_Msg_NE
-- CODEFIX
3359 ("?r?redundant attribute, & is its own base type", N
, Typ
);
3362 Set_Etype
(N
, Base_Type
(Entity
(P
)));
3363 Set_Entity
(N
, Base_Type
(Entity
(P
)));
3364 Rewrite
(N
, New_Occurrence_Of
(Entity
(N
), Loc
));
3372 when Attribute_Bit
=>
3375 if not Is_Object_Reference
(P
) then
3376 Error_Attr_P
("prefix of % attribute must be object");
3378 -- What about the access object cases ???
3384 Set_Etype
(N
, Universal_Integer
);
3390 when Attribute_Bit_Order
=>
3394 if not Is_Record_Type
(P_Type
) then
3395 Error_Attr_P
("prefix of % attribute must be record type");
3398 if Bytes_Big_Endian
xor Reverse_Bit_Order
(P_Type
) then
3400 New_Occurrence_Of
(RTE
(RE_High_Order_First
), Loc
));
3403 New_Occurrence_Of
(RTE
(RE_Low_Order_First
), Loc
));
3406 Set_Etype
(N
, RTE
(RE_Bit_Order
));
3409 -- Reset incorrect indication of staticness
3411 Set_Is_Static_Expression
(N
, False);
3417 -- Note: in generated code, we can have a Bit_Position attribute
3418 -- applied to a (naked) record component (i.e. the prefix is an
3419 -- identifier that references an E_Component or E_Discriminant
3420 -- entity directly, and this is interpreted as expected by Gigi.
3421 -- The following code will not tolerate such usage, but when the
3422 -- expander creates this special case, it marks it as analyzed
3423 -- immediately and sets an appropriate type.
3425 when Attribute_Bit_Position
=>
3426 if Comes_From_Source
(N
) then
3430 Set_Etype
(N
, Universal_Integer
);
3436 when Attribute_Body_Version
=>
3439 Set_Etype
(N
, RTE
(RE_Version_String
));
3445 when Attribute_Callable
3446 | Attribute_Terminated
3449 Set_Etype
(N
, Standard_Boolean
);
3456 when Attribute_Caller
=> Caller
: declare
3463 if Nkind
(P
) in N_Identifier | N_Expanded_Name
then
3466 if not Is_Entry
(Ent
) then
3467 Error_Attr
("invalid entry name", N
);
3471 Error_Attr
("invalid entry name", N
);
3475 for J
in reverse 0 .. Scope_Stack
.Last
loop
3476 S
:= Scope_Stack
.Table
(J
).Entity
;
3478 if S
= Scope
(Ent
) then
3479 Error_Attr
("Caller must appear in matching accept or body", N
);
3485 Set_Etype
(N
, RTE
(RO_AT_Task_Id
));
3492 when Attribute_Ceiling
3494 | Attribute_Fraction
3496 | Attribute_Machine_Rounding
3498 | Attribute_Rounding
3499 | Attribute_Truncation
3500 | Attribute_Unbiased_Rounding
3502 Check_Floating_Point_Type_1
;
3503 Set_Etype
(N
, P_Base_Type
);
3504 Resolve
(E1
, P_Base_Type
);
3510 when Attribute_Class
=>
3511 Check_Restriction
(No_Dispatch
, N
);
3515 -- Applying Class to untagged incomplete type is obsolescent in Ada
3516 -- 2005. Note that we can't test Is_Tagged_Type here on P_Type, since
3517 -- this flag gets set by Find_Type in this situation.
3519 if Restriction_Check_Required
(No_Obsolescent_Features
)
3520 and then Ada_Version
>= Ada_2005
3521 and then Ekind
(P_Type
) = E_Incomplete_Type
3524 DN
: constant Node_Id
:= Declaration_Node
(P_Type
);
3526 if Nkind
(DN
) = N_Incomplete_Type_Declaration
3527 and then not Tagged_Present
(DN
)
3529 Check_Restriction
(No_Obsolescent_Features
, P
);
3538 when Attribute_Code_Address
=>
3541 if Nkind
(P
) = N_Attribute_Reference
3542 and then Attribute_Name
(P
) in Name_Elab_Body | Name_Elab_Spec
3546 elsif not Is_Entity_Name
(P
)
3547 or else (Ekind
(Entity
(P
)) /= E_Function
3549 Ekind
(Entity
(P
)) /= E_Procedure
)
3551 Error_Attr
("invalid prefix for % attribute", P
);
3552 Set_Address_Taken
(Entity
(P
));
3554 -- Issue an error if the prefix denotes an eliminated subprogram
3557 Check_For_Eliminated_Subprogram
(P
, Entity
(P
));
3560 Set_Etype
(N
, RTE
(RE_Address
));
3562 ----------------------
3563 -- Compiler_Version --
3564 ----------------------
3566 when Attribute_Compiler_Version
=>
3568 Check_Standard_Prefix
;
3569 Rewrite
(N
, Make_String_Literal
(Loc
, "GNAT " & Gnat_Version_String
));
3570 Analyze_And_Resolve
(N
, Standard_String
);
3571 Set_Is_Static_Expression
(N
, True);
3573 --------------------
3574 -- Component_Size --
3575 --------------------
3577 when Attribute_Component_Size
=>
3579 Set_Etype
(N
, Universal_Integer
);
3581 -- Note: unlike other array attributes, unconstrained arrays are OK
3583 if Is_Array_Type
(P_Type
) and then not Is_Constrained
(P_Type
) then
3593 when Attribute_Compose
3594 | Attribute_Leading_Part
3597 Check_Floating_Point_Type_2
;
3598 Set_Etype
(N
, P_Base_Type
);
3599 Resolve
(E1
, P_Base_Type
);
3600 Resolve
(E2
, Any_Integer
);
3606 when Attribute_Constrained
=>
3608 Set_Etype
(N
, Standard_Boolean
);
3610 -- Case from RM J.4(2) of constrained applied to private type
3612 if Is_Entity_Name
(P
) and then Is_Type
(Entity
(P
)) then
3613 Check_Restriction
(No_Obsolescent_Features
, P
);
3615 if Warn_On_Obsolescent_Feature
then
3617 ("constrained for private type is an obsolescent feature "
3618 & "(RM J.4)?j?", N
);
3621 -- If we are within an instance, the attribute must be legal
3622 -- because it was valid in the generic unit. Ditto if this is
3623 -- an inlining of a function declared in an instance.
3625 if In_Instance
or else In_Inlined_Body
then
3628 -- For sure OK if we have a real private type itself, but must
3629 -- be completed, cannot apply Constrained to incomplete type.
3631 elsif Is_Private_Type
(Entity
(P
)) then
3633 -- Note: this is one of the Annex J features that does not
3634 -- generate a warning from -gnatwj, since in fact it seems
3635 -- very useful, and is used in the GNAT runtime.
3637 Check_Not_Incomplete_Type
;
3641 -- Normal (non-obsolescent case) of application to object or value of
3642 -- a discriminated type.
3645 -- AI12-0068: In a type or subtype aspect, a prefix denoting the
3646 -- current instance of the (sub)type is defined to be a value,
3647 -- not an object, so the Constrained attribute is always True
3648 -- (see RM 8.6(18/5) and RM 3.7.2(3/5)). We issue a warning about
3649 -- this unintuitive result, to help avoid confusion.
3651 if Is_Current_Instance_Reference_In_Type_Aspect
(P
) then
3652 Error_Msg_Name_1
:= Aname
;
3654 ("current instance attribute % in subtype aspect always " &
3658 Check_Object_Reference
(P
);
3661 -- If N does not come from source, then we allow the
3662 -- the attribute prefix to be of a private type whose
3663 -- full type has discriminants. This occurs in cases
3664 -- involving expanded calls to stream attributes.
3666 if not Comes_From_Source
(N
) then
3667 P_Type
:= Underlying_Type
(P_Type
);
3670 -- Must have discriminants or be an access type designating a type
3671 -- with discriminants. If it is a class-wide type it has unknown
3674 if Has_Discriminants
(P_Type
)
3675 or else Has_Unknown_Discriminants
(P_Type
)
3677 (Is_Access_Type
(P_Type
)
3678 and then Has_Discriminants
(Designated_Type
(P_Type
)))
3682 -- The rule given in 3.7.2 is part of static semantics, but the
3683 -- intent is clearly that it be treated as a legality rule, and
3684 -- rechecked in the visible part of an instance. Nevertheless
3685 -- the intent also seems to be it should legally apply to the
3686 -- actual of a formal with unknown discriminants, regardless of
3687 -- whether the actual has discriminants, in which case the value
3688 -- of the attribute is determined using the J.4 rules. This choice
3689 -- seems the most useful, and is compatible with existing tests.
3691 elsif In_Instance
then
3694 -- Also allow an object of a generic type if extensions allowed
3695 -- and allow this for any type at all.
3697 elsif (Is_Generic_Type
(P_Type
)
3698 or else Is_Generic_Actual_Type
(P_Type
))
3699 and then Extensions_Allowed
3705 -- Fall through if bad prefix
3708 ("prefix of % attribute must be object of discriminated type");
3714 -- Shares processing with Adjacent attribute
3720 when Attribute_Count
=> Count
: declare
3728 if Nkind
(P
) in N_Identifier | N_Expanded_Name
then
3731 if Ekind
(Ent
) /= E_Entry
then
3732 Error_Attr
("invalid entry name", N
);
3735 elsif Nkind
(P
) = N_Indexed_Component
then
3736 if not Is_Entity_Name
(Prefix
(P
))
3737 or else No
(Entity
(Prefix
(P
)))
3738 or else Ekind
(Entity
(Prefix
(P
))) /= E_Entry_Family
3740 if Nkind
(Prefix
(P
)) = N_Selected_Component
3741 and then Present
(Entity
(Selector_Name
(Prefix
(P
))))
3742 and then Ekind
(Entity
(Selector_Name
(Prefix
(P
)))) =
3746 ("attribute % must apply to entry of current task", P
);
3749 Error_Attr
("invalid entry family name", P
);
3754 Ent
:= Entity
(Prefix
(P
));
3757 elsif Nkind
(P
) = N_Selected_Component
3758 and then Present
(Entity
(Selector_Name
(P
)))
3759 and then Ekind
(Entity
(Selector_Name
(P
))) = E_Entry
3762 ("attribute % must apply to entry of current task", P
);
3765 Error_Attr
("invalid entry name", N
);
3769 for J
in reverse 0 .. Scope_Stack
.Last
loop
3770 S
:= Scope_Stack
.Table
(J
).Entity
;
3772 if S
= Scope
(Ent
) then
3773 if Nkind
(P
) = N_Expanded_Name
then
3774 Tsk
:= Entity
(Prefix
(P
));
3776 -- The prefix denotes either the task type, or else a
3777 -- single task whose task type is being analyzed.
3779 if (Is_Type
(Tsk
) and then Tsk
= S
)
3780 or else (not Is_Type
(Tsk
)
3781 and then Etype
(Tsk
) = S
3782 and then not (Comes_From_Source
(S
)))
3787 ("attribute % must apply to entry of current task", N
);
3793 elsif Ekind
(Scope
(Ent
)) in Task_Kind
3794 and then Ekind
(S
) not in E_Block
3799 Error_Attr
("attribute % cannot appear in inner unit", N
);
3801 elsif Ekind
(Scope
(Ent
)) = E_Protected_Type
3802 and then not Has_Completion
(Scope
(Ent
))
3804 Error_Attr
("attribute % can only be used inside body", N
);
3808 if Is_Overloaded
(P
) then
3810 Index
: Interp_Index
;
3814 Get_First_Interp
(P
, Index
, It
);
3815 while Present
(It
.Nam
) loop
3816 if It
.Nam
= Ent
then
3819 -- Ada 2005 (AI-345): Do not consider primitive entry
3820 -- wrappers generated for task or protected types.
3822 elsif Ada_Version
>= Ada_2005
3823 and then not Comes_From_Source
(It
.Nam
)
3828 Error_Attr
("ambiguous entry name", N
);
3831 Get_Next_Interp
(Index
, It
);
3836 Set_Etype
(N
, Universal_Integer
);
3839 -----------------------
3840 -- Default_Bit_Order --
3841 -----------------------
3843 when Attribute_Default_Bit_Order
=> Default_Bit_Order
: declare
3844 Target_Default_Bit_Order
: System
.Bit_Order
;
3847 Check_Standard_Prefix
;
3849 if Bytes_Big_Endian
then
3850 Target_Default_Bit_Order
:= System
.High_Order_First
;
3852 Target_Default_Bit_Order
:= System
.Low_Order_First
;
3856 Make_Integer_Literal
(Loc
,
3857 UI_From_Int
(System
.Bit_Order
'Pos (Target_Default_Bit_Order
))));
3859 Set_Etype
(N
, Universal_Integer
);
3860 Set_Is_Static_Expression
(N
);
3861 end Default_Bit_Order
;
3863 ----------------------------------
3864 -- Default_Scalar_Storage_Order --
3865 ----------------------------------
3867 when Attribute_Default_Scalar_Storage_Order
=> Default_SSO
: declare
3868 RE_Default_SSO
: RE_Id
;
3871 Check_Standard_Prefix
;
3873 case Opt
.Default_SSO
is
3875 if Bytes_Big_Endian
then
3876 RE_Default_SSO
:= RE_High_Order_First
;
3878 RE_Default_SSO
:= RE_Low_Order_First
;
3882 RE_Default_SSO
:= RE_High_Order_First
;
3885 RE_Default_SSO
:= RE_Low_Order_First
;
3888 raise Program_Error
;
3891 Rewrite
(N
, New_Occurrence_Of
(RTE
(RE_Default_SSO
), Loc
));
3898 when Attribute_Definite
=>
3899 Legal_Formal_Attribute
;
3905 when Attribute_Delta
=>
3906 Check_Fixed_Point_Type_0
;
3907 Set_Etype
(N
, Universal_Real
);
3913 when Attribute_Denorm
3914 | Attribute_Signed_Zeros
3916 Check_Floating_Point_Type_0
;
3917 Set_Etype
(N
, Standard_Boolean
);
3923 when Attribute_Deref
=>
3926 Resolve
(E1
, RTE
(RE_Address
));
3927 Set_Etype
(N
, P_Type
);
3929 ---------------------
3930 -- Descriptor_Size --
3931 ---------------------
3933 when Attribute_Descriptor_Size
=>
3936 if not Is_Entity_Name
(P
) or else not Is_Type
(Entity
(P
)) then
3937 Error_Attr_P
("prefix of attribute % must denote a type");
3940 Set_Etype
(N
, Universal_Integer
);
3946 when Attribute_Digits
=>
3950 if not Is_Floating_Point_Type
(P_Type
)
3951 and then not Is_Decimal_Fixed_Point_Type
(P_Type
)
3954 ("prefix of % attribute must be float or decimal type");
3957 Set_Etype
(N
, Universal_Integer
);
3963 -- Also handles processing for Elab_Spec and Elab_Subp_Body
3965 when Attribute_Elab_Body
3966 | Attribute_Elab_Spec
3967 | Attribute_Elab_Subp_Body
3970 Check_Unit_Name
(P
);
3971 Set_Etype
(N
, Standard_Void_Type
);
3973 -- We have to manually call the expander in this case to get
3974 -- the necessary expansion (normally attributes that return
3975 -- entities are not expanded).
3983 -- Shares processing with Elab_Body attribute
3989 when Attribute_Elaborated
=>
3991 Check_Unit_Name
(P
);
3992 Set_Etype
(N
, Standard_Boolean
);
3999 | Attribute_Machine_Emax
4000 | Attribute_Machine_Emin
4001 | Attribute_Machine_Mantissa
4002 | Attribute_Model_Emin
4003 | Attribute_Model_Mantissa
4004 | Attribute_Safe_Emax
4006 Check_Floating_Point_Type_0
;
4007 Set_Etype
(N
, Universal_Integer
);
4013 when Attribute_Enabled
=>
4014 Check_Either_E0_Or_E1
;
4016 if Present
(E1
) then
4017 if not Is_Entity_Name
(E1
) or else No
(Entity
(E1
)) then
4018 Error_Msg_N
("entity name expected for Enabled attribute", E1
);
4023 if Nkind
(P
) /= N_Identifier
then
4024 Error_Msg_N
("identifier expected (check name)", P
);
4025 elsif Get_Check_Id
(Chars
(P
)) = No_Check_Id
then
4026 Error_Msg_N
("& is not a recognized check name", P
);
4029 Set_Etype
(N
, Standard_Boolean
);
4035 when Attribute_Enum_Rep
=>
4037 -- T'Enum_Rep (X) case
4039 if Present
(E1
) then
4041 Check_Discrete_Type
;
4042 Resolve
(E1
, P_Base_Type
);
4044 -- X'Enum_Rep case. X must be an object or enumeration literal
4045 -- (including an attribute reference), and it must be of a
4049 ((Is_Object_Reference
(P
)
4052 and then Ekind
(Entity
(P
)) = E_Enumeration_Literal
)
4053 or else Nkind
(P
) = N_Attribute_Reference
)
4054 and then Is_Discrete_Type
(Etype
(P
)))
4056 Error_Attr_P
("prefix of % attribute must be discrete object");
4059 Set_Etype
(N
, Universal_Integer
);
4065 when Attribute_Enum_Val
=>
4069 if not Is_Enumeration_Type
(P_Type
) then
4070 Error_Attr_P
("prefix of % attribute must be enumeration type");
4073 -- If the enumeration type has a standard representation, the effect
4074 -- is the same as 'Val, so rewrite the attribute as a 'Val.
4076 if not Has_Non_Standard_Rep
(P_Base_Type
) then
4078 Make_Attribute_Reference
(Loc
,
4079 Prefix
=> Relocate_Node
(Prefix
(N
)),
4080 Attribute_Name
=> Name_Val
,
4081 Expressions
=> New_List
(Relocate_Node
(E1
))));
4082 Analyze_And_Resolve
(N
, P_Base_Type
);
4084 -- Non-standard representation case (enumeration with holes)
4088 Resolve
(E1
, Any_Integer
);
4089 Set_Etype
(N
, P_Base_Type
);
4096 when Attribute_Epsilon
4097 | Attribute_Model_Epsilon
4098 | Attribute_Model_Small
4099 | Attribute_Safe_First
4100 | Attribute_Safe_Last
4102 Check_Floating_Point_Type_0
;
4103 Set_Etype
(N
, Universal_Real
);
4109 when Attribute_Exponent
=>
4110 Check_Floating_Point_Type_1
;
4111 Set_Etype
(N
, Universal_Integer
);
4112 Resolve
(E1
, P_Base_Type
);
4118 when Attribute_External_Tag
=>
4122 Set_Etype
(N
, Standard_String
);
4124 if not Is_Tagged_Type
(P_Type
) then
4125 Error_Attr_P
("prefix of % attribute must be tagged");
4132 when Attribute_Fast_Math
=>
4133 Check_Standard_Prefix
;
4134 Rewrite
(N
, New_Occurrence_Of
(Boolean_Literals
(Fast_Math
), Loc
));
4136 -----------------------
4137 -- Finalization_Size --
4138 -----------------------
4140 when Attribute_Finalization_Size
=>
4143 -- The prefix denotes an object
4145 if Is_Object_Reference
(P
) then
4146 Check_Object_Reference
(P
);
4148 -- The prefix denotes a type
4150 elsif Is_Entity_Name
(P
) and then Is_Type
(Entity
(P
)) then
4152 Check_Not_Incomplete_Type
;
4154 -- Attribute 'Finalization_Size is not defined for class-wide
4155 -- types because it is not possible to know statically whether
4156 -- a definite type will have controlled components or not.
4158 if Is_Class_Wide_Type
(Etype
(P
)) then
4160 ("prefix of % attribute cannot denote a class-wide type");
4163 -- The prefix denotes an illegal construct
4167 ("prefix of % attribute must be a definite type or an object");
4170 Set_Etype
(N
, Universal_Integer
);
4176 when Attribute_First
4179 Check_Array_Or_Scalar_Type
;
4180 Bad_Attribute_For_Predicate
;
4186 when Attribute_First_Bit
4187 | Attribute_Last_Bit
4188 | Attribute_Position
4191 Set_Etype
(N
, Universal_Integer
);
4197 when Attribute_First_Valid
4198 | Attribute_Last_Valid
4200 Check_First_Last_Valid
;
4201 Set_Etype
(N
, P_Type
);
4207 when Attribute_Fixed_Value
=>
4208 Check_Fixed_Point_Type
;
4210 Resolve
(E1
, Any_Integer
);
4211 Set_Etype
(N
, P_Base_Type
);
4217 -- Shares processing with Ceiling attribute
4223 when Attribute_Fore
=>
4224 Check_Fixed_Point_Type_0
;
4225 Set_Etype
(N
, Universal_Integer
);
4231 -- Shares processing with Ceiling attribute
4237 when Attribute_From_Any
=>
4239 Check_PolyORB_Attribute
;
4240 Set_Etype
(N
, P_Base_Type
);
4242 -----------------------
4243 -- Has_Access_Values --
4244 -----------------------
4246 when Attribute_Has_Access_Values
4247 | Attribute_Has_Tagged_Values
4251 Set_Etype
(N
, Standard_Boolean
);
4253 ----------------------
4254 -- Has_Same_Storage --
4255 ----------------------
4257 when Attribute_Has_Same_Storage
=>
4260 -- The arguments must be objects of any type
4262 Analyze_And_Resolve
(P
);
4263 Analyze_And_Resolve
(E1
);
4264 Check_Object_Reference
(P
);
4265 Check_Object_Reference
(E1
);
4266 Set_Etype
(N
, Standard_Boolean
);
4268 -----------------------
4269 -- Has_Tagged_Values --
4270 -----------------------
4272 -- Shares processing with Has_Access_Values attribute
4274 -----------------------
4275 -- Has_Discriminants --
4276 -----------------------
4278 when Attribute_Has_Discriminants
=>
4279 Legal_Formal_Attribute
;
4285 when Attribute_Identity
=>
4289 if Etype
(P
) = Standard_Exception_Type
then
4290 Set_Etype
(N
, RTE
(RE_Exception_Id
));
4292 -- Ada 2005 (AI-345): Attribute 'Identity may be applied to task
4293 -- interface class-wide types.
4295 elsif Is_Task_Type
(Etype
(P
))
4296 or else (Is_Access_Type
(Etype
(P
))
4297 and then Is_Task_Type
(Designated_Type
(Etype
(P
))))
4298 or else (Ada_Version
>= Ada_2005
4299 and then Ekind
(Etype
(P
)) = E_Class_Wide_Type
4300 and then Is_Interface
(Etype
(P
))
4301 and then Is_Task_Interface
(Etype
(P
)))
4304 Set_Etype
(N
, RTE
(RO_AT_Task_Id
));
4307 if Ada_Version
>= Ada_2005
then
4309 ("prefix of % attribute must be an exception, a task or a "
4310 & "task interface class-wide object");
4313 ("prefix of % attribute must be a task or an exception");
4321 when Attribute_Image
=>
4322 if Is_Real_Type
(P_Type
) then
4323 if Ada_Version
= Ada_83
and then Comes_From_Source
(N
) then
4324 Error_Msg_Name_1
:= Aname
;
4326 ("(Ada 83) % attribute not allowed for real types", N
);
4330 Analyze_Image_Attribute
(Standard_String
);
4336 when Attribute_Img
=>
4337 Analyze_Image_Attribute
(Standard_String
);
4343 when Attribute_Initialized
=>
4346 if Comes_From_Source
(N
) then
4348 -- This attribute be prefixed with references to objects or
4349 -- values (such as a current instance value given within a type
4350 -- or subtype aspect).
4352 if not Is_Object_Reference
(P
)
4353 and then not Is_Current_Instance_Reference_In_Type_Aspect
(P
)
4355 Error_Attr_P
("prefix of % attribute must be object");
4359 Set_Etype
(N
, Standard_Boolean
);
4365 when Attribute_Input
=>
4367 Check_Stream_Attribute
(TSS_Stream_Input
);
4368 Set_Etype
(N
, P_Base_Type
);
4374 when Attribute_Integer_Value
=>
4377 Resolve
(E1
, Any_Fixed
);
4379 -- Signal an error if argument type is not a specific fixed-point
4380 -- subtype. An error has been signalled already if the argument
4381 -- was not of a fixed-point type.
4383 if Etype
(E1
) = Any_Fixed
and then not Error_Posted
(E1
) then
4384 Error_Attr
("argument of % must be of a fixed-point type", E1
);
4387 Set_Etype
(N
, P_Base_Type
);
4393 when Attribute_Invalid_Value
=>
4396 Set_Etype
(N
, P_Base_Type
);
4397 Invalid_Value_Used
:= True;
4403 when Attribute_Large
4405 | Attribute_Safe_Large
4406 | Attribute_Safe_Small
4410 Set_Etype
(N
, Universal_Real
);
4416 -- Shares processing with First attribute
4422 -- Shares processing with First_Bit attribute
4428 -- Shares processing with First_Valid attribute
4434 -- Shares processing with Compose attribute
4440 when Attribute_Length
=>
4442 Set_Etype
(N
, Universal_Integer
);
4448 when Attribute_Library_Level
=>
4451 if not Is_Entity_Name
(P
) then
4452 Error_Attr_P
("prefix of % attribute must be an entity name");
4455 if not Inside_A_Generic
then
4456 Set_Boolean_Result
(N
,
4457 Is_Library_Level_Entity
(Entity
(P
)));
4460 Set_Etype
(N
, Standard_Boolean
);
4466 when Attribute_Lock_Free
=>
4468 Set_Etype
(N
, Standard_Boolean
);
4470 if not Is_Protected_Type
(P_Type
) then
4472 ("prefix of % attribute must be a protected object");
4479 when Attribute_Loop_Entry
=> Loop_Entry
: declare
4480 procedure Check_References_In_Prefix
(Loop_Id
: Entity_Id
);
4481 -- Inspect the prefix for any uses of entities declared within the
4482 -- related loop. Loop_Id denotes the loop identifier.
4484 --------------------------------
4485 -- Check_References_In_Prefix --
4486 --------------------------------
4488 procedure Check_References_In_Prefix
(Loop_Id
: Entity_Id
) is
4489 Loop_Decl
: constant Node_Id
:= Label_Construct
(Parent
(Loop_Id
));
4491 function Check_Reference
(Nod
: Node_Id
) return Traverse_Result
;
4492 -- Determine whether a reference mentions an entity declared
4493 -- within the related loop.
4495 function Declared_Within
(Nod
: Node_Id
) return Boolean;
4496 -- Determine whether Nod appears in the subtree of Loop_Decl but
4497 -- not within the subtree of the prefix P itself.
4499 ---------------------
4500 -- Check_Reference --
4501 ---------------------
4503 function Check_Reference
(Nod
: Node_Id
) return Traverse_Result
is
4505 if Nkind
(Nod
) = N_Identifier
4506 and then Present
(Entity
(Nod
))
4507 and then Declared_Within
(Declaration_Node
(Entity
(Nod
)))
4510 ("prefix of attribute % cannot reference local entities",
4516 end Check_Reference
;
4518 procedure Check_References
is new Traverse_Proc
(Check_Reference
);
4520 ---------------------
4521 -- Declared_Within --
4522 ---------------------
4524 function Declared_Within
(Nod
: Node_Id
) return Boolean is
4529 while Present
(Stmt
) loop
4530 if Stmt
= Loop_Decl
then
4536 -- Prevent the search from going too far
4538 elsif Is_Body_Or_Package_Declaration
(Stmt
) then
4542 Stmt
:= Parent
(Stmt
);
4546 end Declared_Within
;
4548 -- Start of processing for Check_Prefix_For_Local_References
4551 Check_References
(P
);
4552 end Check_References_In_Prefix
;
4556 Context
: constant Node_Id
:= Parent
(N
);
4558 Encl_Loop
: Node_Id
:= Empty
;
4559 Encl_Prag
: Node_Id
:= Empty
;
4560 Loop_Id
: Entity_Id
:= Empty
;
4564 -- Start of processing for Loop_Entry
4569 -- Set the type of the attribute now to ensure the successful
4570 -- continuation of analysis even if the attribute is misplaced.
4572 Set_Etype
(Attr
, P_Type
);
4574 -- Attribute 'Loop_Entry may appear in several flavors:
4576 -- * Prefix'Loop_Entry - in this form, the attribute applies to the
4577 -- nearest enclosing loop.
4579 -- * Prefix'Loop_Entry (Expr) - depending on what Expr denotes, the
4580 -- attribute may be related to a loop denoted by label Expr or
4581 -- the prefix may denote an array object and Expr may act as an
4582 -- indexed component.
4584 -- * Prefix'Loop_Entry (Expr1, ..., ExprN) - the attribute applies
4585 -- to the nearest enclosing loop, all expressions are part of
4586 -- an indexed component.
4588 -- * Prefix'Loop_Entry (Expr) (...) (...) - depending on what Expr
4589 -- denotes, the attribute may be related to a loop denoted by
4590 -- label Expr or the prefix may denote a multidimensional array
4591 -- array object and Expr along with the rest of the expressions
4592 -- may act as indexed components.
4594 -- Regardless of variations, the attribute reference does not have an
4595 -- expression list. Instead, all available expressions are stored as
4596 -- indexed components.
4598 -- When the attribute is part of an indexed component, find the first
4599 -- expression as it will determine the semantics of 'Loop_Entry.
4601 -- If the attribute is itself an index in an indexed component, i.e.
4602 -- a member of a list, the context itself is not relevant (the code
4603 -- below would lead to an infinite loop) and the attribute applies
4604 -- to the enclosing loop.
4606 if Nkind
(Context
) = N_Indexed_Component
4607 and then not Is_List_Member
(N
)
4609 E1
:= First
(Expressions
(Context
));
4612 -- The attribute reference appears in the following form:
4614 -- Prefix'Loop_Entry (Exp1, Expr2, ..., ExprN) [(...)]
4616 -- In this case, the loop name is omitted and no rewriting is
4619 if Present
(E2
) then
4622 -- The form of the attribute is:
4624 -- Prefix'Loop_Entry (Expr) [(...)]
4626 -- If Expr denotes a loop entry, the whole attribute and indexed
4627 -- component will have to be rewritten to reflect this relation.
4630 pragma Assert
(Present
(E1
));
4632 -- Do not expand the expression as it may have side effects.
4633 -- Simply preanalyze to determine whether it is a loop name or
4636 Preanalyze_And_Resolve
(E1
);
4638 if Is_Entity_Name
(E1
)
4639 and then Present
(Entity
(E1
))
4640 and then Ekind
(Entity
(E1
)) = E_Loop
4642 Loop_Id
:= Entity
(E1
);
4644 -- Transform the attribute and enclosing indexed component
4646 Set_Expressions
(N
, Expressions
(Context
));
4647 Rewrite
(Context
, N
);
4648 Set_Etype
(Context
, P_Type
);
4655 -- The prefix must denote an object
4657 if not Is_Object_Reference
(P
) then
4658 Error_Attr_P
("prefix of attribute % must denote an object");
4661 -- The prefix cannot be of a limited type because the expansion of
4662 -- Loop_Entry must create a constant initialized by the evaluated
4665 if Is_Limited_View
(Etype
(P
)) then
4666 Error_Attr_P
("prefix of attribute % cannot be limited");
4669 -- Climb the parent chain to verify the location of the attribute and
4670 -- find the enclosing loop.
4673 while Present
(Stmt
) loop
4675 -- Locate the corresponding enclosing pragma. Note that in the
4676 -- case of Assert[And_Cut] and Assume, we have already checked
4677 -- that the pragma appears in an appropriate loop location.
4679 if Nkind
(Original_Node
(Stmt
)) = N_Pragma
4681 Pragma_Name_Unmapped
(Original_Node
(Stmt
))
4682 in Name_Loop_Invariant
4685 | Name_Assert_And_Cut
4688 Encl_Prag
:= Original_Node
(Stmt
);
4690 -- Locate the enclosing loop (if any). Note that Ada 2012 array
4691 -- iteration may be expanded into several nested loops, we are
4692 -- interested in the outermost one which has the loop identifier,
4693 -- and comes from source.
4695 elsif Nkind
(Stmt
) = N_Loop_Statement
4696 and then Present
(Identifier
(Stmt
))
4697 and then Comes_From_Source
(Original_Node
(Stmt
))
4698 and then Nkind
(Original_Node
(Stmt
)) = N_Loop_Statement
4702 -- The original attribute reference may lack a loop name. Use
4703 -- the name of the enclosing loop because it is the related
4706 if No
(Loop_Id
) then
4707 Loop_Id
:= Entity
(Identifier
(Encl_Loop
));
4712 -- Prevent the search from going too far
4714 elsif Is_Body_Or_Package_Declaration
(Stmt
) then
4718 Stmt
:= Parent
(Stmt
);
4721 -- Loop_Entry must appear within a Loop_Assertion pragma (Assert,
4722 -- Assert_And_Cut, Assume count as loop assertion pragmas for this
4723 -- purpose if they appear in an appropriate location in a loop,
4724 -- which was already checked by the top level pragma circuit).
4726 -- Loop_Entry also denotes a value and as such can appear within an
4727 -- expression that is an argument for another loop aspect. In that
4728 -- case it will have been expanded into the corresponding assignment.
4731 and then Nkind
(Parent
(N
)) = N_Assignment_Statement
4732 and then not Comes_From_Source
(Parent
(N
))
4736 elsif No
(Encl_Prag
) then
4737 Error_Attr
("attribute% must appear within appropriate pragma", N
);
4740 -- A Loop_Entry that applies to a given loop statement must not
4741 -- appear within a body of accept statement, if this construct is
4742 -- itself enclosed by the given loop statement.
4744 for Index
in reverse 0 .. Scope_Stack
.Last
loop
4745 Scop
:= Scope_Stack
.Table
(Index
).Entity
;
4747 if Ekind
(Scop
) = E_Loop
and then Scop
= Loop_Id
then
4749 elsif Ekind
(Scop
) in E_Block | E_Loop | E_Return_Statement
then
4753 ("attribute % cannot appear in body or accept statement", N
);
4758 -- The prefix cannot mention entities declared within the related
4759 -- loop because they will not be visible once the prefix is moved
4760 -- outside the loop.
4762 Check_References_In_Prefix
(Loop_Id
);
4764 -- The prefix must statically name an object if the pragma does not
4765 -- apply to the innermost enclosing loop statement, or if it appears
4766 -- within a potentially unevaluated expression.
4768 if Is_Entity_Name
(P
)
4769 or else Nkind
(Parent
(P
)) = N_Object_Renaming_Declaration
4770 or else Statically_Names_Object
(P
)
4774 elsif Present
(Encl_Loop
)
4775 and then Entity
(Identifier
(Encl_Loop
)) /= Loop_Id
4778 ("prefix of attribute % that applies to outer loop must denote "
4781 elsif Is_Potentially_Unevaluated
(P
) then
4785 -- Replace the Loop_Entry attribute reference by its prefix if the
4786 -- related pragma is ignored. This transformation is OK with respect
4787 -- to typing because Loop_Entry's type is that of its prefix. This
4788 -- early transformation also avoids the generation of a useless loop
4791 if Present
(Encl_Prag
) and then Is_Ignored
(Encl_Prag
) then
4792 Rewrite
(N
, Relocate_Node
(P
));
4793 Preanalyze_And_Resolve
(N
);
4796 Preanalyze_And_Resolve
(P
);
4804 -- Shares processing with Ceiling attribute
4810 -- Shares processing with Emax attribute
4816 -- Shares processing with Emax attribute
4818 ----------------------
4819 -- Machine_Mantissa --
4820 ----------------------
4822 -- Shares processing with Emax attribute
4824 -----------------------
4825 -- Machine_Overflows --
4826 -----------------------
4828 when Attribute_Machine_Overflows
4829 | Attribute_Machine_Rounds
4833 Set_Etype
(N
, Standard_Boolean
);
4839 when Attribute_Machine_Radix
4840 | Attribute_Mantissa
4844 Set_Etype
(N
, Universal_Integer
);
4846 ----------------------
4847 -- Machine_Rounding --
4848 ----------------------
4850 -- Shares processing with Ceiling attribute
4852 --------------------
4853 -- Machine_Rounds --
4854 --------------------
4856 -- Shares processing with Machine_Overflows attribute
4862 when Attribute_Machine_Size
4863 | Attribute_Object_Size
4864 | Attribute_Value_Size
4868 Check_Not_Incomplete_Type
;
4869 Set_Etype
(N
, Universal_Integer
);
4875 -- Shares processing with Machine_Radix attribute
4881 when Attribute_Max
=>
4884 ----------------------------------
4885 -- Max_Alignment_For_Allocation --
4886 ----------------------------------
4888 when Attribute_Max_Size_In_Storage_Elements
=>
4889 Max_Alignment_For_Allocation_Max_Size_In_Storage_Elements
;
4891 ----------------------
4892 -- Max_Integer_Size --
4893 ----------------------
4895 when Attribute_Max_Integer_Size
=>
4896 Standard_Attribute
(System_Max_Integer_Size
);
4898 ----------------------------------
4899 -- Max_Size_In_Storage_Elements --
4900 ----------------------------------
4902 when Attribute_Max_Alignment_For_Allocation
=>
4903 Max_Alignment_For_Allocation_Max_Size_In_Storage_Elements
;
4905 -----------------------
4906 -- Maximum_Alignment --
4907 -----------------------
4909 when Attribute_Maximum_Alignment
=>
4910 Standard_Attribute
(Ttypes
.Maximum_Alignment
);
4912 --------------------
4913 -- Mechanism_Code --
4914 --------------------
4916 when Attribute_Mechanism_Code
=>
4917 if not Is_Entity_Name
(P
)
4918 or else not Is_Subprogram
(Entity
(P
))
4920 Error_Attr_P
("prefix of % attribute must be subprogram");
4923 Check_Either_E0_Or_E1
;
4925 if Present
(E1
) then
4926 Resolve
(E1
, Any_Integer
);
4927 Set_Etype
(E1
, Standard_Integer
);
4929 if not Is_OK_Static_Expression
(E1
) then
4930 Flag_Non_Static_Expr
4931 ("expression for parameter number must be static!", E1
);
4934 elsif UI_To_Int
(Intval
(E1
)) > Number_Formals
(Entity
(P
))
4935 or else Intval
(E1
) < 0
4937 Error_Attr
("invalid parameter number for % attribute", E1
);
4941 Set_Etype
(N
, Universal_Integer
);
4947 when Attribute_Min
=>
4954 when Attribute_Mod
=>
4956 -- Note: this attribute is only allowed in Ada 2005 mode, but
4957 -- we do not need to test that here, since Mod is only recognized
4958 -- as an attribute name in Ada 2005 mode during the parse.
4961 Check_Modular_Integer_Type
;
4962 Resolve
(E1
, Any_Integer
);
4963 Set_Etype
(N
, P_Base_Type
);
4969 -- Shares processing with Ceiling attribute
4975 -- Shares processing with Emax attribute
4981 -- Shares processing with Epsilon attribute
4983 --------------------
4984 -- Model_Mantissa --
4985 --------------------
4987 -- Shares processing with Emax attribute
4993 -- Shares processing with Epsilon attribute
4999 when Attribute_Modulus
=>
5001 Check_Modular_Integer_Type
;
5002 Set_Etype
(N
, Universal_Integer
);
5004 --------------------
5005 -- Null_Parameter --
5006 --------------------
5008 when Attribute_Null_Parameter
=> Null_Parameter
: declare
5009 Parnt
: constant Node_Id
:= Parent
(N
);
5010 GParnt
: constant Node_Id
:= Parent
(Parnt
);
5012 procedure Bad_Null_Parameter
(Msg
: String);
5013 -- Used if bad Null parameter attribute node is found. Issues
5014 -- given error message, and also sets the type to Any_Type to
5015 -- avoid blowups later on from dealing with a junk node.
5017 procedure Must_Be_Imported
(Proc_Ent
: Entity_Id
);
5018 -- Called to check that Proc_Ent is imported subprogram
5020 ------------------------
5021 -- Bad_Null_Parameter --
5022 ------------------------
5024 procedure Bad_Null_Parameter
(Msg
: String) is
5026 Error_Msg_N
(Msg
, N
);
5027 Set_Etype
(N
, Any_Type
);
5028 end Bad_Null_Parameter
;
5030 ----------------------
5031 -- Must_Be_Imported --
5032 ----------------------
5034 procedure Must_Be_Imported
(Proc_Ent
: Entity_Id
) is
5035 Pent
: constant Entity_Id
:= Ultimate_Alias
(Proc_Ent
);
5038 -- Ignore check if procedure not frozen yet (we will get
5039 -- another chance when the default parameter is reanalyzed)
5041 if not Is_Frozen
(Pent
) then
5044 elsif not Is_Imported
(Pent
) then
5046 ("Null_Parameter can only be used with imported subprogram");
5051 end Must_Be_Imported
;
5053 -- Start of processing for Null_Parameter
5058 Set_Etype
(N
, P_Type
);
5060 -- Case of attribute used as default expression
5062 if Nkind
(Parnt
) = N_Parameter_Specification
then
5063 Must_Be_Imported
(Defining_Entity
(GParnt
));
5065 -- Case of attribute used as actual for subprogram (positional)
5067 elsif Nkind
(Parnt
) in N_Subprogram_Call
5068 and then Is_Entity_Name
(Name
(Parnt
))
5070 Must_Be_Imported
(Entity
(Name
(Parnt
)));
5072 -- Case of attribute used as actual for subprogram (named)
5074 elsif Nkind
(Parnt
) = N_Parameter_Association
5075 and then Nkind
(GParnt
) in N_Subprogram_Call
5076 and then Is_Entity_Name
(Name
(GParnt
))
5078 Must_Be_Imported
(Entity
(Name
(GParnt
)));
5080 -- Not an allowed case
5084 ("Null_Parameter must be actual or default parameter");
5092 -- Shares processing with Machine_Size attribute
5098 when Attribute_Old
=> Old
: declare
5099 procedure Check_References_In_Prefix
(Subp_Id
: Entity_Id
);
5100 -- Inspect the contents of the prefix and detect illegal uses of a
5101 -- nested 'Old, attribute 'Result or a use of an entity declared in
5102 -- the related postcondition expression. Subp_Id is the subprogram to
5103 -- which the related postcondition applies.
5105 --------------------------------
5106 -- Check_References_In_Prefix --
5107 --------------------------------
5109 procedure Check_References_In_Prefix
(Subp_Id
: Entity_Id
) is
5110 function Check_Reference
(Nod
: Node_Id
) return Traverse_Result
;
5111 -- Detect attribute 'Old, attribute 'Result of a use of an entity
5112 -- and perform the appropriate semantic check.
5114 ---------------------
5115 -- Check_Reference --
5116 ---------------------
5118 function Check_Reference
(Nod
: Node_Id
) return Traverse_Result
is
5120 -- Attributes 'Old and 'Result cannot appear in the prefix of
5121 -- another attribute 'Old.
5123 if Nkind
(Nod
) = N_Attribute_Reference
5124 and then Attribute_Name
(Nod
) in Name_Old | Name_Result
5126 Error_Msg_Name_1
:= Attribute_Name
(Nod
);
5127 Error_Msg_Name_2
:= Name_Old
;
5129 ("attribute % cannot appear in the prefix of attribute %",
5133 -- Entities mentioned within the prefix of attribute 'Old must
5134 -- be global to the related postcondition. If this is not the
5135 -- case, then the scope of the local entity is nested within
5136 -- that of the subprogram.
5138 elsif Is_Entity_Name
(Nod
)
5139 and then Present
(Entity
(Nod
))
5140 and then Scope_Within
(Scope
(Entity
(Nod
)), Subp_Id
)
5143 ("prefix of attribute % cannot reference local entities",
5147 -- Otherwise keep inspecting the prefix
5152 end Check_Reference
;
5154 procedure Check_References
is new Traverse_Proc
(Check_Reference
);
5156 -- Start of processing for Check_References_In_Prefix
5159 Check_References
(P
);
5160 end Check_References_In_Prefix
;
5165 Pref_Id
: Entity_Id
;
5166 Pref_Typ
: Entity_Id
;
5167 Spec_Id
: Entity_Id
;
5169 -- Start of processing for Old
5172 -- The attribute reference is a primary. If any expressions follow,
5173 -- then the attribute reference is an indexable object. Transform the
5174 -- attribute into an indexed component and analyze it.
5176 if Present
(E1
) then
5178 Make_Indexed_Component
(Loc
,
5180 Make_Attribute_Reference
(Loc
,
5181 Prefix
=> Relocate_Node
(P
),
5182 Attribute_Name
=> Name_Old
),
5183 Expressions
=> Expressions
(N
)));
5188 Analyze_Attribute_Old_Result
(Legal
, Spec_Id
);
5190 -- The aspect or pragma where attribute 'Old resides should be
5191 -- associated with a subprogram declaration or a body. If this is not
5192 -- the case, then the aspect or pragma is illegal. Return as analysis
5193 -- cannot be carried out.
5195 -- The exception to this rule is when generating C since in this case
5196 -- postconditions are inlined.
5199 and then Modify_Tree_For_C
5200 and then In_Inlined_Body
5202 Spec_Id
:= Entity
(P
);
5204 elsif not Legal
then
5208 -- The prefix must be preanalyzed as the full analysis will take
5209 -- place during expansion.
5211 Preanalyze_And_Resolve
(P
);
5213 -- Ensure that the prefix does not contain attributes 'Old or 'Result
5215 Check_References_In_Prefix
(Spec_Id
);
5217 -- Set the type of the attribute now to prevent cascaded errors
5219 Pref_Typ
:= Etype
(P
);
5220 Set_Etype
(N
, Pref_Typ
);
5224 if Is_Limited_Type
(Pref_Typ
) then
5225 Error_Attr
("attribute % cannot apply to limited objects", P
);
5228 -- The prefix is a simple name
5230 if Is_Entity_Name
(P
) and then Present
(Entity
(P
)) then
5231 Pref_Id
:= Entity
(P
);
5233 -- Emit a warning when the prefix is a constant. Note that the use
5234 -- of Error_Attr would reset the type of N to Any_Type even though
5235 -- this is a warning. Use Error_Msg_XXX instead.
5237 if Is_Constant_Object
(Pref_Id
) then
5238 Error_Msg_Name_1
:= Name_Old
;
5240 ("??attribute % applied to constant has no effect", P
);
5243 -- Otherwise the prefix is not a simple name
5246 -- Ensure that the prefix of attribute 'Old is an entity when it
5247 -- is potentially unevaluated (6.1.1 (27/3)). This rule is
5248 -- relaxed in Ada 2022 - this relaxation is reflected in the
5249 -- call (below) to Eligible_For_Conditional_Evaluation.
5251 if Is_Potentially_Unevaluated
(N
)
5252 and then not Statically_Names_Object
(P
)
5254 Old_Attr_Util
.Conditional_Evaluation
5255 .Eligible_For_Conditional_Evaluation
(N
)
5259 -- Detect a possible infinite recursion when the prefix denotes
5260 -- the related function.
5262 -- function Func (...) return ...
5263 -- with Post => Func'Old ...;
5265 -- The function may be specified in qualified form X.Y where X is
5266 -- a protected object and Y is a protected function. In that case
5267 -- ensure that the qualified form has an entity.
5269 elsif Nkind
(P
) = N_Function_Call
5270 and then Nkind
(Name
(P
)) in N_Has_Entity
5272 Pref_Id
:= Entity
(Name
(P
));
5274 if Ekind
(Spec_Id
) in E_Function | E_Generic_Function
5275 and then Pref_Id
= Spec_Id
5277 Error_Msg_Warn
:= SPARK_Mode
/= On
;
5278 Error_Msg_N
("!possible infinite recursion<<", P
);
5279 Error_Msg_N
("\!??Storage_Error ]<<", P
);
5283 -- The prefix of attribute 'Old may refer to a component of a
5284 -- formal parameter. In this case its expansion may generate
5285 -- actual subtypes that are referenced in an inner context and
5286 -- that must be elaborated within the subprogram itself. If the
5287 -- prefix includes a function call, it may involve finalization
5288 -- actions that should be inserted when the attribute has been
5289 -- rewritten as a declaration. Create a declaration for the prefix
5290 -- and insert it at the start of the enclosing subprogram. This is
5291 -- an expansion activity that has to be performed now to prevent
5292 -- out-of-order issues.
5294 -- This expansion is both harmful and not needed in SPARK mode,
5295 -- since the formal verification back end relies on the types of
5296 -- nodes (hence is not robust w.r.t. a change to base type here),
5297 -- and does not suffer from the out-of-order issue described
5298 -- above. Thus, this expansion is skipped in SPARK mode.
5300 -- The expansion is not relevant for discrete types, which will
5301 -- not generate extra declarations, and where use of the base type
5302 -- may lead to spurious errors if context is a case.
5304 if not GNATprove_Mode
then
5305 if not Is_Discrete_Type
(Pref_Typ
) then
5306 Pref_Typ
:= Base_Type
(Pref_Typ
);
5309 Set_Etype
(N
, Pref_Typ
);
5310 Set_Etype
(P
, Pref_Typ
);
5312 Analyze_Dimension
(N
);
5318 ----------------------
5319 -- Overlaps_Storage --
5320 ----------------------
5322 when Attribute_Overlaps_Storage
=>
5325 -- Both arguments must be objects of any type
5327 Analyze_And_Resolve
(P
);
5328 Analyze_And_Resolve
(E1
);
5329 Check_Object_Reference
(P
);
5330 Check_Object_Reference
(E1
);
5331 Set_Etype
(N
, Standard_Boolean
);
5337 when Attribute_Output
=>
5339 Check_Stream_Attribute
(TSS_Stream_Output
);
5340 Set_Etype
(N
, Standard_Void_Type
);
5341 Resolve
(N
, Standard_Void_Type
);
5347 when Attribute_Partition_ID
=>
5350 if P_Type
/= Any_Type
then
5351 if not Is_Library_Level_Entity
(Entity
(P
)) then
5353 ("prefix of % attribute must be library-level entity");
5355 -- The defining entity of prefix should not be declared inside a
5356 -- Pure unit. RM E.1(8). Is_Pure was set during declaration.
5358 elsif Is_Entity_Name
(P
)
5359 and then Is_Pure
(Entity
(P
))
5361 Error_Attr_P
("prefix of% attribute must not be declared pure");
5365 Set_Etype
(N
, Universal_Integer
);
5367 -------------------------
5368 -- Passed_By_Reference --
5369 -------------------------
5371 when Attribute_Passed_By_Reference
=>
5374 Check_Not_Incomplete_Type
;
5375 Set_Etype
(N
, Standard_Boolean
);
5381 when Attribute_Pool_Address
=>
5383 Set_Etype
(N
, RTE
(RE_Address
));
5389 when Attribute_Pos
=>
5390 Check_Discrete_Type
;
5392 Resolve
(E1
, P_Base_Type
);
5393 Set_Etype
(N
, Universal_Integer
);
5399 -- Shares processing with First_Bit attribute
5410 Resolve
(E1
, P_Base_Type
);
5411 Set_Etype
(N
, P_Base_Type
);
5413 -- Since Pred/Succ work on the base type, we normally do no check for
5414 -- the floating-point case, since the base type is unconstrained. But
5415 -- we make an exception in Check_Float_Overflow mode.
5417 if Is_Floating_Point_Type
(P_Type
) then
5418 if not Range_Checks_Suppressed
(P_Base_Type
) then
5419 Set_Do_Range_Check
(E1
);
5422 -- If not modular type, test for overflow check required
5425 if not Is_Modular_Integer_Type
(P_Type
)
5426 and then not Range_Checks_Suppressed
(P_Base_Type
)
5428 Enable_Range_Check
(E1
);
5432 ----------------------------------
5433 -- Preelaborable_Initialization --
5434 ----------------------------------
5436 when Attribute_Preelaborable_Initialization
=>
5440 -- If we're in an instance, we know that the legality of the
5441 -- attribute prefix type was already checked in the generic.
5443 if not In_Instance
then
5445 -- If the prefix type is a generic formal type, then it must be
5446 -- either a formal private type or a formal derived type.
5448 if Is_Generic_Type
(P_Type
) then
5449 if not Is_Private_Type
(P_Type
)
5450 and then not Is_Derived_Type
(P_Type
)
5452 Error_Attr_P
("formal type prefix of % attribute must be "
5453 & "formal private or formal derived type");
5456 -- Otherwise, the prefix type must be a nonformal composite
5457 -- type declared within the visible part of a package or
5460 elsif not Is_Composite_Type
(P_Type
)
5461 or else not Original_View_In_Visible_Part
(P_Type
)
5464 ("prefix of % attribute must be composite type declared "
5465 & "in visible part of a package or generic package");
5469 Set_Etype
(N
, Standard_Boolean
);
5475 -- Ada 2005 (AI-327): Dynamic ceiling priorities
5477 when Attribute_Priority
=>
5478 if Ada_Version
< Ada_2005
then
5479 Error_Attr
("% attribute is allowed only in Ada 2005 mode", P
);
5484 Check_Restriction
(No_Dynamic_Priorities
, N
);
5486 -- The prefix must be a protected object (AARM D.5.2 (2/2))
5490 if Is_Protected_Type
(Etype
(P
))
5491 or else (Is_Access_Type
(Etype
(P
))
5492 and then Is_Protected_Type
(Designated_Type
(Etype
(P
))))
5496 Error_Attr_P
("prefix of % attribute must be a protected object");
5499 Set_Etype
(N
, Standard_Integer
);
5501 -- Must be called from within a protected procedure or entry of the
5502 -- protected object.
5509 while S
/= Etype
(P
)
5510 and then S
/= Standard_Standard
5515 if S
= Standard_Standard
then
5516 Error_Attr
("the attribute % is only allowed inside protected "
5521 Validate_Non_Static_Attribute_Function_Call
;
5527 when Attribute_Put_Image
=>
5529 Check_Put_Image_Attribute
;
5530 Set_Etype
(N
, Standard_Void_Type
);
5531 Resolve
(N
, Standard_Void_Type
);
5537 when Attribute_Range
=>
5538 Check_Array_Or_Scalar_Type
;
5539 Bad_Attribute_For_Predicate
;
5541 if Ada_Version
= Ada_83
5542 and then Is_Scalar_Type
(P_Type
)
5543 and then Comes_From_Source
(N
)
5546 ("(Ada 83) % attribute not allowed for scalar type", P
);
5553 when Attribute_Result
=> Result
: declare
5554 function Denote_Same_Function
5555 (Pref_Id
: Entity_Id
;
5556 Spec_Id
: Entity_Id
) return Boolean;
5557 -- Determine whether the entity of the prefix Pref_Id denotes the
5558 -- same entity as that of the related subprogram Spec_Id.
5560 --------------------------
5561 -- Denote_Same_Function --
5562 --------------------------
5564 function Denote_Same_Function
5565 (Pref_Id
: Entity_Id
;
5566 Spec_Id
: Entity_Id
) return Boolean
5568 Over_Id
: constant Entity_Id
:= Overridden_Operation
(Spec_Id
);
5569 Subp_Spec
: constant Node_Id
:= Parent
(Spec_Id
);
5572 -- The prefix denotes the related subprogram
5574 if Pref_Id
= Spec_Id
then
5577 -- Account for a special case when attribute 'Result appears in
5578 -- the postcondition of a generic function.
5581 -- function Gen_Func return ...
5582 -- with Post => Gen_Func'Result ...;
5584 -- When the generic function is instantiated, the Chars field of
5585 -- the instantiated prefix still denotes the name of the generic
5586 -- function. Note that any preemptive transformation is impossible
5587 -- without a proper analysis. The structure of the wrapper package
5590 -- package Anon_Gen_Pack is
5591 -- <subtypes and renamings>
5592 -- function Subp_Decl return ...; -- (!)
5593 -- pragma Postcondition (Gen_Func'Result ...); -- (!)
5594 -- function Gen_Func ... renames Subp_Decl;
5595 -- end Anon_Gen_Pack;
5597 elsif Nkind
(Subp_Spec
) = N_Function_Specification
5598 and then Present
(Generic_Parent
(Subp_Spec
))
5599 and then Ekind
(Pref_Id
) in E_Generic_Function | E_Function
5601 if Generic_Parent
(Subp_Spec
) = Pref_Id
then
5604 elsif Present
(Alias
(Pref_Id
))
5605 and then Alias
(Pref_Id
) = Spec_Id
5610 -- Account for a special case where a primitive of a tagged type
5611 -- inherits a class-wide postcondition from a parent type. In this
5612 -- case the prefix of attribute 'Result denotes the overriding
5615 elsif Present
(Over_Id
) and then Pref_Id
= Over_Id
then
5619 -- Otherwise the prefix does not denote the related subprogram
5622 end Denote_Same_Function
;
5626 In_Inlined_C_Postcondition
: constant Boolean :=
5628 and then In_Inlined_Body
;
5631 Pref_Id
: Entity_Id
;
5632 Spec_Id
: Entity_Id
;
5634 -- Start of processing for Result
5637 -- The attribute reference is a primary. If any expressions follow,
5638 -- then the attribute reference is an indexable object. Transform the
5639 -- attribute into an indexed component and analyze it.
5641 if Present
(E1
) then
5643 Make_Indexed_Component
(Loc
,
5645 Make_Attribute_Reference
(Loc
,
5646 Prefix
=> Relocate_Node
(P
),
5647 Attribute_Name
=> Name_Result
),
5648 Expressions
=> Expressions
(N
)));
5653 Analyze_Attribute_Old_Result
(Legal
, Spec_Id
);
5655 -- The aspect or pragma where attribute 'Result resides should be
5656 -- associated with a subprogram declaration or a body. If this is not
5657 -- the case, then the aspect or pragma is illegal. Return as analysis
5658 -- cannot be carried out.
5660 -- The exception to this rule is when generating C since in this case
5661 -- postconditions are inlined.
5663 if No
(Spec_Id
) and then In_Inlined_C_Postcondition
then
5664 Spec_Id
:= Entity
(P
);
5666 elsif not Legal
then
5667 Error_Attr
("prefix of % attribute must be a function", P
);
5671 -- Attribute 'Result is part of a _Postconditions procedure. There is
5672 -- no need to perform the semantic checks below as they were already
5673 -- verified when the attribute was analyzed in its original context.
5674 -- Instead, rewrite the attribute as a reference to formal parameter
5675 -- _Result of the _Postconditions procedure.
5677 if Chars
(Spec_Id
) = Name_uPostconditions
5679 (In_Inlined_C_Postcondition
5680 and then Nkind
(Parent
(Spec_Id
)) = N_Block_Statement
)
5682 Rewrite
(N
, Make_Identifier
(Loc
, Name_uResult
));
5684 -- The type of formal parameter _Result is that of the function
5685 -- encapsulating the _Postconditions procedure. Resolution must
5686 -- be carried out against the function return type.
5688 Analyze_And_Resolve
(N
, Etype
(Scope
(Spec_Id
)));
5690 -- Otherwise attribute 'Result appears in its original context and
5691 -- all semantic checks should be carried out.
5694 -- Verify the legality of the prefix. It must denotes the entity
5695 -- of the related [generic] function.
5697 if Is_Entity_Name
(P
) then
5698 Pref_Id
:= Entity
(P
);
5700 -- Either both the prefix and the annotated spec must be
5701 -- generic functions, or they both must be nongeneric
5702 -- functions, or the prefix must be generic and the spec
5703 -- must be nongeneric (i.e. it must denote an instance).
5705 if (Ekind
(Pref_Id
) in E_Function | E_Generic_Function
5706 and then Ekind
(Pref_Id
) = Ekind
(Spec_Id
))
5708 (Ekind
(Pref_Id
) = E_Generic_Function
5709 and then Ekind
(Spec_Id
) = E_Function
)
5711 if Denote_Same_Function
(Pref_Id
, Spec_Id
) then
5713 -- Correct the prefix of the attribute when the context
5714 -- is a generic function.
5716 if Pref_Id
/= Spec_Id
then
5717 Rewrite
(P
, New_Occurrence_Of
(Spec_Id
, Loc
));
5721 Set_Etype
(N
, Etype
(Spec_Id
));
5723 -- Otherwise the prefix denotes some unrelated function
5726 Error_Msg_Name_2
:= Chars
(Spec_Id
);
5728 ("incorrect prefix for attribute %, expected %", P
);
5731 -- Otherwise the prefix denotes some other form of subprogram
5736 ("attribute % can only appear in postcondition of "
5740 -- Otherwise the prefix is illegal
5743 Error_Msg_Name_2
:= Chars
(Spec_Id
);
5744 Error_Attr
("incorrect prefix for attribute %, expected %", P
);
5753 when Attribute_Range_Length
=>
5755 Check_Discrete_Type
;
5756 Set_Etype
(N
, Universal_Integer
);
5762 when Attribute_Reduce
=>
5765 if not Extensions_Allowed
then
5767 ("% attribute only supported under -gnatX", P
);
5771 Stream
: constant Node_Id
:= Prefix
(N
);
5774 if Nkind
(Stream
) /= N_Aggregate
then
5775 -- Prefix is a name, as for other attributes.
5777 -- If the object is a function we asume that it is not
5778 -- overloaded. AI12-242 does not suggest a name resolution
5779 -- rule for that case, but we can suppose that the expected
5780 -- type of the reduction is the expected type of the component
5783 Analyze_And_Resolve
(Stream
);
5784 Typ
:= Etype
(Stream
);
5786 -- Verify that prefix can be iterated upon.
5788 if Is_Array_Type
(Typ
)
5789 or else Present
(Find_Aspect
(Typ
, Aspect_Default_Iterator
))
5790 or else Present
(Find_Aspect
(Typ
, Aspect_Iterable
))
5795 ("cannot apply Reduce to object of type&", N
, Typ
);
5798 elsif Present
(Expressions
(Stream
))
5799 or else No
(Component_Associations
(Stream
))
5800 or else Nkind
(First
(Component_Associations
(Stream
))) /=
5801 N_Iterated_Component_Association
5804 ("prefix of Reduce must be an iterated component", N
);
5809 Set_Etype
(N
, Etype
(E2
));
5816 when Attribute_Read
=>
5818 Check_Stream_Attribute
(TSS_Stream_Read
);
5819 Set_Etype
(N
, Standard_Void_Type
);
5820 Resolve
(N
, Standard_Void_Type
);
5821 Note_Possible_Modification
(E2
, Sure
=> True);
5827 when Attribute_Ref
=>
5831 if Nkind
(P
) /= N_Expanded_Name
5832 or else not Is_RTE
(P_Type
, RE_Address
)
5834 Error_Attr_P
("prefix of % attribute must be System.Address");
5837 Analyze_And_Resolve
(E1
, Any_Integer
);
5838 Set_Etype
(N
, RTE
(RE_Address
));
5844 -- Shares processing with Adjacent attribute
5846 ---------------------
5847 -- Restriction_Set --
5848 ---------------------
5850 when Attribute_Restriction_Set
=> Restriction_Set
: declare
5853 Unam
: Unit_Name_Type
;
5858 Check_System_Prefix
;
5860 -- No_Dependence case
5862 if Nkind
(E1
) = N_Parameter_Association
then
5863 pragma Assert
(Chars
(Selector_Name
(E1
)) = Name_No_Dependence
);
5864 U
:= Explicit_Actual_Parameter
(E1
);
5866 if not OK_No_Dependence_Unit_Name
(U
) then
5867 Set_Boolean_Result
(N
, False);
5871 -- See if there is an entry already in the table. That's the
5872 -- case in which we can return True.
5874 for J
in No_Dependences
.First
.. No_Dependences
.Last
loop
5875 if Designate_Same_Unit
(U
, No_Dependences
.Table
(J
).Unit
)
5876 and then No_Dependences
.Table
(J
).Warn
= False
5878 Set_Boolean_Result
(N
, True);
5883 -- If not in the No_Dependence table, result is False
5885 Set_Boolean_Result
(N
, False);
5887 -- In this case, we must ensure that the binder will reject any
5888 -- other unit in the partition that sets No_Dependence for this
5889 -- unit. We do that by making an entry in the special table kept
5890 -- for this purpose (if the entry is not there already).
5892 Unam
:= Get_Spec_Name
(Get_Unit_Name
(U
));
5894 for J
in Restriction_Set_Dependences
.First
..
5895 Restriction_Set_Dependences
.Last
5897 if Restriction_Set_Dependences
.Table
(J
) = Unam
then
5902 Restriction_Set_Dependences
.Append
(Unam
);
5904 -- Normal restriction case
5907 if Nkind
(E1
) /= N_Identifier
then
5908 Set_Boolean_Result
(N
, False);
5909 Error_Attr
("attribute % requires restriction identifier", E1
);
5912 R
:= Get_Restriction_Id
(Process_Restriction_Synonyms
(E1
));
5914 if R
= Not_A_Restriction_Id
then
5915 Set_Boolean_Result
(N
, False);
5916 Error_Msg_Node_1
:= E1
;
5917 Error_Attr
("invalid restriction identifier &", E1
);
5919 elsif R
not in Partition_Boolean_Restrictions
then
5920 Set_Boolean_Result
(N
, False);
5921 Error_Msg_Node_1
:= E1
;
5923 ("& is not a boolean partition-wide restriction", E1
);
5926 if Restriction_Active
(R
) then
5927 Set_Boolean_Result
(N
, True);
5929 Check_Restriction
(R
, N
);
5930 Set_Boolean_Result
(N
, False);
5934 end Restriction_Set
;
5940 when Attribute_Round
=>
5942 Check_Decimal_Fixed_Point_Type
;
5943 Set_Etype
(N
, P_Base_Type
);
5945 -- Because the context is universal_real (3.5.10(12)) it is a
5946 -- legal context for a universal fixed expression. This is the
5947 -- only attribute whose functional description involves U_R.
5949 if Etype
(E1
) = Universal_Fixed
then
5951 Conv
: constant Node_Id
:= Make_Type_Conversion
(Loc
,
5952 Subtype_Mark
=> New_Occurrence_Of
(Universal_Real
, Loc
),
5953 Expression
=> Relocate_Node
(E1
));
5961 Resolve
(E1
, Any_Real
);
5967 -- Shares processing with Ceiling attribute
5973 -- Shares processing with Emax attribute
5979 -- Shares processing with Epsilon attribute
5985 -- Shares processing with Large attribute
5991 -- Shares processing with Epsilon attribute
5997 -- Shares processing with Large attribute
5999 --------------------------
6000 -- Scalar_Storage_Order --
6001 --------------------------
6003 when Attribute_Scalar_Storage_Order
=> Scalar_Storage_Order
: declare
6004 Ent
: Entity_Id
:= Empty
;
6010 if not (Is_Record_Type
(P_Type
) or else Is_Array_Type
(P_Type
)) then
6012 -- The attribute applies to generic private types (in which case
6013 -- the legality rule is applied in the instance) as well as to
6014 -- composite types. For noncomposite types it always returns the
6015 -- default bit order for the target.
6016 -- Allowing formal private types was originally introduced in
6017 -- GNAT_Mode only, to compile instances of Sequential_IO, but
6018 -- users find it more generally useful in generic units.
6020 if not (Is_Generic_Type
(P_Type
) and then Is_Private_Type
(P_Type
))
6021 and then not In_Instance
6024 ("prefix of % attribute must be record or array type");
6026 elsif not Is_Generic_Type
(P_Type
) then
6027 if Bytes_Big_Endian
then
6028 Ent
:= RTE
(RE_High_Order_First
);
6030 Ent
:= RTE
(RE_Low_Order_First
);
6034 elsif Bytes_Big_Endian
xor Reverse_Storage_Order
(P_Type
) then
6035 Ent
:= RTE
(RE_High_Order_First
);
6038 Ent
:= RTE
(RE_Low_Order_First
);
6041 if Present
(Ent
) then
6042 Rewrite
(N
, New_Occurrence_Of
(Ent
, Loc
));
6045 Set_Etype
(N
, RTE
(RE_Bit_Order
));
6048 -- Reset incorrect indication of staticness
6050 Set_Is_Static_Expression
(N
, False);
6051 end Scalar_Storage_Order
;
6057 when Attribute_Scale
=>
6059 Check_Decimal_Fixed_Point_Type
;
6060 Set_Etype
(N
, Universal_Integer
);
6066 -- Shares processing with Compose attribute
6072 -- Shares processing with Denorm attribute
6079 | Attribute_VADS_Size
6083 -- If prefix is parameterless function call, rewrite and resolve
6086 if Is_Entity_Name
(P
)
6087 and then Ekind
(Entity
(P
)) = E_Function
6091 -- Similar processing for a protected function call
6093 elsif Nkind
(P
) = N_Selected_Component
6094 and then Ekind
(Entity
(Selector_Name
(P
))) = E_Function
6099 if Is_Object_Reference
(P
) then
6100 Check_Object_Reference
(P
);
6102 elsif Is_Entity_Name
(P
)
6103 and then (Is_Type
(Entity
(P
))
6104 or else Ekind
(Entity
(P
)) = E_Enumeration_Literal
)
6108 elsif Nkind
(P
) = N_Type_Conversion
6109 and then not Comes_From_Source
(P
)
6113 -- Some other compilers allow dubious use of X'???'Size
6115 elsif Relaxed_RM_Semantics
6116 and then Nkind
(P
) = N_Attribute_Reference
6121 Error_Attr_P
("invalid prefix for % attribute");
6124 Check_Not_Incomplete_Type
;
6126 Set_Etype
(N
, Universal_Integer
);
6128 -- If we are processing pragmas Compile_Time_Warning and Compile_
6129 -- Time_Errors after the back end has been called and this occurrence
6130 -- of 'Size is known at compile time then it is safe to perform this
6131 -- evaluation. Needed to perform the static evaluation of the full
6132 -- boolean expression of these pragmas. Note that Known_RM_Size is
6133 -- sometimes True when Size_Known_At_Compile_Time is False, when the
6134 -- back end has computed it.
6136 if In_Compile_Time_Warning_Or_Error
6137 and then Is_Entity_Name
(P
)
6138 and then (Is_Type
(Entity
(P
))
6139 or else Ekind
(Entity
(P
)) = E_Enumeration_Literal
)
6140 and then (Known_RM_Size
(Entity
(P
))
6141 or else Size_Known_At_Compile_Time
(Entity
(P
)))
6147 if Known_Static_RM_Size
(Entity
(P
)) then
6148 Siz
:= RM_Size
(Entity
(P
));
6150 Siz
:= Esize
(Entity
(P
));
6153 Rewrite
(N
, Make_Integer_Literal
(Sloc
(N
), Siz
));
6162 -- Shares processing with Large attribute
6164 ---------------------------------------
6165 -- Small_Denominator/Small_Numerator --
6166 ---------------------------------------
6168 when Attribute_Small_Denominator
6169 | Attribute_Small_Numerator
6171 Check_Fixed_Point_Type_0
;
6172 Set_Etype
(N
, Universal_Integer
);
6178 when Attribute_Storage_Pool
6179 | Attribute_Simple_Storage_Pool
6183 if Is_Access_Type
(P_Type
) then
6184 if Ekind
(P_Type
) = E_Access_Subprogram_Type
then
6186 ("cannot use % attribute for access-to-subprogram type");
6189 -- Set appropriate entity
6191 if Present
(Associated_Storage_Pool
(Root_Type
(P_Type
))) then
6192 Set_Entity
(N
, Associated_Storage_Pool
(Root_Type
(P_Type
)));
6194 Set_Entity
(N
, RTE
(RE_Global_Pool_Object
));
6197 if Attr_Id
= Attribute_Storage_Pool
then
6198 if Present
(Get_Rep_Pragma
(Etype
(Entity
(N
)),
6199 Name_Simple_Storage_Pool_Type
))
6201 Error_Msg_Name_1
:= Aname
;
6202 Error_Msg_Warn
:= SPARK_Mode
/= On
;
6204 ("cannot use % attribute for type with simple storage "
6206 Error_Msg_N
("\Program_Error [<<", N
);
6209 (N
, Make_Raise_Program_Error
6210 (Sloc
(N
), Reason
=> PE_Explicit_Raise
));
6213 Set_Etype
(N
, Class_Wide_Type
(RTE
(RE_Root_Storage_Pool
)));
6215 -- In the Simple_Storage_Pool case, verify that the pool entity is
6216 -- actually of a simple storage pool type, and set the attribute's
6217 -- type to the pool object's type.
6220 if not Present
(Get_Rep_Pragma
(Etype
(Entity
(N
)),
6221 Name_Simple_Storage_Pool_Type
))
6224 ("cannot use % attribute for type without simple " &
6228 Set_Etype
(N
, Etype
(Entity
(N
)));
6231 -- Validate_Remote_Access_To_Class_Wide_Type for attribute
6232 -- Storage_Pool since this attribute is not defined for such
6233 -- types (RM E.2.2(17)).
6235 Validate_Remote_Access_To_Class_Wide_Type
(N
);
6238 Error_Attr_P
("prefix of % attribute must be access type");
6245 when Attribute_Storage_Size
=>
6248 if Is_Task_Type
(P_Type
) then
6249 Set_Etype
(N
, Universal_Integer
);
6251 -- Use with tasks is an obsolescent feature
6253 Check_Restriction
(No_Obsolescent_Features
, P
);
6255 elsif Is_Access_Type
(P_Type
) then
6256 Set_Etype
(N
, Universal_Integer
);
6258 if Ekind
(P_Type
) = E_Access_Subprogram_Type
then
6260 ("cannot use % attribute for access-to-subprogram type");
6263 if Is_Entity_Name
(P
)
6264 and then Is_Type
(Entity
(P
))
6268 -- Validate_Remote_Access_To_Class_Wide_Type for attribute
6269 -- Storage_Size since this attribute is not defined for
6270 -- such types (RM E.2.2(17)).
6272 Validate_Remote_Access_To_Class_Wide_Type
(N
);
6274 -- The prefix is allowed to be an implicit dereference of an
6275 -- access value designating a task.
6282 Error_Attr_P
("prefix of % attribute must be access or task type");
6289 when Attribute_Storage_Unit
=>
6290 Standard_Attribute
(Ttypes
.System_Storage_Unit
);
6296 when Attribute_Stream_Size
=>
6300 if Is_Entity_Name
(P
)
6301 and then Is_Elementary_Type
(Entity
(P
))
6303 Set_Etype
(N
, Universal_Integer
);
6305 Error_Attr_P
("invalid prefix for % attribute");
6312 when Attribute_Stub_Type
=>
6316 if Is_Remote_Access_To_Class_Wide_Type
(Base_Type
(P_Type
)) then
6318 -- For a real RACW [sub]type, use corresponding stub type
6320 if not Is_Generic_Type
(P_Type
) then
6323 (Corresponding_Stub_Type
(Base_Type
(P_Type
)), Loc
));
6325 -- For a generic type (that has been marked as an RACW using the
6326 -- Remote_Access_Type aspect or pragma), use a generic RACW stub
6327 -- type. Note that if the actual is not a remote access type, the
6328 -- instantiation will fail.
6331 -- Note: we go to the underlying type here because the view
6332 -- returned by RTE (RE_RACW_Stub_Type) might be incomplete.
6336 (Underlying_Type
(RTE
(RE_RACW_Stub_Type
)), Loc
));
6341 ("prefix of% attribute must be remote access-to-class-wide");
6348 -- Shares processing with Pred attribute
6350 --------------------------------
6351 -- System_Allocator_Alignment --
6352 --------------------------------
6354 when Attribute_System_Allocator_Alignment
=>
6355 Standard_Attribute
(Ttypes
.System_Allocator_Alignment
);
6361 when Attribute_Tag
=>
6365 if not Is_Tagged_Type
(P_Type
) then
6366 Error_Attr_P
("prefix of % attribute must be tagged");
6368 -- Next test does not apply to generated code why not, and what does
6369 -- the illegal reference mean???
6371 elsif Is_Object_Reference
(P
)
6372 and then not Is_Class_Wide_Type
(P_Type
)
6373 and then Comes_From_Source
(N
)
6376 ("% attribute can only be applied to objects " &
6377 "of class-wide type");
6380 -- The prefix cannot be an incomplete type. However, references to
6381 -- 'Tag can be generated when expanding interface conversions, and
6384 if Comes_From_Source
(N
) then
6385 Check_Not_Incomplete_Type
;
6387 -- 'Tag requires visibility on the corresponding package holding
6388 -- the tag, so record a reference here, to avoid spurious unused
6389 -- with_clause reported when compiling the main unit.
6391 if In_Extended_Main_Source_Unit
(Current_Scope
) then
6392 Set_Referenced
(P_Type
, True);
6393 Set_Referenced
(Scope
(P_Type
), True);
6397 -- Set appropriate type
6399 Set_Etype
(N
, RTE
(RE_Tag
));
6405 when Attribute_Target_Name
=> Target_Name
: declare
6406 TN
: constant String := Sdefault
.Target_Name
.all;
6410 Check_Standard_Prefix
;
6414 if TN
(TL
) = '/' or else TN
(TL
) = '\' then
6419 Make_String_Literal
(Loc
,
6420 Strval
=> TN
(TN
'First .. TL
)));
6421 Analyze_And_Resolve
(N
, Standard_String
);
6422 Set_Is_Static_Expression
(N
, True);
6429 -- Shares processing with Callable attribute
6435 when Attribute_To_Address
=> To_Address
: declare
6440 Check_System_Prefix
;
6442 Generate_Reference
(RTE
(RE_Address
), P
);
6443 Analyze_And_Resolve
(E1
, Any_Integer
);
6444 Set_Etype
(N
, RTE
(RE_Address
));
6445 Set_Is_Static_Expression
(N
, Is_Static_Expression
(E1
));
6447 -- OK static expression case, check range and set appropriate type
6449 if Is_OK_Static_Expression
(E1
) then
6450 Val
:= Expr_Value
(E1
);
6452 if Val
< -(Uint_2
** (System_Address_Size
- 1))
6454 Val
> Uint_2
** System_Address_Size
- 1
6456 Error_Attr
("address value out of range for % attribute", E1
);
6459 -- In most cases the expression is a numeric literal or some other
6460 -- address expression, but if it is a declared constant it may be
6461 -- of a compatible type that must be left on the node.
6463 if Is_Entity_Name
(E1
) then
6466 -- Set type to universal integer if negative
6469 Set_Etype
(E1
, Universal_Integer
);
6471 -- Otherwise set type to Unsigned_64 to accommodate large values
6474 Set_Etype
(E1
, Standard_Unsigned_64
);
6483 when Attribute_To_Any
=>
6485 Check_PolyORB_Attribute
;
6486 Set_Etype
(N
, RTE
(RE_Any
));
6492 -- Shares processing with Ceiling attribute
6498 when Attribute_Type_Class
=>
6501 Check_Not_Incomplete_Type
;
6502 Set_Etype
(N
, RTE
(RE_Type_Class
));
6508 when Attribute_TypeCode
=>
6510 Check_PolyORB_Attribute
;
6511 Set_Etype
(N
, RTE
(RE_TypeCode
));
6517 when Attribute_Type_Key
=> Type_Key
: declare
6518 Full_Name
: constant String_Id
:=
6519 Fully_Qualified_Name_String
(Entity
(P
));
6522 -- The computed signature for the type
6525 -- To simplify the handling of mutually recursive types, follow a
6526 -- single dereference link in a composite type.
6528 procedure Compute_Type_Key
(T
: Entity_Id
);
6529 -- Create a CRC integer from the declaration of the type. For a
6530 -- composite type, fold in the representation of its components in
6531 -- recursive fashion. We use directly the source representation of
6532 -- the types involved.
6534 ----------------------
6535 -- Compute_Type_Key --
6536 ----------------------
6538 procedure Compute_Type_Key
(T
: Entity_Id
) is
6539 Buffer
: Source_Buffer_Ptr
;
6543 SFI
: Source_File_Index
;
6545 procedure Process_One_Declaration
;
6546 -- Update CRC with the characters of one type declaration, or a
6547 -- representation pragma that applies to the type.
6549 -----------------------------
6550 -- Process_One_Declaration --
6551 -----------------------------
6553 procedure Process_One_Declaration
is
6555 -- Scan type declaration, skipping blanks
6557 for Ptr
in P_Min
.. P_Max
loop
6558 if Buffer
(Ptr
) /= ' ' then
6559 System
.CRC32
.Update
(CRC
, Buffer
(Ptr
));
6562 end Process_One_Declaration
;
6564 -- Start of processing for Compute_Type_Key
6567 if Is_Itype
(T
) then
6571 -- If the type is declared in Standard, there is no source, so
6572 -- just use its name.
6574 if Scope
(T
) = Standard_Standard
then
6576 Name
: constant String := Get_Name_String
(Chars
(T
));
6578 for J
in Name
'Range loop
6579 System
.CRC32
.Update
(CRC
, Name
(J
));
6586 Sloc_Range
(Enclosing_Declaration
(T
), P_Min
, P_Max
);
6587 SFI
:= Get_Source_File_Index
(P_Min
);
6588 pragma Assert
(SFI
= Get_Source_File_Index
(P_Max
));
6589 Buffer
:= Source_Text
(SFI
);
6591 Process_One_Declaration
;
6593 -- Recurse on relevant component types
6595 if Is_Array_Type
(T
) then
6596 Compute_Type_Key
(Component_Type
(T
));
6598 elsif Is_Access_Type
(T
) then
6601 Compute_Type_Key
(Designated_Type
(T
));
6604 elsif Is_Derived_Type
(T
) then
6605 Compute_Type_Key
(Etype
(T
));
6607 elsif Is_Record_Type
(T
) then
6611 Comp
:= First_Component
(T
);
6612 while Present
(Comp
) loop
6613 Compute_Type_Key
(Etype
(Comp
));
6614 Next_Component
(Comp
);
6619 if Is_First_Subtype
(T
) then
6621 -- Fold in representation aspects for the type, which appear in
6622 -- the same source buffer. If the representation aspects are in
6623 -- a different source file, then skip them; they apply to some
6624 -- other type, perhaps one we're derived from.
6626 Rep
:= First_Rep_Item
(T
);
6628 while Present
(Rep
) loop
6629 if Comes_From_Source
(Rep
) then
6630 Sloc_Range
(Rep
, P_Min
, P_Max
);
6632 if SFI
= Get_Source_File_Index
(P_Min
) then
6633 pragma Assert
(SFI
= Get_Source_File_Index
(P_Max
));
6634 Process_One_Declaration
;
6638 Next_Rep_Item
(Rep
);
6641 end Compute_Type_Key
;
6643 -- Start of processing for Type_Key
6652 -- Copy all characters in Full_Name but the trailing NUL
6654 for J
in 1 .. String_Length
(Full_Name
) - 1 loop
6655 Store_String_Char
(Get_String_Char
(Full_Name
, Pos
(J
)));
6658 -- Compute CRC and convert it to string one character at a time, so
6659 -- as not to use Image within the compiler.
6662 Compute_Type_Key
(Entity
(P
));
6664 if not Is_Frozen
(Entity
(P
))
6665 and then not Is_Generic_Type
(Entity
(P
))
6667 Error_Msg_N
("premature usage of Type_Key?", N
);
6671 Store_String_Char
(Character'Val (48 + (CRC
rem 10)));
6675 Rewrite
(N
, Make_String_Literal
(Loc
, End_String
));
6676 Analyze_And_Resolve
(N
, Standard_String
);
6679 -----------------------
6680 -- Unbiased_Rounding --
6681 -----------------------
6683 -- Shares processing with Ceiling attribute
6685 ----------------------
6686 -- Unchecked_Access --
6687 ----------------------
6689 when Attribute_Unchecked_Access
=>
6690 if Comes_From_Source
(N
) then
6691 Check_Restriction
(No_Unchecked_Access
, N
);
6694 Analyze_Access_Attribute
;
6695 Check_Not_Incomplete_Type
;
6697 -------------------------
6698 -- Unconstrained_Array --
6699 -------------------------
6701 when Attribute_Unconstrained_Array
=>
6704 Check_Not_Incomplete_Type
;
6705 Set_Etype
(N
, Standard_Boolean
);
6706 Set_Is_Static_Expression
(N
, True);
6708 ------------------------------
6709 -- Universal_Literal_String --
6710 ------------------------------
6712 -- This is a GNAT specific attribute whose prefix must be a named
6713 -- number where the expression is either a single numeric literal,
6714 -- or a numeric literal immediately preceded by a minus sign. The
6715 -- result is equivalent to a string literal containing the text of
6716 -- the literal as it appeared in the source program with a possible
6717 -- leading minus sign.
6719 when Attribute_Universal_Literal_String
=>
6722 if not Is_Entity_Name
(P
)
6723 or else not Is_Named_Number
(Entity
(P
))
6725 Error_Attr_P
("prefix for % attribute must be named number");
6732 Src
: Source_Buffer_Ptr
;
6735 Expr
:= Original_Node
(Expression
(Parent
(Entity
(P
))));
6737 if Nkind
(Expr
) = N_Op_Minus
then
6739 Expr
:= Original_Node
(Right_Opnd
(Expr
));
6744 if Nkind
(Expr
) not in N_Integer_Literal | N_Real_Literal
then
6746 ("named number for % attribute must be simple literal", N
);
6749 -- Build string literal corresponding to source literal text
6754 Store_String_Char
(Get_Char_Code
('-'));
6758 Src
:= Source_Text
(Get_Source_File_Index
(S
));
6760 while Src
(S
) /= ';' and then Src
(S
) /= ' ' loop
6761 Store_String_Char
(Get_Char_Code
(Src
(S
)));
6765 -- Now we rewrite the attribute with the string literal
6768 Make_String_Literal
(Loc
, End_String
));
6770 Set_Is_Static_Expression
(N
, True);
6774 -------------------------
6775 -- Unrestricted_Access --
6776 -------------------------
6778 -- This is a GNAT specific attribute which is like Access except that
6779 -- all scope checks and checks for aliased views are omitted. It is
6780 -- documented as being equivalent to the use of the Address attribute
6781 -- followed by an unchecked conversion to the target access type.
6783 when Attribute_Unrestricted_Access
=>
6785 -- If from source, deal with relevant restrictions
6787 if Comes_From_Source
(N
) then
6788 Check_Restriction
(No_Unchecked_Access
, N
);
6790 if Nkind
(P
) in N_Has_Entity
6791 and then Present
(Entity
(P
))
6792 and then Is_Object
(Entity
(P
))
6794 Check_Restriction
(No_Implicit_Aliasing
, N
);
6798 if Is_Entity_Name
(P
) then
6799 Set_Address_Taken
(Entity
(P
));
6802 -- It might seem reasonable to call Address_Checks here to apply the
6803 -- same set of semantic checks that we enforce for 'Address (after
6804 -- all we document Unrestricted_Access as being equivalent to the
6805 -- use of Address followed by an Unchecked_Conversion). However, if
6806 -- we do enable these checks, we get multiple failures in both the
6807 -- compiler run-time and in our regression test suite, so we leave
6808 -- out these checks for now. To be investigated further some time???
6812 -- Now complete analysis using common access processing
6814 Analyze_Access_Attribute
;
6820 when Attribute_Update
=> Update
: declare
6821 Common_Typ
: Entity_Id
;
6822 -- The common type of a multiple component update for a record
6824 Comps
: Elist_Id
:= No_Elist
;
6825 -- A list used in the resolution of a record update. It contains the
6826 -- entities of all record components processed so far.
6828 procedure Analyze_Array_Component_Update
(Assoc
: Node_Id
);
6829 -- Analyze and resolve array_component_association Assoc against the
6830 -- index of array type P_Type.
6832 procedure Analyze_Record_Component_Update
(Comp
: Node_Id
);
6833 -- Analyze and resolve record_component_association Comp against
6834 -- record type P_Type.
6836 ------------------------------------
6837 -- Analyze_Array_Component_Update --
6838 ------------------------------------
6840 procedure Analyze_Array_Component_Update
(Assoc
: Node_Id
) is
6844 Index_Typ
: Entity_Id
;
6848 -- The current association contains a sequence of indexes denoting
6849 -- an element of a multidimensional array:
6851 -- (Index_1, ..., Index_N)
6853 -- Examine each individual index and resolve it against the proper
6854 -- index type of the array.
6856 if Nkind
(First
(Choices
(Assoc
))) = N_Aggregate
then
6857 Expr
:= First
(Choices
(Assoc
));
6858 while Present
(Expr
) loop
6860 -- The use of others is illegal (SPARK RM 4.4.1(12))
6862 if Nkind
(Expr
) = N_Others_Choice
then
6864 ("OTHERS choice not allowed in attribute %", Expr
);
6866 -- Otherwise analyze and resolve all indexes
6869 Index
:= First
(Expressions
(Expr
));
6870 Index_Typ
:= First_Index
(P_Type
);
6871 while Present
(Index
) and then Present
(Index_Typ
) loop
6872 Analyze_And_Resolve
(Index
, Etype
(Index_Typ
));
6874 Next_Index
(Index_Typ
);
6877 -- Detect a case where the association either lacks an
6878 -- index or contains an extra index.
6880 if Present
(Index
) or else Present
(Index_Typ
) then
6882 ("dimension mismatch in index list", Assoc
);
6889 -- The current association denotes either a single component or a
6890 -- range of components of a one dimensional array:
6894 -- Resolve the index or its high and low bounds (if range) against
6895 -- the proper index type of the array.
6898 Index
:= First
(Choices
(Assoc
));
6899 Index_Typ
:= First_Index
(P_Type
);
6901 if Present
(Next_Index
(Index_Typ
)) then
6902 Error_Msg_N
("too few subscripts in array reference", Assoc
);
6905 while Present
(Index
) loop
6907 -- The use of others is illegal (SPARK RM 4.4.1(12))
6909 if Nkind
(Index
) = N_Others_Choice
then
6911 ("OTHERS choice not allowed in attribute %", Index
);
6913 -- The index denotes a range of elements
6915 elsif Nkind
(Index
) = N_Range
then
6916 Low
:= Low_Bound
(Index
);
6917 High
:= High_Bound
(Index
);
6919 Analyze_And_Resolve
(Low
, Etype
(Index_Typ
));
6920 Analyze_And_Resolve
(High
, Etype
(Index_Typ
));
6922 -- Otherwise the index denotes a single element
6925 Analyze_And_Resolve
(Index
, Etype
(Index_Typ
));
6931 end Analyze_Array_Component_Update
;
6933 -------------------------------------
6934 -- Analyze_Record_Component_Update --
6935 -------------------------------------
6937 procedure Analyze_Record_Component_Update
(Comp
: Node_Id
) is
6938 Comp_Name
: constant Name_Id
:= Chars
(Comp
);
6939 Base_Typ
: Entity_Id
;
6940 Comp_Or_Discr
: Entity_Id
;
6943 -- Find the discriminant or component whose name corresponds to
6944 -- Comp. A simple character comparison is sufficient because all
6945 -- visible names within a record type are unique.
6947 Comp_Or_Discr
:= First_Entity
(P_Type
);
6948 while Present
(Comp_Or_Discr
) loop
6949 if Chars
(Comp_Or_Discr
) = Comp_Name
then
6951 -- Decorate the component reference by setting its entity
6952 -- and type for resolution purposes.
6954 Set_Entity
(Comp
, Comp_Or_Discr
);
6955 Set_Etype
(Comp
, Etype
(Comp_Or_Discr
));
6959 Next_Entity
(Comp_Or_Discr
);
6962 -- Diagnose an illegal reference
6964 if Present
(Comp_Or_Discr
) then
6965 if Ekind
(Comp_Or_Discr
) = E_Discriminant
then
6967 ("attribute % may not modify record discriminants", Comp
);
6969 else pragma Assert
(Ekind
(Comp_Or_Discr
) = E_Component
);
6970 if Contains
(Comps
, Comp_Or_Discr
) then
6971 Error_Msg_N
("component & already updated", Comp
);
6973 -- Mark this component as processed
6976 Append_New_Elmt
(Comp_Or_Discr
, Comps
);
6980 -- The update aggregate mentions an entity that does not belong to
6984 Error_Msg_N
("& is not a component of aggregate subtype", Comp
);
6987 -- Verify the consistency of types when the current component is
6988 -- part of a multiple component update.
6990 -- Comp_1 | ... | Comp_N => <value>
6992 if Present
(Etype
(Comp
)) then
6993 Base_Typ
:= Base_Type
(Etype
(Comp
));
6995 -- Save the type of the first component reference as the
6996 -- remaning references (if any) must resolve to this type.
6998 if No
(Common_Typ
) then
6999 Common_Typ
:= Base_Typ
;
7001 elsif Base_Typ
/= Common_Typ
then
7003 ("components in choice list must have same type", Comp
);
7006 end Analyze_Record_Component_Update
;
7013 -- Start of processing for Update
7016 if Warn_On_Obsolescent_Feature
then
7017 Error_Msg_N
("?j?attribute Update is an obsolescent feature", N
);
7018 Error_Msg_N
("\?j?use a delta aggregate instead", N
);
7023 if not Is_Object_Reference
(P
) then
7024 Error_Attr_P
("prefix of attribute % must denote an object");
7026 elsif not Is_Array_Type
(P_Type
)
7027 and then not Is_Record_Type
(P_Type
)
7029 Error_Attr_P
("prefix of attribute % must be a record or array");
7031 elsif Is_Limited_View
(P_Type
) then
7032 Error_Attr
("prefix of attribute % cannot be limited", N
);
7034 elsif Nkind
(E1
) /= N_Aggregate
then
7035 Error_Attr
("attribute % requires component association list", N
);
7037 elsif Present
(Expressions
(E1
)) then
7038 Error_Attr
("attribute % requires named component associations",
7039 First
(Expressions
(E1
)));
7043 -- Inspect the update aggregate, looking at all the associations and
7044 -- choices. Perform the following checks:
7046 -- 1) Legality of "others" in all cases
7047 -- 2) Legality of <>
7048 -- 3) Component legality for arrays
7049 -- 4) Component legality for records
7051 -- The remaining checks are performed on the expanded attribute
7053 Assoc
:= First
(Component_Associations
(E1
));
7054 while Present
(Assoc
) loop
7056 -- The use of <> is illegal (SPARK RM 4.4.1(1))
7058 if Box_Present
(Assoc
) then
7060 ("default initialization not allowed in attribute %", Assoc
);
7062 -- Otherwise process the association
7065 Analyze
(Expression
(Assoc
));
7067 if Is_Array_Type
(P_Type
) then
7068 Analyze_Array_Component_Update
(Assoc
);
7070 elsif Is_Record_Type
(P_Type
) then
7072 -- Reset the common type used in a multiple component update
7073 -- as we are processing the contents of a new association.
7075 Common_Typ
:= Empty
;
7077 Comp
:= First
(Choices
(Assoc
));
7078 while Present
(Comp
) loop
7079 if Nkind
(Comp
) = N_Identifier
then
7080 Analyze_Record_Component_Update
(Comp
);
7082 -- The use of others is illegal (SPARK RM 4.4.1(5))
7084 elsif Nkind
(Comp
) = N_Others_Choice
then
7086 ("OTHERS choice not allowed in attribute %", Comp
);
7088 -- The name of a record component cannot appear in any
7093 ("name should be identifier or OTHERS", Comp
);
7104 -- The type of attribute 'Update is that of the prefix
7106 Set_Etype
(N
, P_Type
);
7108 Sem_Warn
.Warn_On_Suspicious_Update
(N
);
7115 when Attribute_Val
=>
7117 Check_Discrete_Type
;
7119 -- Note, we need a range check in general, but we wait for the
7120 -- Resolve call to do this, since we want to let Eval_Attribute
7121 -- have a chance to find an static illegality first.
7123 Resolve
(E1
, Any_Integer
);
7124 Set_Etype
(N
, P_Base_Type
);
7130 when Attribute_Valid
=> Valid
: declare
7131 Pred_Func
: constant Entity_Id
:= Predicate_Function
(P_Type
);
7136 -- Ignore check for object if we have a 'Valid reference generated
7137 -- by the expanded code, since in some cases valid checks can occur
7138 -- on items that are names, but are not objects (e.g. attributes).
7140 if Comes_From_Source
(N
) then
7141 Check_Object_Reference
(P
);
7143 if not Is_Scalar_Type
(P_Type
) then
7144 Error_Attr_P
("object for % attribute must be of scalar type");
7147 -- If the attribute appears within the subtype's own predicate
7148 -- function, then issue a warning that this will cause infinite
7151 if Present
(Pred_Func
) and then Current_Scope
= Pred_Func
then
7152 Error_Msg_N
("attribute Valid requires a predicate check??", N
);
7153 Error_Msg_N
("\and will result in infinite recursion??", N
);
7157 Set_Etype
(N
, Standard_Boolean
);
7164 when Attribute_Valid_Value
=>
7166 Check_Enumeration_Type
;
7167 Check_Enum_Image
(Check_Enumeration_Maps
=> True);
7168 Set_Etype
(N
, Standard_Boolean
);
7169 Validate_Non_Static_Attribute_Function_Call
;
7171 if P_Type
in Standard_Boolean
7172 | Standard_Character
7173 | Standard_Wide_Character
7174 | Standard_Wide_Wide_Character
7177 ("prefix of % attribute must not be a type in Standard");
7180 if Discard_Names
(First_Subtype
(P_Type
)) then
7182 ("prefix of % attribute must not have Discard_Names");
7189 when Attribute_Valid_Scalars
=> Valid_Scalars
: declare
7193 if Comes_From_Source
(N
) then
7194 Check_Object_Reference
(P
);
7196 -- Do not emit any diagnostics related to private types to avoid
7197 -- disclosing the structure of the type.
7199 if Is_Private_Type
(P_Type
) then
7201 -- Attribute 'Valid_Scalars is not supported on private tagged
7202 -- types due to a code generation issue. Is_Visible_Component
7203 -- does not allow for a component of a private tagged type to
7204 -- be successfully retrieved.
7205 -- ??? This attribute should simply ignore type privacy
7206 -- (see Validated_View). It should examine components of the
7207 -- tagged type extensions (if any) and recursively examine
7208 -- 'Valid_Scalars of the parent's type (if any).
7210 -- Do not use Error_Attr_P because this bypasses any subsequent
7211 -- processing and leaves the attribute with type Any_Type. This
7212 -- in turn prevents the proper expansion of the attribute into
7215 if Is_Tagged_Type
(P_Type
) then
7216 Error_Msg_Name_1
:= Aname
;
7217 Error_Msg_N
("??effects of attribute % are ignored", N
);
7220 -- Otherwise the type is not private
7223 if not Scalar_Part_Present
(P_Type
) then
7224 Error_Msg_Name_1
:= Aname
;
7226 ("??attribute % always True, no scalars to check", P
);
7227 Set_Boolean_Result
(N
, True);
7230 -- Attribute 'Valid_Scalars is illegal on unchecked union types
7231 -- because it is not always guaranteed that the components are
7232 -- retrievable based on whether the discriminants are inferable
7234 if Has_Unchecked_Union
(P_Type
) then
7236 ("attribute % not allowed for Unchecked_Union type");
7241 Set_Etype
(N
, Standard_Boolean
);
7248 when Attribute_Value
7249 | Attribute_Wide_Value
7250 | Attribute_Wide_Wide_Value
7254 Check_Enum_Image
(Check_Enumeration_Maps
=> True);
7256 -- Set Etype before resolving expression because expansion of
7257 -- expression may require enclosing type. Note that the type
7258 -- returned by 'Value is the base type of the prefix type.
7260 Set_Etype
(N
, P_Base_Type
);
7261 Validate_Non_Static_Attribute_Function_Call
;
7263 -- Check restriction No_Fixed_IO
7265 if Restriction_Check_Required
(No_Fixed_IO
)
7266 and then Is_Fixed_Point_Type
(P_Type
)
7268 Check_Restriction
(No_Fixed_IO
, P
);
7275 -- Shares processing with Machine_Size attribute
7281 when Attribute_Version
=>
7284 Set_Etype
(N
, RTE
(RE_Version_String
));
7290 when Attribute_Wchar_T_Size
=>
7291 Standard_Attribute
(Interfaces_Wchar_T_Size
);
7297 when Attribute_Wide_Image
=>
7298 Analyze_Image_Attribute
(Standard_Wide_String
);
7300 ---------------------
7301 -- Wide_Wide_Image --
7302 ---------------------
7304 when Attribute_Wide_Wide_Image
=>
7305 Analyze_Image_Attribute
(Standard_Wide_Wide_String
);
7311 -- Shares processing with Value attribute
7313 ---------------------
7314 -- Wide_Wide_Value --
7315 ---------------------
7317 -- Shares processing with Value attribute
7319 ---------------------
7320 -- Wide_Wide_Width --
7321 ---------------------
7323 when Attribute_Wide_Wide_Width
7324 | Attribute_Wide_Width
7329 Set_Etype
(N
, Universal_Integer
);
7335 -- Shares processing with Wide_Wide_Width attribute
7341 -- Shares processing with Wide_Wide_Width attribute
7347 when Attribute_Word_Size
=>
7348 Standard_Attribute
(System_Word_Size
);
7354 when Attribute_Write
=>
7356 Check_Stream_Attribute
(TSS_Stream_Write
);
7357 Set_Etype
(N
, Standard_Void_Type
);
7358 Resolve
(N
, Standard_Void_Type
);
7362 -- In SPARK certain attributes (see below) depend on Tasking_State.
7363 -- Ensure that the entity is available for gnat2why by loading it.
7364 -- See SPARK RM 9(18) for the relevant rule.
7366 if GNATprove_Mode
then
7368 when Attribute_Callable
7371 | Attribute_Terminated
7373 SPARK_Implicit_Load
(RE_Tasking_State
);
7380 -- All errors raise Bad_Attribute, so that we get out before any further
7381 -- damage occurs when an error is detected (for example, if we check for
7382 -- one attribute expression, and the check succeeds, we want to be able
7383 -- to proceed securely assuming that an expression is in fact present.
7385 -- Note: we set the attribute analyzed in this case to prevent any
7386 -- attempt at reanalysis which could generate spurious error msgs.
7389 when Bad_Attribute
=>
7391 Set_Etype
(N
, Any_Type
);
7393 end Analyze_Attribute
;
7395 --------------------
7396 -- Eval_Attribute --
7397 --------------------
7399 procedure Eval_Attribute
(N
: Node_Id
) is
7400 Loc
: constant Source_Ptr
:= Sloc
(N
);
7402 C_Type
: constant Entity_Id
:= Etype
(N
);
7403 -- The type imposed by the context
7406 -- Attribute_Name (N) after verification of validity of N
7409 -- Get_Attribute_Id (Aname) after Aname is set
7412 -- Prefix (N) after verification of validity of N
7415 -- First expression, or Empty if none
7418 -- Second expression, or Empty if none
7420 P_Entity
: Entity_Id
;
7421 -- Entity denoted by prefix
7424 -- The type of the prefix
7426 P_Base_Type
: Entity_Id
;
7427 -- The base type of the prefix type
7429 P_Root_Type
: Entity_Id
;
7430 -- The root type of the prefix type
7432 Static
: Boolean := False;
7433 -- True if the result is Static. This is set by the general processing
7434 -- to true if the prefix is static, and all expressions are static. It
7435 -- can be reset as processing continues for particular attributes. This
7436 -- flag can still be True if the reference raises a constraint error.
7437 -- Is_Static_Expression (N) is set to follow this value as it is set
7438 -- and we could always reference this, but it is convenient to have a
7439 -- simple short name to use, since it is frequently referenced.
7441 Lo_Bound
, Hi_Bound
: Node_Id
;
7442 -- Expressions for low and high bounds of type or array index referenced
7443 -- by First, Last, or Length attribute for array, set by Set_Bounds.
7446 -- Constraint error node used if we have an attribute reference has
7447 -- an argument that raises a constraint error. In this case we replace
7448 -- the attribute with a raise constraint_error node. This is important
7449 -- processing, since otherwise gigi might see an attribute which it is
7450 -- unprepared to deal with.
7452 procedure Check_Concurrent_Discriminant
(Bound
: Node_Id
);
7453 -- If Bound is a reference to a discriminant of a task or protected type
7454 -- occurring within the object's body, rewrite attribute reference into
7455 -- a reference to the corresponding discriminal. Use for the expansion
7456 -- of checks against bounds of entry family index subtypes.
7458 procedure Check_Expressions
;
7459 -- In case where the attribute is not foldable, the expressions, if
7460 -- any, of the attribute, are in a non-static context. This procedure
7461 -- performs the required additional checks.
7463 function Compile_Time_Known_Bounds
(Typ
: Entity_Id
) return Boolean;
7464 -- Determines if the given type has compile time known bounds. Note
7465 -- that we enter the case statement even in cases where the prefix
7466 -- type does NOT have known bounds, so it is important to guard any
7467 -- attempt to evaluate both bounds with a call to this function.
7469 procedure Compile_Time_Known_Attribute
(N
: Node_Id
; Val
: Uint
);
7470 -- This procedure is called when the attribute N has a non-static
7471 -- but compile time known value given by Val. It includes the
7472 -- necessary checks for out of range values.
7474 function Fore_Value
return Nat
;
7475 -- Computes the Fore value for the current attribute prefix, which is
7476 -- known to be a static fixed-point type. Used by Fore and Width.
7478 function Mantissa
return Uint
;
7479 -- Returns the Mantissa value for the prefix type
7481 procedure Set_Bounds
;
7482 -- Used for First, Last and Length attributes applied to an array or
7483 -- array subtype. Sets the variables Lo_Bound and Hi_Bound to the low
7484 -- and high bound expressions for the index referenced by the attribute
7485 -- designator (i.e. the first index if no expression is present, and the
7486 -- N'th index if the value N is present as an expression). Also used for
7487 -- First and Last of scalar types and for First_Valid and Last_Valid.
7488 -- Static is reset to False if the type or index type is not statically
7491 -----------------------------------
7492 -- Check_Concurrent_Discriminant --
7493 -----------------------------------
7495 procedure Check_Concurrent_Discriminant
(Bound
: Node_Id
) is
7497 -- The concurrent (task or protected) type
7500 if Nkind
(Bound
) = N_Identifier
7501 and then Ekind
(Entity
(Bound
)) = E_Discriminant
7502 and then Is_Concurrent_Record_Type
(Scope
(Entity
(Bound
)))
7504 Tsk
:= Corresponding_Concurrent_Type
(Scope
(Entity
(Bound
)));
7506 if In_Open_Scopes
(Tsk
) and then Has_Completion
(Tsk
) then
7508 -- Find discriminant of original concurrent type, and use
7509 -- its current discriminal, which is the renaming within
7510 -- the task/protected body.
7514 (Find_Body_Discriminal
(Entity
(Bound
)), Loc
));
7517 end Check_Concurrent_Discriminant
;
7519 -----------------------
7520 -- Check_Expressions --
7521 -----------------------
7523 procedure Check_Expressions
is
7527 while Present
(E
) loop
7528 Check_Non_Static_Context
(E
);
7531 end Check_Expressions
;
7533 ----------------------------------
7534 -- Compile_Time_Known_Attribute --
7535 ----------------------------------
7537 procedure Compile_Time_Known_Attribute
(N
: Node_Id
; Val
: Uint
) is
7538 T
: constant Entity_Id
:= Etype
(N
);
7541 Fold_Uint
(N
, Val
, False);
7543 -- Check that result is in bounds of the type if it is static
7545 if Is_In_Range
(N
, T
, Assume_Valid
=> False) then
7548 elsif Is_Out_Of_Range
(N
, T
) then
7549 Apply_Compile_Time_Constraint_Error
7550 (N
, "value not in range of}??", CE_Range_Check_Failed
);
7552 elsif not Range_Checks_Suppressed
(T
) then
7553 Enable_Range_Check
(N
);
7556 Set_Do_Range_Check
(N
, False);
7558 end Compile_Time_Known_Attribute
;
7560 -------------------------------
7561 -- Compile_Time_Known_Bounds --
7562 -------------------------------
7564 function Compile_Time_Known_Bounds
(Typ
: Entity_Id
) return Boolean is
7567 Compile_Time_Known_Value
(Type_Low_Bound
(Typ
))
7569 Compile_Time_Known_Value
(Type_High_Bound
(Typ
));
7570 end Compile_Time_Known_Bounds
;
7576 -- Note that the Fore calculation is based on the actual values
7577 -- of the bounds, and does not take into account possible rounding.
7579 function Fore_Value
return Nat
is
7580 Lo
: constant Uint
:= Expr_Value
(Type_Low_Bound
(P_Type
));
7581 Hi
: constant Uint
:= Expr_Value
(Type_High_Bound
(P_Type
));
7582 Small
: constant Ureal
:= Small_Value
(P_Type
);
7583 Lo_Real
: constant Ureal
:= Lo
* Small
;
7584 Hi_Real
: constant Ureal
:= Hi
* Small
;
7589 -- Bounds are given in terms of small units, so first compute
7590 -- proper values as reals.
7592 T
:= UR_Max
(abs Lo_Real
, abs Hi_Real
);
7595 -- Loop to compute proper value if more than one digit required
7597 while T
>= Ureal_10
loop
7609 -- Table of mantissa values accessed by function Computed using
7612 -- T'Mantissa = integer next above (D * log(10)/log(2)) + 1)
7614 -- where D is T'Digits (RM83 3.5.7)
7616 Mantissa_Value
: constant array (Nat
range 1 .. 40) of Nat
:= (
7658 function Mantissa
return Uint
is
7661 UI_From_Int
(Mantissa_Value
(UI_To_Int
(Digits_Value
(P_Type
))));
7668 procedure Set_Bounds
is
7674 -- For a string literal subtype, we have to construct the bounds.
7675 -- Valid Ada code never applies attributes to string literals, but
7676 -- it is convenient to allow the expander to generate attribute
7677 -- references of this type (e.g. First and Last applied to a string
7680 -- Note that the whole point of the E_String_Literal_Subtype is to
7681 -- avoid this construction of bounds, but the cases in which we
7682 -- have to materialize them are rare enough that we don't worry.
7684 -- The low bound is simply the low bound of the base type. The
7685 -- high bound is computed from the length of the string and this
7688 if Ekind
(P_Type
) = E_String_Literal_Subtype
then
7689 Ityp
:= Etype
(First_Index
(Base_Type
(P_Type
)));
7690 Lo_Bound
:= Type_Low_Bound
(Ityp
);
7693 Make_Integer_Literal
(Sloc
(P
),
7695 Expr_Value
(Lo_Bound
) + String_Literal_Length
(P_Type
) - 1);
7697 Set_Parent
(Hi_Bound
, P
);
7698 Analyze_And_Resolve
(Hi_Bound
, Etype
(Lo_Bound
));
7701 -- For non-array case, just get bounds of scalar type
7703 elsif Is_Scalar_Type
(P_Type
) then
7706 -- For a fixed-point type, we must freeze to get the attributes
7707 -- of the fixed-point type set now so we can reference them.
7709 if Is_Fixed_Point_Type
(P_Type
)
7710 and then not Is_Frozen
(Base_Type
(P_Type
))
7711 and then Compile_Time_Known_Value
(Type_Low_Bound
(P_Type
))
7712 and then Compile_Time_Known_Value
(Type_High_Bound
(P_Type
))
7714 Freeze_Fixed_Point_Type
(Base_Type
(P_Type
));
7717 -- For array case, get type of proper index
7723 Ndim
:= UI_To_Int
(Expr_Value
(E1
));
7726 Indx
:= First_Index
(P_Type
);
7727 for J
in 1 .. Ndim
- 1 loop
7731 -- If no index type, get out (some other error occurred, and
7732 -- we don't have enough information to complete the job).
7740 Ityp
:= Etype
(Indx
);
7743 -- A discrete range in an index constraint is allowed to be a
7744 -- subtype indication. This is syntactically a pain, but should
7745 -- not propagate to the entity for the corresponding index subtype.
7746 -- After checking that the subtype indication is legal, the range
7747 -- of the subtype indication should be transfered to the entity.
7748 -- The attributes for the bounds should remain the simple retrievals
7749 -- that they are now.
7751 Lo_Bound
:= Type_Low_Bound
(Ityp
);
7752 Hi_Bound
:= Type_High_Bound
(Ityp
);
7754 -- If subtype is non-static, result is definitely non-static
7756 if not Is_Static_Subtype
(Ityp
) then
7758 Set_Is_Static_Expression
(N
, False);
7760 -- Subtype is static, does it raise CE?
7762 elsif not Is_OK_Static_Subtype
(Ityp
) then
7763 Set_Raises_Constraint_Error
(N
);
7767 -- Start of processing for Eval_Attribute
7770 -- Return immediately if e.g. N has been rewritten or is malformed due
7771 -- to previous errors.
7773 if Nkind
(N
) /= N_Attribute_Reference
then
7777 Aname
:= Attribute_Name
(N
);
7778 Id
:= Get_Attribute_Id
(Aname
);
7781 -- The To_Address attribute can be static, but it cannot be evaluated at
7782 -- compile time, so just return.
7784 if Id
= Attribute_To_Address
then
7788 -- Initialize result as non-static, will be reset if appropriate
7790 Set_Is_Static_Expression
(N
, False);
7792 -- Acquire first two expressions (at the moment, no attributes take more
7793 -- than two expressions in any case).
7795 if Present
(Expressions
(N
)) then
7796 E1
:= First
(Expressions
(N
));
7803 -- Special processing for Enabled attribute. This attribute has a very
7804 -- special prefix, and the easiest way to avoid lots of special checks
7805 -- to protect this special prefix from causing trouble is to deal with
7806 -- this attribute immediately and be done with it.
7808 if Id
= Attribute_Enabled
then
7810 -- We skip evaluation if the expander is not active. This is not just
7811 -- an optimization. It is of key importance that we not rewrite the
7812 -- attribute in a generic template, since we want to pick up the
7813 -- setting of the check in the instance.
7815 if not Inside_A_Generic
then
7817 C
: constant Check_Id
:= Get_Check_Id
(Chars
(P
));
7822 if C
in Predefined_Check_Id
then
7823 R
:= Scope_Suppress
.Suppress
(C
);
7825 R
:= Is_Check_Suppressed
(Empty
, C
);
7829 R
:= Is_Check_Suppressed
(Entity
(E1
), C
);
7832 Rewrite
(N
, New_Occurrence_Of
(Boolean_Literals
(not R
), Loc
));
7839 -- Attribute 'Img applied to a static enumeration value is static, and
7840 -- we will do the folding right here (things get confused if we let this
7841 -- case go through the normal circuitry).
7843 if Id
= Attribute_Img
7844 and then Is_Entity_Name
(P
)
7845 and then Is_Enumeration_Type
(Etype
(Entity
(P
)))
7846 and then Is_OK_Static_Expression
(P
)
7849 Lit
: constant Entity_Id
:= Expr_Value_E
(P
);
7854 Get_Unqualified_Decoded_Name_String
(Chars
(Lit
));
7855 Set_Casing
(All_Upper_Case
);
7856 Store_String_Chars
(Name_Buffer
(1 .. Name_Len
));
7859 Rewrite
(N
, Make_String_Literal
(Loc
, Strval
=> Str
));
7860 Analyze_And_Resolve
(N
, Standard_String
);
7861 Set_Is_Static_Expression
(N
, True);
7867 -- Special processing for cases where the prefix is an object or value,
7868 -- including string literals (attributes of string literals can only
7869 -- appear in generated code) and current instance prefixes in type or
7872 if Is_Object_Reference
(P
)
7873 or else Is_Current_Instance_Reference_In_Type_Aspect
(P
)
7874 or else Nkind
(P
) = N_String_Literal
7875 or else (Is_Entity_Name
(P
)
7876 and then Ekind
(Entity
(P
)) = E_Enumeration_Literal
)
7878 -- For Alignment, give alignment of object if available, otherwise we
7879 -- cannot fold Alignment.
7881 if Id
= Attribute_Alignment
then
7882 if Is_Entity_Name
(P
) and then Known_Alignment
(Entity
(P
)) then
7883 Compile_Time_Known_Attribute
(N
, Alignment
(Entity
(P
)));
7890 -- For Component_Size, the prefix is an array object, and we apply
7891 -- the attribute to the type of the object. This is allowed for both
7892 -- unconstrained and constrained arrays, since the bounds have no
7893 -- influence on the value of this attribute.
7895 elsif Id
= Attribute_Component_Size
then
7896 P_Entity
:= Etype
(P
);
7898 -- For Enum_Rep, evaluation depends on the nature of the prefix and
7899 -- the optional argument.
7901 elsif Id
= Attribute_Enum_Rep
then
7902 if Is_Entity_Name
(P
) then
7905 Enum_Expr
: Node_Id
;
7906 -- The enumeration-type expression of interest
7911 if Ekind
(Entity
(P
)) in E_Constant | E_Enumeration_Literal
7915 -- Enum_Type'Enum_Rep (E1) case
7917 elsif Is_Enumeration_Type
(Entity
(P
)) then
7920 -- Otherwise the attribute must be expanded into a
7921 -- conversion and evaluated at run time.
7928 -- We can fold if the expression is an enumeration
7929 -- literal, or if it denotes a constant whose value
7930 -- is known at compile time.
7932 if Nkind
(Enum_Expr
) in N_Has_Entity
7933 and then (Ekind
(Entity
(Enum_Expr
)) =
7934 E_Enumeration_Literal
7936 (Ekind
(Entity
(Enum_Expr
)) = E_Constant
7937 and then Nkind
(Parent
(Entity
(Enum_Expr
))) =
7938 N_Object_Declaration
7940 (Expression
(Parent
(Entity
(P
))))
7941 and then Compile_Time_Known_Value
7942 (Expression
(Parent
(Entity
(P
))))))
7944 P_Entity
:= Etype
(P
);
7951 -- Otherwise the attribute is illegal, do not attempt to perform
7952 -- any kind of folding.
7958 -- For Bit_Position, give Component_Bit_Offset of object if available
7959 -- otherwise we cannot fold Bit_Position. Note that the attribute can
7960 -- be applied to a naked record component in generated code, in which
7961 -- case the prefix is an identifier that references the component or
7962 -- discriminant entity.
7964 elsif Id
= Attribute_Bit_Position
then
7969 if Is_Entity_Name
(P
) then
7972 CE
:= Entity
(Selector_Name
(P
));
7975 if Known_Static_Component_Bit_Offset
(CE
) then
7976 Compile_Time_Known_Attribute
7977 (N
, Component_Bit_Offset
(CE
));
7985 -- For Position, in Ada 2005 (or later) if we have the non-default
7986 -- bit order, we return the original value as given in the component
7987 -- clause (RM 2005 13.5.2(3/2)). Otherwise (Ada 83/95, or later with
7988 -- default bit order) return the value if it is known statically.
7990 elsif Id
= Attribute_Position
then
7992 CE
: constant Entity_Id
:= Entity
(Selector_Name
(P
));
7995 if Present
(Component_Clause
(CE
))
7996 and then Ada_Version
>= Ada_2005
7997 and then Reverse_Bit_Order
(Scope
(CE
))
7999 Compile_Time_Known_Attribute
8000 (N
, Expr_Value
(Position
(Component_Clause
(CE
))));
8002 elsif Known_Static_Component_Bit_Offset
(CE
) then
8003 Compile_Time_Known_Attribute
8004 (N
, Component_Bit_Offset
(CE
) / System_Storage_Unit
);
8013 -- For First_Bit, in Ada 2005 (or later) if we have the non-default
8014 -- bit order, we return the original value as given in the component
8015 -- clause (RM 2005 13.5.2(3/2)). Otherwise (Ada 83/95, or later with
8016 -- default bit order) return the value if it is known statically.
8018 elsif Id
= Attribute_First_Bit
then
8020 CE
: constant Entity_Id
:= Entity
(Selector_Name
(P
));
8023 if Present
(Component_Clause
(CE
))
8024 and then Ada_Version
>= Ada_2005
8025 and then Reverse_Bit_Order
(Scope
(CE
))
8027 Compile_Time_Known_Attribute
8028 (N
, Expr_Value
(First_Bit
(Component_Clause
(CE
))));
8030 elsif Known_Static_Component_Bit_Offset
(CE
) then
8031 Compile_Time_Known_Attribute
8032 (N
, Component_Bit_Offset
(CE
) mod System_Storage_Unit
);
8041 -- For Last_Bit, in Ada 2005 (or later) if we have the non-default
8042 -- bit order, we return the original value as given in the component
8043 -- clause (RM 2005 13.5.2(3/2)). Otherwise (Ada 83/95, or later with
8044 -- default bit order) return the value if it is known statically.
8046 elsif Id
= Attribute_Last_Bit
then
8048 CE
: constant Entity_Id
:= Entity
(Selector_Name
(P
));
8051 if Present
(Component_Clause
(CE
))
8052 and then Ada_Version
>= Ada_2005
8053 and then Reverse_Bit_Order
(Scope
(CE
))
8055 Compile_Time_Known_Attribute
8056 (N
, Expr_Value
(Last_Bit
(Component_Clause
(CE
))));
8058 elsif Known_Static_Component_Bit_Offset
(CE
)
8059 and then Known_Static_Esize
(CE
)
8061 Compile_Time_Known_Attribute
8062 (N
, (Component_Bit_Offset
(CE
) mod System_Storage_Unit
)
8071 -- For First, Last and Length, the prefix is an array object, and we
8072 -- apply the attribute to its type, but we need a constrained type
8073 -- for this, so we use the actual subtype if available.
8075 elsif Id
= Attribute_First
8076 or else Id
= Attribute_Last
8077 or else Id
= Attribute_Length
8080 AS
: constant Entity_Id
:= Get_Actual_Subtype_If_Available
(P
);
8083 if Present
(AS
) and then Is_Constrained
(AS
) then
8086 -- If we have an unconstrained type we cannot fold
8094 elsif Id
= Attribute_Size
then
8095 -- For Enum_Lit'Size, use Enum_Type'Object_Size. Taking the 'Size
8096 -- of a literal is kind of a strange thing to do, so we don't want
8097 -- to pass this oddity on to the back end. Note that Etype of an
8098 -- enumeration literal is always a (base) type, never a
8099 -- constrained subtype, so the Esize is always known.
8101 if Is_Entity_Name
(P
)
8102 and then Ekind
(Entity
(P
)) = E_Enumeration_Literal
8104 pragma Assert
(Known_Static_Esize
(Etype
(P
)));
8105 Compile_Time_Known_Attribute
(N
, Esize
(Etype
(P
)));
8107 -- Otherwise, if Size is available, use that
8109 elsif Is_Entity_Name
(P
) and then Known_Static_Esize
(Entity
(P
))
8111 Compile_Time_Known_Attribute
(N
, Esize
(Entity
(P
)));
8113 -- Otherwise, we cannot fold
8121 -- For Lock_Free, we apply the attribute to the type of the object.
8122 -- This is allowed since we have already verified that the type is a
8125 elsif Id
= Attribute_Lock_Free
then
8126 P_Entity
:= Etype
(P
);
8128 -- No other attributes for objects are folded
8135 -- Cases where P is not an object. Cannot do anything if P is not the
8136 -- name of an entity.
8138 elsif not Is_Entity_Name
(P
) then
8142 -- Otherwise get prefix entity
8145 P_Entity
:= Entity
(P
);
8148 -- If we are asked to evaluate an attribute where the prefix is a
8149 -- non-frozen generic actual type whose RM_Size has not been set,
8150 -- then abandon the effort.
8152 if Is_Type
(P_Entity
)
8153 and then (not Is_Frozen
(P_Entity
)
8154 and then Is_Generic_Actual_Type
(P_Entity
)
8155 and then not Known_RM_Size
(P_Entity
))
8157 -- However, the attribute Unconstrained_Array must be evaluated,
8158 -- since it is documented to be a static attribute (and can for
8159 -- example appear in a Compile_Time_Warning pragma). The frozen
8160 -- status of the type does not affect its evaluation.
8162 and then Id
/= Attribute_Unconstrained_Array
8167 -- At this stage P_Entity is the entity to which the attribute
8168 -- is to be applied. This is usually simply the entity of the
8169 -- prefix, except in some cases of attributes for objects, where
8170 -- as described above, we apply the attribute to the object type.
8172 -- Here is where we make sure that static attributes are properly
8173 -- marked as such. These are attributes whose prefix is a static
8174 -- scalar subtype, whose result is scalar, and whose arguments, if
8175 -- present, are static scalar expressions. Note that such references
8176 -- are static expressions even if they raise Constraint_Error.
8178 -- For example, Boolean'Pos (1/0 = 0) is a static expression, even
8179 -- though evaluating it raises constraint error. This means that a
8180 -- declaration like:
8182 -- X : constant := (if True then 1 else Boolean'Pos (1/0 = 0));
8184 -- is legal, since here this expression appears in a statically
8185 -- unevaluated position, so it does not actually raise an exception.
8187 -- T'Descriptor_Size is never static, even if T is static.
8189 if Is_Scalar_Type
(P_Entity
)
8190 and then not Is_Generic_Type
(P_Entity
)
8191 and then Is_Static_Subtype
(P_Entity
)
8192 and then Is_Scalar_Type
(Etype
(N
))
8195 or else (Is_Static_Expression
(E1
)
8196 and then Is_Scalar_Type
(Etype
(E1
))))
8199 or else (Is_Static_Expression
(E2
)
8200 and then Is_Scalar_Type
(Etype
(E1
))))
8201 and then Id
/= Attribute_Descriptor_Size
8204 Set_Is_Static_Expression
(N
, True);
8207 -- First foldable possibility is a scalar or array type (RM 4.9(7))
8208 -- that is not generic (generic types are eliminated by RM 4.9(25)).
8209 -- Note we allow nonstatic nongeneric types at this stage as further
8212 if Is_Type
(P_Entity
)
8213 and then (Is_Scalar_Type
(P_Entity
) or Is_Array_Type
(P_Entity
))
8214 and then not Is_Generic_Type
(P_Entity
)
8218 -- Second foldable possibility is an array object (RM 4.9(8))
8220 elsif Ekind
(P_Entity
) in E_Variable | E_Constant
8221 and then Is_Array_Type
(Etype
(P_Entity
))
8222 and then not Is_Generic_Type
(Etype
(P_Entity
))
8224 P_Type
:= Etype
(P_Entity
);
8226 -- If the entity is an array constant with an unconstrained nominal
8227 -- subtype then get the type from the initial value. If the value has
8228 -- been expanded into assignments, there is no expression and the
8229 -- attribute reference remains dynamic.
8231 -- We could do better here and retrieve the type ???
8233 if Ekind
(P_Entity
) = E_Constant
8234 and then not Is_Constrained
(P_Type
)
8236 if No
(Constant_Value
(P_Entity
)) then
8239 P_Type
:= Etype
(Constant_Value
(P_Entity
));
8243 -- Definite must be folded if the prefix is not a generic type, that
8244 -- is to say if we are within an instantiation. Same processing applies
8245 -- to selected GNAT attributes.
8247 elsif (Id
= Attribute_Atomic_Always_Lock_Free
or else
8248 Id
= Attribute_Definite
or else
8249 Id
= Attribute_Descriptor_Size
or else
8250 Id
= Attribute_Has_Access_Values
or else
8251 Id
= Attribute_Has_Discriminants
or else
8252 Id
= Attribute_Has_Tagged_Values
or else
8253 Id
= Attribute_Lock_Free
or else
8254 Id
= Attribute_Preelaborable_Initialization
or else
8255 Id
= Attribute_Type_Class
or else
8256 Id
= Attribute_Unconstrained_Array
or else
8257 Id
= Attribute_Max_Alignment_For_Allocation
)
8258 and then not Is_Generic_Type
(P_Entity
)
8262 -- We can fold 'Size applied to a type if the size is known (as happens
8263 -- for a size from an attribute definition clause). At this stage, this
8264 -- can happen only for types (e.g. record types) for which the size is
8265 -- always non-static. We exclude generic types from consideration (since
8266 -- they have bogus sizes set within templates). We can also fold
8267 -- Max_Size_In_Storage_Elements in the same cases.
8269 elsif (Id
= Attribute_Size
or
8270 Id
= Attribute_Max_Size_In_Storage_Elements
)
8271 and then Is_Type
(P_Entity
)
8272 and then not Is_Generic_Type
(P_Entity
)
8273 and then Known_Static_RM_Size
(P_Entity
)
8276 Attr_Value
: Uint
:= RM_Size
(P_Entity
);
8278 if Id
= Attribute_Max_Size_In_Storage_Elements
then
8279 Attr_Value
:= (Attr_Value
+ System_Storage_Unit
- 1)
8280 / System_Storage_Unit
;
8282 Compile_Time_Known_Attribute
(N
, Attr_Value
);
8286 -- We can fold 'Alignment applied to a type if the alignment is known
8287 -- (as happens for an alignment from an attribute definition clause).
8288 -- At this stage, this can happen only for types (e.g. record types) for
8289 -- which the size is always non-static. We exclude generic types from
8290 -- consideration (since they have bogus sizes set within templates).
8292 elsif Id
= Attribute_Alignment
8293 and then Is_Type
(P_Entity
)
8294 and then not Is_Generic_Type
(P_Entity
)
8295 and then Known_Alignment
(P_Entity
)
8297 Compile_Time_Known_Attribute
(N
, Alignment
(P_Entity
));
8300 -- If this is an access attribute that is known to fail accessibility
8301 -- check, rewrite accordingly.
8303 elsif Id
= Attribute_Address
8304 and then Raises_Constraint_Error
(N
)
8307 Make_Raise_Program_Error
(Loc
,
8308 Reason
=> PE_Accessibility_Check_Failed
));
8309 Set_Etype
(N
, C_Type
);
8312 -- No other cases are foldable (they certainly aren't static, and at
8313 -- the moment we don't try to fold any cases other than the ones above).
8320 -- If either attribute or the prefix is Any_Type, then propagate
8321 -- Any_Type to the result and don't do anything else at all.
8323 if P_Type
= Any_Type
8324 or else (Present
(E1
) and then Etype
(E1
) = Any_Type
)
8325 or else (Present
(E2
) and then Etype
(E2
) = Any_Type
)
8327 Set_Etype
(N
, Any_Type
);
8331 -- Scalar subtype case. We have not yet enforced the static requirement
8332 -- of (RM 4.9(7)) and we don't intend to just yet, since there are cases
8333 -- of non-static attribute references (e.g. S'Digits for a non-static
8334 -- floating-point type, which we can compute at compile time).
8336 -- Note: this folding of non-static attributes is not simply a case of
8337 -- optimization. For many of the attributes affected, Gigi cannot handle
8338 -- the attribute and depends on the front end having folded them away.
8340 -- Note: although we don't require staticness at this stage, we do set
8341 -- the Static variable to record the staticness, for easy reference by
8342 -- those attributes where it matters (e.g. Succ and Pred), and also to
8343 -- be used to ensure that non-static folded things are not marked as
8344 -- being static (a check that is done right at the end).
8346 P_Root_Type
:= Root_Type
(P_Type
);
8347 P_Base_Type
:= Base_Type
(P_Type
);
8349 -- If the root type or base type is generic, then we cannot fold. This
8350 -- test is needed because subtypes of generic types are not always
8351 -- marked as being generic themselves (which seems odd???)
8353 if Is_Generic_Type
(P_Root_Type
)
8354 or else Is_Generic_Type
(P_Base_Type
)
8359 if Is_Scalar_Type
(P_Type
) then
8360 if not Is_Static_Subtype
(P_Type
) then
8362 Set_Is_Static_Expression
(N
, False);
8363 elsif not Is_OK_Static_Subtype
(P_Type
) then
8364 Set_Raises_Constraint_Error
(N
);
8367 -- Array case. We enforce the constrained requirement of (RM 4.9(7-8))
8368 -- since we can't do anything with unconstrained arrays. In addition,
8369 -- only the First, Last and Length attributes are possibly static.
8371 -- Atomic_Always_Lock_Free, Definite, Descriptor_Size, Has_Access_Values
8372 -- Has_Discriminants, Has_Tagged_Values, Lock_Free, Type_Class, and
8373 -- Unconstrained_Array are again exceptions, because they apply as well
8374 -- to unconstrained types.
8376 -- In addition Component_Size is an exception since it is possibly
8377 -- foldable, even though it is never static, and it does apply to
8378 -- unconstrained arrays. Furthermore, it is essential to fold this
8379 -- in the packed case, since otherwise the value will be incorrect.
8381 -- Folding can also be done for Preelaborable_Initialization based on
8382 -- whether the prefix type has preelaborable initialization, even though
8383 -- the attribute is nonstatic.
8385 elsif Id
= Attribute_Atomic_Always_Lock_Free
or else
8386 Id
= Attribute_Definite
or else
8387 Id
= Attribute_Descriptor_Size
or else
8388 Id
= Attribute_Has_Access_Values
or else
8389 Id
= Attribute_Has_Discriminants
or else
8390 Id
= Attribute_Has_Tagged_Values
or else
8391 Id
= Attribute_Lock_Free
or else
8392 Id
= Attribute_Preelaborable_Initialization
or else
8393 Id
= Attribute_Type_Class
or else
8394 Id
= Attribute_Unconstrained_Array
or else
8395 Id
= Attribute_Component_Size
8398 Set_Is_Static_Expression
(N
, False);
8400 elsif Id
/= Attribute_Max_Alignment_For_Allocation
then
8401 if not Is_Constrained
(P_Type
)
8402 or else (Id
/= Attribute_First
and then
8403 Id
/= Attribute_Last
and then
8404 Id
/= Attribute_Length
)
8410 -- The rules in (RM 4.9(7,8)) require a static array, but as in the
8411 -- scalar case, we hold off on enforcing staticness, since there are
8412 -- cases which we can fold at compile time even though they are not
8413 -- static (e.g. 'Length applied to a static index, even though other
8414 -- non-static indexes make the array type non-static). This is only
8415 -- an optimization, but it falls out essentially free, so why not.
8416 -- Again we compute the variable Static for easy reference later
8417 -- (note that no array attributes are static in Ada 83).
8419 -- We also need to set Static properly for subsequent legality checks
8420 -- which might otherwise accept non-static constants in contexts
8421 -- where they are not legal.
8424 Ada_Version
>= Ada_95
and then Statically_Denotes_Entity
(P
);
8425 Set_Is_Static_Expression
(N
, Static
);
8431 Nod
:= First_Index
(P_Type
);
8433 -- The expression is static if the array type is constrained
8434 -- by given bounds, and not by an initial expression. Constant
8435 -- strings are static in any case.
8437 if Root_Type
(P_Type
) /= Standard_String
then
8439 Static
and then not Is_Constr_Subt_For_U_Nominal
(P_Type
);
8440 Set_Is_Static_Expression
(N
, Static
);
8443 while Present
(Nod
) loop
8444 if not Is_Static_Subtype
(Etype
(Nod
)) then
8446 Set_Is_Static_Expression
(N
, False);
8448 elsif not Is_OK_Static_Subtype
(Etype
(Nod
)) then
8449 Set_Raises_Constraint_Error
(N
);
8451 Set_Is_Static_Expression
(N
, False);
8454 -- If however the index type is generic, or derived from
8455 -- one, attributes cannot be folded.
8457 if Is_Generic_Type
(Root_Type
(Etype
(Nod
)))
8458 and then Id
/= Attribute_Component_Size
8468 -- Check any expressions that are present. Note that these expressions,
8469 -- depending on the particular attribute type, are either part of the
8470 -- attribute designator, or they are arguments in a case where the
8471 -- attribute reference returns a function. In the latter case, the
8472 -- rule in (RM 4.9(22)) applies and in particular requires the type
8473 -- of the expressions to be scalar in order for the attribute to be
8474 -- considered to be static.
8482 while Present
(E
) loop
8484 -- If expression is not static, then the attribute reference
8485 -- result certainly cannot be static.
8487 if not Is_Static_Expression
(E
) then
8489 Set_Is_Static_Expression
(N
, False);
8492 if Raises_Constraint_Error
(E
) then
8493 Set_Raises_Constraint_Error
(N
);
8496 -- If the result is not known at compile time, or is not of
8497 -- a scalar type, then the result is definitely not static,
8498 -- so we can quit now.
8500 if not Compile_Time_Known_Value
(E
)
8501 or else not Is_Scalar_Type
(Etype
(E
))
8506 -- If the expression raises a constraint error, then so does
8507 -- the attribute reference. We keep going in this case because
8508 -- we are still interested in whether the attribute reference
8509 -- is static even if it is not static.
8511 elsif Raises_Constraint_Error
(E
) then
8512 Set_Raises_Constraint_Error
(N
);
8518 if Raises_Constraint_Error
(Prefix
(N
)) then
8519 Set_Is_Static_Expression
(N
, False);
8524 -- Deal with the case of a static attribute reference that raises
8525 -- constraint error. The Raises_Constraint_Error flag will already
8526 -- have been set, and the Static flag shows whether the attribute
8527 -- reference is static. In any case we certainly can't fold such an
8528 -- attribute reference.
8530 -- Note that the rewriting of the attribute node with the constraint
8531 -- error node is essential in this case, because otherwise Gigi might
8532 -- blow up on one of the attributes it never expects to see.
8534 -- The constraint_error node must have the type imposed by the context,
8535 -- to avoid spurious errors in the enclosing expression.
8537 if Raises_Constraint_Error
(N
) then
8539 Make_Raise_Constraint_Error
(Sloc
(N
),
8540 Reason
=> CE_Range_Check_Failed
);
8541 Set_Etype
(CE_Node
, Etype
(N
));
8542 Set_Raises_Constraint_Error
(CE_Node
);
8544 Rewrite
(N
, Relocate_Node
(CE_Node
));
8545 Set_Raises_Constraint_Error
(N
, True);
8549 -- At this point we have a potentially foldable attribute reference.
8550 -- If Static is set, then the attribute reference definitely obeys
8551 -- the requirements in (RM 4.9(7,8,22)), and it definitely can be
8552 -- folded. If Static is not set, then the attribute may or may not
8553 -- be foldable, and the individual attribute processing routines
8554 -- test Static as required in cases where it makes a difference.
8556 -- In the case where Static is not set, we do know that all the
8557 -- expressions present are at least known at compile time (we assumed
8558 -- above that if this was not the case, then there was no hope of static
8559 -- evaluation). However, we did not require that the bounds of the
8560 -- prefix type be compile time known, let alone static). That's because
8561 -- there are many attributes that can be computed at compile time on
8562 -- non-static subtypes, even though such references are not static
8565 -- For VAX float, the root type is an IEEE type. So make sure to use the
8566 -- base type instead of the root-type for floating point attributes.
8570 -- Attributes related to Ada 2012 iterators; nothing to evaluate for
8573 when Attribute_Constant_Indexing
8574 | Attribute_Default_Iterator
8575 | Attribute_Implicit_Dereference
8576 | Attribute_Iterator_Element
8577 | Attribute_Iterable
8579 | Attribute_Variable_Indexing
8583 -- Internal attributes used to deal with Ada 2012 delayed aspects.
8584 -- These were already rejected by the parser. Thus they shouldn't
8587 when Internal_Attribute_Id
=>
8588 raise Program_Error
;
8594 when Attribute_Adjacent
=>
8598 (P_Base_Type
, Expr_Value_R
(E1
), Expr_Value_R
(E2
)),
8605 when Attribute_Aft
=>
8606 Fold_Uint
(N
, Aft_Value
(P_Type
), Static
);
8612 when Attribute_Alignment
=> Alignment_Block
: declare
8613 P_TypeA
: constant Entity_Id
:= Underlying_Type
(P_Type
);
8616 -- Fold if alignment is set and not otherwise
8618 if Known_Alignment
(P_TypeA
) then
8619 Fold_Uint
(N
, Alignment
(P_TypeA
), Static
);
8621 end Alignment_Block
;
8623 -----------------------------
8624 -- Atomic_Always_Lock_Free --
8625 -----------------------------
8627 -- Atomic_Always_Lock_Free attribute is a Boolean, thus no need to fold
8630 when Attribute_Atomic_Always_Lock_Free
=> Atomic_Always_Lock_Free
:
8632 V
: constant Entity_Id
:=
8634 (Support_Atomic_Primitives_On_Target
8635 and then Support_Atomic_Primitives
(P_Type
));
8638 Rewrite
(N
, New_Occurrence_Of
(V
, Loc
));
8640 -- Analyze and resolve as boolean. Note that this attribute is a
8641 -- static attribute in GNAT.
8643 Analyze_And_Resolve
(N
, Standard_Boolean
);
8645 Set_Is_Static_Expression
(N
);
8646 end Atomic_Always_Lock_Free
;
8652 -- Bit can never be folded
8654 when Attribute_Bit
=>
8661 -- Body_version can never be static
8663 when Attribute_Body_Version
=>
8670 when Attribute_Ceiling
=>
8672 (N
, Eval_Fat
.Ceiling
(P_Base_Type
, Expr_Value_R
(E1
)), Static
);
8674 --------------------
8675 -- Component_Size --
8676 --------------------
8678 -- Fold Component_Size if it is known at compile time, which is always
8679 -- true in the packed array case. It is important that the packed array
8680 -- case is handled here since the back end would otherwise get confused
8681 -- by the equivalent packed array type.
8683 when Attribute_Component_Size
=>
8684 if Known_Static_Component_Size
(P_Type
) then
8685 Fold_Uint
(N
, Component_Size
(P_Type
), Static
);
8692 when Attribute_Compose
=>
8695 Eval_Fat
.Compose
(P_Base_Type
, Expr_Value_R
(E1
), Expr_Value
(E2
)),
8702 -- Constrained is never folded for now, there may be cases that
8703 -- could be handled at compile time. To be looked at later.
8705 when Attribute_Constrained
=>
8707 -- The expander might fold it and set the static flag accordingly,
8708 -- but with expansion disabled, it remains as an attribute reference,
8709 -- and this reference is not static.
8711 Set_Is_Static_Expression
(N
, False);
8717 when Attribute_Copy_Sign
=>
8721 (P_Base_Type
, Expr_Value_R
(E1
), Expr_Value_R
(E2
)),
8728 when Attribute_Definite
=>
8729 Rewrite
(N
, New_Occurrence_Of
(
8730 Boolean_Literals
(Is_Definite_Subtype
(P_Entity
)), Loc
));
8731 Analyze_And_Resolve
(N
, Standard_Boolean
);
8737 when Attribute_Delta
=>
8738 Fold_Ureal
(N
, Delta_Value
(P_Type
), True);
8744 when Attribute_Denorm
=>
8746 (N
, UI_From_Int
(Boolean'Pos (Has_Denormals
(P_Type
))), Static
);
8748 ---------------------
8749 -- Descriptor_Size --
8750 ---------------------
8752 -- Descriptor_Size is nonnull only for unconstrained array types
8754 when Attribute_Descriptor_Size
=>
8755 if not Is_Array_Type
(P_Type
) or else Is_Constrained
(P_Type
) then
8756 Fold_Uint
(N
, Uint_0
, Static
);
8763 when Attribute_Digits
=>
8764 Fold_Uint
(N
, Digits_Value
(P_Type
), Static
);
8770 when Attribute_Emax
=>
8772 -- Ada 83 attribute is defined as (RM83 3.5.8)
8774 -- T'Emax = 4 * T'Mantissa
8776 Fold_Uint
(N
, 4 * Mantissa
, Static
);
8782 when Attribute_Enum_Rep
=> Enum_Rep
: declare
8786 -- The attribute appears in the form:
8788 -- Enum_Typ'Enum_Rep (Const)
8789 -- Enum_Typ'Enum_Rep (Enum_Lit)
8791 if Present
(E1
) then
8794 -- Otherwise the prefix denotes a constant or enumeration literal:
8797 -- Enum_Lit'Enum_Rep
8803 -- For an enumeration type with a non-standard representation use
8804 -- the Enumeration_Rep field of the proper constant. Note that this
8805 -- will not work for types Character/Wide_[Wide-]Character, since no
8806 -- real entities are created for the enumeration literals, but that
8807 -- does not matter since these two types do not have non-standard
8808 -- representations anyway.
8810 if Is_Enumeration_Type
(P_Type
)
8811 and then Has_Non_Standard_Rep
(P_Type
)
8813 Fold_Uint
(N
, Enumeration_Rep
(Expr_Value_E
(Val
)), Static
);
8815 -- For enumeration types with standard representations and all other
8816 -- cases (i.e. all integer and modular types), Enum_Rep is equivalent
8820 Fold_Uint
(N
, Expr_Value
(Val
), Static
);
8828 when Attribute_Enum_Val
=> Enum_Val
: declare
8832 -- We have something like Enum_Type'Enum_Val (23), so search for a
8833 -- corresponding value in the list of Enum_Rep values for the type.
8835 Lit
:= First_Literal
(P_Base_Type
);
8837 if Enumeration_Rep
(Lit
) = Expr_Value
(E1
) then
8838 Fold_Uint
(N
, Enumeration_Pos
(Lit
), Static
);
8845 Apply_Compile_Time_Constraint_Error
8846 (N
, "no representation value matches",
8847 CE_Range_Check_Failed
,
8848 Warn
=> not Static
);
8858 when Attribute_Epsilon
=>
8860 -- Ada 83 attribute is defined as (RM83 3.5.8)
8862 -- T'Epsilon = 2.0**(1 - T'Mantissa)
8864 Fold_Ureal
(N
, Ureal_2
** (1 - Mantissa
), True);
8870 when Attribute_Exponent
=>
8872 Eval_Fat
.Exponent
(P_Base_Type
, Expr_Value_R
(E1
)), Static
);
8874 -----------------------
8875 -- Finalization_Size --
8876 -----------------------
8878 when Attribute_Finalization_Size
=>
8885 when Attribute_First
=>
8888 if Compile_Time_Known_Value
(Lo_Bound
) then
8889 if Is_Real_Type
(P_Type
) then
8890 Fold_Ureal
(N
, Expr_Value_R
(Lo_Bound
), Static
);
8892 Fold_Uint
(N
, Expr_Value
(Lo_Bound
), Static
);
8896 Check_Concurrent_Discriminant
(Lo_Bound
);
8903 when Attribute_First_Valid
=>
8904 if Has_Predicates
(P_Type
)
8905 and then Has_Static_Predicate
(P_Type
)
8908 FirstN
: constant Node_Id
:=
8909 First
(Static_Discrete_Predicate
(P_Type
));
8911 if Nkind
(FirstN
) = N_Range
then
8912 Fold_Uint
(N
, Expr_Value
(Low_Bound
(FirstN
)), Static
);
8914 Fold_Uint
(N
, Expr_Value
(FirstN
), Static
);
8920 Fold_Uint
(N
, Expr_Value
(Lo_Bound
), Static
);
8927 when Attribute_Fixed_Value
=>
8934 when Attribute_Floor
=>
8936 (N
, Eval_Fat
.Floor
(P_Base_Type
, Expr_Value_R
(E1
)), Static
);
8942 when Attribute_Fore
=>
8943 if Compile_Time_Known_Bounds
(P_Type
) then
8944 Fold_Uint
(N
, UI_From_Int
(Fore_Value
), Static
);
8951 when Attribute_Fraction
=>
8953 (N
, Eval_Fat
.Fraction
(P_Base_Type
, Expr_Value_R
(E1
)), Static
);
8955 -----------------------
8956 -- Has_Access_Values --
8957 -----------------------
8959 when Attribute_Has_Access_Values
=>
8960 Rewrite
(N
, New_Occurrence_Of
8961 (Boolean_Literals
(Has_Access_Values
(P_Root_Type
)), Loc
));
8962 Analyze_And_Resolve
(N
, Standard_Boolean
);
8964 -----------------------
8965 -- Has_Discriminants --
8966 -----------------------
8968 when Attribute_Has_Discriminants
=>
8969 Rewrite
(N
, New_Occurrence_Of
(
8970 Boolean_Literals
(Has_Discriminants
(P_Entity
)), Loc
));
8971 Analyze_And_Resolve
(N
, Standard_Boolean
);
8973 ----------------------
8974 -- Has_Same_Storage --
8975 ----------------------
8977 when Attribute_Has_Same_Storage
=>
8980 -----------------------
8981 -- Has_Tagged_Values --
8982 -----------------------
8984 when Attribute_Has_Tagged_Values
=>
8985 Rewrite
(N
, New_Occurrence_Of
8986 (Boolean_Literals
(Has_Tagged_Component
(P_Root_Type
)), Loc
));
8987 Analyze_And_Resolve
(N
, Standard_Boolean
);
8993 when Attribute_Identity
=>
9000 -- Image is a scalar attribute, but is never static, because it is
9001 -- not a static function (having a non-scalar argument (RM 4.9(22))
9002 -- However, we can constant-fold the image of an enumeration literal
9003 -- if names are available.
9005 when Attribute_Image
=>
9006 if Is_Entity_Name
(E1
)
9007 and then Ekind
(Entity
(E1
)) = E_Enumeration_Literal
9008 and then not Discard_Names
(First_Subtype
(Etype
(E1
)))
9009 and then not Global_Discard_Names
9012 Lit
: constant Entity_Id
:= Entity
(E1
);
9016 Get_Unqualified_Decoded_Name_String
(Chars
(Lit
));
9017 Set_Casing
(All_Upper_Case
);
9018 Store_String_Chars
(Name_Buffer
(1 .. Name_Len
));
9020 Rewrite
(N
, Make_String_Literal
(Loc
, Strval
=> Str
));
9021 Analyze_And_Resolve
(N
, Standard_String
);
9022 Set_Is_Static_Expression
(N
, False);
9030 -- We never try to fold Integer_Value (though perhaps we could???)
9032 when Attribute_Integer_Value
=>
9039 -- Invalid_Value is a scalar attribute that is never static, because
9040 -- the value is by design out of range.
9042 when Attribute_Invalid_Value
=>
9049 when Attribute_Large
=>
9051 -- For fixed-point, we use the identity:
9053 -- T'Large = (2.0**T'Mantissa - 1.0) * T'Small
9055 if Is_Fixed_Point_Type
(P_Type
) then
9057 Make_Op_Multiply
(Loc
,
9059 Make_Op_Subtract
(Loc
,
9063 Make_Real_Literal
(Loc
, Ureal_2
),
9065 Make_Attribute_Reference
(Loc
,
9067 Attribute_Name
=> Name_Mantissa
)),
9068 Right_Opnd
=> Make_Real_Literal
(Loc
, Ureal_1
)),
9071 Make_Real_Literal
(Loc
, Small_Value
(Entity
(P
)))));
9073 Analyze_And_Resolve
(N
, C_Type
);
9075 -- Floating-point (Ada 83 compatibility)
9078 -- Ada 83 attribute is defined as (RM83 3.5.8)
9080 -- T'Large = 2.0**T'Emax * (1.0 - 2.0**(-T'Mantissa))
9084 -- T'Emax = 4 * T'Mantissa
9088 Ureal_2
** (4 * Mantissa
) * (Ureal_1
- Ureal_2
** (-Mantissa
)),
9096 when Attribute_Lock_Free
=> Lock_Free
: declare
9097 V
: constant Entity_Id
:= Boolean_Literals
(Uses_Lock_Free
(P_Type
));
9100 Rewrite
(N
, New_Occurrence_Of
(V
, Loc
));
9102 -- Analyze and resolve as boolean. Note that this attribute is a
9103 -- static attribute in GNAT.
9105 Analyze_And_Resolve
(N
, Standard_Boolean
);
9107 Set_Is_Static_Expression
(N
);
9114 when Attribute_Last
=>
9117 if Compile_Time_Known_Value
(Hi_Bound
) then
9118 if Is_Real_Type
(P_Type
) then
9119 Fold_Ureal
(N
, Expr_Value_R
(Hi_Bound
), Static
);
9121 Fold_Uint
(N
, Expr_Value
(Hi_Bound
), Static
);
9125 Check_Concurrent_Discriminant
(Hi_Bound
);
9132 when Attribute_Last_Valid
=>
9133 if Has_Predicates
(P_Type
)
9134 and then Has_Static_Predicate
(P_Type
)
9137 LastN
: constant Node_Id
:=
9138 Last
(Static_Discrete_Predicate
(P_Type
));
9140 if Nkind
(LastN
) = N_Range
then
9141 Fold_Uint
(N
, Expr_Value
(High_Bound
(LastN
)), Static
);
9143 Fold_Uint
(N
, Expr_Value
(LastN
), Static
);
9149 Fold_Uint
(N
, Expr_Value
(Hi_Bound
), Static
);
9156 when Attribute_Leading_Part
=> Leading_Part
: declare
9157 Radix_Digits
: constant Uint
:= Expr_Value
(E2
);
9160 if UI_Le
(Radix_Digits
, Uint_0
) then
9161 Apply_Compile_Time_Constraint_Error
9162 (N
, "Radix_Digits in Leading_Part is zero or negative",
9164 Warn
=> not Static
);
9172 Eval_Fat
.Leading_Part
9173 (P_Base_Type
, Expr_Value_R
(E1
), Radix_Digits
),
9181 when Attribute_Length
=> Length
: declare
9185 -- If any index type is a formal type, or derived from one, the
9186 -- bounds are not static. Treating them as static can produce
9187 -- spurious warnings or improper constant folding.
9189 Ind
:= First_Index
(P_Type
);
9190 while Present
(Ind
) loop
9191 if Is_Generic_Type
(Root_Type
(Etype
(Ind
))) then
9200 -- For two compile time values, we can compute length
9202 if Compile_Time_Known_Value
(Lo_Bound
)
9203 and then Compile_Time_Known_Value
(Hi_Bound
)
9206 UI_Max
(0, 1 + (Expr_Value
(Hi_Bound
) - Expr_Value
(Lo_Bound
))),
9210 -- One more case is where Hi_Bound and Lo_Bound are compile-time
9211 -- comparable, and we can figure out the difference between them.
9214 Diff
: aliased Uint
;
9218 Compile_Time_Compare
9219 (Lo_Bound
, Hi_Bound
, Diff
'Access, Assume_Valid
=> False)
9222 Fold_Uint
(N
, Uint_1
, Static
);
9225 Fold_Uint
(N
, Uint_0
, Static
);
9228 if Present
(Diff
) then
9229 Fold_Uint
(N
, Diff
+ 1, Static
);
9242 -- Loop_Entry acts as an alias of a constant initialized to the prefix
9243 -- of the said attribute at the point of entry into the related loop. As
9244 -- such, the attribute reference does not need to be evaluated because
9245 -- the prefix is the one that is evaluted.
9247 when Attribute_Loop_Entry
=>
9254 -- We use the same rounding as the one used for RM 4.9(38/2)
9256 when Attribute_Machine
=>
9258 (N
, Machine_Number
(P_Base_Type
, Expr_Value_R
(E1
), N
), Static
);
9259 Set_Is_Machine_Number
(N
);
9265 when Attribute_Machine_Emax
=>
9266 Fold_Uint
(N
, Machine_Emax_Value
(P_Type
), Static
);
9272 when Attribute_Machine_Emin
=>
9273 Fold_Uint
(N
, Machine_Emin_Value
(P_Type
), Static
);
9275 ----------------------
9276 -- Machine_Mantissa --
9277 ----------------------
9279 when Attribute_Machine_Mantissa
=>
9280 Fold_Uint
(N
, Machine_Mantissa_Value
(P_Type
), Static
);
9282 -----------------------
9283 -- Machine_Overflows --
9284 -----------------------
9286 when Attribute_Machine_Overflows
=>
9288 -- Always true for fixed-point
9290 if Is_Fixed_Point_Type
(P_Type
) then
9291 Fold_Uint
(N
, True_Value
, Static
);
9293 -- Floating point case
9297 UI_From_Int
(Boolean'Pos (Machine_Overflows_On_Target
)),
9305 when Attribute_Machine_Radix
=>
9306 if Is_Fixed_Point_Type
(P_Type
) then
9307 if Is_Decimal_Fixed_Point_Type
(P_Type
)
9308 and then Machine_Radix_10
(P_Type
)
9310 Fold_Uint
(N
, Uint_10
, Static
);
9312 Fold_Uint
(N
, Uint_2
, Static
);
9315 -- All floating-point type always have radix 2
9318 Fold_Uint
(N
, Uint_2
, Static
);
9321 ----------------------
9322 -- Machine_Rounding --
9323 ----------------------
9325 -- Note: for the folding case, it is fine to treat Machine_Rounding
9326 -- exactly the same way as Rounding, since this is one of the allowed
9327 -- behaviors, and performance is not an issue here. It might be a bit
9328 -- better to give the same result as it would give at run time, even
9329 -- though the non-determinism is certainly permitted.
9331 when Attribute_Machine_Rounding
=>
9333 (N
, Eval_Fat
.Rounding
(P_Base_Type
, Expr_Value_R
(E1
)), Static
);
9335 --------------------
9336 -- Machine_Rounds --
9337 --------------------
9339 when Attribute_Machine_Rounds
=>
9341 -- Always False for fixed-point
9343 if Is_Fixed_Point_Type
(P_Type
) then
9344 Fold_Uint
(N
, False_Value
, Static
);
9346 -- Else yield proper floating-point result
9350 (N
, UI_From_Int
(Boolean'Pos (Machine_Rounds_On_Target
)),
9358 -- Note: Machine_Size is identical to Object_Size
9360 when Attribute_Machine_Size
=> Machine_Size
: declare
9361 P_TypeA
: constant Entity_Id
:= Underlying_Type
(P_Type
);
9364 if Known_Esize
(P_TypeA
) then
9365 Fold_Uint
(N
, Esize
(P_TypeA
), Static
);
9373 when Attribute_Mantissa
=>
9375 -- Fixed-point mantissa
9377 if Is_Fixed_Point_Type
(P_Type
) then
9379 -- Compile time foldable case
9381 if Compile_Time_Known_Value
(Type_Low_Bound
(P_Type
))
9383 Compile_Time_Known_Value
(Type_High_Bound
(P_Type
))
9385 -- The calculation of the obsolete Ada 83 attribute Mantissa
9386 -- is annoying, because of AI00143, quoted here:
9388 -- !question 84-01-10
9390 -- Consider the model numbers for F:
9392 -- type F is delta 1.0 range -7.0 .. 8.0;
9394 -- The wording requires that F'MANTISSA be the SMALLEST
9395 -- integer number for which each bound of the specified
9396 -- range is either a model number or lies at most small
9397 -- distant from a model number. This means F'MANTISSA
9398 -- is required to be 3 since the range -7.0 .. 7.0 fits
9399 -- in 3 signed bits, and 8 is "at most" 1.0 from a model
9400 -- number, namely, 7. Is this analysis correct? Note that
9401 -- this implies the upper bound of the range is not
9402 -- represented as a model number.
9404 -- !response 84-03-17
9406 -- The analysis is correct. The upper and lower bounds for
9407 -- a fixed point type can lie outside the range of model
9418 LBound
:= Expr_Value_R
(Type_Low_Bound
(P_Type
));
9419 UBound
:= Expr_Value_R
(Type_High_Bound
(P_Type
));
9420 Bound
:= UR_Max
(UR_Abs
(LBound
), UR_Abs
(UBound
));
9421 Max_Man
:= UR_Trunc
(Bound
/ Small_Value
(P_Type
));
9423 -- If the Bound is exactly a model number, i.e. a multiple
9424 -- of Small, then we back it off by one to get the integer
9425 -- value that must be representable.
9427 if Small_Value
(P_Type
) * Max_Man
= Bound
then
9428 Max_Man
:= Max_Man
- 1;
9431 -- Now find corresponding size = Mantissa value
9434 while 2 ** Siz
< Max_Man
loop
9438 Fold_Uint
(N
, Siz
, Static
);
9442 -- The case of dynamic bounds cannot be evaluated at compile
9443 -- time. Instead we use a runtime routine (see Exp_Attr).
9448 -- Floating-point Mantissa
9451 Fold_Uint
(N
, Mantissa
, Static
);
9458 when Attribute_Max
=>
9459 if Is_Real_Type
(P_Type
) then
9461 (N
, UR_Max
(Expr_Value_R
(E1
), Expr_Value_R
(E2
)), Static
);
9463 Fold_Uint
(N
, UI_Max
(Expr_Value
(E1
), Expr_Value
(E2
)), Static
);
9466 ----------------------------------
9467 -- Max_Alignment_For_Allocation --
9468 ----------------------------------
9470 -- Max_Alignment_For_Allocation is usually the Alignment. However,
9471 -- arrays are allocated with dope, so we need to take into account both
9472 -- the alignment of the array, which comes from the component alignment,
9473 -- and the alignment of the dope. Also, if the alignment is unknown, we
9474 -- use the max (it's OK to be pessimistic).
9476 when Attribute_Max_Alignment_For_Allocation
=> Max_Align
: declare
9477 A
: Uint
:= UI_From_Int
(Ttypes
.Maximum_Alignment
);
9479 if Known_Alignment
(P_Type
)
9480 and then (not Is_Array_Type
(P_Type
) or else Alignment
(P_Type
) > A
)
9482 A
:= Alignment
(P_Type
);
9485 Fold_Uint
(N
, A
, Static
);
9488 ----------------------------------
9489 -- Max_Size_In_Storage_Elements --
9490 ----------------------------------
9492 -- Max_Size_In_Storage_Elements is simply the Size rounded up to a
9493 -- Storage_Unit boundary. We can fold any cases for which the size
9494 -- is known by the front end.
9496 when Attribute_Max_Size_In_Storage_Elements
=>
9497 if Known_Esize
(P_Type
) then
9499 (Esize
(P_Type
) + System_Storage_Unit
- 1) /
9500 System_Storage_Unit
,
9504 --------------------
9505 -- Mechanism_Code --
9506 --------------------
9508 when Attribute_Mechanism_Code
=> Mechanism_Code
: declare
9510 Mech
: Mechanism_Type
;
9515 Mech
:= Mechanism
(P_Entity
);
9518 Val
:= UI_To_Int
(Expr_Value
(E1
));
9520 Formal
:= First_Formal
(P_Entity
);
9521 for J
in 1 .. Val
- 1 loop
9522 Next_Formal
(Formal
);
9525 Mech
:= Mechanism
(Formal
);
9529 Fold_Uint
(N
, UI_From_Int
(Int
(-Mech
)), Static
);
9537 when Attribute_Min
=>
9538 if Is_Real_Type
(P_Type
) then
9540 (N
, UR_Min
(Expr_Value_R
(E1
), Expr_Value_R
(E2
)), Static
);
9543 (N
, UI_Min
(Expr_Value
(E1
), Expr_Value
(E2
)), Static
);
9550 when Attribute_Mod
=>
9552 (N
, UI_Mod
(Expr_Value
(E1
), Modulus
(P_Base_Type
)), Static
);
9558 when Attribute_Model
=>
9560 (N
, Eval_Fat
.Model
(P_Base_Type
, Expr_Value_R
(E1
)), Static
);
9566 when Attribute_Model_Emin
=>
9567 Fold_Uint
(N
, Model_Emin_Value
(P_Base_Type
), Static
);
9573 when Attribute_Model_Epsilon
=>
9574 Fold_Ureal
(N
, Model_Epsilon_Value
(P_Base_Type
), Static
);
9576 --------------------
9577 -- Model_Mantissa --
9578 --------------------
9580 when Attribute_Model_Mantissa
=>
9581 Fold_Uint
(N
, Model_Mantissa_Value
(P_Base_Type
), Static
);
9587 when Attribute_Model_Small
=>
9588 Fold_Ureal
(N
, Model_Small_Value
(P_Base_Type
), Static
);
9594 when Attribute_Modulus
=>
9595 Fold_Uint
(N
, Modulus
(P_Type
), Static
);
9597 --------------------
9598 -- Null_Parameter --
9599 --------------------
9601 -- Cannot fold, we know the value sort of, but the whole point is
9602 -- that there is no way to talk about this imaginary value except
9603 -- by using the attribute, so we leave it the way it is.
9605 when Attribute_Null_Parameter
=>
9612 -- The Object_Size attribute for a type returns the Esize of the
9613 -- type and can be folded if this value is known.
9615 when Attribute_Object_Size
=> Object_Size
: declare
9616 P_TypeA
: constant Entity_Id
:= Underlying_Type
(P_Type
);
9619 if Known_Esize
(P_TypeA
) then
9620 Fold_Uint
(N
, Esize
(P_TypeA
), Static
);
9624 ----------------------
9625 -- Overlaps_Storage --
9626 ----------------------
9628 when Attribute_Overlaps_Storage
=>
9631 -------------------------
9632 -- Passed_By_Reference --
9633 -------------------------
9635 -- Scalar types are never passed by reference
9637 when Attribute_Passed_By_Reference
=>
9638 Fold_Uint
(N
, False_Value
, Static
);
9644 when Attribute_Pos
=>
9645 Fold_Uint
(N
, Expr_Value
(E1
), Static
);
9651 when Attribute_Pred
=>
9653 -- Floating-point case
9655 if Is_Floating_Point_Type
(P_Type
) then
9657 (N
, Eval_Fat
.Pred
(P_Base_Type
, Expr_Value_R
(E1
)), Static
);
9661 elsif Is_Fixed_Point_Type
(P_Type
) then
9663 (N
, Expr_Value_R
(E1
) - Small_Value
(P_Type
), True);
9665 -- Modular integer case (wraps)
9667 elsif Is_Modular_Integer_Type
(P_Type
) then
9668 Fold_Uint
(N
, (Expr_Value
(E1
) - 1) mod Modulus
(P_Type
), Static
);
9670 -- Other scalar cases
9673 pragma Assert
(Is_Scalar_Type
(P_Type
));
9675 if Is_Enumeration_Type
(P_Type
)
9676 and then Expr_Value
(E1
) =
9677 Expr_Value
(Type_Low_Bound
(P_Base_Type
))
9679 Apply_Compile_Time_Constraint_Error
9680 (N
, "Pred of `&''First`",
9681 CE_Overflow_Check_Failed
,
9683 Warn
=> not Static
);
9689 Fold_Uint
(N
, Expr_Value
(E1
) - 1, Static
);
9692 ----------------------------------
9693 -- Preelaborable_Initialization --
9694 ----------------------------------
9696 when Attribute_Preelaborable_Initialization
=>
9700 (Boolean'Pos (Has_Preelaborable_Initialization
(P_Type
))),
9707 -- No processing required, because by this stage, Range has been
9708 -- replaced by First .. Last, so this branch can never be taken.
9710 when Attribute_Range
=>
9711 raise Program_Error
;
9717 when Attribute_Range_Length
=> Range_Length
: declare
9718 Diff
: aliased Uint
;
9723 -- Can fold if both bounds are compile time known
9725 if Compile_Time_Known_Value
(Hi_Bound
)
9726 and then Compile_Time_Known_Value
(Lo_Bound
)
9730 (0, Expr_Value
(Hi_Bound
) - Expr_Value
(Lo_Bound
) + 1),
9734 -- One more case is where Hi_Bound and Lo_Bound are compile-time
9735 -- comparable, and we can figure out the difference between them.
9737 case Compile_Time_Compare
9738 (Lo_Bound
, Hi_Bound
, Diff
'Access, Assume_Valid
=> False)
9741 Fold_Uint
(N
, Uint_1
, Static
);
9744 Fold_Uint
(N
, Uint_0
, Static
);
9747 if Present
(Diff
) then
9748 Fold_Uint
(N
, Diff
+ 1, Static
);
9760 when Attribute_Ref
=>
9761 Fold_Uint
(N
, Expr_Value
(E1
), Static
);
9767 when Attribute_Remainder
=> Remainder
: declare
9768 X
: constant Ureal
:= Expr_Value_R
(E1
);
9769 Y
: constant Ureal
:= Expr_Value_R
(E2
);
9772 if UR_Is_Zero
(Y
) then
9773 Apply_Compile_Time_Constraint_Error
9774 (N
, "division by zero in Remainder",
9775 CE_Overflow_Check_Failed
,
9776 Warn
=> not Static
);
9782 Fold_Ureal
(N
, Eval_Fat
.Remainder
(P_Base_Type
, X
, Y
), Static
);
9789 when Attribute_Restriction_Set
=>
9790 Rewrite
(N
, New_Occurrence_Of
(Standard_False
, Loc
));
9791 Set_Is_Static_Expression
(N
);
9797 when Attribute_Round
=> Round
: declare
9802 -- First we get the (exact result) in units of small
9804 Sr
:= Expr_Value_R
(E1
) / Small_Value
(C_Type
);
9806 -- Now round that exactly to an integer
9808 Si
:= UR_To_Uint
(Sr
);
9810 -- Finally the result is obtained by converting back to real
9812 Fold_Ureal
(N
, Si
* Small_Value
(C_Type
), Static
);
9819 when Attribute_Rounding
=>
9821 (N
, Eval_Fat
.Rounding
(P_Base_Type
, Expr_Value_R
(E1
)), Static
);
9827 when Attribute_Safe_Emax
=>
9828 Fold_Uint
(N
, Safe_Emax_Value
(P_Type
), Static
);
9834 when Attribute_Safe_First
=>
9835 Fold_Ureal
(N
, Safe_First_Value
(P_Type
), Static
);
9841 when Attribute_Safe_Large
=>
9842 if Is_Fixed_Point_Type
(P_Type
) then
9844 (N
, Expr_Value_R
(Type_High_Bound
(P_Base_Type
)), Static
);
9846 Fold_Ureal
(N
, Safe_Last_Value
(P_Type
), Static
);
9853 when Attribute_Safe_Last
=>
9854 Fold_Ureal
(N
, Safe_Last_Value
(P_Type
), Static
);
9860 when Attribute_Safe_Small
=>
9862 -- In Ada 95, the old Ada 83 attribute Safe_Small is redundant
9863 -- for fixed-point, since is the same as Small, but we implement
9864 -- it for backwards compatibility.
9866 if Is_Fixed_Point_Type
(P_Type
) then
9867 Fold_Ureal
(N
, Small_Value
(P_Type
), Static
);
9869 -- Ada 83 Safe_Small for floating-point cases
9872 Fold_Ureal
(N
, Model_Small_Value
(P_Type
), Static
);
9879 when Attribute_Scale
=>
9880 Fold_Uint
(N
, Scale_Value
(P_Type
), Static
);
9886 when Attribute_Scaling
=>
9890 (P_Base_Type
, Expr_Value_R
(E1
), Expr_Value
(E2
)),
9897 when Attribute_Signed_Zeros
=>
9899 (N
, UI_From_Int
(Boolean'Pos (Has_Signed_Zeros
(P_Type
))), Static
);
9905 -- Size attribute returns the RM size. All scalar types can be folded,
9906 -- as well as any types for which the size is known by the front end,
9907 -- including any type for which a size attribute is specified. This is
9908 -- one of the places where it is annoying that a size of zero means two
9909 -- things (zero size for scalars, unspecified size for non-scalars).
9912 | Attribute_VADS_Size
9915 P_TypeA
: constant Entity_Id
:= Underlying_Type
(P_Type
);
9919 (if Is_Scalar_Type
(P_TypeA
) then Known_RM_Size
(P_TypeA
));
9920 if Known_RM_Size
(P_TypeA
) then
9923 if Id
= Attribute_VADS_Size
or else Use_VADS_Size
then
9925 S
: constant Node_Id
:= Size_Clause
(P_TypeA
);
9928 -- If a size clause applies, then use the size from it.
9929 -- This is one of the rare cases where we can use the
9930 -- Size_Clause field for a subtype when Has_Size_Clause
9931 -- is False. Consider:
9933 -- type x is range 1 .. 64;
9934 -- for x'size use 12;
9935 -- subtype y is x range 0 .. 3;
9937 -- Here y has a size clause inherited from x, but
9938 -- normally it does not apply, and y'size is 2. However,
9939 -- y'VADS_Size is indeed 12 and not 2.
9942 and then Is_OK_Static_Expression
(Expression
(S
))
9944 Fold_Uint
(N
, Expr_Value
(Expression
(S
)), Static
);
9946 -- If no size is specified, then we simply use the object
9947 -- size in the VADS_Size case (e.g. Natural'Size is equal
9948 -- to Integer'Size, not one less).
9951 Fold_Uint
(N
, Esize
(P_TypeA
), Static
);
9955 -- Normal case (Size) in which case we want the RM_Size
9958 Fold_Uint
(N
, RM_Size
(P_TypeA
), Static
);
9967 when Attribute_Small
=>
9969 -- The floating-point case is present only for Ada 83 compatibility.
9970 -- Note that strictly this is an illegal addition, since we are
9971 -- extending an Ada 95 defined attribute, but we anticipate an
9972 -- ARG ruling that will permit this.
9974 if Is_Floating_Point_Type
(P_Type
) then
9976 -- Ada 83 attribute is defined as (RM83 3.5.8)
9978 -- T'Small = 2.0**(-T'Emax - 1)
9982 -- T'Emax = 4 * T'Mantissa
9984 Fold_Ureal
(N
, Ureal_2
** ((-(4 * Mantissa
)) - 1), Static
);
9986 -- Normal Ada 95 fixed-point case
9989 Fold_Ureal
(N
, Small_Value
(P_Type
), True);
9992 -----------------------
9993 -- Small_Denominator --
9994 -----------------------
9996 when Attribute_Small_Denominator
=>
9997 Fold_Uint
(N
, Norm_Den
(Small_Value
(P_Type
)), True);
9999 ---------------------
10000 -- Small_Numerator --
10001 ---------------------
10003 when Attribute_Small_Numerator
=>
10004 Fold_Uint
(N
, Norm_Num
(Small_Value
(P_Type
)), True);
10010 when Attribute_Stream_Size
=>
10017 when Attribute_Succ
=>
10018 -- Floating-point case
10020 if Is_Floating_Point_Type
(P_Type
) then
10022 (N
, Eval_Fat
.Succ
(P_Base_Type
, Expr_Value_R
(E1
)), Static
);
10024 -- Fixed-point case
10026 elsif Is_Fixed_Point_Type
(P_Type
) then
10027 Fold_Ureal
(N
, Expr_Value_R
(E1
) + Small_Value
(P_Type
), Static
);
10029 -- Modular integer case (wraps)
10031 elsif Is_Modular_Integer_Type
(P_Type
) then
10032 Fold_Uint
(N
, (Expr_Value
(E1
) + 1) mod Modulus
(P_Type
), Static
);
10034 -- Other scalar cases
10037 pragma Assert
(Is_Scalar_Type
(P_Type
));
10039 if Is_Enumeration_Type
(P_Type
)
10040 and then Expr_Value
(E1
) =
10041 Expr_Value
(Type_High_Bound
(P_Base_Type
))
10043 Apply_Compile_Time_Constraint_Error
10044 (N
, "Succ of `&''Last`",
10045 CE_Overflow_Check_Failed
,
10046 Ent
=> P_Base_Type
,
10047 Warn
=> not Static
);
10052 Fold_Uint
(N
, Expr_Value
(E1
) + 1, Static
);
10060 when Attribute_Truncation
=>
10063 Eval_Fat
.Truncation
(P_Base_Type
, Expr_Value_R
(E1
)),
10070 when Attribute_Type_Class
=> Type_Class
: declare
10071 Typ
: constant Entity_Id
:= Underlying_Type
(P_Base_Type
);
10075 if Is_Descendant_Of_Address
(Typ
) then
10076 Id
:= RE_Type_Class_Address
;
10078 elsif Is_Enumeration_Type
(Typ
) then
10079 Id
:= RE_Type_Class_Enumeration
;
10081 elsif Is_Integer_Type
(Typ
) then
10082 Id
:= RE_Type_Class_Integer
;
10084 elsif Is_Fixed_Point_Type
(Typ
) then
10085 Id
:= RE_Type_Class_Fixed_Point
;
10087 elsif Is_Floating_Point_Type
(Typ
) then
10088 Id
:= RE_Type_Class_Floating_Point
;
10090 elsif Is_Array_Type
(Typ
) then
10091 Id
:= RE_Type_Class_Array
;
10093 elsif Is_Record_Type
(Typ
) then
10094 Id
:= RE_Type_Class_Record
;
10096 elsif Is_Access_Type
(Typ
) then
10097 Id
:= RE_Type_Class_Access
;
10099 elsif Is_Task_Type
(Typ
) then
10100 Id
:= RE_Type_Class_Task
;
10102 -- We treat protected types like task types. It would make more
10103 -- sense to have another enumeration value, but after all the
10104 -- whole point of this feature is to be exactly DEC compatible,
10105 -- and changing the type Type_Class would not meet this requirement.
10107 elsif Is_Protected_Type
(Typ
) then
10108 Id
:= RE_Type_Class_Task
;
10110 -- Not clear if there are any other possibilities, but if there
10111 -- are, then we will treat them as the address case.
10114 Id
:= RE_Type_Class_Address
;
10117 Rewrite
(N
, New_Occurrence_Of
(RTE
(Id
), Loc
));
10120 -----------------------
10121 -- Unbiased_Rounding --
10122 -----------------------
10124 when Attribute_Unbiased_Rounding
=>
10127 Eval_Fat
.Unbiased_Rounding
(P_Base_Type
, Expr_Value_R
(E1
)),
10130 -------------------------
10131 -- Unconstrained_Array --
10132 -------------------------
10134 when Attribute_Unconstrained_Array
=> Unconstrained_Array
: declare
10135 Typ
: constant Entity_Id
:= Underlying_Type
(P_Type
);
10138 Rewrite
(N
, New_Occurrence_Of
(
10140 Is_Array_Type
(P_Type
)
10141 and then not Is_Constrained
(Typ
)), Loc
));
10143 -- Analyze and resolve as boolean, note that this attribute is
10144 -- a static attribute in GNAT.
10146 Analyze_And_Resolve
(N
, Standard_Boolean
);
10148 Set_Is_Static_Expression
(N
, True);
10149 end Unconstrained_Array
;
10151 -- Attribute Update is never static
10153 when Attribute_Update
=>
10160 -- Processing is shared with Size
10166 when Attribute_Val
=>
10167 if Expr_Value
(E1
) < Expr_Value
(Type_Low_Bound
(P_Base_Type
))
10169 Expr_Value
(E1
) > Expr_Value
(Type_High_Bound
(P_Base_Type
))
10171 Apply_Compile_Time_Constraint_Error
10172 (N
, "Val expression out of range",
10173 CE_Range_Check_Failed
,
10174 Warn
=> not Static
);
10180 Fold_Uint
(N
, Expr_Value
(E1
), Static
);
10187 -- The Value_Size attribute for a type returns the RM size of the type.
10188 -- This an always be folded for scalar types, and can also be folded for
10189 -- non-scalar types if the size is set. This is one of the places where
10190 -- it is annoying that a size of zero means two things!
10192 when Attribute_Value_Size
=> Value_Size
: declare
10193 P_TypeA
: constant Entity_Id
:= Underlying_Type
(P_Type
);
10197 (if Is_Scalar_Type
(P_TypeA
) then Known_RM_Size
(P_TypeA
));
10198 if Known_RM_Size
(P_TypeA
) then
10199 Fold_Uint
(N
, RM_Size
(P_TypeA
), Static
);
10207 -- Version can never be static
10209 when Attribute_Version
=>
10216 -- Wide_Image is a scalar attribute, but is never static, because it
10217 -- is not a static function (having a non-scalar argument (RM 4.9(22))
10219 when Attribute_Wide_Image
=>
10222 ---------------------
10223 -- Wide_Wide_Image --
10224 ---------------------
10226 -- Wide_Wide_Image is a scalar attribute but is never static, because it
10227 -- is not a static function (having a non-scalar argument (RM 4.9(22)).
10229 when Attribute_Wide_Wide_Image
=>
10232 ---------------------
10233 -- Wide_Wide_Width --
10234 ---------------------
10236 -- Processing for Wide_Wide_Width is combined with Width
10242 -- Processing for Wide_Width is combined with Width
10248 -- This processing also handles the case of Wide_[Wide_]Width
10250 when Attribute_Width
10251 | Attribute_Wide_Width
10252 | Attribute_Wide_Wide_Width
10254 if Compile_Time_Known_Bounds
(P_Type
) then
10256 -- Floating-point types
10258 if Is_Floating_Point_Type
(P_Type
) then
10260 -- Width is zero for a null range (RM 3.5 (38))
10262 if Expr_Value_R
(Type_High_Bound
(P_Type
)) <
10263 Expr_Value_R
(Type_Low_Bound
(P_Type
))
10265 Fold_Uint
(N
, Uint_0
, Static
);
10268 -- For floating-point, we have +N.dddE+nnn where length
10269 -- of ddd is determined by type'Digits - 1, but is one
10270 -- if Digits is one (RM 3.5 (33)).
10272 -- nnn is set to 2 for Short_Float and Float (32 bit
10273 -- floats), and 3 for Long_Float and Long_Long_Float.
10274 -- For machines where Long_Long_Float is the IEEE
10275 -- extended precision type, the exponent takes 4 digits.
10279 Int
'Max (2, UI_To_Int
(Digits_Value
(P_Type
)));
10282 if Esize
(P_Type
) <= 32 then
10284 elsif Esize
(P_Type
) = 64 then
10290 Fold_Uint
(N
, UI_From_Int
(Len
), Static
);
10294 -- Fixed-point types
10296 elsif Is_Fixed_Point_Type
(P_Type
) then
10298 -- Width is zero for a null range (RM 3.5 (38))
10300 if Expr_Value
(Type_High_Bound
(P_Type
)) <
10301 Expr_Value
(Type_Low_Bound
(P_Type
))
10303 Fold_Uint
(N
, Uint_0
, Static
);
10305 -- The non-null case depends on the specific real type
10308 -- For fixed-point type width is Fore + 1 + Aft (RM 3.5(34))
10311 (N
, UI_From_Int
(Fore_Value
+ 1) + Aft_Value
(P_Type
),
10319 R
: constant Entity_Id
:= Root_Type
(P_Type
);
10320 Lo
: constant Uint
:= Expr_Value
(Type_Low_Bound
(P_Type
));
10321 Hi
: constant Uint
:= Expr_Value
(Type_High_Bound
(P_Type
));
10334 -- Width for types derived from Standard.Character
10335 -- and Standard.Wide_[Wide_]Character.
10337 elsif Is_Standard_Character_Type
(P_Type
) then
10340 -- Set W larger if needed
10342 for J
in UI_To_Int
(Lo
) .. UI_To_Int
(Hi
) loop
10344 -- All wide characters look like Hex_hhhhhhhh
10348 -- No need to compute this more than once
10353 C
:= Character'Val (J
);
10355 -- Test for all cases where Character'Image
10356 -- yields an image that is longer than three
10357 -- characters. First the cases of Reserved_xxx
10358 -- names (length = 12).
10435 when Space
.. Tilde
10436 | No_Break_Space
.. LC_Y_Diaeresis
10438 -- Special case of soft hyphen in Ada 2005
10440 if C
= Character'Val (16#AD#
)
10441 and then Ada_Version
>= Ada_2005
10449 W
:= Int
'Max (W
, Wt
);
10453 -- Width for types derived from Standard.Boolean
10455 elsif R
= Standard_Boolean
then
10462 -- Width for integer types
10464 elsif Is_Integer_Type
(P_Type
) then
10465 T
:= UI_Max
(abs Lo
, abs Hi
);
10473 -- User declared enum type with discard names
10475 elsif Discard_Names
(R
) then
10477 -- If range is null, result is zero, that has already
10478 -- been dealt with, so what we need is the power of ten
10479 -- that accommodates the Pos of the largest value, which
10480 -- is the high bound of the range + one for the space.
10489 -- Only remaining possibility is user declared enum type
10490 -- with normal case of Discard_Names not active.
10493 pragma Assert
(Is_Enumeration_Type
(P_Type
));
10496 L
:= First_Literal
(P_Type
);
10497 while Present
(L
) loop
10499 -- Only pay attention to in range characters
10501 if Lo
<= Enumeration_Pos
(L
)
10502 and then Enumeration_Pos
(L
) <= Hi
10504 -- For Width case, use decoded name
10506 if Id
= Attribute_Width
then
10507 Get_Decoded_Name_String
(Chars
(L
));
10508 Wt
:= Nat
(Name_Len
);
10510 -- For Wide_[Wide_]Width, use encoded name, and
10511 -- then adjust for the encoding.
10514 Get_Name_String
(Chars
(L
));
10516 -- Character literals are always of length 3
10518 if Name_Buffer
(1) = 'Q' then
10521 -- Otherwise loop to adjust for upper/wide chars
10524 Wt
:= Nat
(Name_Len
);
10526 for J
in 1 .. Name_Len
loop
10527 if Name_Buffer
(J
) = 'U' then
10529 elsif Name_Buffer
(J
) = 'W' then
10536 W
:= Int
'Max (W
, Wt
);
10543 Fold_Uint
(N
, UI_From_Int
(W
), Static
);
10548 -- The following attributes denote functions that cannot be folded
10550 when Attribute_From_Any
10552 | Attribute_TypeCode
10556 -- The following attributes can never be folded, and furthermore we
10557 -- should not even have entered the case statement for any of these.
10558 -- Note that in some cases, the values have already been folded as
10559 -- a result of the processing in Analyze_Attribute or earlier in
10562 when Attribute_Abort_Signal
10564 | Attribute_Address
10565 | Attribute_Address_Size
10566 | Attribute_Asm_Input
10567 | Attribute_Asm_Output
10569 | Attribute_Bit_Order
10570 | Attribute_Bit_Position
10571 | Attribute_Callable
10574 | Attribute_Code_Address
10575 | Attribute_Compiler_Version
10577 | Attribute_Default_Bit_Order
10578 | Attribute_Default_Scalar_Storage_Order
10580 | Attribute_Elaborated
10581 | Attribute_Elab_Body
10582 | Attribute_Elab_Spec
10583 | Attribute_Elab_Subp_Body
10584 | Attribute_Enabled
10585 | Attribute_External_Tag
10586 | Attribute_Fast_Math
10587 | Attribute_First_Bit
10590 | Attribute_Initialized
10591 | Attribute_Last_Bit
10592 | Attribute_Library_Level
10593 | Attribute_Max_Integer_Size
10594 | Attribute_Maximum_Alignment
10597 | Attribute_Partition_ID
10598 | Attribute_Pool_Address
10599 | Attribute_Position
10600 | Attribute_Priority
10601 | Attribute_Put_Image
10604 | Attribute_Scalar_Storage_Order
10605 | Attribute_Simple_Storage_Pool
10606 | Attribute_Storage_Pool
10607 | Attribute_Storage_Size
10608 | Attribute_Storage_Unit
10609 | Attribute_Stub_Type
10610 | Attribute_System_Allocator_Alignment
10612 | Attribute_Target_Name
10613 | Attribute_Terminated
10614 | Attribute_To_Address
10615 | Attribute_Type_Key
10616 | Attribute_Unchecked_Access
10617 | Attribute_Universal_Literal_String
10618 | Attribute_Unrestricted_Access
10620 | Attribute_Valid_Scalars
10621 | Attribute_Valid_Value
10623 | Attribute_Wchar_T_Size
10624 | Attribute_Wide_Value
10625 | Attribute_Wide_Wide_Value
10626 | Attribute_Word_Size
10629 raise Program_Error
;
10632 -- At the end of the case, one more check. If we did a static evaluation
10633 -- so that the result is now a literal, then set Is_Static_Expression
10634 -- in the constant only if the prefix type is a static subtype. For
10635 -- non-static subtypes, the folding is still OK, but not static.
10637 -- An exception is the GNAT attribute Constrained_Array which is
10638 -- defined to be a static attribute in all cases.
10640 if Nkind
(N
) in N_Integer_Literal
10642 | N_Character_Literal
10644 or else (Is_Entity_Name
(N
)
10645 and then Ekind
(Entity
(N
)) = E_Enumeration_Literal
)
10647 Set_Is_Static_Expression
(N
, Static
);
10649 -- If this is still an attribute reference, then it has not been folded
10650 -- and that means that its expressions are in a non-static context.
10652 elsif Nkind
(N
) = N_Attribute_Reference
then
10655 -- Note: the else case not covered here are odd cases where the
10656 -- processing has transformed the attribute into something other
10657 -- than a constant. Nothing more to do in such cases.
10662 end Eval_Attribute
;
10664 ------------------------------
10665 -- Is_Anonymous_Tagged_Base --
10666 ------------------------------
10668 function Is_Anonymous_Tagged_Base
10670 Typ
: Entity_Id
) return Boolean
10674 Anon
= Current_Scope
10675 and then Is_Itype
(Anon
)
10676 and then Associated_Node_For_Itype
(Anon
) = Parent
(Typ
);
10677 end Is_Anonymous_Tagged_Base
;
10679 --------------------------------
10680 -- Name_Implies_Lvalue_Prefix --
10681 --------------------------------
10683 function Name_Implies_Lvalue_Prefix
(Nam
: Name_Id
) return Boolean is
10684 pragma Assert
(Is_Attribute_Name
(Nam
));
10686 return Attribute_Name_Implies_Lvalue_Prefix
(Get_Attribute_Id
(Nam
));
10687 end Name_Implies_Lvalue_Prefix
;
10689 -----------------------
10690 -- Resolve_Attribute --
10691 -----------------------
10693 procedure Resolve_Attribute
(N
: Node_Id
; Typ
: Entity_Id
) is
10694 Loc
: constant Source_Ptr
:= Sloc
(N
);
10695 P
: constant Node_Id
:= Prefix
(N
);
10696 Aname
: constant Name_Id
:= Attribute_Name
(N
);
10697 Attr_Id
: constant Attribute_Id
:= Get_Attribute_Id
(Aname
);
10698 Btyp
: constant Entity_Id
:= Base_Type
(Typ
);
10699 Des_Btyp
: Entity_Id
;
10700 Index
: Interp_Index
;
10702 Nom_Subt
: Entity_Id
;
10704 procedure Accessibility_Message
;
10705 -- Error, or warning within an instance, if the static accessibility
10706 -- rules of 3.10.2 are violated.
10708 function Declared_Within_Generic_Unit
10709 (Entity
: Entity_Id
;
10710 Generic_Unit
: Node_Id
) return Boolean;
10711 -- Returns True if Declared_Entity is declared within the declarative
10712 -- region of Generic_Unit; otherwise returns False.
10714 function Prefix_With_Safe_Accessibility_Level
return Boolean;
10715 -- Return True if the prefix does not have a value conversion of an
10716 -- array because a value conversion is like an aggregate with respect
10717 -- to determining accessibility level (RM 3.10.2); even if evaluation
10718 -- of a value conversion is guaranteed to not create a new object,
10719 -- accessibility rules are defined as if it might.
10721 ---------------------------
10722 -- Accessibility_Message --
10723 ---------------------------
10725 procedure Accessibility_Message
is
10726 Indic
: Node_Id
:= Parent
(Parent
(N
));
10729 -- In an instance, this is a runtime check, but one we
10730 -- know will fail, so generate an appropriate warning.
10732 if In_Instance_Body
then
10733 Error_Msg_Warn
:= SPARK_Mode
/= On
;
10735 ("non-local pointer cannot point to local object<<", P
);
10736 Error_Msg_F
("\Program_Error [<<", P
);
10738 Make_Raise_Program_Error
(Loc
,
10739 Reason
=> PE_Accessibility_Check_Failed
));
10740 Set_Etype
(N
, Typ
);
10744 Error_Msg_F
("non-local pointer cannot point to local object", P
);
10746 -- Check for case where we have a missing access definition
10748 if Is_Record_Type
(Current_Scope
)
10750 Nkind
(Parent
(N
)) in N_Discriminant_Association
10751 | N_Index_Or_Discriminant_Constraint
10753 Indic
:= Parent
(Parent
(N
));
10754 while Present
(Indic
)
10755 and then Nkind
(Indic
) /= N_Subtype_Indication
10757 Indic
:= Parent
(Indic
);
10760 if Present
(Indic
) then
10762 ("\use an access definition for" &
10763 " the access discriminant of&",
10764 N
, Entity
(Subtype_Mark
(Indic
)));
10768 end Accessibility_Message
;
10770 ----------------------------------
10771 -- Declared_Within_Generic_Unit --
10772 ----------------------------------
10774 function Declared_Within_Generic_Unit
10775 (Entity
: Entity_Id
;
10776 Generic_Unit
: Node_Id
) return Boolean
10778 Generic_Encloser
: Node_Id
:= Enclosing_Generic_Unit
(Entity
);
10781 while Present
(Generic_Encloser
) loop
10782 if Generic_Encloser
= Generic_Unit
then
10786 -- We have to step to the scope of the generic's entity, because
10787 -- otherwise we'll just get back the same generic.
10789 Generic_Encloser
:=
10790 Enclosing_Generic_Unit
10791 (Scope
(Defining_Entity
(Generic_Encloser
)));
10795 end Declared_Within_Generic_Unit
;
10797 ------------------------------------------
10798 -- Prefix_With_Safe_Accessibility_Level --
10799 ------------------------------------------
10801 function Prefix_With_Safe_Accessibility_Level
return Boolean is
10802 function Safe_Value_Conversions
return Boolean;
10803 -- Return False if the prefix has a value conversion of an array type
10805 ----------------------------
10806 -- Safe_Value_Conversions --
10807 ----------------------------
10809 function Safe_Value_Conversions
return Boolean is
10814 if Nkind
(PP
) in N_Selected_Component | N_Indexed_Component
then
10817 elsif Comes_From_Source
(PP
)
10818 and then Nkind
(PP
) in N_Type_Conversion
10819 | N_Unchecked_Type_Conversion
10820 and then Is_Array_Type
(Etype
(PP
))
10824 elsif Comes_From_Source
(PP
)
10825 and then Nkind
(PP
) = N_Qualified_Expression
10826 and then Is_Array_Type
(Etype
(PP
))
10827 and then Nkind
(Original_Node
(Expression
(PP
))) in
10828 N_Aggregate | N_Extension_Aggregate
10838 end Safe_Value_Conversions
;
10840 -- Start of processing for Prefix_With_Safe_Accessibility_Level
10843 -- No check required for unchecked and unrestricted access
10845 if Attr_Id
= Attribute_Unchecked_Access
10846 or else Attr_Id
= Attribute_Unrestricted_Access
10850 -- Check value conversions
10852 elsif Ekind
(Btyp
) = E_General_Access_Type
10853 and then not Safe_Value_Conversions
10859 end Prefix_With_Safe_Accessibility_Level
;
10861 -- Start of processing for Resolve_Attribute
10864 -- If error during analysis, no point in continuing, except for array
10865 -- types, where we get better recovery by using unconstrained indexes
10866 -- than nothing at all (see Check_Array_Type).
10868 if Error_Posted
(N
)
10869 and then Attr_Id
/= Attribute_First
10870 and then Attr_Id
/= Attribute_Last
10871 and then Attr_Id
/= Attribute_Length
10872 and then Attr_Id
/= Attribute_Range
10877 -- If attribute was universal type, reset to actual type
10879 if Is_Universal_Numeric_Type
(Etype
(N
)) then
10880 Set_Etype
(N
, Typ
);
10883 -- Remaining processing depends on attribute
10891 -- For access attributes, if the prefix denotes an entity, it is
10892 -- interpreted as a name, never as a call. It may be overloaded,
10893 -- in which case resolution uses the profile of the context type.
10894 -- Otherwise prefix must be resolved.
10896 when Attribute_Access
10897 | Attribute_Unchecked_Access
10898 | Attribute_Unrestricted_Access
10900 -- Note possible modification if we have a variable
10902 if Is_Variable
(P
) then
10904 PN
: constant Node_Id
:= Parent
(N
);
10907 Note
: Boolean := True;
10908 -- Skip this for the case of Unrestricted_Access occurring
10909 -- in the context of a Valid check, since this otherwise
10910 -- leads to a missed warning (the Valid check does not
10911 -- really modify!) If this case, Note will be reset to
10914 -- Skip it as well if the type is an Access_To_Constant,
10915 -- given that no use of the value can modify the prefix.
10918 if Attr_Id
= Attribute_Unrestricted_Access
10919 and then Nkind
(PN
) = N_Function_Call
10923 if Nkind
(Nm
) = N_Expanded_Name
10924 and then Chars
(Nm
) = Name_Valid
10925 and then Nkind
(Prefix
(Nm
)) = N_Identifier
10926 and then Chars
(Prefix
(Nm
)) = Name_Attr_Long_Float
10931 elsif Is_Access_Constant
(Typ
) then
10936 Note_Possible_Modification
(P
, Sure
=> False);
10941 -- Case where prefix is an entity name
10943 if Is_Entity_Name
(P
) then
10945 -- Deal with case where prefix itself is overloaded
10947 if Is_Overloaded
(P
) then
10948 Get_First_Interp
(P
, Index
, It
);
10949 while Present
(It
.Nam
) loop
10950 if Type_Conformant
(Designated_Type
(Typ
), It
.Nam
) then
10951 Set_Entity
(P
, It
.Nam
);
10953 -- The prefix is definitely NOT overloaded anymore at
10954 -- this point, so we reset the Is_Overloaded flag to
10955 -- avoid any confusion when reanalyzing the node.
10957 Set_Is_Overloaded
(P
, False);
10958 Set_Is_Overloaded
(N
, False);
10959 Generate_Reference
(Entity
(P
), P
);
10963 Get_Next_Interp
(Index
, It
);
10966 -- If Prefix is a subprogram name, this reference freezes,
10967 -- but not if within spec expression mode. The profile of
10968 -- the subprogram is not frozen at this point.
10970 if not In_Spec_Expression
then
10971 Freeze_Before
(N
, Entity
(P
), Do_Freeze_Profile
=> False);
10974 -- If it is a type, there is nothing to resolve.
10975 -- If it is a subprogram, do not freeze its profile.
10976 -- If it is an object, complete its resolution.
10978 elsif Is_Overloadable
(Entity
(P
)) then
10979 if not In_Spec_Expression
then
10980 Freeze_Before
(N
, Entity
(P
), Do_Freeze_Profile
=> False);
10983 -- Nothing to do if prefix is a type name
10985 elsif Is_Type
(Entity
(P
)) then
10988 -- Otherwise non-overloaded other case, resolve the prefix
10994 -- Some further error checks
10996 Error_Msg_Name_1
:= Aname
;
10998 if not Is_Entity_Name
(P
) then
11001 elsif Is_Overloadable
(Entity
(P
))
11002 and then Is_Abstract_Subprogram
(Entity
(P
))
11004 Error_Msg_F
("prefix of % attribute cannot be abstract", P
);
11005 Set_Etype
(N
, Any_Type
);
11007 elsif Ekind
(Entity
(P
)) = E_Enumeration_Literal
then
11009 ("prefix of % attribute cannot be enumeration literal", P
);
11010 Set_Etype
(N
, Any_Type
);
11012 -- An attempt to take 'Access of a function that renames an
11013 -- enumeration literal. Issue a specialized error message.
11015 elsif Ekind
(Entity
(P
)) = E_Function
11016 and then Present
(Alias
(Entity
(P
)))
11017 and then Ekind
(Alias
(Entity
(P
))) = E_Enumeration_Literal
11020 ("prefix of % attribute cannot be function renaming "
11021 & "an enumeration literal", P
);
11022 Set_Etype
(N
, Any_Type
);
11024 elsif Convention
(Entity
(P
)) = Convention_Intrinsic
then
11025 Error_Msg_F
("prefix of % attribute cannot be intrinsic", P
);
11026 Set_Etype
(N
, Any_Type
);
11029 -- Assignments, return statements, components of aggregates,
11030 -- generic instantiations will require convention checks if
11031 -- the type is an access to subprogram. Given that there will
11032 -- also be accessibility checks on those, this is where the
11033 -- checks can eventually be centralized ???
11035 if Ekind
(Btyp
) in E_Access_Protected_Subprogram_Type
11036 | E_Access_Subprogram_Type
11037 | E_Anonymous_Access_Protected_Subprogram_Type
11038 | E_Anonymous_Access_Subprogram_Type
11040 -- Deal with convention mismatch
11042 if Convention
(Designated_Type
(Btyp
)) /=
11043 Convention
(Entity
(P
))
11046 ("subprogram & has wrong convention", P
, Entity
(P
));
11047 Error_Msg_Sloc
:= Sloc
(Btyp
);
11048 Error_Msg_FE
("\does not match & declared#", P
, Btyp
);
11050 if not Is_Itype
(Btyp
)
11051 and then not Has_Convention_Pragma
(Btyp
)
11052 and then Convention
(Entity
(P
)) /= Convention_Intrinsic
11055 ("\probable missing pragma Convention for &",
11060 Check_Subtype_Conformant
11061 (New_Id
=> Entity
(P
),
11062 Old_Id
=> Designated_Type
(Btyp
),
11066 if Attr_Id
= Attribute_Unchecked_Access
then
11067 Error_Msg_Name_1
:= Aname
;
11069 ("attribute% cannot be applied to a subprogram", P
);
11071 elsif Aname
= Name_Unrestricted_Access
then
11072 null; -- Nothing to check
11074 -- Check the static accessibility rule of 3.10.2(32).
11075 -- This rule also applies within the private part of an
11076 -- instantiation. This rule does not apply to anonymous
11077 -- access-to-subprogram types in access parameters.
11079 elsif Attr_Id
= Attribute_Access
11080 and then not In_Instance_Body
11082 (Ekind
(Btyp
) = E_Access_Subprogram_Type
11083 or else Is_Local_Anonymous_Access
(Btyp
))
11084 and then Subprogram_Access_Level
(Entity
(P
)) >
11085 Type_Access_Level
(Btyp
)
11088 ("subprogram must not be deeper than access type", P
);
11090 -- Check the restriction of 3.10.2(32) that disallows the
11091 -- access attribute within a generic body when the ultimate
11092 -- ancestor of the type of the attribute is declared outside
11093 -- of the generic unit and the subprogram is declared within
11094 -- that generic unit. This includes any such attribute that
11095 -- occurs within the body of a generic unit that is a child
11096 -- of the generic unit where the subprogram is declared.
11098 -- The rule also prohibits applying the attribute when the
11099 -- access type is a generic formal access type (since the
11100 -- level of the actual type is not known). This restriction
11101 -- does not apply when the attribute type is an anonymous
11102 -- access-to-subprogram type. Note that this check was
11103 -- revised by AI-229, because the original Ada 95 rule
11104 -- was too lax. The original rule only applied when the
11105 -- subprogram was declared within the body of the generic,
11106 -- which allowed the possibility of dangling references).
11107 -- The rule was also too strict in some cases, in that it
11108 -- didn't permit the access to be declared in the generic
11109 -- spec, whereas the revised rule does (as long as it's not
11112 -- There are a couple of subtleties of the test for applying
11113 -- the check that are worth noting. First, we only apply it
11114 -- when the levels of the subprogram and access type are the
11115 -- same (the case where the subprogram is statically deeper
11116 -- was applied above, and the case where the type is deeper
11117 -- is always safe). Second, we want the check to apply
11118 -- within nested generic bodies and generic child unit
11119 -- bodies, but not to apply to an attribute that appears in
11120 -- the generic unit's specification. This is done by testing
11121 -- that the attribute's innermost enclosing generic body is
11122 -- not the same as the innermost generic body enclosing the
11123 -- generic unit where the subprogram is declared (we don't
11124 -- want the check to apply when the access attribute is in
11125 -- the spec and there's some other generic body enclosing
11126 -- generic). Finally, there's no point applying the check
11127 -- when within an instance, because any violations will have
11128 -- been caught by the compilation of the generic unit.
11130 -- We relax this check in Relaxed_RM_Semantics mode for
11131 -- compatibility with legacy code for use by Ada source
11132 -- code analyzers (e.g. CodePeer).
11134 elsif Attr_Id
= Attribute_Access
11135 and then not Relaxed_RM_Semantics
11136 and then not In_Instance
11137 and then Present
(Enclosing_Generic_Unit
(Entity
(P
)))
11138 and then Present
(Enclosing_Generic_Body
(N
))
11139 and then Enclosing_Generic_Body
(N
) /=
11140 Enclosing_Generic_Body
11141 (Enclosing_Generic_Unit
(Entity
(P
)))
11142 and then Subprogram_Access_Level
(Entity
(P
)) =
11143 Type_Access_Level
(Btyp
)
11144 and then Ekind
(Btyp
) /=
11145 E_Anonymous_Access_Subprogram_Type
11146 and then Ekind
(Btyp
) /=
11147 E_Anonymous_Access_Protected_Subprogram_Type
11149 -- The attribute type's ultimate ancestor must be
11150 -- declared within the same generic unit as the
11151 -- subprogram is declared (including within another
11152 -- nested generic unit). The error message is
11153 -- specialized to say "ancestor" for the case where the
11154 -- access type is not its own ancestor, since saying
11155 -- simply "access type" would be very confusing.
11157 if not Declared_Within_Generic_Unit
11159 Enclosing_Generic_Unit
(Entity
(P
)))
11162 ("''Access attribute not allowed in generic body",
11165 if Root_Type
(Btyp
) = Btyp
then
11168 "access type & is declared outside " &
11169 "generic unit (RM 3.10.2(32))", N
, Btyp
);
11172 ("\because ancestor of " &
11173 "access type & is declared outside " &
11174 "generic unit (RM 3.10.2(32))", N
, Btyp
);
11178 ("\move ''Access to private part, or " &
11179 "(Ada 2005) use anonymous access type instead of &",
11182 -- If the ultimate ancestor of the attribute's type is
11183 -- a formal type, then the attribute is illegal because
11184 -- the actual type might be declared at a higher level.
11185 -- The error message is specialized to say "ancestor"
11186 -- for the case where the access type is not its own
11187 -- ancestor, since saying simply "access type" would be
11190 elsif Is_Generic_Type
(Root_Type
(Btyp
)) then
11191 if Root_Type
(Btyp
) = Btyp
then
11193 ("access type must not be a generic formal type",
11197 ("ancestor access type must not be a generic " &
11204 -- If this is a renaming, an inherited operation, or a
11205 -- subprogram instance, use the original entity. This may make
11206 -- the node type-inconsistent, so this transformation can only
11207 -- be done if the node will not be reanalyzed. In particular,
11208 -- if it is within a default expression, the transformation
11209 -- must be delayed until the default subprogram is created for
11210 -- it, when the enclosing subprogram is frozen.
11212 if Is_Entity_Name
(P
)
11213 and then Is_Overloadable
(Entity
(P
))
11214 and then Present
(Alias
(Entity
(P
)))
11215 and then Expander_Active
11218 New_Occurrence_Of
(Alias
(Entity
(P
)), Sloc
(P
)));
11221 elsif Nkind
(P
) = N_Selected_Component
11222 and then Is_Overloadable
(Entity
(Selector_Name
(P
)))
11224 -- Protected operation. If operation is overloaded, must
11225 -- disambiguate. Prefix that denotes protected object itself
11226 -- is resolved with its own type.
11228 if Attr_Id
= Attribute_Unchecked_Access
then
11229 Error_Msg_Name_1
:= Aname
;
11231 ("attribute% cannot be applied to protected operation", P
);
11234 Resolve
(Prefix
(P
));
11236 if not Is_Overloaded
(P
) then
11237 Generate_Reference
(Entity
(Selector_Name
(P
)), P
);
11240 Get_First_Interp
(P
, Index
, It
);
11241 while Present
(It
.Nam
) loop
11242 if Type_Conformant
(Designated_Type
(Typ
), It
.Nam
) then
11243 Set_Entity
(Selector_Name
(P
), It
.Nam
);
11245 -- The prefix is definitely NOT overloaded anymore at
11246 -- this point, so we reset the Is_Overloaded flag to
11247 -- avoid any confusion when reanalyzing the node.
11249 Set_Is_Overloaded
(P
, False);
11250 Set_Is_Overloaded
(N
, False);
11251 Generate_Reference
(Entity
(Selector_Name
(P
)), P
);
11255 Get_Next_Interp
(Index
, It
);
11259 -- Implement check implied by 3.10.2 (18.1/2) : F.all'access is
11260 -- statically illegal if F is an anonymous access to subprogram.
11262 elsif Nkind
(P
) = N_Explicit_Dereference
11263 and then Is_Entity_Name
(Prefix
(P
))
11264 and then Ekind
(Etype
(Entity
(Prefix
(P
)))) =
11265 E_Anonymous_Access_Subprogram_Type
11267 Error_Msg_N
("anonymous access to subprogram "
11268 & "has deeper accessibility than any master", P
);
11270 elsif Is_Overloaded
(P
) then
11272 -- Use the designated type of the context to disambiguate
11273 -- Note that this was not strictly conformant to Ada 95,
11274 -- but was the implementation adopted by most Ada 95 compilers.
11275 -- The use of the context type to resolve an Access attribute
11276 -- reference is now mandated in AI-235 for Ada 2005.
11279 Index
: Interp_Index
;
11283 Get_First_Interp
(P
, Index
, It
);
11284 while Present
(It
.Typ
) loop
11285 if Covers
(Designated_Type
(Typ
), It
.Typ
) then
11286 Resolve
(P
, It
.Typ
);
11290 Get_Next_Interp
(Index
, It
);
11297 -- X'Access is illegal if X denotes a constant and the access type
11298 -- is access-to-variable. Same for 'Unchecked_Access. The rule
11299 -- does not apply to 'Unrestricted_Access. If the reference is a
11300 -- default-initialized aggregate component for a self-referential
11301 -- type the reference is legal.
11303 if not (Ekind
(Btyp
) = E_Access_Subprogram_Type
11304 or else Ekind
(Btyp
) = E_Anonymous_Access_Subprogram_Type
11305 or else (Is_Record_Type
(Btyp
)
11307 Present
(Corresponding_Remote_Type
(Btyp
)))
11308 or else Ekind
(Btyp
) = E_Access_Protected_Subprogram_Type
11309 or else Ekind
(Btyp
)
11310 = E_Anonymous_Access_Protected_Subprogram_Type
11311 or else Is_Access_Constant
(Btyp
)
11312 or else Is_Variable
(P
)
11313 or else Attr_Id
= Attribute_Unrestricted_Access
)
11315 if Is_Entity_Name
(P
)
11316 and then Is_Type
(Entity
(P
))
11318 -- Legality of a self-reference through an access
11319 -- attribute has been verified in Analyze_Access_Attribute.
11323 elsif Comes_From_Source
(N
) then
11324 Error_Msg_F
("access-to-variable designates constant", P
);
11328 Des_Btyp
:= Designated_Type
(Btyp
);
11330 if Ada_Version
>= Ada_2005
11331 and then Is_Incomplete_Type
(Des_Btyp
)
11333 -- Ada 2005 (AI-412): If the (sub)type is a limited view of an
11334 -- imported entity, and the non-limited view is visible, make
11335 -- use of it. If it is an incomplete subtype, use the base type
11338 if From_Limited_With
(Des_Btyp
)
11339 and then Present
(Non_Limited_View
(Des_Btyp
))
11341 Des_Btyp
:= Non_Limited_View
(Des_Btyp
);
11343 elsif Ekind
(Des_Btyp
) = E_Incomplete_Subtype
then
11344 Des_Btyp
:= Etype
(Des_Btyp
);
11348 if (Attr_Id
= Attribute_Access
11350 Attr_Id
= Attribute_Unchecked_Access
)
11351 and then (Ekind
(Btyp
) = E_General_Access_Type
11352 or else Ekind
(Btyp
) = E_Anonymous_Access_Type
)
11354 -- Ada 2005 (AI-230): Check the accessibility of anonymous
11355 -- access types for stand-alone objects, record and array
11356 -- components, and return objects. For a component definition
11357 -- the level is the same of the enclosing composite type.
11359 if Ada_Version
>= Ada_2005
11360 and then (Is_Local_Anonymous_Access
(Btyp
)
11362 -- Handle cases where Btyp is the anonymous access
11363 -- type of an Ada 2012 stand-alone object.
11365 or else Nkind
(Associated_Node_For_Itype
(Btyp
)) =
11366 N_Object_Declaration
)
11367 and then Attr_Id
= Attribute_Access
11369 -- Verify that static checking is OK (namely that we aren't
11370 -- in a specific context requiring dynamic checks on
11371 -- expicitly aliased parameters), and then check the level.
11373 -- Otherwise a check will be generated later when the return
11374 -- statement gets expanded.
11376 and then not Is_Special_Aliased_Formal_Access
(N
)
11378 Static_Accessibility_Level
(N
, Zero_On_Dynamic_Level
) >
11379 Deepest_Type_Access_Level
(Btyp
)
11381 -- In an instance, this is a runtime check, but one we know
11382 -- will fail, so generate an appropriate warning. As usual,
11383 -- this kind of warning is an error in SPARK mode.
11385 if In_Instance_Body
then
11389 not No_Dynamic_Accessibility_Checks_Enabled
(P
);
11392 ("non-local pointer cannot point to local object<<", P
);
11393 Error_Msg_F
("\Program_Error [<<", P
);
11396 Make_Raise_Program_Error
(Loc
,
11397 Reason
=> PE_Accessibility_Check_Failed
));
11398 Set_Etype
(N
, Typ
);
11402 ("non-local pointer cannot point to local object", P
);
11406 if Is_Dependent_Component_Of_Mutable_Object
(P
) then
11408 ("illegal attribute for discriminant-dependent component",
11412 -- Check static matching rule of 3.10.2(27). Nominal subtype
11413 -- of the prefix must statically match the designated type.
11415 Nom_Subt
:= Etype
(P
);
11417 if Is_Constr_Subt_For_U_Nominal
(Nom_Subt
) then
11418 Nom_Subt
:= Base_Type
(Nom_Subt
);
11421 if Is_Tagged_Type
(Designated_Type
(Typ
)) then
11423 -- If the attribute is in the context of an access
11424 -- parameter, then the prefix is allowed to be of
11425 -- the class-wide type (by AI-127).
11427 if Ekind
(Typ
) = E_Anonymous_Access_Type
then
11428 if not Covers
(Designated_Type
(Typ
), Nom_Subt
)
11429 and then not Covers
(Nom_Subt
, Designated_Type
(Typ
))
11435 Desig
:= Designated_Type
(Typ
);
11437 if Is_Class_Wide_Type
(Desig
) then
11438 Desig
:= Etype
(Desig
);
11441 if Is_Anonymous_Tagged_Base
(Nom_Subt
, Desig
) then
11446 ("type of prefix: & not compatible",
11449 ("\with &, the expected designated type",
11450 P
, Designated_Type
(Typ
));
11455 elsif not Covers
(Designated_Type
(Typ
), Nom_Subt
)
11457 (not Is_Class_Wide_Type
(Designated_Type
(Typ
))
11458 and then Is_Class_Wide_Type
(Nom_Subt
))
11461 ("type of prefix: & is not covered", P
, Nom_Subt
);
11463 ("\by &, the expected designated type" &
11464 " (RM 3.10.2 (27))", P
, Designated_Type
(Typ
));
11467 if Is_Class_Wide_Type
(Designated_Type
(Typ
))
11468 and then Has_Discriminants
(Etype
(Designated_Type
(Typ
)))
11469 and then Is_Constrained
(Etype
(Designated_Type
(Typ
)))
11470 and then Designated_Type
(Typ
) /= Nom_Subt
11472 Apply_Discriminant_Check
11473 (N
, Etype
(Designated_Type
(Typ
)));
11476 -- Ada 2005 (AI-363): Require static matching when designated
11477 -- type has discriminants and a constrained partial view, since
11478 -- in general objects of such types are mutable, so we can't
11479 -- allow the access value to designate a constrained object
11480 -- (because access values must be assumed to designate mutable
11481 -- objects when designated type does not impose a constraint).
11483 elsif Subtypes_Statically_Match
(Des_Btyp
, Nom_Subt
) then
11486 elsif Has_Discriminants
(Designated_Type
(Typ
))
11487 and then not Is_Constrained
(Des_Btyp
)
11489 (Ada_Version
< Ada_2005
11491 not Object_Type_Has_Constrained_Partial_View
11492 (Typ
=> Designated_Type
(Base_Type
(Typ
)),
11493 Scop
=> Current_Scope
))
11499 ("object subtype must statically match "
11500 & "designated subtype", P
);
11502 if Is_Entity_Name
(P
)
11503 and then Is_Array_Type
(Designated_Type
(Typ
))
11506 D
: constant Node_Id
:= Declaration_Node
(Entity
(P
));
11509 ("aliased object has explicit bounds??", D
);
11511 ("\declare without bounds (and with explicit "
11512 & "initialization)??", D
);
11514 ("\for use with unconstrained access??", D
);
11519 -- Check the static accessibility rule of 3.10.2(28). Note that
11520 -- this check is not performed for the case of an anonymous
11521 -- access type, since the access attribute is always legal
11522 -- in such a context - unless the restriction
11523 -- No_Dynamic_Accessibility_Checks is active.
11526 No_Dynamic_Acc_Checks
: constant Boolean :=
11527 No_Dynamic_Accessibility_Checks_Enabled
(Btyp
);
11529 Compatible_Alt_Checks
: constant Boolean :=
11530 No_Dynamic_Acc_Checks
and then not Debug_Flag_Underscore_B
;
11532 if Attr_Id
/= Attribute_Unchecked_Access
11533 and then (Ekind
(Btyp
) = E_General_Access_Type
11534 or else No_Dynamic_Acc_Checks
)
11536 -- In the case of the alternate "compatibility"
11537 -- accessibility model we do not perform a static
11538 -- accessibility check on actuals for anonymous access
11539 -- types - so exclude them here.
11541 and then not (Compatible_Alt_Checks
11542 and then Is_Actual_Parameter
(N
)
11543 and then Ekind
(Btyp
)
11544 = E_Anonymous_Access_Type
)
11546 -- Call Accessibility_Level directly to avoid returning
11547 -- zero on cases where the prefix is an explicitly aliased
11548 -- parameter in a return statement, instead of using the
11549 -- normal Static_Accessibility_Level function.
11551 -- Shouldn't this be handled somehow in
11552 -- Static_Accessibility_Level ???
11554 and then Nkind
(Accessibility_Level
(P
, Dynamic_Level
))
11555 = N_Integer_Literal
11557 Intval
(Accessibility_Level
(P
, Dynamic_Level
))
11558 > Deepest_Type_Access_Level
(Btyp
)
11560 Accessibility_Message
;
11566 if Ekind
(Btyp
) in E_Access_Protected_Subprogram_Type
11567 | E_Anonymous_Access_Protected_Subprogram_Type
11569 if Is_Entity_Name
(P
)
11570 and then not Is_Protected_Type
(Scope
(Entity
(P
)))
11572 Error_Msg_F
("context requires a protected subprogram", P
);
11574 -- Check accessibility of protected object against that of the
11575 -- access type, but only on user code, because the expander
11576 -- creates access references for handlers. If the context is an
11577 -- anonymous_access_to_protected, there are no accessibility
11578 -- checks either. Omit check entirely for Unrestricted_Access.
11580 elsif Static_Accessibility_Level
(P
, Zero_On_Dynamic_Level
)
11581 > Deepest_Type_Access_Level
(Btyp
)
11582 and then Comes_From_Source
(N
)
11583 and then Ekind
(Btyp
) = E_Access_Protected_Subprogram_Type
11584 and then Attr_Id
/= Attribute_Unrestricted_Access
11586 Accessibility_Message
;
11589 -- AI05-0225: If the context is not an access to protected
11590 -- function, the prefix must be a variable, given that it may
11591 -- be used subsequently in a protected call.
11593 elsif Nkind
(P
) = N_Selected_Component
11594 and then not Is_Variable
(Prefix
(P
))
11595 and then Ekind
(Entity
(Selector_Name
(P
))) /= E_Function
11598 ("target object of access to protected procedure "
11599 & "must be variable", N
);
11601 elsif Is_Entity_Name
(P
) then
11602 Check_Internal_Protected_Use
(N
, Entity
(P
));
11605 elsif Ekind
(Btyp
) in E_Access_Subprogram_Type
11606 | E_Anonymous_Access_Subprogram_Type
11607 and then Ekind
(Etype
(N
)) = E_Access_Protected_Subprogram_Type
11609 Error_Msg_F
("context requires a non-protected subprogram", P
);
11612 -- AI12-0412: The rule in RM 6.1.1(18.2/5) disallows applying
11613 -- attribute Access to a primitive of an abstract type when the
11614 -- primitive has any Pre'Class or Post'Class aspects specified
11615 -- with nonstatic expressions.
11617 if Attr_Id
= Attribute_Access
11618 and then Ekind
(Btyp
) in E_Access_Subprogram_Type
11619 | E_Anonymous_Access_Subprogram_Type
11620 and then Is_Entity_Name
(P
)
11621 and then Is_Dispatching_Operation
(Entity
(P
))
11623 Is_Prim_Of_Abst_Type_With_Nonstatic_CW_Pre_Post
(Entity
(P
))
11626 ("attribute not allowed for primitive of abstract type with "
11627 & "nonstatic class-wide pre/postconditions",
11631 -- The context cannot be a pool-specific type, but this is a
11632 -- legality rule, not a resolution rule, so it must be checked
11633 -- separately, after possibly disambiguation (see AI-245).
11635 if Ekind
(Btyp
) = E_Access_Type
11636 and then Attr_Id
/= Attribute_Unrestricted_Access
11638 Wrong_Type
(N
, Typ
);
11641 -- The context may be a constrained access type (however ill-
11642 -- advised such subtypes might be) so in order to generate a
11643 -- constraint check we need to set the type of the attribute
11644 -- reference to the base type of the context.
11646 Set_Etype
(N
, Btyp
);
11648 -- Check for incorrect atomic/volatile/VFA reference (RM C.6(12))
11650 if Attr_Id
/= Attribute_Unrestricted_Access
then
11651 if Is_Atomic_Object
(P
)
11652 and then not Is_Atomic
(Designated_Type
(Typ
))
11655 ("access to atomic object cannot yield access-to-" &
11656 "non-atomic type", P
);
11658 elsif Is_Volatile_Object_Ref
(P
)
11659 and then not Is_Volatile
(Designated_Type
(Typ
))
11662 ("access to volatile object cannot yield access-to-" &
11663 "non-volatile type", P
);
11665 elsif Is_Volatile_Full_Access_Object_Ref
(P
)
11666 and then not Is_Volatile_Full_Access
(Designated_Type
(Typ
))
11669 ("access to full access object cannot yield access-to-" &
11670 "non-full-access type", P
);
11673 -- Check for nonatomic subcomponent of a full access object
11674 -- in Ada 2022 (RM C.6 (12)).
11676 if Ada_Version
>= Ada_2022
11677 and then Is_Subcomponent_Of_Full_Access_Object
(P
)
11678 and then not Is_Atomic_Object
(P
)
11681 ("cannot have access attribute with prefix &", N
, P
);
11683 ("\nonatomic subcomponent of full access object "
11684 & "(RM C.6(12))", N
);
11688 -- Check for aliased view. We allow a nonaliased prefix when in
11689 -- an instance because the prefix may have been a tagged formal
11690 -- object, which is defined to be aliased even when the actual
11691 -- might not be (other instance cases will have been caught in
11692 -- the generic). Similarly, within an inlined body we know that
11693 -- the attribute is legal in the original subprogram, therefore
11694 -- legal in the expansion.
11696 if not (Is_Entity_Name
(P
)
11697 and then Is_Overloadable
(Entity
(P
)))
11698 and then not (Nkind
(P
) = N_Selected_Component
11700 Is_Overloadable
(Entity
(Selector_Name
(P
))))
11701 and then not Is_Aliased_View
(Original_Node
(P
))
11702 and then not In_Instance
11703 and then not In_Inlined_Body
11704 and then Comes_From_Source
(N
)
11706 -- Here we have a non-aliased view. This is illegal unless we
11707 -- have the case of Unrestricted_Access, where for now we allow
11708 -- this (we will reject later if expected type is access to an
11709 -- unconstrained array with a thin pointer).
11711 -- No need for an error message on a generated access reference
11712 -- for the controlling argument in a dispatching call: error
11713 -- will be reported when resolving the call.
11715 if Attr_Id
/= Attribute_Unrestricted_Access
then
11716 Error_Msg_Name_1
:= Aname
;
11717 Error_Msg_N
("prefix of % attribute must be aliased", P
);
11719 -- Check for unrestricted access where expected type is a thin
11720 -- pointer to an unconstrained array.
11722 elsif Has_Size_Clause
(Typ
)
11723 and then RM_Size
(Typ
) = System_Address_Size
11726 DT
: constant Entity_Id
:= Designated_Type
(Typ
);
11728 if Is_Array_Type
(DT
)
11729 and then not Is_Constrained
(DT
)
11732 ("illegal use of Unrestricted_Access attribute", P
);
11734 ("\attempt to generate thin pointer to unaliased "
11741 -- Check that the prefix does not have a value conversion of an
11742 -- array type since a value conversion is like an aggregate with
11743 -- respect to determining accessibility level (RM 3.10.2).
11745 if not Prefix_With_Safe_Accessibility_Level
then
11746 Accessibility_Message
;
11750 -- Mark that address of entity is taken in case of
11751 -- 'Unrestricted_Access or in case of a subprogram.
11753 if Is_Entity_Name
(P
)
11754 and then (Attr_Id
= Attribute_Unrestricted_Access
11755 or else Is_Subprogram
(Entity
(P
)))
11757 Set_Address_Taken
(Entity
(P
));
11760 -- Deal with possible elaboration check
11762 if Is_Entity_Name
(P
) and then Is_Subprogram
(Entity
(P
)) then
11764 Subp_Id
: constant Entity_Id
:= Entity
(P
);
11765 Scop
: constant Entity_Id
:= Scope
(Subp_Id
);
11766 Subp_Decl
: constant Node_Id
:=
11767 Unit_Declaration_Node
(Subp_Id
);
11768 Flag_Id
: Entity_Id
;
11769 Subp_Body
: Node_Id
;
11771 -- If the access has been taken and the body of the subprogram
11772 -- has not been see yet, indirect calls must be protected with
11773 -- elaboration checks. We have the proper elaboration machinery
11774 -- for subprograms declared in packages, but within a block or
11775 -- a subprogram the body will appear in the same declarative
11776 -- part, and we must insert a check in the eventual body itself
11777 -- using the elaboration flag that we generate now. The check
11778 -- is then inserted when the body is expanded. This processing
11779 -- is not needed for a stand alone expression function because
11780 -- the internally generated spec and body are always inserted
11781 -- as a pair in the same declarative list.
11785 and then Comes_From_Source
(Subp_Id
)
11786 and then Comes_From_Source
(N
)
11787 and then In_Open_Scopes
(Scop
)
11788 and then Ekind
(Scop
) in E_Block | E_Procedure | E_Function
11789 and then not Has_Completion
(Subp_Id
)
11790 and then No
(Elaboration_Entity
(Subp_Id
))
11791 and then Nkind
(Subp_Decl
) = N_Subprogram_Declaration
11792 and then Nkind
(Original_Node
(Subp_Decl
)) /=
11793 N_Expression_Function
11795 -- Create elaboration variable for it
11797 Flag_Id
:= Make_Temporary
(Loc
, 'E');
11798 Set_Elaboration_Entity
(Subp_Id
, Flag_Id
);
11799 Set_Is_Frozen
(Flag_Id
);
11801 -- Insert declaration for flag after subprogram
11802 -- declaration. Note that attribute reference may
11803 -- appear within a nested scope.
11805 Insert_After_And_Analyze
(Subp_Decl
,
11806 Make_Object_Declaration
(Loc
,
11807 Defining_Identifier
=> Flag_Id
,
11808 Object_Definition
=>
11809 New_Occurrence_Of
(Standard_Short_Integer
, Loc
),
11811 Make_Integer_Literal
(Loc
, Uint_0
)));
11813 -- The above sets the Scope of the flag entity to the
11814 -- current scope, in which the attribute appears, but
11815 -- the flag declaration has been inserted after that
11816 -- of Subp_Id, so the scope of the flag is the same as
11817 -- that of Subp_Id. This is relevant when unnesting,
11818 -- where processing depends on correct scope setting.
11820 Set_Scope
(Flag_Id
, Scop
);
11823 -- Taking the 'Access of an expression function freezes its
11824 -- expression (RM 13.14 10.3/3). This does not apply to an
11825 -- expression function that acts as a completion because the
11826 -- generated body is immediately analyzed and the expression
11827 -- is automatically frozen.
11829 if Is_Expression_Function
(Subp_Id
)
11830 and then Present
(Corresponding_Body
(Subp_Decl
))
11833 Unit_Declaration_Node
(Corresponding_Body
(Subp_Decl
));
11835 -- The body has already been analyzed when the expression
11836 -- function acts as a completion.
11838 if Analyzed
(Subp_Body
) then
11841 -- Attribute 'Access may appear within the generated body
11842 -- of the expression function subject to the attribute:
11844 -- function F is (... F'Access ...);
11846 -- If the expression function is on the scope stack, then
11847 -- the body is currently being analyzed. Do not reanalyze
11848 -- it because this will lead to infinite recursion.
11850 elsif In_Open_Scopes
(Subp_Id
) then
11853 -- If reference to the expression function appears in an
11854 -- inner scope, for example as an actual in an instance,
11855 -- this is not a freeze point either.
11857 elsif Scope
(Subp_Id
) /= Current_Scope
then
11860 -- Analyze the body of the expression function to freeze
11861 -- the expression. This takes care of the case where the
11862 -- 'Access is part of dispatch table initialization and
11863 -- the generated body of the expression function has not
11864 -- been analyzed yet.
11867 Analyze
(Subp_Body
);
11877 -- Deal with resolving the type for Address attribute, overloading
11878 -- is not permitted here, since there is no context to resolve it.
11880 when Attribute_Address
11881 | Attribute_Code_Address
11883 -- To be safe, assume that if the address of a variable is taken,
11884 -- it may be modified via this address, so note modification.
11886 if Is_Variable
(P
) then
11887 Note_Possible_Modification
(P
, Sure
=> False);
11890 if Nkind
(P
) in N_Subexpr
11891 and then Is_Overloaded
(P
)
11893 Get_First_Interp
(P
, Index
, It
);
11894 Get_Next_Interp
(Index
, It
);
11896 if Present
(It
.Nam
) then
11897 Error_Msg_Name_1
:= Aname
;
11899 ("prefix of % attribute cannot be overloaded", P
);
11903 if not Is_Entity_Name
(P
)
11904 or else not Is_Overloadable
(Entity
(P
))
11906 if not Is_Task_Type
(Etype
(P
))
11907 or else Nkind
(P
) = N_Explicit_Dereference
11913 -- If this is the name of a derived subprogram, or that of a
11914 -- generic actual, the address is that of the original entity.
11916 if Is_Entity_Name
(P
)
11917 and then Is_Overloadable
(Entity
(P
))
11918 and then Present
(Alias
(Entity
(P
)))
11921 New_Occurrence_Of
(Alias
(Entity
(P
)), Sloc
(P
)));
11924 if Is_Entity_Name
(P
) then
11925 Set_Address_Taken
(Entity
(P
));
11928 if Nkind
(P
) = N_Slice
then
11930 -- Arr (X .. Y)'address is identical to Arr (X)'address,
11931 -- even if the array is packed and the slice itself is not
11932 -- addressable. Transform the prefix into an indexed component.
11934 -- Note that the transformation is safe only if we know that
11935 -- the slice is non-null. That is because a null slice can have
11936 -- an out of bounds index value.
11938 -- Right now, gigi blows up if given 'Address on a slice as a
11939 -- result of some incorrect freeze nodes generated by the front
11940 -- end, and this covers up that bug in one case, but the bug is
11941 -- likely still there in the cases not handled by this code ???
11943 -- It's not clear what 'Address *should* return for a null
11944 -- slice with out of bounds indexes, this might be worth an ARG
11947 -- One approach would be to do a length check unconditionally,
11948 -- and then do the transformation below unconditionally, but
11949 -- analyze with checks off, avoiding the problem of the out of
11950 -- bounds index. This approach would interpret the address of
11951 -- an out of bounds null slice as being the address where the
11952 -- array element would be if there was one, which is probably
11953 -- as reasonable an interpretation as any ???
11956 Loc
: constant Source_Ptr
:= Sloc
(P
);
11957 D
: constant Node_Id
:= Discrete_Range
(P
);
11961 if Is_Entity_Name
(D
)
11964 (Type_Low_Bound
(Entity
(D
)),
11965 Type_High_Bound
(Entity
(D
)))
11968 Make_Attribute_Reference
(Loc
,
11969 Prefix
=> (New_Occurrence_Of
(Entity
(D
), Loc
)),
11970 Attribute_Name
=> Name_First
);
11972 elsif Nkind
(D
) = N_Range
11973 and then Not_Null_Range
(Low_Bound
(D
), High_Bound
(D
))
11975 Lo
:= Low_Bound
(D
);
11981 if Present
(Lo
) then
11983 Make_Indexed_Component
(Loc
,
11984 Prefix
=> Relocate_Node
(Prefix
(P
)),
11985 Expressions
=> New_List
(Lo
)));
11987 Analyze_And_Resolve
(P
);
11996 -- Prefix of Body_Version attribute can be a subprogram name which
11997 -- must not be resolved, since this is not a call.
11999 when Attribute_Body_Version
=>
12006 -- Prefix of Caller attribute is an entry name which must not
12007 -- be resolved, since this is definitely not an entry call.
12009 when Attribute_Caller
=>
12016 -- Shares processing with Address attribute
12022 -- If the prefix of the Count attribute is an entry name it must not
12023 -- be resolved, since this is definitely not an entry call. However,
12024 -- if it is an element of an entry family, the index itself may
12025 -- have to be resolved because it can be a general expression.
12027 when Attribute_Count
=>
12028 if Nkind
(P
) = N_Indexed_Component
12029 and then Is_Entity_Name
(Prefix
(P
))
12032 Indx
: constant Node_Id
:= First
(Expressions
(P
));
12033 Fam
: constant Entity_Id
:= Entity
(Prefix
(P
));
12035 Resolve
(Indx
, Entry_Index_Type
(Fam
));
12036 Apply_Scalar_Range_Check
(Indx
, Entry_Index_Type
(Fam
));
12044 -- Prefix of the Elaborated attribute is a subprogram name which
12045 -- must not be resolved, since this is definitely not a call. Note
12046 -- that it is a library unit, so it cannot be overloaded here.
12048 when Attribute_Elaborated
=>
12055 -- Prefix of Enabled attribute is a check name, which must be treated
12056 -- specially and not touched by Resolve.
12058 when Attribute_Enabled
=>
12065 -- Do not resolve the prefix of Loop_Entry, instead wait until the
12066 -- attribute has been expanded (see Expand_Loop_Entry_Attributes).
12067 -- The delay ensures that any generated checks or temporaries are
12068 -- inserted before the relocated prefix.
12070 when Attribute_Loop_Entry
=>
12073 --------------------
12074 -- Mechanism_Code --
12075 --------------------
12077 -- Prefix of the Mechanism_Code attribute is a function name
12078 -- which must not be resolved. Should we check for overloaded ???
12080 when Attribute_Mechanism_Code
=>
12087 -- Most processing is done in sem_dist, after determining the
12088 -- context type. Node is rewritten as a conversion to a runtime call.
12090 when Attribute_Partition_ID
=>
12091 Process_Partition_Id
(N
);
12098 when Attribute_Pool_Address
=>
12105 -- We replace the Range attribute node with a range expression whose
12106 -- bounds are the 'First and 'Last attributes applied to the same
12107 -- prefix. The reason that we do this transformation here instead of
12108 -- in the expander is that it simplifies other parts of the semantic
12109 -- analysis which assume that the Range has been replaced; thus it
12110 -- must be done even when in semantic-only mode (note that the RM
12111 -- specifically mentions this equivalence, we take care that the
12112 -- prefix is only evaluated once).
12114 when Attribute_Range
=> Range_Attribute
: declare
12120 if not Is_Entity_Name
(P
) or else not Is_Type
(Entity
(P
)) then
12123 -- If the prefix is a function call returning on the secondary
12124 -- stack, we must make sure to mark/release the stack.
12126 if Nkind
(P
) = N_Function_Call
12127 and then Nkind
(Parent
(N
)) = N_Loop_Parameter_Specification
12128 and then Requires_Transient_Scope
(Etype
(P
))
12130 Set_Uses_Sec_Stack
(Scope
(Current_Scope
));
12134 Dims
:= Expressions
(N
);
12137 Make_Attribute_Reference
(Loc
,
12138 Prefix
=> Duplicate_Subexpr
(P
, Name_Req
=> True),
12139 Attribute_Name
=> Name_Last
,
12140 Expressions
=> Dims
);
12143 Make_Attribute_Reference
(Loc
,
12145 Attribute_Name
=> Name_First
,
12146 Expressions
=> (Dims
));
12148 -- Do not share the dimension indicator, if present. Even though
12149 -- it is a static constant, its source location may be modified
12150 -- when printing expanded code and node sharing will lead to chaos
12153 if Present
(Dims
) then
12154 Set_Expressions
(LB
, New_List
(New_Copy_Tree
(First
(Dims
))));
12157 -- If the original was marked as Must_Not_Freeze (see code in
12158 -- Sem_Ch3.Make_Index), then make sure the rewriting does not
12161 if Must_Not_Freeze
(N
) then
12162 Set_Must_Not_Freeze
(HB
);
12163 Set_Must_Not_Freeze
(LB
);
12164 Set_Must_Not_Freeze
(Prefix
(HB
));
12165 Set_Must_Not_Freeze
(Prefix
(LB
));
12168 if Raises_Constraint_Error
(Prefix
(N
)) then
12170 -- Preserve Sloc of prefix in the new bounds, so that the
12171 -- posted warning can be removed if we are within unreachable
12174 Set_Sloc
(LB
, Sloc
(Prefix
(N
)));
12175 Set_Sloc
(HB
, Sloc
(Prefix
(N
)));
12178 Rewrite
(N
, Make_Range
(Loc
, LB
, HB
));
12179 Analyze_And_Resolve
(N
, Typ
);
12181 -- Ensure that the expanded range does not have side effects
12183 Force_Evaluation
(LB
);
12184 Force_Evaluation
(HB
);
12186 -- Normally after resolving attribute nodes, Eval_Attribute
12187 -- is called to do any possible static evaluation of the node.
12188 -- However, here since the Range attribute has just been
12189 -- transformed into a range expression it is no longer an
12190 -- attribute node and therefore the call needs to be avoided
12191 -- and is accomplished by simply returning from the procedure.
12194 end Range_Attribute
;
12200 when Attribute_Reduce
=>
12202 E1
: constant Node_Id
:= First
(Expressions
(N
));
12203 E2
: constant Node_Id
:= Next
(E1
);
12204 Op
: Entity_Id
:= Empty
;
12206 Index
: Interp_Index
;
12208 function Proper_Op
(Op
: Entity_Id
) return Boolean;
12214 function Proper_Op
(Op
: Entity_Id
) return Boolean is
12215 F1
, F2
: Entity_Id
;
12218 F1
:= First_Formal
(Op
);
12222 F2
:= Next_Formal
(F1
);
12224 or else Present
(Next_Formal
(F2
))
12228 elsif Ekind
(Op
) = E_Procedure
then
12229 return Ekind
(F1
) = E_In_Out_Parameter
12230 and then Covers
(Typ
, Etype
(F1
));
12234 (Ekind
(Op
) = E_Operator
12235 and then Scope
(Op
) = Standard_Standard
)
12236 or else Covers
(Typ
, Etype
(Op
));
12243 if Is_Overloaded
(E1
) then
12244 Get_First_Interp
(E1
, Index
, It
);
12245 while Present
(It
.Nam
) loop
12246 if Proper_Op
(It
.Nam
) then
12248 Set_Entity
(E1
, Op
);
12252 Get_Next_Interp
(Index
, It
);
12255 elsif Nkind
(E1
) = N_Attribute_Reference
12256 and then (Attribute_Name
(E1
) = Name_Max
12257 or else Attribute_Name
(E1
) = Name_Min
)
12261 elsif Proper_Op
(Entity
(E1
)) then
12263 Set_Etype
(N
, Typ
);
12267 Error_Msg_N
("No visible subprogram for reduction", E1
);
12275 -- We will only come here during the prescan of a spec expression
12276 -- containing a Result attribute. In that case the proper Etype has
12277 -- already been set, and nothing more needs to be done here.
12279 when Attribute_Result
=>
12282 ----------------------
12283 -- Unchecked_Access --
12284 ----------------------
12286 -- Processing is shared with Access
12288 -------------------------
12289 -- Unrestricted_Access --
12290 -------------------------
12292 -- Processing is shared with Access
12298 -- Resolve aggregate components in component associations
12300 when Attribute_Update
=> Update
: declare
12301 Aggr
: constant Node_Id
:= First
(Expressions
(N
));
12302 Typ
: constant Entity_Id
:= Etype
(Prefix
(N
));
12308 -- Set the Etype of the aggregate to that of the prefix, even
12309 -- though the aggregate may not be a proper representation of a
12310 -- value of the type (missing or duplicated associations, etc.)
12311 -- Complete resolution of the prefix. Note that in Ada 2012 it
12312 -- can be a qualified expression that is e.g. an aggregate.
12314 Set_Etype
(Aggr
, Typ
);
12315 Resolve
(Prefix
(N
), Typ
);
12317 -- For an array type, resolve expressions with the component type
12318 -- of the array, and apply constraint checks when needed.
12320 if Is_Array_Type
(Typ
) then
12321 Assoc
:= First
(Component_Associations
(Aggr
));
12322 while Present
(Assoc
) loop
12323 Expr
:= Expression
(Assoc
);
12324 Resolve
(Expr
, Component_Type
(Typ
));
12326 -- The choices in the association are static constants,
12327 -- or static aggregates each of whose components belongs
12328 -- to the proper index type. However, they must also
12329 -- belong to the index subtype (s) of the prefix, which
12330 -- may be a subtype (e.g. given by a slice).
12332 -- Choices may also be identifiers with no staticness
12333 -- requirements, in which case they must resolve to the
12342 C
:= First
(Choices
(Assoc
));
12343 while Present
(C
) loop
12344 Indx
:= First_Index
(Etype
(Prefix
(N
)));
12346 if Nkind
(C
) /= N_Aggregate
then
12347 Analyze_And_Resolve
(C
, Etype
(Indx
));
12349 C_E
:= First
(Expressions
(C
));
12350 while Present
(C_E
) loop
12351 Analyze_And_Resolve
(C_E
, Etype
(Indx
));
12365 -- For a record type, use type of each component, which is
12366 -- recorded during analysis.
12369 Assoc
:= First
(Component_Associations
(Aggr
));
12370 while Present
(Assoc
) loop
12371 Comp
:= First
(Choices
(Assoc
));
12372 Expr
:= Expression
(Assoc
);
12374 if Nkind
(Comp
) /= N_Others_Choice
12375 and then not Error_Posted
(Comp
)
12377 Resolve
(Expr
, Etype
(Entity
(Comp
)));
12389 -- Apply range check. Note that we did not do this during the
12390 -- analysis phase, since we wanted Eval_Attribute to have a
12391 -- chance at finding an illegal out of range value.
12393 when Attribute_Val
=>
12395 -- Note that we do our own Eval_Attribute call here rather than
12396 -- use the common one, because we need to do processing after
12397 -- the call, as per above comment.
12399 Eval_Attribute
(N
);
12401 -- Eval_Attribute may replace the node with a raise CE, or
12402 -- fold it to a constant. Obviously we only apply a scalar
12403 -- range check if this did not happen.
12405 if Nkind
(N
) = N_Attribute_Reference
12406 and then Attribute_Name
(N
) = Name_Val
12408 Apply_Scalar_Range_Check
(First
(Expressions
(N
)), Btyp
);
12417 -- Prefix of Version attribute can be a subprogram name which
12418 -- must not be resolved, since this is not a call.
12420 when Attribute_Version
=>
12423 ----------------------
12424 -- Other Attributes --
12425 ----------------------
12427 -- For other attributes, resolve prefix unless it is a type. If
12428 -- the attribute reference itself is a type name ('Base and 'Class)
12429 -- then this is only legal within a task or protected record.
12432 if not Is_Entity_Name
(P
) or else not Is_Type
(Entity
(P
)) then
12436 -- If the attribute reference itself is a type name ('Base,
12437 -- 'Class) then this is only legal within a task or protected
12438 -- record. What is this all about ???
12440 if Is_Entity_Name
(N
) and then Is_Type
(Entity
(N
)) then
12441 if Is_Concurrent_Type
(Entity
(N
))
12442 and then In_Open_Scopes
(Entity
(P
))
12447 ("invalid use of subtype name in expression or call", N
);
12451 -- For attributes whose argument may be a string, complete
12452 -- resolution of argument now. This avoids premature expansion
12453 -- (and the creation of transient scopes) before the attribute
12454 -- reference is resolved.
12457 when Attribute_Valid_Value | Attribute_Value
=>
12458 Resolve
(First
(Expressions
(N
)), Standard_String
);
12460 when Attribute_Wide_Value
=>
12461 Resolve
(First
(Expressions
(N
)), Standard_Wide_String
);
12463 when Attribute_Wide_Wide_Value
=>
12464 Resolve
(First
(Expressions
(N
)), Standard_Wide_Wide_String
);
12466 when others => null;
12469 -- If the prefix of the attribute is a class-wide type then it
12470 -- will be expanded into a dispatching call to a predefined
12471 -- primitive. Therefore we must check for potential violation
12472 -- of such restriction.
12474 if Is_Class_Wide_Type
(Etype
(P
)) then
12475 Check_Restriction
(No_Dispatching_Calls
, N
);
12479 -- Mark use clauses of the original prefix if the attribute is applied
12482 if Nkind
(Original_Node
(P
)) in N_Has_Entity
12483 and then Present
(Entity
(Original_Node
(P
)))
12485 Mark_Use_Clauses
(Original_Node
(P
));
12488 -- Normally the Freezing is done by Resolve but sometimes the Prefix
12489 -- is not resolved, in which case the freezing must be done now.
12491 -- For an elaboration check on a subprogram, we do not freeze its type.
12492 -- It may be declared in an unrelated scope, in particular in the case
12493 -- of a generic function whose type may remain unelaborated.
12495 if Attr_Id
= Attribute_Elaborated
then
12498 -- Should this be restricted to Expander_Active???
12501 Freeze_Expression
(P
);
12504 -- Finally perform static evaluation on the attribute reference
12506 Analyze_Dimension
(N
);
12507 Eval_Attribute
(N
);
12508 end Resolve_Attribute
;
12510 ------------------------
12511 -- Set_Boolean_Result --
12512 ------------------------
12514 procedure Set_Boolean_Result
(N
: Node_Id
; B
: Boolean) is
12515 Loc
: constant Source_Ptr
:= Sloc
(N
);
12518 Rewrite
(N
, New_Occurrence_Of
(Standard_True
, Loc
));
12520 Rewrite
(N
, New_Occurrence_Of
(Standard_False
, Loc
));
12522 end Set_Boolean_Result
;
12524 --------------------------------
12525 -- Stream_Attribute_Available --
12526 --------------------------------
12528 function Stream_Attribute_Available
12530 Nam
: TSS_Name_Type
;
12531 Partial_View
: Entity_Id
:= Empty
) return Boolean
12533 Etyp
: Entity_Id
:= Typ
;
12535 -- Start of processing for Stream_Attribute_Available
12538 -- We need some comments in this body ???
12540 if Has_Stream_Attribute_Definition
(Typ
, Nam
) then
12544 if Is_Class_Wide_Type
(Typ
) then
12545 return not Is_Limited_Type
(Typ
)
12546 or else Stream_Attribute_Available
(Etype
(Typ
), Nam
);
12549 if Nam
= TSS_Stream_Input
12550 and then Is_Abstract_Type
(Typ
)
12551 and then not Is_Class_Wide_Type
(Typ
)
12556 if not (Is_Limited_Type
(Typ
)
12557 or else (Present
(Partial_View
)
12558 and then Is_Limited_Type
(Partial_View
)))
12563 -- In Ada 2005, Input can invoke Read, and Output can invoke Write
12565 if Nam
= TSS_Stream_Input
12566 and then Ada_Version
>= Ada_2005
12567 and then Stream_Attribute_Available
(Etyp
, TSS_Stream_Read
)
12571 elsif Nam
= TSS_Stream_Output
12572 and then Ada_Version
>= Ada_2005
12573 and then Stream_Attribute_Available
(Etyp
, TSS_Stream_Write
)
12578 -- Case of Read and Write: check for attribute definition clause that
12579 -- applies to an ancestor type.
12581 while Etype
(Etyp
) /= Etyp
loop
12583 Derived_Type
: constant Entity_Id
:= Etyp
;
12585 Etyp
:= Etype
(Etyp
);
12587 if Has_Stream_Attribute_Definition
(Etyp
, Nam
) then
12588 if not Derivation_Too_Early_To_Inherit
(Derived_Type
, Nam
) then
12595 if Ada_Version
< Ada_2005
then
12597 -- In Ada 95 mode, also consider a non-visible definition
12600 Btyp
: constant Entity_Id
:= Implementation_Base_Type
(Typ
);
12603 and then Stream_Attribute_Available
12604 (Btyp
, Nam
, Partial_View
=> Typ
);
12609 end Stream_Attribute_Available
;